@@ -99,7 +99,7 @@ func TestBulkIndexer(t *testing.T) {
9999
100100 cfg := BulkIndexerConfig {
101101 NumWorkers : 1 ,
102- FlushBytes : 38 * 2 , // 38 bytes header + body, times 2 to match 2 responses per file in testdata
102+ FlushBytes : 39 * 2 , // 38 bytes header + body, times 2 to match 2 responses per file in testdata
103103 FlushInterval : time .Hour , // Disable auto-flushing, because response doesn't match number of items
104104 Client : es }
105105 if os .Getenv ("DEBUG" ) != "" {
@@ -576,7 +576,7 @@ func TestBulkIndexer(t *testing.T) {
576576 }
577577 es , _ := elasticsearch .NewClient (esCfg )
578578
579- biCfg := BulkIndexerConfig {NumWorkers : 1 , FlushBytes : 50 , Client : es }
579+ biCfg := BulkIndexerConfig {NumWorkers : 1 , FlushBytes : 28 * 2 , Client : es }
580580 if os .Getenv ("DEBUG" ) != "" {
581581 biCfg .DebugLogger = log .New (os .Stdout , "" , 0 )
582582 }
@@ -955,6 +955,36 @@ func TestBulkIndexer(t *testing.T) {
955955 })
956956}
957957
958+ func TestBulkIndexerItem (t * testing.T ) {
959+ body := `{"body":"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."}`
960+ t .Run ("correct computeLength size" , func (t * testing.T ) {
961+ expectedLength := 266
962+ bi := BulkIndexerItem {
963+ Action : "index" ,
964+ DocumentID : strconv .Itoa (1 ),
965+ Body : strings .NewReader (body ),
966+ }
967+ bi .marshallMeta ()
968+ bi .computeLength ()
969+ if bi .payloadLength != expectedLength {
970+ t .Fatalf ("invalid length, expected %d, got %d" , expectedLength , bi .payloadLength )
971+ }
972+ })
973+ t .Run ("empty reader length should be meta length plus newlines" , func (t * testing.T ) {
974+ expectedLength := 23
975+ bi := BulkIndexerItem {
976+ Action : "index" ,
977+ DocumentID : strconv .Itoa (1 ),
978+ Body : strings .NewReader ("" ),
979+ }
980+ bi .marshallMeta ()
981+ bi .computeLength ()
982+ if bi .payloadLength != expectedLength {
983+ t .Fatalf ("invalid length, expected %d, got %d" , expectedLength , bi .payloadLength )
984+ }
985+ })
986+ }
987+
958988type customJSONDecoder struct {}
959989
960990func (d customJSONDecoder ) UnmarshalFromReader (r io.Reader , blk * BulkIndexerResponse ) error {
0 commit comments