| 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 » rdsS "github.com/luci/gae/service/rawdatastore" | 14 » dsS "github.com/luci/gae/service/datastore" |
| 15 tqS "github.com/luci/gae/service/taskqueue" | 15 tqS "github.com/luci/gae/service/taskqueue" |
| 16 "github.com/luci/luci-go/common/clock" | 16 "github.com/luci/luci-go/common/clock" |
| 17 "github.com/luci/luci-go/common/clock/testclock" | 17 "github.com/luci/luci-go/common/clock/testclock" |
| 18 "github.com/luci/luci-go/common/mathrand" | 18 "github.com/luci/luci-go/common/mathrand" |
| 19 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 20 "golang.org/x/net/context" | 20 "golang.org/x/net/context" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 func TestTaskQueue(t *testing.T) { | 23 func TestTaskQueue(t *testing.T) { |
| 24 t.Parallel() | 24 t.Parallel() |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 So(err, ShouldBeNil) | 223 So(err, ShouldBeNil) |
| 224 | 224 |
| 225 t2 := &tqS.Task{Path: "/hi/city"} | 225 t2 := &tqS.Task{Path: "/hi/city"} |
| 226 tEnQ2, err := tq.Add(t2, "") | 226 tEnQ2, err := tq.Add(t2, "") |
| 227 So(err, ShouldBeNil) | 227 So(err, ShouldBeNil) |
| 228 | 228 |
| 229 err = tq.Delete(tEnQ2, "") | 229 err = tq.Delete(tEnQ2, "") |
| 230 So(err, ShouldBeNil) | 230 So(err, ShouldBeNil) |
| 231 | 231 |
| 232 Convey("can view regular tasks", func() { | 232 Convey("can view regular tasks", func() { |
| 233 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 233 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 234 tq := tqS.Get(c).(interface { | 234 tq := tqS.Get(c).(interface { |
| 235 tqS.Testable | 235 tqS.Testable |
| 236 tqS.Interface | 236 tqS.Interface |
| 237 }) | 237 }) |
| 238 | 238 |
| 239 So(tq.GetScheduledTasks()["default"][tEn
Q.Name], ShouldResemble, tEnQ) | 239 So(tq.GetScheduledTasks()["default"][tEn
Q.Name], ShouldResemble, tEnQ) |
| 240 So(tq.GetTombstonedTasks()["default"][tE
nQ2.Name], ShouldResemble, tEnQ2) | 240 So(tq.GetTombstonedTasks()["default"][tE
nQ2.Name], ShouldResemble, tEnQ2) |
| 241 So(tq.GetTransactionTasks()["default"],
ShouldBeNil) | 241 So(tq.GetTransactionTasks()["default"],
ShouldBeNil) |
| 242 return nil | 242 return nil |
| 243 }, nil) | 243 }, nil) |
| 244 }) | 244 }) |
| 245 | 245 |
| 246 Convey("can add a new task", func() { | 246 Convey("can add a new task", func() { |
| 247 tEnQ3 := (*tqS.Task)(nil) | 247 tEnQ3 := (*tqS.Task)(nil) |
| 248 | 248 |
| 249 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 249 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 250 tq := tqS.Get(c).(interface { | 250 tq := tqS.Get(c).(interface { |
| 251 tqS.Testable | 251 tqS.Testable |
| 252 tqS.Interface | 252 tqS.Interface |
| 253 }) | 253 }) |
| 254 | 254 |
| 255 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} | 255 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} |
| 256 tEnQ3, err = tq.Add(t3, "") | 256 tEnQ3, err = tq.Add(t3, "") |
| 257 So(err, ShouldBeNil) | 257 So(err, ShouldBeNil) |
| 258 | 258 |
| 259 So(tq.GetScheduledTasks()["default"][tEn
Q.Name], ShouldResemble, tEnQ) | 259 So(tq.GetScheduledTasks()["default"][tEn
Q.Name], ShouldResemble, tEnQ) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 278 }) | 278 }) |
| 279 | 279 |
| 280 Convey("can a new task (but reset the state in a test)",
func() { | 280 Convey("can a new task (but reset the state in a test)",
func() { |
| 281 tEnQ3 := (*tqS.Task)(nil) | 281 tEnQ3 := (*tqS.Task)(nil) |
| 282 | 282 |
| 283 ttq := interface { | 283 ttq := interface { |
| 284 tqS.Testable | 284 tqS.Testable |
| 285 tqS.Interface | 285 tqS.Interface |
| 286 }(nil) | 286 }(nil) |
| 287 | 287 |
| 288 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 288 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 289 ttq = tqS.Get(c).(interface { | 289 ttq = tqS.Get(c).(interface { |
| 290 tqS.Testable | 290 tqS.Testable |
| 291 tqS.Interface | 291 tqS.Interface |
| 292 }) | 292 }) |
| 293 | 293 |
| 294 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} | 294 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} |
| 295 tEnQ3, err = ttq.Add(t3, "") | 295 tEnQ3, err = ttq.Add(t3, "") |
| 296 So(err, ShouldBeNil) | 296 So(err, ShouldBeNil) |
| 297 | 297 |
| 298 So(ttq.GetScheduledTasks()["default"][tE
nQ.Name], ShouldResemble, tEnQ) | 298 So(ttq.GetScheduledTasks()["default"][tE
nQ.Name], ShouldResemble, tEnQ) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 312 So(len(tq.GetTombstonedTasks()["default"]), Shou
ldEqual, 0) | 312 So(len(tq.GetTombstonedTasks()["default"]), Shou
ldEqual, 0) |
| 313 So(len(tq.GetTransactionTasks()["default"]), Sho
uldEqual, 0) | 313 So(len(tq.GetTransactionTasks()["default"]), Sho
uldEqual, 0) |
| 314 | 314 |
| 315 Convey("and reusing a closed context is bad time
s", func() { | 315 Convey("and reusing a closed context is bad time
s", func() { |
| 316 _, err := ttq.Add(nil, "") | 316 _, err := ttq.Add(nil, "") |
| 317 So(err.Error(), ShouldContainSubstring,
"expired") | 317 So(err.Error(), ShouldContainSubstring,
"expired") |
| 318 }) | 318 }) |
| 319 }) | 319 }) |
| 320 | 320 |
| 321 Convey("you can AddMulti as well", func() { | 321 Convey("you can AddMulti as well", func() { |
| 322 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 322 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 323 tq := tqS.Get(c).(interface { | 323 tq := tqS.Get(c).(interface { |
| 324 tqS.Testable | 324 tqS.Testable |
| 325 tqS.Interface | 325 tqS.Interface |
| 326 }) | 326 }) |
| 327 _, err := tq.AddMulti([]*tqS.Task{t, t,
t}, "") | 327 _, err := tq.AddMulti([]*tqS.Task{t, t,
t}, "") |
| 328 So(err, ShouldBeNil) | 328 So(err, ShouldBeNil) |
| 329 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 1) | 329 So(len(tq.GetScheduledTasks()["default"]
), ShouldEqual, 1) |
| 330 So(len(tq.GetTransactionTasks()["default
"]), ShouldEqual, 3) | 330 So(len(tq.GetTransactionTasks()["default
"]), ShouldEqual, 3) |
| 331 return nil | 331 return nil |
| 332 }, nil) | 332 }, nil) |
| 333 So(len(tq.GetScheduledTasks()["default"]), Shoul
dEqual, 4) | 333 So(len(tq.GetScheduledTasks()["default"]), Shoul
dEqual, 4) |
| 334 So(len(tq.GetTransactionTasks()["default"]), Sho
uldEqual, 0) | 334 So(len(tq.GetTransactionTasks()["default"]), Sho
uldEqual, 0) |
| 335 }) | 335 }) |
| 336 | 336 |
| 337 Convey("unless you add too many things", func() { | 337 Convey("unless you add too many things", func() { |
| 338 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) 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 _, err = tqS.Get(c).Add(t, "") | 340 _, err = tqS.Get(c).Add(t, "") |
| 341 So(err, ShouldBeNil) | 341 So(err, ShouldBeNil) |
| 342 } | 342 } |
| 343 _, err = tqS.Get(c).Add(t, "") | 343 _, err = tqS.Get(c).Add(t, "") |
| 344 So(err.Error(), ShouldContainSubstring,
"BAD_REQUEST") | 344 So(err.Error(), ShouldContainSubstring,
"BAD_REQUEST") |
| 345 return nil | 345 return nil |
| 346 }, nil) | 346 }, nil) |
| 347 }) | 347 }) |
| 348 | 348 |
| 349 Convey("unless you Add to a bad queue", func() { | 349 Convey("unless you Add to a bad queue", func() { |
| 350 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 350 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 351 _, err = tqS.Get(c).Add(t, "meat") | 351 _, err = tqS.Get(c).Add(t, "meat") |
| 352 So(err.Error(), ShouldContainSubstring,
"UNKNOWN_QUEUE") | 352 So(err.Error(), ShouldContainSubstring,
"UNKNOWN_QUEUE") |
| 353 | 353 |
| 354 Convey("unless you add it!", func() { | 354 Convey("unless you add it!", func() { |
| 355 tqS.Get(c).(tqS.Testable).Create
Queue("meat") | 355 tqS.Get(c).(tqS.Testable).Create
Queue("meat") |
| 356 _, err = tqS.Get(c).Add(t, "meat
") | 356 _, err = tqS.Get(c).Add(t, "meat
") |
| 357 So(err, ShouldBeNil) | 357 So(err, ShouldBeNil) |
| 358 }) | 358 }) |
| 359 | 359 |
| 360 return nil | 360 return nil |
| 361 }, nil) | 361 }, nil) |
| 362 }) | 362 }) |
| 363 | 363 |
| 364 Convey("No other features are available, however", func(
) { | 364 Convey("No other features are available, however", func(
) { |
| 365 err := error(nil) | 365 err := error(nil) |
| 366 func() { | 366 func() { |
| 367 defer func() { err = recover().(error) }
() | 367 defer func() { err = recover().(error) }
() |
| 368 » » » » » rdsS.Get(c).RunInTransaction(func(c cont
ext.Context) error { | 368 » » » » » dsS.Get(c).RunInTransaction(func(c conte
xt.Context) error { |
| 369 tqS.Get(c).Delete(t, "") | 369 tqS.Get(c).Delete(t, "") |
| 370 return nil | 370 return nil |
| 371 }, nil) | 371 }, nil) |
| 372 }() | 372 }() |
| 373 So(err.Error(), ShouldContainSubstring, "TaskQue
ue.Delete") | 373 So(err.Error(), ShouldContainSubstring, "TaskQue
ue.Delete") |
| 374 }) | 374 }) |
| 375 | 375 |
| 376 Convey("adding a new task only happens if we don't errou
t", func() { | 376 Convey("adding a new task only happens if we don't errou
t", func() { |
| 377 » » » » rdsS.Get(c).RunInTransaction(func(c context.Cont
ext) error { | 377 » » » » dsS.Get(c).RunInTransaction(func(c context.Conte
xt) error { |
| 378 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} | 378 t3 := &tqS.Task{Path: "/sandwitch/victor
y"} |
| 379 _, err = tqS.Get(c).Add(t3, "") | 379 _, err = tqS.Get(c).Add(t3, "") |
| 380 So(err, ShouldBeNil) | 380 So(err, ShouldBeNil) |
| 381 return fmt.Errorf("nooooo") | 381 return fmt.Errorf("nooooo") |
| 382 }, nil) | 382 }, nil) |
| 383 | 383 |
| 384 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) | 384 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) |
| 385 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) | 385 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) |
| 386 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) | 386 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) |
| 387 }) | 387 }) |
| 388 | 388 |
| 389 Convey("likewise, a panic doesn't schedule anything", fu
nc() { | 389 Convey("likewise, a panic doesn't schedule anything", fu
nc() { |
| 390 func() { | 390 func() { |
| 391 defer func() { recover() }() | 391 defer func() { recover() }() |
| 392 » » » » » rdsS.Get(c).RunInTransaction(func(c cont
ext.Context) error { | 392 » » » » » dsS.Get(c).RunInTransaction(func(c conte
xt.Context) error { |
| 393 tq := tqS.Get(c).(interface { | 393 tq := tqS.Get(c).(interface { |
| 394 tqS.Testable | 394 tqS.Testable |
| 395 tqS.Interface | 395 tqS.Interface |
| 396 }) | 396 }) |
| 397 | 397 |
| 398 t3 := &tqS.Task{Path: "/sandwitc
h/victory"} | 398 t3 := &tqS.Task{Path: "/sandwitc
h/victory"} |
| 399 _, err = tq.Add(t3, "") | 399 _, err = tq.Add(t3, "") |
| 400 So(err, ShouldBeNil) | 400 So(err, ShouldBeNil) |
| 401 | 401 |
| 402 panic(fmt.Errorf("nooooo")) | 402 panic(fmt.Errorf("nooooo")) |
| 403 }, nil) | 403 }, nil) |
| 404 }() | 404 }() |
| 405 | 405 |
| 406 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) | 406 So(tq.GetScheduledTasks()["default"][tEnQ.Name],
ShouldResemble, tEnQ) |
| 407 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) | 407 So(tq.GetTombstonedTasks()["default"][tEnQ2.Name
], ShouldResemble, tEnQ2) |
| 408 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) | 408 So(tq.GetTransactionTasks()["default"], ShouldBe
Nil) |
| 409 }) | 409 }) |
| 410 | 410 |
| 411 }) | 411 }) |
| 412 }) | 412 }) |
| 413 } | 413 } |
| OLD | NEW |