def testCountDownUnthreadedMode(self): counter = ThreadedTimeCounter(mode=ThreadedTimeCounter.MODE_COUNT_DOWN, intervalSecond=1, durationSecond=2) active = True while active: active = counter.count()
def testSplitDurationSecondDDZeroHHZeroMMSS(self): duration = 86404 counter = ThreadedTimeCounter(durationSecond=duration) counter.splitDurationSecond(duration) self.assertEqual(counter.day, 1) self.assertEqual(counter.hour, 0) self.assertEqual(counter.minute, 0) self.assertEqual(counter.second, 4)
def testSplitDurationSecondHHMMSS(self): duration = 3700 counter = ThreadedTimeCounter(durationSecond=duration) counter.splitDurationSecond(duration) self.assertEqual(counter.day, 0) self.assertEqual(counter.hour, 1) self.assertEqual(counter.minute, 1) self.assertEqual(counter.second, 40)
def testSplitDurationSecondZeroSS(self): duration = 0 counter = ThreadedTimeCounter(durationSecond=duration) counter.splitDurationSecond(duration) self.assertEqual(counter.day, 0) self.assertEqual(counter.hour, 0) self.assertEqual(counter.minute, 0) self.assertEqual(counter.second, 0)
def start(self, commandLineArgs=None): ''' Start the data stream. :param commandLineArgs: used only for unit testing only :return: error message if relevant ''' isUnitTestMode = False if commandLineArgs == None: #here, we are not in unit test mode and we collect the parms entered #by the user on the command line commandLineArgs = sys.argv[1:] else: isUnitTestMode = True executionMode, durationStr, primaryFileName, secondaryFileName, isVerbose = self.getCommandLineArgs(commandLineArgs) localNow = arrow.now(LOCAL_TIME_ZONE) if executionMode.upper() == 'R': #C2 executing in real time mode ... tradingPair = 'BTCUSDT' print('Starting the Binance aggregate trade stream for pair {}. Type any key to stop the stream ...'.format( tradingPair)) self.datasource = BinanceDatasource(tradingPair) dateTimeStr = localNow.format(self.DATE_TIME_FORMAT_ARROW) #adding an Archiver to store on disk the primary data self.primaryDataFileName = self.buildPrimaryFileName(primaryFileName, dateTimeStr) self.datasource.addObserver(Archiver(self.primaryDataFileName, Archiver.PRIMARY_DATA_CSV_ROW_HEADER, isVerbose)) #adding an Observer to compute the secondary data, store them on disk and send #them to the Criterion csvSecondaryDataFileName = "{}-{}.csv".format(secondaryFileName, dateTimeStr) #forcing isVerbose to False to avoid interfering with Archiver verbosity ! self.datasource.addObserver(SecondaryDataAggregator(secondaryDataFilename=csvSecondaryDataFileName, doNotPrintOutput=durationStr != None, isVerbose=False)) try: self.datasource.startDataReception() except Exception as e: print('\nERROR - Connection with RT datasource could not be established.\n') self.stop() sys.exit(1) counter = None if durationStr: counter = ThreadedTimeCounter(ThreadedTimeCounter.MODE_COUNT_DOWN, \ intervalSecond=1, \ durationSecond=self.getDurationSeconds(durationStr), \ client=self) if not isUnitTestMode or durationStr != None: #here, we are not in unit test mode and we have to wait for the user to #stop receiving the real time data if counter: counter.start() while not self.stopped: if not os.name == 'posix': import msvcrt # only ok on Windows ! if msvcrt.kbhit(): if counter: counter.stop() print('\nStopping the stream ...') else: print('Stopping the stream ...') self.stop() sys.exit(0) # required for the program to exit ! else: #C2 executing in simulation mode ... if primaryFileName == self.DEFAULT_PRIMARY_FILENAME: errorMsg = "ERROR - in simulation mode, a primary file name must be provided !" print(errorMsg) return errorMsg csvSecondaryDataFileName = self.buildSecondaryFileNameFromPrimaryFileName(primaryFileName, secondaryFileName) try: self.datasource = ArchivedDatasource(primaryFileName) except FileNotFoundError as e: errorMsg = "ERROR - specified file {} not found !".format(e.filename) print(errorMsg) return errorMsg print('Starting C2 in simulation mode on primary data file {}.'.format( primaryFileName)) self.datasource.addObserver(SecondaryDataAggregator(csvSecondaryDataFileName, isVerbose)) self.datasource.processArchivedData()
def testCountDownThreadedMode(self): counter = ThreadedTimeCounter(mode=ThreadedTimeCounter.MODE_COUNT_DOWN, intervalSecond=1, durationSecond=2) counter.start()