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 "math/rand" | 9 "math/rand" |
10 "net/http" | 10 "net/http" |
(...skipping 13 matching lines...) Expand all Loading... |
24 func TestTaskQueue(t *testing.T) { | 24 func TestTaskQueue(t *testing.T) { |
25 t.Parallel() | 25 t.Parallel() |
26 | 26 |
27 Convey("TaskQueue", t, func() { | 27 Convey("TaskQueue", t, func() { |
28 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) | 28 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) |
29 c, tc := testclock.UseTime(context.Background(), now) | 29 c, tc := testclock.UseTime(context.Background(), now) |
30 c = mathrand.Set(c, rand.New(rand.NewSource(clock.Now(c).UnixNan
o()))) | 30 c = mathrand.Set(c, rand.New(rand.NewSource(clock.Now(c).UnixNan
o()))) |
31 c = Use(c) | 31 c = Use(c) |
32 | 32 |
33 tq := tqS.Get(c) | 33 tq := tqS.Get(c) |
34 » » tqt := tq.Testable() | 34 » » tqt := tq.Raw().Testable() |
35 So(tqt, ShouldNotBeNil) | 35 So(tqt, ShouldNotBeNil) |
36 | 36 |
37 So(tq, ShouldNotBeNil) | 37 So(tq, ShouldNotBeNil) |
38 | 38 |
39 Convey("implements TQMultiReadWriter", func() { | 39 Convey("implements TQMultiReadWriter", func() { |
40 Convey("Add", func() { | 40 Convey("Add", func() { |
41 t := tq.NewTask("/hello/world") | 41 t := tq.NewTask("/hello/world") |
42 | 42 |
43 Convey("works", func() { | 43 Convey("works", func() { |
44 t.Delay = 4 * time.Second | 44 t.Delay = 4 * time.Second |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 t := &tqS.Task{Path: "/hello/world"} | 239 t := &tqS.Task{Path: "/hello/world"} |
240 So(tq.Add(t, ""), ShouldBeNil) | 240 So(tq.Add(t, ""), ShouldBeNil) |
241 | 241 |
242 t2 := &tqS.Task{Path: "/hi/city"} | 242 t2 := &tqS.Task{Path: "/hi/city"} |
243 So(tq.Add(t2, ""), ShouldBeNil) | 243 So(tq.Add(t2, ""), ShouldBeNil) |
244 | 244 |
245 So(tq.Delete(t2, ""), ShouldBeNil) | 245 So(tq.Delete(t2, ""), ShouldBeNil) |
246 | 246 |
247 Convey("can view regular tasks", func() { | 247 Convey("can view regular tasks", func() { |
248 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 248 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
249 » » » » » tqt := tqS.Get(c).Testable() | 249 » » » » » tqt := tqS.GetRaw(c).Testable() |
250 | 250 |
251 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 251 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
252 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 252 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
253 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) | 253 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) |
254 return nil | 254 return nil |
255 }, nil) | 255 }, nil) |
256 }) | 256 }) |
257 | 257 |
258 Convey("can add a new task", func() { | 258 Convey("can add a new task", func() { |
259 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 259 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
260 | 260 |
261 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { | 261 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { |
262 tq := tqS.Get(c) | 262 tq := tqS.Get(c) |
263 » » » » » tqt := tq.Testable() | 263 » » » » » tqt := tq.Raw().Testable() |
264 | 264 |
265 So(tq.Add(t3, ""), ShouldBeNil) | 265 So(tq.Add(t3, ""), ShouldBeNil) |
266 So(t3.Name, ShouldEqual, "") | 266 So(t3.Name, ShouldEqual, "") |
267 | 267 |
268 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 268 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
269 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 269 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
270 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) | 270 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) |
271 return nil | 271 return nil |
272 }, nil) | 272 }, nil) |
273 So(err, ShouldBeNil) | 273 So(err, ShouldBeNil) |
(...skipping 11 matching lines...) Expand all Loading... |
285 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 285 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
286 }) | 286 }) |
287 | 287 |
288 Convey("can add a new task (but reset the state in a tes
t)", func() { | 288 Convey("can add a new task (but reset the state in a tes
t)", func() { |
289 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 289 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
290 | 290 |
291 ttq := tqS.Interface(nil) | 291 ttq := tqS.Interface(nil) |
292 | 292 |
293 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 293 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
294 ttq = tqS.Get(c) | 294 ttq = tqS.Get(c) |
295 » » » » » tqt := ttq.Testable() | 295 » » » » » tqt := ttq.Raw().Testable() |
296 | 296 |
297 So(ttq.Add(t3, ""), ShouldBeNil) | 297 So(ttq.Add(t3, ""), ShouldBeNil) |
298 | 298 |
299 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 299 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
300 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 300 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
301 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) | 301 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) |
302 | 302 |
303 tqt.ResetTasks() | 303 tqt.ResetTasks() |
304 | 304 |
305 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) | 305 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) |
306 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) | 306 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) |
307 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) | 307 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) |
308 | 308 |
309 return nil | 309 return nil |
310 }, nil) | 310 }, nil) |
311 | 311 |
312 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) | 312 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) |
313 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) | 313 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) |
314 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 314 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
315 | 315 |
316 Convey("and reusing a closed context is bad time
s", func() { | 316 Convey("and reusing a closed context is bad time
s", func() { |
317 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") | 317 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") |
318 }) | 318 }) |
319 }) | 319 }) |
320 | 320 |
321 Convey("you can AddMulti as well", func() { | 321 Convey("you can AddMulti as well", func() { |
322 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 322 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
323 tq := tqS.Get(c) | 323 tq := tqS.Get(c) |
324 » » » » » tqt := tq.Testable() | 324 » » » » » tqt := tq.Raw().Testable() |
325 | 325 |
326 t.Name = "" | 326 t.Name = "" |
327 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} | 327 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} |
328 So(tq.AddMulti(tasks, ""), ShouldBeNil) | 328 So(tq.AddMulti(tasks, ""), ShouldBeNil) |
329 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) | 329 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) |
330 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) | 330 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) |
331 return nil | 331 return nil |
332 }, nil) | 332 }, nil) |
333 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) | 333 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) |
334 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 334 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
335 }) | 335 }) |
336 | 336 |
337 Convey("unless you add too many things", func() { | 337 Convey("unless you add too many things", func() { |
338 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 338 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
339 for i := 0; i < 5; i++ { | 339 for i := 0; i < 5; i++ { |
340 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) | 340 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) |
341 } | 341 } |
342 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") | 342 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") |
343 return nil | 343 return nil |
344 }, nil) | 344 }, nil) |
345 }) | 345 }) |
346 | 346 |
347 Convey("unless you Add to a bad queue", func() { | 347 Convey("unless you Add to a bad queue", func() { |
348 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 348 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
349 So(tqS.Get(c).Add(t, "meat").Error(), Sh
ouldContainSubstring, "UNKNOWN_QUEUE") | 349 So(tqS.Get(c).Add(t, "meat").Error(), Sh
ouldContainSubstring, "UNKNOWN_QUEUE") |
350 | 350 |
351 Convey("unless you add it!", func() { | 351 Convey("unless you add it!", func() { |
352 » » » » » » tqS.Get(c).Testable().CreateQueu
e("meat") | 352 » » » » » » tqS.GetRaw(c).Testable().CreateQ
ueue("meat") |
353 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) | 353 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) |
354 }) | 354 }) |
355 | 355 |
356 return nil | 356 return nil |
357 }, nil) | 357 }, nil) |
358 }) | 358 }) |
359 | 359 |
360 Convey("No other features are available, however", func(
) { | 360 Convey("No other features are available, however", func(
) { |
361 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 361 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
362 So(tqS.Get(c).Delete(t, "").Error(), Sho
uldContainSubstring, "cannot DeleteMulti from a transaction") | 362 So(tqS.Get(c).Delete(t, "").Error(), Sho
uldContainSubstring, "cannot DeleteMulti from a transaction") |
(...skipping 29 matching lines...) Expand all Loading... |
392 }() | 392 }() |
393 | 393 |
394 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) | 394 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) |
395 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 395 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
396 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 396 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
397 }) | 397 }) |
398 | 398 |
399 }) | 399 }) |
400 }) | 400 }) |
401 } | 401 } |
OLD | NEW |