1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import asyncio
1516import functools
1617from typing import Any , Awaitable , Callable , Dict , List , Optional , Set , Tuple
1718
2526)
2627from pulumi .provider .server import ComponentInitError
2728import pulumi .output
28- from pulumi .provider import ConstructResult
29+ from pulumi .provider import CallResult , ConstructResult
2930from pulumi .provider .provider import Provider
3031import pytest
3132
3637from pulumi .runtime .proto .provider_pb2 import ConstructRequest
3738from pulumi .runtime .proto import status_pb2 , errors_pb2
3839from pulumi .runtime .settings import Settings , configure
40+ from pulumi .runtime import config , settings
3941from semver import VersionInfo as Version
4042from ..grpc_stubs import provider_servicer_stub
4143
@@ -661,6 +663,40 @@ def construct(
661663 )
662664
663665
666+ class ConcurrentConstructCallProvider (Provider ):
667+ def __init__ (self ):
668+ super ().__init__ ("1.0.0" )
669+ self .construct_waiting = asyncio .Event ()
670+ self .call_ran = asyncio .Event ()
671+ self .construct_stack_after_call : Optional [str ] = None
672+ self .construct_config_after_call : Optional [str ] = None
673+
674+ def construct (
675+ self ,
676+ name : str ,
677+ resource_type : str ,
678+ inputs : Inputs ,
679+ options : Optional [ResourceOptions ] = None ,
680+ ) -> ConstructResult :
681+ assert settings .get_stack () == "construct-stack"
682+ assert config .get_config ("test:key" ) == "construct-config"
683+
684+ async def urn () -> str :
685+ self .construct_waiting .set ()
686+ await self .call_ran .wait ()
687+ self .construct_stack_after_call = settings .get_stack ()
688+ self .construct_config_after_call = config .get_config ("test:key" )
689+ return f"urn:pulumi:{ name } ::{ resource_type } ::test-resource"
690+
691+ return ConstructResult (urn = urn (), state = {"result" : "construct" })
692+
693+ def call (self , token : str , args : Inputs ) -> CallResult :
694+ assert settings .get_stack () == "call-stack"
695+ assert config .get_config ("test:key" ) == "call-config"
696+ self .call_ran .set ()
697+ return CallResult ({"result" : "call" })
698+
699+
664700@pytest .mark .asyncio
665701async def test_construct_success ():
666702 provider = MockProvider ()
@@ -674,6 +710,36 @@ async def test_construct_success():
674710 assert "test-resource" in response .urn
675711
676712
713+ @pytest .mark .asyncio
714+ async def test_construct_and_call_can_run_concurrently ():
715+ provider = ConcurrentConstructCallProvider ()
716+ servicer = ProviderServicer (provider , [], "" )
717+
718+ async with provider_servicer_stub (servicer ) as stub :
719+ construct_request = proto .ConstructRequest (
720+ name = "construct" ,
721+ type = "test:index:Component" ,
722+ stack = "construct-stack" ,
723+ config = {"test:key" : "construct-config" },
724+ )
725+ construct_task = asyncio .ensure_future (stub .Construct (construct_request ))
726+
727+ await asyncio .wait_for (provider .construct_waiting .wait (), timeout = 2 )
728+
729+ call_request = proto .CallRequest (
730+ tok = "test:index:call" ,
731+ stack = "call-stack" ,
732+ config = {"test:key" : "call-config" },
733+ )
734+ call_response = await asyncio .wait_for (stub .Call (call_request ), timeout = 2 )
735+ construct_response = await asyncio .wait_for (construct_task , timeout = 2 )
736+
737+ assert getattr (call_response , "return" )["result" ] == "call"
738+ assert construct_response .urn .endswith ("::test-resource" )
739+ assert provider .construct_stack_after_call == "construct-stack"
740+ assert provider .construct_config_after_call == "construct-config"
741+
742+
677743@pytest .mark .asyncio
678744async def test_construct_run_error ():
679745 provider = MockProvider (error_type = "run_error" , error_message = "monitor shut down" )
0 commit comments