From de72f98fbe01d571ac6eea4eca8ecc2dcefbfdc0 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 8 Dec 2014 18:17:32 -0500 Subject: [PATCH 1/4] test for workers equality --- tests/test_worker.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_worker.py b/tests/test_worker.py index c6d85ff..59f0b26 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -318,3 +318,12 @@ class TestWorker(RQTestCase): 'Expected at least some work done.') self.assertEquals(job.result, 'Hi there, Adam!') self.assertEquals(job.description, '你好 世界!') + + def test_worker_hash_(self): + """Workers are hashed by their .name attribute""" + q = Queue('foo') + w1 = Worker([q], name="worker1") + w2 = Worker([q], name="worker2") + w3 = Worker([q], name="worker1") + worker_set = {w1, w2, w3} + self.assertEquals(len(worker_set), 2) From c301369c2eb45a0969aa32384ddc7115458f5da1 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 8 Dec 2014 18:17:55 -0500 Subject: [PATCH 2/4] implement __eq__ and __hash__ for workers --- rq/worker.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rq/worker.py b/rq/worker.py index bf40a65..d14bf88 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -580,6 +580,14 @@ class Worker(object): """Pops the latest exception handler off of the exc handler stack.""" return self._exc_handlers.pop() + def __eq__(self, other): + if not isinstance(other, self.__class__): + raise TypeError('Cannot compare workers to other types (of workers)') + return self.name == other.name + + def __hash__(self): + return hash(self.name) + class SimpleWorker(Worker): def _install_signal_handlers(self, *args, **kwargs): From 0ad4cb3410483dae10381a781d14e3d485a91a08 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 8 Dec 2014 18:29:55 -0500 Subject: [PATCH 3/4] comments --- rq/worker.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rq/worker.py b/rq/worker.py index d14bf88..a2bbb2a 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -581,11 +581,13 @@ class Worker(object): return self._exc_handlers.pop() def __eq__(self, other): + """Equality does not take the database/connection into account""" if not isinstance(other, self.__class__): raise TypeError('Cannot compare workers to other types (of workers)') return self.name == other.name def __hash__(self): + """The hash does not take the database/connection into account""" return hash(self.name) From 6ef9177a7d8a34f49483c80dfe3a8097832fe006 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Tue, 9 Dec 2014 11:13:56 -0500 Subject: [PATCH 4/4] 2.6 compatible set syntax --- tests/test_worker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_worker.py b/tests/test_worker.py index 59f0b26..6fa127f 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -325,5 +325,5 @@ class TestWorker(RQTestCase): w1 = Worker([q], name="worker1") w2 = Worker([q], name="worker2") w3 = Worker([q], name="worker1") - worker_set = {w1, w2, w3} + worker_set = set([w1, w2, w3]) self.assertEquals(len(worker_set), 2)