Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments review #156

Merged
merged 9 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JustEat.StatsD/Buffered/StatsDUtf8Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace JustEat.StatsD.Buffered
{
internal class StatsDUtf8Formatter
internal sealed class StatsDUtf8Formatter
{
private readonly byte[] _utf8Prefix;

Expand Down
8 changes: 4 additions & 4 deletions src/JustEat.StatsD/DisposableTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ internal sealed class DisposableTimer : IDisposableTimer

public string StatName { get; set; }

public DisposableTimer(IStatsDPublisher publisher, string statName)
public DisposableTimer(IStatsDPublisher publisher, string bucket)
{
_publisher = publisher ?? throw new ArgumentNullException(nameof(publisher));

if (string.IsNullOrEmpty(statName))
if (string.IsNullOrEmpty(bucket))
{
throw new ArgumentNullException(nameof(statName));
throw new ArgumentNullException(nameof(bucket));
}

StatName = statName;
StatName = bucket;
martincostello marked this conversation as resolved.
Show resolved Hide resolved
_stopwatch = Stopwatch.StartNew();
}

Expand Down
9 changes: 9 additions & 0 deletions src/JustEat.StatsD/EndpointLookups/CachedEndpointSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ public class CachedEndpointSource : IEndPointSource
/// <exception cref="ArgumentNullException">
/// <paramref name="inner"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="cacheDuration"/> is less than or equal to <see cref="TimeSpan.Zero"/>.
/// </exception>
public CachedEndpointSource(IEndPointSource inner, TimeSpan cacheDuration)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));

if (cacheDuration <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(cacheDuration), cacheDuration, "The end point cache duration must be a positive TimeSpan value.");
}

_cachedValue = null;
_cacheDuration = cacheDuration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace JustEat.StatsD.EndpointLookups
{
/// <summary>
/// A class representing an implementation of <see cref="IEndPointSource"/> that looks up
/// the <see cref="EndPoint"/> for DNS hostname to resolve its IP address.
/// the <see cref="EndPoint"/> for a DNS hostname to resolve its IP address.
/// </summary>
public class DnsLookupIpEndpointSource : IEndPointSource
{
Expand Down
8 changes: 5 additions & 3 deletions src/JustEat.StatsD/SocketProtocol.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Net.Sockets;

namespace JustEat.StatsD
{
/// <summary>
/// The subset of ProtocolType that are supported by SocketTransport
/// UDP or IP.
/// UDP is the default, but IP transport is required for AWS Lambdas.
/// An emueration defining the subset of <see cref="ProtocolType"/> values that are supported by <see cref="SocketTransport"/>.
martincostello marked this conversation as resolved.
Show resolved Hide resolved
/// <para />
/// UDP is the default, but IP transport is required for some environments such as AWS Lambda functions.
/// </summary>
public enum SocketProtocol
{
Expand Down
27 changes: 27 additions & 0 deletions src/JustEat.StatsD/TimerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static class TimerExtensions
/// <returns>
/// An <see cref="IDisposableTimer"/> that publishes the metric when the instance is disposed of.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static IDisposableTimer StartTimer(this IStatsDPublisher publisher, string bucket)
{
return new DisposableTimer(publisher, bucket);
Expand All @@ -30,6 +33,9 @@ public static IDisposableTimer StartTimer(this IStatsDPublisher publisher, strin
/// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
/// <param name="bucket">The bucket to publish the timer for.</param>
/// <param name="action">A delegate to a method whose invocation should be timed.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static void Time(this IStatsDPublisher publisher, string bucket, Action action)
{
using (StartTimer(publisher, bucket))
Expand All @@ -45,6 +51,9 @@ public static void Time(this IStatsDPublisher publisher, string bucket, Action a
/// <param name="publisher">The <see cref="IStatsDPublisher"/> to publish with.</param>
/// <param name="bucket">The bucket to publish the timer for.</param>
/// <param name="action">A delegate to a method whose invocation should be timed.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static void Time(this IStatsDPublisher publisher, string bucket, Action<IDisposableTimer> action)
{
using (var timer = StartTimer(publisher, bucket))
Expand All @@ -63,6 +72,9 @@ public static void Time(this IStatsDPublisher publisher, string bucket, Action<I
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static async Task Time(this IStatsDPublisher publisher, string bucket, Func<Task> action)
{
using (StartTimer(publisher, bucket))
Expand All @@ -82,6 +94,9 @@ public static async Task Time(this IStatsDPublisher publisher, string bucket, Fu
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static async Task Time(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, Task> action)
{
using (var timer = StartTimer(publisher, bucket))
Expand All @@ -101,6 +116,9 @@ public static async Task Time(this IStatsDPublisher publisher, string bucket, Fu
/// <returns>
/// The value from invoking <paramref name="func"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<T> func)
{
using (StartTimer(publisher, bucket))
Expand All @@ -120,6 +138,9 @@ public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<T>
/// <returns>
/// The value from invoking <paramref name="func"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
martincostello marked this conversation as resolved.
Show resolved Hide resolved
public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, T> func)
{
using (var timer = StartTimer(publisher, bucket))
Expand All @@ -139,6 +160,9 @@ public static T Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDi
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static async Task<T> Time<T>(this IStatsDPublisher publisher, string bucket, Func<Task<T>> func)
{
using (StartTimer(publisher, bucket))
Expand All @@ -158,6 +182,9 @@ public static async Task<T> Time<T>(this IStatsDPublisher publisher, string buck
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation to time.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="publisher"/> or <paramref name="bucket"/> is <see langword="null"/>.
/// </exception>
public static async Task<T> Time<T>(this IStatsDPublisher publisher, string bucket, Func<IDisposableTimer, Task<T>> func)
{
using (var timer = StartTimer(publisher, bucket))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public static async Task CachedValueIsReturnedAgainAfterExpiry()
mockInner.Verify(x => x.GetEndpoint(), Times.Exactly(2));
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
public static void ConstructorThrowsIfCacheDurationIsInvalid(long ticks)
{
// Arrange
var inner = Mock.Of<IEndPointSource>();
var cacheDuration = TimeSpan.FromTicks(ticks);

// Act and Assert
var exception = Assert.Throws<ArgumentOutOfRangeException>("cacheDuration", () => new CachedEndpointSource(inner, cacheDuration));

exception.ActualValue.ShouldBe(cacheDuration);
}

private static IPEndPoint MakeTestIpEndPoint()
{
return new IPEndPoint(new IPAddress(new byte[] { 1, 2, 3, 4 }), 8125);
Expand Down