psinet_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psinet
  20. import (
  21. "fmt"
  22. "testing"
  23. "time"
  24. )
  25. func TestDiscoveryBuckets(t *testing.T) {
  26. checkBuckets := func(buckets [][]Server, expectedIDs [][]int) {
  27. if len(buckets) != len(expectedIDs) {
  28. t.Errorf(
  29. "unexpected bucket count: got %d expected %d",
  30. len(buckets), len(expectedIDs))
  31. return
  32. }
  33. for i := 0; i < len(buckets); i++ {
  34. if len(buckets[i]) != len(expectedIDs[i]) {
  35. t.Errorf(
  36. "unexpected bucket %d size: got %d expected %d",
  37. i, len(buckets[i]), len(expectedIDs[i]))
  38. return
  39. }
  40. for j := 0; j < len(buckets[i]); j++ {
  41. expectedID := fmt.Sprintf("%d", expectedIDs[i][j])
  42. if buckets[i][j].Id != expectedID {
  43. t.Errorf(
  44. "unexpected bucket %d item %d: got %s expected %s",
  45. i, j, buckets[i][j].Id, expectedID)
  46. return
  47. }
  48. }
  49. }
  50. }
  51. // Partition test cases from:
  52. // http://stackoverflow.com/questions/2659900/python-slicing-a-list-into-n-nearly-equal-length-partitions
  53. servers := make([]Server, 0)
  54. for i := 0; i < 105; i++ {
  55. servers = append(servers, Server{Id: fmt.Sprintf("%d", i)})
  56. }
  57. t.Run("5 servers, 5 buckets", func(t *testing.T) {
  58. checkBuckets(
  59. bucketizeServerList(servers[0:5], 5),
  60. [][]int{{0}, {1}, {2}, {3}, {4}})
  61. })
  62. t.Run("5 servers, 2 buckets", func(t *testing.T) {
  63. checkBuckets(
  64. bucketizeServerList(servers[0:5], 2),
  65. [][]int{{0, 1, 2}, {3, 4}})
  66. })
  67. t.Run("5 servers, 3 buckets", func(t *testing.T) {
  68. checkBuckets(
  69. bucketizeServerList(servers[0:5], 3),
  70. [][]int{{0, 1}, {2}, {3, 4}})
  71. })
  72. t.Run("105 servers, 10 buckets", func(t *testing.T) {
  73. checkBuckets(
  74. bucketizeServerList(servers, 10),
  75. [][]int{
  76. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  77. {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
  78. {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
  79. {32, 33, 34, 35, 36, 37, 38, 39, 40, 41},
  80. {42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52},
  81. {53, 54, 55, 56, 57, 58, 59, 60, 61, 62},
  82. {63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73},
  83. {74, 75, 76, 77, 78, 79, 80, 81, 82, 83},
  84. {84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94},
  85. {95, 96, 97, 98, 99, 100, 101, 102, 103, 104},
  86. })
  87. })
  88. t.Run("repeatedly discover with fixed IP address", func(t *testing.T) {
  89. // For a IP address values, only one bucket should be used; with enough
  90. // iterations, all and only the items in a single bucket should be discovered.
  91. discoveredServers := make(map[string]bool)
  92. // discoveryValue is derived from the client's IP address and indexes the bucket;
  93. // a value of 0 always maps to the first bucket.
  94. discoveryValue := 0
  95. for i := 0; i < 1000; i++ {
  96. for _, server := range selectServers(servers, i*int(time.Hour/time.Second), discoveryValue) {
  97. discoveredServers[server.Id] = true
  98. }
  99. }
  100. bucketCount := calculateBucketCount(len(servers))
  101. buckets := bucketizeServerList(servers, bucketCount)
  102. if len(buckets[0]) != len(discoveredServers) {
  103. t.Errorf(
  104. "unexpected discovered server count: got %d expected %d",
  105. len(discoveredServers), len(buckets[0]))
  106. return
  107. }
  108. for _, bucketServer := range buckets[0] {
  109. if _, ok := discoveredServers[bucketServer.Id]; !ok {
  110. t.Errorf("unexpected missing discovery server: %s", bucketServer.Id)
  111. return
  112. }
  113. }
  114. })
  115. }