Skip to content

Commit

Permalink
Fixed send_command function as per suggestion from @alabou
Browse files Browse the repository at this point in the history
(cherry picked from commit a3df644)
  • Loading branch information
jonathan-r-thorpe committed Oct 6, 2023
1 parent ab07908 commit 049d674
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
84 changes: 46 additions & 38 deletions nmostesting/IS12Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def __init__(self, url, spec_path, spec_branch):
self.ROOT_BLOCK_OID = 1
self.ncp_websocket = None
self.command_handle = 0
self.expect_notifications = False
self.notifications = []

def load_is12_schemas(self, spec_path):
Expand Down Expand Up @@ -230,47 +231,50 @@ def send_command(self, test, command_json):

self.ncp_websocket.send(json.dumps(command_json))

# Wait for server to respond
results = []
start_time = time.time()
while time.time() < start_time + WS_MESSAGE_TIMEOUT:
if self.ncp_websocket.is_messages_received():
if not self.ncp_websocket.is_messages_received():
time.sleep(0.2)
continue

# find the response to our request
for message in self.ncp_websocket.get_messages():
parsed_message = json.loads(message)

if self.message_type_to_schema_name(parsed_message.get("messageType")):
self.validate_is12_schema(
test,
parsed_message,
self.message_type_to_schema_name(parsed_message["messageType"]),
context=self.message_type_to_schema_name(parsed_message["messageType"]) + ": ")
else:
raise NMOSTestException(test.FAIL("Unrecognised message type: " + parsed_message.get("messageType"),
"https://specs.amwa.tv/is-12/branches/{}"
"/docs/Protocol_messaging.html#command-message-type"
.format(self.spec_branch)))

if parsed_message["messageType"] == MessageTypes.CommandResponse:
responses = parsed_message["responses"]
for response in responses:
if response["handle"] == command_handle:
if response["result"]["status"] != NcMethodStatus.OK:
raise NMOSTestException(test.FAIL(response["result"]))
results.append(response)
if parsed_message["messageType"] == MessageTypes.SubscriptionResponse:
results.append(parsed_message["subscriptions"])
if parsed_message["messageType"] == MessageTypes.Notification:
self.notifications += parsed_message["notifications"]
if parsed_message["messageType"] == MessageTypes.Error:
raise NMOSTestException(test.FAIL(parsed_message, "https://specs.amwa.tv/is-12/branches/{}"
"/docs/Protocol_messaging.html#error-messages"
.format(self.spec_branch)))

if not self.expect_notifications and len(results) != 0:
break
if self.expect_notifications and len(results) != 0 and len(self.notifications) != 0:
break
time.sleep(0.2)

messages = self.ncp_websocket.get_messages()

results = []
# find the response to our request
for message in messages:
parsed_message = json.loads(message)

if self.message_type_to_schema_name(parsed_message["messageType"]):
self.validate_is12_schema(
test,
parsed_message,
self.message_type_to_schema_name(parsed_message["messageType"]),
context=self.message_type_to_schema_name(parsed_message["messageType"]) + ": ")
else:
raise NMOSTestException(test.FAIL("Unrecognised message type: " + parsed_message["messageType"],
"https://specs.amwa.tv/is-12/branches/{}"
"/docs/Protocol_messaging.html#command-message-type"
.format(self.spec_branch)))

if parsed_message["messageType"] == MessageTypes.CommandResponse:
responses = parsed_message["responses"]
for response in responses:
if response["handle"] == command_handle:
if response["result"]["status"] != NcMethodStatus.OK:
raise NMOSTestException(test.FAIL(response["result"]))
results.append(response)
if parsed_message["messageType"] == MessageTypes.SubscriptionResponse:
results.append(parsed_message["subscriptions"])
if parsed_message["messageType"] == MessageTypes.Notification:
self.notifications += parsed_message["notifications"]
if parsed_message["messageType"] == MessageTypes.Error:
raise NMOSTestException(test.FAIL(parsed_message, "https://specs.amwa.tv/is-12/branches/{}"
"/docs/Protocol_messaging.html#error-messages"
.format(self.spec_branch)))
if len(results) == 0:
raise NMOSTestException(test.FAIL("No Message Response received.",
"https://specs.amwa.tv/is-12/branches/{}"
Expand All @@ -285,9 +289,13 @@ def send_command(self, test, command_json):
def get_notifications(self):
return self.notifications

def reset_notifications(self):
def start_logging_notifications(self):
self.expect_notifications = True
self.notifications = []

def stop_logging_notifications(self):
self.expect_notifications = False

def create_command_JSON(self, oid, method_id, arguments):
"""for sending over websocket"""
self.command_handle += 1
Expand Down
3 changes: 2 additions & 1 deletion nmostesting/suites/IS1201Test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,8 +1385,9 @@ def test_35(self, test):
context = "oid: " + str(oid) + ", "

for label in [new_user_label, old_user_label]:
self.is12_utils.reset_notifications()
self.is12_utils.start_logging_notifications()
self.is12_utils.set_property(test, oid, NcObjectProperties.USER_LABEL.value, label)
self.is12_utils.stop_logging_notifications()

if len(self.is12_utils.get_notifications()) == 0:
error = True
Expand Down

0 comments on commit 049d674

Please sign in to comment.