-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from digital-asset/python-single-sandbox-tests-2
python: Move the remaining unit tests off of starting their own Sandbox
- Loading branch information
Showing
17 changed files
with
228 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- UploadTest.daml | ||
-- | ||
-- This is ONLY used to test DAR uploading functionality. The test that uploads | ||
-- this DAR assumes it has not been uploaded by any other process before, so it | ||
-- should only be used for that one test. | ||
daml 1.2 | ||
|
||
module UploadTest where | ||
|
||
template XYZ | ||
with | ||
party: Party | ||
where | ||
signatory party |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
sdk-version: 0.13.32 | ||
name: upload-test | ||
version: 1.0.0 | ||
source: UploadTest.daml | ||
parties: | ||
- Alice | ||
- Bob | ||
dependencies: | ||
- daml-prim | ||
- daml-stdlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from asyncio import set_event_loop, new_event_loop | ||
from threading import Thread | ||
|
||
from dazl import Party, Network | ||
from dazl.model.core import Dar | ||
|
||
|
||
def blocking_setup(url: str, dar: Dar) -> 'Party': | ||
""" | ||
Set up a ledger for a test in a completely blocking fashion. | ||
Used by the tests that test the thread-safe variants of the dazl API where | ||
avoiding contamination of the current async context is more important than | ||
the performance ramifications of calling this function. | ||
:param url: | ||
The URL of the remote Ledger API implementation to connect to. | ||
:param dar: | ||
A DAR file. | ||
:return: | ||
A newly allocated ``Party`` that is guaranteed to be used by no other | ||
client. | ||
""" | ||
return Setup(url, dar).run() | ||
|
||
|
||
class Setup: | ||
def __init__(self, url, dar): | ||
self.url = url | ||
self.party = None | ||
self.dar = dar | ||
self.network = None | ||
|
||
def run(self): | ||
# upload our DAR and allocate our Party in a completely separate thread as to try to avoid | ||
# polluting the current context | ||
t = Thread(target=self._main) | ||
t.start() | ||
t.join() | ||
return self.party | ||
|
||
def _main(self): | ||
# create a private event loop just for us | ||
set_event_loop(new_event_loop()) | ||
|
||
self.network = Network() | ||
self.network.set_config(url=self.url) | ||
|
||
client = self.network.aio_new_party() | ||
|
||
self.party = client.party | ||
|
||
self.network.run_until_complete(self.upload_dar()) | ||
|
||
async def upload_dar(self): | ||
await self.network.aio_global().ensure_dar(self.dar) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,64 @@ | ||
# Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from asyncio import new_event_loop, set_event_loop, sleep | ||
import pytest | ||
from asyncio import sleep | ||
|
||
from dazl import Network, sandbox | ||
from .dars import AllKindsOf | ||
from dazl import Network | ||
from .dars import UploadTest | ||
|
||
|
||
def test_dar_uploads_near_startup(): | ||
set_event_loop(new_event_loop()) | ||
|
||
@pytest.mark.asyncio | ||
async def test_dar_uploads_near_startup(sandbox): | ||
package_ids = [] | ||
|
||
with sandbox([]) as proc: | ||
network = Network() | ||
network.set_config(url=proc.url) | ||
network = Network() | ||
network.set_config(url=sandbox) | ||
|
||
async def upload_dars_and_verify(): | ||
await upload_test_dars(network) | ||
metadata = await network.aio_global().metadata() | ||
package_ids.extend(metadata.store.package_ids()) | ||
async def upload_dars_and_verify(): | ||
await upload_test_dars(network) | ||
metadata = await network.aio_global().metadata() | ||
package_ids.extend(metadata.store.package_ids()) | ||
|
||
network.run_until_complete(upload_dars_and_verify()) | ||
await network.aio_run(upload_dars_and_verify(), keep_open=False) | ||
|
||
# Because we use a single sandbox process, it's somewhat difficult to assert that the specific | ||
# DAR we are attempting to upload has indeed been uploaded, because packages are global and | ||
# other tests will upload packages as well. However, we know that we HAVE indeed uploaded | ||
# SOMETHING, and the Sandbox tests are started without any packages at all. So assume that a | ||
# non-zero package ID list means that DAR uploading works. | ||
assert len(package_ids) > 0 | ||
|
||
|
||
def test_package_events(): | ||
set_event_loop(new_event_loop()) | ||
|
||
@pytest.mark.asyncio | ||
async def test_package_events(sandbox): | ||
initial_events = [] | ||
follow_up_events = [] | ||
|
||
with sandbox([]) as proc: | ||
network = Network() | ||
network.set_config(url=proc.url) | ||
client = network.aio_party('TestParty') | ||
network = Network() | ||
network.set_config(url=sandbox) | ||
client = network.aio_new_party() | ||
|
||
async def upload_dars_and_verify(): | ||
# make sure the client is "ready" before uploading DARs, because we are explicitly | ||
# checking to make sure proper reporting of packages that are uploaded after a | ||
# client is running and # operational | ||
await client.ready() | ||
await upload_test_dars(network) | ||
|
||
async def upload_dars_and_verify(): | ||
# make sure the client is "ready" before uploading DARs, because we are explicitly | ||
# checking to make sure proper reporting of packages that are uploaded after a | ||
# client is running and # operational | ||
await client.ready() | ||
await upload_test_dars(network) | ||
# give the client some time to pick up the new packages; unfortunately there isn't | ||
# much more to do here except wait | ||
await sleep(10) | ||
|
||
# give the client some time to pick up the new packages; unfortunately there isn't | ||
# much more to do here except wait | ||
await sleep(10) | ||
client.add_ledger_packages_added(lambda _: initial_events.append(_), initial=True) | ||
client.add_ledger_packages_added(lambda _: follow_up_events.append(_)) | ||
|
||
client.add_ledger_packages_added(lambda _: initial_events.append(_), initial=True) | ||
client.add_ledger_packages_added(lambda _: follow_up_events.append(_)) | ||
network.run_until_complete(upload_dars_and_verify()) | ||
await network.aio_run(upload_dars_and_verify(), keep_open=False) | ||
|
||
assert len(initial_events) == 2 | ||
assert len(follow_up_events) == 1 | ||
|
||
|
||
async def upload_test_dars(network: 'Network'): | ||
g = network.aio_global() | ||
await g.ensure_dar(AllKindsOf.read_bytes()) | ||
await g.ensure_dar(UploadTest) |
Oops, something went wrong.