Summary

Class:WireMock.Owin.OwinSelfHost
Assembly:WireMock.Net
File(s):C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\OwinSelfHost.cs
Covered lines:54
Uncovered lines:12
Coverable lines:66
Total lines:116
Line coverage:81.8%
Branch coverage:75%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)20100100
StartAsync()10100100
StopAsync()10100100
ThreadWorkInternal()1000
StartServers()42100100

File(s)

C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\OwinSelfHost.cs

#LineLine coverage
 1#if NET45
 2using System;
 3using System.Collections.Generic;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using JetBrains.Annotations;
 7using WireMock.Validation;
 8using Owin;
 9using Microsoft.Owin.Hosting;
 10
 11namespace WireMock.Owin
 12{
 13    internal class OwinSelfHost : IOwinSelfHost
 14    {
 15        private readonly WireMockMiddlewareOptions _options;
 1816        private readonly CancellationTokenSource _cts = new CancellationTokenSource();
 17        private System.Threading.Thread _internalThread;
 18
 1819        public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] params string[] uriPrefixes)
 1820        {
 1821            Check.NotNull(options, nameof(options));
 1822            Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
 23
 9024            foreach (string uriPrefix in uriPrefixes)
 1825            {
 1826                var uri = new Uri(uriPrefix);
 1827                Urls.Add(uri);
 1828                Ports.Add(uri.Port);
 1829            }
 30
 1831            _options = options;
 1832        }
 33
 2934        public bool IsStarted { get; private set; }
 35
 5236        public List<Uri> Urls { get; } = new List<Uri>();
 37
 5438        public List<int> Ports { get; } = new List<int>();
 39
 40        [PublicAPI]
 41        public Task StartAsync()
 1842        {
 1843            return Task.Run(() =>
 3444            {
 3445                StartServers();
 3146            }, _cts.Token);
 47
 48            //if (_internalThread != null)
 49            //    throw new InvalidOperationException("Cannot start a multiple threads.");
 50
 51            //_internalThread = new Thread(ThreadWorkInternal);
 52            //_internalThread.Start();
 1853        }
 54
 55        [PublicAPI]
 56        public Task StopAsync()
 1857        {
 1858            _cts.Cancel();
 59
 1860            var tcs = new TaskCompletionSource<bool>();
 1861            var timer = new System.Timers.Timer(999);
 1862            timer.Elapsed += (sender, e) =>
 3163            {
 3164                 if (_internalThread == null)
 3165                {
 3166                    timer.Stop();
 3167                    tcs.SetResult(true);
 3168                }
 3169            };
 1870            timer.Start();
 71
 1872            return tcs.Task;
 1873        }
 74
 75        private void ThreadWorkInternal()
 076        {
 77            try
 078            {
 079                StartServers();
 080            }
 081            catch (Exception ex)
 082            {
 083                Console.WriteLine(ex);
 084            }
 85            finally
 086            {
 087                _internalThread = null;
 088            }
 089        }
 90
 91        private void StartServers()
 1692        {
 1693            Action<IAppBuilder> startup = app =>
 3294            {
 3295                app.Use<WireMockMiddleware>(_options);
 3296            };
 97
 1698            var servers = new List<IDisposable>();
 8099            foreach (var url in Urls)
 16100            {
 16101                servers.Add(WebApp.Start(url.ToString(), startup));
 16102            }
 103
 16104            IsStarted = true;
 105
 27106             while (!_cts.IsCancellationRequested)
 14107                Thread.Sleep(1000);
 108
 13109            IsStarted = false;
 110
 65111            foreach (var server in servers)
 13112                server.Dispose();
 13113        }
 114    }
 115}
 116#endif