Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Last active April 20, 2020 12:15
Show Gist options
  • Save Kikimora/f7ed0ccbf466760d7cd02b4fb3c8e0fa to your computer and use it in GitHub Desktop.
Save Kikimora/f7ed0ccbf466760d7cd02b4fb3c8e0fa to your computer and use it in GitHub Desktop.
public class TestDbListener : DbListener
{
private readonly BlockingCollection<List<DbListenerEvent>> _events = new BlockingCollection<List<DbListenerEvent>>();
private readonly List<DbListenerEvent> _triggerErrorFlag = new List<DbListenerEvent>();
private Exception _error;
public TestDbListener([NotNull] IOptions<DbListenerOptions> options) : base(options)
{
}
public override Task StopAsync(CancellationToken token)
{
// Wake up WaitForNotificationsAsync to exit run loop
_events.CompleteAdding();
return base.StopAsync(token);
}
protected override async Task<IEnumerable<DbListenerEvent>> WaitForNotificationsAsync(CancellationToken token)
{
var data = await Task.Run(() => _events.Take(), token);
if (data == _triggerErrorFlag)
{
throw _error;
}
return data;
}
protected override Task ConnectAsync(CancellationToken token)
{
return Task.CompletedTask;
}
public void PublishEvent(params DbListenerEvent[] e)
{
_events.Add(new List<DbListenerEvent>(e));
}
public void TriggerError(Exception e)
{
_error = e;
_events.Add(_triggerErrorFlag);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment