taskqueue_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package taskqueue
  5. import (
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "testing"
  10. "google.golang.org/appengine"
  11. "google.golang.org/appengine/internal"
  12. "google.golang.org/appengine/internal/aetesting"
  13. pb "google.golang.org/appengine/internal/taskqueue"
  14. )
  15. func TestAddErrors(t *testing.T) {
  16. var tests = []struct {
  17. err, want error
  18. sameErr bool // if true, should return err exactly
  19. }{
  20. {
  21. err: &internal.APIError{
  22. Service: "taskqueue",
  23. Code: int32(pb.TaskQueueServiceError_TASK_ALREADY_EXISTS),
  24. },
  25. want: ErrTaskAlreadyAdded,
  26. },
  27. {
  28. err: &internal.APIError{
  29. Service: "taskqueue",
  30. Code: int32(pb.TaskQueueServiceError_TOMBSTONED_TASK),
  31. },
  32. want: ErrTaskAlreadyAdded,
  33. },
  34. {
  35. err: &internal.APIError{
  36. Service: "taskqueue",
  37. Code: int32(pb.TaskQueueServiceError_UNKNOWN_QUEUE),
  38. },
  39. want: errors.New("not used"),
  40. sameErr: true,
  41. },
  42. }
  43. for _, tc := range tests {
  44. c := aetesting.FakeSingleContext(t, "taskqueue", "Add", func(req *pb.TaskQueueAddRequest, res *pb.TaskQueueAddResponse) error {
  45. // don't fill in any of the response
  46. return tc.err
  47. })
  48. task := &Task{Path: "/worker", Method: "PULL"}
  49. _, err := Add(c, task, "a-queue")
  50. want := tc.want
  51. if tc.sameErr {
  52. want = tc.err
  53. }
  54. if err != want {
  55. t.Errorf("Add with tc.err = %v, got %#v, want = %#v", tc.err, err, want)
  56. }
  57. }
  58. }
  59. func TestAddMulti(t *testing.T) {
  60. c := aetesting.FakeSingleContext(t, "taskqueue", "BulkAdd", func(req *pb.TaskQueueBulkAddRequest, res *pb.TaskQueueBulkAddResponse) error {
  61. res.Taskresult = []*pb.TaskQueueBulkAddResponse_TaskResult{
  62. {
  63. Result: pb.TaskQueueServiceError_OK.Enum(),
  64. },
  65. {
  66. Result: pb.TaskQueueServiceError_TASK_ALREADY_EXISTS.Enum(),
  67. },
  68. {
  69. Result: pb.TaskQueueServiceError_TOMBSTONED_TASK.Enum(),
  70. },
  71. {
  72. Result: pb.TaskQueueServiceError_INTERNAL_ERROR.Enum(),
  73. },
  74. }
  75. return nil
  76. })
  77. tasks := []*Task{
  78. {Path: "/worker", Method: "PULL"},
  79. {Path: "/worker", Method: "PULL"},
  80. {Path: "/worker", Method: "PULL"},
  81. {Path: "/worker", Method: "PULL"},
  82. }
  83. r, err := AddMulti(c, tasks, "a-queue")
  84. if len(r) != len(tasks) {
  85. t.Fatalf("AddMulti returned %d tasks, want %d", len(r), len(tasks))
  86. }
  87. want := appengine.MultiError{
  88. nil,
  89. ErrTaskAlreadyAdded,
  90. ErrTaskAlreadyAdded,
  91. &internal.APIError{
  92. Service: "taskqueue",
  93. Code: int32(pb.TaskQueueServiceError_INTERNAL_ERROR),
  94. },
  95. }
  96. if !reflect.DeepEqual(err, want) {
  97. t.Errorf("AddMulti got %v, wanted %v", err, want)
  98. }
  99. }
  100. func TestAddWithEmptyPath(t *testing.T) {
  101. c := aetesting.FakeSingleContext(t, "taskqueue", "Add", func(req *pb.TaskQueueAddRequest, res *pb.TaskQueueAddResponse) error {
  102. if got, want := string(req.Url), "/_ah/queue/a-queue"; got != want {
  103. return fmt.Errorf("req.Url = %q; want %q", got, want)
  104. }
  105. return nil
  106. })
  107. if _, err := Add(c, &Task{}, "a-queue"); err != nil {
  108. t.Fatalf("Add: %v", err)
  109. }
  110. }