Skip to content

Commit 7561140

Browse files
author
Jonathan Gaillard
committed
Make sure and check if we are over the wait time after we sleep in Get() to ensure we exit quickly instead of doing another needless findAndModify.
With lots of Queues objects and running in loops the extra query is expensive.
1 parent f686a42 commit 7561140

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

DominionEnterprises.Mongo.Tests/QueueTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,21 @@ public void GetWait()
280280
{
281281
var start = DateTime.Now;
282282

283-
queue.Get(new QueryDocument(), TimeSpan.MaxValue, TimeSpan.FromMilliseconds(200), TimeSpan.MinValue, false);
283+
queue.Get(new QueryDocument(), TimeSpan.MaxValue, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(201), false);
284284

285285
var end = DateTime.Now;
286286

287287
Assert.IsTrue(end - start >= TimeSpan.FromMilliseconds(200));
288288
Assert.IsTrue(end - start < TimeSpan.FromMilliseconds(400));
289+
290+
start = DateTime.Now;
291+
292+
queue.Get(new QueryDocument(), TimeSpan.MaxValue, TimeSpan.FromMilliseconds(200), TimeSpan.MinValue, false);
293+
294+
end = DateTime.Now;
295+
296+
Assert.IsTrue(end - start >= TimeSpan.FromMilliseconds(200));
297+
Assert.IsTrue(end - start < TimeSpan.FromMilliseconds(400));
289298
}
290299

291300
[Test]

DominionEnterprises.Mongo/Queue.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ public BsonDocument Get(QueryDocument query, TimeSpan resetRunning, TimeSpan wai
248248

249249
Thread.Sleep(poll);
250250
}
251+
252+
if (DateTime.UtcNow >= end)
253+
return null;
251254
}
252255
}
253256
#endregion

0 commit comments

Comments
 (0)