You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

181 lines
4.2 KiB

  1. // Copyright 2016 Google LLC
  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. "fmt"
  17. "testing"
  18. "time"
  19. "cloud.google.com/go/internal/testutil"
  20. "golang.org/x/net/context"
  21. "google.golang.org/api/iterator"
  22. )
  23. // All returns the remaining subscriptions from this iterator.
  24. func slurpSubs(it *SubscriptionIterator) ([]*Subscription, error) {
  25. var subs []*Subscription
  26. for {
  27. switch sub, err := it.Next(); err {
  28. case nil:
  29. subs = append(subs, sub)
  30. case iterator.Done:
  31. return subs, nil
  32. default:
  33. return nil, err
  34. }
  35. }
  36. }
  37. func TestSubscriptionID(t *testing.T) {
  38. const id = "id"
  39. c := &Client{projectID: "projid"}
  40. s := c.Subscription(id)
  41. if got, want := s.ID(), id; got != want {
  42. t.Errorf("Subscription.ID() = %q; want %q", got, want)
  43. }
  44. }
  45. func TestListProjectSubscriptions(t *testing.T) {
  46. ctx := context.Background()
  47. c, _ := newFake(t)
  48. topic := mustCreateTopic(t, c, "t")
  49. var want []string
  50. for i := 1; i <= 2; i++ {
  51. id := fmt.Sprintf("s%d", i)
  52. want = append(want, id)
  53. _, err := c.CreateSubscription(ctx, id, SubscriptionConfig{Topic: topic})
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. }
  58. subs, err := slurpSubs(c.Subscriptions(ctx))
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. got := getSubIDs(subs)
  63. if !testutil.Equal(got, want) {
  64. t.Errorf("got %v, want %v", got, want)
  65. }
  66. }
  67. func getSubIDs(subs []*Subscription) []string {
  68. var names []string
  69. for _, sub := range subs {
  70. names = append(names, sub.ID())
  71. }
  72. return names
  73. }
  74. func TestListTopicSubscriptions(t *testing.T) {
  75. ctx := context.Background()
  76. c, _ := newFake(t)
  77. topics := []*Topic{
  78. mustCreateTopic(t, c, "t0"),
  79. mustCreateTopic(t, c, "t1"),
  80. }
  81. wants := make([][]string, 2)
  82. for i := 0; i < 5; i++ {
  83. id := fmt.Sprintf("s%d", i)
  84. sub, err := c.CreateSubscription(ctx, id, SubscriptionConfig{Topic: topics[i%2]})
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. wants[i%2] = append(wants[i%2], sub.ID())
  89. }
  90. for i, topic := range topics {
  91. subs, err := slurpSubs(topic.Subscriptions(ctx))
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. got := getSubIDs(subs)
  96. if !testutil.Equal(got, wants[i]) {
  97. t.Errorf("#%d: got %v, want %v", i, got, wants[i])
  98. }
  99. }
  100. }
  101. const defaultRetentionDuration = 168 * time.Hour
  102. func TestUpdateSubscription(t *testing.T) {
  103. ctx := context.Background()
  104. client, _ := newFake(t)
  105. defer client.Close()
  106. topic := client.Topic("t")
  107. sub, err := client.CreateSubscription(ctx, "s", SubscriptionConfig{Topic: topic})
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. cfg, err := sub.Config(ctx)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. want := SubscriptionConfig{
  116. Topic: topic,
  117. AckDeadline: 10 * time.Second,
  118. RetainAckedMessages: false,
  119. RetentionDuration: defaultRetentionDuration,
  120. }
  121. if !testutil.Equal(cfg, want) {
  122. t.Fatalf("\ngot %+v\nwant %+v", cfg, want)
  123. }
  124. got, err := sub.Update(ctx, SubscriptionConfigToUpdate{
  125. AckDeadline: 20 * time.Second,
  126. RetainAckedMessages: true,
  127. })
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. want = SubscriptionConfig{
  132. Topic: topic,
  133. AckDeadline: 20 * time.Second,
  134. RetainAckedMessages: true,
  135. RetentionDuration: defaultRetentionDuration,
  136. }
  137. if !testutil.Equal(got, want) {
  138. t.Fatalf("\ngot %+v\nwant %+v", got, want)
  139. }
  140. got, err = sub.Update(ctx, SubscriptionConfigToUpdate{RetentionDuration: 2 * time.Hour})
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. want.RetentionDuration = 2 * time.Hour
  145. if !testutil.Equal(got, want) {
  146. t.Fatalf("\ngot %+v\nwant %+v", got, want)
  147. }
  148. _, err = sub.Update(ctx, SubscriptionConfigToUpdate{})
  149. if err == nil {
  150. t.Fatal("got nil, want error")
  151. }
  152. }
  153. func (t1 *Topic) Equal(t2 *Topic) bool {
  154. if t1 == nil && t2 == nil {
  155. return true
  156. }
  157. if t1 == nil || t2 == nil {
  158. return false
  159. }
  160. return t1.c == t2.c && t1.name == t2.name
  161. }