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.

112 lines
2.5 KiB

10 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. . "github.com/tendermint/tendermint/common"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. type testPeer struct {
  9. id string
  10. height uint
  11. }
  12. func makePeers(numPeers int, minHeight, maxHeight uint) map[string]testPeer {
  13. peers := make(map[string]testPeer, numPeers)
  14. for i := 0; i < numPeers; i++ {
  15. peerId := RandStr(12)
  16. height := minHeight + uint(rand.Intn(int(maxHeight-minHeight)))
  17. peers[peerId] = testPeer{peerId, height}
  18. }
  19. return peers
  20. }
  21. func TestBasic(t *testing.T) {
  22. // 100 peers anywhere at height 0 to 1000.
  23. peers := makePeers(100, 0, 1000)
  24. start := uint(42)
  25. maxHeight := uint(300)
  26. timeoutsCh := make(chan string, 100)
  27. requestsCh := make(chan BlockRequest, 100)
  28. blocksCh := make(chan *types.Block, 100)
  29. pool := NewBlockPool(start, timeoutsCh, requestsCh, blocksCh)
  30. pool.Start()
  31. // Introduce each peer.
  32. go func() {
  33. for _, peer := range peers {
  34. pool.SetPeerStatus(peer.id, peer.height)
  35. }
  36. }()
  37. lastSeenBlock := uint(41)
  38. // Pull from channels
  39. for {
  40. select {
  41. case peerId := <-timeoutsCh:
  42. t.Errorf("timeout: %v", peerId)
  43. case request := <-requestsCh:
  44. log.Debug("TEST: Pulled new BlockRequest", "request", request)
  45. // After a while, pretend like we got a block from the peer.
  46. go func() {
  47. block := &types.Block{Header: &types.Header{Height: request.Height}}
  48. pool.AddBlock(block, request.PeerId)
  49. log.Debug("TEST: Added block", "block", request.Height, "peer", request.PeerId)
  50. }()
  51. case block := <-blocksCh:
  52. log.Debug("TEST: Pulled new Block", "height", block.Height)
  53. if block.Height != lastSeenBlock+1 {
  54. t.Fatalf("Wrong order of blocks seen. Expected: %v Got: %v", lastSeenBlock+1, block.Height)
  55. }
  56. lastSeenBlock++
  57. if block.Height == maxHeight {
  58. return // Done!
  59. }
  60. }
  61. }
  62. pool.Stop()
  63. }
  64. func TestTimeout(t *testing.T) {
  65. peers := makePeers(100, 0, 1000)
  66. start := uint(42)
  67. timeoutsCh := make(chan string, 10)
  68. requestsCh := make(chan BlockRequest, 10)
  69. blocksCh := make(chan *types.Block, 100)
  70. pool := NewBlockPool(start, timeoutsCh, requestsCh, blocksCh)
  71. pool.Start()
  72. // Introduce each peer.
  73. go func() {
  74. for _, peer := range peers {
  75. pool.SetPeerStatus(peer.id, peer.height)
  76. }
  77. }()
  78. // Pull from channels
  79. for {
  80. select {
  81. case peerId := <-timeoutsCh:
  82. // Timed out. Done!
  83. if peers[peerId].id != peerId {
  84. t.Errorf("Unexpected peer from timeoutsCh")
  85. }
  86. return
  87. case _ = <-requestsCh:
  88. // Don't do anything, let it time out.
  89. case _ = <-blocksCh:
  90. t.Errorf("Got block when none expected")
  91. return
  92. }
  93. }
  94. pool.Stop()
  95. }