-
Notifications
You must be signed in to change notification settings - Fork 172
/
producer_test.go
68 lines (59 loc) · 1.52 KB
/
producer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// +build goka
package goka
import (
"log"
"sync/atomic"
"testing"
"time"
)
// This tests how a producer behaves when being shutdown to make sure,
// no promises stay unfinished.
// To run the test, get a local kafka-container running (e.g. go to
// examples-directory and do `make restart`), then run the tests with
// `go test -v github.com/lovoo/goka/kafka/ -tags=kafka`
func TestProducerError(t *testing.T) {
cfg := DefaultConfig()
p, err := NewProducer([]string{"localhost:9092"}, cfg)
if err != nil {
t.Fatalf("error creating producer: %v", err)
}
var (
promises []*Promise
donePromises int64
emitted int64
done = make(chan bool)
)
go func() {
defer func() {
recover()
}()
defer close(done)
for {
promise := p.Emit("test", "test", []byte{})
promise.Then(func(err error) {
atomic.AddInt64(&donePromises, 1)
if err != nil {
log.Printf("error producing message: %v", err)
}
})
promises = append(promises, promise)
emitted++
time.Sleep(20 * time.Millisecond)
}
}()
// let it run for 1 second
time.Sleep(1000 * time.Millisecond)
// close the producer
err = p.Close()
if err != nil {
log.Printf("Error closing producer: %v", err)
}
// wait for the promises to be
<-done
if len(promises) != int(emitted) {
t.Errorf("Promises/Emits do not match: promises: %d, emitted. %d", len(promises), emitted)
}
if len(promises) != int(donePromises) {
t.Errorf("Promises/Done promises do not match: promises: %d, done. %d", len(promises), donePromises)
}
}