subscriber_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package discov
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tal-tech/go-zero/core/discov/internal"
  6. )
  7. const (
  8. actionAdd = iota
  9. actionDel
  10. )
  11. func TestContainer(t *testing.T) {
  12. type action struct {
  13. act int
  14. key string
  15. val string
  16. }
  17. tests := []struct {
  18. name string
  19. do []action
  20. expect []string
  21. }{
  22. {
  23. name: "add one",
  24. do: []action{
  25. {
  26. act: actionAdd,
  27. key: "first",
  28. val: "a",
  29. },
  30. },
  31. expect: []string{
  32. "a",
  33. },
  34. },
  35. {
  36. name: "add two",
  37. do: []action{
  38. {
  39. act: actionAdd,
  40. key: "first",
  41. val: "a",
  42. },
  43. {
  44. act: actionAdd,
  45. key: "second",
  46. val: "b",
  47. },
  48. },
  49. expect: []string{
  50. "a",
  51. "b",
  52. },
  53. },
  54. {
  55. name: "add two, delete one",
  56. do: []action{
  57. {
  58. act: actionAdd,
  59. key: "first",
  60. val: "a",
  61. },
  62. {
  63. act: actionAdd,
  64. key: "second",
  65. val: "b",
  66. },
  67. {
  68. act: actionDel,
  69. key: "first",
  70. },
  71. },
  72. expect: []string{"b"},
  73. },
  74. {
  75. name: "add two, delete two",
  76. do: []action{
  77. {
  78. act: actionAdd,
  79. key: "first",
  80. val: "a",
  81. },
  82. {
  83. act: actionAdd,
  84. key: "second",
  85. val: "b",
  86. },
  87. {
  88. act: actionDel,
  89. key: "first",
  90. },
  91. {
  92. act: actionDel,
  93. key: "second",
  94. },
  95. },
  96. expect: []string{},
  97. },
  98. {
  99. name: "add three, dup values, delete two",
  100. do: []action{
  101. {
  102. act: actionAdd,
  103. key: "first",
  104. val: "a",
  105. },
  106. {
  107. act: actionAdd,
  108. key: "second",
  109. val: "b",
  110. },
  111. {
  112. act: actionAdd,
  113. key: "third",
  114. val: "a",
  115. },
  116. {
  117. act: actionDel,
  118. key: "first",
  119. },
  120. {
  121. act: actionDel,
  122. key: "second",
  123. },
  124. },
  125. expect: []string{"a"},
  126. },
  127. {
  128. name: "add three, dup values, delete two, delete not added",
  129. do: []action{
  130. {
  131. act: actionAdd,
  132. key: "first",
  133. val: "a",
  134. },
  135. {
  136. act: actionAdd,
  137. key: "second",
  138. val: "b",
  139. },
  140. {
  141. act: actionAdd,
  142. key: "third",
  143. val: "a",
  144. },
  145. {
  146. act: actionDel,
  147. key: "first",
  148. },
  149. {
  150. act: actionDel,
  151. key: "second",
  152. },
  153. {
  154. act: actionDel,
  155. key: "forth",
  156. },
  157. },
  158. expect: []string{"a"},
  159. },
  160. }
  161. exclusives := []bool{true, false}
  162. for _, test := range tests {
  163. for _, exclusive := range exclusives {
  164. t.Run(test.name, func(t *testing.T) {
  165. var changed bool
  166. c := newContainer(exclusive)
  167. c.addListener(func() {
  168. changed = true
  169. })
  170. assert.Nil(t, c.getValues())
  171. assert.False(t, changed)
  172. for _, order := range test.do {
  173. if order.act == actionAdd {
  174. c.OnAdd(internal.KV{
  175. Key: order.key,
  176. Val: order.val,
  177. })
  178. } else {
  179. c.OnDelete(internal.KV{
  180. Key: order.key,
  181. Val: order.val,
  182. })
  183. }
  184. }
  185. assert.True(t, changed)
  186. assert.True(t, c.dirty.True())
  187. assert.ElementsMatch(t, test.expect, c.getValues())
  188. assert.False(t, c.dirty.True())
  189. assert.ElementsMatch(t, test.expect, c.getValues())
  190. })
  191. }
  192. }
  193. }