def test_get_routes(): osc = OSCThreadServer(encoding='utf8') osc.listen(default=True) values = [] @osc.address(u'/test_route') def dummy(*val): pass @osc.address(u'/_oscpy/routes/answer') def cb(*routes): values.extend(routes) send_message(b'/_oscpy/routes', [osc.getaddress()[1]], *osc.getaddress(), encoding='utf8', encoding_errors='strict') timeout = time() + 2 while not values: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9) assert u'/test_route' in values
def test___dump(capsys): options = Mock() options.repeat = 2 options.host = 'localhost' options.port = randint(60000, 65535) options.address = b'/test' options.safer = False options.encoding = None options.encoding_errors = 'strict' options.message = (1, 2, 3, 4, b"hello world") osc = __dump(options) out = capsys.readouterr().out assert out == '' send_message( options.address, options.message, options.host, options.port, safer=options.safer, encoding=options.encoding, encoding_errors=options.encoding_errors ) sleep(0.1) out, err = capsys.readouterr() assert err == '' lines = out.split('\n') assert lines[0] == u"/test: 1, 2, 3, 4, hello world" osc.stop()
def test_bind_multi(): osc = OSCThreadServer() sock1 = osc.listen() port1 = sock1.getsockname()[1] sock2 = osc.listen() port2 = sock2.getsockname()[1] cont = [] def success1(*values): cont.append(True) def success2(*values): cont.append(False) osc.bind(b'/success', success1, sock1) osc.bind(b'/success', success2, sock2) send_message(b'/success', [b'test', 1, 1.12345], 'localhost', port1) send_message(b'/success', [b'test', 1, 1.12345], 'localhost', port2) timeout = time() + 5 while len(cont) < 2: if time() > timeout: raise OSError('timeout while waiting for success message.') assert True in cont and False in cont
def test_encoding_errors_strict(): with pytest.raises(UnicodeEncodeError) as e_info: # noqa send_message( u'/encoded', [u'ééééé ààààà'], '', 9000, encoding='ascii', )
def send_message( self, osc_address, values, ip_address, port, sock=None, safer=False ): """Shortcut to the client's `send_message` method. Use the default_socket of the server by default. See `client.send_message` for more info about the parameters. """ if not sock and self.default_socket: sock = self.default_socket elif not sock: raise RuntimeError('no default socket yet and no socket provided') stats = send_message( osc_address, values, ip_address, port, sock=sock, safer=safer, encoding=self.encoding, encoding_errors=self.encoding_errors ) self.stats_sent += stats return stats
def test_validate_message_address_disabled(): osc = OSCThreadServer(validate_message_address=False) osc.listen(default=True) received = [] @osc.address(b'malformed') def malformed(*val): received.append(val[0]) send_message(b'malformed', [b'message'], *osc.getaddress()) timeout = time() + 2 while not received: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9)
def test_bind_default(): osc = OSCThreadServer() osc.listen(default=True) port = osc.getaddress()[1] cont = [] def success(*values): cont.append(True) osc.bind(b'/success', success) send_message(b'/success', [b'test', 1, 1.12345], 'localhost', port) timeout = time() + 5 while not cont: if time() > timeout: raise OSError('timeout while waiting for success message.')
def test_send_message(): osc = OSCThreadServer() sock = osc.listen() port = sock.getsockname()[1] acc = [] def success(*values): acc.append(values[0]) osc.bind(b'/success', success, sock) timeout = time() + 5 while len(acc) < 100: if time() > timeout: raise OSError('timeout while waiting for success message.') send_message(b'/success', [1], 'localhost', port)
def test_bind_get_address_smart(): osc = OSCThreadServer(advanced_matching=True) sock = osc.listen() port = sock.getsockname()[1] cont = [] def success(address, *values): assert address == b'/success/a' cont.append(True) osc.bind(b'/success/?', success, sock, get_address=True) send_message(b'/success/a', [b'test', 1, 1.12345], 'localhost', port) timeout = time() + 5 while not cont: if time() > timeout: raise OSError('timeout while waiting for success message.')
def test_unbind_default(): osc = OSCThreadServer() sock = osc.listen(default=True) port = sock.getsockname()[1] cont = [] def failure(*values): cont.append(True) osc.bind(b'/failure', failure) osc.unbind(b'/failure', failure) send_message(b'/failure', [b'test', 1, 1.12345], 'localhost', port) timeout = time() + 1 while time() > timeout: assert not cont sleep(10e-9)
def test_bind_address(): osc = OSCThreadServer() osc.listen(default=True) result = [] @osc.address(b'/test') def success(*args): result.append(True) timeout = time() + 1 send_message(b'/test', [], *osc.getaddress()) while len(result) < 1: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9) assert True in result
def test_encoding_receive(): osc = OSCThreadServer(encoding='utf8') osc.listen(default=True) values = [] @osc.address(u'/encoded') def encoded(*val): for v in val: assert not isinstance(v, bytes) values.append(val) send_message(b'/encoded', [b'hello world', u'ééééé ààààà'.encode('utf8')], *osc.getaddress()) timeout = time() + 2 while not values: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9)
def test_unbind(): osc = OSCThreadServer() sock = osc.listen() port = sock.getsockname()[1] cont = [] def failure(*values): cont.append(True) osc.bind(b'/failure', failure, sock) with pytest.raises(RuntimeError) as e_info: # noqa osc.unbind(b'/failure', failure) osc.unbind(b'/failure', failure, sock) send_message(b'/failure', [b'test', 1, 1.12345], 'localhost', port) timeout = time() + 1 while time() > timeout: assert not cont sleep(10e-9)
def test_get_sender(): osc = OSCThreadServer(encoding='utf8') osc.listen(default=True) values = [] @osc.address(u'/test_route') def callback(*val): values.append(osc.get_sender()) with pytest.raises(RuntimeError, match='get_sender\(\) not called from a callback'): osc.get_sender() send_message(b'/test_route', [osc.getaddress()[1]], *osc.getaddress(), encoding='utf8') timeout = time() + 2 while not values: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9)
def test_get_version(): osc = OSCThreadServer(encoding='utf8') osc.listen(default=True) values = [] @osc.address(u'/_oscpy/version/answer') def cb(val): print(val) values.append(val) send_message(b'/_oscpy/version', [osc.getaddress()[1]], *osc.getaddress(), encoding='utf8', encoding_errors='strict') timeout = time() + 2 while not values: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9) assert __version__ in values
def test_decorator(): osc = OSCThreadServer() sock = osc.listen(default=True) port = sock.getsockname()[1] cont = [] @osc.address(b'/test1', sock) def test1(*values): print("test1 called") cont.append(True) @osc.address(b'/test2') def test2(*values): print("test1 called") cont.append(True) send_message(b'/test1', [], 'localhost', port) send_message(b'/test2', [], 'localhost', port) timeout = time() + 1 while len(cont) < 2: if time() > timeout: raise OSError('timeout while waiting for success message.')
def test_bind_address_class(): osc = OSCThreadServer() osc.listen(default=True) @ServerClass class Test(object): def __init__(self): self.result = [] @osc.address_method(b'/test') def success(self, *args): self.result.append(True) timeout = time() + 1 test = Test() send_message(b'/test', [], *osc.getaddress()) while len(test.result) < 1: if time() > timeout: raise OSError('timeout while waiting for success message.') sleep(10e-9) assert True in test.result
def _send(options): def _parse(s): try: return literal_eval(s) except: return s stats = Stats() for i in range(options.repeat): stats += send_message(options.address, [_parse(x) for x in options.message], options.host, options.port, safer=options.safer, encoding=options.encoding, encoding_errors=options.encoding_errors) print(stats)
def test_intercept_errors(caplog): cont = [] def success(*values): cont.append(True) def broken_callback(*values): raise ValueError("some bad value") osc = OSCThreadServer() sock = osc.listen() port = sock.getsockname()[1] osc.bind(b'/broken_callback', broken_callback, sock) osc.bind(b'/success', success, sock) send_message(b'/broken_callback', [b'test'], 'localhost', port) sleep(0.01) send_message(b'/success', [b'test'], 'localhost', port) assert not osc.join_server(timeout=0.02) # Thread not stopped assert cont == [True] assert len(caplog.records) == 1, caplog.records record = caplog.records[0] assert record.msg == "Unhandled exception caught in oscpy server" assert not record.args assert record.exc_info osc = OSCThreadServer(intercept_errors=False) sock = osc.listen() port = sock.getsockname()[1] osc.bind(b'/broken_callback', broken_callback, sock) send_message(b'/broken_callback', [b'test'], 'localhost', port) assert osc.join_server( timeout=0.02) # Thread properly sets termination event on crash assert len(caplog.records) == 1, caplog.records # Unchanged
sock = SOCK = osc.listen(address=address, family='unix') else: SOCK = sock = osc.listen() address, port = osc.getaddress(sock) osc.bind(b'/count', count, sock=sock) for i, pattern in enumerate(patterns): for safer in (False, True): timeout = time() + DURATION sent = 0 received = 0 while time() < timeout: send_message(b'/count', pattern, address, port, sock=SOCK, safer=safer) sent += 1 sleep(10e-9) size = len(format_message(b'/count', pattern)[0]) / 1000. print( f"{i}: safe: {safer}\t", f"sent:\t{sent}\t({sent / DURATION}/s)\t({sent * size / DURATION:.2f}MB/s)\t" # noqa f"received:\t{received}\t({received / DURATION}/s)\t({received * size / DURATION:.2f}MB/s)\t" # noqa f"loss {((sent - received) / sent) * 100}%") if family == 'unix': os.unlink(address)
def test_encoding_errors_replace(): send_message(u'/encoded', [u'ééééé ààààà'], 'localhost', 9000, encoding='ascii', encoding_errors='replace')