def test_udp(self): addr = ':8000' reader = NetworkReader(addr) (host, port) = addr.split(':') port = int(port) host = '<broadcast>' # special code for broadcast sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) try: # Raspbian doesn't recognize SO_REUSEPORT sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True) except AttributeError: logging.warning( 'Unable to set socket REUSEPORT; system may not support it.') sock.connect((host, port)) # Set timeout we can catch if things are taking too long signal.signal(signal.SIGALRM, self._handler) signal.alarm(5) try: for line in SAMPLE_DATA: sock.send(line.encode('utf-8')) time.sleep(0.2) logging.debug('NetworkReader reading...') result = reader.read() logging.info('network wrote "%s", read "%s"', line, result) #self.assertEqual(line, result) except ReaderTimeout: self.assertTrue( False, 'NetworkReader timed out in test - is port ' '%s open?' % addr) signal.alarm(0)
def run_commands(logger_manager): reader = NetworkReader(network=':8002') api = logger_manager.api time.sleep(1) runner = ServerAPICommandLine(api) runner.process_command('load_cruise %s' % self.cruise_filename) runner.process_command('set_mode NBP1700 port') for i in range(4): self.assertEqual(reader.read(), 'TestLoggerManager ' + sample_data[i]) logging.info('NetworkReader done') logger_manager.quit()
def test_udp_eol(self): addr = ':8001' threading.Thread(target=write_network, args=(addr, EOL_SAMPLE_DATA, 0.1, 0.2)).start() # Set timeout we can catch if things are taking too long signal.signal(signal.SIGALRM, self._handler) signal.alarm(5) try: reader = NetworkReader(addr, eol='\n') result = reader.read() self.assertEqual('f1 line 1\n', result) result = reader.read() self.assertEqual('f1 line 1a\n', result) result = reader.read() self.assertEqual('f1 line 1b\n', result) result = reader.read() self.assertEqual('f1 line 2\n', result) result = reader.read() self.assertEqual('f1 line 3\n', result) except ReaderTimeout: self.assertTrue(False, 'NetworkReader timed out in test - is port ' '%s open?' % addr) signal.alarm(0)
def test_udp(self): addr = ':8000' threading.Thread(target=write_network, args=(addr, SAMPLE_DATA, 0.1, 0.2)).start() # Set timeout we can catch if things are taking too long signal.signal(signal.SIGALRM, self._handler) signal.alarm(5) try: reader = NetworkReader(addr) for line in SAMPLE_DATA: time.sleep(0.1) logging.debug('NetworkReader reading...') result = reader.read() logging.info('network wrote "%s", read "%s"', line, result) self.assertEqual(line, result) except ReaderTimeout: self.assertTrue(False, 'NetworkReader timed out in test - is port ' '%s open?' % addr) signal.alarm(0)
########################## # Now go through new_args and see what they want us to do. Draw # on all_args for the previously-set options that a reader, # transform or writer might need. ########################## # Readers if new_args.file: for filename in new_args.file.split(','): readers.append(TextFileReader( file_spec=filename, tail=all_args.tail, refresh_file_spec=all_args.refresh_file_spec)) if new_args.network: for addr in new_args.network.split(','): readers.append(NetworkReader(network=addr)) if new_args.udp: eol = all_args.network_eol for addr_str in new_args.udp.split(','): addr = addr_str.split(':') source = '' port = int(addr[-1]) # port is last arg if len(addr) > 1: source = addr[-2] # source (multi/broadcast) is prev arg if len(addr) > 2: parser.error('Format error for --udp argument. Format ' 'should be [source:]port') readers.append(UDPReader(port=port, source=source, eol=eol)) if new_args.redis: