public async Task TestGetEnvVar(TargetFrameworkMoniker tfm)
        {
            const string VariableDoesNotExist = "SomeEnvVarThatIsNotSet";
            await TestHostHelper.CreateCollectionRulesHost(
                outputHelper : _outputHelper,
                setup : (Tools.Monitor.RootOptions rootOptions) =>
            {
                rootOptions.CreateCollectionRule(DefaultRuleName)
                .SetStartupTrigger()
                .AddGetEnvironmentVariableAction(TestAppScenarios.EnvironmentVariables.IncrementVariableName)
                .AddGetEnvironmentVariableAction(VariableDoesNotExist);
            },
                hostCallback : async(Extensions.Hosting.IHost host) =>
            {
                GetEnvironmentVariableOptions getOpts     = ActionTestsHelper.GetActionOptions <GetEnvironmentVariableOptions>(host, DefaultRuleName, 0);
                GetEnvironmentVariableOptions getFailOpts = ActionTestsHelper.GetActionOptions <GetEnvironmentVariableOptions>(host, DefaultRuleName, 1);

                ICollectionRuleActionOperations actionOperationsService = host.Services.GetService <ICollectionRuleActionOperations>();
                Assert.True(actionOperationsService.TryCreateFactory(KnownCollectionRuleActions.GetEnvironmentVariable, out ICollectionRuleActionFactoryProxy getFactory));

                EndpointInfoSourceCallback endpointInfoCallback = new(_outputHelper);
                await using ServerSourceHolder sourceHolder     = await _endpointUtilities.StartServerAsync(endpointInfoCallback);

                AppRunner runner    = _endpointUtilities.CreateAppRunner(sourceHolder.TransportName, tfm);
                runner.ScenarioName = TestAppScenarios.EnvironmentVariables.Name;

                Task <IEndpointInfo> newEndpointInfoTask = endpointInfoCallback.WaitAddedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);

                await runner.ExecuteAsync(async() =>
                {
                    IEndpointInfo endpointInfo = await newEndpointInfoTask;

                    ICollectionRuleAction getAction = getFactory.Create(endpointInfo, getOpts);

                    await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.IncVar);
                    Assert.Equal("1", await runner.GetEnvironmentVariable(TestAppScenarios.EnvironmentVariables.IncrementVariableName, CommonTestTimeouts.EnvVarsTimeout));

                    await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.IncVar);
                    Assert.Equal("2", await runner.GetEnvironmentVariable(TestAppScenarios.EnvironmentVariables.IncrementVariableName, CommonTestTimeouts.EnvVarsTimeout));

                    CollectionRuleActionResult result = await ActionTestsHelper.ExecuteAndDisposeAsync(getAction, CommonTestTimeouts.EnvVarsTimeout);
                    Assert.Equal("2", result.OutputValues[CollectionRuleActionConstants.EnvironmentVariableValueName]);

                    await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.IncVar);
                    Assert.Equal("3", await runner.GetEnvironmentVariable(TestAppScenarios.EnvironmentVariables.IncrementVariableName, CommonTestTimeouts.EnvVarsTimeout));

                    ICollectionRuleAction getActionFailure = getFactory.Create(endpointInfo, getFailOpts);
                    CollectionRuleActionException thrownEx = await Assert.ThrowsAsync <CollectionRuleActionException>(async() =>
                    {
                        await ActionTestsHelper.ExecuteAndDisposeAsync(getActionFailure, CommonTestTimeouts.EnvVarsTimeout);
                    });
                    Assert.Contains(VariableDoesNotExist, thrownEx.Message);

                    await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.ShutdownScenario);
                });
            });
        }
Exemplo n.º 2
0
        public async Task ServerSourceNoPruneDuringDumpTest(TargetFrameworkMoniker appTfm)
        {
            EndpointInfoSourceCallback callback     = new(_outputHelper);
            var             operationTrackerService = new OperationTrackerService();
            MockDumpService dumpService             = new(operationTrackerService);

            await using ServerSourceHolder sourceHolder = await _endpointUtilities.StartServerAsync(callback, dumpService, operationTrackerService);

            AppRunner runner = _endpointUtilities.CreateAppRunner(sourceHolder.TransportName, appTfm);

            Task <IEndpointInfo> addedEndpointTask   = callback.WaitAddedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);
            Task <IEndpointInfo> removedEndpointTask = callback.WaitRemovedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);

            Task <Stream> dumpTask     = null;
            IEndpointInfo endpointInfo = null;

            await runner.ExecuteAsync(async() =>
            {
                _outputHelper.WriteLine("Waiting for added endpoint notification.");
                endpointInfo = await addedEndpointTask;
                _outputHelper.WriteLine("Received added endpoint notifications.");

                // Start a dump operation; the process should not be pruned until the operation is completed.
                dumpTask = dumpService.DumpAsync(endpointInfo, DumpType.Triage, CancellationToken.None);

                await runner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
            });

            // At this point, the process should no longer exist; but since the mock dump operation is
            // in progress, the ServiceEndpointInfoSource should not prune the process until the operation
            // is complete.

            int processId = await runner.ProcessIdTask;

            // Test that the process still exists.
            Assert.False(removedEndpointTask.IsCompleted);
            IEnumerable <IEndpointInfo> endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            endpointInfo = Assert.Single(endpointInfos);
            Assert.Equal(processId, endpointInfo.ProcessId);

            // Signal and wait for mock dump operation to complete.
            dumpService.CompleteOperation();
            await dumpTask;

            // Process should no longer exist
            endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            Assert.Empty(endpointInfos);

            // Test that process removal sent notification
            endpointInfo = await removedEndpointTask;
            Assert.Equal(processId, endpointInfo.ProcessId);
        }
Exemplo n.º 3
0
        public async Task ServerSourceAddRemoveSingleConnectionTest(TargetFrameworkMoniker appTfm)
        {
            EndpointInfoSourceCallback callback = new(_outputHelper);

            await using ServerSourceHolder sourceHolder = await _endpointUtilities.StartServerAsync(callback);

            var endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            Assert.Empty(endpointInfos);

            AppRunner runner = _endpointUtilities.CreateAppRunner(sourceHolder.TransportName, appTfm);

            Task addedEndpointTask   = callback.WaitAddedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);
            Task removedEndpointTask = callback.WaitRemovedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);

            await runner.ExecuteAsync(async() =>
            {
                _outputHelper.WriteLine("Waiting for added endpoint notification.");
                await addedEndpointTask;
                _outputHelper.WriteLine("Received added endpoint notifications.");

                endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

                var endpointInfo = Assert.Single(endpointInfos);

                ValidateEndpointInfo(endpointInfo);

                await EndpointUtilities.VerifyConnectionAsync(runner, endpointInfo);

                await runner.SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
            });

            _outputHelper.WriteLine("Waiting for removed endpoint notification.");
            await removedEndpointTask;

            _outputHelper.WriteLine("Received removed endpoint notifications.");

            endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            Assert.Empty(endpointInfos);
        }
        public async Task TestSetEnvVar(TargetFrameworkMoniker tfm)
        {
            await TestHostHelper.CreateCollectionRulesHost(
                outputHelper : _outputHelper,
                setup : (Tools.Monitor.RootOptions rootOptions) =>
            {
                rootOptions.CreateCollectionRule(DefaultRuleName)
                .SetStartupTrigger()
                .AddSetEnvironmentVariableAction(DefaultVarName, DefaultVarValue);
            },
                hostCallback : async(Extensions.Hosting.IHost host) =>
            {
                SetEnvironmentVariableOptions setOpts = ActionTestsHelper.GetActionOptions <SetEnvironmentVariableOptions>(host, DefaultRuleName, 0);

                ICollectionRuleActionOperations actionOperationsService = host.Services.GetService <ICollectionRuleActionOperations>();
                Assert.True(actionOperationsService.TryCreateFactory(KnownCollectionRuleActions.SetEnvironmentVariable, out ICollectionRuleActionFactoryProxy setFactory));

                EndpointInfoSourceCallback endpointInfoCallback = new(_outputHelper);
                await using ServerSourceHolder sourceHolder     = await _endpointUtilities.StartServerAsync(endpointInfoCallback);

                AppRunner runner    = _endpointUtilities.CreateAppRunner(sourceHolder.TransportName, tfm);
                runner.ScenarioName = TestAppScenarios.EnvironmentVariables.Name;

                Task <IEndpointInfo> newEndpointInfoTask = endpointInfoCallback.WaitAddedEndpointInfoAsync(runner, CommonTestTimeouts.StartProcess);

                await runner.ExecuteAsync(async() =>
                {
                    IEndpointInfo endpointInfo = await newEndpointInfoTask;

                    ICollectionRuleAction setAction = setFactory.Create(endpointInfo, setOpts);

                    await ActionTestsHelper.ExecuteAndDisposeAsync(setAction, CommonTestTimeouts.EnvVarsTimeout);

                    Assert.Equal(DefaultVarValue, await runner.GetEnvironmentVariable(DefaultVarName, CommonTestTimeouts.EnvVarsTimeout));

                    await runner.SendCommandAsync(TestAppScenarios.EnvironmentVariables.Commands.ShutdownScenario);
                });
            });
        }
Exemplo n.º 5
0
        public async Task ServerSourceAddRemoveMultipleConnectionTest(TargetFrameworkMoniker appTfm)
        {
            EndpointInfoSourceCallback callback = new(_outputHelper);

            await using ServerSourceHolder sourceHolder = await _endpointUtilities.StartServerAsync(callback);

            var endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            Assert.Empty(endpointInfos);

            const int appCount = 5;

            AppRunner[] runners              = new AppRunner[appCount];
            Task[]      addedEndpointTasks   = new Task[appCount];
            Task[]      removedEndpointTasks = new Task[appCount];

            // Start all app instances
            for (int i = 0; i < appCount; i++)
            {
                runners[i]              = _endpointUtilities.CreateAppRunner(sourceHolder.TransportName, appTfm, appId: i + 1);
                addedEndpointTasks[i]   = callback.WaitAddedEndpointInfoAsync(runners[i], CommonTestTimeouts.StartProcess);
                removedEndpointTasks[i] = callback.WaitRemovedEndpointInfoAsync(runners[i], CommonTestTimeouts.StartProcess);
            }

            await runners.ExecuteAsync(async() =>
            {
                _outputHelper.WriteLine("Waiting for all added endpoint notifications.");
                await Task.WhenAll(addedEndpointTasks);
                _outputHelper.WriteLine("Received all added endpoint notifications.");

                endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

                Assert.Equal(appCount, endpointInfos.Count());

                for (int i = 0; i < appCount; i++)
                {
                    int processId = await runners[i].ProcessIdTask;

                    IEndpointInfo endpointInfo = endpointInfos.FirstOrDefault(info => info.ProcessId == processId);

                    ValidateEndpointInfo(endpointInfo);

                    await EndpointUtilities.VerifyConnectionAsync(runners[i], endpointInfo);

                    await runners[i].SendCommandAsync(TestAppScenarios.AsyncWait.Commands.Continue);
                }
            });

            _outputHelper.WriteLine("Waiting for all removed endpoint notifications.");
            await Task.WhenAll(removedEndpointTasks);

            _outputHelper.WriteLine("Received all removed endpoint notifications.");

            for (int i = 0; i < appCount; i++)
            {
                Assert.True(0 == runners[i].ExitCode, $"App {i} exit code is non-zero.");
            }

            await Task.Delay(TimeSpan.FromSeconds(1));

            endpointInfos = await _endpointUtilities.GetEndpointInfoAsync(sourceHolder.Source);

            Assert.Empty(endpointInfos);
        }