-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_comm_test.go
92 lines (81 loc) · 2.85 KB
/
multi_comm_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
//go:build integration
package goomerang_test
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
"go.eloylp.dev/goomerang/client"
"go.eloylp.dev/goomerang/example/protos"
"go.eloylp.dev/goomerang/internal/test"
"go.eloylp.dev/goomerang/message"
"go.eloylp.dev/goomerang/server"
)
func TestClientsCanInterceptClosedConnection(t *testing.T) {
t.Parallel()
s, run := Server(t)
run()
c1, connect1 := Client(t, client.WithServerAddr(s.Addr()))
defer c1.Close(defaultCtx)
connect1()
c2, connect2 := Client(t, client.WithServerAddr(s.Addr()))
defer c2.Close(defaultCtx)
connect2()
s.Shutdown(defaultCtx)
_, err := c1.Send(defaultMsg())
require.ErrorIs(t, err, client.ErrNotRunning, "expected client to intercept server close")
_, err = c2.Send(defaultMsg())
require.ErrorIs(t, err, client.ErrNotRunning, "expected client to intercept server close")
}
func TestServerSupportMultipleClients(t *testing.T) {
arbiter := test.NewArbiter(t)
s, run := Server(t, server.WithOnErrorHook(func(err error) {
arbiter.ErrorHappened(err)
}))
s.Handle(defaultMsg().Payload, message.HandlerFunc(func(ops message.Sender, msg *message.Message) {
pingMsg, ok := msg.Payload.(*protos.MessageV1)
if !ok {
arbiter.ErrorHappened(errors.New("cannot type assert message"))
return
}
arbiter.ItsAFactThat("SERVER_RECEIVED_FROM_CLIENT_" + pingMsg.Message)
payload := &protos.MessageV1{Message: pingMsg.Message}
_, err := ops.Send(message.New().SetPayload(payload))
if err != nil {
arbiter.ErrorHappened(err)
return
}
}))
run()
defer s.Shutdown(defaultCtx)
c1, connect1 := Client(t, client.WithServerAddr(s.Addr()))
c1.Handle(defaultMsg().Payload, message.HandlerFunc(func(ops message.Sender, msg *message.Message) {
pongMsg, ok := msg.Payload.(*protos.MessageV1)
if !ok {
arbiter.ErrorHappened(errors.New("cannot type assert message"))
return
}
arbiter.ItsAFactThat("CLIENT1_RECEIVED_FROM_SERVER_" + pongMsg.Message)
}))
connect1()
defer c1.Close(defaultCtx)
c2, connect2 := Client(t, client.WithServerAddr(s.Addr()))
c2.Handle(defaultMsg().Payload, message.HandlerFunc(func(ops message.Sender, msg *message.Message) {
pongMsg, ok := msg.Payload.(*protos.MessageV1)
if !ok {
arbiter.ErrorHappened(errors.New("cannot type assert message"))
return
}
arbiter.ItsAFactThat("CLIENT2_RECEIVED_FROM_SERVER_" + pongMsg.Message)
}))
connect2()
defer c2.Close(defaultCtx)
_, err := c1.Send(message.New().SetPayload(&protos.MessageV1{Message: "1"}))
require.NoError(t, err)
_, err = c2.Send(message.New().SetPayload(&protos.MessageV1{Message: "2"}))
require.NoError(t, err)
arbiter.RequireNoErrors()
arbiter.RequireHappened("SERVER_RECEIVED_FROM_CLIENT_1")
arbiter.RequireHappened("SERVER_RECEIVED_FROM_CLIENT_2")
arbiter.RequireHappened("CLIENT1_RECEIVED_FROM_SERVER_1")
arbiter.RequireHappened("CLIENT2_RECEIVED_FROM_SERVER_2")
}