-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_test.go
130 lines (106 loc) · 2.65 KB
/
client_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
package workers
import (
"fmt"
"io"
"strconv"
"strings"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/kr/beanstalk"
)
type closedConnection struct{}
func (i *closedConnection) Read(p []byte) (int, error) {
return 0, io.EOF
}
func (i *closedConnection) Write(p []byte) (int, error) {
return 0, io.EOF
}
func (i *closedConnection) Close() error {
return nil
}
func Example() {
mux := NewWorkMux()
mux.Handle("tube1", HandlerFunc(func(job *Job) {
fmt.Printf("processing job %d with content %v\n", job.ID, job.Body)
job.Delete()
}))
mux.Handle("tube2", HandlerFunc(func(job *Job) {
job.Release(0, 0)
}))
ConnectAndWork("tcp", "localhost:11300", mux)
}
func TestStopClient(t *testing.T) {
client := &Client{
Network: "tcp",
Addr: "localhost:11300",
Handler: HandlerFunc(func(job *Job) {
}),
}
go func() {
time.Sleep(100 * time.Millisecond)
client.Stop()
}()
err := client.ConnectAndWork()
if err != ErrClientHasQuit {
t.Fail()
}
}
func TestUnexpectedErrorReturned(t *testing.T) {
client := &Client{
Handler: HandlerFunc(func(job *Job) {
}),
}
// this test will deadlock if fails
err := client.Reserve(&closedConnection{})
if err == nil || !strings.HasSuffix(err.Error(), io.EOF.Error()) {
t.Fail()
}
}
func TestClientStopsOnSIGTERM(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}()
err := ConnectAndWork("tcp", "localhost:11300", HandlerFunc(func(job *Job) {}))
if err != ErrClientHasQuit {
t.Fail()
}
}
func TestClientStopsOnSIGINT(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
err := ConnectAndWork("tcp", "localhost:11300", HandlerFunc(func(job *Job) {}))
if err != ErrClientHasQuit {
t.Fail()
}
}
func TestReserveIsParallelAndWaits(t *testing.T) {
count := int32(0)
tubeName := strconv.Itoa(int(time.Now().Unix()))
start := time.Now()
mux := NewWorkMux()
mux.Handle(tubeName, HandlerFunc(func(job *Job) {
time.Sleep(time.Second)
atomic.AddInt32(&count, 1)
job.Delete()
}))
go func() {
conn, _ := beanstalk.Dial("tcp", "localhost:11300")
tube := &beanstalk.Tube{Conn: conn, Name: tubeName}
tube.Put([]byte("job1"), 0, 0, time.Minute)
tube.Put([]byte("job2"), 0, 0, time.Minute)
tube.Put([]byte("job3"), 0, 0, time.Minute)
tube.Put([]byte("job4"), 0, 0, time.Minute)
tube.Put([]byte("job5"), 0, 0, time.Minute)
time.Sleep(time.Millisecond * 1100)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}()
ConnectAndWork("tcp", "localhost:11300", mux)
if count != 5 || time.Since(start) > time.Duration(time.Millisecond*2200) {
t.Fail()
}
}