Skip to content

Commit 53b335b

Browse files
authored
Merge pull request #3 from tkuchiki/patch-86
patch jrallison#86
2 parents 0951ee6 + c52b042 commit 53b335b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

enqueue.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ type EnqueueData struct {
2121
EnqueueOptions
2222
}
2323

24+
type EnqueueDataProxy EnqueueData
25+
26+
func (e EnqueueData) MarshalJSON() ([]byte, error) {
27+
o := e.EnqueueOptions
28+
if e.EnqueueOptions.RetryCount > 0 {
29+
s := struct {
30+
EnqueueDataProxy
31+
Retry int `json:"retry,omitempty"`
32+
RetryCount int `json:"retry_count"`
33+
At float64 `json:"at,omitempty"`
34+
}{EnqueueDataProxy(e), o.RetryCount, 0, o.At}
35+
return json.Marshal(s)
36+
}
37+
38+
return json.Marshal(struct {
39+
EnqueueDataProxy
40+
RetryCount int `json:"retry_count,omitempty"`
41+
Retry bool `json:"retry,omitempty"`
42+
At float64 `json:"at,omitempty"`
43+
}{EnqueueDataProxy(e), o.RetryCount, o.Retry, o.At})
44+
}
45+
2446
type EnqueueOptions struct {
2547
RetryCount int `json:"retry_count,omitempty"`
2648
Retry bool `json:"retry,omitempty"`

enqueue_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,23 @@ func EnqueueSpec(c gospec.Context) {
7272
c.Expect(ea, IsWithin(0.1), nowToSecondsWithNanoPrecision())
7373
})
7474

75-
c.Specify("has retry and retry_count when set", func() {
76-
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{RetryCount: 13, Retry: true})
75+
c.Specify("sets retry count to `retry`", func() {
76+
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{RetryCount: 13})
77+
78+
bytes, _ := redis.Bytes(conn.Do("lpop", "prod:queue:enqueue6"))
79+
var result map[string]interface{}
80+
json.Unmarshal(bytes, &result)
81+
c.Expect(result["class"], Equals, "Compare")
82+
83+
retry := result["retry"].(float64)
84+
c.Expect(retry, Equals, float64(13))
85+
86+
retryCount := result["retry_count"].(float64)
87+
c.Expect(retryCount, Equals, float64(0))
88+
})
89+
90+
c.Specify("sets Retry correctly when no count given", func() {
91+
EnqueueWithOptions("enqueue6", "Compare", []string{"foo", "bar"}, EnqueueOptions{Retry: true})
7792

7893
bytes, _ := redis.Bytes(conn.Do("lpop", "prod:queue:enqueue6"))
7994
var result map[string]interface{}
@@ -82,9 +97,6 @@ func EnqueueSpec(c gospec.Context) {
8297

8398
retry := result["retry"].(bool)
8499
c.Expect(retry, Equals, true)
85-
86-
retryCount := int(result["retry_count"].(float64))
87-
c.Expect(retryCount, Equals, 13)
88100
})
89101
})
90102

0 commit comments

Comments
 (0)