/
claimablejob_test.go
167 lines (135 loc) · 3.98 KB
/
claimablejob_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package claimablejob_test
import (
"strconv"
"time"
"github.com/garyburd/redigo/redis"
"github.com/octoblu/claimablejob"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type DumbJob struct {}
func (dumbJob *DumbJob) GetKey() string {
return "I am dumb"
}
var _ = Describe("ClaimableRedisJob", func() {
var redisConn redis.Conn
var sut claimablejob.ClaimableJob
BeforeEach(func() {
var err error
redisConn, err = redis.Dial("tcp", ":6379")
Expect(err).To(BeNil())
})
AfterEach(func(){
redisConn.Close()
})
Describe("New", func() {
Context("called with a key and a conn", func(){
BeforeEach(func(){
sut = claimablejob.New("old-map", redisConn)
})
It("should set the key", func(){
Expect(sut.GetKey()).To(Equal("old-map"))
})
})
})
Describe("NewFromJob", func() {
Context("called with a job", func(){
BeforeEach(func(){
job := new(DumbJob)
sut = claimablejob.NewFromJob(job, redisConn)
})
It("should set the key", func(){
Expect(sut.GetKey()).To(Equal("I am dumb"))
})
})
})
Describe("Claim", func(){
Context("When the job is unset", func(){
BeforeEach(func(){
sut = claimablejob.New("faulty", redisConn)
_,err := redisConn.Do("DEL", "[namespace]-faulty")
Expect(err).To(BeNil())
})
It("should return true", func(){
Expect(sut.Claim()).To(BeTrue())
})
})
Context("When the job has already run this second", func(){
BeforeEach(func(){
sut = claimablejob.New("faulty", redisConn)
then := int64(time.Now().Unix() + 1)
_,err := redisConn.Do("SET", "[namespace]-faulty", then)
Expect(err).To(BeNil())
})
It("should return false", func(){
Expect(sut.Claim()).To(BeFalse())
})
})
Context("When the job with a different name ran this second", func(){
BeforeEach(func(){
sut = claimablejob.New("smokey", redisConn)
_,err := redisConn.Do("DEL", "[namespace]-smokey")
Expect(err).To(BeNil())
})
It("should return true", func(){
Expect(sut.Claim()).To(BeTrue())
})
})
Context("When the job ran in the previous second", func(){
var gotClaim bool
BeforeEach(func(){
sut = claimablejob.New("faulty", redisConn)
now := int64(time.Now().Unix())
_,err := redisConn.Do("SET", "[namespace]-faulty", now)
Expect(err).To(BeNil())
gotClaim,err = sut.Claim()
})
It("should return false", func(){
Expect(gotClaim).To(BeTrue())
})
It("should advance the time", func(){
result,err := redisConn.Do("GET", "[namespace]-faulty")
Expect(err).To(BeNil())
nextTickStr := string(result.([]uint8))
nextTick,err := strconv.ParseInt(nextTickStr, 10, 64)
Expect(err).To(BeNil())
then := int64(time.Now().Unix()) + 1
Expect(nextTick).To(Equal(then))
})
})
})
Describe("PushKeyIntoQueue", func(){
Context("when execution is pushed into an empty some-random-queue", func(){
BeforeEach(func(){
_,err := redisConn.Do("DEL", "some-random-queue")
Expect(err).To(BeNil())
sut = claimablejob.New("execution", redisConn)
err = sut.PushKeyIntoQueue("some-random-queue")
Expect(err).To(BeNil())
})
It("should push the key into the queue", func(){
length,err := redisConn.Do("LLEN", "some-random-queue")
Expect(err).To(BeNil())
Expect(length).To(Equal(int64(1)))
})
})
Context("when falling-tree-branch is pushed into an empty stick-the-landing", func(){
BeforeEach(func(){
_,err := redisConn.Do("DEL", "stick-the-landing")
Expect(err).To(BeNil())
sut = claimablejob.New("falling-tree-branch", redisConn)
err = sut.PushKeyIntoQueue("stick-the-landing")
Expect(err).To(BeNil())
})
It("should push the key into the queue", func(){
length,err := redisConn.Do("LLEN", "stick-the-landing")
Expect(err).To(BeNil())
Expect(length).To(Equal(int64(1)))
key,err := redisConn.Do("LINDEX", "stick-the-landing", 0)
Expect(err).To(BeNil())
keyStr := string(key.([]uint8))
Expect(keyStr).To(Equal("falling-tree-branch"))
})
})
})
})