discover_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package discover
  7. import (
  8. "net/url"
  9. "time"
  10. "testing"
  11. "github.com/syncthing/protocol"
  12. )
  13. type DummyClient struct {
  14. url *url.URL
  15. lookups []protocol.DeviceID
  16. lookupRet []string
  17. stops int
  18. statusRet bool
  19. statusChecks int
  20. }
  21. func (c *DummyClient) Lookup(device protocol.DeviceID) []string {
  22. c.lookups = append(c.lookups, device)
  23. return c.lookupRet
  24. }
  25. func (c *DummyClient) StatusOK() bool {
  26. c.statusChecks++
  27. return c.statusRet
  28. }
  29. func (c *DummyClient) Stop() {
  30. c.stops++
  31. }
  32. func (c *DummyClient) Address() string {
  33. return c.url.String()
  34. }
  35. func TestGlobalDiscovery(t *testing.T) {
  36. c1 := &DummyClient{
  37. statusRet: false,
  38. lookupRet: []string{"test.com:1234"},
  39. }
  40. c2 := &DummyClient{
  41. statusRet: true,
  42. lookupRet: []string{},
  43. }
  44. c3 := &DummyClient{
  45. statusRet: true,
  46. lookupRet: []string{"best.com:2345"},
  47. }
  48. clients := []*DummyClient{c1, c2}
  49. Register("test1", func(uri *url.URL, pkt *Announce) (Client, error) {
  50. c := clients[0]
  51. clients = clients[1:]
  52. c.url = uri
  53. return c, nil
  54. })
  55. Register("test2", func(uri *url.URL, pkt *Announce) (Client, error) {
  56. c3.url = uri
  57. return c3, nil
  58. })
  59. d := NewDiscoverer(device, []string{})
  60. d.localBcastStart = time.Time{}
  61. servers := []string{
  62. "test1://123.123.123.123:1234",
  63. "test1://23.23.23.23:234",
  64. "test2://234.234.234.234.2345",
  65. }
  66. d.StartGlobal(servers, 1234)
  67. if len(d.clients) != 3 {
  68. t.Fatal("Wrong number of clients")
  69. }
  70. status := d.ExtAnnounceOK()
  71. for _, c := range []*DummyClient{c1, c2, c3} {
  72. if status[c.url.String()] != c.statusRet || c.statusChecks != 1 {
  73. t.Fatal("Wrong status")
  74. }
  75. }
  76. addrs := d.Lookup(device)
  77. if len(addrs) != 2 {
  78. t.Fatal("Wrong numer of addresses", addrs)
  79. }
  80. for _, addr := range []string{"test.com:1234", "best.com:2345"} {
  81. found := false
  82. for _, laddr := range addrs {
  83. if laddr == addr {
  84. found = true
  85. break
  86. }
  87. }
  88. if !found {
  89. t.Fatal("Couldn't find", addr)
  90. }
  91. }
  92. for _, c := range []*DummyClient{c1, c2, c3} {
  93. if len(c.lookups) != 1 || c.lookups[0] != device {
  94. t.Fatal("Wrong lookups")
  95. }
  96. }
  97. addrs = d.Lookup(device)
  98. if len(addrs) != 2 {
  99. t.Fatal("Wrong numer of addresses", addrs)
  100. }
  101. // Answer should be cached, so number of lookups should have not incresed
  102. for _, c := range []*DummyClient{c1, c2, c3} {
  103. if len(c.lookups) != 1 || c.lookups[0] != device {
  104. t.Fatal("Wrong lookups")
  105. }
  106. }
  107. d.StopGlobal()
  108. for _, c := range []*DummyClient{c1, c2, c3} {
  109. if c.stops != 1 {
  110. t.Fatal("Wrong number of stops")
  111. }
  112. }
  113. }