next-ref 0.1.0.1 → 0.1.0.2
raw patch · 3 files changed
+58/−53 lines, 3 filesdep ~basedep ~hspecdep ~next-refPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, hspec, next-ref, stm
API changes (from Hackage documentation)
Files
- next-ref.cabal +30/−29
- src/Control/Concurrent/NextRef.hs +4/−1
- test/Spec.hs +24/−23
next-ref.cabal view
@@ -1,33 +1,34 @@-name: next-ref-version: 0.1.0.1-cabal-version: >=1.10-build-type: Simple-license: BSD3-license-file: LICENSE-copyright: 2016 skedge.me-maintainer: jonathangfischoff@gmail.com-synopsis: A concurrency primitive for a slow consumer.-description:- A concurrency primitive for a slow consumer that can tolerate missing some updates.-category: Web-author: Jonathan Fischoff+name: next-ref+version: 0.1.0.2+synopsis: A concurrency primitive for a slow consumer.+description: A concurrency primitive for a slow consumer that can tolerate missing some updates.+license: BSD3+license-file: LICENSE+author: Jonathan Fischoff+maintainer: jonathangfischoff@gmail.com+copyright: 2016 skedge.me+category: Web+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10 library- exposed-modules:- Control.Concurrent.NextRef- build-depends:- base >=4.7 && <5,- stm >=2.4.4.1 && <2.5- default-language: Haskell2010- hs-source-dirs: src+ hs-source-dirs: src+ exposed-modules: Control.Concurrent.NextRef+ build-depends: base >= 4.6 && < 5+ , stm >= 2.2 && < 2.5+ default-language: Haskell2010 test-suite next-ref-test- type: exitcode-stdio-1.0- main-is: Spec.hs- build-depends:- base >=4.8.2.0 && <4.9,- next-ref >=0.1.0.1 && <0.2,- hspec >=2.2.3 && <2.3- default-language: Haskell2010- hs-source-dirs: test- ghc-options: -threaded -rtsopts -with-rtsopts=-N+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , next-ref+ , hspec >= 2 && < 2.3+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/skedgeme/next-ref
src/Control/Concurrent/NextRef.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE LambdaCase, RecordWildCards, BangPatterns #-}+{-# LANGUAGE LambdaCase, RecordWildCards, BangPatterns, CPP #-} {-| This package contains a concurrency primitive which can be used to limit an expensive consumer from running unnecessarily. Crucially the consumer must be able to tolerate missing some updates.@@ -27,6 +27,9 @@ ) where import Control.Concurrent.STM import Data.IORef+#if !MIN_VERSION_base(4,8,0)+import Control.Applicative+#endif -- | Status is used to prevent future reads. When the status is 'Closed' -- 'takeNextRef' will always return 'Nothing'. When the status is
test/Spec.hs view
@@ -1,71 +1,72 @@-{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE LambdaCase, CPP #-} import Test.Hspec import Control.Concurrent.NextRef import GHC.Conc import Control.Monad (void, replicateM_) while :: IO Bool -> IO () -> IO ()-while test act = test >>= \case +while test act = test >>= \case True -> act >> while test act False -> return ()- + main :: IO ()-main = hspec $ describe "NextRef" $ do +main = hspec $ describe "NextRef" $ do it "newNextRef/takeNext/takeNext blocks" $ do ref <- newNextRef () takeNextRef ref threadId <- forkIO $ void $ takeNextRef ref- + while ((== ThreadRunning) `fmap` threadStatus threadId) $ threadDelay 100000- - stat <- threadStatus threadId ++ stat <- threadStatus threadId+#if __GLASGOW_HASKELL__ == 708+ stat `shouldBe` ThreadBlocked BlockedOnOther+#else stat `shouldBe` ThreadBlocked BlockedOnSTM- +#endif+ it "writing never blocks" $ do ref <- newNextRef 1 replicateM_ 10 $ writeNextRef ref 2- True `shouldBe` True - + True `shouldBe` True+ it "readLast does not block" $ do ref <- newNextRef () takeNextRef ref readLast ref- - True `shouldBe` True - ++ True `shouldBe` True+ it "takeNext/write/takeNext doesn't block, updates correctly" $ do ref <- newNextRef (1 :: Int) actual <- takeNextRef ref actual `shouldBe` Just 1- + writeNextRef ref 2- + actual1 <- takeNextRef ref actual1 `shouldBe` Just 2- + it "takeNext/modify/takeNext doesn't block, updates correctly" $ do ref <- newNextRef (1 :: Int) actual <- takeNextRef ref actual `shouldBe` Just 1- + modifyNextRef ref (\x -> (x+1, ()))- + actual1 <- takeNextRef ref actual1 `shouldBe` Just 2- + it "close/takeNext give nothing" $ do ref <- newNextRef () close ref actual <- takeNextRef ref actual `shouldBe` Nothing- + it "close/open/takeNext gives value" $ do ref <- newNextRef () close ref open ref actual <- takeNextRef ref actual `shouldBe` Just ()- - -