-
Notifications
You must be signed in to change notification settings - Fork 16
/
server_test.go
153 lines (127 loc) · 3.87 KB
/
server_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package proximo
import (
"context"
"fmt"
"log"
"net"
"os"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/uw-labs/proximo/backend/mock"
"github.com/uw-labs/proximo/proto"
"github.com/uw-labs/substrate"
proximoc "github.com/uw-labs/substrate/proximo"
"io"
)
var (
backend *mock.Backend
grpcServer *grpc.Server
)
func Setup() error {
lis, err := net.Listen("tcp", ":6868")
if err != nil {
return errors.Wrap(err, "failed to listen")
}
opts := []grpc.ServerOption{
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: 5 * time.Minute,
}),
}
grpcServer = grpc.NewServer(opts...)
backend = mock.NewBackend()
proto.RegisterMessageSourceServer(grpcServer, &SourceServer{SourceFactory: backend})
proto.RegisterMessageSinkServer(grpcServer, &SinkServer{SinkFactory: backend})
go func() {
if err := grpcServer.Serve(lis); err != nil {
log.Printf("gRPC server stopped with error: %s", err)
}
}()
// Wait for server to start
cc, err := grpc.Dial("localhost:6868", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
return errors.Wrap(err, "failed to open client connection")
}
return cc.Close()
}
func TestMain(m *testing.M) {
if err := Setup(); err != nil {
fmt.Fprintf(os.Stderr, "testing: failed to setup tests: %v", err)
os.Exit(1)
}
exitCode := m.Run()
grpcServer.Stop() // This will also close the listener
os.Exit(exitCode)
}
func TestProduceServer_Publish(t *testing.T) {
assert := require.New(t)
toPublish := [][]byte{
[]byte("publish-message-1"),
[]byte("publish-message-2"),
[]byte("publish-message-3"),
[]byte("publish-message-4"),
[]byte("publish-message-5"),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
sink, err := proximoc.NewAsyncMessageSink(proximoc.AsyncMessageSinkConfig{
Broker: "localhost:6868",
Topic: "publish-test",
Insecure: true,
})
assert.NoError(err)
client := substrate.NewSynchronousMessageSink(sink)
defer client.Close()
for _, data := range toPublish {
assert.NoError(client.PublishMessage(ctx, &substrateMsg{data: data}))
}
assert.NoError(client.Close())
published := backend.GetTopic("publish-test")
assert.Equal(len(toPublish), len(published))
for i, msg := range toPublish {
assert.Equal(msg, published[i].Data())
}
}
func TestConsumeServer_Consume(t *testing.T) {
assert := require.New(t)
expected := []substrate.Message{
&substrateMsg{data: []byte("consume-message-1")},
&substrateMsg{data: []byte("consume-message-2")},
&substrateMsg{data: []byte("consume-message-3")},
&substrateMsg{data: []byte("consume-message-4")},
&substrateMsg{data: []byte("consume-message-5")},
}
backend.SetTopic("consume-test", expected)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
source, err := proximoc.NewAsyncMessageSource(proximoc.AsyncMessageSourceConfig{
Broker: "localhost:6868",
Topic: "consume-test",
ConsumerGroup: "test-consumer-group",
Insecure: true,
})
assert.NoError(err)
client := substrate.NewSynchronousMessageSource(source)
defer func() { assert.NoError(client.Close()) }()
consumed := make([]substrate.Message, 0, len(expected))
err = client.ConsumeMessages(ctx, func(_ context.Context, msg substrate.Message) error {
consumed = append(consumed, msg)
return nil
})
/* the substrate proximo source will return EOF, as the mock backend used in the test is a finite one, not like a real kafka one that never "ends" */
assert.Error(io.EOF)
assert.Equal(len(expected), len(consumed))
for i, msg := range expected {
assert.Equal(msg.Data(), consumed[i].Data())
}
}
type substrateMsg struct {
data []byte
}
func (msg *substrateMsg) Data() []byte {
return msg.data
}