Permalink
Browse files

Trigger stateChanged after the connection state has actually changed.

Fixes Issue #598 for the .NET client.
  • Loading branch information...
1 parent e39996d commit b0b2e8150248b51eeca315ce03e504f27eddfe03 @halter73 halter73 committed Oct 16, 2012
@@ -158,14 +158,16 @@ public ConnectionState State
}
private set
{
- if (_state != value)
+ lock (_stateLock)
{
- if (StateChanged != null)
+ if (_state != value)
{
- StateChanged(new StateChange(_state, value));
+ _state = value;
+ if (StateChanged != null)
+ {
+ StateChanged(new StateChange(_state, value));
+ }
}
-
- _state = value;
}
}
}
@@ -274,18 +276,15 @@ private bool ChangeState(ConnectionState oldState, ConnectionState newState)
bool IConnection.ChangeState(ConnectionState oldState, ConnectionState newState)
{
- lock (_stateLock)
+ // If we're in the expected old state then change state and return true
+ if (_state == oldState)
{
- // If we're in the expected old state then change state and return true
- if (_state == oldState)
- {
- State = newState;
- return true;
- }
-
- // Invalid transition
- return false;
+ State = newState;
+ return true;
}
+
+ // Invalid transition
+ return false;
}
private static void VerifyProtocolVersion(string versionString)
@@ -129,6 +129,38 @@ public void SendRaisesOnReceivedFromAllEvents()
Assert.Equal("OnReceivedAsync2", results[3]);
}
+ [Fact]
+ public void SendCanBeCalledAfterStateChangedEvent()
+ {
+ var host = new MemoryHost();
+ host.MapConnection<MySendingConnection>("/multisend");
+
+ var connection = new Client.Connection("http://foo/multisend");
+ var results = new List<string>();
+ connection.Received += data =>
+ {
+ results.Add(data);
+ };
+
+ connection.StateChanged += stateChange =>
+ {
+ if (stateChange.NewState == Client.ConnectionState.Connected)
+ {
+ connection.Send("").Wait();
+ }
+ };
+
+ connection.Start(host).Wait();
+
+ Thread.Sleep(TimeSpan.FromSeconds(5));
+
+ connection.Stop();
+
+ Debug.WriteLine(String.Join(", ", results));
+
+ Assert.Equal(4, results.Count);
+ }
+
public void Dispose()
{
GC.Collect();

0 comments on commit b0b2e81

Please sign in to comment.