publisher_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. restore := setMockClient(cli)
  105. defer restore()
  106. cli.EXPECT().Ctx().AnyTimes()
  107. cli.EXPECT().KeepAlive(gomock.Any(), id)
  108. var wg sync.WaitGroup
  109. wg.Add(1)
  110. cli.EXPECT().Revoke(gomock.Any(), id).Do(func(_, _ interface{}) {
  111. wg.Done()
  112. })
  113. pub := NewPublisher(nil, "thekey", "thevalue")
  114. pub.lease = id
  115. pub.Stop()
  116. assert.Nil(t, pub.keepAliveAsync(cli))
  117. wg.Wait()
  118. }
  119. func TestPublisher_keepAliveAsyncPause(t *testing.T) {
  120. ctrl := gomock.NewController(t)
  121. defer ctrl.Finish()
  122. const id clientv3.LeaseID = 1
  123. cli := internal.NewMockEtcdClient(ctrl)
  124. restore := setMockClient(cli)
  125. defer restore()
  126. cli.EXPECT().Ctx().AnyTimes()
  127. cli.EXPECT().KeepAlive(gomock.Any(), id)
  128. pub := NewPublisher(nil, "thekey", "thevalue")
  129. var wg sync.WaitGroup
  130. wg.Add(1)
  131. cli.EXPECT().Revoke(gomock.Any(), id).Do(func(_, _ interface{}) {
  132. pub.Stop()
  133. wg.Done()
  134. })
  135. pub.lease = id
  136. assert.Nil(t, pub.keepAliveAsync(cli))
  137. pub.Pause()
  138. wg.Wait()
  139. }