on master help sign in
Address review comments and rebase with master
commit51774339fe8a66f094e907c3f17797b8856e44f3 authorRanjeet Ranjan <ranjeetr@composewell.com> dateThu 31 Dec 2020, 14:47 (6 years ago) parent75deef1
5 files changed+106−79
streamly.cabal+2 test/Streamly/Test/Network/Inet/TCP.hs+38−30 test/Streamly/Test/Network/Socket.hs+40−33 test/Streamly/Test/Unicode/Stream.hs+15−16 test/streamly-tests.cabal+11
streamly.cabal+2
@@ -169,6 +169,8 @@ extra-source-files:
169169 test/Streamly/Test/Data/Parser/ParserD.hs
170170 test/Streamly/Test/FileSystem/Event.hs
171171 test/Streamly/Test/FileSystem/Handle.hs
172+ test/Streamly/Test/Network/Socket.hs
173+ test/Streamly/Test/Network/Inet/TCP.hs
172174 test/Streamly/Test/Prelude.hs
173175 test/Streamly/Test/Prelude/*.hs
174176 test/Streamly/Test/Unicode/Stream.hs
test/Streamly/Test/Network/Inet/TCP.hs+38−30
@@ -19,12 +19,12 @@ import Streamly.Prelude (SerialT)
1919import Test.QuickCheck (Property)
2020import Test.QuickCheck.Monadic (monadicIO, assert, run)
2121
22−import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as A
23−import qualified Streamly.Internal.Data.Unfold as UF
22+import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as Array
23+import qualified Streamly.Internal.Data.Unfold as Unfold
2424import qualified Streamly.Internal.Network.Inet.TCP as TCP
25−import qualified Streamly.Internal.Network.Socket as SK
26−import qualified Streamly.Internal.Unicode.Stream as U
27−import qualified Streamly.Prelude as S
25+import qualified Streamly.Internal.Network.Socket as Socket
26+import qualified Streamly.Internal.Unicode.Stream as Unicode
27+import qualified Streamly.Prelude as Stream
2828
2929import Test.Hspec
3030import Test.Hspec.QuickCheck
@@ -35,32 +35,32 @@ import Test.Hspec.QuickCheck
3535testData :: String
3636testData = "Test data 1234567891012131415!@#$%^&*()`~ABCD"
3737
38−testDataLarge :: String
39−testDataLarge = (concat $ replicate 1000 testData)
38+testDataSource :: String
39+testDataSource = (concat $ replicate 1000 testData)
4040
4141------------------------------------------------------------------------------
4242-- Read and Write data on a socket
4343------------------------------------------------------------------------------
4444handlerRW :: Socket -> IO ()
4545handlerRW sk =
46− S.unfold SK.read sk
47− & S.fold (SK.write sk)
46+ Stream.unfold Socket.read sk
47+ & Stream.fold (Socket.write sk)
4848 & discard
4949
5050------------------------------------------------------------------------------
51−-- Accept connecttions and handle connected sockets
51+-- Accept connections and handle connected sockets
5252------------------------------------------------------------------------------
5353server
54− :: UF.Unfold IO PortNumber Socket
54+ :: Unfold.Unfold IO PortNumber Socket
5555 -> PortNumber
5656 -> MVar ()
5757 -> (Socket -> IO ())
5858 -> IO ()
5959server addr port sem handler = do
6060 putMVar sem ()
61− (S.serially $ S.unfold addr port)
62− & (S.asyncly . S.mapM (SK.handleWithM handler))
63− & S.drain
61+ (Stream.serially $ Stream.unfold addr port)
62+ & (Stream.asyncly . Stream.mapM (Socket.handleWithM handler))
63+ & Stream.drain
6464
6565remoteAddr :: (Word8,Word8,Word8,Word8)
6666remoteAddr = (127, 0, 0, 1)
@@ -69,38 +69,46 @@ sender :: PortNumber -> MVar () -> SerialT IO Char
6969sender port sem = do
7070 _ <- liftIO $ takeMVar sem
7171 liftIO $ threadDelay 1000000 -- wait for server
72− S.replicate 1 testDataLarge -- SerialT IO String
73− & U.unwords UF.fromList -- SerialT IO Char
74− & U.encodeLatin1 -- SerialT IO Word8
72+ Stream.replicate 1000 testData -- SerialT IO String
73+ & Stream.concatMap Stream.fromList -- SerialT IO Char
74+ & Unicode.encodeLatin1 -- SerialT IO Word8
7575 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
76− & U.decodeLatin1 -- SerialT IO Char
76+ & Unicode.decodeLatin1 -- SerialT IO Char
7777
7878execute
79− :: UF.Unfold IO PortNumber Socket
79+ :: Unfold.Unfold IO PortNumber Socket
8080 -> PortNumber
8181 -> Int
8282 -> (Socket -> IO ())
83− -> IO [Char]
83+ -> IO (SerialT IO Char)
8484execute addr port size handler = do
8585 sem <- newEmptyMVar
8686 tid <- forkIO $ server addr port sem handler
87− lst <- sender port sem
88− & S.take size
89− & S.toList
90− killThread tid
87+ let lst = sender port sem
88+ & Stream.take size
89+ & Stream.finally (killThread tid)
9190 return $ lst
9291
9392validateOnPort :: Property
9493validateOnPort = monadicIO $ do
95− ls2 <- run $ execute TCP.acceptOnPort 8000 A.defaultChunkSize handlerRW
96− assert (take A.defaultChunkSize testDataLarge == ls2)
94+ res <- run $ do
95+ ls2 <- execute TCP.acceptOnPort 8007 Array.defaultChunkSize handlerRW
96+ let dataChunk = take Array.defaultChunkSize testDataSource
97+ Stream.eqBy (==) (Stream.fromList dataChunk) ls2
98+ assert res
9799
98100validateOnPortLocal :: Property
99101validateOnPortLocal = monadicIO $ do
100− ls2 <-
101− run
102− $ execute TCP.acceptOnPortLocal 8001 A.defaultChunkSize handlerRW
103− assert (take A.defaultChunkSize testDataLarge == ls2)
102+ res <- run $ do
103+ ls2 <-
104+ execute
105+ TCP.acceptOnPortLocal
106+ 8008
107+ Array.defaultChunkSize
108+ handlerRW
109+ let dataChunk = take Array.defaultChunkSize testDataSource
110+ Stream.eqBy (==) (Stream.fromList dataChunk) ls2
111+ assert res
104112
105113main :: IO ()
106114main = hspec $ do
test/Streamly/Test/Network/Socket.hs+40−33
@@ -19,12 +19,11 @@ import Streamly.Prelude (SerialT)
1919import Test.QuickCheck (Property)
2020import Test.QuickCheck.Monadic (monadicIO, assert, run)
2121
22−import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as A
23−import qualified Streamly.Internal.Data.Unfold as UF
22+import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as Array
2423import qualified Streamly.Internal.Network.Inet.TCP as TCP
25−import qualified Streamly.Internal.Network.Socket as SK
26−import qualified Streamly.Internal.Unicode.Stream as U
27−import qualified Streamly.Prelude as S
24+import qualified Streamly.Internal.Network.Socket as Socket
25+import qualified Streamly.Internal.Unicode.Stream as Unicode
26+import qualified Streamly.Prelude as Stream
2827
2928import Test.Hspec
3029import Test.Hspec.QuickCheck
@@ -43,26 +42,26 @@ testDataSource = concat $ replicate 1000 testData
4342------------------------------------------------------------------------------
4443handlerChunksWithBuffer :: Socket -> IO ()
4544handlerChunksWithBuffer sk =
46− S.unfold SK.readChunksWithBufferOf (100, sk)
47− & S.fold (SK.writeChunks sk)
45+ Stream.unfold Socket.readChunksWithBufferOf (100, sk)
46+ & Stream.fold (Socket.writeChunks sk)
4847 & discard
4948
5049handlerChunks :: Socket -> IO ()
5150handlerChunks sk =
52− S.unfold SK.readChunks sk
53− & S.fold (SK.writeChunks sk)
51+ Stream.unfold Socket.readChunks sk
52+ & Stream.fold (Socket.writeChunks sk)
5453 & discard
5554
5655handlerwithbuffer :: Socket -> IO ()
5756handlerwithbuffer sk =
58− S.unfold SK.readWithBufferOf (100, sk)
59− & S.fold (SK.writeWithBufferOf 100 sk)
57+ Stream.unfold Socket.readWithBufferOf (100, sk)
58+ & Stream.fold (Socket.writeWithBufferOf 100 sk)
6059 & discard
6160
6261handlerRW :: Socket -> IO ()
6362handlerRW sk =
64− S.unfold SK.read sk
65− & S.fold (SK.write sk)
63+ Stream.unfold Socket.read sk
64+ & Stream.fold (Socket.write sk)
6665 & discard
6766------------------------------------------------------------------------------
6867-- Accept connecttions and handle connected sockets
@@ -71,9 +70,9 @@ handlerRW sk =
7170server :: PortNumber -> MVar () -> (Socket -> IO ()) -> IO ()
7271server port sem handler = do
7372 putMVar sem ()
74− (S.serially $ S.unfold TCP.acceptOnPort port)
75− & (S.asyncly . S.mapM (SK.handleWithM handler))
76− & S.drain
73+ (Stream.serially $ Stream.unfold TCP.acceptOnPort port)
74+ & (Stream.asyncly . Stream.mapM (Socket.handleWithM handler))
75+ & Stream.drain
7776
7877remoteAddr :: (Word8,Word8,Word8,Word8)
7978remoteAddr = (127, 0, 0, 1)
@@ -82,41 +81,49 @@ sender :: PortNumber -> MVar () -> SerialT IO Char
8281sender port sem = do
8382 _ <- liftIO $ takeMVar sem
8483 liftIO $ threadDelay 1000000 -- wait for server
85− S.replicate 1 testDataSource -- SerialT IO String
86− & U.unwords UF.fromList -- SerialT IO Char
87− & U.encodeLatin1 -- SerialT IO Word8
84+ Stream.replicate 1000 testData -- SerialT IO String
85+ & Stream.concatMap Stream.fromList -- SerialT IO Char
86+ & Unicode.encodeLatin1 -- SerialT IO Word8
8887 & TCP.transformBytesWith remoteAddr port -- SerialT IO Word8
89− & U.decodeLatin1 -- SerialT IO Char
88+ & Unicode.decodeLatin1 -- SerialT IO Char
9089
91−execute :: PortNumber -> Int -> (Socket -> IO ()) -> IO [Char]
90+execute :: PortNumber -> Int -> (Socket -> IO ()) -> IO (SerialT IO Char)
9291execute port size handler = do
9392 sem <- newEmptyMVar
9493 tid <- forkIO $ server port sem handler
95− lst <- sender port sem
96− & S.take size
97− & S.toList
98− killThread tid
94+ let lst = sender port sem
95+ & Stream.take size
96+ & Stream.finally (killThread tid)
9997 return $ lst
10098
10199validateWithBufferOf :: Property
102100validateWithBufferOf = monadicIO $ do
103− ls2 <- run $ execute 8000 45000 handlerwithbuffer
104− assert (testDataSource == ls2)
101+ res <- run $ do
102+ ls2 <- execute 8000 45000 handlerwithbuffer
103+ Stream.eqBy (==) (Stream.fromList testDataSource) ls2
104+ assert res
105105
106106validateRW :: Property
107107validateRW = monadicIO $ do
108− ls2 <- run $ execute 8001 A.defaultChunkSize handlerRW
109− assert (take A.defaultChunkSize testDataSource == ls2)
108+ res <- run $ do
109+ ls2 <- execute 8001 Array.defaultChunkSize handlerRW
110+ let dataChunk = take Array.defaultChunkSize testDataSource
111+ Stream.eqBy (==) (Stream.fromList dataChunk) ls2
112+ assert res
110113
111114validateChunks :: Property
112115validateChunks = monadicIO $ do
113− ls2 <- run $ execute 8002 45000 handlerChunks
114− assert (testDataSource == ls2)
116+ res <- run $ do
117+ ls2 <- execute 8002 45000 handlerChunks
118+ Stream.eqBy (==) (Stream.fromList testDataSource) ls2
119+ assert res
115120
116121validateChunksWithBufferOf :: Property
117122validateChunksWithBufferOf = monadicIO $ do
118− ls2 <- run $ execute 8003 45000 handlerChunksWithBuffer
119− assert (testDataSource == ls2)
123+ res <- run $ do
124+ ls2 <- execute 8003 45000 handlerChunksWithBuffer
125+ Stream.eqBy (==) (Stream.fromList testDataSource) ls2
126+ assert res
120127
121128main :: IO ()
122129main = hspec $ do
test/Streamly/Test/Unicode/Stream.hs+15−16
@@ -86,23 +86,23 @@ propASCIIToLatin1 =
8686
8787propUnicodeToLatin1 :: Property
8888propUnicodeToLatin1 =
89− monadicIO $ do
90− let wrds =
91− SS.decodeLatin1
92− $ SS.encodeLatin1
93− $ S.fromList unicodeTestData
94− lst <- run $ S.toList wrds
95− assert (latin1TestData == lst)
89+ monadicIO $ do
90+ let wrds =
91+ SS.decodeLatin1
92+ $ SS.encodeLatin1
93+ $ S.fromList unicodeTestData
94+ lst <- run $ S.toList wrds
95+ assert (latin1TestData == lst)
9696
9797propUnicodeToLatin1' :: Property
9898propUnicodeToLatin1' =
99− monadicIO $ do
100− let wrds =
101− SS.decodeLatin1
102− $ SS.encodeLatin1'
103− $ S.fromList unicodeTestData
104− lst <- run $ S.toList wrds
105− assert (latin1TestData == lst)
99+ monadicIO $ do
100+ let wrds =
101+ SS.decodeLatin1
102+ $ SS.encodeLatin1'
103+ $ S.fromList unicodeTestData
104+ lst <- run $ S.toList wrds
105+ assert (latin1TestData == lst)
106106
107107testLines :: Property
108108testLines =
@@ -167,7 +167,7 @@ main = H.hspec
167167 prop "decodeUtf8Arrays . encodeUtf8' == id"
168168 propDecodeEncodeIdArrays
169169 prop "Streamly.Data.String.lines == Prelude.lines" testLines
170− prop "Arrays Streamly.Data.String.lines == Prelude.lines"
170+ prop "Arrays Streamly.Data.String.lines == Prelude.lines"
171171 testLinesArray
172172 prop "Streamly.Data.String.words == Prelude.words" testWords
173173 prop
@@ -181,4 +181,3 @@ main = H.hspec
181181 prop "ASCII to Latin1" propASCIIToLatin1
182182 prop "Unicode to Latin1" propUnicodeToLatin1
183183 prop "Unicode to Latin1'" $ expectFailure propUnicodeToLatin1'
184−
test/streamly-tests.cabal+11
@@ -128,6 +128,7 @@ common test-dependencies
128128 , directory >= 1.2.2 && < 1.4
129129 , filepath >= 1.4.1 && < 1.5
130130 , temporary >= 1.3 && < 1.4
131+ , network >= 3.1 && < 3.2
131132
132133 if flag(fusion-plugin) && !impl(ghcjs) && !impl(ghc < 8.6)
133134 build-depends:
@@ -336,3 +337,13 @@ test-suite FileSystem.Handle
336337 type: exitcode-stdio-1.0
337338 main-is: Streamly/Test/FileSystem/Handle.hs
338339 ghc-options: -main-is Streamly.Test.FileSystem.Handle.main
340+
341+test-suite Network.Socket
342+ import: test-options
343+ type: exitcode-stdio-1.0
344+ main-is: Streamly/Test/Network/Socket.hs
345+
346+test-suite Inet.TCP
347+ import: test-options
348+ type: exitcode-stdio-1.0
349+ main-is: Streamly/Test/Network/Inet/TCP.hs