def test_update(self): w = Worker('*****@*****.**') w.update({'idx': '301'}, foo=1, clock=30, bah='foo') self.assertEqual(w.idx, '301') self.assertEqual(w.foo, 1) self.assertEqual(w.clock, 30) self.assertEqual(w.bah, 'foo')
def test_handle_task(self): worker1 = Worker(hostname='fuzzie') worker1.event('online', time(), time(), {}) mw = self.cam.handle_worker((worker1.hostname, worker1)) task1 = self.create_task(worker1) task1.event('received', time(), time(), {}) mt = self.cam.handle_task((task1.uuid, task1)) assert mt.worker == mw worker2 = Worker(hostname=None) task2 = self.create_task(worker2) task2.event('received', time(), time(), {}) mt = self.cam.handle_task((task2.uuid, task2)) assert mt.worker is None task1.event('succeeded', time(), time(), {'result': 42}) assert task1.state == states.SUCCESS assert task1.result == 42 mt = self.cam.handle_task((task1.uuid, task1)) assert mt.name == task1.name assert mt.result == 42 task3 = self.create_task(worker1, name=None) task3.event('revoked', time(), time(), {}) mt = self.cam.handle_task((task3.uuid, task3)) assert mt is None
def test_handle_task(self): worker1 = Worker(hostname='fuzzie') worker1.event('online', time(), time(), {}) mw = self.cam.handle_worker((worker1.hostname, worker1)) task1 = create_task(worker1) task1.event('received', time(), time(), {}) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.worker, mw) worker2 = Worker(hostname=None) task2 = create_task(worker2) task2.event('received', time(), time(), {}) mt = self.cam.handle_task((task2.uuid, task2)) self.assertIsNone(mt.worker) task1.event('succeeded', time(), time(), {'result': 42}) self.assertEqual(task1.state, states.SUCCESS) self.assertEqual(task1.result, 42) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.name, task1.name) self.assertEqual(mt.result, 42) task3 = create_task(worker1, name=None) task3.event('revoked', time(), time(), {}) mt = self.cam.handle_task((task3.uuid, task3)) self.assertIsNone(mt)
def test_update(self): w = Worker('*****@*****.**') w.update({'idx': '301'}, foo=1, clock=30, bah='foo') assert w.idx == '301' assert w.foo == 1 assert w.clock == 30 assert w.bah == 'foo'
def test_updates_heartbeat(self): worker = Worker(hostname='foo') worker.event(None, time(), time()) self.assertEqual(len(worker.heartbeats), 1) h1 = worker.heartbeats[0] worker.event(None, time(), time() - 10) self.assertEqual(len(worker.heartbeats), 2) self.assertEqual(worker.heartbeats[-1], h1)
def test_updates_heartbeat(self): worker = Worker(hostname='foo') worker.event(None, time(), time()) assert len(worker.heartbeats) == 1 h1 = worker.heartbeats[0] worker.event(None, time(), time() - 10) assert len(worker.heartbeats) == 2 assert worker.heartbeats[-1] == h1
def assertExpires(self, dec, expired, tasks=10): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) for total in xrange(tasks): task = create_task(worker) task.event('received', time() - dec, time() - dec, {}) task.event('succeeded', time() - dec, time() - dec, {'result': 42}) self.assertTrue(task.name) self.assertTrue(self.cam.handle_task((task.uuid, task))) self.assertEqual(self.cam.on_cleanup(), expired)
def assertExpires(self, dec, expired, tasks=10): worker = Worker(hostname='fuzzie') worker.on_online(timestamp=time()) for total in xrange(tasks): task = create_task(worker) task.on_received(timestamp=time() - dec) task.on_succeeded(timestamp=time() - dec, result=42) self.assertTrue(task.name) self.assertTrue(self.cam.handle_task((task.uuid, task))) self.assertEqual(self.cam.on_cleanup(), expired)
def test_get_heartbeat(self): worker = Worker(hostname='fuzzie') self.assertIsNone(self.cam.get_heartbeat(worker)) t1 = time() t2 = time() t3 = time() for t in t1, t2, t3: worker.on_heartbeat(timestamp=t) self.assertEqual(self.cam.get_heartbeat(worker), make_aware(datetime.fromtimestamp(t3)))
def assertExpires(self, dec, expired, tasks=10): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) for total in range(tasks): task = create_task(worker) task.event('received', time() - dec, time() - dec, {}) task.event('succeeded', time() - dec, time() - dec, {'result': 42}) self.assertTrue(task.name) self.assertTrue(self.cam.handle_task((task.uuid, task))) self.assertEqual(self.cam.on_cleanup(), expired)
def test_compatible_with_Decimal(self): w = Worker('*****@*****.**') timestamp, local_received = Decimal(time()), time() w.event('worker-online', timestamp, local_received, fields={ 'hostname': '*****@*****.**', 'timestamp': timestamp, 'local_received': local_received, 'freq': Decimal(5.6335431), }) assert w.alive
def test_handle_worker(self): worker = Worker(hostname="fuzzie") worker.on_online(timestamp=time()) m = self.cam.handle_worker((worker.hostname, worker)) self.assertTrue(m) self.assertTrue(m.hostname) self.assertTrue(m.last_heartbeat) self.assertTrue(m.is_alive()) self.assertEqual(unicode(m), unicode(m.hostname)) self.assertTrue(repr(m))
def assertExpires(self, dec, expired, tasks=10): worker = Worker(hostname="fuzzie") worker.on_online(timestamp=time()) for total in xrange(tasks): task = create_task(worker) task.on_received(timestamp=time() - dec) task.on_succeeded(timestamp=time() - dec, result=42) self.assertTrue(task.name) self.assertTrue(self.cam.handle_task((task.uuid, task))) self.assertEqual(self.cam.on_cleanup(), expired)
def test_get_heartbeat(self): worker = Worker(hostname="fuzzie") self.assertIsNone(self.cam.get_heartbeat(worker)) t1 = time() t2 = time() t3 = time() for t in t1, t2, t3: worker.on_heartbeat(timestamp=t) self.assertEqual(self.cam.get_heartbeat(worker), datetime.fromtimestamp(t3))
def test_handle_worker(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam._last_worker_write.clear() m = self.cam.handle_worker((worker.hostname, worker)) assert m assert m.hostname assert m.last_heartbeat assert m.is_alive() assert str(m) == str(m.hostname) assert repr(m)
def test_handle_worker(self): worker = Worker(hostname='fuzzie') worker.on_online(timestamp=time()) self.cam._last_worker_write.clear() m = self.cam.handle_worker((worker.hostname, worker)) self.assertTrue(m) self.assertTrue(m.hostname) self.assertTrue(m.last_heartbeat) self.assertTrue(m.is_alive()) self.assertEqual(unicode(m), unicode(m.hostname)) self.assertTrue(repr(m))
def test_handle_worker(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam._last_worker_write.clear() m = self.cam.handle_worker((worker.hostname, worker)) self.assertTrue(m) self.assertTrue(m.hostname) self.assertTrue(m.last_heartbeat) self.assertTrue(m.is_alive()) self.assertEqual(unicode(m), unicode(m.hostname)) self.assertTrue(repr(m))
def test_get_heartbeat(self): worker = Worker(hostname='fuzzie') self.assertIsNone(self.cam.get_heartbeat(worker)) t1 = time() t2 = time() t3 = time() for t in t1, t2, t3: worker.event('heartbeat', t, t, {}) self.state.workers[worker.hostname] = worker self.assertEqual(self.cam.get_heartbeat(worker), make_aware(datetime.fromtimestamp(t3)))
def test_handle_task_timezone(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam.handle_worker((worker.hostname, worker)) tstamp = 1464793200.0 # 2016-06-01T15:00:00Z with override_settings(USE_TZ=True, TIME_ZONE='Europe/Helsinki'): task = self.create_task( worker, eta='2016-06-01T15:16:17.654321+00:00', expires='2016-07-01T15:16:17.765432+03:00', ) task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) assert ( mt.tstamp == datetime(2016, 6, 1, 15, 0, 0, tzinfo=timezone.utc) ) assert ( mt.eta == datetime(2016, 6, 1, 15, 16, 17, 654321, tzinfo=timezone.utc) ) assert ( mt.expires == datetime(2016, 7, 1, 12, 16, 17, 765432, tzinfo=timezone.utc) ) task = self.create_task(worker, eta='2016-06-04T15:16:17.654321') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) assert ( mt.eta == datetime(2016, 6, 4, 15, 16, 17, 654321, tzinfo=timezone.utc) ) with override_settings(USE_TZ=False, TIME_ZONE='Europe/Helsinki'): task = self.create_task( worker, eta='2016-06-01T15:16:17.654321+00:00', expires='2016-07-01T15:16:17.765432+03:00', ) task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) assert mt.tstamp == datetime(2016, 6, 1, 18, 0, 0) assert mt.eta == datetime(2016, 6, 1, 18, 16, 17, 654321) assert mt.expires == datetime(2016, 7, 1, 15, 16, 17, 765432) task = self.create_task(worker, eta='2016-06-04T15:16:17.654321') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) assert mt.eta == datetime(2016, 6, 4, 15, 16, 17, 654321)
def assert_expires(self, dec, expired, tasks=10): # Cleanup leftovers from previous tests self.cam.on_cleanup() worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) for total in range(tasks): task = self.create_task(worker) task.event('received', time() - dec, time() - dec, {}) task.event('succeeded', time() - dec, time() - dec, {'result': 42}) assert task.name assert self.cam.handle_task((task.uuid, task)) assert self.cam.on_cleanup() == expired
def test_handle_worker(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) old_last_update = timezone.now() - timedelta(hours=1) models.WorkerState.objects.all().update(last_update=old_last_update) m = self.cam.handle_worker((worker.hostname, worker)) assert m assert m.hostname assert m.last_heartbeat assert m.last_update != old_last_update assert m.is_alive() assert str(m) == str(m.hostname) assert repr(m)
def test_handle_task_timezone(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam.handle_worker((worker.hostname, worker)) tstamp = 1464793200.0 # 2016-06-01T15:00:00Z with override_settings(USE_TZ=True, TIME_ZONE='Europe/Helsinki'): task = create_task(worker, eta='2016-06-01T15:16:17.654321+00:00', expires='2016-07-01T15:16:17.765432+03:00') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) self.assertEqual( mt.tstamp, datetime(2016, 6, 1, 15, 0, 0, tzinfo=timezone.utc), ) self.assertEqual( mt.eta, datetime(2016, 6, 1, 15, 16, 17, 654321, tzinfo=timezone.utc), ) self.assertEqual( mt.expires, datetime(2016, 7, 1, 12, 16, 17, 765432, tzinfo=timezone.utc), ) task = create_task(worker, eta='2016-06-04T15:16:17.654321') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) self.assertEqual( mt.eta, datetime(2016, 6, 4, 15, 16, 17, 654321, tzinfo=timezone.utc), ) with override_settings(USE_TZ=False, TIME_ZONE='Europe/Helsinki'): task = create_task(worker, eta='2016-06-01T15:16:17.654321+00:00', expires='2016-07-01T15:16:17.765432+03:00') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.tstamp, datetime(2016, 6, 1, 18, 0, 0)) self.assertEqual(mt.eta, datetime(2016, 6, 1, 18, 16, 17, 654321)) self.assertEqual(mt.expires, datetime(2016, 7, 1, 15, 16, 17, 765432)) task = create_task(worker, eta='2016-06-04T15:16:17.654321') task.event('received', tstamp, tstamp, {}) mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.eta, datetime(2016, 6, 4, 15, 16, 17, 654321))
def test_handle_task_received(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam.handle_worker((worker.hostname, worker)) task = self.create_task(worker) task.event('received', time(), time(), {}) assert task.state == states.RECEIVED mt = self.cam.handle_task((task.uuid, task)) assert mt.name == task.name assert str(mt) assert repr(mt) mt.eta = timezone.now() assert 'eta' in str(mt) assert mt in models.TaskState.objects.active()
def test_handle_task_received(self): worker = Worker(hostname="fuzzie") worker.on_online(timestamp=time()) self.cam.handle_worker((worker.hostname, worker)) task = create_task(worker) task.on_received(timestamp=time()) self.assertEqual(task.state, "RECEIVED") mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.name, task.name) self.assertTrue(unicode(mt)) self.assertTrue(repr(mt)) mt.eta = now() self.assertIn("eta", unicode(mt)) self.assertIn(mt, models.TaskState.objects.active())
def test_handle_task_received(self): worker = Worker(hostname='fuzzie') worker.event('online', time(), time(), {}) self.cam.handle_worker((worker.hostname, worker)) task = create_task(worker) task.event('received', time(), time(), {}) self.assertEqual(task.state, 'RECEIVED') mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.name, task.name) self.assertTrue(unicode(mt)) self.assertTrue(repr(mt)) mt.eta = celery.now() self.assertIn('eta', unicode(mt)) self.assertIn(mt, models.TaskState.objects.active())
def test_handle_task_received(self): worker = Worker(hostname='fuzzie') worker.on_online(timestamp=time()) self.cam.handle_worker((worker.hostname, worker)) task = create_task(worker) task.on_received(timestamp=time()) self.assertEqual(task.state, 'RECEIVED') mt = self.cam.handle_task((task.uuid, task)) self.assertEqual(mt.name, task.name) self.assertTrue(unicode(mt)) self.assertTrue(repr(mt)) mt.eta = celery.now() self.assertIn('eta', unicode(mt)) self.assertIn(mt, models.TaskState.objects.active())
def test_listener_process_worker(bool1, listener): with mock.patch.object(listener.memory.workers, 'get') as wg, \ mock.patch.object(listener.memory, 'event') as mev, \ mock.patch('clearly.event_core.event_listener.immutable_worker') as it: worker_pre = Worker('hostname') wg.return_value = worker_pre if bool1 else None worker = Worker('hostname') mev.return_value = (worker, ''), '' with mock.patch('celery.events.state.Worker.status_string', new_callable=PropertyMock) as wss: wss.side_effect = (('pre_state', ) if bool1 else ()) + ('state', ) listener._process_worker_event(dict(hostname='hostname')) it.assert_called_once_with(worker, 'state', 'pre_state' if bool1 else worker_states.OFFLINE, not bool1)
def test_equality(self): self.assertEqual(Worker(hostname='foo').hostname, 'foo') self.assertEqual( Worker(hostname='foo'), Worker(hostname='foo'), ) self.assertNotEqual( Worker(hostname='foo'), Worker(hostname='bar'), ) self.assertEqual( hash(Worker(hostname='foo')), hash(Worker(hostname='foo')), ) self.assertNotEqual( hash(Worker(hostname='foo')), hash(Worker(hostname='bar')), )
def test_listener_set_worker_event(worker_event_type, listener): worker = Worker('hostname') listener.memory.event.return_value = (worker, ''), '' # noinspection PyProtectedMember gen = listener._set_worker_event(dict(type=worker_event_type)) assert worker == next(gen) listener.memory.event.assert_called_once_with(dict(type=worker_event_type)) assert worker.state == next(gen) assert worker.state == worker_states.TYPES[worker_event_type]
def test_reduce_direct(self): w = Worker('*****@*****.**') w.event('worker-online', 10.0, 13.0, fields={ 'hostname': '*****@*****.**', 'timestamp': 10.0, 'local_received': 13.0, 'freq': 60, }) fun, args = w.__reduce__() w2 = fun(*args) assert w2.hostname == w.hostname assert w2.pid == w.pid assert w2.freq == w.freq assert w2.heartbeats == w.heartbeats assert w2.clock == w.clock assert w2.active == w.active assert w2.processed == w.processed assert w2.loadavg == w.loadavg assert w2.sw_ident == w.sw_ident
def test_reduce_direct(self): w = Worker('*****@*****.**') w.event('worker-online', 10.0, 13.0, fields={ 'hostname': '*****@*****.**', 'timestamp': 10.0, 'local_received': 13.0, 'freq': 60, }) fun, args = w.__reduce__() w2 = fun(*args) self.assertEqual(w2.hostname, w.hostname) self.assertEqual(w2.pid, w.pid) self.assertEqual(w2.freq, w.freq) self.assertEqual(w2.heartbeats, w.heartbeats) self.assertEqual(w2.clock, w.clock) self.assertEqual(w2.active, w.active) self.assertEqual(w2.processed, w.processed) self.assertEqual(w2.loadavg, w.loadavg) self.assertEqual(w2.sw_ident, w.sw_ident)
def test_survives_unknown_worker_leaving(self): s = State(on_node_leave=Mock(name='on_node_leave')) (worker, created), subject = s.event({ 'type': 'worker-offline', 'hostname': '*****@*****.**', 'timestamp': time(), 'local_received': time(), 'clock': 301030134894833, }) self.assertEqual(worker, Worker('*****@*****.**')) self.assertFalse(created) self.assertEqual(subject, 'offline') self.assertNotIn('*****@*****.**', s.workers) s.on_node_leave.assert_called_with(worker)
def test_survives_unknown_worker_leaving(self): s = State(on_node_leave=Mock(name='on_node_leave')) (worker, created), subject = s.event({ 'type': 'worker-offline', 'hostname': '*****@*****.**', 'timestamp': time(), 'local_received': time(), 'clock': 301030134894833, }) assert worker == Worker('*****@*****.**') assert not created assert subject == 'offline' assert '*****@*****.**' not in s.workers s.on_node_leave.assert_called_with(worker)
def test_handle_task(self): worker1 = Worker(hostname="fuzzie") worker1.on_online(timestamp=time()) mw = self.cam.handle_worker((worker1.hostname, worker1)) task1 = create_task(worker1) task1.on_received(timestamp=time()) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.worker, mw) worker2 = Worker(hostname=None) task2 = create_task(worker2) task2.on_received(timestamp=time()) mt = self.cam.handle_task((task2.uuid, task2)) self.assertIsNone(mt.worker) task1.on_succeeded(timestamp=time(), result=42) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.name, task1.name) self.assertEqual(mt.result, 42) task3 = create_task(worker1, name=None) task3.on_revoked(timestamp=time()) mt = self.cam.handle_task((task3.uuid, task3)) self.assertIsNone(mt)
def test_handle_task(self): worker1 = Worker(hostname='fuzzie') worker1.on_online(timestamp=time()) mw = self.cam.handle_worker((worker1.hostname, worker1)) task1 = create_task(worker1) task1.on_received(timestamp=time()) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.worker, mw) worker2 = Worker(hostname=None) task2 = create_task(worker2) task2.on_received(timestamp=time()) mt = self.cam.handle_task((task2.uuid, task2)) self.assertIsNone(mt.worker) task1.on_succeeded(timestamp=time(), result=42) mt = self.cam.handle_task((task1.uuid, task1)) self.assertEqual(mt.name, task1.name) self.assertEqual(mt.result, 42) task3 = create_task(worker1, name=None) task3.on_revoked(timestamp=time()) mt = self.cam.handle_task((task3.uuid, task3)) self.assertIsNone(mt)
def test_drift_warning(self): worker = Worker(hostname="foo") with patch("celery.events.state.warn") as warn: worker.event(None, time() + (HEARTBEAT_DRIFT_MAX * 2), time()) self.assertTrue(warn.called) self.assertIn("Substantial drift", warn.call_args[0][0])
def test_drift_warning(self): worker = Worker(hostname='foo') with patch('celery.events.state.warn') as warn: worker.event(None, time() + (HEARTBEAT_DRIFT_MAX * 2), time()) warn.assert_called() assert 'Substantial drift' in warn.call_args[0][0]
def test_repr(self): assert repr(Worker(hostname='foo'))
def test_survives_missing_timestamp(self): worker = Worker(hostname='foo') worker.event('heartbeat') assert worker.heartbeats == []
def test_updates_heartbeat(self): worker = Worker(hostname="foo") worker.event(None, time(), time()) self.assertEqual(len(worker.heartbeats), 1) worker.event(None, time(), time() - 10) self.assertEqual(len(worker.heartbeats), 1)
def test_eq_ne_other(self): assert Worker('*****@*****.**') == Worker('*****@*****.**') assert Worker('*****@*****.**') != Worker('*****@*****.**') assert Worker('*****@*****.**') != object()
def test_survives_missing_timestamp(self): worker = Worker(hostname="foo") worker.on_heartbeat(timestamp=None) self.assertEqual(worker.heartbeats, [])
def test_update_heartbeat(self): worker = Worker(hostname='foo') worker.update_heartbeat(time(), time()) self.assertEqual(len(worker.heartbeats), 1) worker.update_heartbeat(time() - 10, time()) self.assertEqual(len(worker.heartbeats), 1)
def test_survives_missing_timestamp(self): worker = Worker(hostname='foo') worker.event('heartbeat') self.assertEqual(worker.heartbeats, [])
def test_drift_warning(self): worker = Worker(hostname='foo') with patch('celery.events.state.warn') as warn: worker.update_heartbeat(time(), time() + (HEARTBEAT_DRIFT_MAX * 2)) self.assertTrue(warn.called) self.assertIn('Substantial drift', warn.call_args[0][0])