-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
140 lines (105 loc) · 3.5 KB
/
main_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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"errors"
"time"
"github.com/r3labs/broadcast"
//. "github.com/smartystreets/goconvey/convey"
)
func wait(ch chan *broadcast.Event, duration time.Duration) (*broadcast.Event, error) {
var err error
var msg *broadcast.Event
select {
case event := <-ch:
msg = event
case <-time.After(duration):
err = errors.New("timeout")
}
return msg, err
}
/*
func TestMain(t *testing.T) {
Convey("Given a new server", t, func() {
// New Server
ss = sse.New()
defer ss.Close()
mux := http.NewServeMux()
mux.HandleFunc("/events", ss.HTTPHandler)
hs := httptest.NewServer(mux)
url := hs.URL + "/events"
Convey("When listening for NATS messages", func() {
createEvents := []string{"build.create", "build.delete", "build.import"}
for _, event := range createEvents {
Convey("On receiving "+event, func() {
msg := nats.Msg{Subject: event, Data: []byte(`{"id": "test"}`)}
natsHandler(&msg)
time.Sleep(time.Millisecond * 10)
Convey("It should create a stream for the service", func() {
So(ss.StreamExists("test"), ShouldBeTrue)
})
})
}
deleteEvents := []string{"build.create.done", "build.delete.done", "build.import.done", "build.create.error", "build.delete.error", "build.import.error"}
for _, event := range deleteEvents {
ss.CreateStream("test")
time.Sleep(time.Millisecond * 10)
Convey("On receiving "+event, func() {
msg := nats.Msg{Subject: event, Data: []byte(`{"id": "test"}`)}
natsHandler(&msg)
time.Sleep(time.Millisecond * 1500)
Convey("It should remove the services stream", func() {
So(ss.StreamExists("test"), ShouldBeFalse)
})
})
}
Convey("On receiving an unknown message", func() {
// Clean server
ss.RemoveStream("test")
time.Sleep(time.Millisecond * 10)
msg := nats.Msg{Subject: "test.event", Data: []byte(`{"id": "test"}`)}
natsHandler(&msg)
Convey("It should not create a stream", func() {
So(ss.StreamExists("test"), ShouldBeFalse)
})
})
Convey("When receiving component event network.create.aws.done", func() {
testEvent := `{"service": "test", "name": "network"}`
msg := nats.Msg{Subject: "network.create.aws.done", Data: []byte(testEvent)}
Convey("And a stream exists", func() {
rcv := make(chan *sse.Event)
cl := sse.NewClient(url)
ss.CreateStream("test")
time.Sleep(time.Millisecond * 10)
go func() {
_ = cl.SubscribeChan("test", rcv)
}()
time.Sleep(time.Millisecond * 10)
Convey("It should publish a message to the stream", func() {
natsHandler(&msg)
for {
event, err := wait(rcv, time.Millisecond*100)
So(err, ShouldBeNil)
if len(event.Data) > 0 {
So(string(event.Data), ShouldEqual, `{"_component_id":"","_subject":"network.create.aws.done","_component":"","_state":"","_action":"","_provider":"","name":"network","service":"test"}`)
break
}
}
})
})
ss.RemoveStream("test")
Convey("And a stream doesn't exist", func() {
rcv := make(chan *sse.Event)
cl := sse.NewClient(url)
time.Sleep(time.Millisecond * 10)
Convey("It should error when connecting to the stream", func() {
err := cl.SubscribeChan("test", rcv)
So(err, ShouldNotBeNil)
})
})
})
})
})
}
*/