-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
89 lines (73 loc) · 1.29 KB
/
helpers_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
package rrpubsub
import (
"fmt"
"log"
"math/rand"
"net"
"os/exec"
"syscall"
"time"
"github.com/gomodule/redigo/redis"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func logDebug(msg string) {
log.Println(msg)
}
type testServer struct {
port int
address string
cmd *exec.Cmd
}
func newTestServer() (*testServer, error) {
port := 6000 + rand.Intn(5000)
s := &testServer{
port: port,
address: fmt.Sprintf("localhost:%d", port),
}
s.start()
return s, nil
}
func (s *testServer) Kill() {
if s.cmd != nil {
s.cmd.Process.Kill()
s.cmd.Wait()
s.cmd = nil
}
}
func (s *testServer) start() {
s.cmd = exec.Command("redis-server", "--port", fmt.Sprintf("%d", s.port))
s.cmd.Start()
// Wait for it to be up
for {
conn, err := net.Dial("tcp", s.address)
if err == nil {
conn.Close()
break
}
}
}
func (s *testServer) Restart() {
s.Kill()
s.start()
}
func (s *testServer) Send(ch, msg string) error {
c, err := redis.Dial("tcp", s.address)
if err != nil {
return err
}
// log.Printf("PUBLISH: %s %s", ch, msg)
_, err = c.Do("PUBLISH", ch, msg)
return err
}
func (s *testServer) Freeze() {
if s.cmd != nil {
s.cmd.Process.Signal(syscall.SIGSTOP)
}
}
func (s *testServer) Continue() {
if s.cmd != nil {
s.cmd.Process.Signal(syscall.SIGCONT)
}
}