Skip to content

Commit

Permalink
Merge pull request #62 from nkot/httpendpoint-does-not-throw
Browse files Browse the repository at this point in the history
HttpListener.Stop() will not throw if it is not initialized
  • Loading branch information
etishor committed Feb 11, 2015
2 parents 1489a96 + 1f01efd commit 3beac51
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
1 change: 1 addition & 0 deletions Src/Metrics.Tests/Metrics.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<Compile Include="Core\DefaultContextCustomMetricsTests.cs" />
<Compile Include="Core\MetricTagsTests.cs" />
<Compile Include="Json\JsonSerializationTests.cs" />
<Compile Include="MetricsConfigTests\HttpEndpointTests.cs" />
<Compile Include="Metrics\CounterMetricTests.cs" />
<Compile Include="Metrics\GaugeMetricTests.cs" />
<Compile Include="Sampling\WeightedSnapshotTests.cs" />
Expand Down
78 changes: 78 additions & 0 deletions Src/Metrics.Tests/MetricsConfigTests/HttpEndpointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Metrics;
using System.Net.NetworkInformation;
using System.Collections;
using System.Net;

namespace Metrics.Tests.MetricsConfigTests
{
public class HttpEndpointTests
{

[Fact]
public void HttpEndpointCanBeDisposed()
{
var config = Metric.Config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/HttpEndpointCanBeDisposed/");
config.Dispose();
}

[Fact]
public void HttpEndpointDoesNotThrowIfPortIsOccupied()
{
using (var listener = new HttpListener())
{
listener.Prefixes.Add("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
listener.Start();

Metric.Config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
listener.Close();
}
}

[Fact]
public void HttpEndportLogsAnErrorIfPortIsOccupied()
{
using (var listener = new HttpListener())
{
listener.Prefixes.Add("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
listener.Start();

var loggedAnError = false;
var config = Metric.Config;
config.WithErrorHandler((exception, s) => { loggedAnError = true; }, true);
config.WithErrorHandler((exception) => { loggedAnError = true; }, true);

config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
Assert.True(loggedAnError);
listener.Close();
}
}

[Fact]
public void SecondCallToWithHttpEndportDoesNotThrow()
{
var config = Metric.Config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/sameendpoint/");
config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/sameendpoint/");
}

[Fact]
public void HttpEndpointDoesNotThrowOnDispose()
{
using (var listener = new HttpListener())
{
listener.Prefixes.Add("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
listener.Start();

var config = Metric.Config.WithHttpEndpoint("http:https://localhost:58888/metricstest/HttpListenerTests/OccupiedPort/");
config.Dispose();
listener.Close();
}
}

}
}
9 changes: 6 additions & 3 deletions Src/Metrics/Visualization/MetricsHttpListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,14 @@ private static void AddCORSHeaders(HttpListenerResponse response)
response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

public void Stop()
private void Stop()
{
cts.Cancel();
this.httpListener.Stop();
this.httpListener.Prefixes.Clear();
if (this.httpListener.IsListening)
{
this.httpListener.Stop();
this.httpListener.Prefixes.Clear();
}
if (processingTask != null && !processingTask.IsCompleted)
{
processingTask.Wait();
Expand Down

0 comments on commit 3beac51

Please sign in to comment.