コード例 #1
0
    def test_multiple_messages(self):
        context = messaging.Context()

        pub = messaging.PubSocket()
        pub.connect(context, 'controlsState')

        sub = messaging.SubSocket()
        sub.connect(context, 'controlsState')

        time.sleep(0.1)  # Slow joiner

        for i in range(100):
            pub.send(str(i))

        msg_seen = False
        i = 0
        while True:
            r = sub.receive(non_blocking=True)

            if r is not None:
                self.assertEqual(str(i), r.decode('utf8'))

                msg_seen = True
                i += 1

            if r is None and msg_seen:  # ZMQ sometimes receives nothing on the first receive
                break

        del pub
        del sub
        context.term()
コード例 #2
0
    def test_poll_and_create_many_subscribers(self):
        context = messaging.Context()

        pub = messaging.PubSocket()
        pub.connect(context, 'controlsState')

        with concurrent.futures.ThreadPoolExecutor() as e:
            poll = e.submit(poller)

            time.sleep(0.1)  # Slow joiner syndrome
            c = messaging.Context()
            for _ in range(10):
                messaging.SubSocket().connect(c, 'controlsState')

            time.sleep(0.1)

            # Send message
            pub.send("a")

            # Wait for poll result
            result = poll.result()

        del pub
        context.term()

        self.assertEqual(result, [b"a"])
コード例 #3
0
def poller():
    context = messaging.Context()

    p = messaging.Poller()

    sub = messaging.SubSocket()
    sub.connect(context, 'controlsState')
    p.registerSocket(sub)

    socks = p.poll(10000)
    r = [s.receive(non_blocking=True) for s in socks]

    return r
コード例 #4
0
    def test_conflate(self):
        context = messaging.Context()

        pub = messaging.PubSocket()
        pub.connect(context, 'controlsState')

        sub = messaging.SubSocket()
        sub.connect(context, 'controlsState', conflate=True)

        time.sleep(0.1)  # Slow joiner
        pub.send('a')
        pub.send('b')

        self.assertEqual(b'b', sub.receive())

        del pub
        del sub
        context.term()