def test_make_worker_with_threadpool_size(self): """ The reactor threadpool can be resized with a command line option. """ from twisted.internet import reactor old_maxthreads = reactor.getThreadPool().max self.add_cleanup(reactor.suggestThreadPoolSize, old_maxthreads) # Explicitly set the threadpool size to something different from the # value we're testing with. reactor.suggestThreadPoolSize(5) self.mk_config_file('worker', ["transport_name: sphex"]) maker = VumiWorkerServiceMaker() # By default, we don't touch the threadpool. options = StartWorkerOptions() options.parseOptions([ '--worker-class', 'vumi.demos.words.EchoWorker', '--config', self.config_file['worker'], ]) worker = maker.makeService(options) self.assertEqual({'transport_name': 'sphex'}, worker.config) self.assertEqual(reactor.getThreadPool().max, 5) # If asked, we set the threadpool's maximum size. options_mt = StartWorkerOptions() options_mt.parseOptions([ '--worker-class', 'vumi.demos.words.EchoWorker', '--config', self.config_file['worker'], '--maxthreads', '2', ]) worker = maker.makeService(options_mt) self.assertEqual({'transport_name': 'sphex'}, worker.config) self.assertEqual(reactor.getThreadPool().max, 2)
def test_config_file_with_include(self): # Create include file self.mk_config_file('provider_prefixes', ['"+27606": "vodacom"', '"+27603": "mtn"']) # Create root file self.mk_config_file('worker', [ 'transport_name: foo', 'middleware:', ' - provider_inbound_mw: vumi.provider.foo', 'provider_inbound_mw:' ]) # Find rel path between include and root files rel_path_providers = os.path.relpath( self.config_file['provider_prefixes'], os.path.dirname(self.config_file['worker'])) # Add rel path to root file with open(self.config_file['worker'], 'a') as f: f.write(' !include %s\n' % rel_path_providers) # Process config files options = StartWorkerOptions() options.parseOptions([ '--worker-class', 'foo.fooWorker', '--config', self.config_file['worker'] ]) self.assertEqual(options.worker_config['provider_inbound_mw'], { '+27606': 'vodacom', '+27603': 'mtn', })
def test_config_file_with_include(self): # Create include file self.mk_config_file('provider_prefixes', [ '"+27606": "vodacom"', '"+27603": "mtn"']) # Create root file self.mk_config_file('worker', [ 'transport_name: foo', 'middleware:', ' - provider_inbound_mw: vumi.provider.foo', 'provider_inbound_mw:']) # Find rel path between include and root files rel_path_providers = os.path.relpath( self.config_file['provider_prefixes'], os.path.dirname(self.config_file['worker'])) # Add rel path to root file with open(self.config_file['worker'], 'a') as f: f.write(' !include %s\n' % rel_path_providers) # Process config files options = StartWorkerOptions() options.parseOptions([ '--worker-class', 'foo.fooWorker', '--config', self.config_file['worker']]) self.assertEqual( options.worker_config['provider_inbound_mw'], { '+27606': 'vodacom', '+27603': 'mtn', })
def test_make_worker(self): self.mk_config_file('worker', ["transport_name: sphex"]) options = StartWorkerOptions() options.parseOptions(['--worker-class', 'vumi.demos.words.EchoWorker', '--config', self.config_file['worker'], ]) maker = VumiWorkerServiceMaker() worker = maker.makeService(options) self.assertEqual({'transport_name': 'sphex'}, worker.config)
def check_worker_help(self, worker_class_name, expected_emits): exits, emits = [], [] options = StartWorkerOptions() options.exit = lambda: exits.append(None) # stub out exit options.emit = lambda text: emits.append(text) options.parseOptions(['--worker-class', worker_class_name, '--worker-help', ]) self.assertEqual(len(exits), 1) self.assertEqual(emits, expected_emits)
def check_worker_help(self, worker_class_name, expected_emits): exits, emits = [], [] options = StartWorkerOptions() options.exit = lambda: exits.append(None) # stub out exit options.emit = lambda text: emits.append(text) options.parseOptions([ '--worker-class', worker_class_name, '--worker-help', ]) self.assertEqual(len(exits), 1) self.assertEqual(emits, expected_emits)
def test_override(self): options = StartWorkerOptions() options.parseOptions(['--worker-class', 'foo.FooWorker', '--set-option', 'blah:bleh', '--set-option', 'hungry:supper', ]) self.assertEqual(VumiOptions.default_vumi_options, options.vumi_options) self.assertEqual({}, options.opts) self.assertEqual({ 'blah': 'bleh', 'hungry': 'supper', }, options.worker_config)
def test_config_file(self): self.mk_config_file('worker', ["transport_name: sphex", "blah: thingy"]) options = StartWorkerOptions() options.parseOptions(['--worker-class', 'foo.FooWorker', '--config', self.config_file['worker'], ]) self.assertEqual(VumiOptions.default_vumi_options, options.vumi_options) self.assertEqual({}, options.opts) self.assertEqual({ 'transport_name': 'sphex', 'blah': 'thingy', }, options.worker_config)
def test_with_vumi_opts(self): self.mk_config_file('vumi', ["username: foo", "password: bar"]) self.mk_config_file('worker', ["transport_name: sphex"]) options = StartWorkerOptions() options.parseOptions(['--vumi-config', self.config_file['vumi'], '--hostname', 'blah', '--username', 'haxor', '--worker-class', 'foo.FooWorker', '--config', self.config_file['worker'], ]) self.assertEqual(dict(VumiOptions.default_vumi_options, username='******', password='******', hostname='blah'), options.vumi_options) self.assertEqual({}, options.opts)
def test_make_worker_with_sentry(self): services = [] dummy_service = DummyService() def service(*a, **kw): services.append((a, kw)) return dummy_service self.patch(servicemaker, 'SentryLoggerService', service) self.mk_config_file('worker', ["transport_name: sphex"]) options = StartWorkerOptions() options.parseOptions(['--worker-class', 'vumi.demos.words.EchoWorker', '--config', self.config_file['worker'], '--sentry', 'http://*****:*****@example.com/2/', ]) maker = VumiWorkerServiceMaker() worker = maker.makeService(options) self.assertEqual(services, [ (('http://*****:*****@example.com/2/', 'echoworker', 'global:echoworker'), {}) ]) self.assertTrue(dummy_service in worker.services)
def test_make_worker_with_sentry(self): services = [] dummy_service = DummyService() def service(*a, **kw): services.append((a, kw)) return dummy_service self.patch(servicemaker, 'SentryLoggerService', service) self.mk_config_file('worker', ["transport_name: sphex"]) options = StartWorkerOptions() options.parseOptions([ '--worker-class', 'vumi.demos.words.EchoWorker', '--config', self.config_file['worker'], '--sentry', 'http://*****:*****@example.com/2/', ]) maker = VumiWorkerServiceMaker() worker = maker.makeService(options) self.assertEqual(services, [(('http://*****:*****@example.com/2/', 'echoworker', 'global:echoworker'), {})]) self.assertTrue(dummy_service in worker.services)