discover_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. package discover
  16. import (
  17. "net/url"
  18. "time"
  19. "testing"
  20. "github.com/syncthing/syncthing/internal/protocol"
  21. )
  22. type DummyClient struct {
  23. url *url.URL
  24. lookups []protocol.DeviceID
  25. lookupRet []string
  26. stops int
  27. statusRet bool
  28. statusChecks int
  29. }
  30. func (c *DummyClient) Lookup(device protocol.DeviceID) []string {
  31. c.lookups = append(c.lookups, device)
  32. return c.lookupRet
  33. }
  34. func (c *DummyClient) StatusOK() bool {
  35. c.statusChecks++
  36. return c.statusRet
  37. }
  38. func (c *DummyClient) Stop() {
  39. c.stops++
  40. }
  41. func (c *DummyClient) Address() string {
  42. return c.url.String()
  43. }
  44. func TestGlobalDiscovery(t *testing.T) {
  45. c1 := &DummyClient{
  46. statusRet: false,
  47. lookupRet: []string{"test.com:1234"},
  48. }
  49. c2 := &DummyClient{
  50. statusRet: true,
  51. lookupRet: []string{},
  52. }
  53. c3 := &DummyClient{
  54. statusRet: true,
  55. lookupRet: []string{"best.com:2345"},
  56. }
  57. clients := []*DummyClient{c1, c2}
  58. Register("test1", func(uri *url.URL, pkt *Announce) (Client, error) {
  59. c := clients[0]
  60. clients = clients[1:]
  61. c.url = uri
  62. return c, nil
  63. })
  64. Register("test2", func(uri *url.URL, pkt *Announce) (Client, error) {
  65. c3.url = uri
  66. return c3, nil
  67. })
  68. d := NewDiscoverer(device, []string{})
  69. d.localBcastStart = time.Time{}
  70. servers := []string{
  71. "test1://123.123.123.123:1234",
  72. "test1://23.23.23.23:234",
  73. "test2://234.234.234.234.2345",
  74. }
  75. d.StartGlobal(servers, 1234)
  76. if len(d.clients) != 3 {
  77. t.Fatal("Wrong number of clients")
  78. }
  79. status := d.ExtAnnounceOK()
  80. for _, c := range []*DummyClient{c1, c2, c3} {
  81. if status[c.url.String()] != c.statusRet || c.statusChecks != 1 {
  82. t.Fatal("Wrong status")
  83. }
  84. }
  85. addrs := d.Lookup(device)
  86. if len(addrs) != 2 {
  87. t.Fatal("Wrong numer of addresses", addrs)
  88. }
  89. for _, addr := range []string{"test.com:1234", "best.com:2345"} {
  90. found := false
  91. for _, laddr := range addrs {
  92. if laddr == addr {
  93. found = true
  94. break
  95. }
  96. }
  97. if !found {
  98. t.Fatal("Couldn't find", addr)
  99. }
  100. }
  101. for _, c := range []*DummyClient{c1, c2, c3} {
  102. if len(c.lookups) != 1 || c.lookups[0] != device {
  103. t.Fatal("Wrong lookups")
  104. }
  105. }
  106. addrs = d.Lookup(device)
  107. if len(addrs) != 2 {
  108. t.Fatal("Wrong numer of addresses", addrs)
  109. }
  110. // Answer should be cached, so number of lookups should have not incresed
  111. for _, c := range []*DummyClient{c1, c2, c3} {
  112. if len(c.lookups) != 1 || c.lookups[0] != device {
  113. t.Fatal("Wrong lookups")
  114. }
  115. }
  116. d.StopGlobal()
  117. for _, c := range []*DummyClient{c1, c2, c3} {
  118. if c.stops != 1 {
  119. t.Fatal("Wrong number of stops")
  120. }
  121. }
  122. }