Skip to content

Commit 74f195c

Browse files
author
Eric Maupin
committed
Fix message response timeouts
1 parent 391b21e commit 74f195c

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

Desktop/Tempest/Tests/MessageResponseManagerTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ public void SendForTimeout()
9898

9999
Task<Message> response = mrm.SendFor (msg, tcs.Task, 1000);
100100

101-
Thread.Sleep (2000);
102-
103-
mrm.CheckTimeouts();
101+
DateTime start = DateTime.Now;
102+
while ((DateTime.Now - start) < TimeSpan.FromSeconds (2)) {
103+
mrm.CheckTimeouts();
104+
Thread.Sleep (1);
105+
}
104106

105107
try {
106108
if (!response.Wait (10000))

Tempest/MessageResponseManager.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Task<Message> SendFor (Message message, Func<Message, Task<bool>> sender,
5252
throw new ArgumentNullException ("message", "Message.Header is null");
5353

5454
if (timeout > 0) {
55-
if (!this.timeouts.TryAdd (message.Header.MessageId, timeout))
55+
if (!this.timeouts.TryAdd (message.Header.MessageId, DateTime.Now + TimeSpan.FromMilliseconds (timeout)))
5656
throw new InvalidOperationException ("Message already waiting response");
5757
}
5858

@@ -96,7 +96,7 @@ public Task<Message> SendFor (Message message, Task<bool> sendTask, int timeout)
9696
throw new ArgumentNullException ("message", "Message.Header is null");
9797

9898
if (timeout > 0) {
99-
if (!this.timeouts.TryAdd (message.Header.MessageId, timeout))
99+
if (!this.timeouts.TryAdd (message.Header.MessageId, DateTime.Now + TimeSpan.FromMilliseconds (timeout)))
100100
throw new InvalidOperationException ("Message already waiting response");
101101
}
102102

@@ -137,7 +137,7 @@ public void Receive (Message message)
137137

138138
tcs.TrySetResult (message);
139139

140-
int timeout;
140+
DateTime timeout;
141141
this.timeouts.TryRemove (message.Header.MessageId, out timeout);
142142
}
143143

@@ -156,13 +156,11 @@ public void Clear()
156156
/// </summary>
157157
public void CheckTimeouts()
158158
{
159-
var timespan = DateTime.Now - this.lastTimeoutCheck;
160-
this.lastTimeoutCheck = DateTime.Now;
161-
159+
var now = DateTime.Now;
162160
List<int> remove = new List<int>();
163161

164162
foreach (var kvp in this.timeouts) {
165-
if (timespan.TotalMilliseconds < kvp.Value)
163+
if (now < kvp.Value)
166164
continue;
167165

168166
TaskCompletionSource<Message> tcs;
@@ -174,16 +172,15 @@ public void CheckTimeouts()
174172

175173
foreach (int id in remove) {
176174
TaskCompletionSource<Message> tcs;
177-
int timeout;
175+
DateTime timeout;
178176

179177
this.timeouts.TryRemove (id, out timeout);
180178
this.messageResponses.TryRemove (id, out tcs);
181179
}
182180
}
183181

184182
private readonly ConcurrentDictionary<int, TaskCompletionSource<Message>> messageResponses = new ConcurrentDictionary<int, TaskCompletionSource<Message>>();
185-
private readonly ConcurrentDictionary<int, int> timeouts = new ConcurrentDictionary<int, int>();
186-
private DateTime lastTimeoutCheck = DateTime.Now;
183+
private readonly ConcurrentDictionary<int, DateTime> timeouts = new ConcurrentDictionary<int, DateTime>();
187184

188185
private Task<Message> SendForCore (Message message, Func<Message, Task<bool>> sender = null, Task<bool> sendTask = null)
189186
{

0 commit comments

Comments
 (0)