Ejemplo n.º 1
0
 def test_expected_messages_read_from_log_are_handled(self):
     # When no expected message is provided, then next message is read from
     # the frame log and used instead.
     EXPECTED = {'op': 'something', 'request_id': 42}
     MESSAGE = json.dumps(EXPECTED)
     written = []
     log = FauxLogger()
     frames = iter([
         Frame(EXPECTED, 'to server'),
         Frame('next 1', 'to client'),
         Frame('next 2', 'to client')])
     handle_message(MESSAGE, frames, written.append, log=log)
     # The next messages to the client were sent.
     self.assertEqual(written, ['"next 1"', '"next 2"'])
Ejemplo n.º 2
0
 def test_expected_messages_are_handled(self):
     # When the message received from the client was expected, and there
     # are one or more messages that must then be sent to the client, then
     # we send them.
     MESSAGE = {'op': 'something', 'request_id': 42}
     written = []
     log = FauxLogger()
     frames = iter([
         Frame('next 1', 'to client'),
         Frame('next 2', 'to client')])
     handle_message(json.dumps(MESSAGE), frames, written.append,
                    expected=Frame(MESSAGE, 'to client'), log=log)
     # The next messages to the client were sent.
     self.assertEqual(written, ['"next 1"', '"next 2"'])
Ejemplo n.º 3
0
 def test_returning_next_expected_message(self):
     # When the frames include expected messages from the client after
     # messages from the server, the next expected message is returned.
     EXPECTED = {'op': 'something', 'request_id': 42}
     MESSAGE = json.dumps(EXPECTED)
     NEXT_EXPECTED = Frame('next expected', 'to server')
     written = []
     log = FauxLogger()
     frames = iter([
         Frame(EXPECTED, 'to server'),
         Frame('next 1', 'to client'),
         Frame('next 2', 'to client'), NEXT_EXPECTED
     ])
     result = handle_message(MESSAGE, frames, written.append, log=log)
     self.assertEqual(result, NEXT_EXPECTED)
Ejemplo n.º 4
0
 def test_sent_messages_are_logged(self):
     # Each message received is logged.
     EXPECTED = Frame({'op': 'something', 'request_id': 42}, 'to client')
     MESSAGE = json.dumps(EXPECTED.message)
     TO_CLIENT_1 = 'next 1'
     TO_CLIENT_2 = 'next 2'
     frames = iter([
         Frame(TO_CLIENT_1, 'to client'),
         Frame(TO_CLIENT_2, 'to client')])
     written = []
     log = FauxLogger()
     handle_message(MESSAGE, frames, written.append, expected=EXPECTED,
                    log=log)
     self.assertIn(('sent', (TO_CLIENT_1,)), log.logged)
     self.assertIn(('sent', (TO_CLIENT_2,)), log.logged)
Ejemplo n.º 5
0
 def test_included_direction_is_used(self):
     # If the log contains direction info (whether or not the message was
     # sent from the client and to the server or vice versa) that info will
     # be used.
     log = """\
         junk
         to mars
         {"foo": 1}
         junk
         to venus
         {"foo": 2}
         junk""".split('\n')
     self.assertEqual([
         Frame(message={u'foo': 1}, direction='to mars'),
         Frame(message={u'foo': 2}, direction='to venus')
     ], list(read_frames(log)))
Ejemplo n.º 6
0
 def test_missing_direction_is_infered(self):
     # If the log does not contain direction info, the directionality of
     # each message will deduced from the message's contents.
     log = """\
         junk
         {"foo": 1, "request_id": 1}
         junk
         {"foo": 2}
         junk""".split('\n')
     self.assertEqual([
         Frame(message={
             u'foo': 1,
             u'request_id': 1
         }, direction='to server'),
         Frame(message={u'foo': 2}, direction='to client')
     ], list(read_frames(log)))
Ejemplo n.º 7
0
 def test_reading_frames(self):
     # Frames interspersed with junk are read correctly.
     log = """\
         this is junk
         {"foo": 1, "bar": 2, "request_id": 42}
         more junk
         {"response": "hi", "request_id": 42}
         the last junk""".split('\n')
     self.assertEqual(
         [Frame(
             message={u'foo': 1, u'bar': 2, u'request_id': 42},
             direction='to server'),
          Frame(
             message={u'response': u'hi', u'request_id': 42},
             direction='to client')],
         list(read_frames(log)))
Ejemplo n.º 8
0
 def test_received_messages_are_logged(self):
     # Each message received is logged.
     EXPECTED = Frame({'op': 'something', 'request_id': 42}, 'to client')
     MESSAGE = json.dumps(EXPECTED.message)
     written = []
     log = FauxLogger()
     handle_message(MESSAGE, iter([]), written.append, expected=EXPECTED,
                    log=log)
     self.assertIn(('received', (MESSAGE,)), log.logged)
Ejemplo n.º 9
0
 def test_unexpected_message_comes_in(self):
     # If something other than the expected message comes in, a SystemExit
     # exception is raised and details of the messages are logged.
     MESSAGE = {'op': 'something', 'request_id': 42}
     UNEXPECTED = json.dumps(MESSAGE)
     EXPECTED = {'op': 'something else', 'request_id': 'nope'}
     written = []
     log = FauxLogger()
     frames = iter([
         Frame(EXPECTED, 'to server'),
         Frame('next 1', 'to client'),
         Frame('next 2', 'to client')])
     # The program will exit.
     with self.assertRaises(SystemExit):
         handle_message(UNEXPECTED, frames, written.append, log=log)
     # Debugging output is displayed.
     self.assertIn(('error', ('mismatched messages:',)), log.logged)
     self.assertIn(('error', ('\tgot', MESSAGE)), log.logged)
     self.assertIn(('error', ('\texpected', EXPECTED)), log.logged)
Ejemplo n.º 10
0
 def test_out_of_frames_message_is_logged(self):
     # When the end of the log is reached, a message is displayed saying
     # so.
     EXPECTED = Frame({'op': 'something', 'request_id': 42}, 'to client')
     MESSAGE = json.dumps(EXPECTED.message)
     frames = iter([])
     written = []
     log = FauxLogger()
     handle_message(MESSAGE, frames, written.append, expected=EXPECTED,
                    log=log)
     self.assertIn(
         ('message', ('reached end of log, ignoring incoming frames',)),
         log.logged)