| 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.Raw().(tqS.Testable) | 34 » » tqt := tq.Testable() |
| 35 » » So(tqt, ShouldNotBeNil) |
| 35 | 36 |
| 36 So(tq, ShouldNotBeNil) | 37 So(tq, ShouldNotBeNil) |
| 37 | 38 |
| 38 Convey("implements TQMultiReadWriter", func() { | 39 Convey("implements TQMultiReadWriter", func() { |
| 39 Convey("Add", func() { | 40 Convey("Add", func() { |
| 40 t := tq.NewTask("/hello/world") | 41 t := tq.NewTask("/hello/world") |
| 41 | 42 |
| 42 Convey("works", func() { | 43 Convey("works", func() { |
| 43 t.Delay = 4 * time.Second | 44 t.Delay = 4 * time.Second |
| 44 t.Header = http.Header{} | 45 t.Header = http.Header{} |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 t := &tqS.Task{Path: "/hello/world"} | 239 t := &tqS.Task{Path: "/hello/world"} |
| 239 So(tq.Add(t, ""), ShouldBeNil) | 240 So(tq.Add(t, ""), ShouldBeNil) |
| 240 | 241 |
| 241 t2 := &tqS.Task{Path: "/hi/city"} | 242 t2 := &tqS.Task{Path: "/hi/city"} |
| 242 So(tq.Add(t2, ""), ShouldBeNil) | 243 So(tq.Add(t2, ""), ShouldBeNil) |
| 243 | 244 |
| 244 So(tq.Delete(t2, ""), ShouldBeNil) | 245 So(tq.Delete(t2, ""), ShouldBeNil) |
| 245 | 246 |
| 246 Convey("can view regular tasks", func() { | 247 Convey("can view regular tasks", func() { |
| 247 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 248 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 248 » » » » » tqt := tqS.Get(c).Raw().(tqS.Testable) | 249 » » » » » tqt := tqS.Get(c).Testable() |
| 249 | 250 |
| 250 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 251 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
| 251 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 252 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
| 252 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) | 253 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) |
| 253 return nil | 254 return nil |
| 254 }, nil) | 255 }, nil) |
| 255 }) | 256 }) |
| 256 | 257 |
| 257 Convey("can add a new task", func() { | 258 Convey("can add a new task", func() { |
| 258 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 259 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
| 259 | 260 |
| 260 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { | 261 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { |
| 261 tq := tqS.Get(c) | 262 tq := tqS.Get(c) |
| 262 » » » » » tqt := tq.Raw().(tqS.Testable) | 263 » » » » » tqt := tq.Testable() |
| 263 | 264 |
| 264 So(tq.Add(t3, ""), ShouldBeNil) | 265 So(tq.Add(t3, ""), ShouldBeNil) |
| 265 So(t3.Name, ShouldEqual, "") | 266 So(t3.Name, ShouldEqual, "") |
| 266 | 267 |
| 267 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 268 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
| 268 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 269 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
| 269 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) | 270 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) |
| 270 return nil | 271 return nil |
| 271 }, nil) | 272 }, nil) |
| 272 So(err, ShouldBeNil) | 273 So(err, ShouldBeNil) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 284 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 285 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 285 }) | 286 }) |
| 286 | 287 |
| 287 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() { |
| 288 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 289 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
| 289 | 290 |
| 290 ttq := tqS.Interface(nil) | 291 ttq := tqS.Interface(nil) |
| 291 | 292 |
| 292 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 293 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 293 ttq = tqS.Get(c) | 294 ttq = tqS.Get(c) |
| 294 » » » » » tqt := ttq.Raw().(tqS.Testable) | 295 » » » » » tqt := ttq.Testable() |
| 295 | 296 |
| 296 So(ttq.Add(t3, ""), ShouldBeNil) | 297 So(ttq.Add(t3, ""), ShouldBeNil) |
| 297 | 298 |
| 298 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 299 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
| 299 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 300 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
| 300 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) | 301 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) |
| 301 | 302 |
| 302 tqt.ResetTasks() | 303 tqt.ResetTasks() |
| 303 | 304 |
| 304 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) | 305 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) |
| 305 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) | 306 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) |
| 306 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) | 307 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) |
| 307 | 308 |
| 308 return nil | 309 return nil |
| 309 }, nil) | 310 }, nil) |
| 310 | 311 |
| 311 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) | 312 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) |
| 312 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) | 313 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) |
| 313 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 314 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
| 314 | 315 |
| 315 Convey("and reusing a closed context is bad time
s", func() { | 316 Convey("and reusing a closed context is bad time
s", func() { |
| 316 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") | 317 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") |
| 317 }) | 318 }) |
| 318 }) | 319 }) |
| 319 | 320 |
| 320 Convey("you can AddMulti as well", func() { | 321 Convey("you can AddMulti as well", func() { |
| 321 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 322 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 322 tq := tqS.Get(c) | 323 tq := tqS.Get(c) |
| 323 » » » » » tqt := tq.Raw().(tqS.Testable) | 324 » » » » » tqt := tq.Testable() |
| 324 | 325 |
| 325 t.Name = "" | 326 t.Name = "" |
| 326 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} | 327 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} |
| 327 So(tq.AddMulti(tasks, ""), ShouldBeNil) | 328 So(tq.AddMulti(tasks, ""), ShouldBeNil) |
| 328 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) | 329 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) |
| 329 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) | 330 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) |
| 330 return nil | 331 return nil |
| 331 }, nil) | 332 }, nil) |
| 332 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) | 333 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) |
| 333 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 334 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
| 334 }) | 335 }) |
| 335 | 336 |
| 336 Convey("unless you add too many things", func() { | 337 Convey("unless you add too many things", func() { |
| 337 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 338 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 338 for i := 0; i < 5; i++ { | 339 for i := 0; i < 5; i++ { |
| 339 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) | 340 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) |
| 340 } | 341 } |
| 341 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") | 342 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") |
| 342 return nil | 343 return nil |
| 343 }, nil) | 344 }, nil) |
| 344 }) | 345 }) |
| 345 | 346 |
| 346 Convey("unless you Add to a bad queue", func() { | 347 Convey("unless you Add to a bad queue", func() { |
| 347 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 348 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 348 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") |
| 349 | 350 |
| 350 Convey("unless you add it!", func() { | 351 Convey("unless you add it!", func() { |
| 351 » » » » » » tqS.Get(c).Raw().(tqS.Testable).
CreateQueue("meat") | 352 » » » » » » tqS.Get(c).Testable().CreateQueu
e("meat") |
| 352 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) | 353 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) |
| 353 }) | 354 }) |
| 354 | 355 |
| 355 return nil | 356 return nil |
| 356 }, nil) | 357 }, nil) |
| 357 }) | 358 }) |
| 358 | 359 |
| 359 Convey("No other features are available, however", func(
) { | 360 Convey("No other features are available, however", func(
) { |
| 360 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 361 dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 361 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 13 matching lines...) Expand all Loading... |
| 375 | 376 |
| 376 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) | 377 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) |
| 377 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 378 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
| 378 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 379 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 379 }) | 380 }) |
| 380 | 381 |
| 381 Convey("likewise, a panic doesn't schedule anything", fu
nc() { | 382 Convey("likewise, a panic doesn't schedule anything", fu
nc() { |
| 382 func() { | 383 func() { |
| 383 defer func() { recover() }() | 384 defer func() { recover() }() |
| 384 dsS.Get(c).RunInTransaction(func(c conte
xt.Context) error { | 385 dsS.Get(c).RunInTransaction(func(c conte
xt.Context) error { |
| 385 » » » » » » tq := tqS.Get(c).(interface { | 386 » » » » » » tq := tqS.Get(c) |
| 386 » » » » » » » tqS.Testable | |
| 387 » » » » » » » tqS.Interface | |
| 388 » » » » » » }) | |
| 389 | 387 |
| 390 So(tq.Add(tq.NewTask("/sandwitch
/victory"), ""), ShouldBeNil) | 388 So(tq.Add(tq.NewTask("/sandwitch
/victory"), ""), ShouldBeNil) |
| 391 | 389 |
| 392 panic(fmt.Errorf("nooooo")) | 390 panic(fmt.Errorf("nooooo")) |
| 393 }, nil) | 391 }, nil) |
| 394 }() | 392 }() |
| 395 | 393 |
| 396 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) | 394 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) |
| 397 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 395 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
| 398 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 396 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 399 }) | 397 }) |
| 400 | 398 |
| 401 }) | 399 }) |
| 402 }) | 400 }) |
| 403 } | 401 } |
| OLD | NEW |