-
Notifications
You must be signed in to change notification settings - Fork 1
/
sima_test.go
100 lines (71 loc) · 2.31 KB
/
sima_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
package sima
import (
. "gopkg.in/check.v1"
"testing"
"context"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type SimaSuite struct{}
var _ = Suite(&SimaSuite{})
func (s *SimaSuite)TestSingleSubscriptionsWithNoSenders(c *C) {
tf := NewTopicFactory()
hello := NewSima(tf)
hello.Connect(func(context context.Context, sender *Topic) interface{} {
return sender
},"")
response := hello.Dispatch(context.Background(), "")
c.Assert(len(response), Equals, 1)
c.Assert(response[0], DeepEquals, tf.GetByName(ANY))
}
func (s *SimaSuite)TestSingleSubscriptionsWithSenders(c *C) {
tf := NewTopicFactory()
hello := NewSima(tf)
hello.Connect(func(context context.Context, sender *Topic) interface{} {
return sender
},"hello")
response1 := hello.Dispatch(context.Background(), "")
c.Assert(len(response1), Equals, 0)
response2 := hello.Dispatch(context.Background(), "world")
c.Assert(len(response2), Equals, 0)
response3 := hello.Dispatch(context.Background(), "hello")
c.Assert(len(response3), Equals, 1)
c.Assert(response3[0], DeepEquals, tf.GetByName("hello"))
}
func (s *SimaSuite)TestHasReceiversFor(c *C) {
tf := NewTopicFactory()
hello := NewSima(tf)
c.Check(hello.HasReceiversFor(""), Equals, false)
c.Check(hello.HasReceiversFor(""), Equals, false)
hello.Connect(func(context context.Context, sender *Topic) interface{} {
return sender
},"")
c.Check(hello.HasReceiversFor(""), Equals, true)
hello.Connect(func(context context.Context, sender *Topic) interface{} {
return sender
}, "Hello")
c.Check(hello.HasReceiversFor("Hello"), Equals, true)
}
func (s *SimaSuite)TestMultipleSubscriptions(c *C) {
tf := NewTopicFactory()
hello := NewSima(tf)
var i int
f := func(context context.Context, sender *Topic) interface{} {
i += 1
return sender
}
hello.Connect(f, "hello")
hello.Connect(f, "hello")
response := hello.Dispatch(context.Background(), "hello")
// Called only once for same function and sender
c.Assert(i, Equals, 1)
c.Assert(len(response), Equals, 1)
hello.Connect(func(context context.Context, sender *Topic) interface{} {
i += 1
return sender
}, "hello")
response = hello.Dispatch(context.Background(), "hello")
// Called every-time for same function signature and sender
c.Assert(i, Equals, 3)
c.Assert(len(response), Equals, 2)
}