on master help sign in
Add tests for retry
1af9389Adithya Kumar
test/lib/Streamly/Test/Prelude/Common.hs+35−1
@@ -136,6 +136,8 @@ import qualified Streamly.Data.Fold as FL
136import qualified Streamly.Internal.Data.Stream.IsStream as S
136import qualified Streamly.Internal.Data.Stream.IsStream as S
137import qualified Streamly.Internal.Data.Unfold as UF
137import qualified Streamly.Internal.Data.Unfold as UF
138
138
139+import qualified Data.Map.Strict as Map
140+
139import Streamly.Test.Common
141import Streamly.Test.Common
140
142
141maxStreamLen :: Int
143maxStreamLen :: Int
@@ -1498,6 +1500,37 @@ finallyProp t vec =
1498 refValue <- run $ readIORef ioRef
1500 refValue <- run $ readIORef ioRef
1499 assert $ refValue == 1
1501 assert $ refValue == 1
1500
1502
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+
1501#ifdef DEVBUILD
1534#ifdef DEVBUILD
1502finallyPartialStreamProp ::
1535finallyPartialStreamProp ::
1503 (IsStream t) => (t IO Int -> SerialT IO Int) -> [Int] -> Property
1536 (IsStream t) => (t IO Int -> SerialT IO Int) -> [Int] -> Property
@@ -1606,12 +1639,13 @@ exceptionOps desc t = do
1606#endif
1639#endif
1607 prop (desc <> " finally exception in stream") $ finallyExceptionProp t
1640 prop (desc <> " finally exception in stream") $ finallyExceptionProp t
1608 prop (desc <> " handle") $ handleProp t
1641 prop (desc <> " handle") $ handleProp t
1642+ retry
1609
1643
1610-------------------------------------------------------------------------------
1644-------------------------------------------------------------------------------
1611-- Compose with MonadThrow
1645-- Compose with MonadThrow
1612-------------------------------------------------------------------------------
1646-------------------------------------------------------------------------------
1613
1647
1614−newtype ExampleException = ExampleException String deriving (Eq, Show)
1648+newtype ExampleException = ExampleException String deriving (Eq, Show, Ord)
1615
1649
1616instance Exception ExampleException
1650instance Exception ExampleException
1617
1651