/
pump_test.go
82 lines (68 loc) · 1.62 KB
/
pump_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
package pulse
import (
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/garyburd/redigo/redis"
"github.com/polydice/pulse/testutil"
. "launchpad.net/gocheck"
)
var port string = ":4000"
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}
var _ = Suite(&S{})
type S struct {
pump *Pump
}
func (self *S) SetUpTest(c *C) {
self.pump = New(port)
go self.pump.Start(true)
}
func (self *S) TestPublish(c *C) {
req := testutil.RequestFromSNS()
resp, err := http.Post("http://localhost"+port+"/publish/foo", "text/plain", req.Body)
c.Check(err, IsNil)
c.Check(resp.StatusCode, Equals, 200)
}
func (self *S) TestSubscribe(c *C) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(time.Second)
req := testutil.RequestFromSNS()
http.Post("http://localhost"+port+"/publish/foo", "text/plain", req.Body)
wg.Done()
}()
p := make([]byte, 50)
resp, err := http.Get("http://localhost" + port + "/subscribe/foo")
n, err := resp.Body.Read(p)
text := string(p[:n])
c.Check(err, IsNil)
c.Check(strings.Trim(text, "\n"), Equals, "data: test message")
wg.Wait()
}
func (self *S) TestRedisSubscribe(c *C) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(time.Second)
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
panic(err)
}
defer conn.Close()
conn.Do("PUBLISH", "foo", "test message")
wg.Done()
}()
p := make([]byte, 50)
resp, err := http.Get("http://localhost" + port + "/subscribe/foo")
n, err := resp.Body.Read(p)
text := string(p[:n])
c.Check(err, IsNil)
c.Check(strings.Trim(text, "\n"), Equals, "data: test message")
wg.Wait()
}