-
Notifications
You must be signed in to change notification settings - Fork 0
/
facility_test.go
137 lines (127 loc) · 2.88 KB
/
facility_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
package main
import (
"testing"
"time"
"github.com/garyburd/redigo/redis"
. "github.com/smartystreets/goconvey/convey"
)
const (
timeout = time.Millisecond * 1000
)
func init() {
redisPrefix = "ws-test"
}
// MemoryMessageProvider uses channels for source of messages
type MemoryMessageProvider struct {
Messages MessageChan
}
// Channel returns MessageChan
func (p MemoryMessageProvider) Channel() MessageChan {
return p.Messages
}
func TestMemoryFacility(t *testing.T) {
Convey("Create", t, func() {
c := make(MessageChan)
p := MemoryMessageProvider{c}
f := NewFacility("test", p)
message := []byte("testmessage")
Convey("Subscribe", func() {
m := f.Get()
Convey("Broadcast", func() {
c <- message
Convey("The message should be recieved", func() {
var recieved []byte
t := time.NewTimer(timeout)
var timedOut bool
select {
case recievedMessage, ok := <-m:
So(ok, ShouldBeTrue)
recieved = recievedMessage
case _ = <-t.C:
recieved = nil
timedOut = true
}
Convey("Not timed out", func() {
So(timedOut, ShouldBeFalse)
})
So(string(recieved), ShouldEqual, string(message))
})
})
Convey("Unsubscribe", func() {
f.Remove(m)
t := time.NewTimer(timeout)
timedOut := false
select {
case _, ok := <-m:
So(ok, ShouldBeFalse)
case _ = <-t.C:
timedOut = true
}
Convey("Not timed out", func() {
So(timedOut, ShouldBeFalse)
})
})
})
})
}
func TestRedisFacility(t *testing.T) {
Convey("Create", t, func() {
So(NewRedisFacility("test"), ShouldNotBeNil)
})
}
func TestRedisProvider(t *testing.T) {
Convey("Panic", t, func() {
defer func() {
redisNetwork = "tcp"
}()
So(func() {
redisNetwork = "kek"
NewRedisProvider("pek")
}, ShouldPanic)
})
Convey("Bad database", t, func() {
defer func() {
redisDatabase = 0
}()
redisDatabase = 1024
So(func() {
NewRedisProvider("pek")
}, ShouldPanic)
})
Convey("Reconnect", t, func() {
p := NewRedisProvider("tst~2")
conn := p.(*RedisMessageProvider).conn
conn.Close()
So(p.(*RedisMessageProvider).connect(), ShouldBeNil)
})
Convey("Create", t, func() {
k := "redis-test:provider"
p := NewRedisProvider(k)
c := p.Channel()
conn, err := redis.Dial(redisNetwork, redisAddr)
So(err, ShouldBeNil)
message := []byte("testmessage")
_, err = conn.Do("select", redisDatabase)
So(err, ShouldBeNil)
Convey("Send", func() {
_, err := conn.Do("publish", k, message)
So(err, ShouldBeNil)
Convey("Recieved", func() {
var recieved []byte
t := time.NewTimer(timeout)
var timedOut bool
select {
case m, ok := <-c:
So(ok, ShouldBeTrue)
recieved = m
case _ = <-t.C:
timedOut = true
}
Convey("Not timed out", func() {
So(timedOut, ShouldBeFalse)
})
So(string(recieved), ShouldEqual, string(message))
})
})
})
}