def test_call3(self): self.observer({ 'message': '', 'log_namespace': 'test namespace', 'log_level': LogType.Group, 'log_time': time.mktime(now().timetuple()), }) self.assertEqual(len(self.observer.logs), 1)
def test_datetime_conversion(self): tz = pytz.timezone('Pacific/Johnston') now_tz = datetime.datetime.now(tz) doc = {'createdAt': now_tz} f = Fields(date_times=['createdAt']) f.convert_call(doc) self.assertEqual(doc['createdAt'], now_tz) self.assertLess(now() - doc['createdAt'], datetime.timedelta(seconds=1))
def call_hello(self): """ call_hello Calling the 'hello' endpoint on our own microservice. """ send_time = now() response = yield self.group_context('mdgroup').call('mdgroup.hello_world.endpoint.hello', { 'greeting': 'Calling self', 'sendTime': send_time }) # Reporting some delay times of the round call receive_time = now() return_time = from_utc_string(response['returnTime']) self.report_delay('Component -> User', receive_time - return_time) self.report_delay('Total', receive_time - send_time)
def test_construction(self): self.assertEqual(self.observer.session, None) self.assertEqual(self.observer.sessions, []) self.assertEqual(self.observer.log_type, LogType.User) self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] self.assertEqual(self.observer.logs, [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) self.assertEqual(self.observer.flushing, False) self.assertEqual(os.path.abspath(self.observer.recovery_file(self.session)), os.path.abspath('/logs/recovery.json'))
def test_call2(self): t = now() self.observer({ 'message': 'hello test', 'log_namespace': 'test namespace', 'log_level': LogType.Group, 'log_time': timestamp(t), }) self.assertLessEqual(t - from_utc_string(self.observer.logs[1]['time']), timedelta(seconds=1)) del self.observer.logs[1]['time'] self.assertEqual(self.observer.logs[1], { 'level': 'Group', 'source': 'test namespace', 'message': 'hello test' })
def test_start_flushing(self): self.observer.session = self.session self.observer.sleep = mock.MagicMock() yield self.observer.start_flushing(self.session) self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] yield self.observer.flush_logs() self.assertFalse(os.path.isfile(self.observer.recovery_file(self.session))) self.assertEqual(self.observer.logs, []) self.session.flush_logs.assert_called_once_with([{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) self.assertEqual(self.observer.session, self.session)
def hello(self, request, claims): """ hello endpoint Accepts a 'request' dictionary as described by the hello-request.v1.json schema in <package>/schema/endpoints and returns a response dictionary described by hello-response.v1.json The response consist of the recieved message prefixed with 'Hello World!' and a return time stamp. """ # Service specific settings as defined in the package settings.*.yml/json are # exposed in self.component_config.settings if self.component_config.settings['printInEndpoint']: self.log.info('Endpoint request object:') pprint(request) return_time = now() # The 'sendTime' argument is not required. Set to current time if not provided if 'sendTime' not in request: self.log.info('No "sendTime" argument in request, set to current time') send_time = return_time else: send_time = from_utc_string(request['sendTime']) # Reuse the request dictionary as response request['greeting'] = 'Hello World!: {0}'.format(request['greeting']) request['sendTime'] = send_time.isoformat() request['returnTime'] = return_time.isoformat() # Log the call delay self.report_delay('User -> Component', return_time - send_time) if self.component_config.settings['printInEndpoint']: self.log.info('Endpoint response object:') pprint(request) # Return the request dictionary. This will be validated against the # hello-response.v1.json JSON schema return request
def test_store_recovery(self): with Patcher() as patcher: self.observer.session = self.session patcher.fs.MakeDirectory('/logs') self.assertLessEqual(now() - from_utc_string(self.observer.logs[0]['time']), timedelta(seconds=1)) del self.observer.logs[0]['time'] self.assertEqual(self.observer.logs, [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }]) yield self.observer.store_recovery() self.assertEqual(self.observer.logs, []) with open(self.observer.recovery_file(self.session)) as f: self.assertEqual(json.load(f), [{ 'level': 'info', 'source': 'mdstudio.logging.impl.session_observer.SessionLogObserver', 'message': 'Collecting logs on session "MagicMock"' }])
def test_today(self): self.assertEqual(today(), now().date())
def test_now(self): self.assertLess(now() - datetime.now(tz=pytz.utc), timedelta(seconds=1))