From cf6bbc0fdf9fa10fc2f77f2fbead286481579e8c Mon Sep 17 00:00:00 2001 From: GuillaumedeVolpiano Date: Fri, 20 Nov 2026 12:07:58 +0100 Subject: [PATCH] Fix inverted predicates in takeInterval and dropInterval The stream elements are wrapped in Just and the timer ticks are Nothing, so "takeWhile isNothing" ended the stream at the first element and "dropWhile isNothing" dropped nothing. Use isJust in both. Add tests that do not depend on time measurement, replacing the dummy test. Fixes #3271 Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_019RtUYCYCwddhe8t5hVfLiv --- src/Streamly/Internal/Data/Stream/Time.hs | 6 +- .../Streamly/Test/Data/Stream/Prelude/Time.hs | 61 ++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/Streamly/Internal/Data/Stream/Time.hs b/src/Streamly/Internal/Data/Stream/Time.hs index a9dd2211b1..a2fda87c73 100644 --- a/src/Streamly/Internal/Data/Stream/Time.hs +++ b/src/Streamly/Internal/Data/Stream/Time.hs @@ -62,7 +62,7 @@ import Control.Monad.IO.Class (MonadIO(..)) import Data.Heap (Entry(..)) import Data.Kind (Type) import Data.Map (Map) -import Data.Maybe (isNothing) +import Data.Maybe (isJust, isNothing) import Data.Proxy (Proxy(..)) import Streamly.Data.Fold (Fold) import Streamly.Data.Array (Unbox) @@ -190,7 +190,7 @@ interject f n xs = parListEagerFst [xs, periodic f n] takeInterval :: MonadAsync m => Double -> Stream m a -> Stream m a takeInterval d = Stream.catMaybes - . Stream.takeWhile isNothing + . Stream.takeWhile isJust . interject (return Nothing) d . fmap Just -- | Take time interval @i@ seconds at the end of the stream. @@ -214,7 +214,7 @@ takeLastInterval = undefined dropInterval :: MonadAsync m => Double -> Stream m a -> Stream m a dropInterval d = Stream.catMaybes - . Stream.dropWhile isNothing + . Stream.dropWhile isJust . interject (return Nothing) d . fmap Just -- | Drop time interval @i@ seconds at the end of the stream. diff --git a/test/Streamly/Test/Data/Stream/Prelude/Time.hs b/test/Streamly/Test/Data/Stream/Prelude/Time.hs index a065d1d91a..b4224524d8 100644 --- a/test/Streamly/Test/Data/Stream/Prelude/Time.hs +++ b/test/Streamly/Test/Data/Stream/Prelude/Time.hs @@ -13,19 +13,19 @@ module Streamly.Test.Data.Stream.Prelude.Time (main) where -#ifdef DEVBUILD import Control.Concurrent ( threadDelay ) +#ifdef DEVBUILD import Control.Monad ( when) import Data.Int (Int64) import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime) import Streamly.Internal.Data.Time.Units (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64) +#endif import qualified Streamly.Internal.Data.Fold as Fold import qualified Streamly.Internal.Data.Stream as Stream import qualified Streamly.Internal.Data.Stream.Prelude as Stream -#endif import Test.Hspec as H @@ -76,6 +76,53 @@ testDropInterval = do checkTakeDropTime (Just t0, mt1) #endif +-- The tests below do not measure time, they only use intervals that are either +-- much longer or much shorter than the time taken by the stream. + +longInterval :: Double +longInterval = 100 + +shortInterval :: Double +shortInterval = 0.1 + +-- | An endless stream of consecutive numbers, one every millisecond. +slowEnumeration :: Stream.Stream IO Int +slowEnumeration = + Stream.mapM (\x -> threadDelay 1000 >> return x) (Stream.enumerateFrom 1) + +takeIntervalAll :: IO [Int] +takeIntervalAll = + Stream.fold Fold.toList + $ Stream.takeInterval longInterval + $ Stream.fromList [1..10] + +dropIntervalAll :: IO [Int] +dropIntervalAll = + Stream.fold Fold.toList + $ Stream.dropInterval longInterval + $ Stream.fromList [1..10] + +-- The stream is endless, so this returns only if takeInterval ends it. The +-- elements taken must be the beginning of the stream. +takeIntervalPrefix :: IO Bool +takeIntervalPrefix = do + xs <- + Stream.fold Fold.toList + $ Stream.takeInterval shortInterval slowEnumeration + return $ not (null xs) && xs == [1..length xs] + +-- The elements that remain must be a suffix of the stream, starting after the +-- first element. +dropIntervalSuffix :: IO Bool +dropIntervalSuffix = do + xs <- + Stream.fold Fold.toList + $ Stream.take 10 + $ Stream.dropInterval shortInterval slowEnumeration + return $ case xs of + x : _ -> x > 1 && xs == [x .. x + 9] + [] -> False + moduleName :: String moduleName = "Data.Stream.Time" @@ -93,5 +140,13 @@ main = hspec it "dropInterval" (testDropInterval `shouldReturn` True) #endif #endif - it "dummy" (return () `shouldReturn` ()) + describe "Trimming" $ do + it "takeInterval takes all the elements within the interval" + (takeIntervalAll `shouldReturn` [1..10]) + it "takeInterval ends the stream after the interval" + (takeIntervalPrefix `shouldReturn` True) + it "dropInterval drops all the elements within the interval" + (dropIntervalAll `shouldReturn` []) + it "dropInterval yields the elements after the interval" + (dropIntervalSuffix `shouldReturn` True)