diff options
author | joe <joe@jerkface.net> | 2017-01-18 21:24:38 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-01-18 21:24:38 -0500 |
commit | 1d7dd944e0a13d3f09b65f7629d1f96098ea7974 (patch) | |
tree | 6c02f4d9d6e95f9a2d596c1854d5938daeeeddcc /tests/Network/KRPCSpec.hs | |
parent | 3c9e37d4f349ba2b4395cb10b5a3671decf89d68 (diff) | |
parent | a8498921ddf37e864968a3865e3e254352b5d285 (diff) |
Merge branch 'krpc' into dht-only
Diffstat (limited to 'tests/Network/KRPCSpec.hs')
-rw-r--r-- | tests/Network/KRPCSpec.hs | 59 |
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 #-} | ||
3 | module Network.KRPCSpec (spec) where | ||
4 | import Control.Monad.Logger | ||
5 | import Control.Monad.Reader | ||
6 | import Network.KRPC | ||
7 | import Network.KRPC.MethodSpec hiding (spec) | ||
8 | import Test.Hspec | ||
9 | |||
10 | servAddr :: SockAddr | ||
11 | servAddr = SockAddrInet 6000 (256 * 256 * 256 + 127) | ||
12 | |||
13 | handlers :: [Handler IO] | ||
14 | handlers = | ||
15 | [ handler $ \ _ Ping -> return Ping | ||
16 | , handler $ \ _ (Echo a) -> return (Echo (a :: Bool)) | ||
17 | , handler $ \ _ (Echo a) -> return (Echo (a :: Int)) | ||
18 | ] | ||
19 | |||
20 | instance MonadLogger IO where | ||
21 | monadLoggerLog _ _ _ _ = return () | ||
22 | |||
23 | opts :: Options | ||
24 | opts = def { optQueryTimeout = 1 } | ||
25 | |||
26 | spec :: Spec | ||
27 | spec = 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) | ||