mirror of
https://github.com/nikita-volkov/hasql.git
synced 2024-12-27 04:22:59 +03:00
Add threads test
This commit is contained in:
parent
c0d96eb5b2
commit
4f23fe5908
24
hasql.cabal
24
hasql.cabal
@ -138,6 +138,30 @@ test-suite tasty
|
||||
rebase
|
||||
|
||||
|
||||
test-suite threads-test
|
||||
type:
|
||||
exitcode-stdio-1.0
|
||||
hs-source-dirs:
|
||||
threads-test
|
||||
main-is:
|
||||
Main.hs
|
||||
other-modules:
|
||||
Main.Queries
|
||||
ghc-options:
|
||||
-O2
|
||||
-threaded
|
||||
"-with-rtsopts=-N"
|
||||
default-extensions:
|
||||
Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples
|
||||
default-language:
|
||||
Haskell2010
|
||||
build-depends:
|
||||
-- database:
|
||||
hasql,
|
||||
-- base,
|
||||
rebase
|
||||
|
||||
|
||||
benchmark benchmark
|
||||
type:
|
||||
exitcode-stdio-1.0
|
||||
|
45
threads-test/Main.hs
Normal file
45
threads-test/Main.hs
Normal file
@ -0,0 +1,45 @@
|
||||
module Main where
|
||||
|
||||
import Rebase.Prelude
|
||||
import qualified Hasql.Connection
|
||||
import qualified Hasql.Query
|
||||
import qualified Hasql.Encoders
|
||||
import qualified Hasql.Decoders
|
||||
import qualified Hasql.Session
|
||||
import qualified Main.Queries as Queries
|
||||
|
||||
|
||||
main =
|
||||
acquire >>= use
|
||||
where
|
||||
acquire =
|
||||
(,) <$> acquire <*> acquire
|
||||
where
|
||||
acquire =
|
||||
join $
|
||||
fmap (either (fail . show) return) $
|
||||
Hasql.Connection.acquire connectionSettings
|
||||
where
|
||||
connectionSettings =
|
||||
Hasql.Connection.settings "localhost" 5432 "postgres" "" "postgres"
|
||||
use (connection1, connection2) =
|
||||
do
|
||||
beginVar <- newEmptyMVar
|
||||
finishVar <- newEmptyMVar
|
||||
forkIO $ do
|
||||
traceM "1: in"
|
||||
putMVar beginVar ()
|
||||
session connection1 (Hasql.Session.query 0.2 Queries.selectSleep)
|
||||
traceM "1: out"
|
||||
void (tryPutMVar finishVar False)
|
||||
forkIO $ do
|
||||
takeMVar beginVar
|
||||
traceM "2: in"
|
||||
session connection2 (Hasql.Session.query 0.1 Queries.selectSleep)
|
||||
traceM "2: out"
|
||||
void (tryPutMVar finishVar True)
|
||||
bool exitFailure exitSuccess . traceShowId =<< takeMVar finishVar
|
||||
where
|
||||
session connection session =
|
||||
Hasql.Session.run session connection >>=
|
||||
either (fail . show) return
|
20
threads-test/Main/Queries.hs
Normal file
20
threads-test/Main/Queries.hs
Normal file
@ -0,0 +1,20 @@
|
||||
module Main.Queries where
|
||||
|
||||
import Rebase.Prelude
|
||||
import Hasql.Query
|
||||
import qualified Hasql.Encoders as E
|
||||
import qualified Hasql.Decoders as D
|
||||
|
||||
|
||||
selectSleep :: Query Double ()
|
||||
selectSleep =
|
||||
statement sql encoder decoder True
|
||||
where
|
||||
sql =
|
||||
"select pg_sleep($1)"
|
||||
encoder =
|
||||
E.value E.float8
|
||||
decoder =
|
||||
D.unit
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user