-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhemera_test.go
152 lines (108 loc) · 2.86 KB
/
hemera_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
package hemera
import (
"fmt"
"testing"
"time"
natsServer "github.com/nats-io/gnatsd/server"
gnatsd "github.com/nats-io/gnatsd/test"
nats "github.com/nats-io/go-nats"
"github.com/stretchr/testify/assert"
)
const TEST_PORT = 8368
var reconnectOpts = nats.Options{
Url: fmt.Sprintf("nats://localhost:%d", TEST_PORT),
AllowReconnect: true,
MaxReconnect: 10,
ReconnectWait: 100 * time.Millisecond,
Timeout: nats.DefaultTimeout,
}
func RunServerOnPort(port int) *natsServer.Server {
opts := gnatsd.DefaultTestOptions
opts.Port = port
return RunServerWithOptions(opts)
}
func RunServerWithOptions(opts natsServer.Options) *natsServer.Server {
return gnatsd.RunServer(&opts)
}
type MathPattern struct {
Topic string
Cmd string
}
type RequestPattern struct {
Topic string
Cmd string
A int
B int
}
type Response struct {
Result int
}
func TestCreateHemera(t *testing.T) {
assert := assert.New(t)
ts := RunServerOnPort(TEST_PORT)
defer ts.Shutdown()
opts := reconnectOpts
nc, err := opts.Connect()
defer nc.Close()
if err != nil {
panic(err)
}
h, _ := CreateHemera(nc)
assert.NotEqual(h, nil, "they should not nil")
}
func TestAdd(t *testing.T) {
assert := assert.New(t)
ts := RunServerOnPort(TEST_PORT)
defer ts.Shutdown()
opts := reconnectOpts
nc, err := opts.Connect()
defer nc.Close()
if err != nil {
panic(err)
}
h, _ := CreateHemera(nc)
pattern := MathPattern{Topic: "math", Cmd: "add"}
h.Add(pattern, func(req *RequestPattern, reply Reply, context *Context) {
reply.Send(Response{Result: req.A + req.B})
})
assert.Equal(len(h.Router.List()), 1, "Should be 1")
}
func TestActRequest(t *testing.T) {
ts := RunServerOnPort(TEST_PORT)
defer ts.Shutdown()
opts := reconnectOpts
nc, err := opts.Connect()
defer nc.Close()
if err != nil {
panic(err)
}
h, _ := CreateHemera(nc)
pattern := MathPattern{Topic: "math", Cmd: "add"}
h.Add(pattern, func(req *RequestPattern, reply Reply, context *Context) {
reply.Send(Response{Result: req.A + req.B})
})
requestPattern := RequestPattern{Topic: "math", Cmd: "add", A: 1, B: 2}
res := &Response{}
h.Act(requestPattern, res)
assert.Equal(t, res.Result, 3, "Should be 3")
}
func TestNoDuplicatesAllowed(t *testing.T) {
assert := assert.New(t)
ts := RunServerOnPort(TEST_PORT)
defer ts.Shutdown()
opts := reconnectOpts
nc, err := opts.Connect()
defer nc.Close()
if err != nil {
panic(err)
}
h, _ := CreateHemera(nc)
pattern := MathPattern{Topic: "math", Cmd: "add"}
h.Add(pattern, func(req *RequestPattern, reply Reply, context Context) {
reply.Send(Response{Result: req.A + req.B})
})
_, errAdd := h.Add(pattern, func(req *RequestPattern, reply Reply, context Context) {
reply.Send(Response{Result: req.A + req.B})
})
assert.Equal(errAdd.Error(), "add: duplicate pattern", "Should be not allowed to add duplicate patterns")
}