def test_udp_try_send(self): self.datagram = None def on_receive(udp_handle, data, status, address, flags): self.datagram = data udp_handle.receive_stop() def on_timeout(timer): try: self.client.try_send(b'hello', (TEST_IPV4, TEST_PORT1)) except uv.error.TemporaryUnavailableError: self.server.close() self.datagram = b'hello' self.server = uv.UDP(self.loop) self.server.bind((TEST_IPV4, TEST_PORT1)) self.server.receive_start(on_receive) self.client = uv.UDP(self.loop) self.client.bind(('0.0.0.0', 0)) self.timer = uv.Timer(self.loop) self.timer.start(on_timeout, 1, 0) self.loop.run() self.assert_equal(self.datagram, b'hello')
def test_try_write(self): self.buffer = b'' self.bytes_written = 0 def on_read(connection, data, status): self.buffer += data connection.stop_read() def on_connection(pipe_handle, status): client = uv.Pipe(pipe_handle.loop) pipe_handle.accept(client) client.start_read(on_read) pipe_handle.close() def on_timeout(timer): try: self.bytes_written = self.client.try_write(b'hello') except uv.error.TemporaryUnavailableError: self.server.close() finally: timer.close() self.server = uv.Pipe(self.loop) self.server.bind(TEST_PIPE1) self.server.listen(on_connection) self.client = uv.Pipe(self.loop) self.client.connect(TEST_PIPE1) self.timer = uv.Timer(self.loop) self.timer.start(on_timeout, 1, 0) self.loop.run() self.assert_equal(self.buffer, b'hello'[:self.bytes_written])
def test_timer_close(self): self.timer_called = 0 def on_timeout(_): self.timer_called += 1 timer = uv.Timer(self.loop) timer.start(on_timeout, 5, 0) timer.close() self.loop.run() self.assert_equal(self.timer_called, 0)
def test_timer_simple(self): self.timer_called = 0 def on_timeout(_): self.timer_called += 1 print(self.timer_called) timer = uv.Timer(self.loop) timer.start(on_timeout, 1, 0) self.loop.run() self.assert_equal(self.timer_called, 1)
def test_timer_repeat(self): self.timer_called = 0 def on_timeout(t): self.timer_called += 1 if self.timer_called == 3: t.close() timer = uv.Timer(self.loop) timer.start(on_timeout, 2, 2) self.loop.run() self.assert_equal(self.timer_called, 3)
def test_closed(self): self.timer = uv.Timer(self.loop) self.timer.close() try: repeat = self.timer.repeat except uv.HandleClosedError: pass else: self.assert_true(False) try: self.timer.repeat = 10 except uv.HandleClosedError: pass else: self.assert_true(False) self.assert_raises(uv.HandleClosedError, self.timer.again) self.assert_raises(uv.HandleClosedError, self.timer.start, None, 10, 0) self.assert_is(self.timer.stop(), None)
def test_fs_poll_change(self): def on_change(fs_poll, status, previous_stat, current_stat): #self.assert_not_equal(previous_stat.mtim, current_stat.mtim) #TODO self.assert_equal(status, uv.error.STATUS_SUCCESS) fs_poll.close() def on_timeout(timer): os.utime(self.temp_file.name, U_TIME) timer.close() self.fs_poll = uv.FSPoll(self.loop, interval=2000, callback=on_change) self.timer = uv.Timer(self.loop) with tempfile.NamedTemporaryFile() as temp_file: self.temp_file = temp_file self.fs_poll.path = temp_file.name self.fs_poll.start() self.timer.start(on_timeout, 1, 0) self.loop.run()
def test_timer_reference(self): self.timer_called = 0 def on_timeout(_): self.timer_called += 1 timer = uv.Timer(self.loop) timer.start(on_timeout, 5, 0) timer.dereference() self.loop.run() self.assert_false(timer.referenced) self.assert_equal(self.timer_called, 0) timer.reference() self.loop.run() self.assert_true(timer.referenced) self.assert_equal(self.timer_called, 1)
def test_fs_event_rename(self): def on_event(fs_event, filename, events, status): self.assert_equal(status, uv.error.STATUS_SUCCESS) self.assert_equal(filename, os.path.basename(self.temp_file.name)) self.assert_equal(events, uv.fs_event.UV_FS_EVENTS_RENAME) fs_event.close() def on_timeout(timer): os.rename(self.temp_file.name, self.temp_file.name + '-new-name') timer.close() self.fs_event = uv.FSEvent(self.loop, callback=on_event) self.timer = uv.Timer(self.loop) with tempfile.NamedTemporaryFile() as temp_file: self.temp_file = temp_file self.fs_event.path = temp_file.name self.fs_event.start() self.timer.start(on_timeout, 1, 0) self.loop.run() os.rename(self.temp_file.name + '-new-name', self.temp_file.name)
def test_check(self): def on_check(check): self.on_check_called += 1 if self.on_check_called > 5: check.close() def on_timeout(timer): self.on_timeout_called += 1 if self.on_timeout_called > 5: timer.close() self.on_check_called = 0 self.on_timeout_called = 0 self.check = uv.Check(self.loop) self.check.start(on_check) self.timer = uv.Timer(self.loop) self.timer.start(on_timeout, 1, 1) self.loop.run() self.assert_less_equal(self.on_timeout_called, self.on_check_called)