manypeers_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (C) 2014 The Syncthing Authors.
  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. // +build integration
  16. package integration
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "log"
  21. "testing"
  22. "time"
  23. "github.com/syncthing/syncthing/internal/config"
  24. "github.com/syncthing/syncthing/internal/osutil"
  25. "github.com/syncthing/syncthing/internal/protocol"
  26. )
  27. func TestManyPeers(t *testing.T) {
  28. log.Println("Cleaning...")
  29. err := removeAll("s1", "s2", "h1/index", "h2/index")
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. log.Println("Generating files...")
  34. err = generateFiles("s1", 200, 20, "../LICENSE")
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. receiver := syncthingProcess{ // id2
  39. instance: "2",
  40. argv: []string{"-home", "h2"},
  41. port: 8082,
  42. apiKey: apiKey,
  43. }
  44. err = receiver.start()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. defer receiver.stop()
  49. resp, err := receiver.get("/rest/config")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if resp.StatusCode != 200 {
  54. t.Fatalf("Code %d != 200", resp.StatusCode)
  55. }
  56. var cfg config.Configuration
  57. json.NewDecoder(resp.Body).Decode(&cfg)
  58. resp.Body.Close()
  59. for len(cfg.Devices) < 100 {
  60. bs := make([]byte, 16)
  61. ReadRand(bs)
  62. id := protocol.NewDeviceID(bs)
  63. cfg.Devices = append(cfg.Devices, config.DeviceConfiguration{DeviceID: id})
  64. cfg.Folders[0].Devices = append(cfg.Folders[0].Devices, config.FolderDeviceConfiguration{DeviceID: id})
  65. }
  66. osutil.Rename("h2/config.xml", "h2/config.xml.orig")
  67. defer osutil.Rename("h2/config.xml.orig", "h2/config.xml")
  68. var buf bytes.Buffer
  69. json.NewEncoder(&buf).Encode(cfg)
  70. resp, err = receiver.post("/rest/config", &buf)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if resp.StatusCode != 200 {
  75. t.Fatalf("Code %d != 200", resp.StatusCode)
  76. }
  77. resp.Body.Close()
  78. log.Println("Starting up...")
  79. sender := syncthingProcess{ // id1
  80. instance: "1",
  81. argv: []string{"-home", "h1"},
  82. port: 8081,
  83. apiKey: apiKey,
  84. }
  85. err = sender.start()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. defer sender.stop()
  90. for {
  91. comp, err := sender.peerCompletion()
  92. if err != nil {
  93. if isTimeout(err) {
  94. time.Sleep(250 * time.Millisecond)
  95. continue
  96. }
  97. t.Fatal(err)
  98. }
  99. if comp[id2] == 100 {
  100. return
  101. }
  102. time.Sleep(2 * time.Second)
  103. }
  104. log.Println("Comparing directories...")
  105. err = compareDirectories("s1", "s2")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. }