Skip to content

Commit

Permalink
actually create one MVar per DB context ...
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Dec 16, 2019
1 parent 159c9f4 commit 4639180
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import Cardano.Wallet.Primitive.AddressDiscovery
import Control.Arrow
( (***) )
import Control.Concurrent.MVar
( newEmptyMVar, putMVar, readMVar )
( modifyMVar_, newMVar )
import Control.DeepSeq
( NFData )
import Control.Exception
Expand All @@ -111,6 +111,8 @@ import Data.Coerce
( coerce )
import Data.Either
( isRight )
import Data.Functor
( ($>) )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.List.Split
Expand Down Expand Up @@ -220,7 +222,7 @@ newDBFactory
-- ^ Path to database directory, or Nothing for in-memory database
-> IO (DBFactory IO s k)
newDBFactory cfg tr mDatabaseDir = do
mvar <- newEmptyMVar
mvar <- newMVar mempty
case mDatabaseDir of
Nothing -> pure DBFactory
{ withDatabase = \_ action ->
Expand All @@ -229,17 +231,20 @@ newDBFactory cfg tr mDatabaseDir = do
pure ()
}
Just databaseDir -> pure DBFactory
{ withDatabase = \wid action ->
withDBLayer cfg tracerDB (Just $ databaseFile wid) $ \(ctx,db) -> do
putMVar mvar ctx
action db
{ withDatabase = \wid action -> do
withDBLayer cfg tracerDB (Just $ databaseFile wid)
$ \(ctx, db) -> do
modifyMVar_ mvar (pure . Map.insert wid ctx)
action db
, removeDatabase = \wid -> do
let files =
[ databaseFile wid
, databaseFile wid <> "-wal"
, databaseFile wid <> "-shm"
]
readMVar mvar >>= destroyDBLayer
modifyMVar_ mvar $ \m -> case Map.lookup wid m of
Nothing -> pure m
Just ctx -> destroyDBLayer ctx $> Map.delete wid m
mapM_ removePathForcibly files
}
where
Expand Down
5 changes: 5 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/DB/SqliteSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ import Cardano.Wallet.Unsafe
( unsafeRunExceptT )
import Control.Concurrent
( forkIO )
import Control.Concurrent.MVar
( newEmptyMVar, takeMVar, tryPutMVar )
import Control.DeepSeq
( NFData )
import Control.Exception
Expand Down Expand Up @@ -356,15 +358,18 @@ fileModeSpec = do
withSystemTempDirectory "DBFactory" $ \dir -> do
cfg <- defaultConfigTesting
DBFactory{..} <- newDBFactory cfg nullTracer (Just dir)
mvar <- newEmptyMVar

-- NOTE
-- Start a concurrent worker which makes action on the DB in
-- parallel to simulate activity.
_ <- forkIO $ withDatabase testWid $ \(DBLayer{..} :: TestDBSeq) -> do
forever $ do
cp <- atomically $ readCheckpoint $ PrimaryKey testWid
_ <- tryPutMVar mvar ()
B8.putStrLn (B8.pack $ show cp)

takeMVar mvar
removeDatabase testWid
listDirectory dir `shouldReturn` mempty

Expand Down

0 comments on commit 4639180

Please sign in to comment.