Skip to content

Commit

Permalink
Make sure the queue is started before draining.
Browse files Browse the repository at this point in the history
- Added unit tests for shifting from the initial state.

SignalR#1832
  • Loading branch information
davidfowl committed Apr 8, 2013
1 parent 2fcc7d6 commit 9cfc0d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Microsoft.AspNet.SignalR.Core/Messaging/ScaleoutTaskQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public void Close()
{
if (ChangeState(QueueState.Closed))
{
// Ensure the queue is started
EnsureQueueStarted();

// Drain the queue to stop all sends
task = Drain(_queue);
}
Expand Down Expand Up @@ -170,6 +173,8 @@ private void InitializeCore()

private Task DrainQueue()
{
EnsureQueueStarted();

_taskCompletionSource = new TaskCompletionSource<object>();

if (_queue != null)
Expand All @@ -182,6 +187,14 @@ private Task DrainQueue()
return _taskCompletionSource.Task;
}

private void EnsureQueueStarted()
{
if (_taskCompletionSource != null)
{
_taskCompletionSource.TrySetResult(null);
}
}

private bool ChangeState(QueueState newState)
{
if (_state != newState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,29 @@ public void SendAfterCloseThenOpenRemainsClosed()
queue.Open();
Assert.Throws<InvalidOperationException>(() => queue.Enqueue(_ => TaskAsyncHelper.Empty, null));
}

[Fact]
public void InitialToBufferingToOpenToSend()
{
int x = 0;
var queue = new ScaleoutTaskQueue(new TraceSource("Queue"), "0", new ScaleoutConfiguration() { RetryOnError = true });
queue.SetError(new Exception());
queue.Open();
queue.Enqueue(_ =>
{
x++;
return TaskAsyncHelper.Empty;
},
null).Wait();

Assert.Equal(1, x);
}

[Fact(Timeout = 1000)]
public void InitialToClosed()
{
var queue = new ScaleoutTaskQueue(new TraceSource("Queue"), "0", new ScaleoutConfiguration() { RetryOnError = true });
queue.Close();
}
}
}

0 comments on commit 9cfc0d8

Please sign in to comment.