@@ -1090,12 +1090,12 @@ func TestAPIDispatch_ActionFiltering(t *testing.T) {
10901090func TestScheduledWorkflow (t * testing.T ) {
10911091 ctx := context .Background ()
10921092 te := newTestEnv (t )
1093- ctx , _ , gid := authenticate (t , ctx , te )
10941093 execClient := te .GetRemoteExecutionClient ().(* fakeExecutionClient )
10951094 te .SetRemoteExecutionClient (execClient )
10961095 provider := setupFakeGitProvider (t , te )
10971096 repoURL := makeTempRepo (t )
10981097 _ = runBBServer (ctx , t , te )
1098+ authCtx , _ , gid := authenticate (t , ctx , te )
10991099 createWorkflow (t , te , repoURL , gid , false )
11001100 provider .FileContents = map [string ]string {
11011101 config .FilePath : `
@@ -1119,7 +1119,6 @@ actions:
11191119 GroupID : gid ,
11201120 RepoURL : repoURL ,
11211121 ActionName : "Should Run" ,
1122- Branch : "main" ,
11231122 // Run every hour, on the hour.
11241123 CronExpr : "0 * * * *" ,
11251124 NextRunUsec : now .UnixMicro (),
@@ -1130,7 +1129,6 @@ actions:
11301129 GroupID : gid ,
11311130 RepoURL : repoURL ,
11321131 ActionName : "Should Run 2" ,
1133- Branch : "main" ,
11341132 // Run every hour, 5 minutes before the hour.
11351133 CronExpr : "55 * * * *" ,
11361134 NextRunUsec : fiveMinutesAgo .UnixMicro (),
@@ -1141,7 +1139,6 @@ actions:
11411139 GroupID : gid ,
11421140 RepoURL : repoURL ,
11431141 ActionName : "Not Time Yet" ,
1144- Branch : "main" ,
11451142 // Run every day at 7PM UTC.
11461143 CronExpr : "0 19 * * *" ,
11471144 NextRunUsec : sevenPM .UnixMicro (),
@@ -1152,7 +1149,6 @@ actions:
11521149 GroupID : gid ,
11531150 RepoURL : repoURL ,
11541151 ActionName : "Already Leased" ,
1155- Branch : "main" ,
11561152 // Run every hour, on the hour.
11571153 CronExpr : "0 * * * *" ,
11581154 NextRunUsec : now .UnixMicro (),
@@ -1164,22 +1160,23 @@ actions:
11641160 GroupID : gid ,
11651161 RepoURL : repoURL ,
11661162 ActionName : "Expired Lease" ,
1167- Branch : "main" ,
11681163 // Run every hour, on the hour.
11691164 CronExpr : "0 * * * *" ,
11701165 NextRunUsec : now .UnixMicro (),
11711166 LeaseExpiresUsec : now .Add (- 1 * time .Minute ).UnixMicro (),
11721167 })
11731168
1174- err := te .GetWorkflowService ().RunScheduledWorkflows (ctx )
1169+ // Intentionally use a non-authenticated context, like the cron scheduler would.
1170+ err := te .GetWorkflowService ().RunScheduledWorkflows (t .Context ())
11751171 require .NoError (t , err )
11761172
11771173 expectedActionNames := []string {"Should Run" , "Should Run 2" , "Expired Lease" }
11781174 executedActionNames := make ([]string , 0 , len (expectedActionNames ))
11791175 for range expectedActionNames {
11801176 select {
11811177 case execReq := <- execClient .executeRequests :
1182- actionName := getExecutedActionName (t , ctx , te , execReq .Payload )
1178+ // When fetching the executions, make sure to use the authenticated context.
1179+ actionName := getExecutedActionName (t , authCtx , te , execReq .Payload )
11831180 executedActionNames = append (executedActionNames , actionName )
11841181 case <- time .After (5 * time .Second ):
11851182 t .Fatal ("timed out waiting for scheduled workflow execution" )
@@ -1211,10 +1208,44 @@ actions:
12111208 require .Equal (t , int64 (0 ), notTimeYet .LeaseExpiresUsec )
12121209}
12131210
1211+ func TestScheduledWorkflow_NoEligibleSchedules (t * testing.T ) {
1212+ ctx := context .Background ()
1213+ te := newTestEnv (t )
1214+ _ , _ , gid := authenticate (t , ctx , te )
1215+ repoURL := makeTempRepo (t )
1216+ _ = runBBServer (ctx , t , te )
1217+ createWorkflow (t , te , repoURL , gid , false )
1218+
1219+ now := time .Date (2026 , 1 , 2 , 15 , 0 , 0 , 0 , time .UTC )
1220+ te .SetClock (clockwork .NewFakeClockAt (now ))
1221+
1222+ // All next run times are in the future.
1223+ insertScheduledRun (t , te , & tables.ScheduledRun {
1224+ ScheduleID : "future-1" ,
1225+ GroupID : gid ,
1226+ RepoURL : repoURL ,
1227+ ActionName : "Some Action" ,
1228+ CronExpr : "0 19 * * *" ,
1229+ NextRunUsec : now .Add (1 * time .Hour ).UnixMicro (),
1230+ })
1231+
1232+ // Intentionally use a non-authenticated context, like the cron scheduler would.
1233+ err := te .GetWorkflowService ().RunScheduledWorkflows (t .Context ())
1234+ require .NoError (t , err )
1235+
1236+ // No executions should have been started.
1237+ require .Zero (t , len (te .GetRemoteExecutionClient ().(* fakeExecutionClient ).executeRequests ))
1238+
1239+ // Schedule rows should be untouched.
1240+ future1 := getScheduledRun (t , te , "future-1" )
1241+ require .Equal (t , now .Add (1 * time .Hour ).UnixMicro (), future1 .NextRunUsec )
1242+ require .Equal (t , int64 (0 ), future1 .LeaseExpiresUsec )
1243+ }
1244+
12141245func TestScheduledWorkflow_ConcurrentServers (t * testing.T ) {
12151246 ctx := context .Background ()
12161247 te := newTestEnv (t )
1217- ctx , _ , gid := authenticate (t , ctx , te )
1248+ authCtx , _ , gid := authenticate (t , ctx , te )
12181249 execClient := te .GetRemoteExecutionClient ().(* fakeExecutionClient )
12191250 te .SetRemoteExecutionClient (execClient )
12201251 provider := setupFakeGitProvider (t , te )
@@ -1239,7 +1270,6 @@ actions:
12391270 GroupID : gid ,
12401271 RepoURL : repoURL ,
12411272 ActionName : "Test" ,
1242- Branch : "main" ,
12431273 // Run every hour, on the hour.
12441274 CronExpr : "0 * * * *" ,
12451275 NextRunUsec : now .UnixMicro (),
@@ -1250,7 +1280,6 @@ actions:
12501280 GroupID : gid ,
12511281 RepoURL : repoURL ,
12521282 ActionName : "Test 2" ,
1253- Branch : "main" ,
12541283 // Run every hour, on the hour.
12551284 CronExpr : "0 * * * *" ,
12561285 NextRunUsec : now .UnixMicro (),
@@ -1263,7 +1292,8 @@ actions:
12631292 wg .Add (1 )
12641293 go func () {
12651294 defer wg .Done ()
1266- errCh <- te .GetWorkflowService ().RunScheduledWorkflows (ctx )
1295+ // Intentionally use a non-authenticated context, like the cron scheduler would.
1296+ errCh <- te .GetWorkflowService ().RunScheduledWorkflows (t .Context ())
12671297 }()
12681298 }
12691299 wg .Wait ()
@@ -1277,7 +1307,8 @@ actions:
12771307 for range 2 {
12781308 select {
12791309 case execReq := <- execClient .executeRequests :
1280- actionName := getExecutedActionName (t , ctx , te , execReq .Payload )
1310+ // When fetching the executions, make sure to use the authenticated context.
1311+ actionName := getExecutedActionName (t , authCtx , te , execReq .Payload )
12811312 executedActionNames = append (executedActionNames , actionName )
12821313 case <- time .After (5 * time .Second ):
12831314 t .Fatal ("timed out waiting for scheduled workflow execution" )
0 commit comments