on master help sign in
added failing test for callbacks
commitc0e4ee29246a539a5a7820ae93dcedfe68c748f1 authorSaurabh Nanda <saurabh@vacationlabs.com> dateSat 26 Jun 2021, 19:49 (5 years ago) parent1f76f88
1 file changed+37−7
test/Test.hs+37−7
test/Test.hs+37−7
@@ -95,9 +95,14 @@ tests appPool jobPool = testGroup "All tests"
9595 , testJobFailure appPool jobPool
9696 , testEnsureShutdown appPool jobPool
9797 , testGracefulShutdown appPool jobPool
98− , testJobErrHandler appPool jobPool
9998 , testPushFailedJobEndQueue jobPool
10099 , testRetryBackoff appPool jobPool
100+ , testGroup "callback tests"
101+ [ testOnJobFailed appPool jobPool
102+ , testOnJobStart appPool jobPool
103+ , testOnJobSuccess appPool jobPool
104+ , testOnJobTimeout appPool jobPool
105+ ]
101106 ]
102107 -- , testGroup "property tests" [ testEverything appPool jobPool
103108 -- -- , propFilterJobs appPool jobPool
@@ -344,7 +349,6 @@ testJobFailure appPool jobPool = testCase "job failure" $ do
344349 assertEqual "Exepcting job to be in Failed status" Job.Failed jobStatus
345350 assertEqual ("Expecting job attempts to be 3. Found " <> show jobAttempts) 3 jobAttempts
346351
347−
348352testPushFailedJobEndQueue :: Pool Connection -> TestTree
349353testPushFailedJobEndQueue jobPool = testCase "testPushFailedJobEndQueue" $ do
350354 withRandomTable jobPool $ \tname -> do
@@ -357,7 +361,7 @@ testPushFailedJobEndQueue jobPool = testCase "testPushFailedJobEndQueue" $ do
357361 "Expecting the current job to be 2 since job 1 has been modified"
358362 (jobId job2) resId
359363
360−testJobErrHandler appPool jobPool = testCase "job error handler" $ do
364+testOnJobFailed appPool jobPool = testCase "job error handler" $ do
361365 withRandomTable jobPool $ \tname -> do
362366 Pool.withResource appPool $ \conn -> do
363367 _ <- Job.createJob conn tname (PayloadThrowStringException "forced exception")
@@ -375,6 +379,36 @@ testJobErrHandler appPool jobPool = testCase "job error handler" $ do
375379 readMVar mvar1 >>= assertEqual "Error Handler 1 doesn't seem to have run" True
376380 readMVar mvar2 >>= assertEqual "Error Handler 2 doesn't seem to have run" True
377381
382+testOnJobStart appPool jobPool = testCase "onJobStart" $ do
383+ withRandomTable jobPool $ \tname -> do
384+ callbackRef <- newIORef False
385+ let callback = const $ modifyIORef' callbackRef (const True)
386+ withNamedJobMonitor tname jobPool (\cfg -> cfg{Job.cfgOnJobStart=callback}) $ \logRef -> do
387+ Pool.withResource appPool $ \conn -> do
388+ Job.createJob conn tname (PayloadSucceed 0)
389+ delaySeconds (2 * Job.defaultPollingInterval)
390+ readIORef callbackRef >>= assertBool "It seems that onJobStart callback has not been called"
391+
392+testOnJobSuccess appPool jobPool = testCase "onJobSuccess" $ do
393+ withRandomTable jobPool $ \tname -> do
394+ callbackRef <- newIORef False
395+ let callback = const $ modifyIORef' callbackRef (const True)
396+ withNamedJobMonitor tname jobPool (\cfg -> cfg{Job.cfgOnJobSuccess=callback}) $ \logRef -> do
397+ Pool.withResource appPool $ \conn -> do
398+ Job.createJob conn tname (PayloadSucceed 0)
399+ delaySeconds (2 * Job.defaultPollingInterval)
400+ readIORef callbackRef >>= assertBool "It seems that onJobSuccess callback has not been called"
401+
402+testOnJobTimeout appPool jobPool = testCase "onJobTimeout" $ do
403+ withRandomTable jobPool $ \tname -> do
404+ callbackRef <- newIORef False
405+ let callback = const $ modifyIORef' callbackRef (const True)
406+ withNamedJobMonitor tname jobPool (\cfg -> cfg{Job.cfgOnJobTimeout=callback, Job.cfgDefaultJobTimeout=Seconds 2}) $ \logRef -> do
407+ Pool.withResource appPool $ \conn -> do
408+ Job.createJob conn tname (PayloadSucceed 10)
409+ delaySeconds (2 * Job.defaultPollingInterval)
410+ readIORef callbackRef >>= assertBool "It seems that onJobTimeout callback has not been called"
411+
378412testRetryBackoff appPool jobPool = testCase "retry backoff" $ do
379413 withRandomTable jobPool $ \tname -> do
380414 withNamedJobMonitor tname jobPool modifyRetryBackoff $ \_logRef -> do
@@ -516,10 +550,6 @@ payloadDelay jobPollingInterval = payloadDelay_ (Seconds 0)
516550 PayloadThrowStringException _ -> defaultDelay 0
517551 PayloadThrowDivideByZero -> defaultDelay 0
518552
519−
520−
521−-- TODO: test to ensure that errors in callback do not affect the running of jobs
522−
523553deriving instance Enum Time.Unit
524554deriving instance Enum Time.Direction
525555