Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

[Design] Add overloads for IsEnabled #65

Merged
merged 4 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@ public static IDisposable SubscribeWithAdapter(
var adapter = new DiagnosticSourceAdapter(target, isEnabled);
return diagnostic.Subscribe(adapter, adapter.IsEnabled);
}

public static IDisposable SubscribeWithAdapter(
this DiagnosticListener diagnostic,
object target,
Func<string, object, object, bool> isEnabled)
{
var adapter = new DiagnosticSourceAdapter(target, isEnabled);
return diagnostic.Subscribe(adapter, adapter.IsEnabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DiagnosticSourceAdapter : IObserver<KeyValuePair<string, object>>
private readonly IDiagnosticSourceMethodAdapter _methodAdapter;

public DiagnosticSourceAdapter(object target)
: this(target, null, new ProxyDiagnosticSourceMethodAdapter())
: this(target, (Func<string, bool>)null, new ProxyDiagnosticSourceMethodAdapter())
{
}

Expand All @@ -25,17 +25,29 @@ public DiagnosticSourceAdapter(object target, Func<string, bool> isEnabled)
{
}

public DiagnosticSourceAdapter(object target, Func<string, object, object, bool> isEnabled)
: this(target, isEnabled, methodAdapter: new ProxyDiagnosticSourceMethodAdapter())
{
}

public DiagnosticSourceAdapter(
object target,
Func<string, bool> isEnabled,
IDiagnosticSourceMethodAdapter methodAdapter)
: this(target, isEnabled: (isEnabled == null) ? (Func<string, object, object, bool>)null : (a, b, c) => isEnabled(a), methodAdapter: methodAdapter)
{
_methodAdapter = methodAdapter;
}

public DiagnosticSourceAdapter(
object target,
Func<string, object, object, bool> isEnabled,
IDiagnosticSourceMethodAdapter methodAdapter)
{
_methodAdapter = methodAdapter;
_listener = EnlistTarget(target, isEnabled);
}

private static Listener EnlistTarget(object target, Func<string, bool> isEnabled)
private static Listener EnlistTarget(object target, Func<string, object, object, bool> isEnabled)
{
var listener = new Listener(target, isEnabled);

Expand All @@ -55,6 +67,11 @@ private static Listener EnlistTarget(object target, Func<string, bool> isEnabled
}

public bool IsEnabled(string diagnosticName)
{
return IsEnabled(diagnosticName, null);
}

public bool IsEnabled(string diagnosticName, object arg1, object arg2 = null)
{
if (_listener.Subscriptions.Count == 0)
{
Expand All @@ -63,7 +80,7 @@ public bool IsEnabled(string diagnosticName)

return
_listener.Subscriptions.ContainsKey(diagnosticName) &&
(_listener.IsEnabled == null || _listener.IsEnabled(diagnosticName));
(_listener.IsEnabled == null || _listener.IsEnabled(diagnosticName, arg1, arg2));
}

public void Write(string diagnosticName, object parameters)
Expand All @@ -79,11 +96,6 @@ public void Write(string diagnosticName, object parameters)
return;
}

if (_listener.IsEnabled != null && !_listener.IsEnabled(diagnosticName))
{
return;
}

var succeeded = false;
foreach (var adapter in subscription.Adapters)
{
Expand Down Expand Up @@ -130,16 +142,15 @@ void IObserver<KeyValuePair<string, object>>.OnCompleted()

private class Listener
{

public Listener(object target, Func<string, bool> isEnabled)
public Listener(object target, Func<string, object, object, bool> isEnabled)
{
Target = target;
IsEnabled = isEnabled;

Subscriptions = new Dictionary<string, Subscription>(StringComparer.Ordinal);
}

public Func<string, bool> IsEnabled { get; }
public Func<string, object, object, bool> IsEnabled { get; }

public object Target { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public void IsEnabled_True_PredicateCalledForIsEnabled()
Assert.Equal(1, callCount);
}

[Fact]
public void IsEnabled_True_PredicateCalledForIsEnabled_WithContext()
{
// Arrange
var callCount = 0;
Func<string, object, object, bool> isEnabled = (name, arg1, arg2) =>
{
Assert.Equal("One", name);
Assert.Equal("Target info", arg1);
callCount++;
return true;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test where you register a Func<string, bool> but call `IsEnabled(string, object, object)


var adapter = CreateAdapter(new OneTarget(), isEnabled);

// Act & Assert
Assert.True(adapter.IsEnabled("One", "Target info"));
Assert.Equal(1, callCount);
}

[Fact]
public void IsEnabled_False_PredicateCalledForIsEnabled()
{
Expand All @@ -88,6 +108,45 @@ public void IsEnabled_False_PredicateCalledForIsEnabled()
Assert.Equal(1, callCount);
}

[Fact]
public void IsEnabled_False_PredicateCalledForIsEnabled_WithContext()
{
// Arrange
var callCount = 0;
Func<string, object, object, bool> isEnabled = (name, arg1, arg2) =>
{
Assert.Equal("One", name);
Assert.Equal("Target info", arg1);
callCount++;
return false;
};

var adapter = CreateAdapter(new OneTarget(), isEnabled);

// Act & Assert
Assert.False(adapter.IsEnabled("One", "Target info"));
Assert.Equal(1, callCount);
}

[Fact]
public void IsEnabled_RegisterWithoutContext_CallIsEnabledWithContext()
{
// Arrange
var callCount = 0;
Func<string, bool> isEnabled = (name) =>
{
Assert.Equal("One", name);
callCount++;
return false;
};

var adapter = CreateAdapter(new OneTarget(), (a, b, c) => isEnabled(a));

// Act & Assert
Assert.False(adapter.IsEnabled("One", new object(), new object()));
Assert.Equal(1, callCount);
}

[Fact]
public void IsEnabled_FalseForNonenlistedEvent()
{
Expand All @@ -98,6 +157,26 @@ public void IsEnabled_FalseForNonenlistedEvent()
Assert.False(adapter.IsEnabled("Two"));
}

[Fact]
public void IsEnabledWithContext_FalseForNonenlistedEvent()
{
// Arrange
var adapter = CreateAdapter(new OneTarget(), (Func<string, object, object, bool>)null);

// Act & Assert
Assert.False(adapter.IsEnabled("Two", "Target info"));
}

[Fact]
public void IsEnabledWithContext_TrueForListedEvent()
{
// Arrange
var adapter = CreateAdapter(new OneTarget(), (Func<string, object, object, bool>)null);

// Act & Assert
Assert.True(adapter.IsEnabled("One", "Target info"));
}

[Fact]
public void CallingWriteWithNullForNonNullableConverts()
{
Expand Down Expand Up @@ -173,13 +252,12 @@ public void CallingWriteForNonEnlistedNameIsHarmless()
}

[Fact]
public void Write_True_CallsIsEnabled()
public void Write_EnlistedDiagnosticName_DoesNotCallIsEnabled()
{
// Arrange
var callCount = 0;
Func<string, bool> isEnabled = (name) =>
Func<string, object, object, bool> isEnabled = (name, arg1, arg2) =>
{
Assert.Equal("One", name);
callCount++;
return true;
};
Expand All @@ -191,18 +269,17 @@ public void Write_True_CallsIsEnabled()
adapter.Write("One", new { });

// Assert
Assert.Equal(1, callCount);
Assert.Equal(0, callCount);
Assert.Equal(1, target.OneCallCount);
}

[Fact]
public void Write_False_CallsIsEnabled()
public void Write_NonEnlistedDiagnosticName_DoesNotCallIsEnabled()
{
// Arrange
var callCount = 0;
Func<string, bool> isEnabled = (name) =>
Func<string, object, object, bool> isEnabled = (name, arg1, arg2) =>
{
Assert.Equal("One", name);
callCount++;
return false;
};
Expand All @@ -211,10 +288,10 @@ public void Write_False_CallsIsEnabled()
var adapter = CreateAdapter(target, isEnabled);

// Act
adapter.Write("One", new { });
adapter.Write("Two", new { });

// Assert
Assert.Equal(1, callCount);
Assert.Equal(0, callCount);
Assert.Equal(0, target.OneCallCount);
}

Expand Down Expand Up @@ -471,5 +548,10 @@ private static DiagnosticSourceAdapter CreateAdapter(object target, Func<string,
{
return new DiagnosticSourceAdapter(target, isEnabled, new ProxyDiagnosticSourceMethodAdapter());
}

private static DiagnosticSourceAdapter CreateAdapter(object target, Func<string, object, object, bool> isEnabled)
{
return new DiagnosticSourceAdapter(target, isEnabled, new ProxyDiagnosticSourceMethodAdapter());
}
}
}