public void InvocationShouldAlwaysTriggerLaunchWhenMethodLaunchModeSetToAlways()
        {
            Task <GreetingResponse> HandleAsync(GreetingRequest greetingRequest, MethodCallContext context)
            {
                return(Task.FromResult(new GreetingResponse {
                    Greeting = greetingRequest.Name
                }));
            }

            RunWith10SecTimeout(async() =>
            {
                var serverCreatedCount = 0;
                var echoServerFactory  = new TestClientFactory(
                    (broker, id) =>
                {
                    var optionsBuilder = new ClientOptionsBuilder()
                                         .WithBrokerWorkingDir(_testBrokerFixture.SharedInstance.WorkingDir)
                                         .WithAppInstanceId(id)
                                         .WithApplicationId(EchoServerClient.Id)
                                         .WithDefaultConfiguration()
                                         .WithProvidedService(
                        GreetingService.Id,
                        "AlwaysLaunchGreetingService",
                        x => x.WithUnaryMethod <GreetingRequest, GreetingResponse>("Hello", HandleAsync));

                    serverCreatedCount++;

                    return(Task.FromResult(ClientFactory.Instance.Create(optionsBuilder.Build())));
                });

                var appLauncher = RegisterDisposable(
                    new TestAppLauncher(
                        _testBrokerFixture.SharedInstance,
                        new Dictionary <string, TestClientFactory>
                {
                    { EchoServerClient.Id, echoServerFactory }
                }
                        )
                    );
                await appLauncher.StartAsync();
                ConnectEchoServer();
                var client = new EchoClient(s => s.WithBrokerWorkingDir(_testBrokerFixture.SharedInstance.WorkingDir));
                client.ConnectAsync().ShouldCompleteIn(Timeout5Sec);
                var callDescriptor = new MethodCallDescriptor(
                    ProvidedMethodReference.Create(GreetingService.Id, "AlwaysLaunchGreetingService", "Hello", EchoServerClient.Id));
                await client.CallInvoker.CallUnary(callDescriptor, new GreetingRequest {
                    Name = "Test"
                });
                await client.CallInvoker.CallUnary(callDescriptor, new GreetingRequest {
                    Name = "Test"
                });
                serverCreatedCount.ShouldBe(2);
            });
        }
        public void InvocationShouldTriggerLaunchWithSingleInstanceModeByDefault()
        {
            var serverInvokedCount = 0;

            Task <GreetingResponse> HandleAsync(GreetingRequest greetingRequest, MethodCallContext context)
            {
                Interlocked.Increment(ref serverInvokedCount);
                return(Task.FromResult(new GreetingResponse {
                    Greeting = greetingRequest.Name
                }));
            }

            RunWith10SecTimeout(async() =>
            {
                var serverCreatedCount = 0;
                var echoServerFactory  = new TestClientFactory(
                    (broker, id) =>
                {
                    WriteLog("Launching server on demand");

                    var optionsBuilder = new ClientOptionsBuilder()
                                         .WithBrokerWorkingDir(_testBrokerFixture.SharedInstance.WorkingDir)
                                         .WithAppInstanceId(id)
                                         .WithApplicationId(EchoServerClient.Id)
                                         .WithDefaultConfiguration()
                                         .WithProvidedService(
                        GreetingService.Id,
                        x => x.WithUnaryMethod <GreetingRequest, GreetingResponse>("Hello", HandleAsync));

                    serverCreatedCount++;

                    return(ClientFactory.Instance.Create(optionsBuilder.Build()));
                });

                var appLauncher = RegisterDisposable(
                    new TestAppLauncher(
                        _testBrokerFixture.SharedInstance,
                        new Dictionary <string, TestClientFactory>
                {
                    { EchoServerClient.Id, echoServerFactory }
                }
                        )
                    );
                await appLauncher.StartAsync();
                var client = new EchoClient(s => s.WithBrokerWorkingDir(_testBrokerFixture.SharedInstance.WorkingDir));
                client.ConnectAsync().ShouldCompleteIn(Timeout5Sec);
                var callDescriptor = new MethodCallDescriptor(
                    ProvidedMethodReference.Create(GreetingService.Id, "Hello", EchoServerClient.Id));
                var call1 = client.CallInvoker.CallUnary(callDescriptor, new GreetingRequest {
                    Name = "Test"
                });
                var call2 = client.CallInvoker.CallUnary(callDescriptor, new GreetingRequest {
                    Name = "Test"
                });
                await Task.WhenAny(call1.AsTask(), call2.AsTask());
                WriteLog("Call 1 completed");
                await Task.WhenAll(call1.AsTask(), call2.AsTask());
                WriteLog("Call 2 completed");
                serverCreatedCount.ShouldBe(1);
                serverInvokedCount.ShouldBe(2);
            });
        }