Browse Source

[1924] Decode the delivered messages in test

Decode the messages to see they are not errors. Check they contain
correct data and they are delivered to the correct socket.

Parsing of the errors is not present yet, it needs to be found out how
such an error looks like.
Michal 'vorner' Vaner 12 years ago
parent
commit
85b7b8462a
1 changed files with 18 additions and 2 deletions
  1. 18 2
      src/bin/msgq/tests/msgq_test.py

+ 18 - 2
src/bin/msgq/tests/msgq_test.py

@@ -11,6 +11,8 @@ import threading
 import isc.cc
 import collections
 import isc.log
+import struct
+import json
 
 #
 # Currently only the subscription part and some sending is implemented...
@@ -144,6 +146,18 @@ class MsgQTest(unittest.TestCase):
     def setUp(self):
         self.__msgq = MsgQ()
 
+    def parse_msg(self, msg):
+        """
+        Parse a binary representation of message to the routing header and the
+        data payload. It assumes the message is correctly encoded and the
+        payload is not omitted. It'd probably throw in other cases, but we
+        don't use it in such situations in this test.
+        """
+        (length, header_len) = struct.unpack('>IH', msg[:6])
+        header = json.loads(msg[6:6 + header_len].decode('utf-8'))
+        data = json.loads(msg[6 + header_len:].decode('utf-8'))
+        return (header, data)
+
     def test_undeliverable_errors(self):
         sent_messages = []
         def fake_end_prepared_msg(socket, msg):
@@ -190,7 +204,8 @@ class MsgQTest(unittest.TestCase):
         self.__msgq.subs.find = lambda group, instance: [recipient]
         self.__msgq.process_command_send(sender, routing, data)
         self.assertEqual(1, len(sent_messages))
-        # TODO: Parse the message to see it's the sent message.
+        self.assertEqual(2, sent_messages[0][0]) # The recipient
+        self.assertEqual((routing, data), self.parse_msg(sent_messages[0][1]))
         sent_messages = []
         # When we send a direct message and the recipient is not there, we get
         # the error too
@@ -203,7 +218,8 @@ class MsgQTest(unittest.TestCase):
         self.__msgq.lnames["lname"] = recipient
         self.__msgq.process_command_send(sender, routing, data)
         self.assertEqual(1, len(sent_messages))
-        # TODO: Parse the message and see it's not an error.
+        self.assertEqual(2, sent_messages[0][0]) # The recipient
+        self.assertEqual((routing, data), self.parse_msg(sent_messages[0][1]))
         sent_messages = []
 
 class DummySocket: