channeldb/graph_test: properly check entries within edge update index
Due to entries within the edge update index having a nil value, the tests need to be modified to account for this. Previously, we'd assume that if we were unable to retrieve a value for a certain key that the entry was non-existent, which is why the improper pruning bug was not caught. Instead, we'll assert the number of entries to be the expected value and populate a lookup map to determine whether the correct entries exist within it.
This commit is contained in:
parent
2f22e6c35f
commit
8dec659e10
@ -2130,43 +2130,53 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
||||
t.Fatalf("unable to update edge: %v", err)
|
||||
}
|
||||
|
||||
// Now that both edges have been updated, if we manually check the
|
||||
// update index, we should have an entry for both edges.
|
||||
if err := db.View(func(tx *bolt.Tx) error {
|
||||
// checkIndexTimestamps is a helper function that checks the edge update
|
||||
// index only includes the given timestamps.
|
||||
checkIndexTimestamps := func(timestamps ...uint64) {
|
||||
timestampSet := make(map[uint64]struct{})
|
||||
for _, t := range timestamps {
|
||||
timestampSet[t] = struct{}{}
|
||||
}
|
||||
|
||||
err := db.View(func(tx *bolt.Tx) error {
|
||||
edges := tx.Bucket(edgeBucket)
|
||||
if edges == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
edgeIndex := edges.Bucket(edgeIndexBucket)
|
||||
if edgeIndex == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
||||
if edgeUpdateIndex == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
|
||||
var edgeKey [8 + 8]byte
|
||||
|
||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge1.LastUpdate.Unix()))
|
||||
byteOrder.PutUint64(edgeKey[8:], edge1.ChannelID)
|
||||
|
||||
if edgeUpdateIndex.Get(edgeKey[:]) == nil {
|
||||
return fmt.Errorf("first edge not found in update " +
|
||||
"index")
|
||||
numEntries := edgeUpdateIndex.Stats().KeyN
|
||||
expectedEntries := len(timestampSet)
|
||||
if numEntries != expectedEntries {
|
||||
return fmt.Errorf("expected %v entries in the "+
|
||||
"update index, got %v", expectedEntries,
|
||||
numEntries)
|
||||
}
|
||||
|
||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge2.LastUpdate.Unix()))
|
||||
byteOrder.PutUint64(edgeKey[8:], edge2.ChannelID)
|
||||
if edgeUpdateIndex.Get(edgeKey[:]) == nil {
|
||||
return fmt.Errorf("second edge not found in update " +
|
||||
"index")
|
||||
return edgeUpdateIndex.ForEach(func(k, _ []byte) error {
|
||||
t := byteOrder.Uint64(k[:8])
|
||||
if _, ok := timestampSet[t]; !ok {
|
||||
return fmt.Errorf("found unexpected "+
|
||||
"timestamp "+"%d", t)
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("unable to read update index: %v", err)
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// With both edges policies added, we'll make sure to check they exist
|
||||
// within the edge update index.
|
||||
checkIndexTimestamps(
|
||||
uint64(edge1.LastUpdate.Unix()),
|
||||
uint64(edge2.LastUpdate.Unix()),
|
||||
)
|
||||
|
||||
// Now we'll prune the graph, removing the edges, and also the update
|
||||
// index entries from the database all together.
|
||||
@ -2179,43 +2189,10 @@ func TestChannelEdgePruningUpdateIndexDeletion(t *testing.T) {
|
||||
t.Fatalf("unable to prune graph: %v", err)
|
||||
}
|
||||
|
||||
// We'll now check the database state again, at this point, we should
|
||||
// no longer be able to locate the entries within the edge update
|
||||
// index.
|
||||
if err := db.View(func(tx *bolt.Tx) error {
|
||||
edges := tx.Bucket(edgeBucket)
|
||||
if edges == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
edgeIndex := edges.Bucket(edgeIndexBucket)
|
||||
if edgeIndex == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
edgeUpdateIndex := edges.Bucket(edgeUpdateIndexBucket)
|
||||
if edgeUpdateIndex == nil {
|
||||
return ErrGraphNoEdgesFound
|
||||
}
|
||||
|
||||
var edgeKey [8 + 8]byte
|
||||
|
||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge1.LastUpdate.Unix()))
|
||||
byteOrder.PutUint64(edgeKey[8:], edge1.ChannelID)
|
||||
if edgeUpdateIndex.Get(edgeKey[:]) != nil {
|
||||
return fmt.Errorf("first edge still found in update " +
|
||||
"index")
|
||||
}
|
||||
|
||||
byteOrder.PutUint64(edgeKey[:8], uint64(edge2.LastUpdate.Unix()))
|
||||
byteOrder.PutUint64(edgeKey[8:], edge2.ChannelID)
|
||||
if edgeUpdateIndex.Get(edgeKey[:]) != nil {
|
||||
return fmt.Errorf("second edge still found in update " +
|
||||
"index")
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("unable to read update index: %v", err)
|
||||
}
|
||||
// Finally, we'll check the database state one last time to conclude
|
||||
// that we should no longer be able to locate _any_ entries within the
|
||||
// edge update index.
|
||||
checkIndexTimestamps()
|
||||
}
|
||||
|
||||
// TestPruneGraphNodes tests that unconnected vertexes are pruned via the
|
||||
|
Loading…
Reference in New Issue
Block a user