tendermint/blockchain/v2/scheduler_test.go

154 lines
5.3 KiB
Go
Raw Normal View History

2019-07-14 21:45:16 +03:00
package v2
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
2019-07-15 22:17:25 +03:00
"github.com/tendermint/tendermint/p2p"
2019-07-14 21:45:16 +03:00
)
const (
2019-07-15 22:17:25 +03:00
initHeight int64 = 5
peerID p2p.ID = "1"
peerIDTwo p2p.ID = "2"
peerHeight int64 = 20
blockSize int64 = 10
2019-07-14 21:45:16 +03:00
)
func TestScheduleInit(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
2019-07-14 21:45:16 +03:00
2019-07-16 18:34:02 +03:00
assert.Equal(t, blockStateNew, sc.getStateAtHeight(initHeight))
assert.Equal(t, blockStateProcessed, sc.getStateAtHeight(initHeight-1))
assert.Equal(t, blockStateUnknown, sc.getStateAtHeight(initHeight+1))
2019-07-14 21:45:16 +03:00
}
func TestAddPeer(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
2019-07-14 21:45:16 +03:00
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.addPeer(peerID))
assert.Nil(t, sc.addPeer(peerIDTwo))
2019-07-14 21:45:16 +03:00
assert.Equal(t, sc.addPeer(peerID), errDuplicatePeer)
}
func TestTouchPeer(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
2019-07-14 21:45:16 +03:00
now := time.Now()
2019-07-15 22:17:25 +03:00
assert.Equal(t, errPeerNotFound, sc.touchPeer(peerID, now),
2019-07-14 21:45:16 +03:00
"Touching an unknown peer should return errPeerNotFound")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.addPeer(peerID),
2019-07-14 21:45:16 +03:00
"Adding a peer should return no error")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.touchPeer(peerID, now),
2019-07-14 21:45:16 +03:00
"Touching a peer should return no error")
2019-07-16 18:34:02 +03:00
// the peer should
2019-07-14 21:45:16 +03:00
threshold := 10 * time.Second
2019-07-16 18:34:02 +03:00
assert.Equal(t, 0, len(sc.peersSince(threshold, now.Add(9*time.Second))),
2019-07-14 21:45:16 +03:00
"Expected no peers to have been touched over 9 seconds")
2019-07-15 22:49:20 +03:00
assert.Equal(t, peerID, sc.peersSince(threshold, now.Add(11*time.Second))[0].peerID,
2019-07-14 21:45:16 +03:00
"Expected one peer to have been touched over 10 seconds ago")
}
func TestPeerHeight(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
2019-07-14 21:45:16 +03:00
2019-07-16 18:34:02 +03:00
assert.Nil(t, sc.addPeer(peerID),
"Adding a peer should return no error")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight))
for i := initHeight; i <= peerHeight; i++ {
2019-07-14 21:45:16 +03:00
assert.Equal(t, sc.getStateAtHeight(i), blockStateNew,
"Expected all blocks to be in blockStateNew")
2019-07-15 22:49:20 +03:00
assert.Equal(t, len(sc.getPeersAtHeight(i)), 1,
"Expected the block to be registered to the 1 peer")
//TODO check peerID
2019-07-14 21:45:16 +03:00
}
}
func TestHeightFSM(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
now := time.Now()
2019-07-14 21:45:16 +03:00
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.addPeer(peerID),
2019-07-14 21:45:16 +03:00
"Adding a peer should return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, errPeerNotFound, sc.markPending(peerID, peerHeight, now),
2019-07-14 21:45:16 +03:00
"Expected markingPending on an unknown peer to return errPeerNotFound")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight),
2019-07-14 21:45:16 +03:00
"Expected setPeerHeight to return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, errBadSchedule, sc.markReceived(peerID, peerHeight, blockSize, now.Add(1*time.Second)),
2019-07-14 21:45:16 +03:00
"Expecting transitioning from blockStateNew to blockStateReceived to fail")
2019-07-15 22:17:25 +03:00
assert.Equal(t, errBadSchedule, sc.markProcessed(peerHeight),
2019-07-14 21:45:16 +03:00
"Expecting transitioning from blockStateNew to blockStateReceived to fail")
assert.Equal(t, blockStateUnknown, sc.getStateAtHeight(peerHeight+10),
"Expected the maximum height seen + 10 to be in blockStateUnknown")
2019-07-16 18:34:02 +03:00
2019-07-15 22:17:25 +03:00
assert.Equal(t, errBadSchedule, sc.markPending(peerID, peerHeight+10, now.Add(1*time.Second)),
2019-07-14 21:45:16 +03:00
"Expected markPending on block in blockStateUnknown height to fail")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.markPending(peerID, initHeight, now.Add(1*time.Second)),
2019-07-14 21:45:16 +03:00
"Expected markPending on a known height with a known peer to return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, blockStatePending, sc.getStateAtHeight(initHeight),
2019-07-14 21:45:16 +03:00
"Expected a the markedBlock to be in blockStatePending")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.markReceived(peerID, initHeight, blockSize, now.Add(2*time.Second)),
2019-07-16 18:34:02 +03:00
"Expected marking markReceived on a pending block to return no error")
2019-07-14 21:45:16 +03:00
2019-07-16 18:34:02 +03:00
// here we are trying to reset blocks that were received
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.resetBlocks(peerID),
2019-07-14 21:45:16 +03:00
"Expected resetBlocks to return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, blockStateNew, sc.getStateAtHeight(initHeight),
2019-07-14 21:45:16 +03:00
"Expected blocks to be in blockStateNew after being reset")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.markPending(peerID, initHeight, now),
2019-07-14 21:45:16 +03:00
"Expected marking a reset block to pending to return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, blockStatePending, sc.getStateAtHeight(initHeight),
2019-07-14 21:45:16 +03:00
"Expected block to be in blockStatePending")
2019-07-16 18:34:02 +03:00
assert.Nil(t, sc.markReceived(peerID, initHeight, blockSize, now.Add(2*time.Second)),
2019-07-14 21:45:16 +03:00
"Expected marking a pending block as received to return no error")
2019-07-15 22:17:25 +03:00
assert.Equal(t, blockStateReceived, sc.getStateAtHeight(initHeight))
2019-07-14 21:45:16 +03:00
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.markProcessed(initHeight),
2019-07-14 21:45:16 +03:00
"Expected marking a block as processed to success")
2019-07-15 22:17:25 +03:00
assert.Equal(t, blockStateProcessed, sc.getStateAtHeight(initHeight),
2019-07-14 21:45:16 +03:00
"Expected the block to in blockStateProcessed")
}
func TestMinMaxHeight(t *testing.T) {
2019-07-15 22:17:25 +03:00
sc := newSchedule(initHeight)
now := time.Now()
2019-07-14 21:45:16 +03:00
2019-07-15 22:17:25 +03:00
assert.Equal(t, initHeight, sc.minHeight(),
2019-07-14 21:45:16 +03:00
"Expected min height to be the initialized height")
2019-07-15 22:17:25 +03:00
assert.Equal(t, initHeight, sc.maxHeight(),
2019-07-14 21:45:16 +03:00
"Expected max height to be the initialized height")
2019-07-15 22:17:25 +03:00
assert.Equal(t, initHeight, sc.maxHeight(),
2019-07-14 21:45:16 +03:00
"Expected max height to be the initialized height")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.addPeer(peerID),
2019-07-14 21:45:16 +03:00
"Adding a peer should return no error")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight),
2019-07-14 21:45:16 +03:00
"Expected setPeerHeight to return no error")
assert.Equal(t, peerHeight, sc.maxHeight(),
"Expected max height to increase to peerHeight")
2019-07-15 22:17:25 +03:00
assert.Nil(t, sc.markPending(peerID, initHeight, now.Add(1*time.Second)),
"Expected marking initHeight as pending to return no error")
assert.Equal(t, initHeight+1, sc.minHeight(),
"Expected marking initHeight as pending to move minHeight forward")
}
func TestLatePeers(t *testing.T) {
}
2019-07-14 21:45:16 +03:00
2019-07-15 22:17:25 +03:00
func TestSlowPeers(t *testing.T) {
2019-07-14 21:45:16 +03:00
}