Skip to content

Commit

Permalink
Added support for toggle update event (#153)
Browse files Browse the repository at this point in the history
* Added support for toggle update event

* Updated README for TogglesUpdatedEvent

* Remove redundant parens per code analysis

* Static code analysis wants 2 spaces, so I added two spaces for all event items in the list
  • Loading branch information
YayBurritos authored Aug 14, 2023
1 parent 8158ccf commit 76427ee
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ Refer to the [Unleash context](#unleash-context) section for more information ab
## Handling events

Currently supported events:
- [Impression data events](https://docs.getunleash.io/advanced/impression-data#impression-event-data)
- Error events
- [Impression data events](https://docs.getunleash.io/advanced/impression-data#impression-event-data)
- Error events
- Toggles updated event

```csharp

Expand All @@ -187,6 +188,7 @@ unleash.ConfigureEvents(cfg =>
{
cfg.ImpressionEvent = evt => { Console.WriteLine($"{evt.FeatureName}: {evt.Enabled}"); };
cfg.ErrorEvent = evt => { /* Handling code here */ Console.WriteLine($"{evt.ErrorType} occured."); };
cfg.TogglesUpdatedEvent = evt => { /* Handling code here */ Console.WriteLine($"Toggles updated on: {evt.UpdatedOn}"); };
});

```
Expand Down
10 changes: 10 additions & 0 deletions src/Unleash/Internal/EventCallbackConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class EventCallbackConfig
{
public Action<ImpressionEvent> ImpressionEvent { get; set; }
public Action<ErrorEvent> ErrorEvent { get; set; }
public Action<TogglesUpdatedEvent> TogglesUpdatedEvent { get; set; }

public void RaiseError(ErrorEvent evt)
{
Expand All @@ -15,5 +16,14 @@ public void RaiseError(ErrorEvent evt)
ErrorEvent(evt);
}
}

public void RaiseTogglesUpdated(TogglesUpdatedEvent evt)
{
if (TogglesUpdatedEvent != null)
{
TogglesUpdatedEvent(evt);
}
}

}
}
11 changes: 11 additions & 0 deletions src/Unleash/Internal/TogglesUpdatedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Unleash.Internal
{
public class TogglesUpdatedEvent
{
public DateTime UpdatedOn { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/Unleash/Scheduling/FetchFeatureTogglesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public async Task ExecuteAsync(CancellationToken cancellationToken)
}

if (!result.HasChanged)
{
return;
}
else
{
eventConfig?.RaiseTogglesUpdated(new TogglesUpdatedEvent { UpdatedOn = DateTime.UtcNow });
}

if (string.IsNullOrEmpty(result.Etag))
return;
Expand Down
74 changes: 74 additions & 0 deletions tests/Unleash.Tests/Internal/TogglesUpdatedEvent_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using FakeItEasy;
using FluentAssertions;
using NUnit.Framework;
using System.Threading;
using System.Threading.Tasks;
using Unleash.Communication;
using Unleash.Internal;
using Unleash.Scheduling;
using Unleash.Serialization;
using Unleash.Tests.Mock;

namespace Unleash.Tests.Internal
{
public class TogglesUpdatedEvent_Tests
{
[Test]
public void TogglesUpdated_Event_Gets_Called_For_HasChanged_True()
{
// Arrange
TogglesUpdatedEvent callbackEvent = null;
var callbackConfig = new EventCallbackConfig
{
TogglesUpdatedEvent = evt => { callbackEvent = evt; }
};

var fakeApiClient = A.Fake<IUnleashApiClient>();
A.CallTo(() => fakeApiClient.FetchToggles(A<string>._, A<CancellationToken>._))
.Returns(Task.FromResult(new FetchTogglesResult { HasChanged = true, ToggleCollection = new ToggleCollection(), Etag = "one" }));

var collection = new ThreadSafeToggleCollection();
var serializer = new DynamicNewtonsoftJsonSerializer();
serializer.TryLoad();

var filesystem = new MockFileSystem();
var tokenSource = new CancellationTokenSource();
var task = new FetchFeatureTogglesTask(fakeApiClient, collection, serializer, filesystem, callbackConfig, "togglefile.txt", "etagfile.txt");

// Act
Task.WaitAll(task.ExecuteAsync(tokenSource.Token));

// Assert
callbackEvent.Should().NotBeNull();
}

[Test]
public void TogglesUpdated_Event_Does_Not_Get_Called_For_HasChanged_False()
{
// Arrange
TogglesUpdatedEvent callbackEvent = null;
var callbackConfig = new EventCallbackConfig
{
TogglesUpdatedEvent = evt => { callbackEvent = evt; }
};

var fakeApiClient = A.Fake<IUnleashApiClient>();
A.CallTo(() => fakeApiClient.FetchToggles(A<string>._, A<CancellationToken>._))
.Returns(Task.FromResult(new FetchTogglesResult { HasChanged = false, ToggleCollection = new ToggleCollection(), Etag = "one" }));

var collection = new ThreadSafeToggleCollection();
var serializer = new DynamicNewtonsoftJsonSerializer();
serializer.TryLoad();

var filesystem = new MockFileSystem();
var tokenSource = new CancellationTokenSource();
var task = new FetchFeatureTogglesTask(fakeApiClient, collection, serializer, filesystem, callbackConfig, "togglefile.txt", "etagfile.txt");

// Act
Task.WaitAll(task.ExecuteAsync(tokenSource.Token));

// Assert
callbackEvent.Should().BeNull();
}
}
}
1 change: 1 addition & 0 deletions tests/Unleash.Tests/Unleash.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Internal\CachedFilesLoaderTestBase.cs" />
<Compile Include="Internal\CachedFilesLoader_Backup_And_Etag_Tests.cs" />
<Compile Include="Internal\CachedFilesLoader_Bootstrap_Tests.cs" />
<Compile Include="Internal\TogglesUpdatedEvent_Tests.cs" />
<Compile Include="Internal\ErrorEvents_Tests.cs" />
<Compile Include="Internal\ImpressionData_Tests.cs" />
<Compile Include="IOTests.cs" />
Expand Down

0 comments on commit 76427ee

Please sign in to comment.