expiring-cache-map-0.0.6.1: General purpose simple caching.

Copyright | (c) 2014 Edward L. Blake
License | BSD-style
Maintainer | Edward L. Blake <edwardlblake@gmail.com>
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Caching.ExpiringCacheMap.Utils.TestSequence

Description

TestSequence monad for testing caching behaviour.

    {-# LANGUAGE OverloadedStrings #-}

    import Caching.ExpiringCacheMap.HashECM (newECMForM, lookupECM, CacheSettings(..), consistentDuration)
    import qualified Caching.ExpiringCacheMap.Utils.TestSequence as TestSeq

    import qualified Data.ByteString.Char8 as BS

    test = do
      (TestSeq.TestSequenceState (_, events, _), return_value) <- TestSeq.runTestSequence test'
      (putStrLn . show . reverse) events
      return ()
      where
        test' = do
          filecache <- newECMForM
                (consistentDuration 100 -- Duration between access and expiry time of each item, no state needed.
                  (\state _id -> do number <- TestSeq.readNumber
                                    return (state, number)))
                (TestSeq.getCurrentTime >>= return)
                12000 -- Time check frequency: (accumulator `mod` this_number) == 0.
                (CacheWithLRUList
                  6   -- Expected size of key-value map when removing elements.
                  6   -- Size of map when to remove items from key-value map.
                  12  -- Size of list when to compact
                  )
                TestSeq.newTestSVar TestSeq.enterTestSVar TestSeq.readTestSVar

          -- Use lookupECM whenever the contents of "file1" is needed.
          b <- lookupECM filecache ("file1" :: BS.ByteString)
          TestSeq.haveNumber b
          b <- lookupECM filecache "file1"
          b <- lookupECM filecache "file2"
          TestSeq.haveNumber b
          return b

Evaluating the `test` function results in a list of events.

    >>> test
    [GetVar 3,ReadNumber 4,GetTime 7,PutVar 11,HaveNumber 4,GetVar 14,PutVar 17,
     GetVar 19,ReadNumber 20,GetTime 23,PutVar 27,HaveNumber 20]

In this example the history shows 2 time accesses (`GetTime 7` and `GetTime 23`) since the time check frequency number is a high value (12000), but regardless the high value a time check is still requested again because of the new key request for `"file2"`.

Changing the time frequency to 1 will alter the list of events with more frequent time checks:

    >>> test
    [GetVar 3,ReadNumber 4,GetTime 7,PutVar 11,HaveNumber 4,GetVar 14,GetTime 15,
     GetTime 18,PutVar 22,GetVar 24,ReadNumber 25,GetTime 28,PutVar 32,
     HaveNumber 25]

# Documentation

runTestSequence :: Show a => TestSequence b a -> IO (TestSequenceState b, a) #

newTestSVar :: a -> TestSequence a (TestSVar a) #

enterTestSVar :: TestSVar a -> (a -> TestSequence a (a, b)) -> TestSequence a b #

readTestSVar :: TestSVar a -> TestSequence a a #

getCurrentTime :: TestSequence a Int #

readNumber :: TestSequence a Int #

haveNumber :: Int -> TestSequence a () #

data TestSequenceEvents #

Constructors

GetVar Word32 |
PutVar Word32 |
GetTime Word32 |
ReadNumber Int |
HaveNumber Int |

Instances

Eq TestSequenceEvents # |
Methods (==) :: TestSequenceEvents -> TestSequenceEvents -> Bool # (/=) :: TestSequenceEvents -> TestSequenceEvents -> Bool #
Show TestSequenceEvents # |
Methods showsPrec :: Int -> TestSequenceEvents -> ShowS # show :: TestSequenceEvents -> String # showList :: [TestSequenceEvents] -> ShowS #

newtype TestSequenceState b #

Constructors

TestSequenceState (Word32, [TestSequenceEvents], Maybe b) |

Instances

Show (TestSequenceState ct) # |
Methods showsPrec :: Int -> TestSequenceState ct -> ShowS # show :: TestSequenceState ct -> String # showList :: [TestSequenceState ct] -> ShowS #

newtype TestSequence b a #

Constructors

TestSequence (TestSequenceState b -> (TestSequenceState b, a)) |

Instances

Monad (TestSequence a) # |
Methods (>>=) :: TestSequence a a -> (a -> TestSequence a b) -> TestSequence a b # (>>) :: TestSequence a a -> TestSequence a b -> TestSequence a b # return :: a -> TestSequence a a # fail :: String -> TestSequence a a #
Functor (TestSequence a) # |
Methods fmap :: (a -> b) -> TestSequence a a -> TestSequence a b # (<$) :: a -> TestSequence a b -> TestSequence a a #
Applicative (TestSequence a) # |
Methods pure :: a -> TestSequence a a # (<*>) :: TestSequence a (a -> b) -> TestSequence a a -> TestSequence a b # (*>) :: TestSequence a a -> TestSequence a b -> TestSequence a b # (<*) :: TestSequence a a -> TestSequence a b -> TestSequence a a #

newtype TestSVar a #

Constructors

TestSVar a |