concurrency2_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package files_test
  16. import (
  17. "fmt"
  18. "log"
  19. "math/rand"
  20. "os"
  21. "runtime"
  22. "sync"
  23. "testing"
  24. "time"
  25. "github.com/syncthing/syncthing/internal/files"
  26. "github.com/syncthing/syncthing/internal/protocol"
  27. "github.com/syndtr/goleveldb/leveldb"
  28. "github.com/syndtr/goleveldb/leveldb/opt"
  29. )
  30. func TestLongConcurrent(t *testing.T) {
  31. if testing.Short() || runtime.GOMAXPROCS(-1) < 4 {
  32. return
  33. }
  34. os.RemoveAll("/tmp/test.db")
  35. db, err := leveldb.OpenFile("/tmp/test.db", &opt.Options{CachedOpenFiles: 100})
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. start := make(chan struct{})
  40. log.Println("preparing")
  41. var wg sync.WaitGroup
  42. for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
  43. i := i
  44. rem0, rem1 := generateFiles()
  45. wg.Add(1)
  46. go func() {
  47. defer wg.Done()
  48. longConcurrentTest(db, fmt.Sprintf("folder%d", i), rem0, rem1, start)
  49. }()
  50. }
  51. log.Println("starting")
  52. close(start)
  53. wg.Wait()
  54. }
  55. func generateFiles() ([]protocol.FileInfo, []protocol.FileInfo) {
  56. var rem0, rem1 fileList
  57. for i := 0; i < 10000; i++ {
  58. n := rand.Int()
  59. rem0 = append(rem0, protocol.FileInfo{
  60. Name: fmt.Sprintf("path/path/path/path/path/path/path%d/path%d/path%d/file%d", n, n, n, n),
  61. Version: uint64(rand.Int63()),
  62. Blocks: genBlocks(rand.Intn(25)),
  63. Flags: uint32(rand.Int31()),
  64. })
  65. }
  66. for i := 0; i < 10000; i++ {
  67. if i%2 == 0 {
  68. // Same file as rem0, randomly newer or older
  69. f := rem0[i]
  70. f.Version = uint64(rand.Int63())
  71. rem1 = append(rem1, f)
  72. } else {
  73. // Different file
  74. n := rand.Int()
  75. f := protocol.FileInfo{
  76. Name: fmt.Sprintf("path/path/path/path/path/path/path%d/path%d/path%d/file%d", n, n, n, n),
  77. Version: uint64(rand.Int63()),
  78. Blocks: genBlocks(rand.Intn(25)),
  79. Flags: uint32(rand.Int31()),
  80. }
  81. rem1 = append(rem1, f)
  82. }
  83. }
  84. return rem0, rem1
  85. }
  86. func longConcurrentTest(db *leveldb.DB, folder string, rem0, rem1 []protocol.FileInfo, start chan struct{}) {
  87. s := files.NewSet(folder, db)
  88. <-start
  89. t0 := time.Now()
  90. cont := func() bool {
  91. return time.Since(t0) < 60*time.Second
  92. }
  93. log.Println(folder, "start")
  94. var wg sync.WaitGroup
  95. // Fast updater
  96. wg.Add(1)
  97. go func() {
  98. defer wg.Done()
  99. for cont() {
  100. log.Println(folder, "u0")
  101. for i := 0; i < 10000; i += 250 {
  102. s.Update(remoteDevice0, rem0[i:i+250])
  103. }
  104. time.Sleep(25 * time.Millisecond)
  105. s.Replace(remoteDevice0, nil)
  106. time.Sleep(25 * time.Millisecond)
  107. }
  108. }()
  109. // Fast updater
  110. wg.Add(1)
  111. go func() {
  112. defer wg.Done()
  113. for cont() {
  114. log.Println(folder, "u1")
  115. for i := 0; i < 10000; i += 250 {
  116. s.Update(remoteDevice1, rem1[i:i+250])
  117. }
  118. time.Sleep(25 * time.Millisecond)
  119. s.Replace(remoteDevice1, nil)
  120. time.Sleep(25 * time.Millisecond)
  121. }
  122. }()
  123. // Fast need list
  124. wg.Add(1)
  125. go func() {
  126. defer wg.Done()
  127. for cont() {
  128. needList(s, protocol.LocalDeviceID)
  129. time.Sleep(25 * time.Millisecond)
  130. }
  131. }()
  132. // Fast global list
  133. wg.Add(1)
  134. go func() {
  135. defer wg.Done()
  136. for cont() {
  137. globalList(s)
  138. time.Sleep(25 * time.Millisecond)
  139. }
  140. }()
  141. // Long running need lists
  142. go func() {
  143. for i := 0; i < 10; i++ {
  144. time.Sleep(25 * time.Millisecond)
  145. wg.Add(1)
  146. go func() {
  147. defer wg.Done()
  148. for cont() {
  149. s.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
  150. time.Sleep(50 * time.Millisecond)
  151. return cont()
  152. })
  153. }
  154. }()
  155. }
  156. }()
  157. // Long running global lists
  158. go func() {
  159. for i := 0; i < 10; i++ {
  160. time.Sleep(25 * time.Millisecond)
  161. wg.Add(1)
  162. go func() {
  163. defer wg.Done()
  164. for cont() {
  165. s.WithGlobal(func(intf protocol.FileIntf) bool {
  166. time.Sleep(50 * time.Millisecond)
  167. return cont()
  168. })
  169. }
  170. }()
  171. }
  172. }()
  173. wg.Wait()
  174. log.Println(folder, "done")
  175. }