/
component_test.go
165 lines (132 loc) · 3.6 KB
/
component_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
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/json"
"fmt"
"reflect"
"time"
"testing"
"github.com/benschw/chinchilla/config"
"github.com/benschw/chinchilla/ep"
"github.com/benschw/chinchilla/example/ex"
_ "github.com/benschw/chinchilla/queue"
"github.com/benschw/opin-go/rando"
"github.com/streadway/amqp"
"github.com/stretchr/testify/assert"
)
var conn *amqp.Connection
type Msg struct {
Message string
}
func init() {
c, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
panic(err)
}
conn = c
}
func GetPublisher(cfg *config.EndpointConfig) *ex.Publisher {
p := &ex.Publisher{
Conn: conn,
Config: cfg,
}
return p
}
func GetServices() (*ep.EndpointApp, *ex.Server, *ex.Publisher, *ex.Publisher) {
port := uint16(rando.Port())
server := ex.NewServer(fmt.Sprintf(":%d", port))
epCfg := config.EndpointConfig{
Name: "Foo",
ServiceHost: fmt.Sprintf("http://localhost:%d", port),
Uri: "/foo",
Method: "POST",
QueueConfig: map[interface{}]interface{}{
"queuename": "test.foo",
},
}
epCfg2 := config.EndpointConfig{
Name: "Bar",
ServiceHost: fmt.Sprintf("http://localhost:%d", port),
Uri: "/bar",
Method: "POST",
QueueConfig: map[interface{}]interface{}{
"queuename": "test.bar",
},
}
p := GetPublisher(&epCfg)
p2 := GetPublisher(&epCfg2)
repo := &config.StaticRepo{
Address: config.RabbitAddress{
User: "guest",
Password: "guest",
Host: "localhost",
Port: 5672,
},
Endpoints: []config.EndpointConfig{epCfg, epCfg2},
}
mgr := ep.NewApp(repo, repo)
return mgr, server, p, p2
}
func TestPublish(t *testing.T) {
// setup
mgr, server, p, _ := GetServices()
go server.Start()
go mgr.Run()
// wait for queue creation to prevent race condition... do this better
time.Sleep(200 * time.Millisecond)
api := &Msg{Message: "Hello World"}
apiB, _ := json.Marshal(api)
apiStr := string(apiB)
// when
err := p.Publish(apiStr, "application/json")
assert.Nil(t, err)
time.Sleep(200 * time.Millisecond)
mgr.Stop()
server.Stop()
// then
statLen := len(server.H.Stats["Foo"])
assert.Equal(t, 1, statLen, "wrong number of stats")
if statLen > 0 {
foundApi := &Msg{}
err := json.Unmarshal([]byte(server.H.Stats["Foo"][0]), foundApi)
assert.Nil(t, err, "err should be nil")
assert.True(t, reflect.DeepEqual(api, foundApi), fmt.Sprintf("\n %+v\n!= %+v", api, foundApi))
}
}
func TestPublishLotsAndLots(t *testing.T) {
// setup
mgr, server, p, p2 := GetServices()
go server.Start()
go mgr.Run()
body := "Hello World"
// when
for i := 0; i < 100; i++ {
err := p.Publish(body, "text/plain")
assert.Nil(t, err)
err = p2.Publish(body, "text/plain")
assert.Nil(t, err)
}
server.Stop()
mgr.Stop()
// then
assert.Equal(t, 100, len(server.H.Stats["Foo"]), "wrong number of stats")
assert.Equal(t, 100, len(server.H.Stats["Bar"]), "wrong number of stats")
}
func TestMessageHeaders(t *testing.T) {
// setup
mgr, server, p, _ := GetServices()
go server.Start()
go mgr.Run()
body := "Hello World"
// when
err := p.Publish(body, "text/plain")
assert.Nil(t, err)
time.Sleep(3 * time.Second)
server.Stop()
mgr.Stop()
// then
timestamp, _ := time.Parse("2006-01-02 15:04:05", server.H.Request.Header.Get("X-Timestamp"))
assert.Equal(t, "foo.poo", server.H.Request.Header.Get("X-reply-to"), "reply_to is wrong")
assert.True(t, time.Now().Second()-timestamp.Second() <= 3, "wrong timestamp")
assert.Equal(t, "test.foo", server.H.Request.Header.Get("X-routing-key"), "wrong routing key")
assert.Equal(t, ex.MessageId, server.H.Request.Header.Get("X-message-id"), "wrong message id")
}