-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_test.go
106 lines (97 loc) · 2.99 KB
/
registration_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
//go:build integration
package goomerang_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.eloylp.dev/goomerang/client"
"go.eloylp.dev/goomerang/example/protos"
"go.eloylp.dev/goomerang/message"
"go.eloylp.dev/goomerang/server"
)
func TestHandlerRegistrationMoment(t *testing.T) {
t.Run("Client CAN register multiple handlers BEFORE run", func(t *testing.T) {
c, err := client.New()
require.NoError(t, err)
registerClientDumbHandler(c)
registerClientDumbHandler(c)
})
t.Run("Client CANNOT register handlers AFTER run", func(t *testing.T) {
s, run := Server(t)
run()
defer s.Shutdown(defaultCtx)
c, connect := Client(t, client.WithServerAddr(s.Addr()))
connect()
defer c.Close(defaultCtx)
assert.Panics(t, func() {
registerClientDumbHandler(c)
})
})
t.Run("Client CAN register multiple middlewares BEFORE run", func(t *testing.T) {
c, err := client.New()
require.NoError(t, err)
registerClientDumbMiddleware(c)
registerClientDumbMiddleware(c)
})
t.Run("Client CANNOT register middlewares AFTER run", func(t *testing.T) {
s, run := Server(t)
run()
defer s.Shutdown(defaultCtx)
c, connect := Client(t, client.WithServerAddr(s.Addr()))
connect()
defer c.Close(defaultCtx)
assert.Panics(t, func() {
registerClientDumbMiddleware(c)
})
})
t.Run("Server CAN register multiple handlers BEFORE run", func(t *testing.T) {
s, err := server.New()
require.NoError(t, err)
registerServerDumbHandler(s)
registerServerDumbHandler(s)
})
t.Run("Server CANNOT register handlers AFTER run", func(t *testing.T) {
s, run := Server(t)
run()
defer s.Shutdown(defaultCtx)
c, connect := Client(t, client.WithServerAddr(s.Addr()))
connect()
defer c.Close(defaultCtx)
assert.Panics(t, func() {
registerServerDumbHandler(s)
})
})
t.Run("Server CAN register multiple middlewares BEFORE run", func(t *testing.T) {
s, err := server.New()
require.NoError(t, err)
registerServerDumbMiddleware(s)
registerServerDumbMiddleware(s)
})
t.Run("Server CANNOT register middlewares AFTER run", func(t *testing.T) {
s, run := Server(t)
run()
defer s.Shutdown(defaultCtx)
c, connect := Client(t, client.WithServerAddr(s.Addr()))
connect()
defer c.Close(defaultCtx)
assert.Panics(t, func() {
registerServerDumbMiddleware(s)
})
})
}
func registerClientDumbHandler(c *client.Client) {
c.Handle(&protos.MessageV1{}, message.HandlerFunc(func(s message.Sender, w *message.Message) {}))
}
func registerClientDumbMiddleware(c *client.Client) {
c.Middleware(func(h message.Handler) message.Handler {
return message.HandlerFunc(func(s message.Sender, w *message.Message) {})
})
}
func registerServerDumbHandler(s *server.Server) {
s.Handle(&protos.MessageV1{}, message.HandlerFunc(func(s message.Sender, w *message.Message) {}))
}
func registerServerDumbMiddleware(s *server.Server) {
s.Middleware(func(h message.Handler) message.Handler {
return message.HandlerFunc(func(s message.Sender, w *message.Message) {})
})
}