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)
 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)
 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)
 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)
 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"'])
 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"'])
 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"'])
 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"'])
 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)
 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)
 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)
 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)
 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)
 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)