sync_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package dnsdisc
  17. import (
  18. "math/rand"
  19. "strconv"
  20. "testing"
  21. )
  22. func TestLinkCache(t *testing.T) {
  23. var lc linkCache
  24. // Check adding links.
  25. lc.addLink("1", "2")
  26. if !lc.changed {
  27. t.Error("changed flag not set")
  28. }
  29. lc.changed = false
  30. lc.addLink("1", "2")
  31. if lc.changed {
  32. t.Error("changed flag set after adding link that's already present")
  33. }
  34. lc.addLink("2", "3")
  35. lc.addLink("3", "1")
  36. lc.addLink("2", "4")
  37. lc.changed = false
  38. if !lc.isReferenced("3") {
  39. t.Error("3 not referenced")
  40. }
  41. if lc.isReferenced("6") {
  42. t.Error("6 is referenced")
  43. }
  44. lc.resetLinks("1", nil)
  45. if !lc.changed {
  46. t.Error("changed flag not set")
  47. }
  48. if len(lc.backrefs) != 0 {
  49. t.Logf("%+v", lc)
  50. t.Error("reference maps should be empty")
  51. }
  52. }
  53. func TestLinkCacheRandom(t *testing.T) {
  54. tags := make([]string, 1000)
  55. for i := range tags {
  56. tags[i] = strconv.Itoa(i)
  57. }
  58. // Create random links.
  59. var lc linkCache
  60. var remove []string
  61. for i := 0; i < 100; i++ {
  62. a, b := tags[rand.Intn(len(tags))], tags[rand.Intn(len(tags))]
  63. lc.addLink(a, b)
  64. remove = append(remove, a)
  65. }
  66. // Remove all the links.
  67. for _, s := range remove {
  68. lc.resetLinks(s, nil)
  69. }
  70. if len(lc.backrefs) != 0 {
  71. t.Logf("%+v", lc)
  72. t.Error("reference maps should be empty")
  73. }
  74. }