<@U84SY03DF> <@UB3BGTV63> I'm trying to understand...
# python
w
@big-piano-35669 @white-balloon-205 I'm trying to understand the event loop especially when writing Python test. If I execute
my_resource.some_output.apply(lambda val: print(val))
, I get back another output that I believe has a future wrapped up in it that will execute my lambda. When does that actually get awaited?
w
When the value of
some_output
is available (either after a resource is created or updated) the Pulumi engine will cause that future to be resolved with the value of the resource returned by the cloud provider.
w
Ahh - sorry - missed that "test" part of your question. In unit tests, resource outputs are never resolved, except for outputs that are the same name as inputs - which will be resolved to the value of the input. See https://github.com/pulumi/pulumi/pull/2638#issuecomment-483809925 for some additional commentary on this.
w
that makes sense — but our problem is with Python specifically. tests will hang if there is an async test and a non-async test in the same file. we are using pytest-asyncio
in particular, this seems to happen if our infrastructure is imported more than once in a file — we import it in every test function as shown in Joe's example
e.g., the second test here will hang:
Copy code
def test_bucket_name_is_my_bucket():
    from infrastructure import bucket_name

    assert bucket_name() == "my-bucket"


@pytest.mark.asyncio
async def test_bucket_urn_is_my_bucket():
    from infrastructure import bucket

    bucket_urn = await get_output(bucket.urn)

    assert bucket_urn.endswith("aws:s3/bucket:Bucket::my-bucket")


def get_output(output):
    future = asyncio.Future()
    output.apply(lambda value: future.set_result(value))
    return future
but if i remove the first test, the async test runs with no problem.