@@ -105,7 +105,6 @@ func TestWriter(t *testing.T) {
105
105
scenario : "closing a writer right after creating it returns promptly with no error" ,
106
106
function : testWriterClose ,
107
107
},
108
-
109
108
{
110
109
scenario : "writing 1 message through a writer using round-robin balancing produces 1 message to the first partition" ,
111
110
function : testWriterRoundRobin1 ,
@@ -130,6 +129,10 @@ func TestWriter(t *testing.T) {
130
129
scenario : "writing a batch of messages" ,
131
130
function : testWriterBatchSize ,
132
131
},
132
+ {
133
+ scenario : "writing and flushing a batch of messages" ,
134
+ function : testsWriterFlush ,
135
+ },
133
136
134
137
{
135
138
scenario : "writing messages with a small batch byte size" ,
@@ -450,7 +453,7 @@ func readPartition(topic string, partition int, offset int64) (msgs []Message, e
450
453
}
451
454
}
452
455
453
- func testWriterBatchBytes (t * testing.T ) {
456
+ func testsWriterFlush (t * testing.T ) {
454
457
topic := makeTopic ()
455
458
createTopic (t , topic , 1 )
456
459
defer deleteTopic (t , topic )
@@ -461,10 +464,13 @@ func testWriterBatchBytes(t *testing.T) {
461
464
}
462
465
463
466
w := newTestWriter (WriterConfig {
464
- Topic : topic ,
465
- BatchBytes : 50 ,
466
- BatchTimeout : math .MaxInt32 * time .Second ,
467
+ Topic : topic ,
468
+ // Set the batch timeout to a large value to avoid the timeout
469
+ BatchSize : 1000 ,
470
+ BatchBytes : 1000000 ,
471
+ BatchTimeout : 1000 * time .Second ,
467
472
Balancer : & RoundRobin {},
473
+ Async : true ,
468
474
})
469
475
defer w .Close ()
470
476
@@ -480,7 +486,65 @@ func testWriterBatchBytes(t *testing.T) {
480
486
return
481
487
}
482
488
483
- if w .Stats ().Writes != 2 {
489
+ if err := w .Flush (ctx ); err != nil {
490
+ t .Errorf ("flush error %v" , err )
491
+ return
492
+ }
493
+
494
+ if w .Stats ().Writes != 1 {
495
+ t .Error ("didn't create expected batches" )
496
+ return
497
+ }
498
+ msgs , err := readPartition (topic , 0 , offset )
499
+ if err != nil {
500
+ t .Error ("error reading partition" , err )
501
+ return
502
+ }
503
+
504
+ if len (msgs ) != 4 {
505
+ t .Error ("bad messages in partition" , msgs )
506
+ return
507
+ }
508
+
509
+ for i , m := range msgs {
510
+ if string (m .Value ) == "M" + strconv .Itoa (i ) {
511
+ continue
512
+ }
513
+ t .Error ("bad messages in partition" , string (m .Value ))
514
+ }
515
+ }
516
+
517
+ func testWriterBatchBytes (t * testing.T ) {
518
+ topic := makeTopic ()
519
+ createTopic (t , topic , 1 )
520
+ defer deleteTopic (t , topic )
521
+
522
+ offset , err := readOffset (topic , 0 )
523
+ if err != nil {
524
+ t .Fatal (err )
525
+ }
526
+
527
+ w := newTestWriter (WriterConfig {
528
+ Topic : topic ,
529
+ BatchBytes : 50 ,
530
+ BatchTimeout : math .MaxInt32 * time .Second ,
531
+ Balancer : & RoundRobin {},
532
+ })
533
+ defer w .Close ()
534
+
535
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
536
+ defer cancel ()
537
+ if err := w .WriteMessages (ctx , []Message {
538
+ {Value : []byte ("M0" )},
539
+ {Value : []byte ("M1" )},
540
+ {Value : []byte ("M2" )},
541
+ {Value : []byte ("M3" )},
542
+ }... ); err != nil {
543
+ t .Error (err )
544
+ return
545
+ }
546
+
547
+ if w .Stats ().Writes != 1 {
484
548
t .Error ("didn't create expected batches" )
485
549
return
486
550
}
0 commit comments