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.

133 lines
2.7 KiB

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