vid_map_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package wdclient
  2. import (
  3. "context"
  4. "fmt"
  5. "google.golang.org/grpc"
  6. "strconv"
  7. "sync"
  8. "testing"
  9. "time"
  10. )
  11. func TestLocationIndex(t *testing.T) {
  12. vm := &vidMap{}
  13. // test must be failed
  14. mustFailed := func(length int) {
  15. _, err := vm.getLocationIndex(length)
  16. if err == nil {
  17. t.Errorf("length %d must be failed", length)
  18. }
  19. if err.Error() != fmt.Sprintf("invalid length: %d", length) {
  20. t.Errorf("length %d must be failed. error: %v", length, err)
  21. }
  22. }
  23. mustFailed(-1)
  24. mustFailed(0)
  25. mustOk := func(length, cursor, expect int) {
  26. if length <= 0 {
  27. t.Fatal("please don't do this")
  28. }
  29. vm.cursor = int32(cursor)
  30. got, err := vm.getLocationIndex(length)
  31. if err != nil {
  32. t.Errorf("length: %d, why? %v\n", length, err)
  33. return
  34. }
  35. if got != expect {
  36. t.Errorf("cursor: %d, length: %d, expect: %d, got: %d\n", cursor, length, expect, got)
  37. return
  38. }
  39. }
  40. for i := -1; i < 100; i++ {
  41. mustOk(7, i, (i+1)%7)
  42. }
  43. // when cursor reaches MaxInt64
  44. mustOk(7, maxCursorIndex, 0)
  45. // test with constructor
  46. vm = newVidMap("")
  47. length := 7
  48. for i := 0; i < 100; i++ {
  49. got, err := vm.getLocationIndex(length)
  50. if err != nil {
  51. t.Errorf("length: %d, why? %v\n", length, err)
  52. return
  53. }
  54. if got != i%length {
  55. t.Errorf("length: %d, i: %d, got: %d\n", length, i, got)
  56. }
  57. }
  58. }
  59. func TestLookupFileId(t *testing.T) {
  60. mc := NewMasterClient(grpc.EmptyDialOption{}, "", "", "", "", "", nil)
  61. length := 5
  62. //Construct a cache linked list of length 5
  63. for i := 0; i < length; i++ {
  64. mc.addLocation(uint32(i), Location{Url: strconv.FormatInt(int64(i), 10)})
  65. mc.resetVidMap()
  66. }
  67. for i := 0; i < length; i++ {
  68. locations, found := mc.GetLocations(uint32(i))
  69. if !found || len(locations) != 1 || locations[0].Url != strconv.FormatInt(int64(i), 10) {
  70. t.Fatalf("urls of vid=%d is not valid.", i)
  71. }
  72. }
  73. //When continue to add nodes to the linked list, the previous node will be deleted, and the cache of the response will be gone.
  74. for i := length; i < length+5; i++ {
  75. mc.addLocation(uint32(i), Location{Url: strconv.FormatInt(int64(i), 10)})
  76. mc.resetVidMap()
  77. }
  78. for i := 0; i < length; i++ {
  79. locations, found := mc.GetLocations(uint32(i))
  80. if found {
  81. t.Fatalf("urls of vid[%d] should not exists, but found: %v", i, locations)
  82. }
  83. }
  84. //The delete operation will be applied to all cache nodes
  85. _, found := mc.GetLocations(uint32(length))
  86. if !found {
  87. t.Fatalf("urls of vid[%d] not found", length)
  88. }
  89. //If the locations of the current node exist, return directly
  90. newUrl := "abc"
  91. mc.addLocation(uint32(length), Location{Url: newUrl})
  92. locations, found := mc.GetLocations(uint32(length))
  93. if !found || locations[0].Url != newUrl {
  94. t.Fatalf("urls of vid[%d] not found", length)
  95. }
  96. //After delete `abc`, cache nodes are searched
  97. deleteLoc := Location{Url: newUrl}
  98. mc.deleteLocation(uint32(length), deleteLoc)
  99. locations, found = mc.GetLocations(uint32(length))
  100. if found && locations[0].Url != strconv.FormatInt(int64(length), 10) {
  101. t.Fatalf("urls of vid[%d] not expected", length)
  102. }
  103. //lock: concurrent test
  104. var wg sync.WaitGroup
  105. for i := 0; i < 20; i++ {
  106. wg.Add(1)
  107. go func() {
  108. defer wg.Done()
  109. for i := 0; i < 100; i++ {
  110. for i := 0; i < 20; i++ {
  111. _, _ = mc.GetLocations(uint32(i))
  112. }
  113. }
  114. }()
  115. }
  116. for i := 0; i < 100; i++ {
  117. mc.addLocation(uint32(i), Location{})
  118. }
  119. wg.Wait()
  120. }
  121. func TestConcurrentGetLocations(t *testing.T) {
  122. mc := NewMasterClient(grpc.EmptyDialOption{}, "", "", "", "", "", nil)
  123. location := Location{Url: "TestDataRacing"}
  124. mc.addLocation(1, location)
  125. ctx, cancel := context.WithCancel(context.Background())
  126. wg := sync.WaitGroup{}
  127. for i := 0; i < 50; i++ {
  128. wg.Add(1)
  129. go func() {
  130. defer wg.Done()
  131. for {
  132. select {
  133. case <-ctx.Done():
  134. return
  135. default:
  136. _, found := mc.GetLocations(1)
  137. if !found {
  138. cancel()
  139. t.Error("vid map invalid due to data racing. ")
  140. return
  141. }
  142. }
  143. }
  144. }()
  145. }
  146. //Simulate vidmap reset with cache when leader changes
  147. for i := 0; i < 100; i++ {
  148. mc.resetVidMap()
  149. mc.addLocation(1, location)
  150. time.Sleep(1 * time.Microsecond)
  151. }
  152. cancel()
  153. wg.Wait()
  154. }
  155. func BenchmarkLocationIndex(b *testing.B) {
  156. b.SetParallelism(8)
  157. vm := vidMap{
  158. cursor: maxCursorIndex - 4000,
  159. }
  160. b.ResetTimer()
  161. b.RunParallel(func(pb *testing.PB) {
  162. for pb.Next() {
  163. _, err := vm.getLocationIndex(3)
  164. if err != nil {
  165. b.Error(err)
  166. }
  167. }
  168. })
  169. }