NodeCache: write tests for lookup (#2)

This commit is contained in:
Trolli Schmittlauch 2020-04-16 01:08:41 +02:00
parent a803751213
commit 90daa1ba9a

View file

@ -68,13 +68,46 @@ spec = do
}
print nsReady
describe "NodeCache" $ do
it "entries can be added to a node cache" $ do
let
emptyCache = nodeCache exampleNodeState
anotherNode = exampleNodeState { nid = toNodeID 2^(23::Integer)+1}
newCache <- addCacheEntry exampleNodeState 0 =<< addCacheEntry anotherNode 10 emptyCache
Map.size newCache - Map.size emptyCache `shouldBe` 2
-- ToDo: query/ retrieve
exampleID = nid exampleNodeState
anotherID = toNodeID 2^(230::Integer)+1
anotherNode = exampleNodeState { nid = anotherID}
maxNode = exampleNodeState { nid = maxBound}
newCache = addCacheEntry exampleNodeState 0 =<< addCacheEntry anotherNode 10 emptyCache
it "entries can be added to a node cache and looked up again" $ do
nC <- newCache
-- the cache includes 2 additional proxy elements right from the start
Map.size nC - Map.size emptyCache `shouldBe` 2
-- normal entry lookup
nid . cacheGetNodeStateUnvalidated <$> cacheLookup anotherID nC `shouldBe` Just anotherID
nid . cacheGetNodeStateUnvalidated <$> cacheLookup (anotherID+1) nC `shouldBe` Nothing
-- initially, the proxy elements store nothing
cacheLookup minBound emptyCache `shouldBe` Nothing
cacheLookup maxBound emptyCache `shouldBe` Nothing
-- now store a node at that ID
cacheWithMaxNode <- addCacheEntry maxNode 0 =<< newCache
nid . cacheGetNodeStateUnvalidated <$> cacheLookup maxBound cacheWithMaxNode `shouldBe` Just maxBound
it "looking up predecessor and successor works like on a modular ring" $ do
-- ignore empty proxy elements in initial cache
nid. cacheGetNodeStateUnvalidated <$> cacheLookupPred (exampleID + 10) emptyCache `shouldBe` Nothing
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc exampleID emptyCache `shouldBe` Nothing
nC <- newCache
-- given situation: 0 < nid exampleNodeState < anotherNode < maxBound
-- first try non-modular queries between the 2 stored nodes
nid. cacheGetNodeStateUnvalidated <$> cacheLookupPred (exampleID + 10) nC `shouldBe` Just exampleID
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc exampleID nC `shouldBe` Just exampleID
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc (exampleID + 10) nC `shouldBe` Just anotherID
-- queries that require a (pseudo)modular structure
nid. cacheGetNodeStateUnvalidated <$> cacheLookupPred (exampleID - 2) nC `shouldBe` Just anotherID
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc (anotherID + 2) nC `shouldBe` Just exampleID
-- now store a node in one of the ProxyEntries
cacheWithProxyNodeEntry <- addCacheEntry maxNode 0 =<< newCache
nid. cacheGetNodeStateUnvalidated <$> cacheLookupPred (exampleID - 2) cacheWithProxyNodeEntry `shouldBe` Just maxBound
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc (anotherID + 2) cacheWithProxyNodeEntry `shouldBe` Just maxBound
describe "NodeCache query lookups" $ do
let
emptyCache = Map.empty :: NodeCache
@ -90,13 +123,15 @@ spec = do
incomingQuery exampleNodeState emptyCache 1 (toNodeID 2342) `shouldBe` FORWARD []
it "work on a cache with less entries than needed" $ do
c2 <- cacheWith2Entries
print c2
let (FORWARD nodelist) = incomingQuery exampleNodeState emptyCache 3 (toNodeID 2^(25::Integer))
map (nid . cacheGetNodeStateUnvalidated) nodelist `shouldBe` []
it "work on a cache with sufficient entries" $ do
c4 <- cacheWith4Entries
incomingQuery exampleNodeState c4 3 (toNodeID 2342) `shouldBe` FORWARD []
incomingQuery exampleNodeState c4 1 (toNodeID 2342) `shouldBe` FORWARD []
let
(FORWARD nodelist1) = incomingQuery exampleNodeState c4 3 (toNodeID 2342)
(FORWARD nodelist2) = incomingQuery exampleNodeState c4 1 (toNodeID 2342)
map (nid . cacheGetNodeStateUnvalidated) nodelist1 `shouldBe` []
map (nid . cacheGetNodeStateUnvalidated) nodelist2 `shouldBe` []
-- some example data