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 » » c, tc := testclock.UseTime(context.Background(), now) |
27 » » » ret := now | 29 » » c = wrapper.SetMathRand(c, rand.New(rand.NewSource(clock.Now(c).
UnixNano()))) |
28 » » » now = now.Add(time.Second) | |
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) | 30 c = Use(c) |
34 | 31 |
35 tq := wrapper.GetTQ(c).(interface { | 32 tq := wrapper.GetTQ(c).(interface { |
36 wrapper.TQMultiReadWriter | 33 wrapper.TQMultiReadWriter |
37 wrapper.TQTestable | 34 wrapper.TQTestable |
38 }) | 35 }) |
39 | 36 |
40 So(tq, ShouldNotBeNil) | 37 So(tq, ShouldNotBeNil) |
41 | 38 |
42 Convey("implements TQMultiReadWriter", func() { | 39 Convey("implements TQMultiReadWriter", func() { |
43 Convey("Add", func() { | 40 Convey("Add", func() { |
44 t := &taskqueue.Task{Path: "/hello/world"} | 41 t := &taskqueue.Task{Path: "/hello/world"} |
45 | 42 |
46 Convey("works", func() { | 43 Convey("works", func() { |
47 curTime := now | |
48 t.Delay = 4 * time.Second | 44 t.Delay = 4 * time.Second |
49 t.Header = http.Header{} | 45 t.Header = http.Header{} |
50 t.Header.Add("Cat", "tabby") | 46 t.Header.Add("Cat", "tabby") |
51 t.Payload = []byte("watwatwat") | 47 t.Payload = []byte("watwatwat") |
52 t.RetryOptions = &taskqueue.RetryOptions
{AgeLimit: 7 * time.Second} | 48 t.RetryOptions = &taskqueue.RetryOptions
{AgeLimit: 7 * time.Second} |
53 _, err := tq.Add(t, "") | 49 _, err := tq.Add(t, "") |
54 So(err, ShouldBeNil) | 50 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" | 51 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{ | 52 So(*tq.GetScheduledTasks()["default"][na
me], ShouldResemble, taskqueue.Task{ |
57 » » » » » » ETA: curTime.Add(4 * ti
me.Second), | 53 » » » » » » ETA: now.Add(4 * time.S
econd), |
58 Header: http.Header{"Cat":
[]string{"tabby"}}, | 54 Header: http.Header{"Cat":
[]string{"tabby"}}, |
59 Method: "POST", | 55 Method: "POST", |
60 Name: name, | 56 Name: name, |
61 Path: "/hello/world", | 57 Path: "/hello/world", |
62 Payload: []byte("watwatwat"
), | 58 Payload: []byte("watwatwat"
), |
63 RetryOptions: &taskqueue.RetryOp
tions{AgeLimit: 7 * time.Second}, | 59 RetryOptions: &taskqueue.RetryOp
tions{AgeLimit: 7 * time.Second}, |
64 }) | 60 }) |
65 }) | 61 }) |
66 | 62 |
67 Convey("cannot add to bad queues", func() { | 63 Convey("cannot add to bad queues", func() { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 99 |
104 err = tq.Delete(t, "") | 100 err = tq.Delete(t, "") |
105 So(err, ShouldBeNil) | 101 So(err, ShouldBeNil) |
106 | 102 |
107 // can't add a deleted task! | 103 // can't add a deleted task! |
108 _, err = tq.Add(t, "") | 104 _, err = tq.Add(t, "") |
109 So(err, ShouldEqual, taskqueue.ErrTaskAl
readyAdded) | 105 So(err, ShouldEqual, taskqueue.ErrTaskAl
readyAdded) |
110 }) | 106 }) |
111 | 107 |
112 Convey("cannot set ETA+Delay", func() { | 108 Convey("cannot set ETA+Delay", func() { |
113 » » » » » t.ETA = wrapper.GetTimeNow(c).Add(time.H
our) | 109 » » » » » t.ETA = clock.Now(c).Add(time.Hour) |
| 110 » » » » » tc.Add(time.Second) |
114 t.Delay = time.Hour | 111 t.Delay = time.Hour |
115 So(func() { tq.Add(t, "") }, ShouldPanic
) | 112 So(func() { tq.Add(t, "") }, ShouldPanic
) |
116 }) | 113 }) |
117 | 114 |
118 Convey("must use a reasonable method", func() { | 115 Convey("must use a reasonable method", func() { |
119 t.Method = "Crystal" | 116 t.Method = "Crystal" |
120 _, err := tq.Add(t, "") | 117 _, err := tq.Add(t, "") |
121 So(err.Error(), ShouldContainSubstring,
"bad method") | 118 So(err.Error(), ShouldContainSubstring,
"bad method") |
122 }) | 119 }) |
123 | 120 |
(...skipping 14 matching lines...) Expand all Loading... |
138 So(err.Error(), ShouldContainSubstring,
"INVALID_TASK_NAME") | 135 So(err.Error(), ShouldContainSubstring,
"INVALID_TASK_NAME") |
139 }) | 136 }) |
140 | 137 |
141 Convey("can be broken", func() { | 138 Convey("can be broken", func() { |
142 tq.BreakFeatures(nil, "Add") | 139 tq.BreakFeatures(nil, "Add") |
143 _, err := tq.Add(t, "") | 140 _, err := tq.Add(t, "") |
144 So(err.Error(), ShouldContainSubstring,
"TRANSIENT_ERROR") | 141 So(err.Error(), ShouldContainSubstring,
"TRANSIENT_ERROR") |
145 }) | 142 }) |
146 | 143 |
147 Convey("AddMulti also works", func() { | 144 Convey("AddMulti also works", func() { |
148 curTime := now | |
149 | |
150 t2 := dupTask(t) | 145 t2 := dupTask(t) |
151 t2.Path = "/hi/city" | 146 t2.Path = "/hi/city" |
152 | 147 |
153 expect := []*taskqueue.Task{t, t2} | 148 expect := []*taskqueue.Task{t, t2} |
154 | 149 |
155 tasks, err := tq.AddMulti(expect, "defau
lt") | 150 tasks, err := tq.AddMulti(expect, "defau
lt") |
156 So(err, ShouldBeNil) | 151 So(err, ShouldBeNil) |
157 So(len(tasks), ShouldEqual, 2) | 152 So(len(tasks), ShouldEqual, 2) |
158 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 2) | 153 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 2) |
159 | 154 |
160 for i := range expect { | 155 for i := range expect { |
161 Convey(fmt.Sprintf("task %d: %s"
, i, expect[i].Path), func() { | 156 Convey(fmt.Sprintf("task %d: %s"
, i, expect[i].Path), func() { |
162 expect[i].Method = "POST
" | 157 expect[i].Method = "POST
" |
163 » » » » » » » expect[i].ETA = curTime.
Add(time.Duration(i) * time.Second) | 158 » » » » » » » expect[i].ETA = now |
164 So(expect[i].Name, Shoul
dEqual, "") | 159 So(expect[i].Name, Shoul
dEqual, "") |
165 So(len(tasks[i].Name), S
houldEqual, 500) | 160 So(len(tasks[i].Name), S
houldEqual, 500) |
166 tasks[i].Name = "" | 161 tasks[i].Name = "" |
167 So(tasks[i], ShouldResem
ble, expect[i]) | 162 So(tasks[i], ShouldResem
ble, expect[i]) |
168 }) | 163 }) |
169 } | 164 } |
170 | 165 |
171 Convey("can be broken", func() { | 166 Convey("can be broken", func() { |
172 tq.BreakFeatures(nil, "AddMulti"
) | 167 tq.BreakFeatures(nil, "AddMulti"
) |
173 _, err := tq.AddMulti([]*taskque
ue.Task{t}, "") | 168 _, err := tq.AddMulti([]*taskque
ue.Task{t}, "") |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 }() | 458 }() |
464 | 459 |
465 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) | 460 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) |
466 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) | 461 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) |
467 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) | 462 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) |
468 }) | 463 }) |
469 | 464 |
470 }) | 465 }) |
471 }) | 466 }) |
472 } | 467 } |
OLD | NEW |