on master help sign in
Add tests for retry
commit1af938999bea948f6a1d229d9e80068cdd6ebeb9 authorAdithya Kumar <adithya@composewell.com> dateSat 7 Aug 2021, 00:44 (5 years ago) parent17dfc5b
1 file changed+35−1
test/lib/Streamly/Test/Prelude/Common.hs+35−1
test/lib/Streamly/Test/Prelude/Common.hs+35−1
@@ -136,6 +136,8 @@ import qualified Streamly.Data.Fold as FL
136136import qualified Streamly.Internal.Data.Stream.IsStream as S
137137import qualified Streamly.Internal.Data.Unfold as UF
138138
139+import qualified Data.Map.Strict as Map
140+
139141import Streamly.Test.Common
140142
141143maxStreamLen :: Int
@@ -1498,6 +1500,37 @@ finallyProp t vec =
14981500 refValue <- run $ readIORef ioRef
14991501 assert $ refValue == 1
15001502
1503+retry :: Spec
1504+retry = do
1505+ ref <- runIO $ newIORef (0 :: Int)
1506+ res <- runIO $ S.toList (S.retry emap handler (stream1 ref))
1507+ refVal <- runIO $ readIORef ref
1508+ spec res refVal
1509+
1510+ where
1511+
1512+ emap = Map.singleton (ExampleException "E") 10
1513+
1514+ stream1 ref =
1515+ S.fromListM
1516+ [ return 1
1517+ , return 2
1518+ , atomicModifyIORef' ref (\a -> (a + 1, ()))
1519+ >> throwM (ExampleException "E")
1520+ >> return 3
1521+ , return 4
1522+ ]
1523+
1524+ stream2 = S.fromList [5, 6, 7 :: Int]
1525+ handler = const stream2
1526+ expectedRes = [1, 2, 5, 6, 7]
1527+ expectedRefVal = 11
1528+
1529+ spec res refVal = do
1530+ it "Runs the exception handler properly" $ res `shouldBe` expectedRes
1531+ it "Runs retires the exception correctly"
1532+ $ refVal `shouldBe` expectedRefVal
1533+
15011534#ifdef DEVBUILD
15021535finallyPartialStreamProp ::
15031536 (IsStream t) => (t IO Int -> SerialT IO Int) -> [Int] -> Property
@@ -1606,12 +1639,13 @@ exceptionOps desc t = do
16061639#endif
16071640 prop (desc <> " finally exception in stream") $ finallyExceptionProp t
16081641 prop (desc <> " handle") $ handleProp t
1642+ retry
16091643
16101644-------------------------------------------------------------------------------
16111645-- Compose with MonadThrow
16121646-------------------------------------------------------------------------------
16131647
1614−newtype ExampleException = ExampleException String deriving (Eq, Show)
1648+newtype ExampleException = ExampleException String deriving (Eq, Show, Ord)
16151649
16161650instance Exception ExampleException
16171651