-- | Tests for the 'EventSource' and 'EventSink' implementation in 'Data.EventSource.SQLite'.
module Hydra.Events.SQLiteBasedSpec where

import Hydra.Prelude hiding (label)
import Test.Hydra.Prelude

import Control.Concurrent.Class.MonadSTM (putTMVar, takeTMVar, tryPutTMVar)
import Control.Tracer (Tracer (..), emit)
import Control.Tracer.JSON (nullTracer)
import Data.EventSource (EventSink (..), EventSource (..), getEvents, putEvent)
import Data.EventSource.Rotation (EventStore (..))
import Data.EventSource.SQLite (EventDecodingException, SQLiteLog (..), getSchemaVersion, nextVersion, withSQLiteEventStore)
import Data.List (zipWith3)
import Data.List qualified as List
import Database.SQLite.Simple (close, execute, open)
import Hydra.HeadLogic.Outcome (StateChanged (NetworkConnected))
import Hydra.HeadLogic.StateEvent (StateEvent (..))
import Hydra.Ledger.Simple (SimpleTx)
import System.Directory (doesFileExist)
import Test.Hydra.HeadLogic.StateEvent ()
import Test.Hydra.Ledger.Simple ()
import Test.QuickCheck (forAllShrink, generate, ioProperty, sublistOf, suchThat, (===))
import Test.QuickCheck.Gen (listOf)

spec :: Spec
spec :: Spec
spec = do
  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"bounded batch writes" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$
    [Int] -> (Int -> Spec) -> Spec
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
999, Int
1000, Int
1001, Int
2501 :: Int] ((Int -> Spec) -> Spec) -> (Int -> Spec) -> Spec
forall a b. (a -> b) -> a -> b
$ \Int
count ->
      String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it (String
"persists and deduplicates a batch of " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall b a. (Show a, IsString b) => a -> b
show Int
count String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" events") (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
        NominalDiffTime -> IO () -> IO ()
forall (m :: * -> *) a.
(HasCallStack, MonadTimer m, MonadThrow m) =>
NominalDiffTime -> m a -> m a
failAfter NominalDiffTime
10 (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
          (EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO ())
-> IO ()
forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink ((EventSource (StateEvent SimpleTx) IO
  -> EventSink (StateEvent SimpleTx) IO -> IO ())
 -> IO ())
-> (EventSource (StateEvent SimpleTx) IO
    -> EventSink (StateEvent SimpleTx) IO -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \EventSource (StateEvent SimpleTx) IO
source EventSink (StateEvent SimpleTx) IO
sink -> do
            UTCTime
now <- IO UTCTime
forall (m :: * -> *). MonadTime m => m UTCTime
getCurrentTime
            let events :: [StateEvent SimpleTx]
events = [EventId -> StateChanged SimpleTx -> UTCTime -> StateEvent SimpleTx
forall tx. EventId -> StateChanged tx -> UTCTime -> StateEvent tx
StateEvent EventId
i StateChanged SimpleTx
forall tx. StateChanged tx
NetworkConnected UTCTime
now | EventId
i <- [EventId
0 .. Int -> EventId
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count EventId -> EventId -> EventId
forall a. Num a => a -> a -> a
- EventId
1]]
            EventSink (StateEvent SimpleTx) IO
-> HasEventId (StateEvent SimpleTx) =>
   [StateEvent SimpleTx] -> IO ()
forall e (m :: * -> *).
EventSink e m -> HasEventId e => [e] -> m ()
putEvents EventSink (StateEvent SimpleTx) IO
sink [StateEvent SimpleTx]
events
            EventSink (StateEvent SimpleTx) IO
-> HasEventId (StateEvent SimpleTx) =>
   [StateEvent SimpleTx] -> IO ()
forall e (m :: * -> *).
EventSink e m -> HasEventId e => [e] -> m ()
putEvents EventSink (StateEvent SimpleTx) IO
sink [StateEvent SimpleTx]
events
            EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
source IO [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> IO ()
`shouldReturn` [StateEvent SimpleTx]
events

  String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reports SQLite completion only after the batch commits" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
    NominalDiffTime -> IO () -> IO ()
forall (m :: * -> *) a.
(HasCallStack, MonadTimer m, MonadThrow m) =>
NominalDiffTime -> m a -> m a
failAfter NominalDiffTime
5 (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
      String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"sqlite-completion" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
dir -> do
        TMVar ()
started <- String -> IO (TMVar IO ())
forall (m :: * -> *) a.
MonadLabelledSTM m =>
String -> m (TMVar m a)
newLabelledEmptyTMVarIO String
"write-started"
        TMVar ()
allowCommit <- String -> IO (TMVar IO ())
forall (m :: * -> *) a.
MonadLabelledSTM m =>
String -> m (TMVar m a)
newLabelledEmptyTMVarIO String
"allow-commit"
        IORef [SQLiteLog]
observations <- [SQLiteLog] -> IO (IORef [SQLiteLog])
forall (m :: * -> *) a. MonadIO m => a -> m (IORef a)
newIORef []
        UTCTime
now <- IO UTCTime
forall (m :: * -> *). MonadTime m => m UTCTime
getCurrentTime
        let tracer :: Tracer IO SQLiteLog
tracer = TracerA IO SQLiteLog () -> Tracer IO SQLiteLog
forall (m :: * -> *) a. TracerA m a () -> Tracer m a
Tracer (TracerA IO SQLiteLog () -> Tracer IO SQLiteLog)
-> ((SQLiteLog -> IO ()) -> TracerA IO SQLiteLog ())
-> (SQLiteLog -> IO ())
-> Tracer IO SQLiteLog
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SQLiteLog -> IO ()) -> TracerA IO SQLiteLog ()
forall (m :: * -> *) a.
Applicative m =>
(a -> m ()) -> TracerA m a ()
emit ((SQLiteLog -> IO ()) -> Tracer IO SQLiteLog)
-> (SQLiteLog -> IO ()) -> Tracer IO SQLiteLog
forall a b. (a -> b) -> a -> b
$ \SQLiteLog
message -> do
              IORef [SQLiteLog] -> ([SQLiteLog] -> ([SQLiteLog], ())) -> IO ()
forall (m :: * -> *) a b.
MonadIO m =>
IORef a -> (a -> (a, b)) -> m b
atomicModifyIORef' IORef [SQLiteLog]
observations (([SQLiteLog] -> ([SQLiteLog], ())) -> IO ())
-> ([SQLiteLog] -> ([SQLiteLog], ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[SQLiteLog]
events -> (SQLiteLog
message SQLiteLog -> [SQLiteLog] -> [SQLiteLog]
forall a. a -> [a] -> [a]
: [SQLiteLog]
events, ())
              case SQLiteLog
message of
                SQLiteLog
SQLiteWriteStarted -> STM IO () -> IO ()
forall a. HasCallStack => STM IO a -> IO a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (TMVar IO () -> () -> STM IO ()
forall a. TMVar IO a -> a -> STM IO ()
forall (m :: * -> *) a. MonadSTM m => TMVar m a -> a -> STM m ()
putTMVar TMVar ()
TMVar IO ()
started ()) IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> STM IO () -> IO ()
forall a. HasCallStack => STM IO a -> IO a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (TMVar IO () -> STM IO ()
forall a. TMVar IO a -> STM IO a
forall (m :: * -> *) a. MonadSTM m => TMVar m a -> STM m a
takeTMVar TMVar ()
TMVar IO ()
allowCommit)
                SQLiteLog
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            event :: StateEvent SimpleTx
event = EventId -> StateChanged SimpleTx -> UTCTime -> StateEvent SimpleTx
forall tx. EventId -> StateChanged tx -> UTCTime -> StateEvent tx
StateEvent EventId
0 StateChanged SimpleTx
forall tx. StateChanged tx
NetworkConnected UTCTime
now :: StateEvent SimpleTx
        Tracer IO SQLiteLog
-> String
-> String
-> (EventStore (StateEvent SimpleTx) IO -> IO ())
-> IO ()
forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore Tracer IO SQLiteLog
tracer (String
dir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db") (String
dir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state") ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore{EventSink (StateEvent SimpleTx) IO
eventSink :: EventSink (StateEvent SimpleTx) IO
eventSink :: forall e (m :: * -> *). EventStore e m -> EventSink e m
eventSink, EventSource (StateEvent SimpleTx) IO
eventSource :: EventSource (StateEvent SimpleTx) IO
eventSource :: forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource} -> do
          EventSink (StateEvent SimpleTx) IO
-> HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent EventSink (StateEvent SimpleTx) IO
eventSink StateEvent SimpleTx
event
          IO () -> IO () -> IO () -> IO ()
forall a b c. IO a -> IO b -> IO c -> IO c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> m b -> m c -> m c
bracket_
            (STM IO () -> IO ()
forall a. HasCallStack => STM IO a -> IO a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM IO () -> IO ()) -> STM IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ TMVar IO () -> STM IO ()
forall a. TMVar IO a -> STM IO a
forall (m :: * -> *) a. MonadSTM m => TMVar m a -> STM m a
takeTMVar TMVar ()
TMVar IO ()
started)
            (IO Bool -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO Bool -> IO ()) -> (STM Bool -> IO Bool) -> STM Bool -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. STM Bool -> IO Bool
STM IO Bool -> IO Bool
forall a. HasCallStack => STM IO a -> IO a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM Bool -> IO ()) -> STM Bool -> IO ()
forall a b. (a -> b) -> a -> b
$ TMVar IO () -> () -> STM IO Bool
forall a. TMVar IO a -> a -> STM IO Bool
forall (m :: * -> *) a. MonadSTM m => TMVar m a -> a -> STM m Bool
tryPutTMVar TMVar ()
TMVar IO ()
allowCommit ())
            ( do
                [SQLiteLog]
samples <- IORef [SQLiteLog] -> IO [SQLiteLog]
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef [SQLiteLog]
observations
                [Double
writeSeconds | SQLiteWriteCompleted{Double
writeSeconds :: Double
writeSeconds :: SQLiteLog -> Double
writeSeconds} <- [SQLiteLog]
samples] [Double] -> [Double] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` []
            )
          EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
eventSource IO [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> IO ()
`shouldReturn` [StateEvent SimpleTx
event]
          [SQLiteLog]
samples <- IORef [SQLiteLog] -> IO [SQLiteLog]
forall (m :: * -> *) a. MonadIO m => IORef a -> m a
readIORef IORef [SQLiteLog]
observations
          [Double
writeSeconds Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
>= Double
0 | SQLiteWriteCompleted{Double
writeSeconds :: SQLiteLog -> Double
writeSeconds :: Double
writeSeconds} <- [SQLiteLog]
samples] [Bool] -> [Bool] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [Bool
True]

  String -> Spec -> Spec
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"mkSQLiteEventStore" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"can stream events" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
      Gen [StateEvent SimpleTx]
-> ([StateEvent SimpleTx] -> [[StateEvent SimpleTx]])
-> ([StateEvent SimpleTx] -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink Gen [StateEvent SimpleTx]
genContinuousEvents [StateEvent SimpleTx] -> [[StateEvent SimpleTx]]
forall a. Arbitrary a => a -> [a]
shrink (([StateEvent SimpleTx] -> Property) -> Property)
-> ([StateEvent SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateEvent SimpleTx]
events ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$
          (EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink ((EventSource (StateEvent SimpleTx) IO
  -> EventSink (StateEvent SimpleTx) IO -> IO Property)
 -> IO Property)
-> (EventSource (StateEvent SimpleTx) IO
    -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall a b. (a -> b) -> a -> b
$ \EventSource{HasEventId (StateEvent SimpleTx) =>
ConduitT () (StateEvent SimpleTx) (ResourceT IO) ()
sourceEvents :: HasEventId (StateEvent SimpleTx) =>
ConduitT () (StateEvent SimpleTx) (ResourceT IO) ()
sourceEvents :: forall e (m :: * -> *).
EventSource e m -> HasEventId e => ConduitT () e (ResourceT m) ()
sourceEvents} EventSink{HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent :: forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent :: HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent} -> do
            [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
events StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
            -- XXX: Should assert while streaming
            [StateEvent SimpleTx]
streamedEvents <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents ((HasEventId (StateEvent SimpleTx) =>
 ConduitT () (StateEvent SimpleTx) (ResourceT IO) ())
-> EventSource (StateEvent SimpleTx) IO
forall e (m :: * -> *).
(HasEventId e => ConduitT () e (ResourceT m) ()) -> EventSource e m
EventSource ConduitT () (StateEvent SimpleTx) (ResourceT IO) ()
HasEventId (StateEvent SimpleTx) =>
ConduitT () (StateEvent SimpleTx) (ResourceT IO) ()
sourceEvents)
            Property -> IO Property
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Property -> IO Property) -> Property -> IO Property
forall a b. (a -> b) -> a -> b
$
              [StateEvent SimpleTx]
streamedEvents [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [StateEvent SimpleTx]
events

    String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"can handle continuous events" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
      Gen [StateEvent SimpleTx]
-> ([StateEvent SimpleTx] -> [[StateEvent SimpleTx]])
-> ([StateEvent SimpleTx] -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink Gen [StateEvent SimpleTx]
genContinuousEvents [StateEvent SimpleTx] -> [[StateEvent SimpleTx]]
forall a. Arbitrary a => a -> [a]
shrink (([StateEvent SimpleTx] -> Property) -> Property)
-> ([StateEvent SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateEvent SimpleTx]
events ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ do
          (EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink ((EventSource (StateEvent SimpleTx) IO
  -> EventSink (StateEvent SimpleTx) IO -> IO Property)
 -> IO Property)
-> (EventSource (StateEvent SimpleTx) IO
    -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall a b. (a -> b) -> a -> b
$ \EventSource (StateEvent SimpleTx) IO
src EventSink{HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent :: forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent :: HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent} -> do
            [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
events StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
            [StateEvent SimpleTx]
loadedEvents <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
src
            Property -> IO Property
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Property -> IO Property) -> Property -> IO Property
forall a b. (a -> b) -> a -> b
$
              [StateEvent SimpleTx]
loadedEvents [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [StateEvent SimpleTx]
events

    String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"can handle non-continuous events" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
      Gen [StateEvent SimpleTx]
-> ([StateEvent SimpleTx] -> [[StateEvent SimpleTx]])
-> ([StateEvent SimpleTx] -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink ([StateEvent SimpleTx] -> Gen [StateEvent SimpleTx]
forall a. [a] -> Gen [a]
sublistOf ([StateEvent SimpleTx] -> Gen [StateEvent SimpleTx])
-> Gen [StateEvent SimpleTx] -> Gen [StateEvent SimpleTx]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Gen [StateEvent SimpleTx]
genContinuousEvents) [StateEvent SimpleTx] -> [[StateEvent SimpleTx]]
forall a. Arbitrary a => a -> [a]
shrink (([StateEvent SimpleTx] -> Property) -> Property)
-> ([StateEvent SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateEvent SimpleTx]
events ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$ do
          (EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink ((EventSource (StateEvent SimpleTx) IO
  -> EventSink (StateEvent SimpleTx) IO -> IO Property)
 -> IO Property)
-> (EventSource (StateEvent SimpleTx) IO
    -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall a b. (a -> b) -> a -> b
$ \EventSource (StateEvent SimpleTx) IO
src EventSink{HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent :: forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent :: HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent} -> do
            [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
events StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
            [StateEvent SimpleTx]
loadedEvents <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
src
            Property -> IO Property
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Property -> IO Property) -> Property -> IO Property
forall a b. (a -> b) -> a -> b
$
              [StateEvent SimpleTx]
loadedEvents [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [StateEvent SimpleTx]
events

    String -> Property -> Spec
forall prop.
(HasCallStack, Testable prop) =>
String -> prop -> Spec
prop String
"can handle duplicate events" (Property -> Spec) -> Property -> Spec
forall a b. (a -> b) -> a -> b
$
      Gen [StateEvent SimpleTx]
-> ([StateEvent SimpleTx] -> [[StateEvent SimpleTx]])
-> ([StateEvent SimpleTx] -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> [a]) -> (a -> prop) -> Property
forAllShrink Gen [StateEvent SimpleTx]
genContinuousEvents [StateEvent SimpleTx] -> [[StateEvent SimpleTx]]
forall a. Arbitrary a => a -> [a]
shrink (([StateEvent SimpleTx] -> Property) -> Property)
-> ([StateEvent SimpleTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[StateEvent SimpleTx]
events ->
        IO Property -> Property
forall prop. Testable prop => IO prop -> Property
ioProperty (IO Property -> Property) -> IO Property -> Property
forall a b. (a -> b) -> a -> b
$
          (EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink ((EventSource (StateEvent SimpleTx) IO
  -> EventSink (StateEvent SimpleTx) IO -> IO Property)
 -> IO Property)
-> (EventSource (StateEvent SimpleTx) IO
    -> EventSink (StateEvent SimpleTx) IO -> IO Property)
-> IO Property
forall a b. (a -> b) -> a -> b
$ \EventSource (StateEvent SimpleTx) IO
src EventSink{HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent :: forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent :: HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent} -> do
            [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
events StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
            [StateEvent SimpleTx]
loadedEvents <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
src
            -- Put the loaded events again (as the node would do)
            [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
loadedEvents StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
            [StateEvent SimpleTx]
allEvents <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
src
            Property -> IO Property
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Property -> IO Property) -> Property -> IO Property
forall a b. (a -> b) -> a -> b
$
              [StateEvent SimpleTx]
allEvents [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [StateEvent SimpleTx]
loadedEvents

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"throws EventDecodingException on invalid data in database" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"hydra-sqlite-persistence" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
tmpDir -> do
        let dbFile :: String
dbFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db"
            stateFile :: String
stateFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state"
        forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore @(StateEvent SimpleTx) Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore (StateEvent SimpleTx) IO
store -> do
          -- Insert a row with undecodable data directly via a separate connection
          IO Connection
-> (Connection -> IO ()) -> (Connection -> IO ()) -> IO ()
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (String -> IO Connection
open String
dbFile) Connection -> IO ()
close ((Connection -> IO ()) -> IO ()) -> (Connection -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Connection
conn ->
            Connection -> Query -> (EventId, ByteString) -> IO ()
forall q. ToRow q => Connection -> Query -> q -> IO ()
execute Connection
conn Query
"INSERT INTO events (event_id, event_data) VALUES (?, ?)" (EventId
1 :: Word64, ByteString
"not valid cbor" :: ByteString)
          EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents (EventStore (StateEvent SimpleTx) IO
-> EventSource (StateEvent SimpleTx) IO
forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource EventStore (StateEvent SimpleTx) IO
store)
            IO [StateEvent SimpleTx]
-> Selector EventDecodingException -> IO ()
forall e a.
(HasCallStack, Exception e) =>
IO a -> Selector e -> IO ()
`shouldThrow` \(EventDecodingException
_ :: EventDecodingException) -> Bool
True

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"fresh database ends up at nextVersion" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"hydra-sqlite-persistence" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
tmpDir -> do
        let dbFile :: String
dbFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db"
            stateFile :: String
stateFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state"
        forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore @(StateEvent SimpleTx) Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore (StateEvent SimpleTx) IO
_ -> do
          Int
v <- IO Connection
-> (Connection -> IO ()) -> (Connection -> IO Int) -> IO Int
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (String -> IO Connection
open String
dbFile) Connection -> IO ()
close Connection -> IO Int
getSchemaVersion
          Int
v Int -> Int -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` Int
nextVersion

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"reopens the current format and replays its persisted events" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$
      String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"hydra-sqlite-persistence" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
tmpDir -> do
        let dbFile :: String
dbFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db"
            stateFile :: String
stateFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state"
        StateEvent SimpleTx
event :: StateEvent SimpleTx <- Gen (StateEvent SimpleTx) -> IO (StateEvent SimpleTx)
forall a. Gen a -> IO a
generate Gen (StateEvent SimpleTx)
forall a. Arbitrary a => Gen a
arbitrary
        Tracer IO SQLiteLog
-> String
-> String
-> (EventStore (StateEvent SimpleTx) IO -> IO ())
-> IO ()
forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore{EventSink (StateEvent SimpleTx) IO
eventSink :: forall e (m :: * -> *). EventStore e m -> EventSink e m
eventSink :: EventSink (StateEvent SimpleTx) IO
eventSink} ->
          EventSink (StateEvent SimpleTx) IO
-> HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent EventSink (StateEvent SimpleTx) IO
eventSink StateEvent SimpleTx
event
        Tracer IO SQLiteLog
-> String
-> String
-> (EventStore (StateEvent SimpleTx) IO -> IO ())
-> IO ()
forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore{EventSource (StateEvent SimpleTx) IO
eventSource :: forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource :: EventSource (StateEvent SimpleTx) IO
eventSource} -> do
          EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
eventSource IO [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> IO ()
`shouldReturn` [StateEvent SimpleTx
event]
          IO Connection
-> (Connection -> IO ()) -> (Connection -> IO Int) -> IO Int
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (String -> IO Connection
open String
dbFile) Connection -> IO ()
close Connection -> IO Int
getSchemaVersion IO Int -> Int -> IO ()
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> IO ()
`shouldReturn` Int
nextVersion

    String -> IO () -> SpecWith (Arg (IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"keeps a backup of the database before rotating" (IO () -> SpecWith (Arg (IO ())))
-> IO () -> SpecWith (Arg (IO ()))
forall a b. (a -> b) -> a -> b
$ do
      String -> (String -> IO ()) -> IO ()
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"hydra-sqlite-persistence" ((String -> IO ()) -> IO ()) -> (String -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \String
tmpDir -> do
        let dbFile :: String
dbFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db"
            stateFile :: String
stateFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state"
        [StateEvent SimpleTx]
events <- Gen [StateEvent SimpleTx] -> IO [StateEvent SimpleTx]
forall a. Gen a -> IO a
generate (Gen [StateEvent SimpleTx] -> IO [StateEvent SimpleTx])
-> Gen [StateEvent SimpleTx] -> IO [StateEvent SimpleTx]
forall a b. (a -> b) -> a -> b
$ Gen [StateEvent SimpleTx]
genContinuousEvents Gen [StateEvent SimpleTx]
-> ([StateEvent SimpleTx] -> Bool) -> Gen [StateEvent SimpleTx]
forall a. Gen a -> (a -> Bool) -> Gen a
`suchThat` (Bool -> Bool
not (Bool -> Bool)
-> ([StateEvent SimpleTx] -> Bool) -> [StateEvent SimpleTx] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [StateEvent SimpleTx] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)
        let lastId :: EventId
lastId = StateEvent SimpleTx -> EventId
forall tx. StateEvent tx -> EventId
eventId ([StateEvent SimpleTx] -> StateEvent SimpleTx
forall a. HasCallStack => [a] -> a
List.last [StateEvent SimpleTx]
events)
            -- content is irrelevant here; we only assert on the archived events
            checkpointEvent :: StateEvent SimpleTx
checkpointEvent = [StateEvent SimpleTx] -> StateEvent SimpleTx
forall a. HasCallStack => [a] -> a
List.last [StateEvent SimpleTx]
events
        forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore @(StateEvent SimpleTx) Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore{eventSink :: forall e (m :: * -> *). EventStore e m -> EventSink e m
eventSink = EventSink{HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent :: forall e (m :: * -> *). EventSink e m -> HasEventId e => e -> m ()
putEvent :: HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent}, EventSource (StateEvent SimpleTx) IO
eventSource :: forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource :: EventSource (StateEvent SimpleTx) IO
eventSource, EventId -> StateEvent SimpleTx -> IO ()
rotate :: EventId -> StateEvent SimpleTx -> IO ()
rotate :: forall e (m :: * -> *). EventStore e m -> EventId -> e -> m ()
rotate} -> do
          [StateEvent SimpleTx] -> (StateEvent SimpleTx -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [StateEvent SimpleTx]
events StateEvent SimpleTx -> IO ()
HasEventId (StateEvent SimpleTx) => StateEvent SimpleTx -> IO ()
putEvent
          EventId -> StateEvent SimpleTx -> IO ()
rotate EventId
lastId StateEvent SimpleTx
checkpointEvent
          -- the active store holds only the checkpoint after rotation
          [StateEvent SimpleTx]
active <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
eventSource
          [StateEvent SimpleTx]
active [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [StateEvent SimpleTx
checkpointEvent]
        -- the pre-rotation events are retained in the numbered backup
        let backupPath :: String
backupPath = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/old-state/hydra-" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> EventId -> String
forall b a. (Show a, IsString b) => a -> b
show EventId
lastId String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
".db"
        String -> IO Bool
doesFileExist String
backupPath IO Bool -> Bool -> IO ()
forall a. (HasCallStack, Show a, Eq a) => IO a -> a -> IO ()
`shouldReturn` Bool
True
        forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore @(StateEvent SimpleTx) Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
backupPath String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ())
-> (EventStore (StateEvent SimpleTx) IO -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \EventStore{EventSource (StateEvent SimpleTx) IO
eventSource :: forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource :: EventSource (StateEvent SimpleTx) IO
eventSource} -> do
          [StateEvent SimpleTx]
backedUp <- EventSource (StateEvent SimpleTx) IO -> IO [StateEvent SimpleTx]
forall e (m :: * -> *).
(HasEventId e, MonadUnliftIO m) =>
EventSource e m -> m [e]
getEvents EventSource (StateEvent SimpleTx) IO
eventSource
          [StateEvent SimpleTx]
backedUp [StateEvent SimpleTx] -> [StateEvent SimpleTx] -> IO ()
forall a. (HasCallStack, Show a, Eq a) => a -> a -> IO ()
`shouldBe` [StateEvent SimpleTx]
events

genContinuousEvents :: Gen [StateEvent SimpleTx]
genContinuousEvents :: Gen [StateEvent SimpleTx]
genContinuousEvents =
  [StateChanged SimpleTx] -> [UTCTime] -> [StateEvent SimpleTx]
forall tx. [StateChanged tx] -> [UTCTime] -> [StateEvent tx]
mkContinuousEvents ([StateChanged SimpleTx] -> [UTCTime] -> [StateEvent SimpleTx])
-> Gen [StateChanged SimpleTx]
-> Gen ([UTCTime] -> [StateEvent SimpleTx])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (StateChanged SimpleTx) -> Gen [StateChanged SimpleTx]
forall a. Gen a -> Gen [a]
listOf Gen (StateChanged SimpleTx)
forall a. Arbitrary a => Gen a
arbitrary Gen ([UTCTime] -> [StateEvent SimpleTx])
-> Gen [UTCTime] -> Gen [StateEvent SimpleTx]
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen UTCTime -> Gen [UTCTime]
forall a. Gen a -> Gen [a]
listOf Gen UTCTime
forall a. Arbitrary a => Gen a
arbitrary

mkContinuousEvents :: [StateChanged tx] -> [UTCTime] -> [StateEvent tx]
mkContinuousEvents :: forall tx. [StateChanged tx] -> [UTCTime] -> [StateEvent tx]
mkContinuousEvents = (EventId -> StateChanged tx -> UTCTime -> StateEvent tx)
-> [EventId] -> [StateChanged tx] -> [UTCTime] -> [StateEvent tx]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 EventId -> StateChanged tx -> UTCTime -> StateEvent tx
forall tx. EventId -> StateChanged tx -> UTCTime -> StateEvent tx
StateEvent [EventId
0 ..]

withEventSourceAndSink :: (EventSource (StateEvent SimpleTx) IO -> EventSink (StateEvent SimpleTx) IO -> IO b) -> IO b
withEventSourceAndSink :: forall b.
(EventSource (StateEvent SimpleTx) IO
 -> EventSink (StateEvent SimpleTx) IO -> IO b)
-> IO b
withEventSourceAndSink EventSource (StateEvent SimpleTx) IO
-> EventSink (StateEvent SimpleTx) IO -> IO b
action =
  String -> (String -> IO b) -> IO b
forall (m :: * -> *) r.
(MonadIO m, MonadMask m) =>
String -> (String -> m r) -> m r
withTempDir String
"hydra-sqlite-persistence" ((String -> IO b) -> IO b) -> (String -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \String
tmpDir -> do
    let dbFile :: String
dbFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/hydra.db"
        stateFile :: String
stateFile = String
tmpDir String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"/state"
    Tracer IO SQLiteLog
-> String
-> String
-> (EventStore (StateEvent SimpleTx) IO -> IO b)
-> IO b
forall e a.
(ToCBOR e, FromCBOR e, HasEventId e) =>
Tracer IO SQLiteLog
-> String -> String -> (EventStore e IO -> IO a) -> IO a
withSQLiteEventStore Tracer IO SQLiteLog
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer String
dbFile String
stateFile ((EventStore (StateEvent SimpleTx) IO -> IO b) -> IO b)
-> (EventStore (StateEvent SimpleTx) IO -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \EventStore{EventSource (StateEvent SimpleTx) IO
eventSource :: forall e (m :: * -> *). EventStore e m -> EventSource e m
eventSource :: EventSource (StateEvent SimpleTx) IO
eventSource, EventSink (StateEvent SimpleTx) IO
eventSink :: forall e (m :: * -> *). EventStore e m -> EventSink e m
eventSink :: EventSink (StateEvent SimpleTx) IO
eventSink} ->
      EventSource (StateEvent SimpleTx) IO
-> EventSink (StateEvent SimpleTx) IO -> IO b
action EventSource (StateEvent SimpleTx) IO
eventSource EventSink (StateEvent SimpleTx) IO
eventSink