Exemplo n.º 1
0
 public void PreInitializeIsNoop()
 {
     using (var tempFiles = new TemporaryFiles(MockBuilder.StatelessServiceContext(),
                                               NullLogger <TemporaryFiles> .Instance))
     {
         string testPath = tempFiles.GetFilePath("asdfpoiu");
         string parent   = Path.GetDirectoryName(testPath);
         Directory.Exists(parent).Should().BeFalse();
     }
 }
 public void InitializeCreatedRoot()
 {
     using (var tempFiles = new TemporaryFiles(MockBuilder.StatelessServiceContext(),
                                               NullLogger <TemporaryFiles> .Instance))
     {
         tempFiles.Initialize();
         string testPath = tempFiles.GetFilePath("asdfpoiu");
         string parent   = Path.GetDirectoryName(testPath);
         Assert.True(Directory.Exists(parent));
     }
 }
Exemplo n.º 3
0
 private static void CleanTempFolderForTest()
 {
     using (var tempFiles = new TemporaryFiles(MockBuilder.StatelessServiceContext(),
                                               NullLogger <TemporaryFiles> .Instance))
     {
         tempFiles.Initialize();
         string testPath = tempFiles.GetFilePath("asdfpoiu");
         string parent   = Path.GetDirectoryName(testPath);
         if (Directory.Exists(parent))
         {
             Directory.Delete(parent);
         }
     }
 }
        public void InterceptCreatesScope()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);


            Thing innerThing = null;

            var collection = new ServiceCollection();

            collection.AddSingleton(client);
            collection.AddScoped <IFakeService, FakeService>();
            MockBuilder.RegisterStatelessServiceContext(collection);
            collection.AddScoped <Thing>();
            collection.AddSingleton <Action <Thing> >(t => innerThing = t);
            ServiceProvider provider = collection.BuildServiceProvider();

            var outerThing = provider.GetRequiredService <Thing>();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                (object)null,
                new InvokeInNewScopeInterceptor <IFakeService>(provider));

            impl.TestServiceMethod().Should().Be(FakeService.RetValue);
            FakeService.Calls.Should().Be(1);
            innerThing.Should().NotBeNull();

            // It's supposed to be a new scope, so it should have gotten a different thing
            innerThing.Should().NotBe(outerThing);

            client.Flush();
            telemetryChannel.Telemetry.OfType <EventTelemetry>().Count(e => e.Name == "TestEvent").Should().Be(1);
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            requestTelemetries.Should().ContainSingle();
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            requestTelemetry.Name.Should().NotBeNull();
            (requestTelemetry.Success ?? true).Should().BeTrue();
            requestTelemetry.Name.Should().Contain("IFakeService");
            requestTelemetry.Name.Should().ContainEquivalentOf("service://TestName");

            telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().Should().BeEmpty();
        }
Exemplo n.º 5
0
        public void DisposeCleansUp()
        {
            string parent;

            using (var tempFiles = new TemporaryFiles(MockBuilder.StatelessServiceContext(),
                                                      NullLogger <TemporaryFiles> .Instance))
            {
                tempFiles.Initialize();
                string testPath = tempFiles.GetFilePath("asdfpoiu");
                File.WriteAllText(testPath, "Test content");
                parent = Path.GetDirectoryName(testPath);
            }

            Directory.Exists(parent).Should().BeFalse();
        }
        public void ComplexAggregateExceptionIsReported()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod())
            .Throws(new AggregateException(new InvalidOperationException("Test exception text"),
                                           new InvalidOperationException("Another test exception text")));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                service.Object,
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            var invocationException = Assert.Throws <AggregateException>(() => impl.TestServiceMethod());

            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            Assert.Single(dependencyTelemetries);
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            Assert.False(dependencyTelemetry.Success);
            Assert.Equal("ServiceFabricRemoting", dependencyTelemetry.Type);
            Assert.Equal("other://uri.test", dependencyTelemetry.Target);
            Assert.StartsWith("other://uri.test", dependencyTelemetry.Data);
            Assert.Contains(nameof(IFakeService), dependencyTelemetry.Data, StringComparison.OrdinalIgnoreCase);
            Assert.Contains(nameof(IFakeService.TestServiceMethod),
                            dependencyTelemetry.Data,
                            StringComparison.OrdinalIgnoreCase);

            List <ExceptionTelemetry> exceptionTelemetries =
                telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            Assert.Single(exceptionTelemetries);
            ExceptionTelemetry exceptionTelemetry = exceptionTelemetries[0];

            Assert.Same(invocationException, exceptionTelemetry.Exception);
        }
        public void ComplexAggregateExceptionIsReported()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod())
            .Throws(new AggregateException(new InvalidOperationException("Test exception text"),
                                           new InvalidOperationException("Another test exception text")));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                service.Object,
                new LoggingServiceProxyInterceptor(client, ctx, "other://uri.test"));

            var invocationException = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <AggregateException>().Which;

            client.Flush();
            List <DependencyTelemetry> dependencyTelemetries =
                telemetryChannel.Telemetry.OfType <DependencyTelemetry>().ToList();

            dependencyTelemetries.Should().ContainSingle();
            DependencyTelemetry dependencyTelemetry = dependencyTelemetries[0];

            dependencyTelemetry.Success.Should().BeFalse();
            dependencyTelemetry.Type.Should().Be("ServiceFabricRemoting");
            dependencyTelemetry.Target.Should().Be("other://uri.test");
            dependencyTelemetry.Data.Should().StartWith("other://uri.test");
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService));
            dependencyTelemetry.Data.Should().Contain(nameof(IFakeService.TestServiceMethod));

            List <ExceptionTelemetry> exceptionTelemetries =
                telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().ToList();

            exceptionTelemetries.Should().ContainSingle();
            ExceptionTelemetry exceptionTelemetry = exceptionTelemetries[0];

            exceptionTelemetry.Exception.Should().BeSameAs(invocationException);
        }
        public void InterceptCatchesExceptions()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);


            var service = new Mock <IFakeService>();

            service.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var collection = new ServiceCollection();

            collection.AddSingleton(client);
            MockBuilder.RegisterStatelessServiceContext(collection);
            collection.AddSingleton(service.Object);
            ServiceProvider provider = collection.BuildServiceProvider();

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                (object)null,
                new InvokeInNewScopeInterceptor <IFakeService>(provider));

            var ex = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <InvalidOperationException>().Which;

            ex.Message.Should().Be("Test Exception Text");

            client.Flush();

            RequestTelemetry requestTelemetry = telemetryChannel.Telemetry.OfType <RequestTelemetry>().FirstOrDefault();

            requestTelemetry.Should().NotBeNull();
            requestTelemetry.Success.Should().BeFalse();
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            exceptionTelemetry.Should().NotBeNull();
            exceptionTelemetry.Exception.Should().BeSameAs(ex);
        }
Exemplo n.º 9
0
        public void CleanupResilientToOpenHandles()
        {
            StreamWriter writer = null;

            try
            {
                string parent;
                using (var tempFiles = new TemporaryFiles(MockBuilder.StatelessServiceContext(),
                                                          NullLogger <TemporaryFiles> .Instance))
                {
                    tempFiles.Initialize();
                    string testPath = tempFiles.GetFilePath("asdfpoiu");
                    writer = File.CreateText(testPath);
                    parent = Path.GetDirectoryName(testPath);
                }

                Directory.Exists(parent).Should().BeTrue();
            }
            finally
            {
                writer?.Dispose();
            }
        }
Exemplo n.º 10
0
        public void ExceptionLogsFailedRequest()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            Mock <IFakeService> fakeService = new Mock <IFakeService>();

            fakeService.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                fakeService.Object,
                new LoggingServiceInterceptor(ctx, client));

            var ex = (((Func <object>)(() => impl.TestServiceMethod()))).Should().Throw <InvalidOperationException>().Which;

            ex.Message.Should().Be("Test Exception Text");

            client.Flush();
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            requestTelemetries.Should().ContainSingle();
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            requestTelemetry.Success.Should().BeFalse();
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            exceptionTelemetry.Should().NotBeNull();
            exceptionTelemetry.Exception.Should().BeSameAs(ex);
        }
Exemplo n.º 11
0
        public void ExceptionLogsFailedRequest()
        {
            var telemetryChannel = new FakeChannel();
            var config           = new TelemetryConfiguration("00000000-0000-0000-0000-000000000001", telemetryChannel);
            var client           = new TelemetryClient(config);

            StatelessServiceContext ctx = MockBuilder.StatelessServiceContext();

            Mock <IFakeService> fakeService = new Mock <IFakeService>();

            fakeService.Setup(s => s.TestServiceMethod()).Throws(new InvalidOperationException("Test Exception Text"));

            var gen  = new ProxyGenerator();
            var impl = (IFakeService)gen.CreateInterfaceProxyWithTargetInterface(
                typeof(IFakeService),
                new Type[0],
                fakeService.Object,
                new LoggingServiceInterceptor(ctx, client));

            var ex = Assert.ThrowsAny <InvalidOperationException>(() => impl.TestServiceMethod());

            Assert.Equal("Test Exception Text", ex.Message);

            client.Flush();
            List <RequestTelemetry> requestTelemetries =
                telemetryChannel.Telemetry.OfType <RequestTelemetry>().ToList();

            Assert.Single(requestTelemetries);
            RequestTelemetry requestTelemetry = requestTelemetries[0];

            Assert.False(requestTelemetry.Success);
            ExceptionTelemetry exceptionTelemetry = telemetryChannel.Telemetry.OfType <ExceptionTelemetry>().FirstOrDefault();

            Assert.NotNull(exceptionTelemetry);
            Assert.Same(ex, exceptionTelemetry.Exception);
        }