Skip to content

Commit

Permalink
Better backplane events
Browse files Browse the repository at this point in the history
  • Loading branch information
jodydonetti committed Jan 30, 2022
1 parent dc649c9 commit 628b8ab
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,28 @@ public FusionCacheBackplaneEventsHub(IFusionCache cache, FusionCacheOptions opti
public event EventHandler<FusionCacheCircuitBreakerChangeEventArgs>? CircuitBreakerChange;

/// <summary>
/// The event for a backplane message.
/// The event for a sent backplane message.
/// </summary>
public event EventHandler<FusionCacheBackplaneMessageEventArgs>? Message;
public event EventHandler<FusionCacheBackplaneMessageEventArgs>? MessageSent;

/// <summary>
/// The event for a received backplane message.
/// </summary>
public event EventHandler<FusionCacheBackplaneMessageEventArgs>? MessageReceived;

internal void OnCircuitBreakerChange(string? operationId, string? key, bool isClosed)
{
CircuitBreakerChange?.SafeExecute(operationId, key, _cache, () => new FusionCacheCircuitBreakerChangeEventArgs(isClosed), nameof(CircuitBreakerChange), _logger, _errorsLogLevel, _syncExecution);
}

internal void OnMessage(string operationId, string key, BackplaneMessage message)
internal void OnMessageReceived(string operationId, string key, BackplaneMessage message)
{
MessageReceived?.SafeExecute(operationId, key, _cache, () => new FusionCacheBackplaneMessageEventArgs(message), nameof(MessageReceived), _logger, _errorsLogLevel, _syncExecution);
}

internal void OnMessageSent(string operationId, string key, BackplaneMessage message)
{
Message?.SafeExecute(operationId, key, _cache, () => new FusionCacheBackplaneMessageEventArgs(message), nameof(Message), _logger, _errorsLogLevel, _syncExecution);
MessageSent?.SafeExecute(operationId, key, _cache, () => new FusionCacheBackplaneMessageEventArgs(message), nameof(MessageSent), _logger, _errorsLogLevel, _syncExecution);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void ProcessMessage(BackplaneMessage message)
}

// EVENT
_events.OnMessage("", message.CacheKey, message);
_events.OnMessageReceived("", message.CacheKey, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ await ExecuteOperationAsync(
async ct =>
{
await _backplane.SendNotificationAsync(message, options, ct).ConfigureAwait(false);

// EVENT
_events.OnMessageSent(operationId, message.CacheKey, message);
},
"sending backplane notification",
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public bool SendNotification(string operationId, BackplaneMessage message, Fusio
_ =>
{
_backplane.SendNotification(message, options);

// EVENT
_events.OnMessageSent(operationId, message.CacheKey, message);
},
"sending backplane notification",
options,
Expand Down
9 changes: 7 additions & 2 deletions src/ZiggyCreatures.FusionCache/ZiggyCreatures.FusionCache.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 36 additions & 19 deletions tests/ZiggyCreatures.FusionCache.Tests/EventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum EntryActionKind
Remove = 4,
FailSafeActivate = 5,
FactoryError = 6,
BackplaneMessage = 7
BackplaneMessageSent = 7,
BackplaneMessageReceived = 8
}

public class EntryActionsStats
Expand Down Expand Up @@ -803,12 +804,16 @@ public async Task BackplaneEventsAsync()
cache2.SetupBackplane(new MemoryBackplane(new MemoryBackplaneOptions()));
cache3.SetupBackplane(new MemoryBackplane(new MemoryBackplaneOptions()));

EventHandler<FusionCacheBackplaneMessageEventArgs> onMessage2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessage);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessage3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessage);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageSent2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessageSent);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageReceived2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessageReceived);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageSent3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessageSent);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageReceived3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessageReceived);

// SETUP HANDLERS
cache2.Events.Backplane.Message += onMessage2;
cache3.Events.Backplane.Message += onMessage3;
cache2.Events.Backplane.MessageSent += onMessageSent2;
cache2.Events.Backplane.MessageReceived += onMessageReceived2;
cache3.Events.Backplane.MessageSent += onMessageSent3;
cache3.Events.Backplane.MessageReceived += onMessageReceived3;

// CACHE 1
await cache1.SetAsync("foo", 21);
Expand All @@ -818,11 +823,15 @@ public async Task BackplaneEventsAsync()
await cache2.RemoveAsync("foo");

// REMOVE HANDLERS
cache2.Events.Backplane.Message -= onMessage2;
cache3.Events.Backplane.Message -= onMessage3;

Assert.Equal(2, stats2.Data[EntryActionKind.BackplaneMessage]);
Assert.Equal(3, stats3.Data[EntryActionKind.BackplaneMessage]);
cache2.Events.Backplane.MessageSent -= onMessageSent2;
cache2.Events.Backplane.MessageReceived -= onMessageReceived2;
cache3.Events.Backplane.MessageSent -= onMessageSent3;
cache3.Events.Backplane.MessageReceived -= onMessageReceived3;

Assert.Equal(1, stats2.Data[EntryActionKind.BackplaneMessageSent]);
Assert.Equal(2, stats2.Data[EntryActionKind.BackplaneMessageReceived]);
Assert.Equal(0, stats3.Data[EntryActionKind.BackplaneMessageSent]);
Assert.Equal(3, stats3.Data[EntryActionKind.BackplaneMessageReceived]);
}

[Fact]
Expand All @@ -846,12 +855,16 @@ public void BackplaneEvents()
cache2.SetupBackplane(new MemoryBackplane(new MemoryBackplaneOptions()));
cache3.SetupBackplane(new MemoryBackplane(new MemoryBackplaneOptions()));

EventHandler<FusionCacheBackplaneMessageEventArgs> onMessage2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessage);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessage3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessage);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageSent2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessageSent);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageReceived2 = (s, e) => stats2.RecordAction(EntryActionKind.BackplaneMessageReceived);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageSent3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessageSent);
EventHandler<FusionCacheBackplaneMessageEventArgs> onMessageReceived3 = (s, e) => stats3.RecordAction(EntryActionKind.BackplaneMessageReceived);

// SETUP HANDLERS
cache2.Events.Backplane.Message += onMessage2;
cache3.Events.Backplane.Message += onMessage3;
cache2.Events.Backplane.MessageSent += onMessageSent2;
cache2.Events.Backplane.MessageReceived += onMessageReceived2;
cache3.Events.Backplane.MessageSent += onMessageSent3;
cache3.Events.Backplane.MessageReceived += onMessageReceived3;

// CACHE 1
cache1.Set("foo", 21);
Expand All @@ -861,11 +874,15 @@ public void BackplaneEvents()
cache2.Remove("foo");

// REMOVE HANDLERS
cache2.Events.Backplane.Message -= onMessage2;
cache3.Events.Backplane.Message -= onMessage3;

Assert.Equal(2, stats2.Data[EntryActionKind.BackplaneMessage]);
Assert.Equal(3, stats3.Data[EntryActionKind.BackplaneMessage]);
cache2.Events.Backplane.MessageSent -= onMessageSent2;
cache2.Events.Backplane.MessageReceived -= onMessageReceived2;
cache3.Events.Backplane.MessageSent -= onMessageSent3;
cache3.Events.Backplane.MessageReceived -= onMessageReceived3;

Assert.Equal(1, stats2.Data[EntryActionKind.BackplaneMessageSent]);
Assert.Equal(2, stats2.Data[EntryActionKind.BackplaneMessageReceived]);
Assert.Equal(0, stats3.Data[EntryActionKind.BackplaneMessageSent]);
Assert.Equal(3, stats3.Data[EntryActionKind.BackplaneMessageReceived]);
}
}
}
2 changes: 0 additions & 2 deletions tests/ZiggyCreatures.FusionCache.Tests/SingleLevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
sw.Stop();

Assert.Equal(-1, res);
// TODO: MAYBE DON'T RELY ON ELAPSED TIME
Assert.True(sw.ElapsedMilliseconds > outerCancelDelayMs, "Elapsed is lower or equal than outer cancel");
Assert.True(sw.ElapsedMilliseconds < factoryDelayMs, "Elapsed is greater or equal than factory delay");
}
Expand All @@ -443,7 +442,6 @@ public void CancelingAnOperationActuallyCancelsIt()
sw.Stop();

Assert.Equal(-1, res);
// TODO: MAYBE DON'T RELY ON ELAPSED TIME
Assert.True(sw.ElapsedMilliseconds > outerCancelDelayMs, "Elapsed is lower or equal than outer cancel");
Assert.True(sw.ElapsedMilliseconds < factoryDelayMs, "Elapsed is greater or equal than factory delay");
}
Expand Down

0 comments on commit 628b8ab

Please sign in to comment.