Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

143 righe
3.6 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. "google.golang.org/grpc/status"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/codes"
  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, _ := newFake(t)
  58. s := c.Topic(id)
  59. if got, want := s.ID(), id; got != want {
  60. t.Errorf("Token.ID() = %q; want %q", got, want)
  61. }
  62. }
  63. func TestListTopics(t *testing.T) {
  64. c, _ := newFake(t)
  65. var ids []string
  66. for i := 1; i <= 4; i++ {
  67. id := fmt.Sprintf("t%d", i)
  68. ids = append(ids, id)
  69. mustCreateTopic(t, c, id)
  70. }
  71. checkTopicListing(t, c, ids)
  72. }
  73. func TestListCompletelyEmptyTopics(t *testing.T) {
  74. c, _ := newFake(t)
  75. checkTopicListing(t, c, nil)
  76. }
  77. func TestStopPublishOrder(t *testing.T) {
  78. // Check that Stop doesn't panic if called before Publish.
  79. // Also that Publish after Stop returns the right error.
  80. ctx := context.Background()
  81. c := &Client{projectID: "projid"}
  82. topic := c.Topic("t")
  83. topic.Stop()
  84. r := topic.Publish(ctx, &Message{})
  85. _, err := r.Get(ctx)
  86. if err != errTopicStopped {
  87. t.Errorf("got %v, want errTopicStopped", err)
  88. }
  89. }
  90. func TestPublishTimeout(t *testing.T) {
  91. ctx := context.Background()
  92. serv, err := testutil.NewServer()
  93. pubsubpb.RegisterPublisherServer(serv.Gsrv, &alwaysFailPublish{})
  94. conn, err := grpc.Dial(serv.Addr, grpc.WithInsecure())
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. c, err := NewClient(ctx, "projectID", option.WithGRPCConn(conn))
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. topic := c.Topic("t")
  103. topic.PublishSettings.Timeout = 3 * time.Second
  104. r := topic.Publish(ctx, &Message{})
  105. defer topic.Stop()
  106. select {
  107. case <-r.Ready():
  108. _, err = r.Get(ctx)
  109. if err != context.DeadlineExceeded {
  110. t.Fatalf("got %v, want context.DeadlineExceeded", err)
  111. }
  112. case <-time.After(2 * topic.PublishSettings.Timeout):
  113. t.Fatal("timed out")
  114. }
  115. }
  116. type alwaysFailPublish struct {
  117. pubsubpb.PublisherServer
  118. }
  119. func (s *alwaysFailPublish) Publish(ctx context.Context, req *pubsubpb.PublishRequest) (*pubsubpb.PublishResponse, error) {
  120. return nil, status.Errorf(codes.Unavailable, "try again")
  121. }
  122. func mustCreateTopic(t *testing.T, c *Client, id string) *Topic {
  123. topic, err := c.CreateTopic(context.Background(), id)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. return topic
  128. }