Skip to content

Commit 62faafe

Browse files
committed
Some improvements based on Zap's fork
1 parent 16e2ca1 commit 62faafe

File tree

19 files changed

+83
-48
lines changed

19 files changed

+83
-48
lines changed

src/netstandard/Default/WampSharp.NewtonsoftJson/Newtonsoft/JTokenMessageParser.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public WampMessage<JToken> Parse(string text)
3939

4040
public string Format(WampMessage<object> message)
4141
{
42-
StringWriter writer = new StringWriter();
43-
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
42+
using StringWriter writer = new StringWriter();
43+
using JsonTextWriter jsonWriter = new JsonTextWriter(writer);
4444
jsonWriter.Formatting = Formatting.None;
4545
object[] array = mMessageFormatter.Format(message);
4646
mSerializer.Serialize(jsonWriter, array);
4747
string result = writer.ToString();
48-
48+
4949
mLogger.DebugFormat("Formatted message {JsonMessage}", result);
5050
return result;
5151
}
@@ -59,17 +59,15 @@ public WampMessage<JToken> Parse(Stream stream)
5959
{
6060
try
6161
{
62-
using (JsonReader reader = new JsonTextReader(new StreamReader(stream)) {CloseInput = false})
63-
{
64-
JToken parsed = JToken.ReadFrom(reader);
65-
66-
if (mLogger.IsDebugEnabled())
67-
{
68-
mLogger.DebugFormat("Trying to parse message {JsonMessage}", parsed.ToString(Formatting.None));
69-
}
62+
using JsonReader reader = new JsonTextReader(new StreamReader(stream)) {CloseInput = false};
63+
JToken parsed = JToken.ReadFrom(reader);
7064

71-
return mMessageFormatter.Parse(parsed);
65+
if (mLogger.IsDebugEnabled())
66+
{
67+
mLogger.DebugFormat("Trying to parse message {JsonMessage}", parsed.ToString(Formatting.None));
7268
}
69+
70+
return mMessageFormatter.Parse(parsed);
7371
}
7472
catch (Exception ex)
7573
{

src/netstandard/Extensions/WampSharp.Owin/Owin/OwinWebSocketTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public OwinWebSocketTransport
2727
base(authenticatorFactory)
2828
{
2929
mHandler = this.EmptyHandler;
30-
app.Use(HttpHandler);
30+
AppBuilderUseExtensions.Use(app, HttpHandler);
3131
mMaxFrameSize = maxFrameSize;
3232
}
3333

src/netstandard/Extensions/WampSharp.WebSockets/WebSockets/WebSocketWrapperConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ private ArraySegment<byte> GetMessageInBytes(WampMessage<object> message)
9292

9393
protected abstract WebSocketMessageType WebSocketMessageType { get; }
9494

95-
protected async void InnerConnect()
95+
protected void InnerConnect()
9696
{
97-
bool connected = await TryConnect();
97+
bool connected = Task.Run(TryConnect, mCancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
9898

9999
if (connected)
100100
{
101-
await Task.Run(this.RunAsync, mCancellationToken).ConfigureAwait(false);
101+
Task.Run(RunAsync, mCancellationToken);
102102
}
103103
}
104104

@@ -138,7 +138,7 @@ data is very small.
138138
// Buffer for received bits.
139139
ArraySegment<byte> receivedDataBuffer = new ArraySegment<byte>(new byte[maxMessageSize]);
140140

141-
MemoryStream memoryStream = new MemoryStream();
141+
using MemoryStream memoryStream = new MemoryStream();
142142

143143
// Checks WebSocket state.
144144
while (IsConnected && !mCancellationToken.IsCancellationRequested)

src/netstandard/Tests/WampSharp.Tests.Wampv2/Client/Callee/InvocationCalleeeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public override void Act()
129129
IWampChannel channel =
130130
playground.GetChannel(mDealer, "realm1", mBinding);
131131

132-
channel.Open();
132+
channel.Open().Wait();
133133

134134
Task register =
135135
channel.RealmProxy.RpcCatalog.Register(mOperation, new RegisterOptions());

src/netstandard/Tests/WampSharp.Tests.Wampv2/Client/Caller/CallerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override void Act()
9595
IWampChannel channel =
9696
playground.GetChannel(mServerMock, "realm1", mBinding);
9797

98-
channel.Open();
98+
channel.Open().Wait();
9999

100100
mCallAction(channel.RealmProxy.RpcCatalog, mCallbackMock);
101101
}

src/netstandard/Tests/WampSharp.Tests.Wampv2/Integration/AuthenticationClientTests.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Runtime.Serialization;
4+
using System.Threading.Tasks;
45
using Newtonsoft.Json.Linq;
56
using NUnit.Framework;
67
using WampSharp.Binding;
@@ -25,7 +26,7 @@ public void AuthenticatorSendsDetailsToHello()
2526
new CustomAuthenticator
2627
{
2728
AuthenticationId = "peter",
28-
AuthenticationMethods = new string[] {"ticket"}
29+
AuthenticationMethods = new string[] { "ticket" }
2930
};
3031

3132
HelloMock mock = new HelloMock();
@@ -41,6 +42,7 @@ public void AuthenticatorSendsDetailsToHello()
4142

4243
channel.Open();
4344

45+
mock.HelloCalled.Task.Wait();
4446
IDictionary<string, ISerializedValue> deserializedDetails =
4547
mock.Details.OriginalValue.Deserialize<IDictionary<string, ISerializedValue>>
4648
();
@@ -84,8 +86,9 @@ public void AuthenticatorGetsChallengeMessage()
8486

8587
channel.Open();
8688

89+
authenticator.Authenticated.Task.Wait();
8790
Assert.That(authenticator.AuthMethod, Is.EqualTo("ticket"));
88-
91+
8992
Assert.That(authenticator.Extra.OriginalValue.Deserialize<MyChallengeDetails>(),
9093
Is.EqualTo(myChallengeDetails));
9194
}
@@ -101,13 +104,13 @@ public void AuthenticatorAuthenticateResultCallsAuthenticate()
101104
{
102105
return new AuthenticationResponse()
103106
{
104-
Extra = new MyAuthenticateExtraData() {Secret1 = 3},
107+
Extra = new MyAuthenticateExtraData() { Secret1 = 3 },
105108
Signature = "secretsignature"
106109
};
107110
})
108111
{
109112
AuthenticationId = "peter",
110-
AuthenticationMethods = new string[] {"ticket"}
113+
AuthenticationMethods = new string[] { "ticket" }
111114
};
112115

113116
AuthenticateMock mock = new AuthenticateMock("ticket");
@@ -123,10 +126,11 @@ public void AuthenticatorAuthenticateResultCallsAuthenticate()
123126

124127
channel.Open();
125128

129+
mock.Authenticated.Task.Wait();
126130
Assert.That(mock.Signature,
127131
Is.EqualTo("secretsignature"));
128132

129-
IDictionary<string, ISerializedValue> deserializedExtra =
133+
IDictionary<string, ISerializedValue> deserializedExtra =
130134
mock.Extra.OriginalValue.Deserialize<IDictionary<string, ISerializedValue>>();
131135

132136
Assert.That(deserializedExtra["secret1"].Deserialize<int>(),
@@ -166,7 +170,7 @@ public void AuthenticatorAuthenticateExceptionCallsAbort()
166170
jsonBinding,
167171
authenticator);
168172

169-
channel.Open();
173+
Assert.ThrowsAsync<WampAuthenticationException>(channel.Open);
170174

171175
Assert.That(mock.Reason,
172176
Is.EqualTo("some reason"));
@@ -194,7 +198,7 @@ public override bool Equals(object obj)
194198
if (ReferenceEquals(null, obj)) return false;
195199
if (ReferenceEquals(this, obj)) return true;
196200
if (obj.GetType() != this.GetType()) return false;
197-
return Equals((MyAbortDetails) obj);
201+
return Equals((MyAbortDetails)obj);
198202
}
199203

200204
public override int GetHashCode()
@@ -221,6 +225,7 @@ public override void Abort(IWampSessionClient client, AbortDetails details, stri
221225

222226
private class AuthenticateMock : ChallengeMock
223227
{
228+
public TaskCompletionSource<int> Authenticated { get; } = new TaskCompletionSource<int>();
224229
public string Signature { get; private set; }
225230
public AuthenticateExtraData Extra { get; private set; }
226231

@@ -232,6 +237,7 @@ public override void Authenticate(IWampSessionClient client, string signature, A
232237
{
233238
Extra = extra;
234239
Signature = signature;
240+
Authenticated.SetResult(1);
235241
}
236242
}
237243

@@ -262,12 +268,14 @@ private class HelloMock : HelloMock<JToken>
262268

263269
private class HelloMock<TMessage> : MockServer<TMessage>
264270
{
271+
public TaskCompletionSource<int> HelloCalled { get; } = new TaskCompletionSource<int>();
265272

266273
public HelloDetails Details { get; private set; }
267274

268275
public override void Hello(IWampSessionClient client, string realm, HelloDetails details)
269276
{
270277
Details = details;
278+
HelloCalled.SetResult(0);
271279
}
272280
}
273281

@@ -392,8 +400,9 @@ public void Unsubscribe(IWampSubscriber subscriber, long requestId, long subscri
392400
private class CustomAuthenticator : IWampClientAuthenticator
393401
{
394402
private readonly Func<string, ChallengeDetails, AuthenticationResponse> mAuthenticate;
403+
public TaskCompletionSource<int> Authenticated { get; } = new TaskCompletionSource<int>();
395404

396-
public CustomAuthenticator() :
405+
public CustomAuthenticator() :
397406
this((authMethod, extra) => new AuthenticationResponse())
398407
{
399408
}
@@ -407,6 +416,7 @@ public AuthenticationResponse Authenticate(string authmethod, ChallengeDetails e
407416
{
408417
Extra = extra;
409418
AuthMethod = authmethod;
419+
Authenticated.SetResult(0);
410420
return mAuthenticate(authmethod, extra);
411421
}
412422

@@ -435,7 +445,7 @@ public override bool Equals(object obj)
435445
if (ReferenceEquals(null, obj)) return false;
436446
if (ReferenceEquals(this, obj)) return true;
437447
if (obj.GetType() != this.GetType()) return false;
438-
return Equals((MyChallengeDetails) obj);
448+
return Equals((MyChallengeDetails)obj);
439449
}
440450

441451
public override int GetHashCode()

src/netstandard/Tests/WampSharp.Tests.Wampv2/Integration/CallerDealerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public async Task ErrorsService(int number, object[] arguments, string errorUri)
102102
}
103103
else
104104
{
105+
await Task.WhenAny(result);
105106
AggregateException exception = result.Exception;
106107
Exception actualException = exception.InnerException;
107108
Assert.That(actualException, Is.TypeOf<WampException>());

src/netstandard/WampSharp/Core/Utilities/TaskExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static Task<TResult> ContinueWithSafe<TTask, TResult>(this TTask task, F
136136
throw new ArgumentNullException(nameof(transform));
137137
}
138138

139-
TaskCompletionSource<TResult> taskResult = new TaskCompletionSource<TResult>();
139+
TaskCompletionSource<TResult> taskResult = new TaskCompletionSource<TResult>(TaskCreationOptions.RunContinuationsAsynchronously);
140140

141141
task.ContinueWith(_ =>
142142
{

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/Async/AsyncOperationCallback.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace WampSharp.V2.CalleeProxy
1010
{
1111
internal class AsyncOperationCallback<TResult> : IWampRawRpcOperationClientCallback
1212
{
13-
private readonly TaskCompletionSource<TResult> mTask = new TaskCompletionSource<TResult>();
13+
private readonly TaskCompletionSource<TResult> mTask = new TaskCompletionSource<TResult>(TaskCreationOptions.RunContinuationsAsynchronously);
1414
private readonly IOperationResultExtractor<TResult> mResultExtractor;
1515

1616
public AsyncOperationCallback(IOperationResultExtractor<TResult> resultExtractor)

src/netstandard/WampSharp/WAMP2/V2/Api/CalleeProxy/Callbacks/Sync/SyncCallbackBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace WampSharp.V2.CalleeProxy
1010
{
11-
internal abstract class SyncCallbackBase : IWampRawRpcOperationClientCallback
11+
internal abstract class SyncCallbackBase : IWampRawRpcOperationClientCallback, IDisposable
1212
{
1313
private readonly ManualResetEvent mWaitHandle = new ManualResetEvent(false);
1414

@@ -58,5 +58,10 @@ public void SetException(Exception exception)
5858
Exception = exception;
5959
mWaitHandle.Set();
6060
}
61+
62+
public void Dispose()
63+
{
64+
mWaitHandle?.Dispose();
65+
}
6166
}
6267
}

0 commit comments

Comments
 (0)