MatrixSynapse/tests/appservice/test_scheduler.py

213 lines
7.9 KiB
Python
Raw Normal View History

2015-03-06 12:50:27 +01:00
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from synapse.appservice import ApplicationServiceState, AppServiceTransaction
2015-03-06 12:50:27 +01:00
from synapse.appservice.scheduler import (
_ServiceQueuer, _TransactionController, _Recoverer
2015-03-06 12:50:27 +01:00
)
from twisted.internet import defer
from ..utils import MockClock
from mock import Mock
from tests import unittest
2015-03-06 15:36:52 +01:00
class ApplicationServiceSchedulerTransactionCtrlTestCase(unittest.TestCase):
def setUp(self):
self.clock = MockClock()
self.store = Mock()
self.as_api = Mock()
self.recoverer = Mock()
self.recoverer_fn = Mock(return_value=self.recoverer)
self.txnctrl = _TransactionController(
clock=self.clock, store=self.store, as_api=self.as_api,
recoverer_fn=self.recoverer_fn
)
def test_single_service_up_txn_sent(self):
# Test: The AS is up and the txn is successfully sent.
service = Mock()
events = [Mock(), Mock()]
txn_id = "foobar"
txn = Mock(id=txn_id, service=service, events=events)
# mock methods
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.UP)
)
txn.send = Mock(return_value=defer.succeed(True))
self.store.create_appservice_txn = Mock(
return_value=defer.succeed(txn)
)
# actual call
self.txnctrl.send(service, events)
self.store.create_appservice_txn.assert_called_once_with(
service=service, events=events # txn made and saved
)
self.assertEquals(0, len(self.txnctrl.recoverers)) # no recoverer made
txn.complete.assert_called_once_with(self.store) # txn completed
def test_single_service_down(self):
# Test: The AS is down so it shouldn't push; Recoverers will do it.
# It should still make a transaction though.
service = Mock()
events = [Mock(), Mock()]
txn = Mock(id="idhere", service=service, events=events)
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.DOWN)
)
self.store.create_appservice_txn = Mock(
return_value=defer.succeed(txn)
)
# actual call
self.txnctrl.send(service, events)
self.store.create_appservice_txn.assert_called_once_with(
service=service, events=events # txn made and saved
)
self.assertEquals(0, txn.send.call_count) # txn not sent though
self.assertEquals(0, txn.complete.call_count) # or completed
def test_single_service_up_txn_not_sent(self):
# Test: The AS is up and the txn is not sent. A Recoverer is made and
# started.
service = Mock()
events = [Mock(), Mock()]
txn_id = "foobar"
txn = Mock(id=txn_id, service=service, events=events)
# mock methods
self.store.get_appservice_state = Mock(
return_value=defer.succeed(ApplicationServiceState.UP)
)
self.store.set_appservice_state = Mock(return_value=defer.succeed(True))
txn.send = Mock(return_value=defer.succeed(False)) # fails to send
self.store.create_appservice_txn = Mock(
return_value=defer.succeed(txn)
)
# actual call
self.txnctrl.send(service, events)
self.store.create_appservice_txn.assert_called_once_with(
service=service, events=events
)
self.assertEquals(1, self.recoverer_fn.call_count) # recoverer made
self.assertEquals(1, self.recoverer.recover.call_count) # and invoked
self.assertEquals(1, len(self.txnctrl.recoverers)) # and stored
self.assertEquals(0, txn.complete.call_count) # txn not completed
self.store.set_appservice_state.assert_called_once_with(
service, ApplicationServiceState.DOWN # service marked as down
)
2015-03-06 12:50:27 +01:00
class ApplicationServiceSchedulerRecovererTestCase(unittest.TestCase):
def setUp(self):
self.clock = MockClock()
self.as_api = Mock()
self.store = Mock()
self.service = Mock()
self.callback = Mock()
self.recoverer = _Recoverer(
clock=self.clock,
as_api=self.as_api,
store=self.store,
service=self.service,
callback=self.callback,
)
2015-03-06 15:36:52 +01:00
def test_recover_single_txn(self):
txn = Mock()
# return one txn to send, then no more old txns
txns = [txn, None]
def take_txn(*args, **kwargs):
return defer.succeed(txns.pop(0))
2015-03-06 17:17:01 +01:00
self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
2015-03-06 12:50:27 +01:00
self.recoverer.recover()
2015-03-06 15:36:52 +01:00
# shouldn't have called anything prior to waiting for exp backoff
2015-03-06 17:17:01 +01:00
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
2015-03-06 15:36:52 +01:00
txn.send = Mock(return_value=True)
# wait for exp backoff
self.clock.advance_time(2)
2015-03-06 15:36:52 +01:00
self.assertEquals(1, txn.send.call_count)
self.assertEquals(1, txn.complete.call_count)
# 2 because it needs to get None to know there are no more txns
2015-03-06 17:17:01 +01:00
self.assertEquals(2, self.store.get_oldest_unsent_txn.call_count)
2015-03-06 16:12:24 +01:00
self.callback.assert_called_once_with(self.recoverer)
self.assertEquals(self.recoverer.service, self.service)
2015-03-06 12:50:27 +01:00
2015-03-06 15:36:52 +01:00
def test_recover_retry_txn(self):
txn = Mock()
txns = [txn, None]
pop_txn = False
2015-03-06 12:50:27 +01:00
2015-03-06 15:36:52 +01:00
def take_txn(*args, **kwargs):
if pop_txn:
return defer.succeed(txns.pop(0))
else:
return defer.succeed(txn)
2015-03-06 17:17:01 +01:00
self.store.get_oldest_unsent_txn = Mock(side_effect=take_txn)
2015-03-06 12:50:27 +01:00
2015-03-06 15:36:52 +01:00
self.recoverer.recover()
2015-03-06 17:17:01 +01:00
self.assertEquals(0, self.store.get_oldest_unsent_txn.call_count)
2015-03-06 15:36:52 +01:00
txn.send = Mock(return_value=False)
self.clock.advance_time(2)
2015-03-06 15:36:52 +01:00
self.assertEquals(1, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count)
self.assertEquals(0, self.callback.call_count)
self.clock.advance_time(4)
2015-03-06 15:36:52 +01:00
self.assertEquals(2, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count)
self.assertEquals(0, self.callback.call_count)
self.clock.advance_time(8)
2015-03-06 15:36:52 +01:00
self.assertEquals(3, txn.send.call_count)
self.assertEquals(0, txn.complete.call_count)
self.assertEquals(0, self.callback.call_count)
txn.send = Mock(return_value=True) # successfully send the txn
pop_txn = True # returns the txn the first time, then no more.
self.clock.advance_time(16)
2015-03-06 15:36:52 +01:00
self.assertEquals(1, txn.send.call_count) # new mock reset call count
self.assertEquals(1, txn.complete.call_count)
2015-03-06 16:12:24 +01:00
self.callback.assert_called_once_with(self.recoverer)
2015-03-06 12:50:27 +01:00
class ApplicationServiceSchedulerQueuerTestCase(unittest.TestCase):
2015-03-06 12:50:27 +01:00
def setUp(self):
self.txn_ctrl = Mock()
self.queuer = _ServiceQueuer(self.txn_ctrl)
def test_send_single_event_no_queue(self):
# Expect the event to be sent immediately.
pass
def test_send_single_event_with_queue(self):
# - Send an event and don't resolve it just yet.
# - Send another event: expect send() to NOT be called.
# - Resolve the send event
# - Expect queued event to be sent
pass
def test_multiple_service_queues(self):
# Tests that each service has its own queue, and that they don't block
# on each other.
pass