def test_get_reservation_exhausted(self): now = now_utc_sec() account_id = random_string() coordinate = '{}:{}'.format(self.resource_name, account_id) mock_token_table = create_non_fung_table(self.token_table) mock_limit_table = create_limit_table(self.limit_table) _insert_limit(mock_limit_table, self.resource_name, account_id, self.limit) self.manager._token_table = mock_token_table self.manager._limit_table = mock_limit_table # Insert enough tokens to reach the limit for i in range(0, self.limit): token = { 'resourceCoordinate': coordinate, 'resourceName': self.resource_name, 'accountId': account_id, 'resourceId': 'resource-' + str(i), 'expirationTime': now + 10000, 'reservationId': random_string() } mock_token_table.put_item(Item=token) self.assertRaises(CapacityExhaustedException, self.manager.get_reservation, account_id)
def test_get_reservation(self): now = now_utc_sec() account_id = random_string() coordinate = '{}:{}'.format(self.resource_name, account_id) mock_token_table = create_non_fung_table(self.token_table) mock_limit_table = create_limit_table(self.limit_table) _insert_limit(mock_limit_table, self.resource_name, account_id, self.limit) self.manager._token_table = mock_token_table self.manager._limit_table = mock_limit_table self.manager.get_reservation(account_id) response = mock_token_table.query( KeyConditionExpression=Key('resourceCoordinate').eq(coordinate)) self.assertEqual(1, response['Count']) items = dict(pair for item in response['Items'] for pair in item.items()) self.assertEqual(self.resource_name, items['resourceName']) self.assertEqual(account_id, items['accountId']) self.assertTrue(items['expirationTime'] > now) self.assertIn('resourceId', items)
def test_delete_fallback_no_type(self): event_source = random_string() detail_type = random_string() event = {'source': event_source, 'detail-type': detail_type} mock_default_processor = Mock() mock_default_processor.source = event_source mock_default_processor.type = None mock_default_processor.test_and_get_id = MagicMock( return_value=self.resource_id) mock_type_processor = Mock() mock_type_processor.source = event_source mock_type_processor.type = detail_type + random_string() mock_type_processor.test_and_get_id = MagicMock( side_effect=Exception('Wrong processor invoked')) mock_table = create_non_fung_table(self.table_name, self.index_name) self._insert_token(mock_table) self.assertEqual(1, self._get_resource_id_count(mock_table)) manager = EventProcessorManager( table_name=self.table_name, index_name=self.index_name, processors=[mock_default_processor, mock_type_processor]) manager._table = mock_table manager.process_event(event) self.assertEqual(0, self._get_resource_id_count(mock_table))
def test_get_token_count(self): now = now_utc_sec() account_id = random_string() coordinate = '{}:{}'.format(self.resource_name, account_id) mock_token_table = create_non_fung_table(self.token_table) mock_limit_table = create_limit_table(self.limit_table) _insert_limit(mock_limit_table, self.resource_name, account_id, self.limit) expected_count = 1 # Insert 2 expired and 1 valid expired_token_1 = { 'resourceCoordinate': coordinate, 'resourceName': self.resource_name, 'accountId': account_id, 'resourceId': random_string(), 'expirationTime': now, 'reservationId': random_string() } expired_token_2 = { 'resourceCoordinate': coordinate, 'resourceName': self.resource_name, 'accountId': account_id, 'resourceId': random_string(), 'expirationTime': now - 1000, 'reservationId': random_string() } valid_token = { 'resourceCoordinate': coordinate, 'resourceName': self.resource_name, 'accountId': account_id, 'resourceId': random_string(), 'expirationTime': now + 300, 'reservationId': random_string() } mock_token_table.put_item(Item=expired_token_1) mock_token_table.put_item(Item=expired_token_2) mock_token_table.put_item(Item=valid_token) self.manager._token_table = mock_token_table self.manager._limit_table = mock_limit_table actual_count = self.manager._get_token_count(account_id, now) self.assertEqual(expected_count, actual_count)
def test_get_token_count_no_tokens(self): now = now_utc_sec() account_id = random_string() mock_token_table = create_non_fung_table(self.token_table) mock_limit_table = create_limit_table(self.limit_table) _insert_limit(mock_limit_table, self.resource_name, account_id, self.limit) self.manager._token_table = mock_token_table self.manager._limit_table = mock_limit_table expected_count = 0 actual_count = self.manager._get_token_count(account_id, now) self.assertEqual(expected_count, actual_count)
def test_delete_reservation(self): mock_token_table = create_non_fung_table(self.table_name) reserve = TokenReservation(self.res_id, mock_token_table, self.resource_name, self.account_id, self.coordinate) _insert_reservation(mock_token_table, reserve) response = mock_token_table.query(KeyConditionExpression=Key( 'resourceCoordinate').eq(self.coordinate)) self.assertEqual(1, response['Count']) reserve.delete() response = mock_token_table.query(KeyConditionExpression=Key( 'resourceCoordinate').eq(self.coordinate)) self.assertEqual(0, response['Count'])
def test_delete_no_id_from_processor(self): event_source = random_string() event = {'source': event_source} mock_processor = Mock() mock_processor.source = event_source mock_processor.type = None mock_processor.test_and_get_id = MagicMock(return_value=None) mock_table = create_non_fung_table(self.table_name, self.index_name) self._insert_token(mock_table) self.assertEqual(1, self._get_resource_id_count(mock_table)) manager = EventProcessorManager(table_name=self.table_name, index_name=self.index_name, processors=[mock_processor]) manager._table = mock_table manager.process_event(event) self.assertEqual(1, self._get_resource_id_count(mock_table))