-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration_test.go
133 lines (100 loc) · 2.62 KB
/
integration_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tankgo
import (
"context"
"testing"
"github.com/TheBestCo/tankgo/message"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type TankContainer struct {
testcontainers.Container
Port nat.Port
}
func setupTankContainer(ctx context.Context) (*TankContainer, error) {
req := testcontainers.ContainerRequest{
Image: "phaistos/tank",
ExposedPorts: []string{"11011"},
WaitingFor: wait.ForAll(
wait.ForLog("1 topics registered"),
wait.ForListeningPort("11011/tcp"),
),
Cmd: []string{
"/bin/sh", "-c",
"mkdir -p /data/test_topic/0; tank -p /data -l :11011",
},
}
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
p, err := c.MappedPort(ctx, "11011")
if err != nil {
return nil, err
}
return &TankContainer{Container: c, Port: p}, nil
}
func TestIntegration(t *testing.T) {
ctx := context.Background()
tankC, err := setupTankContainer(ctx)
assert.NoError(t, err)
defer tankC.Terminate(ctx)
// Send message
_, err = tankC.Exec(ctx, []string{"/usr/local/bin/tank-cli", "-t", "test_topic", "produce", "one", "two", "three"})
assert.NoError(t, err)
req := message.ConsumeRequest{
ClientVersion: 2,
RequestID: 123,
Client: "test_case_1",
MaxWaitMS: 0,
MinBytes: 0,
Topics: []message.FetchRequestTopic{
{
Name: "test_topic",
Partitions: []message.FetchRequestTopicPartition{
{
PartitionID: 0,
ABSSequenceNumber: 1,
FetchSize: 1024,
},
},
},
},
}
s := &TankSubscriber{}
err = s.Connect(ctx, "127.0.0.1:"+tankC.Port.Port(), DefaultConTimeout, DefaultBufSize)
assert.NoError(t, err)
defer s.Close()
err = s.Ping()
assert.NoError(t, err)
highWaterMarkMap, err := s.GetTopicsHighWaterMark(&req)
assert.NoError(t, err)
assert.Equal(t, uint64(3), highWaterMarkMap["test_topic"])
err = s.Reset(ctx)
assert.NoError(t, err)
err = s.Ping()
assert.NoError(t, err)
messages, errChan := s.Subscribe(&req, 10)
assert.NoError(t, err)
msgList := make([]message.Log, 0, 10)
done := make(chan bool, 1)
go func() {
for m := range messages {
msgList = append(msgList, m)
}
done <- true
}()
<-done
err = <-errChan
if err != nil {
t.Fatal(err)
}
assert.True(t, len(msgList) == 3)
assert.Equal(t, "one", string(msgList[0].Payload))
assert.Equal(t, "two", string(msgList[1].Payload))
assert.Equal(t, "three", string(msgList[2].Payload))
}