def test_select_reflects_archive_events(self): notification_count = 3 # we expect that, upon each on_created notification of an OperatorNotification contract, # when we query the ACS, we get precisely the same number of contracts. expected_select_count = notification_count * notification_count actual_select_count = 0 def on_notification_contract(_, __): nonlocal actual_select_count actual_select_count += len( party_client.select(OperatorNotification)) with sandbox(DAML_FILE) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: create( OperatorRole, {'operator': PARTY})) party_client.on_created( OperatorRole, lambda cid, cdata: exercise( cid, 'PublishMany', dict(count=3))) party_client.on_created( OperatorNotification, lambda cid, _: exercise(cid, 'Archive')) party_client.on_created(OperatorNotification, on_notification_contract) client.run_until_complete() final_select_count = len( party_client.select(OperatorNotification)) self.assertEqual(actual_select_count, expected_select_count) self.assertEqual(0, final_select_count)
def run(self, url): with create_client(participant_url=url, parties=ALL_PARTIES) as client_manager: operator = client_manager.client(OPERATOR_PARTY) operator.on_ready(lambda *args, **kwargs: create( Simple.OperatorRole, {'operator': OPERATOR_PARTY})) operator.on_created(Simple.OperatorRole, self.on_operator) operator.on_created(Simple.OperatorNotification, self.on_notification) client_manager.run_until_complete(asyncio.sleep(15))
def run_app(url: str): client_role_dicts = get_client_role_data() all_parties = tuple(c["client"] for c in client_role_dicts) + ("Processor",) with create_client(participant_url=url, parties=all_parties) as client_mgr: for client_role_dict in client_role_dicts: client = client_mgr.client(client_role_dict["client"]) client.on_ready(lambda *args, **kwargs: create(ClientRole, client_role_dict)) register_processor_callbacks("Processor", client_mgr.client("Processor"))
def test_maps(self): with sandbox(TEST_DAML) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: create( 'AllKindsOf.MappyContract', { 'operator': PARTY, 'value': {'Map_internal': []} })) client.run_until_complete()
def run(self, url): with create_client(participant_url=url, parties=ALL_PARTIES) as client_manager: operator = client_manager.client(OPERATOR_PARTY) operator.on_ready(self.on_ready) operator.on_created(Simple.OperatorNotification, self.on_notification_created) operator.on_archived(Simple.OperatorNotification, self.on_notification_archived) client_manager.run_until_complete() self.events.append(('finished', (), ()))
def test_select_unknown_template_retrieves_empty_set(self): with sandbox(DAML_FILE) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: create( OperatorRole, {'operator': PARTY})) client.run_until_complete() data = party_client.select('NonExistentTemplate') self.assertEqual(len(data), 0)
def test_duplicate_ledger(self): """ Main method. """ with sandbox(TEMPLATE_DAML_FILE) as damli_proc_1, \ sandbox(TEMPLATE_DAML_FILE) as damli_proc_2, \ create_client(participant_url=damli_proc_1.url, parties=['POSTMAN'] + [m['party_name'] for m in MEMBERS]) as client_1, \ create_client(participant_url=damli_proc_2.url, parties=['POSTMAN'] + [m['party_name'] for m in MEMBERS]) as client_2: app = set_up() register_application(client_1, app) register_application(client_2, app) # Expect program that is expected to terminate. It will be considered done # only once all pending commands have been completed (including any of their # follow-ups) event_loop = asyncio.get_event_loop() event_loop.run_until_complete( asyncio.gather(client_1.main(False), client_2.main(False))) LOG.info('Application finished.')
def test_ssl_connectivity(self): client_ssl_settings, server_ssl_settings = create_ssl_test_package() messages_received = [] with sandbox(TEMPLATE_DAML_FILE, ssl_settings=server_ssl_settings) as proc: with create_client(participant_url=proc.url, parties=['SOME_PARTY'], ca_file=client_ssl_settings.ca_file, cert_file=client_ssl_settings.cert_file, cert_key_file=client_ssl_settings.cert_key_file) as client_mgr: client = client_mgr.client('SOME_PARTY') client.on_ready(lambda *args, **kwargs: create('Main.PostmanRole', dict(party='SOME_PARTY'))) client.on_created('Main.PostmanRole', lambda cid, cdata: messages_received.append(cid)) ledger_run = client_mgr.run_until_complete() self.assertEqual(ledger_run.exit_code, 0) self.assertEqual(len(messages_received), 1)
def _sandbox_test(self, extra_args=None): cids = [] with sandbox(DAML_FILE, extra_args=extra_args) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: create( OperatorRole, {'operator': PARTY})) party_client.on_created(OperatorRole, lambda cid, cdata: cids.append(cid)) client.run_until_complete() print('got to the end with contracts: ', cids) self.assertEqual(len(cids), 1)
def test_select_template_retrieves_contracts(self): seen_notifications = [] with sandbox(DAML_FILE) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_created( OperatorNotification, lambda cid, cdata: seen_notifications.append(cid)) client.run_until_complete(async_test_case(party_client)) data = party_client.select(OperatorNotification) self.assertEqual(len(data), 5) self.assertEqual(len(seen_notifications), 8)
def test_complicated_types(self): recorded_data = dict() with sandbox(DAML_FILE) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: create( Complicated.OperatorRole, {'operator': PARTY})) party_client.on_created(Complicated.OperatorRole, _create_empty_notification) party_client.on_created(Complicated.OperatorRole, _create_complicated_notifications) party_client.on_created( Complicated.OperatorFormulaNotification, partial(setitem, recorded_data)) client.run_until_complete() print('got to the end with contracts: ', recorded_data) self.assertEqual(len(recorded_data), 4)
def test_all_types(self): test_case = AllTypesTestCase() with sandbox(TEST_DAML) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(test_case.create_one_of_everything) party_client.on_created(TEMPLATE, test_case.on_one_of_everything) client.run_until_complete() self.assertIsNotNone( test_case.found_instance, 'Expected to find an instance of OneOfEverything!') self.assertEqual( SOME_ARGS.keys(), test_case.found_instance.keys(), 'There are either extra fields or missing fields!') for key in SOME_ARGS: expected = SOME_ARGS.get(key) actual = test_case.found_instance.get(key) self.assertEqual(expected, actual, f'Failed to compare types for key: {key}')
def test_some_party_receives_public_contract(self): some_party_cids = [] publisher_cids = [] with sandbox(DAML_FILE, extra_args=None) as proc: with create_client(participant_url=proc.url, parties=[SOME_PARTY, PUBLISHER], party_groups=[ALL_PARTY]) as client: some_client = client.client(SOME_PARTY) some_client.on_ready(lambda *args, **kwargs: create(PrivateContract, {'someParty': SOME_PARTY})) publisher_client = client.client(PUBLISHER) publisher_client.on_ready(lambda *args, **kwargs: create(PublicContract, {'publisher': PUBLISHER, 'allParty': ALL_PARTY})) some_client.on_created(PublicContract, lambda cid, cdata: some_party_cids.append(cid)) some_client.on_created(PrivateContract, lambda cid, cdata: some_party_cids.append(cid)) publisher_client.on_created(PublicContract, lambda cid, cdata: publisher_cids.append(cid)) publisher_client.on_created(PrivateContract, lambda cid, cdata: publisher_cids.append(cid)) client.run_until_complete() print(f'got to the end with some_party contracts: {some_party_cids} and publisher contracts: {publisher_cids}') self.assertEqual(len(some_party_cids), 2) self.assertEqual(len(publisher_cids), 1)
def test_select_template_retrieves_contracts(self): number_of_contracts = 10 with sandbox(DAML_FILE) as proc: with create_client(participant_url=proc.url, parties=[PARTY]) as client: party_client = client.client(PARTY) party_client.on_ready(lambda *args, **kwargs: [ create(Counter, {'owner': PARTY, 'value': 0}), *[create(AccountRequest, {'owner': PARTY}) for i in range(number_of_contracts)], ]) async def on_account_request(cid, _): counter_cid, counter_cdata = await party_client.select_first(Counter) return [ exercise(cid, 'CreateAccount', dict(accountId=counter_cdata['value'])), exercise(counter_cid, 'Increment') ] party_client.on_created(AccountRequest, on_account_request) client.run_until_complete() data = party_client.select(Account) self.assertEqual(len(data), number_of_contracts)