forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Achille Roussel
committed
Jun 21, 2017
1 parent
b2e5925
commit 0ea8d5d
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package kafka | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestWriter(t *testing.T) { | ||
tests := []struct { | ||
scenario string | ||
function func(*testing.T) | ||
}{ | ||
{ | ||
scenario: "closing a writer right after creating it returns promptly with no error", | ||
function: testWriterClose, | ||
}, | ||
|
||
{ | ||
scenario: "writing 1 message through a writer using round-robin balancing produces 1 message to the first partition", | ||
function: testWriterRoundRobin1, | ||
}, | ||
} | ||
|
||
t.Parallel() | ||
// Kafka takes a while to create the initial topics and partitions... | ||
time.Sleep(15 * time.Second) | ||
|
||
for _, test := range tests { | ||
testFunc := test.function | ||
t.Run(test.scenario, func(t *testing.T) { | ||
t.Parallel() | ||
testFunc(t) | ||
}) | ||
} | ||
} | ||
|
||
func newTestWriter(config WriterConfig) *Writer { | ||
config.Brokers = []string{"localhost:9092"} | ||
return NewWriter(config) | ||
} | ||
|
||
func testWriterClose(t *testing.T) { | ||
w := newTestWriter(WriterConfig{ | ||
Topic: "test-writer-0", | ||
}) | ||
|
||
if err := w.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func testWriterRoundRobin1(t *testing.T) { | ||
const topic = "test-writer-1" | ||
|
||
offset, err := readOffset(topic, 0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
w := newTestWriter(WriterConfig{ | ||
Topic: topic, | ||
Balancer: &RoundRobin{}, | ||
}) | ||
defer w.Close() | ||
|
||
if err := w.WriteMessages(context.Background(), Message{ | ||
Value: []byte("Hello World!"), | ||
}); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
msgs, err := readPartition(topic, 0, offset) | ||
|
||
if err != nil { | ||
t.Error("error reading partition", err) | ||
return | ||
} | ||
|
||
if len(msgs) != 1 { | ||
t.Error("bad messages in partition", msgs) | ||
return | ||
} | ||
|
||
for _, m := range msgs { | ||
if string(m.Value) != "Hello World!" { | ||
t.Error("bad messages in partition", msgs) | ||
break | ||
} | ||
} | ||
} | ||
|
||
func readOffset(topic string, partition int) (offset int64, err error) { | ||
var conn *Conn | ||
|
||
if conn, err = DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition); err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
offset, err = conn.ReadLastOffset() | ||
return | ||
} | ||
|
||
func readPartition(topic string, partition int, offset int64) (msgs []Message, err error) { | ||
var conn *Conn | ||
|
||
if conn, err = DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition); err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
conn.Seek(offset, 1) | ||
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) | ||
batch := conn.ReadBatch(0, 1000000000) | ||
defer batch.Close() | ||
|
||
for { | ||
var msg Message | ||
|
||
if msg, err = batch.ReadMessage(); err != nil { | ||
if err == io.EOF { | ||
err = nil | ||
} | ||
return | ||
} | ||
|
||
msgs = append(msgs, msg) | ||
} | ||
} |