File: NetworkSpec.hs

package info (click to toggle)
haskell-conduit-extra 1.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 288 kB
  • sloc: haskell: 2,601; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (5)
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
{-# LANGUAGE OverloadedStrings #-}
module Data.Conduit.NetworkSpec (spec) where

import Data.Conduit
import Data.Conduit.Network
import Control.Concurrent (forkIO, threadDelay, putMVar, newEmptyMVar, takeMVar, killThread)
import Control.Monad (replicateM_)
import Test.Hspec

spec :: Spec
spec = describe "Data.Conduit.Network" $ do
    describe "run general server" $ do
        it "running tcp server" $ do
            _ <- forkIO $ runTCPServer (serverSettings 4009 "*4") echo
            threadDelay 1000000
            replicateM_ 100
                $ runTCPClient (clientSettings 4009 "127.0.0.1") doNothing
    describe "fork server" $ do
        it "can connect to server" $ do
            let set = serverSettings 4010 "*4"
            threadId <- forkTCPServer set echo
            replicateM_ 100
                $ runTCPClient (clientSettings 4010 "127.0.0.1") doNothing
            killThread threadId

        it "fork server also executes custom afterBind" $ do
            assertMVar <- newEmptyMVar
            let set = serverSettings 4010 "*4"
                setWithAfterBind = setAfterBind (\_ -> putMVar assertMVar ()) set
            threadId <- forkTCPServer setWithAfterBind echo
            takeMVar assertMVar
            killThread threadId

        it "fork server really waits for server to be finalized before returning" $ do
            let set = serverSettings 4010 "*4"
                setWithAfterBind = setAfterBind (\_ -> threadDelay 1000000) set
            threadId <- forkTCPServer setWithAfterBind echo
            replicateM_ 100
                $ runTCPClient (clientSettings 4010 "127.0.0.1") doNothing
            killThread threadId



echo :: AppData -> IO ()
echo ad = runConduit $ appSource ad .| appSink ad

doNothing :: AppData -> IO ()
doNothing _ = return ()