public static ITestHost CreateHost(HostType hostType, TransportType transportType, string testName, string url = null)
        {
            ITestHost host =  null;

            string logBasePath = Path.Combine(Directory.GetCurrentDirectory(), "..");

            switch (hostType)
            {
                case HostType.IISExpress:
                    host = new IISExpressTestHost(testName);
                    host.TransportFactory = () => CreateTransport(transportType);
                    host.Transport = host.TransportFactory();
                    break;
                case HostType.External:
                    host = new ExternalTestHost(url);
                    host.TransportFactory = () => CreateTransport(transportType);
                    host.Transport = host.TransportFactory();
                    break;
                case HostType.Memory:
                default: 
                    var mh = new MemoryHost();
                    host = new MemoryTestHost(mh, Path.Combine(logBasePath, testName));
                    host.TransportFactory = () => CreateTransport(transportType, mh);
                    host.Transport = host.TransportFactory();
                    break;
                case HostType.HttpListener:
                    host = new OwinTestHost(Path.Combine(logBasePath, testName));
                    host.TransportFactory = () => CreateTransport(transportType);
                    host.Transport = host.TransportFactory();
                    break;
            }

            var writer = CreateClientTraceWriter(testName);
            host.ClientTraceOutput = writer;

            if (hostType != HostType.Memory && hostType != HostType.External)
            {
                string clientNetworkPath = Path.Combine(logBasePath, testName + ".client.network.log");
                host.Disposables.Add(SystemNetLogging.Enable(clientNetworkPath));

                string httpSysTracePath = Path.Combine(logBasePath, testName + ".httpSys");
                IDisposable httpSysTracing = StartHttpSysTracing(httpSysTracePath);

                // If tracing is enabled then turn it off on host dispose
                if (httpSysTracing != null)
                {
                    host.Disposables.Add(httpSysTracing);
                }
            }

            TraceListener traceListener = EnableTracing(testName, logBasePath);

            host.Disposables.Add(new DisposableAction(() =>
            {
                traceListener.Close();
                Trace.Listeners.Remove(traceListener);
            }));

            EventHandler<UnobservedTaskExceptionEventArgs> handler = (sender, args) =>
            {
                Trace.TraceError("Unobserved task exception: " + args.Exception.GetBaseException());

                args.SetObserved();
            };

            TaskScheduler.UnobservedTaskException += handler;
            host.Disposables.Add(new DisposableAction(() =>
            {
                TaskScheduler.UnobservedTaskException -= handler;
            }));

            return host;
        }
        public static ITestHost CreateHost(HostType hostType, TransportType transportType, string testName, string url = null)
        {
            ITestHost host = null;

            string        logBasePath   = Path.Combine(Directory.GetCurrentDirectory(), "..");
            TraceListener traceListener = EnableTracing(testName, logBasePath);

            switch (hostType)
            {
            case HostType.IISExpress:
                host = new IISExpressTestHost(testName);
                host.TransportFactory = () => CreateTransport(transportType);
                host.Transport        = host.TransportFactory();
                break;

            case HostType.External:
                host = new ExternalTestHost(url);
                host.TransportFactory = () => CreateTransport(transportType);
                host.Transport        = host.TransportFactory();
                break;

            case HostType.Memory:
            default:
                var mh = new MemoryHost();
                host = new MemoryTestHost(mh, Path.Combine(logBasePath, testName));
                host.TransportFactory = () => CreateTransport(transportType, mh);
                host.Transport        = host.TransportFactory();
                break;

            case HostType.HttpListener:
                host = new OwinTestHost(Path.Combine(logBasePath, testName));
                host.TransportFactory = () => CreateTransport(transportType);
                host.Transport        = host.TransportFactory();
                Trace.TraceInformation("HttpListener url: {0}", host.Url);
                break;
            }

            var writer = CreateClientTraceWriter(testName);

            host.ClientTraceOutput = writer;

            if (hostType != HostType.Memory && hostType != HostType.External)
            {
                string clientNetworkPath = Path.Combine(logBasePath, testName + ".client.network.log");
                host.Disposables.Add(SystemNetLogging.Enable(clientNetworkPath));

                string      httpSysTracePath = Path.Combine(logBasePath, testName + ".httpSys");
                IDisposable httpSysTracing   = StartHttpSysTracing(httpSysTracePath);

                // If tracing is enabled then turn it off on host dispose
                if (httpSysTracing != null)
                {
                    host.Disposables.Add(httpSysTracing);
                }
            }

            host.Disposables.Add(new DisposableAction(() =>
            {
                traceListener.Close();
                Trace.Listeners.Remove(traceListener);
            }));

            EventHandler <UnobservedTaskExceptionEventArgs> handler = (sender, args) =>
            {
                Trace.TraceError("Unobserved task exception: " + args.Exception.GetBaseException());

                args.SetObserved();
            };

            TaskScheduler.UnobservedTaskException += handler;
            host.Disposables.Add(new DisposableAction(() =>
            {
                TaskScheduler.UnobservedTaskException -= handler;
            }));

            return(host);
        }
Example #3
0
        public static ITestHost CreateHost(HostType hostType, TransportType transportType, string testName, string url = null)
        {
            ITestHost host = null;

            var traceDisposable = TestTraceManager.CreateTraceListener(testName + ".test.trace.log");

            switch (hostType)
            {
            case HostType.IISExpress:
                throw new NotSupportedException("IIS Express testing is disabled.");

            case HostType.External:
                host = new ExternalTestHost(url);
                host.TransportFactory = () => CreateTransport(transportType);
                host.Transport        = host.TransportFactory();
                break;

            case HostType.Memory:
            default:
                var mh = new MemoryHost();
                host = new MemoryTestHost(mh, TestTraceManager.GetTraceFilePath(testName));
                host.TransportFactory = () => CreateTransport(transportType, mh);
                host.Transport        = host.TransportFactory();
                break;

            case HostType.HttpListener:
                host = new OwinTestHost(TestTraceManager.GetTraceFilePath(testName));
                host.TransportFactory = () => CreateTransport(transportType);
                host.Transport        = host.TransportFactory();
                Trace.TraceInformation("HttpListener url: {0}", host.Url);
                break;
            }

            host.Disposables.Add(traceDisposable);

            host.ClientTraceOutput = CreateClientTraceWriter(testName);

            if (hostType != HostType.Memory && hostType != HostType.External && TestTraceManager.IsEnabled)
            {
                host.Disposables.Add(SystemNetLogging.Enable(TestTraceManager.GetTraceFilePath($"{testName}.client.network.log")));
                var httpSysTracing = StartHttpSysTracing(TestTraceManager.GetTraceFilePath($"{testName}.httpSys"));

                // If tracing is enabled then turn it off on host dispose
                if (httpSysTracing != null)
                {
                    host.Disposables.Add(httpSysTracing);
                }
            }

            EventHandler <UnobservedTaskExceptionEventArgs> handler = (sender, args) =>
            {
                Trace.TraceError("Unobserved task exception: " + args.Exception.GetBaseException());

                args.SetObserved();
            };

            TaskScheduler.UnobservedTaskException += handler;
            host.Disposables.Add(new DisposableAction(() =>
            {
                TaskScheduler.UnobservedTaskException -= handler;
            }));

            return(host);
        }