def test_loop(self): class Event(object): def __init__(self, count, data, stop=False): self.count = count self.data = data self.stop = stop def __repr__(self): return '<{}, {}, {}>'.format(self.count, self.data, self.stop) class MockPort(object): def __init__(self, events=[]): self._file = b'' self._out = b'' self._is_open = False self.event = threading.Event() self.count = 0 self.events = [Event(*x) for x in sorted(events, key=lambda x: x[0])] def write(self, request): # Process the packet and put together a response. p = parse_request(request) if p['action'] == stentura._OPEN: self._out = make_response(p['seq'], p['action']) self._is_open = True elif p['action'] == stentura._READC: if not self._is_open: raise Exception("no open") length, block, byte = p['p3'], p['p4'], p['p5'] seq = p['seq'] action = stentura._READC start = block * 512 + byte end = start + length data = self._file[start:end] self._out = make_response(seq, action, p1=len(data), data=data) while self.events and self.events[0].count <= self.count: event = self.events.pop(0) self.append(event.data) if event.stop: self.event.set() self.count += 1 return len(request) def read(self, count): requested_bytes = self._out[0:count] self._out = self._out[count:] return requested_bytes def append(self, data): self._file += data return self def flushInput(self): pass def flushOutput(self): pass data1 = bytearray([0b11001010] * 4 * 9) data1_trans = [['S-', 'K-', 'R-', 'O-', '-F', '-P', '-T', '-D']] * 9 data2 = bytearray([0b11001011] * 4 * 30) tests = [ # No inputs but nothing crashes either. (MockPort([(30, b'', True)]), []), # A few strokes. (MockPort([(23, data1), (43, b'', True)]), data1_trans), # Ignore data that's there before we started. (MockPort([(46, b'', True)]).append(data2), []), # Ignore data that was there and also parse some strokes. (MockPort([(25, data1), (36, b'', True)]).append(data2), data1_trans) ] for test in tests: read_data = [] def callback(data): read_data.append(data) port = test[0] expected = test[1] ready_called = [False] def ready(): ready_called[0] = True try: ready_called[0] = False stentura._loop(port, port.event, callback, ready, timeout=0) except stentura._StopException: pass self.assertEqual(read_data, expected) self.assertTrue(ready_called[0])
def test_loop(self): class Event(object): def __init__(self, count, data, stop=False): self.count = count self.data = data self.stop = stop def __repr__(self): return '<{}, {}, {}>'.format(self.count, self.data, self.stop) class MockPort(object): def __init__(self, events=[]): self._file = b'' self._out = b'' self._is_open = False self.event = threading.Event() self.count = 0 self.events = [ Event(*x) for x in sorted(events, key=lambda x: x[0]) ] def write(self, request): # Process the packet and put together a response. p = parse_request(request) if p['action'] == stentura._OPEN: self._out = make_response(p['seq'], p['action']) self._is_open = True elif p['action'] == stentura._READC: if not self._is_open: raise Exception("no open") length, block, byte = p['p3'], p['p4'], p['p5'] seq = p['seq'] action = stentura._READC start = block * 512 + byte end = start + length data = self._file[start:end] self._out = make_response(seq, action, p1=len(data), data=data) while self.events and self.events[0].count <= self.count: event = self.events.pop(0) self.append(event.data) if event.stop: self.event.set() self.count += 1 return len(request) def read(self, count): requested_bytes = self._out[0:count] self._out = self._out[count:] return requested_bytes def append(self, data): self._file += data return self def flushInput(self): pass def flushOutput(self): pass data1 = bytearray([0b11001010] * 4 * 9) data1_trans = [['S-', 'K-', 'R-', 'O-', '-F', '-P', '-T', '-D']] * 9 data2 = bytearray([0b11001011] * 4 * 30) tests = [ # No inputs but nothing crashes either. (MockPort([(30, b'', True)]), []), # A few strokes. (MockPort([(23, data1), (43, b'', True)]), data1_trans), # Ignore data that's there before we started. (MockPort([(46, b'', True)]).append(data2), []), # Ignore data that was there and also parse some strokes. (MockPort([(25, data1), (36, b'', True)]).append(data2), data1_trans) ] for test in tests: read_data = [] def callback(data): read_data.append(data) port = test[0] expected = test[1] ready_called = [False] def ready(): ready_called[0] = True try: ready_called[0] = False stentura._loop(port, port.event, callback, ready, timeout=0) except stentura._StopException: pass self.assertEqual(read_data, expected) self.assertTrue(ready_called[0])