/
ReceiveTests.cs
157 lines (137 loc) · 5.2 KB
/
ReceiveTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//-----------------------------------------------------------------------
// <copyright file="ReceiveTests.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using Akka.Actor;
using Akka.TestKit;
using Xunit;
namespace Akka.Testkit.Tests.TestKitBaseTests
{
public class ReceiveTests : AkkaSpec
{
[Fact]
public void ReceiveN_should_receive_correct_number_of_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell("4");
ReceiveN(3).ShouldOnlyContainInOrder("1", "2", "3");
ReceiveN(1).ShouldOnlyContainInOrder("4");
}
[Fact]
public void ReceiveN_should_timeout_if_no_messages()
{
Intercept(() => 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)));
}
[Fact]
public void FishForMessage_should_return_matched_message()
{
TestActor.Tell(1);
TestActor.Tell(2);
TestActor.Tell(10);
TestActor.Tell(20);
FishForMessage<int>(i => i>=10).ShouldBe(10);
}
[Fact]
public void FishForMessage_should_timeout_if_no_messages()
{
Intercept(() => 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)));
}
[Fact]
public void ReceiveWhile_Filter_should_on_a_timeout_return_no_messages()
{
ReceiveWhile<object>(_ => _, TimeSpan.FromMilliseconds(10)).Count.ShouldBe(0);
}
[Fact]
public void ReceiveWhile_Filter_should_break_on_function_returning_null_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell(2);
TestActor.Tell("3");
TestActor.Tell(99999.0);
TestActor.Tell(4);
ReceiveWhile<string>(_ => _ is double ? null : _.ToString()).ShouldOnlyContainInOrder("1", "2", "3");
}
[Fact]
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);
ExpectMsg(4711);
}
[Fact]
public void ReceiveWhile_Predicate_should_on_a_timeout_return_no_messages()
{
ReceiveWhile<object>(_ => false, TimeSpan.FromMilliseconds(10)).Count.ShouldBe(0);
}
[Fact]
public void ReceiveWhile_Predicate_should_break_when_predicate_returns_false_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell("-----------");
TestActor.Tell("4");
ReceiveWhile<string>(s => s.Length == 1).ShouldOnlyContainInOrder("1", "2", "3");
}
[Fact]
public void ReceiveWhile_Predicate_should_break_when_type_is_wrong_and_we_dont_ignore_those_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell(4);
TestActor.Tell("5");
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: false).ShouldOnlyContainInOrder("1", "2", "3");
}
[Fact]
public void ReceiveWhile_Predicate_should_continue_when_type_is_other_but_we_ignore_other_types_and_return_correct_messages()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell("3");
TestActor.Tell(4);
TestActor.Tell("5");
ReceiveWhile<string>(s => s.Length == 1, shouldIgnoreOtherMessageTypes: true).ShouldOnlyContainInOrder("1", "2", "3","5");
}
[Fact]
public void ReceiveWhile_Predicate_should_not_consume_last_message_that_didnt_match()
{
TestActor.Tell("1");
TestActor.Tell("2");
TestActor.Tell(4711);
TestActor.Tell("3");
TestActor.Tell("4");
TestActor.Tell("5");
TestActor.Tell(6);
TestActor.Tell("7");
TestActor.Tell("8");
var received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("1", "2");
ExpectMsg(4711);
received = ReceiveWhile<object>(_ => _ is string);
received.ShouldOnlyContainInOrder("3", "4", "5");
ExpectMsg(6);
}
}
}