|
| 1 | +"""Test the status acquiring appears when a SMS message is received and before the data is ready""" |
| 2 | + |
| 3 | +import time |
| 4 | +import json |
| 5 | +import tests.utils.db as db_utils |
| 6 | + |
| 7 | + |
| 8 | +class TestSMSQueues: |
| 9 | + instrument = "arcs" |
| 10 | + IPTS = "IPTS-11111" |
| 11 | + user = "InstrumentScientist" |
| 12 | + pwd = "InstrumentScientist" |
| 13 | + |
| 14 | + def create_and_send_msg(self, conn, run_number): |
| 15 | + conn.send( |
| 16 | + f"/topic/SNS.{self.instrument.upper()}.APP.SMS", |
| 17 | + json.dumps( |
| 18 | + { |
| 19 | + "instrument": self.instrument, |
| 20 | + "facility": "SNS", |
| 21 | + "ipts": self.IPTS, |
| 22 | + "run_number": run_number, |
| 23 | + "data_file": "", |
| 24 | + "reason": "SMS run started", |
| 25 | + "msg_type": "0", |
| 26 | + } |
| 27 | + ), |
| 28 | + ) |
| 29 | + |
| 30 | + def clear_run(self, conn, run_number): |
| 31 | + # remove everything for this run |
| 32 | + cursor = conn.cursor() |
| 33 | + cursor.execute("SELECT id FROM report_instrument where name = %s;", (self.instrument,)) |
| 34 | + inst_id = cursor.fetchone() |
| 35 | + if inst_id is None: |
| 36 | + return |
| 37 | + |
| 38 | + cursor.execute( |
| 39 | + "SELECT id FROM report_datarun WHERE instrument_id_id = %s AND run_number = %s;", (inst_id, run_number) |
| 40 | + ) |
| 41 | + run_id = cursor.fetchone() |
| 42 | + if run_id is None: |
| 43 | + return |
| 44 | + |
| 45 | + db_utils.clear_previous_runstatus(conn, run_id) |
| 46 | + cursor.execute("DELETE FROM report_workflowsummary WHERE run_id_id = %s;", run_id) |
| 47 | + cursor.execute("DELETE FROM report_instrumentstatus WHERE last_run_id_id = %s;", run_id) |
| 48 | + cursor.execute("DELETE FROM report_datarun WHERE id = %s;", (run_id)) |
| 49 | + conn.commit() |
| 50 | + cursor.close() |
| 51 | + |
| 52 | + def test_acquiring(self, amq_connection, db_connection, request_page): |
| 53 | + # remove data run so the tests always starts fresh |
| 54 | + self.clear_run(db_connection, 100) |
| 55 | + self.clear_run(db_connection, 101) |
| 56 | + # send SMS message for 2 runs |
| 57 | + self.create_and_send_msg(amq_connection, 100) |
| 58 | + self.create_and_send_msg(amq_connection, 101) |
| 59 | + |
| 60 | + # wait a second while things run |
| 61 | + time.sleep(1) |
| 62 | + |
| 63 | + # check IPTS page /report/arcs/experiment/IPTS-11111/ for acquiring |
| 64 | + response = request_page("/report/arcs/experiment/IPTS-11111/", self.user, self.pwd) |
| 65 | + assert response.status_code == 200 |
| 66 | + assert response.text.count("acquiring") == 2 |
| 67 | + |
| 68 | + # now send data_ready for run 100 only and check that it is no longer acquiring |
| 69 | + self.create_and_send_msg(amq_connection, 100) |
| 70 | + |
| 71 | + amq_connection.send( |
| 72 | + "/queue/POSTPROCESS.DATA_READY", |
| 73 | + json.dumps( |
| 74 | + { |
| 75 | + "instrument": self.instrument, |
| 76 | + "facility": "SNS", |
| 77 | + "ipts": self.IPTS, |
| 78 | + "run_number": 100, |
| 79 | + "data_file": "", |
| 80 | + } |
| 81 | + ), |
| 82 | + ) |
| 83 | + time.sleep(1) |
| 84 | + |
| 85 | + # check IPTS page /report/arcs/experiment/IPTS-11111/ for acquiring |
| 86 | + response = request_page("/report/arcs/experiment/IPTS-11111/", self.user, self.pwd) |
| 87 | + assert response.status_code == 200 |
| 88 | + assert response.text.count("acquiring") == 1 # 101 is still acquiring but not 100 |
0 commit comments