Browse Source

Moved to task scheduler.

pull/195/head
Sebastian Stehle 8 years ago
parent
commit
f486fe696d
  1. 2
      src/Squidex.Infrastructure/CQRS/Events/Actors/EventConsumerActor.cs
  2. 2
      src/Squidex.Infrastructure/States/StateFactory.cs
  3. 126
      tests/Squidex.Infrastructure.Tests/InMemoryPubSubTests.cs

2
src/Squidex.Infrastructure/CQRS/Events/Actors/EventConsumerActor.cs

@ -19,7 +19,7 @@ namespace Squidex.Infrastructure.CQRS.Events.Actors
private readonly EventDataFormatter formatter;
private readonly IEventStore eventStore;
private readonly ISemanticLog log;
private readonly TaskFactory taskFactory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(1));
private readonly SingleThreadedDispatcher dispatcher = new SingleThreadedDispatcher(1);
private IEventSubscription currentSubscription;
private IEventConsumer eventConsumer;

2
src/Squidex.Infrastructure/States/StateFactory.cs

@ -76,7 +76,7 @@ namespace Squidex.Infrastructure.States
.SetAbsoluteExpiration(CacheDuration)
.RegisterPostEvictionCallback((k, v, r, s) =>
{
taskFactory.StartNew(async () =>
taskFactory.StartNew(() =>
{
state.Dispose();
states.Remove(state);

126
tests/Squidex.Infrastructure.Tests/InMemoryPubSubTests.cs

@ -27,68 +27,68 @@ namespace Squidex.Infrastructure
public string Text { get; set; }
}
//[Fact]
//public void Should_publish_to_handlers()
//{
// var channel1Events = new List<string>();
// var channel2Events = new List<string>();
// sut.Subscribe<MessageA>(m =>
// {
// channel1Events.Add(m.Text);
// });
// sut.Subscribe<MessageA>(m =>
// {
// channel1Events.Add(m.Text);
// });
// sut.Subscribe<MessageB>(m =>
// {
// channel2Events.Add(m.Text);
// });
// sut.Publish(new MessageA { Text = "1" }, true);
// sut.Publish(new MessageA { Text = "2" }, true);
// sut.Publish(new MessageA { Text = "3" }, false);
// sut.Publish(new MessageB { Text = "a" }, true);
// sut.Publish(new MessageB { Text = "b" }, true);
// Assert.Equal(new[] { "1", "1", "2", "2" }, channel1Events.ToArray());
// Assert.Equal(new[] { "a", "b" }, channel2Events.ToArray());
//}
//[Fact]
//public async Task Should_make_request_reply_requests()
//{
// sut.ReceiveAsync<int, int>(x =>
// {
// return Task.FromResult(x + x);
// }, true);
// var response = await sut.RequestAsync<int, int>(2, TimeSpan.FromSeconds(2), true);
// Assert.Equal(4, response);
//}
//[Fact]
//public async Task Should_timeout_when_response_is_too_slow()
//{
// sut.ReceiveAsync<int, int>(async x =>
// {
// await Task.Delay(1000);
// return x + x;
// }, true);
// await Assert.ThrowsAsync<TaskCanceledException>(() => sut.RequestAsync<int, int>(1, TimeSpan.FromSeconds(0.5), true));
//}
//[Fact]
//public async Task Should_timeout_when_nobody_responds()
//{
// await Assert.ThrowsAsync<TaskCanceledException>(() => sut.RequestAsync<int, int>(2, TimeSpan.FromSeconds(0.5), true));
//}
[Fact]
public void Should_publish_to_handlers()
{
var channel1Events = new List<string>();
var channel2Events = new List<string>();
sut.Subscribe<MessageA>(m =>
{
channel1Events.Add(m.Text);
});
sut.Subscribe<MessageA>(m =>
{
channel1Events.Add(m.Text);
});
sut.Subscribe<MessageB>(m =>
{
channel2Events.Add(m.Text);
});
sut.Publish(new MessageA { Text = "1" }, true);
sut.Publish(new MessageA { Text = "2" }, true);
sut.Publish(new MessageA { Text = "3" }, false);
sut.Publish(new MessageB { Text = "a" }, true);
sut.Publish(new MessageB { Text = "b" }, true);
Assert.Equal(new[] { "1", "1", "2", "2" }, channel1Events.ToArray());
Assert.Equal(new[] { "a", "b" }, channel2Events.ToArray());
}
[Fact]
public async Task Should_make_request_reply_requests()
{
sut.ReceiveAsync<int, int>(x =>
{
return Task.FromResult(x + x);
}, true);
var response = await sut.RequestAsync<int, int>(2, TimeSpan.FromSeconds(2), true);
Assert.Equal(4, response);
}
[Fact]
public async Task Should_timeout_when_response_is_too_slow()
{
sut.ReceiveAsync<int, int>(async x =>
{
await Task.Delay(1000);
return x + x;
}, true);
await Assert.ThrowsAsync<TaskCanceledException>(() => sut.RequestAsync<int, int>(1, TimeSpan.FromSeconds(0.5), true));
}
[Fact]
public async Task Should_timeout_when_nobody_responds()
{
await Assert.ThrowsAsync<TaskCanceledException>(() => sut.RequestAsync<int, int>(2, TimeSpan.FromSeconds(0.5), true));
}
}
}

Loading…
Cancel
Save