on master help sign in
Use a basePort var, rename test, add to targets.sh
00a2f7bHarendra Kumar
bin/targets.sh+2
@@ -107,4 +107,6 @@ INDIVIDUAL_TARGETS="\
107 Unicode.Stream \
107 Unicode.Stream \
108 FileSystem.Handle \
108 FileSystem.Handle \
109 `test_only FileSystem.Event` \
109 `test_only FileSystem.Event` \
110+ `test_only Network.Socket` \
111+ `test_only Network.Inet.TCP` \
110 `test_only version-bounds`"
112 `test_only version-bounds`"
test/Streamly/Test/Network/Inet/TCP.hs+28−14
@@ -36,7 +36,7 @@ testData :: String
36testData = "Test data 1234567891012131415!@#$%^&*()`~ABCD"
36testData = "Test data 1234567891012131415!@#$%^&*()`~ABCD"
37
37
38testDataSource :: String
38testDataSource :: String
39−testDataSource = (concat $ replicate 1000 testData)
39+testDataSource = concat $ replicate 1000 testData
40
40
41------------------------------------------------------------------------------
41------------------------------------------------------------------------------
42-- Read and Write data on a socket
42-- Read and Write data on a socket
@@ -50,15 +50,28 @@ handlerRW sk =
50------------------------------------------------------------------------------
50------------------------------------------------------------------------------
51-- Accept connections and handle connected sockets
51-- Accept connections and handle connected sockets
52------------------------------------------------------------------------------
52------------------------------------------------------------------------------
53+
54+-- Ideally we should choose an available port automatically. However, to test
55+-- the APIs that do not allow choosing a port automatically we still need to
56+-- use a hardcoded port or search an available port on the system.
57+--
58+-- This is unreliable and the test may fail on systems where this port is not
59+-- available. We choose a higher port number so that the likelihood of it being
60+-- available is more.
61+--
62+-- Also, we cannot run the test after running it once until the timeout.
63+basePort :: PortNumber
64+basePort = 64100
65+
53server
66server
54 :: Unfold.Unfold IO PortNumber Socket
67 :: Unfold.Unfold IO PortNumber Socket
55 -> PortNumber
68 -> PortNumber
56 -> MVar ()
69 -> MVar ()
57 -> (Socket -> IO ())
70 -> (Socket -> IO ())
58 -> IO ()
71 -> IO ()
59−server addr port sem handler = do
72+server listener port sem handler = do
60 putMVar sem ()
73 putMVar sem ()
61− (Stream.serially $ Stream.unfold addr port)
74+ Stream.serially (Stream.unfold listener port)
62 & (Stream.asyncly . Stream.mapM (Socket.handleWithM handler))
75 & (Stream.asyncly . Stream.mapM (Socket.handleWithM handler))
63 & Stream.drain
76 & Stream.drain
64
77
@@ -69,8 +82,8 @@ sender :: PortNumber -> MVar () -> SerialT IO Char
69sender port sem = do
82sender port sem = do
70 _ <- liftIO $ takeMVar sem
83 _ <- liftIO $ takeMVar sem
71 liftIO $ threadDelay 1000000 -- wait for server
84 liftIO $ threadDelay 1000000 -- wait for server
72− Stream.replicate 1000 testData -- SerialT IO String
85+ Stream.replicate 1000 testData -- SerialT IO String
73− & Stream.concatMap Stream.fromList -- SerialT IO Char
86+ & Stream.concatMap Stream.fromList -- SerialT IO Char
74 & Unicode.encodeLatin1 -- SerialT IO Word8
87 & Unicode.encodeLatin1 -- SerialT IO Word8
75 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
88 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
76 & Unicode.decodeLatin1 -- SerialT IO Char
89 & Unicode.decodeLatin1 -- SerialT IO Char
@@ -81,18 +94,19 @@ execute
81 -> Int
94 -> Int
82 -> (Socket -> IO ())
95 -> (Socket -> IO ())
83 -> IO (SerialT IO Char)
96 -> IO (SerialT IO Char)
84−execute addr port size handler = do
97+execute listener port size handler = do
85 sem <- newEmptyMVar
98 sem <- newEmptyMVar
86− tid <- forkIO $ server addr port sem handler
99+ tid <- forkIO $ server listener port sem handler
87 let lst = sender port sem
100 let lst = sender port sem
88 & Stream.take size
101 & Stream.take size
89 & Stream.finally (killThread tid)
102 & Stream.finally (killThread tid)
90− return $ lst
103+ return lst
91
104
92validateOnPort :: Property
105validateOnPort :: Property
93validateOnPort = monadicIO $ do
106validateOnPort = monadicIO $ do
94 res <- run $ do
107 res <- run $ do
108+ ls2 <-
95− ls2 <- execute TCP.acceptOnPort 8007 Array.defaultChunkSize handlerRW
109+ execute TCP.acceptOnPort basePort Array.defaultChunkSize handlerRW
96 let dataChunk = take Array.defaultChunkSize testDataSource
110 let dataChunk = take Array.defaultChunkSize testDataSource
97 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
111 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
98 assert res
112 assert res
@@ -102,10 +116,10 @@ validateOnPortLocal = monadicIO $ do
102 res <- run $ do
116 res <- run $ do
103 ls2 <-
117 ls2 <-
104 execute
118 execute
105− TCP.acceptOnPortLocal
119+ TCP.acceptOnPortLocal
106− 8008
120+ (basePort + 1)
107− Array.defaultChunkSize
121+ Array.defaultChunkSize
108− handlerRW
122+ handlerRW
109 let dataChunk = take Array.defaultChunkSize testDataSource
123 let dataChunk = take Array.defaultChunkSize testDataSource
110 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
124 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
111 assert res
125 assert res
@@ -113,7 +127,7 @@ validateOnPortLocal = monadicIO $ do
113main :: IO ()
127main :: IO ()
114main = hspec $ do
128main = hspec $ do
115 modifyMaxSuccess (const 1) $ do
129 modifyMaxSuccess (const 1) $ do
116− describe "TCP Tests: " $ do
130+ describe "Accept Connections" $ do
117 prop "acceptOnPort" validateOnPort
131 prop "acceptOnPort" validateOnPort
118 prop "acceptOnPortLocal" validateOnPortLocal
132 prop "acceptOnPortLocal" validateOnPortLocal
119 -- prop "acceptOnAddr/connect" Tested as part of above test cases
133 -- prop "acceptOnAddr/connect" Tested as part of above test cases
test/Streamly/Test/Network/Socket.hs+23−11
@@ -64,14 +64,26 @@ handlerRW sk =
64 & Stream.fold (Socket.write sk)
64 & Stream.fold (Socket.write sk)
65 & discard
65 & discard
66------------------------------------------------------------------------------
66------------------------------------------------------------------------------
67−-- Accept connecttions and handle connected sockets
67+-- Accept connections and handle connected sockets
68------------------------------------------------------------------------------
68------------------------------------------------------------------------------
69
69
70+-- Ideally we should choose an available port automatically. However, to test
71+-- the APIs that do not allow choosing a port automatically we still need to
72+-- use a hardcoded port or search an available port on the system.
73+--
74+-- This is unreliable and the test may fail on systems where this port is not
75+-- available. We choose a higher port number so that the likelihood of it being
76+-- available is more.
77+--
78+-- Also, we cannot run the test after running it once until the timeout.
79+basePort :: PortNumber
80+basePort = 64000
81+
70server :: PortNumber -> MVar () -> (Socket -> IO ()) -> IO ()
82server :: PortNumber -> MVar () -> (Socket -> IO ()) -> IO ()
71server port sem handler = do
83server port sem handler = do
72 putMVar sem ()
84 putMVar sem ()
73− (Stream.serially $ Stream.unfold TCP.acceptOnPort port)
85+ Stream.serially (Stream.unfold TCP.acceptOnPort port)
74− & (Stream.asyncly . Stream.mapM (Socket.handleWithM handler))
86+ & Stream.asyncly . Stream.mapM (Socket.handleWithM handler)
75 & Stream.drain
87 & Stream.drain
76
88
77remoteAddr :: (Word8,Word8,Word8,Word8)
89remoteAddr :: (Word8,Word8,Word8,Word8)
@@ -81,7 +93,7 @@ sender :: PortNumber -> MVar () -> SerialT IO Char
81sender port sem = do
93sender port sem = do
82 _ <- liftIO $ takeMVar sem
94 _ <- liftIO $ takeMVar sem
83 liftIO $ threadDelay 1000000 -- wait for server
95 liftIO $ threadDelay 1000000 -- wait for server
84− Stream.replicate 1000 testData -- SerialT IO String
96+ Stream.replicate 1000 testData -- SerialT IO String
85 & Stream.concatMap Stream.fromList -- SerialT IO Char
97 & Stream.concatMap Stream.fromList -- SerialT IO Char
86 & Unicode.encodeLatin1 -- SerialT IO Word8
98 & Unicode.encodeLatin1 -- SerialT IO Word8
87 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
99 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
@@ -94,19 +106,19 @@ execute port size handler = do
94 let lst = sender port sem
106 let lst = sender port sem
95 & Stream.take size
107 & Stream.take size
96 & Stream.finally (killThread tid)
108 & Stream.finally (killThread tid)
97− return $ lst
109+ return lst
98
110
99validateWithBufferOf :: Property
111validateWithBufferOf :: Property
100validateWithBufferOf = monadicIO $ do
112validateWithBufferOf = monadicIO $ do
101 res <- run $ do
113 res <- run $ do
102− ls2 <- execute 8000 45000 handlerwithbuffer
114+ ls2 <- execute basePort 45000 handlerwithbuffer
103 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
115 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
104 assert res
116 assert res
105
117
106validateRW :: Property
118validateRW :: Property
107validateRW = monadicIO $ do
119validateRW = monadicIO $ do
108 res <- run $ do
120 res <- run $ do
109− ls2 <- execute 8001 Array.defaultChunkSize handlerRW
121+ ls2 <- execute (basePort + 1) Array.defaultChunkSize handlerRW
110 let dataChunk = take Array.defaultChunkSize testDataSource
122 let dataChunk = take Array.defaultChunkSize testDataSource
111 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
123 Stream.eqBy (==) (Stream.fromList dataChunk) ls2
112 assert res
124 assert res
@@ -114,22 +126,22 @@ validateRW = monadicIO $ do
114validateChunks :: Property
126validateChunks :: Property
115validateChunks = monadicIO $ do
127validateChunks = monadicIO $ do
116 res <- run $ do
128 res <- run $ do
117− ls2 <- execute 8002 45000 handlerChunks
129+ ls2 <- execute (basePort + 2) 45000 handlerChunks
118 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
130 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
119 assert res
131 assert res
120
132
121validateChunksWithBufferOf :: Property
133validateChunksWithBufferOf :: Property
122validateChunksWithBufferOf = monadicIO $ do
134validateChunksWithBufferOf = monadicIO $ do
123 res <- run $ do
135 res <- run $ do
124− ls2 <- execute 8003 45000 handlerChunksWithBuffer
136+ ls2 <- execute (basePort + 3) 45000 handlerChunksWithBuffer
125 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
137 Stream.eqBy (==) (Stream.fromList testDataSource) ls2
126 assert res
138 assert res
127
139
128main :: IO ()
140main :: IO ()
129main = hspec $ do
141main = hspec $ do
130 modifyMaxSuccess (const 1) $ do
142 modifyMaxSuccess (const 1) $ do
131− describe "Socket Tests: " $ do
143+ describe "Read/Write" $ do
132 prop "read/write" validateRW
144 prop "read/write" validateRW
133 prop "readWithBufferOf/writeWithBufferOf" validateWithBufferOf
145 prop "readWithBufferOf/writeWithBufferOf" validateWithBufferOf
134− prop "readChunksWithBufferOf" validateChunksWithBufferOf
135 prop "readChunks/writeChunks" validateChunks
146 prop "readChunks/writeChunks" validateChunks
147+ prop "readChunksWithBufferOf" validateChunksWithBufferOf
test/streamly-tests.cabal+2−2
@@ -341,9 +341,9 @@ test-suite FileSystem.Handle
341test-suite Network.Socket
341test-suite Network.Socket
342 import: test-options
342 import: test-options
343 type: exitcode-stdio-1.0
343 type: exitcode-stdio-1.0
344− main-is: Streamly/Test/Network/Socket.hs
344+ main-is: Streamly/Test/Network/Socket.hs
345
345
346−test-suite Inet.TCP
346+test-suite Network.Inet.TCP
347 import: test-options
347 import: test-options
348 type: exitcode-stdio-1.0
348 type: exitcode-stdio-1.0
349 main-is: Streamly/Test/Network/Inet/TCP.hs
349 main-is: Streamly/Test/Network/Inet/TCP.hs