def test_dispatch(self, dispatcher): request = 'test' dispatcher.__iadd__ = Mock() plugin = Mock() builtin = Builtin(plugin) result = builtin.dispatch(request) builtin.dispatcher.dispatch.assert_called_once_with(request) self.assertEqual(result, builtin.dispatcher.dispatch.return_value)
def test_provides(self, dispatcher): name = 'test' dispatcher.__iadd__ = Mock() plugin = Mock() builtin = Builtin(plugin) p = builtin.provides(name) builtin.dispatcher.provides.assert_called_once_with(name) self.assertEqual(p, builtin.dispatcher.provides.return_value)
def __init__(self, plugin): """ :param plugin: A plugin. :type plugin: gofer.agent.plugin.Plugin """ Thread.__init__(self, name='scheduler:%s' % plugin.name) self.plugin = plugin self.pending = Pending(plugin.name) self.builtin = Builtin(plugin) self.setDaemon(True)
def test_dispatch(self): request = Document(routing=['A', 'B'], request={ 'classname': 'Admin', 'method': 'hello', 'args': [], 'keywords': {} }) plugin = Mock() builtin = Builtin(plugin) result = builtin.dispatch(request) self.assertEqual(result.retval, Admin().hello())
def test_dispatch(self): request = Document( routing=['A', 'B'], request={ 'classname': 'Admin', 'method': 'hello', 'args': [], 'keywords': {} }) plugin = Mock() builtin = Builtin(plugin) result = builtin.dispatch(request) self.assertEqual(result.retval, Admin().hello())
def test_properties(self): plugin = Mock(container=Mock(), authenticator=Mock(), url='http://1234') builtin = Builtin(plugin) self.assertEqual(builtin.url, builtin.url) self.assertEqual(builtin.authenticator, builtin.authenticator)
def test_properties(self, dispatcher): dispatcher.__iadd__ = Mock() plugin = Mock(container=Mock(), authenticator=Mock(), url='http://1234') builtin = Builtin(plugin) self.assertEqual(builtin.url, builtin.url) self.assertEqual(builtin.authenticator, builtin.authenticator)
def test_init(self, pool, dispatcher): dispatcher.__iadd__ = Mock() plugin = Mock(container=Mock()) builtin = Builtin(plugin) pool.assert_called_once_with(3) self.assertEqual(builtin.dispatcher, builtin._dispatcher.return_value) self.assertEqual(builtin.pool, pool.return_value) self.assertEqual(builtin.plugin, plugin)
def __init__(self, plugin): """ :param plugin: A plugin. :type plugin: gofer.agent.plugin.Plugin """ Thread.__init__(self, name="scheduler:%s" % plugin.name) self.plugin = plugin self.pending = Pending(plugin.name) self.builtin = Builtin(plugin) self.setDaemon(True)
def test_provides(self): name = 'Admin' plugin = Mock() builtin = Builtin(plugin) p = builtin.provides(name) self.assertTrue(p)
class Scheduler(Thread): """ The pending request scheduler. Processes the *pending* queue. """ def __init__(self, plugin): """ :param plugin: A plugin. :type plugin: gofer.agent.plugin.Plugin """ Thread.__init__(self, name="scheduler:%s" % plugin.name) self.plugin = plugin self.pending = Pending(plugin.name) self.builtin = Builtin(plugin) self.setDaemon(True) def run(self): """ Read the pending queue and dispatch requests to the plugin thread pool. """ while not Thread.aborted(): try: request = self.pending.get() except Empty: # aborted break try: plugin = self.select_plugin(request) transaction = Transaction(plugin, self.pending, request) task = Task(transaction) plugin.pool.run(task) except Exception: self.pending.commit(request.sn) log.exception(request.sn) def select_plugin(self, request): """ Select the plugin based on the request. :param request: A request to be scheduled. :rtype request: gofer.messaging.Document :return: The appropriate plugin. :rtype: gofer.agent.plugin.Plugin """ call = Document(request.request) if self.builtin.provides(call.classname): plugin = self.builtin else: plugin = self.plugin return plugin def add(self, request): """ Add a request to be scheduled. :param request: A request to be scheduled. :rtype request: gofer.messaging.Document """ self.pending.put(request) def shutdown(self): """ Shutdown the scheduler. """ self.builtin.shutdown() self.abort()
def test_shutdown(self, pool): plugin = Mock(container=Mock()) builtin = Builtin(plugin) builtin.shutdown() pool.return_value.shutdown.assert_called_once_with()
class Scheduler(Thread): """ The pending request scheduler. Processes the *pending* queue. """ def __init__(self, plugin): """ :param plugin: A plugin. :type plugin: gofer.agent.plugin.Plugin """ Thread.__init__(self, name='scheduler:%s' % plugin.name) self.plugin = plugin self.pending = Pending(plugin.name) self.builtin = Builtin(plugin) self.setDaemon(True) def run(self): """ Read the pending queue and dispatch requests to the plugin thread pool. """ self.builtin.start() while not Thread.aborted(): try: request = self.pending.get() except Empty: # aborted break try: plugin = self.select_plugin(request) transaction = Transaction(plugin, self.pending, request) task = Task(transaction) plugin.pool.run(task) except Exception: self.pending.commit(request.sn) log.exception(request.sn) def select_plugin(self, request): """ Select the plugin based on the request. :param request: A request to be scheduled. :rtype request: gofer.messaging.Document :return: The appropriate plugin. :rtype: gofer.agent.plugin.Plugin """ call = Document(request.request) if self.builtin.provides(call.classname): plugin = self.builtin else: plugin = self.plugin return plugin def add(self, request): """ Add a request to be scheduled. :param request: A request to be scheduled. :rtype request: gofer.messaging.Document """ self.pending.put(request) def shutdown(self): """ Shutdown the scheduler. """ self.builtin.shutdown() self.abort()
def test__dispatcher(self): dispatcher = Builtin._dispatcher() self.assertTrue(Admin.__name__ in dispatcher.catalog)