publisher_test.go 4.1 KB

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