hive_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2016 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 network
  17. import (
  18. "io/ioutil"
  19. "log"
  20. "os"
  21. "testing"
  22. p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
  23. "github.com/ethereum/go-ethereum/swarm/state"
  24. )
  25. func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (*bzzTester, *Hive) {
  26. // setup
  27. addr := RandomAddr() // tested peers peer address
  28. to := NewKademlia(addr.OAddr, NewKadParams())
  29. pp := NewHive(params, to, store) // hive
  30. return newBzzBaseTester(t, n, addr, DiscoverySpec, pp.Run), pp
  31. }
  32. func TestRegisterAndConnect(t *testing.T) {
  33. params := NewHiveParams()
  34. s, pp := newHiveTester(t, params, 1, nil)
  35. id := s.IDs[0]
  36. raddr := NewAddrFromNodeID(id)
  37. pp.Register([]OverlayAddr{OverlayAddr(raddr)})
  38. // start the hive and wait for the connection
  39. err := pp.Start(s.Server)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer pp.Stop()
  44. // retrieve and broadcast
  45. err = s.TestDisconnected(&p2ptest.Disconnect{
  46. Peer: s.IDs[0],
  47. Error: nil,
  48. })
  49. if err == nil || err.Error() != "timed out waiting for peers to disconnect" {
  50. t.Fatalf("expected peer to connect")
  51. }
  52. }
  53. func TestHiveStatePersistance(t *testing.T) {
  54. log.SetOutput(os.Stdout)
  55. dir, err := ioutil.TempDir("", "hive_test_store")
  56. if err != nil {
  57. panic(err)
  58. }
  59. defer os.RemoveAll(dir)
  60. store, err := state.NewDBStore(dir) //start the hive with an empty dbstore
  61. params := NewHiveParams()
  62. s, pp := newHiveTester(t, params, 5, store)
  63. peers := make(map[string]bool)
  64. for _, id := range s.IDs {
  65. raddr := NewAddrFromNodeID(id)
  66. pp.Register([]OverlayAddr{OverlayAddr(raddr)})
  67. peers[raddr.String()] = true
  68. }
  69. // start the hive and wait for the connection
  70. err = pp.Start(s.Server)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. pp.Stop()
  75. store.Close()
  76. persistedStore, err := state.NewDBStore(dir) //start the hive with an empty dbstore
  77. s1, pp := newHiveTester(t, params, 1, persistedStore)
  78. //start the hive and wait for the connection
  79. pp.Start(s1.Server)
  80. i := 0
  81. pp.Overlay.EachAddr(nil, 256, func(addr OverlayAddr, po int, nn bool) bool {
  82. delete(peers, addr.(*BzzAddr).String())
  83. i++
  84. return true
  85. })
  86. if len(peers) != 0 || i != 5 {
  87. t.Fatalf("invalid peers loaded")
  88. }
  89. }