-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.hs
69 lines (60 loc) · 1.77 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Control.Concurrent
import Control.Exception
import Data.Typeable
import Network.Fancy
import System.IO
import System.IO.Unsafe
import System.Directory
import System.Random
main = do
ipv4_test
ipv6_test
unix_test
put "> connect tests"
tryE $ connectStream $ IPv4 "google.com" 80
tryE $ connectStream $ IPv4 "foobarbaz.invalid" 0
tryE $ connectDgram $ IPv4 "foobarbaz.invalid" 0
tryE $ connectStream $ IP "foobarbaz.invalid" 0
put "> done"
pri =<< getCurrentHost
put ">testing instances"
let a = IP "" 0
pri a
pri (a == a)
pri (a < a)
put "> dgram server"
tryE $ do
addr <- IPv4 "127.0.0.1" `fmap` rport
let rev :: String -> String
rev = reverse
dgramServer (serverSpec { address = addr, reverseAddress = ReverseName }) (\s sa -> put ("< connect from "++show sa) >> return [rev s])
withDgram addr $ \s -> do
send s "PING"
"GNIP" <- recv s 99
return ()
rport = randomRIO (2000,50000)
ipv4_test = server_test =<< (IPv4 "127.0.0.1" `fmap` rport)
ipv6_test = server_test =<< (IPv6 "::1" `fmap` rport)
unix_test = do
tryE $ removeFile "/tmp/unix_test"
server_test $ Unix "/tmp/unix_test"
server_test adr = tryE $ do
put ("> running server_test "++show adr)
streamServer (serverSpec { address = adr }) $ \h ra -> do
put ("< connect from "++show ra)
hGetLine h >>= hPutStrLn h . reverse
put "> starting client"
withStream adr $ \h -> do
put ("> client to "++show adr)
hPutStrLn h "PING"
hFlush h
"GNIP" <- hGetLine h
put "> ok"
tryE :: IO a -> IO ()
tryE x = try x >>= eh
eh :: Either SomeException a -> IO ()
eh (Left e) = put ("FAILURE: "++ show e)
eh _ = return ()
logLock = unsafePerformIO $ newMVar ()
pri x = withMVar logLock $ \_ -> print x
put x = withMVar logLock $ \_ -> putStrLn x