setmatrix_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package common
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. "time"
  7. _ "github.com/docker/libnetwork/testutils"
  8. )
  9. func TestSetSerialInsertDelete(t *testing.T) {
  10. s := NewSetMatrix()
  11. b, i := s.Insert("a", "1")
  12. if !b || i != 1 {
  13. t.Fatalf("error in insert %t %d", b, i)
  14. }
  15. b, i = s.Insert("a", "1")
  16. if b || i != 1 {
  17. t.Fatalf("error in insert %t %d", b, i)
  18. }
  19. b, i = s.Insert("a", "2")
  20. if !b || i != 2 {
  21. t.Fatalf("error in insert %t %d", b, i)
  22. }
  23. b, i = s.Insert("a", "1")
  24. if b || i != 2 {
  25. t.Fatalf("error in insert %t %d", b, i)
  26. }
  27. b, i = s.Insert("a", "3")
  28. if !b || i != 3 {
  29. t.Fatalf("error in insert %t %d", b, i)
  30. }
  31. b, i = s.Insert("a", "2")
  32. if b || i != 3 {
  33. t.Fatalf("error in insert %t %d", b, i)
  34. }
  35. b, i = s.Insert("a", "3")
  36. if b || i != 3 {
  37. t.Fatalf("error in insert %t %d", b, i)
  38. }
  39. b, i = s.Insert("a", "4")
  40. if !b || i != 4 {
  41. t.Fatalf("error in insert %t %d", b, i)
  42. }
  43. b, p := s.Contains("a", "1")
  44. if !b || !p {
  45. t.Fatalf("error in contains %t %t", b, p)
  46. }
  47. b, p = s.Contains("a", "2")
  48. if !b || !p {
  49. t.Fatalf("error in contains %t %t", b, p)
  50. }
  51. b, p = s.Contains("a", "3")
  52. if !b || !p {
  53. t.Fatalf("error in contains %t %t", b, p)
  54. }
  55. b, p = s.Contains("a", "4")
  56. if !b || !p {
  57. t.Fatalf("error in contains %t %t", b, p)
  58. }
  59. i, b = s.Cardinality("a")
  60. if !b || i != 4 {
  61. t.Fatalf("error in cardinality count %t %d", b, i)
  62. }
  63. b, i = s.Remove("a", "1")
  64. if !b || i != 3 {
  65. t.Fatalf("error in remove %t %d", b, i)
  66. }
  67. b, i = s.Remove("a", "3")
  68. if !b || i != 2 {
  69. t.Fatalf("error in remove %t %d", b, i)
  70. }
  71. b, i = s.Remove("a", "1")
  72. if b || i != 2 {
  73. t.Fatalf("error in remove %t %d", b, i)
  74. }
  75. b, i = s.Remove("a", "4")
  76. if !b || i != 1 {
  77. t.Fatalf("error in remove %t %d", b, i)
  78. }
  79. b, i = s.Remove("a", "2")
  80. if !b || i != 0 {
  81. t.Fatalf("error in remove %t %d", b, i)
  82. }
  83. b, i = s.Remove("a", "2")
  84. if b || i != 0 {
  85. t.Fatalf("error in remove %t %d", b, i)
  86. }
  87. i, b = s.Cardinality("a")
  88. if b || i != 0 {
  89. t.Fatalf("error in cardinality count %t %d", b, i)
  90. }
  91. }
  92. func insertDeleteRotuine(ctx context.Context, endCh chan int, s SetMatrix, key, value string) {
  93. for {
  94. select {
  95. case <-ctx.Done():
  96. endCh <- 0
  97. return
  98. default:
  99. b, _ := s.Insert(key, value)
  100. if !b {
  101. endCh <- 1
  102. return
  103. }
  104. b, _ = s.Remove(key, value)
  105. if !b {
  106. endCh <- 2
  107. return
  108. }
  109. }
  110. }
  111. }
  112. func TestSetParallelInsertDelete(t *testing.T) {
  113. s := NewSetMatrix()
  114. parallelRoutines := 6
  115. endCh := make(chan int)
  116. // Let the routines running and competing for 10s
  117. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  118. defer cancel()
  119. for i := 0; i < parallelRoutines; i++ {
  120. go insertDeleteRotuine(ctx, endCh, s, "key-"+strconv.Itoa(i%3), strconv.Itoa(i))
  121. }
  122. for parallelRoutines > 0 {
  123. v := <-endCh
  124. if v == 1 {
  125. t.Fatalf("error one goroutine failed on the insert")
  126. }
  127. if v == 2 {
  128. t.Fatalf("error one goroutine failed on the remove")
  129. }
  130. parallelRoutines--
  131. }
  132. if i, b := s.Cardinality("key"); b || i > 0 {
  133. t.Fatalf("error the set should be empty %t %d", b, i)
  134. }
  135. }