Skip to content

Commit db7c4d9

Browse files
committed
add storage check and fix flake
1 parent fcde8df commit db7c4d9

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

testutil/long_running_network.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ func (n *LongRunningInMemoryNetwork) ConnectNodes(nodeIndexes ...uint64) {
140140
}
141141
}
142142

143+
func (n *LongRunningInMemoryNetwork) waitUntilAllRoundsEqual() {
144+
maxWait := time.After(30 * time.Second)
145+
ticker := time.NewTicker(500 * time.Millisecond)
146+
defer ticker.Stop()
147+
148+
for {
149+
select {
150+
case <-maxWait:
151+
n.t.Fatal("timed out waiting for all nodes to have the same round")
152+
case <-ticker.C:
153+
rounds := make(map[uint64]struct{})
154+
for _, instance := range n.Instances {
155+
rounds[instance.E.Metadata().Round] = struct{}{}
156+
}
157+
if len(rounds) == 1 {
158+
return
159+
}
160+
}
161+
}
162+
}
163+
143164
// StopAndAssert stops all nodes and asserts their storage and WALs are consistent.
144165
// If tailingMessages is true, it will also assert that no extra messages are being sent
145166
// after telling the network no more blocks should be built.
@@ -148,11 +169,14 @@ func (n *LongRunningInMemoryNetwork) ConnectNodes(nodeIndexes ...uint64) {
148169
func (n *LongRunningInMemoryNetwork) StopAndAssert(tailingMessages bool) {
149170
n.NoMoreBlocks()
150171

172+
// ensures all nodes have the same round before checking storage/WAL consistency
173+
n.waitUntilAllRoundsEqual()
174+
151175
// check all the nodes have the same wal, storage, etc
152176
for i, instance := range n.Instances {
153177
instance.WAL.AssertHealthy(instance.E.BlockDeserializer, instance.E.QCDeserializer)
154178
if i != 0 {
155-
instance.Storage.Compare(n.Instances[0].Storage)
179+
require.NoError(n.t, instance.Storage.Compare(n.Instances[0].Storage), "node %d storage does not match node 0 storage", i)
156180
}
157181
instance.E.Stop()
158182
}

testutil/storage.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,43 +113,43 @@ func (mem *InMemStorage) Index(ctx context.Context, block simplex.VerifiedBlock,
113113
return nil
114114
}
115115

116-
func (mem *InMemStorage) Compare(other *InMemStorage) bool {
116+
func (mem *InMemStorage) Compare(other *InMemStorage) error {
117117
mem.lock.Lock()
118118
defer mem.lock.Unlock()
119119

120120
other.lock.Lock()
121121
defer other.lock.Unlock()
122122

123123
if len(mem.data) != len(other.data) {
124-
return false
124+
return fmt.Errorf("storage lengths differ: %d vs %d", len(mem.data), len(other.data))
125125
}
126126

127127
for seq, item := range mem.data {
128128
otherItem, ok := other.data[seq]
129129
if !ok {
130-
return false
130+
return fmt.Errorf("other storage missing seq %d", seq)
131131
}
132132

133133
// compare blocks
134134
blockBytes, err := item.VerifiedBlock.Bytes()
135135
if err != nil {
136-
return false
136+
return fmt.Errorf("failed getting bytes for seq %d: %v", seq, err)
137137
}
138138

139139
otherBlockBytes, err := otherItem.VerifiedBlock.Bytes()
140140
if err != nil {
141-
return false
141+
return fmt.Errorf("failed getting bytes for seq %d: %v", seq, err)
142142
}
143143

144144
if !bytes.Equal(blockBytes, otherBlockBytes) {
145-
return false
145+
return fmt.Errorf("blocks differ at seq %d", seq)
146146
}
147147

148148
// compare finalizations
149149
if item.Finalization.Finalization.Digest != otherItem.Finalization.Finalization.Digest {
150-
return false
150+
return fmt.Errorf("finalizations differ at seq %d", seq)
151151
}
152152
}
153153

154-
return true
154+
return nil
155155
}

0 commit comments

Comments
 (0)