subscriber_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package discov
  2. import (
  3. "testing"
  4. "zero/core/discov/internal"
  5. "github.com/stretchr/testify/assert"
  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. val: "a",
  71. },
  72. },
  73. expect: []string{
  74. "b",
  75. },
  76. },
  77. {
  78. name: "add two, delete two",
  79. do: []action{
  80. {
  81. act: actionAdd,
  82. key: "first",
  83. val: "a",
  84. },
  85. {
  86. act: actionAdd,
  87. key: "second",
  88. val: "b",
  89. },
  90. {
  91. act: actionDel,
  92. key: "first",
  93. val: "a",
  94. },
  95. {
  96. act: actionDel,
  97. key: "second",
  98. val: "b",
  99. },
  100. },
  101. expect: []string{},
  102. },
  103. }
  104. for _, test := range tests {
  105. t.Run(test.name, func(t *testing.T) {
  106. c := newContainer(false)
  107. for _, order := range test.do {
  108. if order.act == actionAdd {
  109. c.OnAdd(internal.KV{
  110. Key: order.key,
  111. Val: order.val,
  112. })
  113. } else {
  114. c.OnDelete(internal.KV{
  115. Key: order.key,
  116. Val: order.val,
  117. })
  118. }
  119. }
  120. assert.True(t, c.dirty.True())
  121. assert.ElementsMatch(t, test.expect, c.getValues())
  122. assert.False(t, c.dirty.True())
  123. assert.ElementsMatch(t, test.expect, c.getValues())
  124. })
  125. }
  126. }