-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathMessageQueueTest.cs
More file actions
174 lines (135 loc) · 6.67 KB
/
Copy pathMessageQueueTest.cs
File metadata and controls
174 lines (135 loc) · 6.67 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using NUnit.Framework;
using kOS.Safe.Communication;
using kOS.Safe.Exceptions;
using kOS.Safe.Encapsulation;
using kOS.Safe.Serialization;
using kOS.Safe.Utilities;
namespace kOS.Safe.Test.Communication
{
[TestFixture]
public class MessageQueueTest
{
private GenericMessageQueue<BaseMessage, FakeCurrentTimeProvider> queue;
[SetUp]
public void Setup()
{
queue = new GenericMessageQueue<BaseMessage, FakeCurrentTimeProvider>();
queue.TimeProvider.SetTime(0);
}
[Test]
public void CanHandleNoMessages()
{
Assert.AreEqual(0, queue.Count());
Assert.AreEqual(0, queue.ReceivedCount());
Assert.Throws<KOSCommunicationException>(delegate { queue.Pop(); });
Assert.Throws<KOSCommunicationException>(delegate { queue.Peek(); });
}
[Test]
public void CanHandleMultipleMessagesInOrder()
{
queue.Push(new BaseMessage(new StringValue("content1"), 1, 10));
queue.Push(new BaseMessage(new StringValue("content2"), 2, 10));
queue.Push(new BaseMessage(new StringValue("content3"), 3, 11));
queue.Push(new BaseMessage(new StringValue("content4"), 4, 12));
queue.Push(new BaseMessage(new StringValue("content5"), 5, 13));
queue.TimeProvider.SetTime(5);
// time now is 5, no messages should be received
Assert.AreEqual(5, queue.Count());
Assert.AreEqual(0, queue.ReceivedCount());
Assert.Throws<KOSCommunicationException>(delegate { queue.Pop(); });
Assert.Throws<KOSCommunicationException>(delegate { queue.Peek(); });
queue.TimeProvider.SetTime(10);
// time now is 10, there should be 2 messages
Assert.AreEqual(5, queue.Count());
Assert.AreEqual(2, queue.ReceivedCount());
BaseMessage received = queue.Pop();
Assert.AreEqual(new StringValue("content1"), received.Content);
Assert.AreEqual(10, received.ReceivedAt);
Assert.AreEqual(1, received.SentAt);
queue.TimeProvider.SetTime(13);
// time now is 13, there should be 4 messages
Assert.AreEqual(4, queue.Count());
Assert.AreEqual(4, queue.ReceivedCount());
Assert.AreEqual(new StringValue("content2"), queue.Pop().Content);
Assert.AreEqual(new StringValue("content3"), queue.Pop().Content);
Assert.AreEqual(new StringValue("content4"), queue.Pop().Content);
Assert.AreEqual(new StringValue("content5"), queue.Pop().Content);
}
[Test]
public void CanHandleMultipleRandomMessages()
{
queue.Push(new BaseMessage(new StringValue("content1"), 1, 13));
queue.Push(new BaseMessage(new StringValue("content2"), 2, 3));
queue.TimeProvider.SetTime(3);
Assert.AreEqual(1, queue.ReceivedCount());
Assert.AreEqual(new StringValue("content2"), queue.Pop().Content);
queue.Push(new BaseMessage(new StringValue("content3"), 3, 9));
queue.Push(new BaseMessage(new StringValue("content4"), 3, 14));
queue.Push(new BaseMessage(new StringValue("content5"), 3, 5));
queue.TimeProvider.SetTime(9);
Assert.AreEqual(2, queue.ReceivedCount());
Assert.AreEqual(new StringValue("content5"), queue.Pop().Content);
Assert.AreEqual(new StringValue("content3"), queue.Pop().Content);
queue.TimeProvider.SetTime(14);
Assert.AreEqual(2, queue.ReceivedCount());
Assert.AreEqual(new StringValue("content1"), queue.Pop().Content);
Assert.AreEqual(new StringValue("content4"), queue.Pop().Content);
}
[Test]
public void CanClear()
{
queue.Push(new BaseMessage(new StringValue("content1"), 1, 10));
queue.Push(new BaseMessage(new StringValue("content2"), 2, 10));
queue.Push(new BaseMessage(new StringValue("content3"), 3, 11));
queue.Push(new BaseMessage(new StringValue("content4"), 4, 12));
queue.Push(new BaseMessage(new StringValue("content5"), 5, 13));
// this should remove nothing
queue.Clear();
Assert.AreEqual(5, queue.Count());
Assert.AreEqual(0, queue.ReceivedCount());
queue.TimeProvider.SetTime(10);
// this should remove two messages
queue.Clear();
Assert.AreEqual(3, queue.Count());
Assert.AreEqual(0, queue.ReceivedCount());
queue.TimeProvider.SetTime(13);
Assert.AreEqual(3, queue.Count());
Assert.AreEqual(3, queue.ReceivedCount());
// this should remove the rest
queue.Clear();
Assert.AreEqual(0, queue.ReceivedCount());
Assert.AreEqual(0, queue.Count());
}
[Test]
public void CanHandleSerializableStructures()
{
Lexicon lex = new Lexicon();
lex.Add(new StringValue("key1"), new StringValue("value1"));
//queue.Push(new BaseMessage(new SafeSerializationMgr(null).Dump(lex), 0, 0));
Lexicon read = null;// new SafeSerializationMgr(null).CreateFromDump(queue.Pop().Content as Dump) as Lexicon;
Assert.AreEqual(new StringValue("value1"), read[new StringValue("key1")]);
}
[Test]
public void CanDump()
{
queue.Push(new BaseMessage(new StringValue("content1"), 1, 10));
queue.Push(new BaseMessage(new StringValue("content2"), 2, 10));
queue.Push(new BaseMessage(new StringValue("content3"), 3, 11));
queue.Push(new BaseMessage(new StringValue("content4"), 4, 12));
queue.Push(new BaseMessage(new StringValue("content5"), 5, 13));
GenericMessageQueue<BaseMessage,FakeCurrentTimeProvider> newQueue = new GenericMessageQueue<BaseMessage,FakeCurrentTimeProvider>();
newQueue.LoadDump(queue.Dump(new DumperState()) as DumpList);
newQueue.TimeProvider.SetTime(11);
// time now is 13, there should be 4 messages
Assert.AreEqual(5, newQueue.Count());
Assert.AreEqual(3, newQueue.ReceivedCount());
BaseMessage received = newQueue.Pop();
Assert.AreEqual(1, received.SentAt);
Assert.AreEqual(10, received.ReceivedAt);
Assert.AreEqual(new StringValue("content1"), received.Content);
Assert.AreEqual(new StringValue("content2"), newQueue.Pop().Content);
Assert.AreEqual(new StringValue("content3"), newQueue.Pop().Content);
}
}
}