summaryrefslogtreecommitdiff
path: root/tests/Network/KRPCSpec.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-18 21:24:38 -0500
committerjoe <joe@jerkface.net>2017-01-18 21:24:38 -0500
commit1d7dd944e0a13d3f09b65f7629d1f96098ea7974 (patch)
tree6c02f4d9d6e95f9a2d596c1854d5938daeeeddcc /tests/Network/KRPCSpec.hs
parent3c9e37d4f349ba2b4395cb10b5a3671decf89d68 (diff)
parenta8498921ddf37e864968a3865e3e254352b5d285 (diff)
Merge branch 'krpc' into dht-only
Diffstat (limited to 'tests/Network/KRPCSpec.hs')
-rw-r--r--tests/Network/KRPCSpec.hs59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/Network/KRPCSpec.hs b/tests/Network/KRPCSpec.hs
new file mode 100644
index 00000000..eabcc817
--- /dev/null
+++ b/tests/Network/KRPCSpec.hs
@@ -0,0 +1,59 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
3module Network.KRPCSpec (spec) where
4import Control.Monad.Logger
5import Control.Monad.Reader
6import Network.KRPC
7import Network.KRPC.MethodSpec hiding (spec)
8import Test.Hspec
9
10servAddr :: SockAddr
11servAddr = SockAddrInet 6000 (256 * 256 * 256 + 127)
12
13handlers :: [Handler IO]
14handlers =
15 [ handler $ \ _ Ping -> return Ping
16 , handler $ \ _ (Echo a) -> return (Echo (a :: Bool))
17 , handler $ \ _ (Echo a) -> return (Echo (a :: Int))
18 ]
19
20instance MonadLogger IO where
21 monadLoggerLog _ _ _ _ = return ()
22
23opts :: Options
24opts = def { optQueryTimeout = 1 }
25
26spec :: Spec
27spec = do
28 let qr :: MonadKRPC h m => SockAddr -> Echo Int -> m (Echo Int)
29 qr = query
30
31 describe "manager" $ do
32 it "is active until closeManager called" $ do
33 m <- newManager opts servAddr []
34 isActive m `shouldReturn` True
35 closeManager m
36 isActive m `shouldReturn` False
37
38 describe "query" $ do
39 it "run handlers" $ do
40 let int = 0xabcd :: Int
41 (withManager opts servAddr handlers $ runReaderT $ do
42 listen
43 query servAddr (Echo int))
44 `shouldReturn` Echo int
45
46 it "count transactions properly" $ do
47 (withManager opts servAddr handlers $ runReaderT $ do
48 listen
49 _ <- qr servAddr (Echo 0xabcd)
50 _ <- qr servAddr (Echo 0xabcd)
51 getQueryCount
52 )
53 `shouldReturn` 2
54
55 it "throw timeout exception" $ do
56 (withManager opts servAddr handlers $ runReaderT $ do
57 qr servAddr (Echo 0xabcd)
58 )
59 `shouldThrow` (== TimeoutExpired)