Skip to content

Commit

Permalink
Code and spec cleanup, make sure that code is readable and works as i…
Browse files Browse the repository at this point in the history
…ntended
  • Loading branch information
Arkatufus committed Jun 14, 2021
1 parent 4c60539 commit 09ece29
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 53 deletions.
29 changes: 18 additions & 11 deletions src/core/Akka.TestKit.Tests/TestKitBaseTests/ReceiveTests.cs
Expand Up @@ -9,6 +9,7 @@
using Akka.Actor;
using Akka.TestKit;
using Xunit;
using Xunit.Sdk;

namespace Akka.Testkit.Tests.TestKitBaseTests
{
Expand All @@ -28,15 +29,15 @@ public void ReceiveN_should_receive_correct_number_of_messages()
[Fact]
public void ReceiveN_should_timeout_if_no_messages()
{
Intercept(() => ReceiveN(3, TimeSpan.FromMilliseconds(10)));
AssertThrows<TrueException>(() => ReceiveN(3, TimeSpan.FromMilliseconds(10)));
}

[Fact]
public void ReceiveN_should_timeout_if_to_few_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
Intercept(() => ReceiveN(3, TimeSpan.FromMilliseconds(100)));
AssertThrows<TrueException>(() => ReceiveN(3, TimeSpan.FromMilliseconds(100)));
}


Expand All @@ -53,15 +54,15 @@ public void FishForMessage_should_return_matched_message()
[Fact]
public void FishForMessage_should_timeout_if_no_messages()
{
Intercept(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(10)));
AssertThrows<TrueException>(() => FishForMessage(_=>false, TimeSpan.FromMilliseconds(10)));
}

[Fact]
public void FishForMessage_should_timeout_if_to_few_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
Intercept(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(100)));
AssertThrows<TrueException>(() => FishForMessage(_ => false, TimeSpan.FromMilliseconds(100)));
}

[Fact]
Expand All @@ -87,8 +88,18 @@ public void ReceiveWhile_Filter_should_not_consume_last_message_that_didnt_match
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell(4711);
ReceiveWhile<object>(_ => _ is string ? _ : null);
TestActor.Tell("3");
TestActor.Tell("4");
TestActor.Tell("56");
TestActor.Tell("7");

ReceiveWhile(_ => _ is string ? _ : null)
.ShouldOnlyContainInOrder("1", "2");
ExpectMsg(4711);

ReceiveWhile(_ => _ is string s && s.Length == 1 ? s : null)
.ShouldOnlyContainInOrder("3", "4");
ExpectMsg("56");
}

[Fact]
Expand Down Expand Up @@ -141,14 +152,10 @@ public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_ma
TestActor.Tell("7");
TestActor.Tell("8");

var received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("1", "2");

ReceiveWhile<object>(_ => _ is string).ShouldOnlyContainInOrder("1", "2");
ExpectMsg(4711);

received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("3", "4", "5");

ReceiveWhile<object>(_ => _ is string).ShouldOnlyContainInOrder("3", "4", "5");
ExpectMsg(6);
}

Expand Down
97 changes: 55 additions & 42 deletions src/core/Akka.TestKit/TestKitBase_Receive.cs
Expand Up @@ -229,36 +229,46 @@ private bool InternalTryReceiveOne(out MessageEnvelope envelope, TimeSpan? max,
/// <returns>TBD</returns>
public IReadOnlyList<T> ReceiveWhile<T>(Func<object, T> filter, TimeSpan? max = null, TimeSpan? idle = null, int msgs = int.MaxValue) where T : class
{
var maxValue = RemainingOrDilated(max);
var start = Now;
var maxValue = RemainingOrDilated(max);
var stop = start + maxValue;
ConditionalLog("Trying to receive {0}messages of type {1} while filter returns non-nulls during {2}", msgs == int.MaxValue ? "" : msgs + " ", typeof(T), maxValue);
var count = 0;

var acc = new List<T>();
var idleValue = idle.GetValueOrDefault(Timeout.InfiniteTimeSpan);
MessageEnvelope msg = NullMessageEnvelope.Instance;
while (count < msgs)
while (acc.Count < msgs)
{
MessageEnvelope envelope;
if (!TryReceiveOne(out envelope, (stop - Now).Min(idleValue)))
{
_testState.LastMessage = msg;
if (!TryReceiveOne(out var envelope, (stop - Now).Min(idleValue)))
break;
}
var message = envelope.Message;
var result = filter(message);
if (result == null)

var shouldStop = false;
switch (envelope)
{
_testState.Queue.AddFirst(envelope); //Put the message back in the queue
_testState.LastMessage = msg;
break;
case NullMessageEnvelope _:
shouldStop = true;
break;

case RealMessageEnvelope m when filter(m.Message) != null:
msg = _testState.LastMessage;
acc.Add(filter(m.Message));
break;

case RealMessageEnvelope _:
_testState.Queue.AddFirst(envelope); //Put the message back in the queue
shouldStop = true;
break;

case var unexpected:
throw new Exception($"Unexpected {unexpected}");
}
msg = envelope;
acc.Add(result);
count++;

if (shouldStop)
break;
}
ConditionalLog("Received {0} messages with filter during {1}", count, Now - start);

ConditionalLog("Received {0} messages with filter during {1}", acc.Count, Now - start);
_testState.LastMessage = msg;
_testState.LastWasNoMsg = true;
return acc;
}
Expand Down Expand Up @@ -290,47 +300,50 @@ private bool InternalTryReceiveOne(out MessageEnvelope envelope, TimeSpan? max,
var stop = start + maxValue;
ConditionalLog("Trying to receive {0}messages of type {1} while predicate returns true during {2}. Messages of other types will {3}", msgs == int.MaxValue ? "" : msgs + " ", typeof(T), maxValue, shouldIgnoreOtherMessageTypes ? "be ignored" : "cause this to stop");

var count = 0;
var acc = new List<T>();
var idleValue = idle.GetValueOrDefault(Timeout.InfiniteTimeSpan);
MessageEnvelope msg = NullMessageEnvelope.Instance;
while (count < msgs)
while (acc.Count < msgs)
{
MessageEnvelope envelope;
if (!TryReceiveOne(out envelope, (stop - Now).Min(idleValue)))
{
_testState.LastMessage = msg;
if (!TryReceiveOne(out var envelope, (stop - Now).Min(idleValue)))
break;
}
var message = envelope.Message;
var typedMessage = message as T;

var shouldStop = false;
if (typedMessage != null)
switch (envelope)
{
if (shouldIgnore(typedMessage))
{
acc.Add(typedMessage);
count++;
}
else
{
case NullMessageEnvelope _:
shouldStop = true;
}
}
else
{
shouldStop = !shouldIgnoreOtherMessageTypes;
break;

case RealMessageEnvelope m when m.Message is T typedMessage:
if (shouldIgnore(typedMessage))
{
msg = _testState.LastMessage;
acc.Add(typedMessage);
break;
}
shouldStop = true;
break;

case RealMessageEnvelope _:
shouldStop = !shouldIgnoreOtherMessageTypes;
break;

case var unexpected:
throw new Exception($"Unexpected {unexpected}");
}

if (shouldStop)
{
_testState.Queue.AddFirst(envelope); //Put the message back in the queue
_testState.LastMessage = msg;
break;
}
msg = envelope;
}
ConditionalLog("Received {0} messages with filter during {1}", count, Now - start);

ConditionalLog("Received {0} messages with filter during {1}", acc.Count, Now - start);

_testState.LastMessage = msg;
_testState.LastWasNoMsg = true;
return acc;
}
Expand Down

0 comments on commit 09ece29

Please sign in to comment.