22
33from datetime import datetime , timedelta , timezone
44from http import HTTPStatus
5+ from time import sleep
56
67import pytest
78from fastapi .testclient import TestClient
@@ -394,7 +395,7 @@ def test_insert_and_reschedule(normal_user_client: TestClient):
394395 }
395396
396397
397- ## test edge case for rescheduling
398+ # test edge case for rescheduling
398399
399400
400401def test_reschedule_job_attr_update (normal_user_client : TestClient ):
@@ -1084,9 +1085,7 @@ def test_heartbeat(normal_user_client: TestClient, valid_job_id: int):
10841085 assert old_data ["HeartBeatTime" ] is None
10851086
10861087 payload = {valid_job_id : {"Vsize" : 1234 }}
1087- r = normal_user_client .patch (
1088- "/api/jobs/heartbeat" , json = payload , params = {"force" : True }
1089- )
1088+ r = normal_user_client .patch ("/api/jobs/heartbeat" , json = payload )
10901089 r .raise_for_status ()
10911090
10921091 r = normal_user_client .post ("/api/jobs/search" , json = search_body )
@@ -1097,4 +1096,43 @@ def test_heartbeat(normal_user_client: TestClient, valid_job_id: int):
10971096 # TODO: This should be timezone aware
10981097 assert hbt .tzinfo is None
10991098 hbt = hbt .replace (tzinfo = timezone .utc )
1100- assert hbt > datetime .now (tz = timezone .utc ) - timedelta (seconds = 10 )
1099+ assert hbt >= datetime .now (tz = timezone .utc ) - timedelta (seconds = 15 )
1100+
1101+ # Kill the job by setting the status on it
1102+ r = normal_user_client .patch (
1103+ "/api/jobs/status" ,
1104+ json = {
1105+ valid_job_id : {
1106+ str (datetime .now (timezone .utc )): {
1107+ "Status" : JobStatus .KILLED ,
1108+ "MinorStatus" : "Marked for termination" ,
1109+ }
1110+ }
1111+ },
1112+ )
1113+ r .raise_for_status ()
1114+
1115+ sleep (1 )
1116+ # Send another heartbeat and check that a Kill job command was set
1117+ payload = {valid_job_id : {"Vsize" : 1235 }}
1118+ r = normal_user_client .patch ("/api/jobs/heartbeat" , json = payload )
1119+ r .raise_for_status ()
1120+
1121+ commands = r .json ()
1122+ assert len (commands ) == 1 , "Exactly one job command should be returned"
1123+ assert commands [0 ]["job_id" ] == valid_job_id , (
1124+ "Wrong job id," f" should be '{ valid_job_id } ' but got { commands [0 ]['job_id' ]= } "
1125+ )
1126+ assert commands [0 ]["command" ] == "Kill" , (
1127+ "Wrong job command received," f" should be 'Kill' but got { commands [0 ]= } "
1128+ )
1129+ sleep (1 )
1130+
1131+ # Send another heartbeat and check the job commands are empty
1132+ payload = {valid_job_id : {"Vsize" : 1234 }}
1133+ r = normal_user_client .patch ("/api/jobs/heartbeat" , json = payload )
1134+ r .raise_for_status ()
1135+ commands = r .json ()
1136+ assert (
1137+ len (commands ) == 0
1138+ ), "Exactly zero job commands should be returned after heartbeat commands are sent"
0 commit comments