OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package memory | 5 package memory |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "infra/gae/libs/wrapper" | 9 "infra/gae/libs/wrapper" |
| 10 "infra/libs/clock" |
| 11 "infra/libs/clock/testclock" |
10 "math/rand" | 12 "math/rand" |
11 "net/http" | 13 "net/http" |
12 "testing" | 14 "testing" |
13 "time" | 15 "time" |
14 | 16 |
15 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
16 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
17 | 19 |
18 "appengine/taskqueue" | 20 "appengine/taskqueue" |
19 ) | 21 ) |
20 | 22 |
21 func TestTaskQueue(t *testing.T) { | 23 func TestTaskQueue(t *testing.T) { |
22 t.Parallel() | 24 t.Parallel() |
23 | 25 |
24 Convey("TaskQueue", t, func() { | 26 Convey("TaskQueue", t, func() { |
25 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) | 27 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) |
26 » » timeNow := func(context.Context) time.Time { | 28 » » tc := testclock.New(now) |
27 » » » ret := now | 29 » » c := clock.SetClock(context.Background(), tc) |
28 » » » now = now.Add(time.Second) | 30 » » c = wrapper.SetMathRand(c, rand.New(rand.NewSource(clock.Now(c).
UnixNano()))) |
29 » » » return ret | |
30 » » } | |
31 » » c := wrapper.SetTimeNowFactory(context.Background(), timeNow) | |
32 » » c = wrapper.SetMathRand(c, rand.New(rand.NewSource(wrapper.GetTi
meNow(c).UnixNano()))) | |
33 c = Use(c) | 31 c = Use(c) |
34 | 32 |
35 tq := wrapper.GetTQ(c).(interface { | 33 tq := wrapper.GetTQ(c).(interface { |
36 wrapper.TQMultiReadWriter | 34 wrapper.TQMultiReadWriter |
37 wrapper.TQTestable | 35 wrapper.TQTestable |
38 }) | 36 }) |
39 | 37 |
40 So(tq, ShouldNotBeNil) | 38 So(tq, ShouldNotBeNil) |
41 | 39 |
42 Convey("implements TQMultiReadWriter", func() { | 40 Convey("implements TQMultiReadWriter", func() { |
43 Convey("Add", func() { | 41 Convey("Add", func() { |
44 t := &taskqueue.Task{Path: "/hello/world"} | 42 t := &taskqueue.Task{Path: "/hello/world"} |
45 | 43 |
46 Convey("works", func() { | 44 Convey("works", func() { |
47 curTime := now | |
48 t.Delay = 4 * time.Second | 45 t.Delay = 4 * time.Second |
49 t.Header = http.Header{} | 46 t.Header = http.Header{} |
50 t.Header.Add("Cat", "tabby") | 47 t.Header.Add("Cat", "tabby") |
51 t.Payload = []byte("watwatwat") | 48 t.Payload = []byte("watwatwat") |
52 t.RetryOptions = &taskqueue.RetryOptions
{AgeLimit: 7 * time.Second} | 49 t.RetryOptions = &taskqueue.RetryOptions
{AgeLimit: 7 * time.Second} |
53 _, err := tq.Add(t, "") | 50 _, err := tq.Add(t, "") |
54 So(err, ShouldBeNil) | 51 So(err, ShouldBeNil) |
55 name := "Z_UjshxM9ecyMQfGbZmUGOEcgxWU0_5
CGLl_-RntudwAw2DqQ5-58bzJiWQN4OKzeuUb9O4JrPkUw2rOvk2Ax46THojnQ6avBQgZdrKcJmrwQ6o
4qKfJdiyUbGXvy691yRfzLeQhs6cBhWrgf3wH-VPMcA4SC-zlbJ2U8An7I0zJQA5nBFnMNoMgT-2peGo
ay3rCSbj4z9VFFm9kS_i6JCaQH518ujLDSNCYdjTq6B6lcWrZAh0U_q3a1S2nXEwrKiw_t9MTNQFgAQZ
WyGBbvZQPmeRYtu8SPaWzTfd25v_YWgBuVL2rRSPSMvlDwE04nNdtvVzE8vNNiA1zRimmdzKeqATQF9_
ReUvj4D7U8dcS703DZWfKMBLgBffY9jqCassOOOw77V72Oq5EVauUw3Qw0L6bBsfM9FtahTKUdabzRZj
XUoze3EK4KXPt3-wdidau-8JrVf2XFocjjZbwHoxcGvbtT3b4nGLDlgwdC00bwaFBZWff" | 52 name := "Z_UjshxM9ecyMQfGbZmUGOEcgxWU0_5
CGLl_-RntudwAw2DqQ5-58bzJiWQN4OKzeuUb9O4JrPkUw2rOvk2Ax46THojnQ6avBQgZdrKcJmrwQ6o
4qKfJdiyUbGXvy691yRfzLeQhs6cBhWrgf3wH-VPMcA4SC-zlbJ2U8An7I0zJQA5nBFnMNoMgT-2peGo
ay3rCSbj4z9VFFm9kS_i6JCaQH518ujLDSNCYdjTq6B6lcWrZAh0U_q3a1S2nXEwrKiw_t9MTNQFgAQZ
WyGBbvZQPmeRYtu8SPaWzTfd25v_YWgBuVL2rRSPSMvlDwE04nNdtvVzE8vNNiA1zRimmdzKeqATQF9_
ReUvj4D7U8dcS703DZWfKMBLgBffY9jqCassOOOw77V72Oq5EVauUw3Qw0L6bBsfM9FtahTKUdabzRZj
XUoze3EK4KXPt3-wdidau-8JrVf2XFocjjZbwHoxcGvbtT3b4nGLDlgwdC00bwaFBZWff" |
56 So(*tq.GetScheduledTasks()["default"][na
me], ShouldResemble, taskqueue.Task{ | 53 So(*tq.GetScheduledTasks()["default"][na
me], ShouldResemble, taskqueue.Task{ |
57 » » » » » » ETA: curTime.Add(4 * ti
me.Second), | 54 » » » » » » ETA: now.Add(4 * time.S
econd), |
58 Header: http.Header{"Cat":
[]string{"tabby"}}, | 55 Header: http.Header{"Cat":
[]string{"tabby"}}, |
59 Method: "POST", | 56 Method: "POST", |
60 Name: name, | 57 Name: name, |
61 Path: "/hello/world", | 58 Path: "/hello/world", |
62 Payload: []byte("watwatwat"
), | 59 Payload: []byte("watwatwat"
), |
63 RetryOptions: &taskqueue.RetryOp
tions{AgeLimit: 7 * time.Second}, | 60 RetryOptions: &taskqueue.RetryOp
tions{AgeLimit: 7 * time.Second}, |
64 }) | 61 }) |
65 }) | 62 }) |
66 | 63 |
67 Convey("cannot add to bad queues", func() { | 64 Convey("cannot add to bad queues", func() { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 100 |
104 err = tq.Delete(t, "") | 101 err = tq.Delete(t, "") |
105 So(err, ShouldBeNil) | 102 So(err, ShouldBeNil) |
106 | 103 |
107 // can't add a deleted task! | 104 // can't add a deleted task! |
108 _, err = tq.Add(t, "") | 105 _, err = tq.Add(t, "") |
109 So(err, ShouldEqual, taskqueue.ErrTaskAl
readyAdded) | 106 So(err, ShouldEqual, taskqueue.ErrTaskAl
readyAdded) |
110 }) | 107 }) |
111 | 108 |
112 Convey("cannot set ETA+Delay", func() { | 109 Convey("cannot set ETA+Delay", func() { |
113 » » » » » t.ETA = wrapper.GetTimeNow(c).Add(time.H
our) | 110 » » » » » t.ETA = clock.Now(c).Add(time.Hour) |
| 111 » » » » » tc.Add(time.Second) |
114 t.Delay = time.Hour | 112 t.Delay = time.Hour |
115 So(func() { tq.Add(t, "") }, ShouldPanic
) | 113 So(func() { tq.Add(t, "") }, ShouldPanic
) |
116 }) | 114 }) |
117 | 115 |
118 Convey("must use a reasonable method", func() { | 116 Convey("must use a reasonable method", func() { |
119 t.Method = "Crystal" | 117 t.Method = "Crystal" |
120 _, err := tq.Add(t, "") | 118 _, err := tq.Add(t, "") |
121 So(err.Error(), ShouldContainSubstring,
"bad method") | 119 So(err.Error(), ShouldContainSubstring,
"bad method") |
122 }) | 120 }) |
123 | 121 |
(...skipping 14 matching lines...) Expand all Loading... |
138 So(err.Error(), ShouldContainSubstring,
"INVALID_TASK_NAME") | 136 So(err.Error(), ShouldContainSubstring,
"INVALID_TASK_NAME") |
139 }) | 137 }) |
140 | 138 |
141 Convey("can be broken", func() { | 139 Convey("can be broken", func() { |
142 tq.BreakFeatures(nil, "Add") | 140 tq.BreakFeatures(nil, "Add") |
143 _, err := tq.Add(t, "") | 141 _, err := tq.Add(t, "") |
144 So(err.Error(), ShouldContainSubstring,
"TRANSIENT_ERROR") | 142 So(err.Error(), ShouldContainSubstring,
"TRANSIENT_ERROR") |
145 }) | 143 }) |
146 | 144 |
147 Convey("AddMulti also works", func() { | 145 Convey("AddMulti also works", func() { |
148 curTime := now | |
149 | |
150 t2 := dupTask(t) | 146 t2 := dupTask(t) |
151 t2.Path = "/hi/city" | 147 t2.Path = "/hi/city" |
152 | 148 |
153 expect := []*taskqueue.Task{t, t2} | 149 expect := []*taskqueue.Task{t, t2} |
154 | 150 |
155 tasks, err := tq.AddMulti(expect, "defau
lt") | 151 tasks, err := tq.AddMulti(expect, "defau
lt") |
156 So(err, ShouldBeNil) | 152 So(err, ShouldBeNil) |
157 So(len(tasks), ShouldEqual, 2) | 153 So(len(tasks), ShouldEqual, 2) |
158 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 2) | 154 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 2) |
159 | 155 |
160 for i := range expect { | 156 for i := range expect { |
161 Convey(fmt.Sprintf("task %d: %s"
, i, expect[i].Path), func() { | 157 Convey(fmt.Sprintf("task %d: %s"
, i, expect[i].Path), func() { |
162 expect[i].Method = "POST
" | 158 expect[i].Method = "POST
" |
163 » » » » » » » expect[i].ETA = curTime.
Add(time.Duration(i) * time.Second) | 159 » » » » » » » expect[i].ETA = now |
164 So(expect[i].Name, Shoul
dEqual, "") | 160 So(expect[i].Name, Shoul
dEqual, "") |
165 So(len(tasks[i].Name), S
houldEqual, 500) | 161 So(len(tasks[i].Name), S
houldEqual, 500) |
166 tasks[i].Name = "" | 162 tasks[i].Name = "" |
167 So(tasks[i], ShouldResem
ble, expect[i]) | 163 So(tasks[i], ShouldResem
ble, expect[i]) |
168 }) | 164 }) |
169 } | 165 } |
170 | 166 |
171 Convey("can be broken", func() { | 167 Convey("can be broken", func() { |
172 tq.BreakFeatures(nil, "AddMulti"
) | 168 tq.BreakFeatures(nil, "AddMulti"
) |
173 _, err := tq.AddMulti([]*taskque
ue.Task{t}, "") | 169 _, err := tq.AddMulti([]*taskque
ue.Task{t}, "") |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 }() | 459 }() |
464 | 460 |
465 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) | 461 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) |
466 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) | 462 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) |
467 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) | 463 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) |
468 }) | 464 }) |
469 | 465 |
470 }) | 466 }) |
471 }) | 467 }) |
472 } | 468 } |
OLD | NEW |