tendermint/blockchain/v2/scheduler_test.go

221 lines
7.6 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
2019-07-18 11:12:27 +03:00
blockSize int64 = 1024
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-19 01:44:27 +03:00
assert.Error(t, sc.addPeer(peerID))
2019-07-14 21:45:16 +03:00
}
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-19 01:44:27 +03:00
assert.Error(t, 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-18 11:12:27 +03:00
assert.Equal(t, 0, len(sc.peersInactiveSince(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-18 11:12:27 +03:00
assert.Containsf(t, sc.peersInactiveSince(threshold, now.Add(11*time.Second)), peerID,
"Expected one %s to have been touched over 10 seconds ago", peerID)
2019-07-14 21:45:16 +03:00
}
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-19 11:18:21 +03:00
assert.NoError(t, sc.addPeer(peerID),
2019-07-16 18:34:02 +03:00
"Adding a peer should return no error")
2019-07-19 11:18:21 +03:00
assert.NoError(t, sc.setPeerHeight(peerID, peerHeight))
2019-07-15 22:17:25 +03:00
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-19 11:18:21 +03:00
peerIDs := []p2p.ID{}
for _, peer := range sc.getPeersAtHeight(i) {
peerIDs = append(peerIDs, peer.peerID)
}
assert.Containsf(t, peerIDs, peerID,
"Expected %s to have block %d", peerID, i)
2019-07-14 21:45:16 +03:00
}
}
2019-07-18 11:12:27 +03:00
// TODO Split this into transitions
// TODO: Use formatting to describe test failures
2019-07-19 11:32:50 +03:00
func TestTransitionPending(t *testing.T) {
sc := newSchedule(initHeight)
now := time.Now()
assert.NoError(t, sc.addPeer(peerID),
"Adding a peer should return no error")
assert.Nil(t, sc.addPeer(peerIDTwo),
"Adding a peer should return no error")
assert.Error(t, sc.markPending(peerID, peerHeight, now),
"Expected scheduling a block from a peer in peerStateNew to fail")
assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
"Expected setPeerHeight to return no error")
assert.NoError(t, sc.setPeerHeight(peerIDTwo, peerHeight),
"Expected setPeerHeight to return no error")
assert.NoError(t, sc.markPending(peerID, peerHeight, now),
"Expected markingPending new block to succeed")
assert.Error(t, sc.markPending(peerIDTwo, peerHeight, now),
"Expected markingPending by a second peer to fail")
assert.Equal(t, blockStatePending, sc.getStateAtHeight(peerHeight),
"Expected the block to to be in blockStatePending")
assert.NoError(t, sc.removePeer(peerID),
"Expected removePeer to return no error")
assert.Equal(t, blockStateNew, sc.getStateAtHeight(peerHeight),
"Expected the block to to be in blockStateNew")
assert.Error(t, sc.markPending(peerID, peerHeight, now),
"Expected markingPending removed peer to fail")
assert.NoError(t, sc.markPending(peerIDTwo, peerHeight, now),
"Expected markingPending on a ready peer to succeed")
assert.Equal(t, blockStatePending, sc.getStateAtHeight(peerHeight),
"Expected the block to to be in blockStatePending")
}
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")
assert.Nil(t, sc.addPeer(peerIDTwo),
"Adding a peer should return no error")
2019-07-19 01:44:27 +03:00
assert.Error(t, sc.markPending(peerID, peerHeight, now),
"Expected markingPending on an unknown peer to return an 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.Nil(t, sc.setPeerHeight(peerIDTwo, peerHeight),
"Expected setPeerHeight to return no error")
2019-07-14 21:45:16 +03:00
2019-07-19 01:44:27 +03:00
assert.Error(t, 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-19 01:44:27 +03:00
assert.Error(t, 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-19 01:44:27 +03:00
assert.Error(t, 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
assert.NoError(t, sc.removePeer(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")
assert.NoError(t, sc.markPending(peerIDTwo, 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")
assert.NoError(t, sc.markReceived(peerIDTwo, 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-19 01:44:27 +03:00
assert.NoError(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-19 01:44:27 +03:00
assert.NoError(t, sc.addPeer(peerID),
2019-07-14 21:45:16 +03:00
"Adding a peer should return no error")
2019-07-19 01:44:27 +03:00
assert.NoError(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")
}
2019-07-18 11:12:27 +03:00
func TestPeersSlowerThan(t *testing.T) {
sc := newSchedule(initHeight)
now := time.Now()
receivedAt := now.Add(1 * time.Second)
2019-07-19 01:44:27 +03:00
assert.NoError(t, sc.addPeer(peerID),
2019-07-18 11:12:27 +03:00
"Adding a peer should return no error")
2019-07-19 01:44:27 +03:00
assert.NoError(t, sc.setPeerHeight(peerID, peerHeight),
2019-07-18 11:12:27 +03:00
"Expected setPeerHeight to return no error")
2019-07-19 01:44:27 +03:00
assert.NoError(t, sc.markPending(peerID, peerHeight, now),
2019-07-18 11:12:27 +03:00
"Expected markingPending on to return no error")
2019-07-19 01:44:27 +03:00
assert.NoError(t, sc.markReceived(peerID, peerHeight, blockSize, receivedAt),
2019-07-18 11:12:27 +03:00
"Expected markingPending on to return no error")
assert.Empty(t, sc.peersSlowerThan(blockSize-1),
"expected no peers to be slower than blockSize-1 bytes/sec")
2019-07-14 21:45:16 +03:00
2019-07-18 11:12:27 +03:00
assert.Containsf(t, sc.peersSlowerThan(blockSize+1), peerID,
"expected %s to be slower than blockSize+1 bytes/sec", peerID)
2019-07-14 21:45:16 +03:00
}