From 1106acba53097047334ed2b7afbcad089ede0037 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Thu, 17 Sep 2020 09:49:20 -0400 Subject: [PATCH] Speed up the recently added "database is busy" SQLite3 test It was passing but only after ~57 seconds because the busy_timeout setting in the test definition was being applied to a different connection than was being used by the implementation. With this change we hook into the connection function and apply the shorter busy_timeout after the normal setup work is done but before handing the connection off to application code. --- test/Persistence.hs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/test/Persistence.hs b/test/Persistence.hs index bd28094..1f2cd6d 100644 --- a/test/Persistence.hs +++ b/test/Persistence.hs @@ -205,16 +205,24 @@ sqlite3DatabaseVoucherPaymentTests = sqlite3Tests = testGroup "SQLite3-specific voucher" [ testCase "database is busy" $ do - getDB <- makeDatabase - db <- getDB - case db of + aDatabase <- makeDatabase + normalConnection <- aDatabase + case normalConnection of (SQLiteDB connect) -> do - conn <- connect - -- Tweak the timeout down so the test completes quickly - Sqlite.execute_ conn "PRAGMA busy_timeout = 0" - -- Acquire a write lock before letting the application code run so that - -- the application code is denied the write lock. - Sqlite.withExclusiveTransaction conn $ do - result <- redeemVoucher db voucher fingerprint + -- Acquire a write lock before letting the application code run + -- so that the application code is denied the write lock. + normalConn <- connect + fastBusyConn <- fastBusyConnection connect + Sqlite.withExclusiveTransaction normalConn $ do + result <- redeemVoucher fastBusyConn voucher fingerprint assertEqual "Redeeming voucher while database busy" result $ Left DatabaseUnavailable ] + where + fastBusyConnection + :: IO Sqlite.Connection + -> IO VoucherDatabaseState + fastBusyConnection connect = do + conn <- connect + -- Tweak the timeout down so the test completes quickly + Sqlite.execute_ conn "PRAGMA busy_timeout = 0" + return . SQLiteDB . return $ conn -- GitLab