예제 #1
0
def test_surveyor_respondent():
    with pynng.Surveyor0(listen=addr, recv_timeout=1000) as surveyor, \
            pynng.Respondent0(dial=addr, recv_timeout=1000) as resp1, \
            pynng.Respondent0(dial=addr, recv_timeout=2000) as resp2:
        query = b"hey how's it going buddy?"
        # wait for sockets to connect
        time.sleep(0.03)
        surveyor.send(query)
        assert resp1.recv() == query
        assert resp2.recv() == query
        resp1.send(b'not too bad I suppose')

        msg2 = b'''
            Thanks for asking.  It's been a while since I've had
            human contact; times have been difficult for me.  I woke up this
            morning and again could not find a pair of matching socks.  I know that
            a lot of people think it's worth it to just throw all your old socks
            out and buy like 12 pairs of identical socks, but that just seems so
            mundane.  Life is about more than socks, you know?  So anyway, since I
            couldn't find any socks, I went ahead and put banana peels on my
            feet.  They don't match *perfectly* but it's close enough.  Anyway
            thanks for asking, I guess I'm doing pretty good.
        '''
        resp2.send(msg2)
        resp = [surveyor.recv() for _ in range(2)]
        assert b'not too bad I suppose' in resp
        # if they're not the same
        assert msg2 in resp

        with pytest.raises(pynng.BadState):
            resp2.send(b'oadsfji')
예제 #2
0
파일: survey.py 프로젝트: mlasch/pynng
 async def send_survey_eternally():
     nonlocal max_survey
     with pynng.Respondent0() as sock:
         sock.dial(address)
         while max_survey:
             await sock.arecv_msg()
             print(f"CLIENT ({name}): RECEIVED SURVEY REQUEST\"")
             print(f"CLIENT ({name}): SENDING DATE SURVEY RESPONSE")
             await sock.asend(get_current_date().encode())       
             max_survey -= 1