@@ -6,9 +6,11 @@ import {
66import {
77 Context ,
88 IWorkflowEngineService ,
9+ Logger ,
910 RemoteQueryFunction ,
1011} from "@medusajs/framework/types"
1112import {
13+ ContainerRegistrationKeys ,
1214 Module ,
1315 Modules ,
1416 promiseAll ,
@@ -41,6 +43,7 @@ import {
4143 workflowEventGroupIdStep2Mock ,
4244} from "../__fixtures__/workflow_event_group_id"
4345import { createScheduled } from "../__fixtures__/workflow_scheduled"
46+ import { container , MedusaContainer } from "@medusajs/framework"
4447
4548jest . setTimeout ( 60000 )
4649
@@ -54,15 +57,44 @@ const failTrap = (done) => {
5457 } , 5000 )
5558}
5659
60+ function times ( num ) {
61+ let resolver
62+ let counter = 0
63+ const promise = new Promise ( ( resolve ) => {
64+ resolver = resolve
65+ } )
66+
67+ return {
68+ next : ( ) => {
69+ counter += 1
70+ if ( counter === num ) {
71+ resolver ( )
72+ }
73+ } ,
74+ // Force resolution after 10 seconds to prevent infinite awaiting
75+ promise : Promise . race ( [
76+ promise ,
77+ new Promise ( ( _ , reject ) => {
78+ setTimeoutSync (
79+ ( ) => reject ( "times has not been resolved after 10 seconds." ) ,
80+ 10000
81+ )
82+ } ) ,
83+ ] ) ,
84+ }
85+ }
86+
5787moduleIntegrationTestRunner < IWorkflowEngineService > ( {
5888 moduleName : Modules . WORKFLOW_ENGINE ,
5989 resolve : __dirname + "/../.." ,
6090 testSuite : ( { service : workflowOrcModule , medusaApp } ) => {
6191 describe ( "Workflow Orchestrator module" , function ( ) {
6292 let query : RemoteQueryFunction
93+ let sharedContainer_ : MedusaContainer
6394
6495 beforeEach ( ( ) => {
6596 query = medusaApp . query
97+ sharedContainer_ = medusaApp . sharedContainer
6698 } )
6799
68100 it ( `should export the appropriate linkable configuration` , ( ) => {
@@ -797,7 +829,6 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
797829 describe ( "Scheduled workflows" , ( ) => {
798830 beforeEach ( ( ) => {
799831 jest . clearAllMocks ( )
800- jest . useFakeTimers ( )
801832
802833 // Register test-value in the container for all tests
803834 const sharedContainer =
@@ -809,86 +840,69 @@ moduleIntegrationTestRunner<IWorkflowEngineService>({
809840 )
810841 } )
811842
812- afterEach ( ( ) => {
813- jest . useRealTimers ( )
814- } )
815-
816843 it ( "should execute a scheduled workflow" , async ( ) => {
817- const spy = createScheduled ( "standard" , {
818- interval : 1000 ,
819- } )
820-
821- expect ( spy ) . toHaveBeenCalledTimes ( 0 )
822-
823- await jest . advanceTimersByTimeAsync ( 1100 )
824-
825- expect ( spy ) . toHaveBeenCalledTimes ( 1 )
826-
827- await jest . advanceTimersByTimeAsync ( 1100 )
844+ const wait = times ( 2 )
845+ const spy = createScheduled ( "standard" , wait . next )
828846
847+ await wait . promise
829848 expect ( spy ) . toHaveBeenCalledTimes ( 2 )
849+ WorkflowManager . unregister ( "standard" )
830850 } )
831851
832852 it ( "should stop executions after the set number of executions" , async ( ) => {
833- const spy = await createScheduled ( "num-executions" , {
853+ const wait = times ( 2 )
854+ const spy = createScheduled ( "num-executions" , wait . next , {
834855 interval : 1000 ,
835856 numberOfExecutions : 2 ,
836857 } )
837858
838- expect ( spy ) . toHaveBeenCalledTimes ( 0 )
839-
840- await jest . advanceTimersByTimeAsync ( 1100 )
841-
842- expect ( spy ) . toHaveBeenCalledTimes ( 1 )
843-
844- await jest . advanceTimersByTimeAsync ( 1100 )
845-
859+ await wait . promise
846860 expect ( spy ) . toHaveBeenCalledTimes ( 2 )
847861
848- await jest . advanceTimersByTimeAsync ( 1100 )
849-
862+ // Make sure that on the next tick it doesn't execute again
863+ await setTimeoutPromise ( 1100 )
850864 expect ( spy ) . toHaveBeenCalledTimes ( 2 )
865+
866+ WorkflowManager . unregister ( "num-execution" )
851867 } )
852868
853869 it ( "should remove scheduled workflow if workflow no longer exists" , async ( ) => {
854- const spy = await createScheduled ( "remove-scheduled" , {
870+ const wait = times ( 1 )
871+ const logger = sharedContainer_ . resolve < Logger > (
872+ ContainerRegistrationKeys . LOGGER
873+ )
874+
875+ const spy = createScheduled ( "remove-scheduled" , wait . next , {
855876 interval : 1000 ,
856877 } )
857- const logSpy = jest . spyOn ( console , "warn" )
858-
859- expect ( spy ) . toHaveBeenCalledTimes ( 0 )
860-
861- await jest . advanceTimersByTimeAsync ( 1100 )
878+ const logSpy = jest . spyOn ( logger , "warn" )
862879
880+ await wait . promise
863881 expect ( spy ) . toHaveBeenCalledTimes ( 1 )
864-
865882 WorkflowManager [ "workflows" ] . delete ( "remove-scheduled" )
866883
867- await jest . advanceTimersByTimeAsync ( 1100 )
884+ await setTimeoutPromise ( 1100 )
868885 expect ( spy ) . toHaveBeenCalledTimes ( 1 )
869886 expect ( logSpy ) . toHaveBeenCalledWith (
870887 "Tried to execute a scheduled workflow with ID remove-scheduled that does not exist, removing it from the scheduler."
871888 )
872889 } )
873890
874891 it ( "the scheduled workflow should have access to the shared container" , async ( ) => {
875- const spy = await createScheduled ( "shared-container-job" , {
892+ const wait = times ( 1 )
893+
894+ const spy = await createScheduled ( "shared-container-job" , wait . next , {
876895 interval : 1000 ,
877- numberOfExecutions : 1 ,
878896 } )
897+ await wait . promise
879898
880- const initialCallCount = spy . mock . calls . length
881-
882- await jest . advanceTimersByTimeAsync ( 1100 )
899+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
883900
884- expect ( spy ) . toHaveBeenCalledTimes ( initialCallCount + 1 )
901+ console . log ( spy . mock . results )
885902 expect ( spy ) . toHaveReturnedWith (
886903 expect . objectContaining ( { output : { testValue : "test" } } )
887904 )
888-
889- await jest . advanceTimersByTimeAsync ( 1100 )
890-
891- expect ( spy ) . toHaveBeenCalledTimes ( initialCallCount + 1 )
905+ WorkflowManager . unregister ( "shared-container-job" )
892906 } )
893907
894908 it ( "should fetch an idempotent workflow after its completion" , async ( ) => {
0 commit comments