on master help sign in
Add test for onException and finally.
commit27018944004c8c8b0f06602848389be527b8794c authorpranaysashank <pranaysashank@composewell.com> dateThu 26 Nov 2020, 13:30 (6 years ago) parentedab416
1 file changed+74−1
test/Streamly/Test/Prelude/Common.hs+74−1
test/Streamly/Test/Prelude/Common.hs+74−1
@@ -1415,7 +1415,7 @@ bracketProp t vec =
14151415 (\ioref ->
14161416 S.mapM
14171417 (\a -> writeIORef ioref 2 >> return a)
1418− (S.fromList []))
1418+ (S.fromList vec))
14191419 refValue <- run $ readIORef ioRef
14201420 assert $ refValue == 1
14211421
@@ -1460,6 +1460,75 @@ bracketExceptionProp t =
14601460 refValue <- run $ readIORef ioRef
14611461 assert $ refValue == 1
14621462
1463+finallyProp :: (IsStream t) => (t IO Int -> SerialT IO Int) -> [Int] -> Property
1464+finallyProp t vec =
1465+ withMaxSuccess maxTestCount $
1466+ monadicIO $ do
1467+ ioRef <- run $ newIORef (0 :: Int)
1468+ run $
1469+ S.drain . t $
1470+ S.finally
1471+ (writeIORef ioRef 1)
1472+ (S.mapM (\a -> writeIORef ioRef 2 >> return a) (S.fromList vec))
1473+ refValue <- run $ readIORef ioRef
1474+ assert $ refValue == 1
1475+
1476+-- XXX This test fails
1477+_finallyPartialStreamProp ::
1478+ (IsStream t) => (t IO Int -> SerialT IO Int) -> [Int] -> Property
1479+_finallyPartialStreamProp t vec =
1480+ forAll (choose (0, length vec)) $ \len -> do
1481+ withMaxSuccess maxTestCount $
1482+ monadicIO $ do
1483+ ioRef <- run $ newIORef (0 :: Int)
1484+ run $
1485+ S.drain . t $
1486+ S.take len $
1487+ S.finally
1488+ (writeIORef ioRef 1)
1489+ (S.mapM
1490+ (\a -> writeIORef ioRef 2 >> return a)
1491+ (S.fromList vec))
1492+ run performMajorGC
1493+ refValue <- run $ readIORef ioRef
1494+ assert $ refValue == 1
1495+
1496+finallyExceptionProp ::
1497+ (IsStream t, MonadThrow (t IO))
1498+ => (t IO Int -> SerialT IO Int)
1499+ -> Property
1500+finallyExceptionProp t =
1501+ withMaxSuccess maxTestCount $
1502+ monadicIO $ do
1503+ ioRef <- run $ newIORef (0 :: Int)
1504+ res <-
1505+ run $
1506+ try . S.drain . t $
1507+ S.finally
1508+ (writeIORef ioRef 1)
1509+ (throwM (ExampleException "E") <> S.nil)
1510+ assert $ res == Left (ExampleException "E")
1511+ refValue <- run $ readIORef ioRef
1512+ assert $ refValue == 1
1513+
1514+onExceptionProp ::
1515+ (IsStream t, MonadThrow (t IO))
1516+ => (t IO Int -> SerialT IO Int)
1517+ -> Property
1518+onExceptionProp t =
1519+ withMaxSuccess maxTestCount $
1520+ monadicIO $ do
1521+ ioRef <- run $ newIORef (0 :: Int)
1522+ res <-
1523+ run $
1524+ try . S.drain . t $
1525+ S.onException
1526+ (writeIORef ioRef 1)
1527+ (throwM (ExampleException "E") <> S.nil)
1528+ assert $ res == Left (ExampleException "E")
1529+ refValue <- run $ readIORef ioRef
1530+ assert $ refValue == 1
1531+
14631532exceptionOps ::
14641533 (IsStream t, MonadThrow (t IO))
14651534 => String
@@ -1471,6 +1540,10 @@ exceptionOps desc t = do
14711540 prop (desc <> " bracket end of stream") $ bracketProp t
14721541 -- prop (desc <> " bracket partial stream") $ bracketPartialStreamProp t
14731542 prop (desc <> " bracket exception in stream") $ bracketExceptionProp t
1543+ prop (desc <> " onException") $ onExceptionProp t
1544+ prop (desc <> " finally end of stream") $ finallyProp t
1545+ -- prop (desc <> " finally partial stream") $ finallyPartialStreamProp t
1546+ prop (desc <> " finally exception in stream") $ finallyExceptionProp t
14741547
14751548-------------------------------------------------------------------------------
14761549-- Compose with MonadThrow