-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTest.hs
51 lines (41 loc) · 1.58 KB
/
Test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- Memcached interface.
-- Copyright (C) 2005 Evan Martin <martine@danga.com>
module Main where
import qualified Network.Memcache
import Network.Memcache.Key(hash)
import qualified Network.Memcache.Protocol as S
import Control.Exception
import System.Process
import System.IO -- used for emulating sleep()
import Test.HUnit
withServerConnection :: (S.Server -> IO ()) -> IO ()
withServerConnection f = bracket connect disconnect f where
connect = S.connect "localhost" 11211
disconnect = S.disconnect
statsTest :: Test
statsTest = TestCase $ withServerConnection $ \server -> do
stats <- S.stats server
assertBool "stats returns multiple stats" (length stats > 10)
setGetTest :: Test
setGetTest = TestCase $ withServerConnection $ \server -> do
let foo = 3 :: Int
success <- Network.Memcache.set server "foo" foo
foo' <- Network.Memcache.get server "foo"
case foo' of
Nothing -> assertFailure "'foo' not found just after setting it"
Just v -> assertEqual "foo value" (3 :: Int) v
hashTest :: Test
hashTest = TestCase $ do
assertBool "hash produces different values" (hash key1 /= hash key2)
where key1 = "foo"; key2 = "bar"
-- XXX hack: is there no other way to wait?
sleep :: Int -> IO ()
sleep x = hWaitForInput stdin x >> return ()
main :: IO ()
main = bracket upDaemon downDaemon runTests >> return () where
upDaemon = do m <- runCommand "memcached"
sleep 200 -- give it time to start up and bind.
return m
downDaemon = terminateProcess
runTests _ = runTestTT $ TestList [statsTest, setGetTest, hashTest]
-- vim: set ts=2 sw=2 et :