You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
2.9 KiB

8 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package blockchain
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "github.com/tendermint/tendermint/types"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. "github.com/tendermint/tmlibs/log"
  9. )
  10. func init() {
  11. peerTimeoutSeconds = time.Duration(2)
  12. }
  13. type testPeer struct {
  14. id string
  15. height int
  16. }
  17. func makePeers(numPeers int, minHeight, maxHeight int) map[string]testPeer {
  18. peers := make(map[string]testPeer, numPeers)
  19. for i := 0; i < numPeers; i++ {
  20. peerID := cmn.RandStr(12)
  21. height := minHeight + rand.Intn(maxHeight-minHeight)
  22. peers[peerID] = testPeer{peerID, height}
  23. }
  24. return peers
  25. }
  26. func TestBasic(t *testing.T) {
  27. start := 42
  28. peers := makePeers(10, start+1, 1000)
  29. timeoutsCh := make(chan string, 100)
  30. requestsCh := make(chan BlockRequest, 100)
  31. pool := NewBlockPool(start, requestsCh, timeoutsCh)
  32. pool.SetLogger(log.TestingLogger())
  33. err := pool.Start()
  34. if err != nil {
  35. t.Error(err)
  36. }
  37. defer pool.Stop()
  38. // Introduce each peer.
  39. go func() {
  40. for _, peer := range peers {
  41. pool.SetPeerHeight(peer.id, peer.height)
  42. }
  43. }()
  44. // Start a goroutine to pull blocks
  45. go func() {
  46. for {
  47. if !pool.IsRunning() {
  48. return
  49. }
  50. first, second := pool.PeekTwoBlocks()
  51. if first != nil && second != nil {
  52. pool.PopRequest()
  53. } else {
  54. time.Sleep(1 * time.Second)
  55. }
  56. }
  57. }()
  58. // Pull from channels
  59. for {
  60. select {
  61. case peerID := <-timeoutsCh:
  62. t.Errorf("timeout: %v", peerID)
  63. case request := <-requestsCh:
  64. t.Logf("Pulled new BlockRequest %v", request)
  65. if request.Height == 300 {
  66. return // Done!
  67. }
  68. // Request desired, pretend like we got the block immediately.
  69. go func() {
  70. block := &types.Block{Header: &types.Header{Height: request.Height}}
  71. pool.AddBlock(request.PeerID, block, 123)
  72. t.Logf("Added block from peer %v (height: %v)", request.PeerID, request.Height)
  73. }()
  74. }
  75. }
  76. }
  77. func TestTimeout(t *testing.T) {
  78. start := 42
  79. peers := makePeers(10, start+1, 1000)
  80. timeoutsCh := make(chan string, 100)
  81. requestsCh := make(chan BlockRequest, 100)
  82. pool := NewBlockPool(start, requestsCh, timeoutsCh)
  83. pool.SetLogger(log.TestingLogger())
  84. err := pool.Start()
  85. if err != nil {
  86. t.Error(err)
  87. }
  88. defer pool.Stop()
  89. for _, peer := range peers {
  90. t.Logf("Peer %v", peer.id)
  91. }
  92. // Introduce each peer.
  93. go func() {
  94. for _, peer := range peers {
  95. pool.SetPeerHeight(peer.id, peer.height)
  96. }
  97. }()
  98. // Start a goroutine to pull blocks
  99. go func() {
  100. for {
  101. if !pool.IsRunning() {
  102. return
  103. }
  104. first, second := pool.PeekTwoBlocks()
  105. if first != nil && second != nil {
  106. pool.PopRequest()
  107. } else {
  108. time.Sleep(1 * time.Second)
  109. }
  110. }
  111. }()
  112. // Pull from channels
  113. counter := 0
  114. timedOut := map[string]struct{}{}
  115. for {
  116. select {
  117. case peerID := <-timeoutsCh:
  118. t.Logf("Peer %v timeouted", peerID)
  119. if _, ok := timedOut[peerID]; !ok {
  120. counter++
  121. if counter == len(peers) {
  122. return // Done!
  123. }
  124. }
  125. case request := <-requestsCh:
  126. t.Logf("Pulled new BlockRequest %+v", request)
  127. }
  128. }
  129. }