Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow polling until a test succeeds #201

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions beam/task/backend/rhyolite-beam-task-worker-backend.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ test-suite test
, filepath
, gargoyle-postgresql-connect
, hspec
, HUnit
, lens
, monad-logger
, postgresql-simple
Expand Down
31 changes: 24 additions & 7 deletions beam/task/backend/test/Test.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where

Expand All @@ -21,14 +24,28 @@ import Database.PostgreSQL.Serializable
import Gargoyle.PostgreSQL.Connect
import System.FilePath
import System.Posix.Temp
import System.Timeout
import Test.Hspec
import Test.HUnit.Lang

import Rhyolite.Task.Beam
import Rhyolite.Task.Beam.Worker

import Types
import Utils

seconds :: Num a => a -> a
seconds = (* 1e6)

threadDelayPolling :: Int -> Int -> Expectation -> Expectation
threadDelayPolling delayTimeout pollInterval test = timeout delayTimeout go >>= \case
Nothing -> test
Just () -> pure ()
where
go = do
threadDelay pollInterval
catch test $ \(e :: HUnitFailure) -> go
Copy link
Contributor Author

@alexfmpe alexfmpe Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this type is stated to be internal only:
https://hackage.haskell.org/package/HUnit-1.6.2.0/docs/Test-HUnit-Lang.html#g:1
I didn't find a suitable public API equivalent though


setupTable :: Pool Connection -> IO (Pool Connection)
setupTable pool = do
withResource pool $ \dbConn ->
Expand Down Expand Up @@ -251,10 +268,10 @@ main = do
-- All tasks should have the result of the work set, as Just True
-- Each task should have been checked out at some time, with an entry in the map
-- Each task should have been checked out only once.
threadDelay $ (taskCount * timeForOneTask) `div` threadCount
taskMap <- readIORef mapRef
tasks <- allTestTasks c
map _testTaskT_checkedOutBy tasks `shouldBe` replicate taskCount Nothing
map _testTaskT_result tasks `shouldBe` replicate taskCount (Just True)
M.keys taskMap `shouldBe` [1..fromIntegral taskCount]
all (\set -> S.size set == 1) (M.elems taskMap) `shouldBe` True
threadDelayPolling ((taskCount * timeForOneTask * 10) `div` threadCount) (seconds 1) $ do
taskMap <- readIORef mapRef
tasks <- allTestTasks c
map _testTaskT_checkedOutBy tasks `shouldBe` replicate taskCount Nothing
map _testTaskT_result tasks `shouldBe` replicate taskCount (Just True)
M.keys taskMap `shouldBe` [1..fromIntegral taskCount]
all (\set -> S.size set == 1) (M.elems taskMap) `shouldBe` True