Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

88 rader
2.2 KiB

  1. // Copyright 2018 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. "log"
  17. "sync/atomic"
  18. "testing"
  19. "time"
  20. "golang.org/x/net/context"
  21. "cloud.google.com/go/pubsub/pstest"
  22. "google.golang.org/api/option"
  23. "google.golang.org/grpc"
  24. )
  25. // Using the fake PubSub server in the pstest package, verify that streaming
  26. // pull resumes if the server stream times out.
  27. func TestStreamTimeout(t *testing.T) {
  28. log.SetFlags(log.Lmicroseconds)
  29. ctx := context.Background()
  30. srv := pstest.NewServer()
  31. srv.SetStreamTimeout(2 * time.Second)
  32. conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. client, err := NewClient(ctx, "P", option.WithGRPCConn(conn))
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. defer client.Close()
  41. topic, err := client.CreateTopic(ctx, "T")
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. sub, err := client.CreateSubscription(ctx, "sub", SubscriptionConfig{Topic: topic, AckDeadline: 10 * time.Second})
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. const nPublish = 8
  50. rctx, cancel := context.WithTimeout(ctx, 30*time.Second)
  51. defer cancel()
  52. errc := make(chan error)
  53. var nSeen int64
  54. go func() {
  55. errc <- sub.Receive(rctx, func(ctx context.Context, m *Message) {
  56. m.Ack()
  57. n := atomic.AddInt64(&nSeen, 1)
  58. if n >= nPublish {
  59. cancel()
  60. }
  61. })
  62. }()
  63. for i := 0; i < nPublish; i++ {
  64. pr := topic.Publish(ctx, &Message{Data: []byte("msg")})
  65. _, err := pr.Get(ctx)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. time.Sleep(250 * time.Millisecond)
  70. }
  71. err = <-errc
  72. if err := sub.Delete(ctx); err != nil {
  73. t.Fatal(err)
  74. }
  75. n := atomic.LoadInt64(&nSeen)
  76. if n < nPublish {
  77. t.Errorf("got %d messages, want %d", n, nPublish)
  78. }
  79. }