示例#1
0
文件: test_pubsub.py 项目: thrau/pymq
    def test_unsubscribe_before_init(self, pymq_init):
        invocations = queue.Queue()

        def handler(event: str):
            invocations.put(event)

        pymq.subscribe(handler, channel="early/subscription")
        pymq.unsubscribe(handler, channel="early/subscription")

        pymq_init()
        pymq.publish("hello", channel="early/subscription")
        with pytest.raises(queue.Empty):
            invocations.get(timeout=0.25)
示例#2
0
    def test_remove_listener_also_removes_redis_subscription(
            self, pymq_redis, redislite):
        def listener1(event: MyRedisEvent):
            pass

        def listener2(event: MyRedisEvent):
            pass

        pymq.subscribe(listener1)
        pymq.subscribe(listener2)

        assert 1 == len(redislite.pubsub_channels())
        pymq.unsubscribe(listener1)
        assert 1 == len(redislite.pubsub_channels())
        pymq.unsubscribe(listener2)
        assert 0 == len(redislite.pubsub_channels())
示例#3
0
文件: test_pubsub.py 项目: thrau/pymq
    def test_subscribe_before_init_and_unsubscribe(self, pymq_init):
        event = threading.Event()

        def subscriber(arg):
            event.set()

        pymq.subscribe(subscriber, "my_channel")

        pymq_init()

        pymq.publish("hello", "my_channel")
        assert event.wait(2)
        event.clear()

        pymq.unsubscribe(subscriber, "my_channel")
        assert not event.wait(1)

        pymq.subscribe(subscriber, "my_channel")
        pymq.publish("hello", "my_channel")
        assert event.wait(2)
示例#4
0
    def run(self, n=None, ia=None):
        clients_done = set(self.client_ids)

        # lots of problems with this unfortunately, may never terminate if clients disappear, concurrent events from
        # previous (aborted) calls will interfere with future calls, etc.

        def done_subscriber(event: WorkloadDoneEvent):
            clients_done.remove(event.client_id)
            if len(clients_done) == 0:
                with self.lock:
                    self.done = True
                    self.lock.notify_all()

        try:
            with self.lock:
                pymq.subscribe(done_subscriber)
                for c in self.client_ids:
                    self.ctrl.set_workload(c, ia, n)

                self.lock.wait_for(self.stopped)
        finally:
            pymq.unsubscribe(done_subscriber)