write tests for (local) cache lookup on queries

#2
This commit is contained in:
Trolli Schmittlauch 2020-04-17 12:22:53 +02:00
parent 66be1cc2b6
commit c1f8ec2c56

View file

@ -6,6 +6,7 @@ import Control.Exception
import Network.Socket
import qualified Data.Map.Strict as Map
import qualified Data.ByteString as BS
import qualified Data.Set as Set
import Hash2Pub.FediChord
import Hash2Pub.DHTProtocol
@ -108,30 +109,33 @@ spec = do
nid. cacheGetNodeStateUnvalidated <$> cacheLookupSucc (anotherID + 2) cacheWithProxyNodeEntry `shouldBe` Just maxBound
describe "NodeCache query lookups" $ do
describe "NodeCache query lookup" $ do
let
emptyCache = Map.empty :: NodeCache
node1 = exampleNodeState { nid = toNodeID 2^(23::Integer)+1}
node2 = exampleNodeState { nid = toNodeID 2^(230::Integer)+12}
node3 = exampleNodeState { nid = toNodeID 2^(25::Integer)+10}
node4 = exampleNodeState { nid = toNodeID 2^(9::Integer)+100}
--cacheWith2Entries <- mapM (\n -> addCacheEntry n 0) [node1, node2] $ emptyCache
emptyCache = nodeCache exampleNodeState
nid1 = toNodeID 2^(23::Integer)+1
node1 = exampleNodeState { nid = nid1}
nid2 = toNodeID 2^(230::Integer)+12
node2 = exampleNodeState { nid = nid2}
nid3 = toNodeID 2^(25::Integer)+10
node3 = exampleNodeState { nid = nid3}
nid4 = toNodeID 2^(9::Integer)+100
node4 = exampleNodeState { nid = nid4}
cacheWith2Entries = addCacheEntry node1 120 =<< addCacheEntry node2 0 emptyCache
cacheWith4Entries = addCacheEntry node3 110 =<< addCacheEntry node4 0 =<< cacheWith2Entries
it "work on an empty cache" $ do
incomingQuery exampleNodeState emptyCache 3 (toNodeID 2^(25::Integer)) `shouldBe` FORWARD []
incomingQuery exampleNodeState emptyCache 1 (toNodeID 2342) `shouldBe` FORWARD []
it "work on a cache with less entries than needed" $ do
it "works on an empty cache" $ do
incomingQuery exampleNodeState emptyCache 3 (toNodeID 2^(9::Integer)+5) `shouldBe` FORWARD Set.empty
incomingQuery exampleNodeState emptyCache 1 (toNodeID 2342) `shouldBe` FORWARD Set.empty
it "works on a cache with less entries than needed" $ do
c2 <- cacheWith2Entries
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
let (FORWARD nodeset) = incomingQuery exampleNodeState c2 4 (toNodeID 2^(9::Integer)+5)
Set.map (nid . cacheGetNodeStateUnvalidated) nodeset `shouldBe` Set.fromList [ nid1, nid2 ]
it "works on a cache with sufficient entries" $ do
c4 <- cacheWith4Entries
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` []
(FORWARD nodeset1) = incomingQuery exampleNodeState c4 3 (toNodeID 2^(9::Integer)+5)
(FORWARD nodeset2) = incomingQuery exampleNodeState c4 1 (toNodeID 2^(9::Integer)+5)
Set.map (nid . cacheGetNodeStateUnvalidated) nodeset1 `shouldBe` Set.fromList [nid4, nid2, nid3]
Set.map (nid . cacheGetNodeStateUnvalidated) nodeset2 `shouldBe` Set.fromList [nid4]
-- some example data