| 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" |
| 11 "testing" | 11 "testing" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 dsS "github.com/luci/gae/service/datastore" | 14 dsS "github.com/luci/gae/service/datastore" |
| 15 "github.com/luci/gae/service/info" | 15 "github.com/luci/gae/service/info" |
| 16 tqS "github.com/luci/gae/service/taskqueue" | 16 tqS "github.com/luci/gae/service/taskqueue" |
| 17 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/clock/testclock" | 18 "github.com/luci/luci-go/common/clock/testclock" |
| 19 "github.com/luci/luci-go/common/mathrand" | 19 "github.com/luci/luci-go/common/mathrand" |
| 20 . "github.com/luci/luci-go/common/testing/assertions" |
| 20 . "github.com/smartystreets/goconvey/convey" | 21 . "github.com/smartystreets/goconvey/convey" |
| 21 "golang.org/x/net/context" | 22 "golang.org/x/net/context" |
| 22 ) | 23 ) |
| 23 | 24 |
| 24 func TestTaskQueue(t *testing.T) { | 25 func TestTaskQueue(t *testing.T) { |
| 25 t.Parallel() | 26 t.Parallel() |
| 26 | 27 |
| 27 Convey("TaskQueue", t, func() { | 28 Convey("TaskQueue", t, func() { |
| 28 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) | 29 now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC) |
| 29 c, tc := testclock.UseTime(context.Background(), now) | 30 c, tc := testclock.UseTime(context.Background(), now) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 So(tq.Delete(t, ""), ShouldBeNil) | 108 So(tq.Delete(t, ""), ShouldBeNil) |
| 108 | 109 |
| 109 // can't add a deleted task! | 110 // can't add a deleted task! |
| 110 So(tq.Add(t, ""), ShouldEqual, tqS.ErrTa
skAlreadyAdded) | 111 So(tq.Add(t, ""), ShouldEqual, tqS.ErrTa
skAlreadyAdded) |
| 111 }) | 112 }) |
| 112 | 113 |
| 113 Convey("cannot set ETA+Delay", func() { | 114 Convey("cannot set ETA+Delay", func() { |
| 114 t.ETA = clock.Now(c).Add(time.Hour) | 115 t.ETA = clock.Now(c).Add(time.Hour) |
| 115 tc.Add(time.Second) | 116 tc.Add(time.Second) |
| 116 t.Delay = time.Hour | 117 t.Delay = time.Hour |
| 117 » » » » » So(func() { tq.Add(t, "") }, ShouldPanic
) | 118 » » » » » So(func() { |
| 119 » » » » » » So(tq.Add(t, ""), ShouldBeNil) |
| 120 » » » » » }, ShouldPanic) |
| 118 }) | 121 }) |
| 119 | 122 |
| 120 Convey("must use a reasonable method", func() { | 123 Convey("must use a reasonable method", func() { |
| 121 t.Method = "Crystal" | 124 t.Method = "Crystal" |
| 122 So(tq.Add(t, "").Error(), ShouldContainS
ubstring, "bad method") | 125 So(tq.Add(t, "").Error(), ShouldContainS
ubstring, "bad method") |
| 123 }) | 126 }) |
| 124 | 127 |
| 125 Convey("payload gets dumped for non POST/PUT met
hods", func() { | 128 Convey("payload gets dumped for non POST/PUT met
hods", func() { |
| 126 t.Method = "HEAD" | 129 t.Method = "HEAD" |
| 127 t.Payload = []byte("coool") | 130 t.Payload = []byte("coool") |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 Convey("works with transactions", func() { | 241 Convey("works with transactions", func() { |
| 239 t := &tqS.Task{Path: "/hello/world"} | 242 t := &tqS.Task{Path: "/hello/world"} |
| 240 So(tq.Add(t, ""), ShouldBeNil) | 243 So(tq.Add(t, ""), ShouldBeNil) |
| 241 | 244 |
| 242 t2 := &tqS.Task{Path: "/hi/city"} | 245 t2 := &tqS.Task{Path: "/hi/city"} |
| 243 So(tq.Add(t2, ""), ShouldBeNil) | 246 So(tq.Add(t2, ""), ShouldBeNil) |
| 244 | 247 |
| 245 So(tq.Delete(t2, ""), ShouldBeNil) | 248 So(tq.Delete(t2, ""), ShouldBeNil) |
| 246 | 249 |
| 247 Convey("can view regular tasks", func() { | 250 Convey("can view regular tasks", func() { |
| 248 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 251 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 249 tqt := tqS.GetRaw(c).Testable() | 252 tqt := tqS.GetRaw(c).Testable() |
| 250 | 253 |
| 251 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 254 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
| 252 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 255 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
| 253 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) | 256 So(tqt.GetTransactionTasks()["default"],
ShouldBeNil) |
| 254 return nil | 257 return nil |
| 255 » » » » }, nil) | 258 » » » » }, nil), ShouldBeNil) |
| 256 }) | 259 }) |
| 257 | 260 |
| 258 Convey("can add a new task", func() { | 261 Convey("can add a new task", func() { |
| 259 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 262 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
| 260 | 263 |
| 261 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { | 264 err := dsS.Get(c).RunInTransaction(func(c contex
t.Context) error { |
| 262 tq := tqS.Get(c) | 265 tq := tqS.Get(c) |
| 263 tqt := tq.Testable() | 266 tqt := tq.Testable() |
| 264 | 267 |
| 265 So(tq.Add(t3, ""), ShouldBeNil) | 268 So(tq.Add(t3, ""), ShouldBeNil) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 283 | 286 |
| 284 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 287 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
| 285 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 288 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 286 }) | 289 }) |
| 287 | 290 |
| 288 Convey("can add a new task (but reset the state in a tes
t)", func() { | 291 Convey("can add a new task (but reset the state in a tes
t)", func() { |
| 289 t3 := &tqS.Task{Path: "/sandwitch/victory"} | 292 t3 := &tqS.Task{Path: "/sandwitch/victory"} |
| 290 | 293 |
| 291 ttq := tqS.Interface(nil) | 294 ttq := tqS.Interface(nil) |
| 292 | 295 |
| 293 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 296 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 294 ttq = tqS.Get(c) | 297 ttq = tqS.Get(c) |
| 295 tqt := ttq.Testable() | 298 tqt := ttq.Testable() |
| 296 | 299 |
| 297 So(ttq.Add(t3, ""), ShouldBeNil) | 300 So(ttq.Add(t3, ""), ShouldBeNil) |
| 298 | 301 |
| 299 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) | 302 So(tqt.GetScheduledTasks()["default"][t.
Name], ShouldResemble, t) |
| 300 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) | 303 So(tqt.GetTombstonedTasks()["default"][t
2.Name], ShouldResemble, t2) |
| 301 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) | 304 So(tqt.GetTransactionTasks()["default"][
0], ShouldResemble, t3) |
| 302 | 305 |
| 303 tqt.ResetTasks() | 306 tqt.ResetTasks() |
| 304 | 307 |
| 305 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) | 308 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 0) |
| 306 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) | 309 So(len(tqt.GetTombstonedTasks()["default
"]), ShouldEqual, 0) |
| 307 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) | 310 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 0) |
| 308 | 311 |
| 309 return nil | 312 return nil |
| 310 » » » » }, nil) | 313 » » » » }, nil), ShouldBeNil) |
| 311 | 314 |
| 312 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) | 315 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 0) |
| 313 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) | 316 So(len(tqt.GetTombstonedTasks()["default"]), Sho
uldEqual, 0) |
| 314 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 317 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
| 315 | 318 |
| 316 Convey("and reusing a closed context is bad time
s", func() { | 319 Convey("and reusing a closed context is bad time
s", func() { |
| 317 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") | 320 So(ttq.Add(nil, "").Error(), ShouldConta
inSubstring, "expired") |
| 318 }) | 321 }) |
| 319 }) | 322 }) |
| 320 | 323 |
| 321 Convey("you can AddMulti as well", func() { | 324 Convey("you can AddMulti as well", func() { |
| 322 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 325 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 323 tq := tqS.Get(c) | 326 tq := tqS.Get(c) |
| 324 tqt := tq.Testable() | 327 tqt := tq.Testable() |
| 325 | 328 |
| 326 t.Name = "" | 329 t.Name = "" |
| 327 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} | 330 tasks := []*tqS.Task{t.Duplicate(), t.Du
plicate(), t.Duplicate()} |
| 328 So(tq.AddMulti(tasks, ""), ShouldBeNil) | 331 So(tq.AddMulti(tasks, ""), ShouldBeNil) |
| 329 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) | 332 So(len(tqt.GetScheduledTasks()["default"
]), ShouldEqual, 1) |
| 330 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) | 333 So(len(tqt.GetTransactionTasks()["defaul
t"]), ShouldEqual, 3) |
| 331 return nil | 334 return nil |
| 332 » » » » }, nil) | 335 » » » » }, nil), ShouldBeNil) |
| 333 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) | 336 So(len(tqt.GetScheduledTasks()["default"]), Shou
ldEqual, 4) |
| 334 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) | 337 So(len(tqt.GetTransactionTasks()["default"]), Sh
ouldEqual, 0) |
| 335 }) | 338 }) |
| 336 | 339 |
| 337 Convey("unless you add too many things", func() { | 340 Convey("unless you add too many things", func() { |
| 338 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 341 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 339 for i := 0; i < 5; i++ { | 342 for i := 0; i < 5; i++ { |
| 340 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) | 343 So(tqS.Get(c).Add(t.Duplicate(),
""), ShouldBeNil) |
| 341 } | 344 } |
| 342 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") | 345 So(tqS.Get(c).Add(t, "").Error(), Should
ContainSubstring, "BAD_REQUEST") |
| 343 return nil | 346 return nil |
| 344 » » » » }, nil) | 347 » » » » }, nil), ShouldBeNil) |
| 345 }) | 348 }) |
| 346 | 349 |
| 347 Convey("unless you Add to a bad queue", func() { | 350 Convey("unless you Add to a bad queue", func() { |
| 348 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 351 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 349 So(tqS.Get(c).Add(t, "meat").Error(), Sh
ouldContainSubstring, "UNKNOWN_QUEUE") | 352 So(tqS.Get(c).Add(t, "meat").Error(), Sh
ouldContainSubstring, "UNKNOWN_QUEUE") |
| 350 | 353 |
| 351 Convey("unless you add it!", func() { | 354 Convey("unless you add it!", func() { |
| 352 tqS.GetRaw(c).Testable().CreateQ
ueue("meat") | 355 tqS.GetRaw(c).Testable().CreateQ
ueue("meat") |
| 353 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) | 356 So(tqS.Get(c).Add(t, "meat"), Sh
ouldBeNil) |
| 354 }) | 357 }) |
| 355 | 358 |
| 356 return nil | 359 return nil |
| 357 » » » » }, nil) | 360 » » » » }, nil), ShouldBeNil) |
| 358 }) | 361 }) |
| 359 | 362 |
| 360 Convey("No other features are available, however", func(
) { | 363 Convey("No other features are available, however", func(
) { |
| 361 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 364 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 362 So(tqS.Get(c).Delete(t, "").Error(), Sho
uldContainSubstring, "cannot DeleteMulti from a transaction") | 365 So(tqS.Get(c).Delete(t, "").Error(), Sho
uldContainSubstring, "cannot DeleteMulti from a transaction") |
| 363 So(tqS.Get(c).Purge("").Error(), ShouldC
ontainSubstring, "cannot Purge from a transaction") | 366 So(tqS.Get(c).Purge("").Error(), ShouldC
ontainSubstring, "cannot Purge from a transaction") |
| 364 _, err := tqS.Get(c).Stats("") | 367 _, err := tqS.Get(c).Stats("") |
| 365 So(err.Error(), ShouldContainSubstring,
"cannot Stats from a transaction") | 368 So(err.Error(), ShouldContainSubstring,
"cannot Stats from a transaction") |
| 366 return nil | 369 return nil |
| 367 » » » » }, nil) | 370 » » » » }, nil), ShouldBeNil) |
| 368 }) | 371 }) |
| 369 | 372 |
| 370 Convey("adding a new task only happens if we don't errou
t", func() { | 373 Convey("adding a new task only happens if we don't errou
t", func() { |
| 371 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { | 374 » » » » So(dsS.Get(c).RunInTransaction(func(c context.Co
ntext) error { |
| 372 t3 := tq.NewTask("/sandwitch/victory") | 375 t3 := tq.NewTask("/sandwitch/victory") |
| 373 So(tqS.Get(c).Add(t3, ""), ShouldBeNil) | 376 So(tqS.Get(c).Add(t3, ""), ShouldBeNil) |
| 374 return fmt.Errorf("nooooo") | 377 return fmt.Errorf("nooooo") |
| 375 » » » » }, nil) | 378 » » » » }, nil), ShouldErrLike, "nooooo") |
| 376 | 379 |
| 377 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) | 380 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) |
| 378 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 381 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
| 379 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 382 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 380 }) | 383 }) |
| 381 | 384 |
| 382 Convey("likewise, a panic doesn't schedule anything", fu
nc() { | 385 Convey("likewise, a panic doesn't schedule anything", fu
nc() { |
| 383 func() { | 386 func() { |
| 384 » » » » » defer func() { recover() }() | 387 » » » » » defer func() { _ = recover() }() |
| 385 » » » » » dsS.Get(c).RunInTransaction(func(c conte
xt.Context) error { | 388 » » » » » So(dsS.Get(c).RunInTransaction(func(c co
ntext.Context) error { |
| 386 tq := tqS.Get(c) | 389 tq := tqS.Get(c) |
| 387 | 390 |
| 388 So(tq.Add(tq.NewTask("/sandwitch
/victory"), ""), ShouldBeNil) | 391 So(tq.Add(tq.NewTask("/sandwitch
/victory"), ""), ShouldBeNil) |
| 389 | 392 |
| 390 panic(fmt.Errorf("nooooo")) | 393 panic(fmt.Errorf("nooooo")) |
| 391 » » » » » }, nil) | 394 » » » » » }, nil), ShouldBeNil) |
| 392 }() | 395 }() |
| 393 | 396 |
| 394 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) | 397 So(tqt.GetScheduledTasks()["default"][t.Name], S
houldResemble, t) |
| 395 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) | 398 So(tqt.GetTombstonedTasks()["default"][t2.Name],
ShouldResemble, t2) |
| 396 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) | 399 So(tqt.GetTransactionTasks()["default"], ShouldB
eNil) |
| 397 }) | 400 }) |
| 398 | 401 |
| 399 }) | 402 }) |
| 400 }) | 403 }) |
| 401 } | 404 } |
| OLD | NEW |