publisher_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package discov
  2. import (
  3. "errors"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/golang/mock/gomock"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/core/discov/internal"
  10. "github.com/zeromicro/go-zero/core/lang"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "github.com/zeromicro/go-zero/core/stringx"
  13. clientv3 "go.etcd.io/etcd/client/v3"
  14. )
  15. func init() {
  16. logx.Disable()
  17. }
  18. func TestPublisher_register(t *testing.T) {
  19. ctrl := gomock.NewController(t)
  20. defer ctrl.Finish()
  21. const id = 1
  22. cli := internal.NewMockEtcdClient(ctrl)
  23. restore := setMockClient(cli)
  24. defer restore()
  25. cli.EXPECT().Ctx().AnyTimes()
  26. cli.EXPECT().Grant(gomock.Any(), timeToLive).Return(&clientv3.LeaseGrantResponse{
  27. ID: id,
  28. }, nil)
  29. cli.EXPECT().Put(gomock.Any(), makeEtcdKey("thekey", id), "thevalue", gomock.Any())
  30. pub := NewPublisher(nil, "thekey", "thevalue",
  31. WithPubEtcdAccount(stringx.Rand(), "bar"))
  32. _, err := pub.register(cli)
  33. assert.Nil(t, err)
  34. }
  35. func TestPublisher_registerWithId(t *testing.T) {
  36. ctrl := gomock.NewController(t)
  37. defer ctrl.Finish()
  38. const id = 2
  39. cli := internal.NewMockEtcdClient(ctrl)
  40. restore := setMockClient(cli)
  41. defer restore()
  42. cli.EXPECT().Ctx().AnyTimes()
  43. cli.EXPECT().Grant(gomock.Any(), timeToLive).Return(&clientv3.LeaseGrantResponse{
  44. ID: 1,
  45. }, nil)
  46. cli.EXPECT().Put(gomock.Any(), makeEtcdKey("thekey", id), "thevalue", gomock.Any())
  47. pub := NewPublisher(nil, "thekey", "thevalue", WithId(id))
  48. _, err := pub.register(cli)
  49. assert.Nil(t, err)
  50. }
  51. func TestPublisher_registerError(t *testing.T) {
  52. ctrl := gomock.NewController(t)
  53. defer ctrl.Finish()
  54. cli := internal.NewMockEtcdClient(ctrl)
  55. restore := setMockClient(cli)
  56. defer restore()
  57. cli.EXPECT().Ctx().AnyTimes()
  58. cli.EXPECT().Grant(gomock.Any(), timeToLive).Return(nil, errors.New("error"))
  59. pub := NewPublisher(nil, "thekey", "thevalue")
  60. val, err := pub.register(cli)
  61. assert.NotNil(t, err)
  62. assert.Equal(t, clientv3.NoLease, val)
  63. }
  64. func TestPublisher_revoke(t *testing.T) {
  65. ctrl := gomock.NewController(t)
  66. defer ctrl.Finish()
  67. const id clientv3.LeaseID = 1
  68. cli := internal.NewMockEtcdClient(ctrl)
  69. restore := setMockClient(cli)
  70. defer restore()
  71. cli.EXPECT().Ctx().AnyTimes()
  72. cli.EXPECT().Revoke(gomock.Any(), id)
  73. pub := NewPublisher(nil, "thekey", "thevalue")
  74. pub.lease = id
  75. pub.revoke(cli)
  76. }
  77. func TestPublisher_revokeError(t *testing.T) {
  78. ctrl := gomock.NewController(t)
  79. defer ctrl.Finish()
  80. const id clientv3.LeaseID = 1
  81. cli := internal.NewMockEtcdClient(ctrl)
  82. restore := setMockClient(cli)
  83. defer restore()
  84. cli.EXPECT().Ctx().AnyTimes()
  85. cli.EXPECT().Revoke(gomock.Any(), id).Return(nil, errors.New("error"))
  86. pub := NewPublisher(nil, "thekey", "thevalue")
  87. pub.lease = id
  88. pub.revoke(cli)
  89. }
  90. func TestPublisher_keepAliveAsyncError(t *testing.T) {
  91. ctrl := gomock.NewController(t)
  92. defer ctrl.Finish()
  93. const id clientv3.LeaseID = 1
  94. cli := internal.NewMockEtcdClient(ctrl)
  95. restore := setMockClient(cli)
  96. defer restore()
  97. cli.EXPECT().Ctx().AnyTimes()
  98. cli.EXPECT().KeepAlive(gomock.Any(), id).Return(nil, errors.New("error"))
  99. pub := NewPublisher(nil, "thekey", "thevalue")
  100. pub.lease = id
  101. assert.NotNil(t, pub.keepAliveAsync(cli))
  102. }
  103. func TestPublisher_keepAliveAsyncQuit(t *testing.T) {
  104. ctrl := gomock.NewController(t)
  105. defer ctrl.Finish()
  106. const id clientv3.LeaseID = 1
  107. cli := internal.NewMockEtcdClient(ctrl)
  108. cli.EXPECT().ActiveConnection()
  109. cli.EXPECT().Close()
  110. defer cli.Close()
  111. cli.ActiveConnection()
  112. restore := setMockClient(cli)
  113. defer restore()
  114. cli.EXPECT().Ctx().AnyTimes()
  115. cli.EXPECT().KeepAlive(gomock.Any(), id)
  116. var wg sync.WaitGroup
  117. wg.Add(1)
  118. cli.EXPECT().Revoke(gomock.Any(), id).Do(func(_, _ any) {
  119. wg.Done()
  120. })
  121. pub := NewPublisher(nil, "thekey", "thevalue")
  122. pub.lease = id
  123. pub.Stop()
  124. assert.Nil(t, pub.keepAliveAsync(cli))
  125. wg.Wait()
  126. }
  127. func TestPublisher_keepAliveAsyncPause(t *testing.T) {
  128. ctrl := gomock.NewController(t)
  129. defer ctrl.Finish()
  130. const id clientv3.LeaseID = 1
  131. cli := internal.NewMockEtcdClient(ctrl)
  132. restore := setMockClient(cli)
  133. defer restore()
  134. cli.EXPECT().Ctx().AnyTimes()
  135. cli.EXPECT().KeepAlive(gomock.Any(), id)
  136. pub := NewPublisher(nil, "thekey", "thevalue")
  137. var wg sync.WaitGroup
  138. wg.Add(1)
  139. cli.EXPECT().Revoke(gomock.Any(), id).Do(func(_, _ any) {
  140. pub.Stop()
  141. wg.Done()
  142. })
  143. pub.lease = id
  144. assert.Nil(t, pub.keepAliveAsync(cli))
  145. pub.Pause()
  146. wg.Wait()
  147. }
  148. func TestPublisher_Resume(t *testing.T) {
  149. publisher := new(Publisher)
  150. publisher.resumeChan = make(chan lang.PlaceholderType)
  151. go func() {
  152. publisher.Resume()
  153. }()
  154. go func() {
  155. time.Sleep(time.Minute)
  156. t.Fail()
  157. }()
  158. <-publisher.resumeChan
  159. }