Skip to content

Commit

Permalink
Init test with helper to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacolvin0 authored and hkalodner committed Jul 22, 2022
1 parent bad7967 commit 799f77f
Showing 1 changed file with 24 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,95 +36,31 @@ func TestGetEmptyCacheMessages(t *testing.T) {
}
}

func TestGetCacheMessages(t *testing.T) {
buffer := ConfirmedAccumulatorCatchupBuffer{
broadcastMessages: []*BroadcastFeedMessage{
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(40),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(40),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(41),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
func createDummyBroadcastMessages(lastSeqNums []int) []*BroadcastFeedMessage {
broadcastMessages := make([]*BroadcastFeedMessage, 0, len(lastSeqNums))
for _, lastSeqNum := range lastSeqNums {
broadcastMessage := &BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(int64(lastSeqNum)),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
Signature: []byte{},
PrevAcc: common.Hash{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(45),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(46),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(47),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
&BroadcastFeedMessage{
FeedItem: SequencerFeedItem{
BatchItem: inbox.SequencerBatchItem{
LastSeqNum: big.NewInt(48),
Accumulator: common.Hash{},
TotalDelayedCount: big.NewInt(0),
SequencerMessage: []byte{},
},
PrevAcc: common.Hash{},
},
Signature: []byte{},
},
},
cacheSize: 10,
Signature: []byte{},
}
broadcastMessages = append(broadcastMessages, broadcastMessage)
}

return broadcastMessages
}

func TestGetCacheMessages(t *testing.T) {
buffer := ConfirmedAccumulatorCatchupBuffer{
broadcastMessages: createDummyBroadcastMessages([]int{40, 40, 41, 45, 46, 47, 48}),
cacheSize: 10,
}

// Get everything
Expand Down Expand Up @@ -169,6 +105,7 @@ func TestGetCacheMessages(t *testing.T) {
t.Errorf("expected lastSeqNum 45, got %d", bm.Messages[0].FeedItem.BatchItem.LastSeqNum.Int64())
}

// Test when duplicate message
bm = buffer.getCacheMessages(big.NewInt(42))
if len(bm.Messages) != 5 {
t.Errorf("expected only 5 messages, got %d messages", len(bm.Messages))
Expand Down

0 comments on commit 799f77f

Please sign in to comment.