keepalive_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "reflect"
  17. "sort"
  18. "sync"
  19. "testing"
  20. "time"
  21. "golang.org/x/net/context"
  22. )
  23. func TestKeepAlive(t *testing.T) {
  24. tick := make(chan time.Time)
  25. deadline := time.Nanosecond * 15
  26. s := &testService{modDeadlineCalled: make(chan modDeadlineCall)}
  27. c := &Client{projectID: "projid", s: s}
  28. checkModDeadlineCall := func(ackIDs []string) {
  29. got := <-s.modDeadlineCalled
  30. sort.Strings(got.ackIDs)
  31. want := modDeadlineCall{
  32. subName: "subname",
  33. deadline: deadline,
  34. ackIDs: ackIDs,
  35. }
  36. if !reflect.DeepEqual(got, want) {
  37. t.Errorf("keepalive: got:\n%v\nwant:\n%v", got, want)
  38. }
  39. }
  40. ka := &keepAlive{
  41. Client: c,
  42. Ctx: context.Background(),
  43. Sub: "subname",
  44. ExtensionTick: tick,
  45. Deadline: deadline,
  46. MaxExtension: time.Hour,
  47. }
  48. ka.Start()
  49. ka.Add("a")
  50. ka.Add("b")
  51. tick <- time.Time{}
  52. checkModDeadlineCall([]string{"a", "b"})
  53. ka.Add("c")
  54. ka.Remove("b")
  55. tick <- time.Time{}
  56. checkModDeadlineCall([]string{"a", "c"})
  57. ka.Remove("a")
  58. ka.Remove("c")
  59. // modifyAckDeadline should not be called now because there are no ackIDs left.
  60. tick <- time.Time{}
  61. ka.Add("d")
  62. tick <- time.Time{}
  63. checkModDeadlineCall([]string{"d"})
  64. ka.Remove("d")
  65. ka.Stop()
  66. }
  67. // TestKeepAliveStop checks that Stop blocks until all ackIDs have been removed.
  68. func TestKeepAliveStop(t *testing.T) {
  69. ticker := time.NewTicker(100 * time.Microsecond)
  70. defer ticker.Stop()
  71. s := &testService{modDeadlineCalled: make(chan modDeadlineCall, 100)}
  72. c := &Client{projectID: "projid", s: s}
  73. ka := &keepAlive{
  74. Client: c,
  75. ExtensionTick: ticker.C,
  76. MaxExtension: time.Hour,
  77. }
  78. ka.Start()
  79. events := make(chan string, 10)
  80. // Add an ackID so that ka.Stop will not return immediately.
  81. ka.Add("a")
  82. var wg sync.WaitGroup
  83. wg.Add(1)
  84. go func() {
  85. defer wg.Done()
  86. time.Sleep(time.Nanosecond * 1000)
  87. events <- "pre-remove"
  88. ka.Remove("a")
  89. time.Sleep(time.Nanosecond * 1000)
  90. events <- "post-second-sleep"
  91. }()
  92. wg.Add(1)
  93. go func() {
  94. defer wg.Done()
  95. events <- "pre-stop"
  96. ka.Stop()
  97. events <- "stopped"
  98. }()
  99. wg.Wait()
  100. close(events)
  101. eventSequence := []string{}
  102. for e := range events {
  103. eventSequence = append(eventSequence, e)
  104. }
  105. want := []string{"pre-stop", "pre-remove", "stopped", "post-second-sleep"}
  106. if !reflect.DeepEqual(eventSequence, want) {
  107. t.Errorf("keepalive eventsequence: got:\n%v\nwant:\n%v", eventSequence, want)
  108. }
  109. }
  110. // TestMaxExtensionDeadline checks we stop extending after the configured duration.
  111. func TestMaxExtensionDeadline(t *testing.T) {
  112. ticker := time.NewTicker(100 * time.Microsecond)
  113. defer ticker.Stop()
  114. s := &testService{modDeadlineCalled: make(chan modDeadlineCall, 100)}
  115. c := &Client{projectID: "projid", s: s}
  116. maxExtension := time.Millisecond
  117. ka := &keepAlive{
  118. Client: c,
  119. ExtensionTick: ticker.C,
  120. MaxExtension: maxExtension,
  121. }
  122. ka.Start()
  123. ka.Add("a")
  124. stopped := make(chan struct{})
  125. go func() {
  126. ka.Stop()
  127. stopped <- struct{}{}
  128. }()
  129. select {
  130. case <-stopped:
  131. case <-time.After(maxExtension + 2*time.Second):
  132. t.Fatalf("keepalive failed to stop after maxExtension deadline")
  133. }
  134. }