Make sure we prevent timeouts when in idle mode
This commit is contained in:
parent
9895f5197c
commit
451b52fde5
@ -82,7 +82,7 @@ def idle(context, subsystems=None):
|
||||
|
||||
active = context.subscriptions.intersection(context.events)
|
||||
if not active:
|
||||
context.session.connection.disable_timeout()
|
||||
context.session.prevent_timeout = True
|
||||
return
|
||||
|
||||
response = []
|
||||
@ -100,7 +100,7 @@ def noidle(context):
|
||||
return
|
||||
context.subscriptions = set()
|
||||
context.events = set()
|
||||
context.session.connection.enable_timeout()
|
||||
context.session.prevent_timeout = False
|
||||
|
||||
@handle_request(r'^stats$')
|
||||
def stats(context):
|
||||
|
||||
@ -286,6 +286,7 @@ class LineProtocol(ThreadingActor):
|
||||
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
self.prevent_timeout = False
|
||||
self.recv_buffer = ''
|
||||
|
||||
@property
|
||||
@ -316,7 +317,8 @@ class LineProtocol(ThreadingActor):
|
||||
line = self.decode(line)
|
||||
self.on_line_received(line)
|
||||
|
||||
self.connection.enable_timeout()
|
||||
if not self.prevent_timeout:
|
||||
self.connection.enable_timeout()
|
||||
|
||||
def on_stop(self):
|
||||
"""Ensure that cleanup when actor stops."""
|
||||
|
||||
@ -42,10 +42,6 @@ class IdleHandlerTest(protocol.BaseTestCase):
|
||||
self.assertNoEvents()
|
||||
self.assertNoResponse()
|
||||
|
||||
def test_noidle_does_not_call_enable_timeout(self):
|
||||
self.sendRequest(u'noidle')
|
||||
self.assertEqual(0, self.connection.enable_timeout.call_count)
|
||||
|
||||
def test_idle_player(self):
|
||||
self.sendRequest(u'idle player')
|
||||
self.assertEqualSubscriptions(['player'])
|
||||
|
||||
@ -11,11 +11,13 @@ class LineProtocolTest(unittest.TestCase):
|
||||
self.mock = Mock(spec=network.LineProtocol)
|
||||
self.mock.terminator = network.LineProtocol.terminator
|
||||
self.mock.encoding = network.LineProtocol.encoding
|
||||
self.mock.prevent_timeout = False
|
||||
|
||||
def test_init_stores_values_in_attributes(self):
|
||||
network.LineProtocol.__init__(self.mock, sentinel.connection)
|
||||
self.assertEqual(sentinel.connection, self.mock.connection)
|
||||
self.assertEqual('', self.mock.recv_buffer)
|
||||
self.assertFalse(self.mock.prevent_timeout)
|
||||
|
||||
def test_on_receive_no_new_lines_adds_to_recv_buffer(self):
|
||||
self.mock.connection = Mock(spec=network.Connection)
|
||||
@ -36,6 +38,16 @@ class LineProtocolTest(unittest.TestCase):
|
||||
self.mock.connection.disable_timeout.assert_called_once_with()
|
||||
self.mock.connection.enable_timeout.assert_called_once_with()
|
||||
|
||||
def test_on_receive_toggles_unless_prevent_timeout_is_set(self):
|
||||
self.mock.connection = Mock(spec=network.Connection)
|
||||
self.mock.recv_buffer = ''
|
||||
self.mock.parse_lines.return_value = []
|
||||
self.mock.prevent_timeout = True
|
||||
|
||||
network.LineProtocol.on_receive(self.mock, {'received': 'data'})
|
||||
self.mock.connection.disable_timeout.assert_called_once_with()
|
||||
self.assertEqual(0, self.mock.connection.enable_timeout.call_count)
|
||||
|
||||
def test_on_receive_no_new_lines_calls_parse_lines(self):
|
||||
self.mock.connection = Mock(spec=network.Connection)
|
||||
self.mock.recv_buffer = ''
|
||||
|
||||
Loading…
Reference in New Issue
Block a user