puller_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub
  15. import (
  16. "errors"
  17. "reflect"
  18. "testing"
  19. "golang.org/x/net/context"
  20. )
  21. type fetcherService struct {
  22. service
  23. msgs [][]*Message
  24. }
  25. func (s *fetcherService) fetchMessages(ctx context.Context, subName string, maxMessages int64) ([]*Message, error) {
  26. if len(s.msgs) == 0 {
  27. return nil, errors.New("bang")
  28. }
  29. ret := s.msgs[0]
  30. s.msgs = s.msgs[1:]
  31. return ret, nil
  32. }
  33. func TestPuller(t *testing.T) {
  34. s := &fetcherService{
  35. msgs: [][]*Message{
  36. {{AckID: "a"}, {AckID: "b"}},
  37. {},
  38. {{AckID: "c"}, {AckID: "d"}},
  39. {{AckID: "e"}},
  40. },
  41. }
  42. c := &Client{projectID: "projid", s: s}
  43. pulled := make(chan string, 10)
  44. pull := &puller{
  45. Client: c,
  46. Sub: "subname",
  47. BatchSize: 2,
  48. Notify: func(ackID string) { pulled <- ackID },
  49. }
  50. got := []string{}
  51. for i := 0; i < 5; i++ {
  52. m, err := pull.Next(context.Background())
  53. got = append(got, m.AckID)
  54. if err != nil {
  55. t.Errorf("unexpected err from pull.Next: %v", err)
  56. }
  57. }
  58. _, err := pull.Next(context.Background())
  59. if err == nil {
  60. t.Errorf("unexpected err from pull.Next: %v", err)
  61. }
  62. want := []string{"a", "b", "c", "d", "e"}
  63. if !reflect.DeepEqual(got, want) {
  64. t.Errorf("pulled ack ids: got: %v ; want: %v", got, want)
  65. }
  66. }
  67. func TestPullerNotification(t *testing.T) {
  68. s := &fetcherService{
  69. msgs: [][]*Message{
  70. {{AckID: "a"}, {AckID: "b"}},
  71. {{AckID: "c"}, {AckID: "d"}},
  72. },
  73. }
  74. c := &Client{projectID: "projid", s: s}
  75. pulled := make(chan string, 10)
  76. pull := &puller{
  77. Client: c,
  78. Sub: "subname",
  79. BatchSize: 2,
  80. Notify: func(ackID string) { pulled <- ackID },
  81. }
  82. got := []string{}
  83. for i := 0; i < 3; i++ {
  84. m, err := pull.Next(context.Background())
  85. got = append(got, m.AckID)
  86. if err != nil {
  87. t.Errorf("unexpected err from pull.Next: %v", err)
  88. }
  89. }
  90. want := []string{"a", "b", "c"}
  91. if !reflect.DeepEqual(got, want) {
  92. t.Errorf("pulled ack ids: got: %v ; want: %v", got, want)
  93. }
  94. close(pulled)
  95. // We should have seen "d" written to the channel too, even though it hasn't been returned yet.
  96. pulledIDs := []string{}
  97. for id := range pulled {
  98. pulledIDs = append(pulledIDs, id)
  99. }
  100. want = append(want, "d")
  101. if !reflect.DeepEqual(pulledIDs, want) {
  102. t.Errorf("pulled ack ids: got: %v ; want: %v", pulledIDs, want)
  103. }
  104. }