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.
 
 
 

196 lines
4.7 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. "context"
  17. "fmt"
  18. "testing"
  19. "time"
  20. "cloud.google.com/go/internal/testutil"
  21. "google.golang.org/api/iterator"
  22. "google.golang.org/api/option"
  23. pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/status"
  27. )
  28. func checkTopicListing(t *testing.T, c *Client, want []string) {
  29. topics, err := slurpTopics(c.Topics(context.Background()))
  30. if err != nil {
  31. t.Fatalf("error listing topics: %v", err)
  32. }
  33. var got []string
  34. for _, topic := range topics {
  35. got = append(got, topic.ID())
  36. }
  37. if !testutil.Equal(got, want) {
  38. t.Errorf("topic list: got: %v, want: %v", got, want)
  39. }
  40. }
  41. // All returns the remaining topics from this iterator.
  42. func slurpTopics(it *TopicIterator) ([]*Topic, error) {
  43. var topics []*Topic
  44. for {
  45. switch topic, err := it.Next(); err {
  46. case nil:
  47. topics = append(topics, topic)
  48. case iterator.Done:
  49. return topics, nil
  50. default:
  51. return nil, err
  52. }
  53. }
  54. }
  55. func TestTopicID(t *testing.T) {
  56. const id = "id"
  57. c, srv := newFake(t)
  58. defer c.Close()
  59. defer srv.Close()
  60. s := c.Topic(id)
  61. if got, want := s.ID(), id; got != want {
  62. t.Errorf("Token.ID() = %q; want %q", got, want)
  63. }
  64. }
  65. func TestListTopics(t *testing.T) {
  66. c, srv := newFake(t)
  67. defer c.Close()
  68. defer srv.Close()
  69. var ids []string
  70. for i := 1; i <= 4; i++ {
  71. id := fmt.Sprintf("t%d", i)
  72. ids = append(ids, id)
  73. mustCreateTopic(t, c, id)
  74. }
  75. checkTopicListing(t, c, ids)
  76. }
  77. func TestListCompletelyEmptyTopics(t *testing.T) {
  78. c, srv := newFake(t)
  79. defer c.Close()
  80. defer srv.Close()
  81. checkTopicListing(t, c, nil)
  82. }
  83. func TestStopPublishOrder(t *testing.T) {
  84. // Check that Stop doesn't panic if called before Publish.
  85. // Also that Publish after Stop returns the right error.
  86. ctx := context.Background()
  87. c := &Client{projectID: "projid"}
  88. topic := c.Topic("t")
  89. topic.Stop()
  90. r := topic.Publish(ctx, &Message{})
  91. _, err := r.Get(ctx)
  92. if err != errTopicStopped {
  93. t.Errorf("got %v, want errTopicStopped", err)
  94. }
  95. }
  96. func TestPublishTimeout(t *testing.T) {
  97. ctx := context.Background()
  98. serv, err := testutil.NewServer()
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. pubsubpb.RegisterPublisherServer(serv.Gsrv, &alwaysFailPublish{})
  103. conn, err := grpc.Dial(serv.Addr, grpc.WithInsecure())
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. c, err := NewClient(ctx, "projectID", option.WithGRPCConn(conn))
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. topic := c.Topic("t")
  112. topic.PublishSettings.Timeout = 3 * time.Second
  113. r := topic.Publish(ctx, &Message{})
  114. defer topic.Stop()
  115. select {
  116. case <-r.Ready():
  117. _, err = r.Get(ctx)
  118. if err != context.DeadlineExceeded {
  119. t.Fatalf("got %v, want context.DeadlineExceeded", err)
  120. }
  121. case <-time.After(2 * topic.PublishSettings.Timeout):
  122. t.Fatal("timed out")
  123. }
  124. }
  125. func TestUpdateTopic(t *testing.T) {
  126. ctx := context.Background()
  127. client, srv := newFake(t)
  128. defer client.Close()
  129. defer srv.Close()
  130. topic := mustCreateTopic(t, client, "T")
  131. config, err := topic.Config(ctx)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. want := TopicConfig{}
  136. if !testutil.Equal(config, want) {
  137. t.Errorf("got %+v, want %+v", config, want)
  138. }
  139. // replace labels
  140. labels := map[string]string{"label": "value"}
  141. config2, err := topic.Update(ctx, TopicConfigToUpdate{Labels: labels})
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. want = TopicConfig{
  146. Labels: labels,
  147. MessageStoragePolicy: MessageStoragePolicy{[]string{"US"}},
  148. }
  149. if !testutil.Equal(config2, want) {
  150. t.Errorf("got %+v, want %+v", config2, want)
  151. }
  152. // delete all labels
  153. labels = map[string]string{}
  154. config3, err := topic.Update(ctx, TopicConfigToUpdate{Labels: labels})
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. want.Labels = nil
  159. if !testutil.Equal(config3, want) {
  160. t.Errorf("got %+v, want %+v", config3, want)
  161. }
  162. }
  163. type alwaysFailPublish struct {
  164. pubsubpb.PublisherServer
  165. }
  166. func (s *alwaysFailPublish) Publish(ctx context.Context, req *pubsubpb.PublishRequest) (*pubsubpb.PublishResponse, error) {
  167. return nil, status.Errorf(codes.Unavailable, "try again")
  168. }
  169. func mustCreateTopic(t *testing.T, c *Client, id string) *Topic {
  170. topic, err := c.CreateTopic(context.Background(), id)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. return topic
  175. }