@@ -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 : testWriterBatchSize ,
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 tetsWriterFlush (t * testing.T ) {
454
457
topic := makeTopic ()
455
458
createTopic (t , topic , 1 )
456
459
defer deleteTopic (t , topic )
@@ -461,9 +464,11 @@ 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 {},
468
473
})
469
474
defer w .Close ()
@@ -480,6 +485,11 @@ func testWriterBatchBytes(t *testing.T) {
480
485
return
481
486
}
482
487
488
+ if err := w .Flush (ctx ); err != nil {
489
+ t .Errorf ("flush error %v" , err )
490
+ return
491
+ }
492
+
483
493
if w .Stats ().Writes != 2 {
484
494
t .Error ("didn't create expected batches" )
485
495
return
@@ -503,6 +513,59 @@ func testWriterBatchBytes(t *testing.T) {
503
513
}
504
514
}
505
515
516
+ func testWriterBatchBytes (t * testing.T ) {
517
+ topic := makeTopic ()
518
+ createTopic (t , topic , 1 )
519
+ defer deleteTopic (t , topic )
520
+
521
+ offset , err := readOffset (topic , 0 )
522
+ if err != nil {
523
+ t .Fatal (err )
524
+ }
525
+
526
+ w := newTestWriter (WriterConfig {
527
+ Topic : topic ,
528
+ BatchBytes : 50 ,
529
+ BatchTimeout : math .MaxInt32 * time .Second ,
530
+ Balancer : & RoundRobin {},
531
+ })
532
+ defer w .Close ()
533
+
534
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
535
+ defer cancel ()
536
+ if err := w .WriteMessages (ctx , []Message {
537
+ {Value : []byte ("M0" )},
538
+ {Value : []byte ("M1" )},
539
+ {Value : []byte ("M2" )},
540
+ {Value : []byte ("M3" )},
541
+ }... ); err != nil {
542
+ t .Error (err )
543
+ return
544
+ }
545
+
546
+ if w .Stats ().Writes != 1 {
547
+ t .Error ("didn't create expected batches" )
548
+ return
549
+ }
550
+ msgs , err := readPartition (topic , 0 , offset )
551
+ if err != nil {
552
+ t .Error ("error reading partition" , err )
553
+ return
554
+ }
555
+
556
+ if len (msgs ) != 4 {
557
+ t .Error ("bad messages in partition" , msgs )
558
+ return
559
+ }
560
+
561
+ for i , m := range msgs {
562
+ if string (m .Value ) == "M" + strconv .Itoa (i ) {
563
+ continue
564
+ }
565
+ t .Error ("bad messages in partition" , string (m .Value ))
566
+ }
567
+ }
568
+
506
569
func testWriterBatchSize (t * testing.T ) {
507
570
topic := makeTopic ()
508
571
createTopic (t , topic , 1 )
0 commit comments