-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b5843a
commit 3b93eaa
Showing
5 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/FeatureToggle.Core/DefaultToDisabledOnErrorDecorator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
|
||
namespace FeatureToggle.Core | ||
{ | ||
/// <summary> | ||
/// This decorator should be used with care or not at all. It will essentially hide any configuration errors | ||
/// or other problems with the wrapped toggle knowing if it should enable the feature or not. | ||
/// This feature is provided as a response to a user request. | ||
/// </summary> | ||
public class DefaultToDisabledOnErrorDecorator : IFeatureToggle | ||
{ | ||
private readonly Action<Exception> _logAction; | ||
public IFeatureToggle Toggle { get; private set; } | ||
|
||
/// <summary> | ||
/// Decorator constructor that allows the wrapping of another toggle | ||
/// </summary> | ||
/// <param name="toggle">The toggle to wrap (decorate)</param> | ||
/// <param name="logAction">Optional action that gets called if an exception is thrown by the wrapped toggle before returning enabled = false</param> | ||
public DefaultToDisabledOnErrorDecorator(IFeatureToggle toggle, Action<Exception> logAction = null) | ||
{ | ||
Toggle = toggle; | ||
_logAction = logAction; | ||
} | ||
|
||
public bool FeatureEnabled | ||
{ | ||
get | ||
{ | ||
try | ||
{ | ||
return Toggle.FeatureEnabled; | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (_logAction != null) | ||
{ | ||
_logAction(ex); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Tests/FeatureToggle.Tests/DefaultToDisabledOnErrorDecoratorShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using FeatureToggle.Core; | ||
using Xunit; | ||
|
||
namespace FeatureToggle.Tests | ||
{ | ||
public class DefaultToDisabledOnErrorDecoratorShould | ||
{ | ||
[Fact] | ||
public void ReturnFalseWhenError() | ||
{ | ||
var sut = new DefaultToDisabledOnErrorDecorator(new FeatureToggleThatThrowsAnException()); | ||
|
||
Assert.False(sut.FeatureEnabled); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ReturnConfiguredValueWhenNoError() | ||
{ | ||
var sut = new DefaultToDisabledOnErrorDecorator(new FeatureToggleThatDoesNotThrowAnException()); | ||
|
||
Assert.False(sut.FeatureEnabled); | ||
} | ||
|
||
|
||
[Fact] | ||
public void AllowAccessToWrappedToggle() | ||
{ | ||
var wrappedToggle = new FeatureToggleThatDoesNotThrowAnException(); | ||
|
||
var sut = new DefaultToDisabledOnErrorDecorator(wrappedToggle); | ||
|
||
Assert.Same(wrappedToggle, sut.Toggle); | ||
} | ||
|
||
|
||
[Fact] | ||
public void CallLoggingActionOnErrorIfSet() | ||
{ | ||
var wrappedToggle = new FeatureToggleThatThrowsAnException(); | ||
|
||
string log = ""; | ||
|
||
var sut = new DefaultToDisabledOnErrorDecorator(wrappedToggle, ex => log += ex.Message); | ||
|
||
|
||
|
||
try | ||
{ | ||
var isEnabled = sut.FeatureEnabled; | ||
} | ||
catch | ||
{ | ||
// ignore exception so we can assert that the specified action was called | ||
} | ||
|
||
|
||
|
||
Assert.Equal("Exception for testing purposes", log); | ||
} | ||
|
||
|
||
private class FeatureToggleThatThrowsAnException : IFeatureToggle { | ||
public bool FeatureEnabled | ||
{ | ||
get | ||
{ | ||
throw new Exception("Exception for testing purposes"); | ||
} | ||
} | ||
} | ||
|
||
|
||
private class FeatureToggleThatDoesNotThrowAnException : IFeatureToggle | ||
{ | ||
public bool FeatureEnabled | ||
{ | ||
get { return false; } | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters