def test_ETH_USDT_order(self): origin = { 'FileName': 'ETH_USDT-okex_order.gz', 'Date': '2018/06/26', 'Output': 'ETH_USDT-okex.book.csv.gz', } self.initialization('ETH/USDT', 'order', origin['Date']) input_key = self.Config['RedisCollectKey'] self.write_into_redis(input_key, self.RedisConnection, origin['FileName']) try: with timeout(60, exception=RuntimeWarning): self.process_data() except RuntimeWarning: pass try: with timeout(60, exception=RuntimeWarning): self.write_data_csv() except RuntimeWarning: pass fn1 = origin['Output'] fn2 = '%s/%s/%s' % (HOME_PATH, origin['Date'], self.Config['FileName']) self.compare_two_csv(fn1, fn2)
def test_BTC_USDT_ticker(self): origin = { 'FileName': 'BTC_USDT-binance_ticker.gz', 'Date': '2018/06/06', 'Output': 'BTC_USDT-binance.ticker.csv.gz', } self.initialization('BTC/USDT', 'ticker', origin['Date']) input_key = self.Config['RedisCollectKey'] self.write_into_redis(input_key, self.RedisConnection, origin['FileName']) try: with timeout(10, exception=RuntimeWarning): self.process_data() except RuntimeWarning: pass try: with timeout(20, exception=RuntimeWarning): self.write_data_csv() except RuntimeWarning: pass fn1 = origin['Output'] fn2 = '%s/%s/%s' % (HOME_PATH, origin['Date'], self.Config['FileName']) self.compare_two_csv(fn1, fn2)
def test_ETH_USD_trade(self): origin = { 'FileName': 'ETH_USD-bitmex_trade.gz', 'Date': '2018/08/08', 'Output': 'ETH_USD-bitmex.trade.csv.gz', } self.initialization('ETH/USD', 'trade', origin['Date']) input_key = self.Config['RedisCollectKey'] self.write_into_redis(input_key, self.RedisConnection, origin['FileName']) try: with timeout(10, exception=RuntimeWarning): self.process_data() except RuntimeWarning: pass try: with timeout(5, exception=RuntimeWarning): self.write_data_csv() except RuntimeWarning: pass fn1 = origin['Output'] fn2 = '%s/%s/%s' % (HOME_PATH, origin['Date'], self.Config['FileName']) self.compare_two_csv(fn1, fn2) self.delete_tmp_file(fn2)
def test_BTC_USD_orderbook10(self): origin = { 'FileName': 'BTC_USD-bitmex_order.gz', 'Date': '2018/04/24', 'Output': 'BTC_USD-bitmex.book.csv.gz', } self.initialization('BTC/USD', 'order', origin['Date']) input_key = self.Config['RedisCollectKey'] self.write_into_redis(input_key, self.RedisConnection, origin['FileName']) try: with timeout(30, exception=RuntimeWarning): self.process_data() except RuntimeWarning: pass try: with timeout(25, exception=RuntimeWarning): self.write_data_csv() except RuntimeWarning: pass fn1 = origin['Output'] fn2 = '%s/%s/%s' % (HOME_PATH, origin['Date'], self.Config['FileName']) self.compare_two_csv(fn1, fn2) self.delete_tmp_file(fn2)
def test_suppressed_inner(self): try: with timeout(1, Outer): with timeout(1.1, Inner): time.sleep(2) except Outer: pass time.sleep(1.5)
def vanilla_start(): logger.info("Starting the server, please wait...") zmq_context = zmq.Context() zmq_socket = zmq_context.socket(zmq.REQ) zmq_socket.setsockopt( zmq.LINGER, 0 ) # The default value is -1, which means wait until all messages have been sent before allowing termination. Set to 0 to discard unsent messages immediately, and any positive integer will be the number of milliseconds to keep trying to send before discard. subprocess.run([ "/usr/bin/sudo ", "/usr/sbin/etherwake", "-i", "eth0", secrets.MAC_ADDRESS ]) logger.info( "Sent Wake on LAN packet. Waiting 10 seconds for the server to switch on..." ) time.sleep(10) try: with timeout(5, exception=RuntimeError): logger.info("Attempting to connect to the control server...") zmq_socket.connect("tcp://" + secrets.LOCAL_IP + ":5555") logger.info("Connected. Sending \"start\" command...") zmq_socket.send_string("start") pass with timeout(5, exception=RuntimeError): logger.info("Done. Waiting for response...") response = zmq_socket.recv_string() pass if response == "done": logger.info("The server has been switched on ✅") zmq_socket.close() zmq_context.term() return {"result": "done"} else: logger.info("Received response: " + response) logger.info( "The server was switched on but the Minecraft Server was not switched on" ) zmq_socket.close() zmq_context.term() return {"result": "failed"} except RuntimeError: logger.info("No response...") logger.info("The server could not be switched on") zmq_socket.close() zmq_context.term() return {"result": "failed"} zmq_socket.close() zmq_context.term()
def test_nesting(self): q1 = Quota(0.5) q2 = Quota(1) try: with timeout(q1, Outer): with timeout(q2, Inner): time.sleep(1) self.fail() except Outer: self.assertFalse(q1.running()) self.assertLessEqual(q1.remaining(), 0.05) self.assertFalse(q2.running()) self.assertTrue(0.45 < q2.remaining() <= 0.5)
def test_games_make_it_to_elasticsearch_in_reasonable_time(self): Tasks.add(TEST_GAMES, []) wt = WatcherThread(TEST_KEY, cycles=1) wt.start() REASONABLE_TIME = 20 # seconds with timeout(REASONABLE_TIME): while True: try: # TODO - assert that the all items made it to ES docs = self.es.mget(index=TEST_ES_INDEX, doc_type=GAME_DOCTYPE, body={'ids': TEST_GAMES})['docs'] assert all([d['found'] for d in docs]) break except: pass sleep(0.1) wt.join() # 1. check that the game queue is now empty ONE_SHITLOAD = 10000 self.assertGreater(ONE_SHITLOAD, Tasks.redis.llen(GAME_QUEUE)) newly_queued_games = Tasks.redis._bulk_rpop(GAME_QUEUE, ONE_SHITLOAD) self.assertEquals(len(set(newly_queued_games)), 0) # 2. check that processed games made it to the GAME_SET self.assertEquals(Tasks.redis.scard(GAME_SET), len(set(TEST_GAMES))) items, is_old = zip(*Tasks.redis._intersect(GAME_SET, TEST_GAMES, insert=False)) self.assertTrue(all(is_old))
def get_availability(parkingID): try: starting_time_clock = time.clock() starting_time_time = time.time() from interruptingcow import timeout timeoutVar = 60 try: with timeout(timeoutVar, exception=RuntimeError): if parking_spaces is not None : if parkingID <= len(parking_spaces) : parking_space = parking_spaces[parkingID-1] initAvailability(parking_space) if parking_space.availability is not None : #print ("TIME get_availability: %f;%f" % (time.clock() - starting_time_clock, time.time() - starting_time_time)) print ("get_availability_TIME: %f" % ((time.time() - starting_time_time)*1000.0)) return parking_space.availability else : raise ValueError('Wrong parkingID in get_availability') else : raise ValueError('get_availability is called before parking_spaces is initialized') except RuntimeError: print("ERROR get_availability: does not get availability of parking spaces with %s seconds, please check the Data Federation component!" %timeoutVar) return 1 except Exception, e: print('ERROR get_availability: %s, please check the Data Federation component!' % str(e)) return 1
def get_parking_spaces(point_of_interest, distance_range): try: starting_time_clock = time.clock() starting_time_time = time.time() from interruptingcow import timeout timeoutVar = 60 try: with timeout(timeoutVar, exception=RuntimeError): initParkingSpaces(point_of_interest, distance_range) except RuntimeError: if routes == []: print("ERROR get_parking_spaces: does not get parking spaces with %s seconds, please check the KnowledgeBase component!" %timeoutVar) output = [] if parking_spaces is not None : parkingNumber = 1 for parking_space in parking_spaces : output.append((parkingNumber, parking_space.position)) parkingNumber +=1 #print ("TIME get_parking_spaces: %f;%f" % (time.clock() - starting_time_clock, time.time() - starting_time_time)) print ("get_parking_spaces_TIME: %f" % ((time.time() - starting_time_time)*1000.0)) return output except Exception, e: print('ERROR get_parking_spaces: %s, , please check the KnowledgeBase component!' % str(e)) return []
def get_walking_distance(parkingID, point_of_interest): try: starting_time_clock = time.clock() starting_time_time = time.time() from interruptingcow import timeout timeoutVar = 60 try: with timeout(timeoutVar, exception=RuntimeError): if parking_spaces is not None : if parkingID <= len(parking_spaces) : parking_space = parking_spaces[parkingID-1] initWalkingDistance(parking_space, point_of_interest) if parking_space.walkingDistance is not None : #print ("TIME get_walking_distance: %f;%f" % (time.clock() - starting_time_clock, time.time() - starting_time_time)) print ("get_walking_distance_TIME: %f" % ((time.time() - starting_time_time)*1000.0)) return parking_space.walkingDistance else : raise ValueError('Wrong parkingID in get_walking_distance') else : raise ValueError('get_walking_distance is called before parking_spaces is initialized') except RuntimeError: print("ERROR get_walking_distance: does not get walking distance of parking spaces with %s seconds, please check the Routing component!" %timeoutVar) return 100 except Exception, e: print('ERROR get_walking_distance: %s, please check the Routing component!' % str(e)) return 100
def _welcome(self, island_id, n=0): # type: (str, int) -> typing.Tuple[ndarray,ndarray,typing.List[str]] ''' Gets ``n`` incoming migrants for the given island and returns them. If ``n`` is zero, return all migrants. ''' consumer = self._builder.build_consumer( self.topic(island_id)) # consumer_timeout_ms=time_limit*1000 result_migrants = [] total_migrants = 0 class TimeoutError(Exception): pass try: with timeout(self._time_limit, exception=TimeoutError): for migrant_msg in consumer: # unpack the message migrant = self.deserialize(migrant_msg) result_migrants.append(migrant) total_migrants += migrant.migrants.shape[0] if n != 0 and total_migrants >= n: # we have the requested number of migrants - return break except TimeoutError: print('Timeout for request from Island : {0}'.format(island_id)) # sort by most recent sorted_migrants = sorted(result_migrants, key=lambda migrant: migrant.timestamp, reverse=True) #sorted_migrants = result_migrants return to_migrant_tuple(sorted_migrants, n)
def test_mock_watcher(setup_orthanc0, setup_orthanc1, capfd): obs = ObservableProxiedDicom(proxy_desc={"port": 8043}) watcher = Watcher() trigger0 = Trigger(evtype=DicomEventType.STUDY_ADDED, source=obs, action=obs.say) watcher.add_trigger(trigger0) p = Process(target=mock_runner) p.start() try: with timeout(10): print("Starting watcher") watcher.run() except: print("Stopping watcher") # Force p to flush stdout sys.stdout.flush() p.terminate() if capfd: captured = capfd.readouterr() print(captured.out) assert "AccessionNumber" in captured.out assert "SeriesInstanceUID" not in captured.out
def getDistance(self): try: with timeout(2, exception=RuntimeError): sleep(0.2) GPIO.output(self.TRIG, True) sleep(0.00001) GPIO.output(self.TRIG, False) while GPIO.input(self.ECHO) == 0: pulse_start = time() while GPIO.input(self.ECHO) == 1: pulse_end = time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) return distance except RuntimeError: print "Took longer than 5 seconds" pass except UnboundLocalError: print "Unbound Error" pass
def run(self): while True: cmd = self.cmd popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True) exit_really = False try: iterator = iter(popen.stdout.readline, "") while True: with timeout(0.8, exception=RuntimeError): stdout_line = next(iterator) s = stdout_line self.data = { 'data': [float(number) for number in s.split()] } self.return_data() if not self.death_pill_queue.empty(): exit_really = True break except RuntimeError: self.data = {'info': "Minimu response timeout"} self.return_data() popen.stdout.close() if exit_really: return_code = popen.wait() break popen.kill() if return_code: raise subprocess.CalledProcessError(return_code, cmd)
def test_site_submission(setup_orthanc0): reset_mock_seed() O = Orthanc() assert (O.check()) n_instances_init = O.gateway.statistics()["CountInstances"] logging.debug(O.gateway.statistics()) site_desc = yaml.load(sample_site_desc) H = MockSite.Factory.create(desc=site_desc)[0] try: with timeout(15): print("Starting mock site") H.run(pacs=O) except: print("Stopping mock site") n_instances = O.gateway.statistics()["CountInstances"] # At least 500 new instances arrived in the last 15 seconds assert (n_instances > n_instances_init + 500)
def fuzz_dumb_mode(address, port, raw, loop, seed): _generate_mutated_package(seed, raw, loop) for loop_step in range(loop): try: print '\n' print(address + ":" + str(port) + " " + str(loop_step + 1)) #print(tmp_fuzz_folder + "/" + str(seed) +'_'+ str(loop_step+1) + '.rdm', 'r') with open( tmp_fuzz_folder + "/" + str(seed) + '_' + str(loop_step + 1) + '.rdm', 'r') as file: raw = file.read().replace('\n', '') try: with timeout(1, exception=RuntimeException): __socket = connect_with_socket(address, port) send_data(__socket, raw) recieve_data(__socket) close_socket(__socket) except RuntimeException as ex: #print("exception 1" + ex.message) print colored('fuzz_dumb_mode_Exception1:' + ex.message, 'red') exit(1) except Exception as ex: print("fuzz_dumb_mode_exception 2:" + ex.message)
def status_update(self): from bluepy import btle _LOGGER.info("Updating %d %s devices", len(self.devices), repr(self)) for name, data in self.devices.items(): _LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name, data["mac"]) # from btlewrap import BluetoothBackendException try: with timeout(self.command_timeout, exception=DeviceTimeoutError): yield self.update_device_state(name, data["poller"]) except btle.BTLEException as e: logger.log_exception( _LOGGER, "Error during update of %s device '%s' (%s): %s", repr(self), name, data["mac"], type(e).__name__, suppress=True, ) except DeviceTimeoutError: logger.log_exception( _LOGGER, "Time out during update of %s device '%s' (%s)", repr(self), name, data["mac"], suppress=True, )
def stream_screen_log_by_id(success, error, unique_id='noID'): """ Streams tail of log file for a set period of time before timing out. :return: Returns """ try: with io.open( file='{mc_server_dir}/{screen_log}/screenlog.0'.format( mc_server_dir=ServerUtilities.MC_SERVER_DIR, screen_log=ServerUtilities.SCREEN_LOG), mode='rt', ) as log, timeout(ServerUtilities.MAX_RUNTIME, exception=RuntimeError): start_sending = False while True: line = log.readline() if start_sending and line: yield line elif unique_id in line: start_sending = True except RuntimeError: print('Run time was exceeded.') yield False except Exception as error: print('Other error in stream tail screen log', error) yield False
def GET_DATA(args, **kwargs): global GET Target, Number = args.split(';') # Session FOR SITE SIS = requests.Session() # EDITE THE USER AGENT FOR AECCES SOURSE SITE SIS.headers['User-Agent'] = '' # [ SET ] Proxy In the Process # SIS.proxies = PROXY # [ GET ] THE DATA SOURSE with timeout(10): try: time.sleep(1.25) GET = SIS.get(f"{Url}{Path_search}{Target}&start={Number}{'&filter=0' if int(Number) > 0 else '' }") if GET.status_code != 200: print(f"[{R}!!!{N}] Sorry You've been banned from Google") return 'gogend' except TimeoutError: print(f"[{R}!!!{N}] Timeout Error");exit() except requests.exceptions.ConnectionError: print(f"[{R}!!!{N}] Sorry No internet");exit() # return DATA WITH CONTENT LIKE [ BIN ] return GET.content
def GET_SEND_MESSAGE(*args, **kwargs): s_name, s_email, s_hidden, s_message, s_list, name_name, name_email, name_hidden, name_message = args[ 0].split('#|') try: with timeout(10): try: MES = SIS.post(url=s_list, data={ name_name: s_name, name_email: s_email, name_message: s_message, name_hidden if name_hidden != '' else '': s_hidden if s_hidden != '' else '' }) if MES.status_code == 200: return 'ok' else: return f'code#|{MES.status_code}' except requests.exceptions.ConnectionError: return 'connect#|' except TimeoutError: return 'time#|'
def Temp(basename, sensor, freq, stype, senml, temp, pres, shumid, tslight, dB, CO, count): try: with timeout(60*.5, exception=RuntimeError): #delay = 5 Min while True: tsl = TSL2561() tslight = round(tsl.readLux(), 2) temp = "%0.2f" % ((sensor.read_temperature()*(1.8))+32) test = 0 time.sleep(.25) # sound must last .25 seconds dB = adc.readADCSingleEnded(2, 6144, 475) / 10 # Sound in volts*100 #if 150 > dB > 50: # Record loud burst if dB > 50.00: # Adjust for large dB if dB > 150.00: dB = 150.00 # make graph easier to read curs.execute ("""INSERT INTO all_graphs ( category,temp, pres, shumid, adc, tslight, CO ) VALUES(%s, %s, %s, %s, %s, %s, %s)""",(time.strftime("%m/%d/%y %H:%M:%S"), "null", "null", "null", dB, "null", "null" ) ) db.commit() print "Excessive Noise" else: curs.execute ("""INSERT INTO all_graphs ( category,temp, pres, shumid, adc, tslight, CO ) VALUES(%s, %s, %s, %s, %s, %s, %s)""",(time.strftime("%m/%d/%y %H:%M:%S"), "null", "null", "null", dB, "null", "null" ) ) db.commit() print "Noise" if test == .5: # delay break test = test - 1 except RuntimeError: count = count + 1 if count < 5: curs.execute ("""INSERT INTO all_graphs ( category, temp, pres, shumid, adc, tslight, CO ) VALUES(%s, %s, %s, %s, %s, %s, %s)""",(time.strftime("%m/%d/%y %H:%M:%S"), "null", "null", "null", "null", tslight, "null", ) ) curs.execute ("""INSERT INTO all_graphs ( category, temp, pres, shumid, adc, tslight, CO ) VALUES(%s, %s, %s, %s, %s, %s, %s)""",(time.strftime("%m/%d/%y %H:%M:%S"), temp, "null", "null", "null", "null", "null", ) ) db.commit() print "Temp/Light", count Light(basename, sensor, freq, stype, senml, temp, pres, shumid, tslight, dB, CO, count) ADC(basename, sensor, freq, stype, senml, temp, pres, shumid, tslight, dB, CO, count) pass
def test(): """Test for FileTailer Python Class""" # Grab a test file data_path = file_utils.relative_dir(__file__, '../../data') test_path = os.path.join(data_path, 'http.log') print('Opening Data File: {:s}'.format(test_path)) # Create the Class tailer = FileTailer(test_path, tail=False) # First with no tailing for line in tailer.readlines(): print(line) print('Read with NoTail Test successful!') # Now include tailing (note: as an automated test this needs to timeout quickly) try: from interruptingcow import timeout # Spin up the class tailer = FileTailer(test_path) # Tail = True # Tail the file for 2 seconds and then quit try: with timeout(2, exception=RuntimeError): for line in tailer.readlines(): print(line) except RuntimeError: # InterruptingCow raises a RuntimeError on timeout print('Tailing Test successful!') except ImportError: print('Tailing Test not run, need interruptcow module...')
def execute(self): messages = [] try: with timeout( self._timeout, exception=WorkerTimeoutError( "Execution of command {} timed out after {} seconds" .format(self._source, self._timeout)), ): if inspect.isgeneratorfunction(self._callback): for message in self._callback(*self._args): messages += message else: messages = self._callback(*self._args) except WorkerTimeoutError as e: if messages: logger.log_exception(_LOGGER, "%s, sending only partial update", e, suppress=True) else: raise e _LOGGER.debug("Execution result of command %s: %s", self._source, messages) return messages
def execute(self): messages = [] with timeout(self._timeout, exception=TimeoutError('Execution of command {} timed out after {} seconds'.format(self._source, self._timeout))): messages = self._callback(*self._args) _LOGGER.debug('Execution result of command %s: %s', self._source, messages) return messages
def get_max_pollution(routeID): try: starting_time_clock = time.clock() starting_time_time = time.time() from interruptingcow import timeout timeoutVar = 60 try: with timeout(timeoutVar, exception=RuntimeError): if routes is not None : if routeID <= len(routes) : route = routes[routeID-1] initPollutionValues(route) if route.pollutionValues is not None : #print ("TIME get_max_pollution: %f;%f" % (time.clock() - starting_time_clock, time.time() - starting_time_time)) print ("get_max_pollution_TIME: %f" % ((time.time() - starting_time_time)*1000.0)) return max(route.pollutionValues) else : raise ValueError('Wrong routeID in get_max_pollution') else : raise ValueError('get_max_pollution is called before routes is initialized') except RuntimeError: print("ERROR get_max_pollution: does not get pollution with %s seconds, please check the Data Federation component!" %timeoutVar) return 0 except Exception, e: print('ERROR get_max_pollution: %s, please check the Data Federation component!' % str(e)) return 0
def get_routes_data(starting_point, ending_point, cost_mode, number_of_routes): try: starting_time_clock = time.clock() starting_time_time = time.time() from interruptingcow import timeout timeoutVar = 60 try: with timeout(timeoutVar, exception=RuntimeError): initRoutes(starting_point, ending_point, cost_mode, number_of_routes) except RuntimeError: if routes == []: print("ERROR get_routes: does not get routes with %s seconds, please check the Routing component!" %timeoutVar) output = [] if routes is not None : routeNumber = 1 for route in routes : # print((routeNumber, route.time, route.distance)) output.append((routeNumber, route.time, route.distance)) routeNumber +=1 #print ("TIME get_routes_data: %f;%f" % (time.clock() - starting_time_clock, time.time() - starting_time_time)) print ("get_routes_data_TIME: %f" % ((time.time() - starting_time_time)*1000.0)) return output except Exception, e: print('ERROR get_routes_data: %s, please check the Routing component!' % str(e)) return []
def test_double_route(capsys): print("Starting tripple") watcher = Watcher(action_interval=0.5) source_desc = {"ctype": "MockObservable", "polling_interval": 0.5} tr0 = mk_route("say_instances", source_desc=source_desc) tr1 = mk_route("say_hello_instances", source_desc=source_desc) tr2 = mk_route("say_series", source_desc=source_desc) tr2.source.observations = sample_observations watcher.add_trigger(tr0) watcher.add_trigger(tr1) watcher.add_trigger(tr2) try: with timeout(5): watcher.run() except: watcher.stop() if capsys: captured = capsys.readouterr() assert "INSTANCE" in captured.out assert "hello" in captured.out assert "SERIES" in captured.out
def initial_boot(): file = open('sequences.txt','a') file.write('\nInitial Boot at ' + time.strftime('%H:%M') + '\n') file.close() time.sleep(30) try: with timeout(300, exception=RuntimeError): # Connect to network hologram = connect_hologram() if hologram == 'Moo': raise RuntimeError except RuntimeError: file = open('error_messages.txt','a') file.write('MOOOOOOOOOOOOOO Connection:' + time.strftime('%Y-%m-%d %H:%M') + '\n') file.close() os.system('sudo hologram modem disconnect') os.system('sudo reboot') time.sleep(45) # Set timezone os.environ['TZ'] = 'US/Central' time.tzset() # Disconnect from network os.system('sudo hologram modem disconnect')
def run_programs(): players = bots[:] for bot in bots: bot.start() round = 0 while round < MAX_ROUNDS: if not bots or not maze.num_pellets: break for ghost in all_ghosts: ghost.move() for bot in bots[:]: bot.check_square() for bot in bots: bot.move() for bot in bots[:]: bot.check_square() for spite in itertools.chain(all_ghosts, bots): spite.end_round() graphics.draw_maze() round += 1 for player in players: player.send_message("Q") if not WINDOWS: for player in players: try: with timeout(2, exception=RuntimeError): player.process.wait() except RuntimeError: pass
def process_file(out_file_name): tagger = treetaggerwrapper.TreeTagger(TAGLANG='pl') f = open(out_file_name + '.txt', 'r') w = open(out_file_name + '_lemmatized.txt', 'w') i = 0 wrong_pos = ['SENT', 'interp'] for line in f: try: with timeout(5, exception=RuntimeError): tags = tagger.tag_text(line) tag_list = [] tags2 = treetaggerwrapper.make_tags(tags) for tag in tags2: if tag.pos not in wrong_pos: tag_list.append(tag.lemma) w.write(' '.join(tag_list) + '\n') i += 1 if i % 100: print(i) except RuntimeError: continue
def test(): """Test for BroLogReader Python Class""" # Grab a test file data_path = file_utils.relative_dir(__file__, '../data') # For each file, create the Class and test the reader files = [ 'conn.log', 'dns.log', 'http.log', 'dhcp.log', 'files.log', 'weird.log' ] for bro_log in files: test_path = os.path.join(data_path, bro_log) print('Opening Data File: {:s}'.format(test_path)) reader = BroLogReader(test_path, tail=False) # First with no tailing for line in reader.readrows(): print(line) print('Read with NoTail Test successful!') # Now include tailing (note: as an automated test this needs to timeout quickly) try: from interruptingcow import timeout # Spin up the class tailer = BroLogReader(test_path, tail=True) # Tail the file for 2 seconds and then quit try: with timeout(2, exception=RuntimeError): for line in tailer.readrows(): print(line) except RuntimeError: # InterruptingCow raises a RuntimeError on timeout print('Tailing Test successful!') except ImportError: print('Tailing Test not run, need interruptcow module...')
def evaluate_original(self, x): # type: (array) -> SupportsFloat """ Evaluate the objective function and return the result. Identical to original biopredynbench evaluation, i.e. divides by scaled error. Difficult to compare across models. """ from interruptingcow import timeout self.reset() self.setParameterVector(x) self.r.reset() def worker(): t_now = 0. scaled_residuals = zeros( (len(self.time_values), len(self.measured_quantities))) for it_next in range(1, len(self.time_values)): t_now = self.r.simulate(t_now, self.time_values[it_next], 100) t_now = self.time_values[it_next] if self.divergent(): return 1e9 * self.penalty_scale for iq, q in enumerate(self.measured_quantities): scaled_residuals[ it_next - 1, iq] = (self.r[q] - self.scaled_data[it_next - 1, iq] ) / self.scaled_error[it_next - 1, iq] return sqrt(mean(scaled_residuals**2.)) try: with timeout(10, StalledSimulation): return worker() except (RuntimeError, StalledSimulation): # if convergence fails, use a penalty score return 1e9 * self.penalty_scale
def test_orthanc_watcher(setup_orthanc0, capfd): print("Starting") obs = ObservableOrthanc() watcher = Watcher() trigger0 = Trigger(evtype=DicomEventType.INSTANCE_ADDED, source=obs, action=obs.say) watcher.add_trigger(trigger0) p = Process(target=orth_test_runner) p.start() try: with timeout(5): print("Starting watcher") watcher.run() except: print("Stopping watcher") watcher.stop() if capfd: captured = capfd.readouterr() assert "8ea24299-e051fc03-3ae9ed25-adc22b32-971a056a" in captured.out
def evaluate(self, x): # type: (array) -> SupportsFloat """ Evaluate the objective function and return the result. """ from interruptingcow import timeout self.reset() self.setParameterVector(x) self.r.reset() def worker(): t_now = 0. scaled_residuals = zeros( (len(self.time_values), len(self.measured_quantities))) for it_next in range(0, len(self.time_values)): for iq, q in enumerate(self.measured_quantities): scaled_residuals[it_next, iq] = (self.r[q] - self.scaled_data[it_next, iq] ) / self.reference_value_means[iq] if it_next + 1 < len(self.time_values): self.r.simulate(t_now, self.time_values[it_next + 1], 100) t_now = self.time_values[it_next + 1] if self.divergent(): return 1e9 * self.penalty_scale # print('normalized errors: ', sqrt(mean(scaled_residuals**2, axis=0))) return sqrt(mean(scaled_residuals**2.)) try: with timeout(10, StalledSimulation): return worker() except (RuntimeError, StalledSimulation): # if convergence fails, use a penalty score return 1e9 * self.penalty_scale
def const_mapper(df, log=None): """ As the AlgorithmX code requires inputs starting from zero we shall take all values in the dataframes and map them to ints. This function will return the solver required. The df is always randomly resampled when we run this so that we get a different initial answer each time. """ df = df.sample(len(df)) name_cols = get_name_cols(df) const_list = np.unique(df[name_cols].stack()) n = len(const_list) mapping = {} for i in range(n): mapping[const_list[i]] = i for col in name_cols: df = df.replace({col: mapping}) solver = AlgorithmX(n) gc.collect() for index, row in df.iterrows(): try: with timeout(TIMEOUT, exception=RuntimeError): solver.appendRow([r for r in row[name_cols]], row['set_no']) except RuntimeError: log.warning("Failed to create solver") solver = None break return solver
def run(self): try: with timeout(self.timeout, exception=TimeoutError): start = time() for m in self.trace_modules: try: m_item = self.module_selector(m) m.start_tracing() self.running_modules.append(m_item) except NotImplementedError: print("Invalid module: " + m) finished_modules = [] try: for m in self.exp_modules: try: m_item = self.module_selector(m) m_item.explore() finished_modules.append(m) except NotImplementedError: print("Invalid module: " + m) except NotRespondingError: self.shut_down() for m in finished_modules: self.exploration_modules.remove(m) analysis = Analysis(target=self.target, self.trace_modules, self.exploration_modules, self.snapshot, self.output_dir, self.time - (time() - start)) analysis.run() except TimeoutError: self.shut_down()
def execute(self): messages = [] with timeout(35): messages = self._callback(*self._args) _LOGGER.debug(messages) return messages
def intelectual_fuzz(_address, _port, _package_list, _loop_count, _seed, _bypass): for __package in _package_list: # start generate package _generate_mutated_package(_seed, __package, _loop_count) # start fuzz loop for __loop_step in range(_loop_count): try: print(_address + ":" + str(_port) + " " + str(__loop_step)) # get mutade package raw with open(tmp_fuzz_folder + "/" + str(_seed) + str(__loop_step+1) + '.rdm', 'r') as file: raw = file.read().replace('\n', '') try: with timeout(1, exception=RuntimeException): __socket = connect_with_socket(_address, _port) for __package_ in _package_list: if _package_list.index(__package_) == _package_list.index(__package) and _package_list.index(__package) != _bypass: # send mutate package send_data(__socket, raw) # print(raw) recieve_data(__socket) else: # send work package send_data(__socket, __package_) # print(__package_) recieve_data(__socket) close_socket(__socket) except RuntimeException as ex: print("exception 1" + ex.message) except Exception as ex: print("exception 2" + ex.message)
def query(request): try: with interruptingcow.timeout(30, Timeout): return _query(request) except Timeout: return JsonResponse({ 'results': [{"line": 0, "result": "Calculation took too long.", "out": []}] })
def test_timeout(self): q = Quota(1) with timeout(q, RuntimeError): time.sleep(0.2) self.assertTrue(0.75 < q.remaining() <= 0.8) time.sleep(1) self.assertTrue(0.75 < q.remaining() <= 0.8) with timeout(q, RuntimeError): time.sleep(0.2) self.assertTrue(0.55 < q.remaining() <= 0.6) try: with timeout(q, RuntimeError): time.sleep(0.7) self.fail() except RuntimeError: pass
def get_rss_entries(self, feed): try: with timeout(1, exception=RuntimeError): rss = feedparser.parse(feed) if rss.status == 200: return rss.entries except: return []
def test_busy_loop(self): try: with timeout(0.5): while True: pass except RuntimeError: pass else: self.fail('busy loop failed to interrupt')
def test_run_no_sensrs(self): ''' Run with no sensors. ''' try: with timeout(1.5): self.agent.run() except RuntimeError: pass assert self.agent._start_subprocess.call_count == 2
def test_run_no_data(self): ''' Run without upcoming data. ''' try: with timeout(1.5): self.receiver.run() except RuntimeError: pass assert self.receiver.assert_parent_exists.called
def retrieve_pid(self, pid_fpath): ret = None _command = 'cat %s' % pid_fpath try: with timeout(self.file_content_retrieving_timeout, exception=CLAUDEFileContentRetrievingTimeout): while not ret: _, stdout_stream, _ = self.ssh.exec_command(_command) ret = stdout_stream.readline().strip() except AttributeError, SSHException: logger.error('Not connected', exc_info=True) raise CLAUDENotConnectedError
def test_run(self): ''' Run sensor. ''' self.sensor.send_results = MagicMock() try: with timeout(1.5): self.sensor.run() except RuntimeError: pass assert self.sensor.send_results.called
def http_request(self, do_http_request, type, parse=True, quiet=True, timeout=60, throttle=(lambda: None)): """Perform HTTP request. Args: do_http_request: function that returns request object type (str): GET, POST, HEAD parse (bool, optional): Specifies if return should be parsed. Autodetects parse type as html, xml, or json. quiet (bool, optional): suppress output if True (default True) timeout (int, optional): Description throttle (lambda, optional): Description Returns: request object: if parse is False soup: soupified/jsonified text of http request """ response = None for i in range(10): try: with interruptingcow.timeout(timeout, exception=requests.exceptions.Timeout): response = do_http_request() except (requests.exceptions.Timeout, requests.exceptions.ConnectionError): if i > 1: print('THROTTLING REQUESTER', file=sys.stderr) # TODO - should not be stderr, maybe warning? throttle() print("Requester error:", str(sys.exc_info()[0]), file=sys.stderr) self.new_user_agent() continue if response is not None: break if i > 1: print('THROTTLING REQUESTER', file=sys.stderr) # TODO - should not be stderr, maybe warning? throttle() if not quiet: print(type, response.url) if not parse: return response soup = Requester.markup(response) if soup or soup == []: return soup else: return response
def get_response(self): if __debug__: print "waiting for response from " + self.name try: with timeout(self.time_limit, exception=RuntimeError): response = self.process.stdout.readline() if __debug__: print "got response from " + self.name + " : " + response.rstrip() return response except RuntimeError: if __debug__: print "gave up on " + self.name self.remove() raise RuntimeError(self.name + " didn't produce a response within one second")
def test_agent_server_time_diff(self, offset): ''' Test for client to shutdown when there's over 10min differential to / from server ''' self.agent.make_request.return_value.text = json.dumps( {'type': 'timestamp', 'timestamp': ( str(datetime_to_milliseconds(datetime.utcnow()) + offset) )}) # Function should terminate, so 1s should be enough with timeout(1): self.agent.run() assert self.agent.running is False
def jsonrpc(method, *params): try: headers = {'content-type': 'application/json'} payload = json.dumps({"method": method, 'params': params, 'jsonrpc': '2.0'}) # Cowtime for daemon RPC response set to timeout value in configuration with timeout(int(CONFIG['loader']['rpctimeout']), exception=Exception('Connection Timeout')): response = requests.post(URL, headers=headers, data=payload) if response.status_code != requests.codes.ok: return {'Status': 'error', 'Data': response.status_code} if response.json()['error']: return {'Status': 'error', 'Data': response.json()['error']} return {'Status': "ok", "Data": (response.json()['result'])} except Exception as e: comm_error_log(e, 'jsonrpc') return {'Status': 'error', 'Data': e}
def send_message(self, message): if __debug__: print "send message to " + self.name + " : " + message try: with timeout(self.time_limit, exception=RuntimeError): while not self.pollin.poll(0): time.sleep(0.1) self.process.stdin.write(message + "\n") if __debug__: print "sent message to " + self.name except RuntimeError: if __debug__: print "gave up on " + self.name self.remove() raise RuntimeError(self.name + " didn't accept a message within one second")
def _create_connection(self): try: with timeout(15, exception=RuntimeError): self._token = login(self._user, self._password) self._conn = HarmonyClient(self._token) print "he" +str(self._ip) + ":" + str(self._port) self._conn.connect(address=(self._ip, self._port), use_tls=False, use_ssl=False) print 'adf' self._conn.process(block=False) while not self._conn.sessionstarted: time.sleep(0.1) except RuntimeError: self._logger.error("Harmony: Connection error") raise RuntimeError return False return True
def fuzz_dumb_mode(address, port, raw, loop, seed): _generate_mutated_package(seed, raw, loop) for loop_step in range(loop): try: print(address + ":" + str(port) + " " + str(loop_step)) with open(tmp_fuzz_folder + "/" + str(seed) + str(loop_step) + '.rdm', 'r') as file: raw = file.read().replace('\n', '') try: with timeout(1, exception=RuntimeException): __socket = connect_with_socket(address, port) send_data(__socket, raw) recieve_data(__socket) close_socket(__socket) except RuntimeException as ex: print("exception 1" + ex.message) except Exception as ex: print("exception 2" + ex.message) os.system('rm -r ' + tmp_fuzz_folder + '/*')
def test_run_one_sensor(self): ''' Run with one sensor - uptime. ''' self.agent.agentconfig = {'sensors': [{ 'config': {'sampling_period': 60}, 'sensor': 'uptime', 'config_id': 'config_id', 'target': 'target', 'target_id': 'target_id' }]} try: with timeout(2.5): self.agent.run() except RuntimeError: pass assert self.agent._start_subprocess.call_count == 3
def test_IO_interrupt(self): """Make sure os.read() does not swallow our interruption.""" with tempfile.NamedTemporaryFile() as tf: fname = tf.name os.mkfifo(fname) def writer(): with open(fname, 'w'): time.sleep(2) threading.Thread(target=writer).start() try: fd = os.open(fname, os.O_RDONLY) try: with timeout(0.5): os.read(fd, 1024) self.fail('interrupt failed') except RuntimeError: pass finally: os.unlink(fname)
def test(): """Test for BroLogReader Python Class""" # Grab a test file data_path = file_utils.relative_dir(__file__, '../data') # For each file, create the Class and test the reader files = ['app_stats.log', 'conn.log', 'dhcp.log', 'dns.log', 'files.log', 'ftp.log', 'http.log', 'notice.log', 'smtp.log', 'ssl.log', 'weird.log', 'x509.log'] for bro_log in files: test_path = os.path.join(data_path, bro_log) print('Opening Data File: {:s}'.format(test_path)) reader = BroLogReader(test_path, tail=False) # First with no tailing for line in reader.readrows(): print(line) print('Read with NoTail Test successful!') # Test some of the error conditions reader.field_names = ['good', 'error'] reader.type_converters = [int, lambda x: datetime.datetime.fromtimestamp(float(x))] reader.make_dict([5, '0, .5, .5']) # Now include tailing (note: as an automated test this needs to timeout quickly) try: from interruptingcow import timeout # Spin up the class tailer = BroLogReader(test_path, tail=True) # Tail the file for 2 seconds and then quit try: with timeout(2, exception=RuntimeError): for line in tailer.readrows(): print(line) except RuntimeError: # InterruptingCow raises a RuntimeError on timeout print('Tailing Test successful!') except ImportError: print('Tailing Test not run, need interruptcow module...')