events_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 simulation
  17. import (
  18. "context"
  19. "sync"
  20. "testing"
  21. "time"
  22. )
  23. // TestPeerEvents creates simulation, adds two nodes,
  24. // register for peer events, connects nodes in a chain
  25. // and waits for the number of connection events to
  26. // be received.
  27. func TestPeerEvents(t *testing.T) {
  28. sim := New(noopServiceFuncMap)
  29. defer sim.Close()
  30. _, err := sim.AddNodes(2)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  35. defer cancel()
  36. events := sim.PeerEvents(ctx, sim.NodeIDs())
  37. // two nodes -> two connection events
  38. expectedEventCount := 2
  39. var wg sync.WaitGroup
  40. wg.Add(expectedEventCount)
  41. go func() {
  42. for e := range events {
  43. if e.Error != nil {
  44. if e.Error == context.Canceled {
  45. return
  46. }
  47. t.Error(e.Error)
  48. continue
  49. }
  50. wg.Done()
  51. }
  52. }()
  53. err = sim.Net.ConnectNodesChain(sim.NodeIDs())
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. wg.Wait()
  58. }
  59. func TestPeerEventsTimeout(t *testing.T) {
  60. sim := New(noopServiceFuncMap)
  61. defer sim.Close()
  62. _, err := sim.AddNodes(2)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
  67. defer cancel()
  68. events := sim.PeerEvents(ctx, sim.NodeIDs())
  69. done := make(chan struct{})
  70. go func() {
  71. for e := range events {
  72. if e.Error == context.Canceled {
  73. return
  74. }
  75. if e.Error == context.DeadlineExceeded {
  76. close(done)
  77. return
  78. } else {
  79. t.Fatal(e.Error)
  80. }
  81. }
  82. }()
  83. select {
  84. case <-time.After(time.Second):
  85. t.Error("no context deadline received")
  86. case <-done:
  87. // all good, context deadline detected
  88. }
  89. }