def test_send_message_too_large(self): # Send a message that exceeds the maximum message size. The connection # should be closed. transport = MockTransport() protocol = DbusProtocol(True, self.store_messages, 'foo') transport.start(protocol) protocol.data_received(b'\0AUTH ANONYMOUS\r\nBEGIN\r\n') message = txdbus.MethodCallMessage('/org/freedesktop/DBus', 'Hello', interface='org.freedesktop.DBus', destination='org.freedesktop.DBus') protocol.data_received(message.rawMessage) gruvi.sleep(0) self.assertTrue(protocol._name_acquired) # Send a signal with a size equal to the high-water mark. This should work. message = txdbus.SignalMessage('/my/path', 'Signal', 'my.iface', signature='s', body=['x'*100]) msglen = len(message.rawMessage) self.assertGreater(msglen, 100) protocol.set_read_buffer_limits(msglen) protocol.data_received(message.rawMessage) gruvi.sleep(0) self.assertIsNone(protocol._error) self.assertFalse(transport.closed) self.assertEqual(len(self.messages), 1) # Now send a signal with a size larger than the high-water mark. This should fail. message = txdbus.SignalMessage('/my/path', 'Signal', 'my.iface', signature='s', body=['x'*100]) msglen = len(message.rawMessage) protocol.set_read_buffer_limits(msglen-1) protocol.data_received(message.rawMessage) gruvi.sleep(0) self.assertIsInstance(protocol._error, DbusError) self.assertTrue(transport.closed) self.assertEqual(len(self.messages), 1)
def test_read_write_flow_control(self): # Send a lot of messages filling up the protocol read buffer. transport = MockTransport() protocol = DbusProtocol(True, self.store_and_echo_messages) transport.start(protocol) protocol.data_received(b'\0AUTH ANONYMOUS\r\nBEGIN\r\n') auth = protocol._authenticator self.assertTrue(auth.authenticationSucceeded()) message = txdbus.MethodCallMessage('/org/freedesktop/DBus', 'Hello', interface='org.freedesktop.DBus', destination='org.freedesktop.DBus') protocol.data_received(message.rawMessage) gruvi.sleep(0) self.assertTrue(protocol._name_acquired) interrupted = 0 message = txdbus.SignalMessage('/my/path', 'Signal', 'my.iface', signature='s', body=['x'*100]) msglen = len(message.rawMessage) protocol.set_read_buffer_limits(10*msglen) transport.buffer.seek(0) transport.buffer.truncate() transport.set_write_buffer_limits(7*msglen) for i in range(100): # Fill up protocol read buffer message = txdbus.SignalMessage('/my/path', 'Signal', 'my.iface', signature='s', body=['x'*100]) protocol.data_received(message.rawMessage) if protocol._reading: continue interrupted += 1 self.assertGreater(protocol._queue.qsize(), 0) # Run the dispatcher to fill up the transport write buffer gruvi.sleep(0) # Now the write buffer is full and the read buffer still contains # some entries because it is larger. self.assertTrue(protocol._reading) self.assertGreater(protocol._queue.qsize(), 0) self.assertFalse(protocol._may_write.is_set()) # Drain write buffer and resume writing transport.buffer.seek(0) transport.buffer.truncate() protocol.resume_writing() # Should be interrupted > 10 times. The write buffer is the limiting factor # not the read buffer. self.assertGreater(interrupted, 10)