@@ -88,19 +88,24 @@ func testEncodeDecode(t *testing.T, m kafka.Message, codec pkg.Codec) {
88
88
t .Run ("encode with " + codec .Name (), func (t * testing.T ) {
89
89
r1 , err = compress (codec , m .Value )
90
90
if err != nil {
91
- t .Error (err )
91
+ t .Fatal (err )
92
92
}
93
93
})
94
94
95
95
t .Run ("decode with " + codec .Name (), func (t * testing.T ) {
96
+ if r1 == nil {
97
+ if r1 , err = compress (codec , m .Value ); err != nil {
98
+ t .Fatal (err )
99
+ }
100
+ }
96
101
r2 , err = decompress (codec , r1 )
97
102
if err != nil {
98
- t .Error (err )
103
+ t .Fatal (err )
99
104
}
100
105
if string (r2 ) != "message" {
101
106
t .Error ("bad message" )
102
- t .Log ( "got: " , string (r2 ))
103
- t .Log ( "expected: " , string (m . Value ))
107
+ t .Logf ( "expected: %q " , string (m . Value ))
108
+ t .Logf ( "got: %q " , string (r2 ))
104
109
}
105
110
})
106
111
}
@@ -116,15 +121,16 @@ func TestCompressedMessages(t *testing.T) {
116
121
}
117
122
118
123
func testCompressedMessages (t * testing.T , codec pkg.Codec ) {
119
- t .Run ("produce/consume with" + codec .Name (), func (t * testing.T ) {
120
- topic := createTopic ( t , 1 )
121
- defer deleteTopic ( t , topic )
124
+ t .Run (codec .Name (), func (t * testing.T ) {
125
+ client , topic , shutdown := newLocalClientAndTopic ( )
126
+ defer shutdown ( )
122
127
123
128
w := & kafka.Writer {
124
129
Addr : kafka .TCP ("127.0.0.1:9092" ),
125
130
Topic : topic ,
126
131
Compression : kafka .Compression (codec .Code ()),
127
132
BatchTimeout : 10 * time .Millisecond ,
133
+ Transport : client .Transport ,
128
134
}
129
135
defer w .Close ()
130
136
@@ -185,19 +191,23 @@ func testCompressedMessages(t *testing.T, codec pkg.Codec) {
185
191
}
186
192
187
193
func TestMixedCompressedMessages (t * testing.T ) {
188
- topic := createTopic ( t , 1 )
189
- defer deleteTopic ( t , topic )
194
+ client , topic , shutdown := newLocalClientAndTopic ( )
195
+ defer shutdown ( )
190
196
191
197
offset := 0
192
198
var values []string
193
199
produce := func (n int , codec pkg.Codec ) {
194
200
w := & kafka.Writer {
195
- Addr : kafka .TCP ("127.0.0.1:9092" ),
196
- Topic : topic ,
197
- Compression : kafka . Compression ( codec . Code ()) ,
201
+ Addr : kafka .TCP ("127.0.0.1:9092" ),
202
+ Topic : topic ,
203
+ Transport : client . Transport ,
198
204
}
199
205
defer w .Close ()
200
206
207
+ if codec != nil {
208
+ w .Compression = kafka .Compression (codec .Code ())
209
+ }
210
+
201
211
msgs := make ([]kafka.Message , n )
202
212
for i := range msgs {
203
213
value := fmt .Sprintf ("Hello World %d!" , offset )
@@ -407,58 +417,72 @@ func benchmarkCompression(b *testing.B, codec pkg.Codec, buf *bytes.Buffer, payl
407
417
return 1 - (float64 (buf .Len ()) / float64 (len (payload )))
408
418
}
409
419
420
+ func init () {
421
+ rand .Seed (time .Now ().UnixNano ())
422
+ }
423
+
410
424
func makeTopic () string {
411
425
return fmt .Sprintf ("kafka-go-%016x" , rand .Int63 ())
412
426
}
413
427
414
- func createTopic ( t * testing. T , partitions int ) string {
428
+ func newLocalClientAndTopic () ( * kafka. Client , string , func ()) {
415
429
topic := makeTopic ()
416
-
417
- conn , err := kafka .Dial ("tcp" , "localhost:9092" )
430
+ client , shutdown := newLocalClient ()
431
+
432
+ _ , err := client .CreateTopics (context .Background (), & kafka.CreateTopicsRequest {
433
+ Topics : []kafka.TopicConfig {{
434
+ Topic : topic ,
435
+ NumPartitions : 1 ,
436
+ ReplicationFactor : 1 ,
437
+ }},
438
+ })
418
439
if err != nil {
419
- t .Fatal (err )
440
+ shutdown ()
441
+ panic (err )
420
442
}
421
- defer conn .Close ()
422
443
423
- err = conn .CreateTopics (kafka.TopicConfig {
424
- Topic : topic ,
425
- NumPartitions : partitions ,
426
- ReplicationFactor : 1 ,
427
- })
444
+ // Topic creation seems to be asynchronous. Metadata for the topic partition
445
+ // layout in the cluster is available in the controller before being synced
446
+ // with the other brokers, which causes "Error:[3] Unknown Topic Or Partition"
447
+ // when sending requests to the partition leaders.
448
+ for i := 0 ; i < 20 ; i ++ {
449
+ r , err := client .Fetch (context .Background (), & kafka.FetchRequest {
450
+ Topic : topic ,
451
+ Partition : 0 ,
452
+ Offset : 0 ,
453
+ })
454
+ if err == nil && r .Error == nil {
455
+ break
456
+ }
457
+ time .Sleep (100 * time .Millisecond )
458
+ }
428
459
429
- switch err {
430
- case nil :
431
- // ok
432
- case kafka .TopicAlreadyExists :
433
- // ok
434
- default :
435
- t .Error ("bad createTopics" , err )
436
- t .FailNow ()
460
+ return client , topic , func () {
461
+ client .DeleteTopics (context .Background (), & kafka.DeleteTopicsRequest {
462
+ Topics : []string {topic },
463
+ })
464
+ shutdown ()
437
465
}
466
+ }
438
467
439
- return topic
468
+ func newLocalClient () (* kafka.Client , func ()) {
469
+ return newClient (kafka .TCP ("127.0.0.1:9092" ))
440
470
}
441
471
442
- func deleteTopic (t * testing.T , topic ... string ) {
443
- conn , err := kafka .Dial ("tcp" , "localhost:9092" )
444
- if err != nil {
445
- t .Fatal (err )
472
+ func newClient (addr net.Addr ) (* kafka.Client , func ()) {
473
+ conns := & ktesting.ConnWaitGroup {
474
+ DialFunc : (& net.Dialer {}).DialContext ,
446
475
}
447
- defer conn .Close ()
448
476
449
- controller , err := conn .Controller ()
450
- if err != nil {
451
- t .Fatal (err )
477
+ transport := & kafka.Transport {
478
+ Dial : conns .Dial ,
452
479
}
453
480
454
- conn , err = kafka .Dial ("tcp" , net .JoinHostPort (controller .Host , strconv .Itoa (controller .Port )))
455
- if err != nil {
456
- t .Fatal (err )
481
+ client := & kafka.Client {
482
+ Addr : addr ,
483
+ Timeout : 5 * time .Second ,
484
+ Transport : transport ,
457
485
}
458
486
459
- conn .SetDeadline (time .Now ().Add (2 * time .Second ))
460
-
461
- if err := conn .DeleteTopics (topic ... ); err != nil {
462
- t .Fatal (err )
463
- }
487
+ return client , func () { transport .CloseIdleConnections (); conns .Wait () }
464
488
}
0 commit comments