reconnect_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright 2017 Google Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client_test
  14. import (
  15. "errors"
  16. "testing"
  17. "time"
  18. "context"
  19. "notabug.org/themusicgod1/gnmi/client"
  20. fclient "notabug.org/themusicgod1/gnmi/client/fake"
  21. )
  22. func TestReconnect(t *testing.T) {
  23. c := breakingSubscribeClient{
  24. BaseClient: &client.BaseClient{},
  25. ch: make(chan error),
  26. }
  27. disconnect := make(chan struct{}, 1)
  28. reset := make(chan struct{})
  29. rc := client.Reconnect(c, func() { disconnect <- struct{}{} }, func() { reset <- struct{}{} })
  30. client.ReconnectBaseDelay = time.Nanosecond
  31. client.ReconnectMaxDelay = time.Millisecond
  32. subscribeDone := make(chan struct{})
  33. go func() {
  34. defer close(subscribeDone)
  35. rc.Subscribe(context.Background(), client.Query{Type: client.Stream})
  36. }()
  37. for i := 0; i < 100; i++ {
  38. c.ch <- errors.New("break")
  39. <-disconnect
  40. <-reset
  41. }
  42. // Try to trigger a race between rc.Close and rc.Subscribe.
  43. // rc.Close should return only after rc.Subscribe is done.
  44. closeDone := make(chan struct{})
  45. go func() {
  46. defer close(closeDone)
  47. rc.Close()
  48. }()
  49. select {
  50. case <-subscribeDone:
  51. case <-closeDone:
  52. t.Error("rc.Close returned before rc.Subscribe")
  53. }
  54. }
  55. type breakingSubscribeClient struct {
  56. *client.BaseClient
  57. ch chan error
  58. }
  59. func (c breakingSubscribeClient) Subscribe(ctx context.Context, q client.Query, clientType ...string) error {
  60. return <-c.ch
  61. }
  62. func (c breakingSubscribeClient) Close() error {
  63. c.ch <- errors.New("closed")
  64. return nil
  65. }
  66. func TestReconnectEarlyClose(t *testing.T) {
  67. block := make(fclient.Block)
  68. defer close(block)
  69. fclient.Mock("fake", []interface{}{block})
  70. rc := client.Reconnect(&client.BaseClient{}, nil, nil)
  71. rc.Close()
  72. done := make(chan struct{})
  73. go func() {
  74. rc.Subscribe(context.Background(), client.Query{
  75. Type: client.Stream,
  76. NotificationHandler: func(client.Notification) error { return nil },
  77. }, "fake")
  78. close(done)
  79. }()
  80. select {
  81. case <-done:
  82. case <-time.After(time.Second):
  83. t.Error("Subscribe on a closed ReconnectClient didn't return after 1s")
  84. }
  85. }