manypeers_test.go 2.9 KB

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