Skip to content

Commit 47dbeb3

Browse files
alexandru-calinoiuElad Zelingher
authored andcommitted
Throw caught exception on OpenAsync
renames test open will throw and exception Refactored some stuff
1 parent 84fae73 commit 47dbeb3

File tree

6 files changed

+155
-111
lines changed

6 files changed

+155
-111
lines changed

src/WampSharp.Tests/Api/PubSubTests.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,5 @@ public void TopicOnNextCallsSubjectOnNext()
4545

4646
Assert.That(@event, Is.EqualTo(value));
4747
}
48-
49-
[Test]
50-
public void OpenWillNotBlockOnConnectionLost()
51-
{
52-
var wampChannelFactory = new WampChannelFactory<MockRaw>(new MockRawFormatter());
53-
var mockControlledWampConnection = new MockControlledWampConnection<MockRaw>();
54-
55-
var wampChannel = wampChannelFactory.CreateChannel(mockControlledWampConnection);
56-
var openAsync = wampChannel.OpenAsync();
57-
58-
Assert.IsFalse(openAsync.IsCompleted);
59-
mockControlledWampConnection.OnCompleted();
60-
Assert.IsTrue(openAsync.IsCompleted);
61-
}
6248
}
6349
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using NUnit.Framework;
3+
using WampSharp.Tests.TestHelpers;
4+
5+
namespace WampSharp.Tests.Api
6+
{
7+
[TestFixture]
8+
public class WampChannelTests
9+
{
10+
[Test]
11+
public void OpenAsyncWillNotBlockOnConnectionLost()
12+
{
13+
var wampChannelFactory = new WampChannelFactory<MockRaw>(new MockRawFormatter());
14+
var mockControlledWampConnection = new MockControlledWampConnection<MockRaw>();
15+
16+
var wampChannel = wampChannelFactory.CreateChannel(mockControlledWampConnection);
17+
var openAsync = wampChannel.OpenAsync();
18+
19+
Assert.IsFalse(openAsync.IsCompleted);
20+
mockControlledWampConnection.OnCompleted();
21+
Assert.IsTrue(openAsync.IsCompleted);
22+
}
23+
24+
[Test]
25+
public void OpenAsyncWillThrowAndExpcetionIfAnErrorOccurs()
26+
{
27+
var wampChannelFactory = new WampChannelFactory<MockRaw>(new MockRawFormatter());
28+
var mockControlledWampConnection = new MockControlledWampConnection<MockRaw>();
29+
30+
var wampChannel = wampChannelFactory.CreateChannel(mockControlledWampConnection);
31+
var openAsyncTask = wampChannel.OpenAsync();
32+
33+
Exception myException = new Exception("My exception");
34+
35+
mockControlledWampConnection.OnError(myException);
36+
37+
Assert.IsNotNull(openAsyncTask.Exception);
38+
39+
Assert.That(openAsyncTask.Exception.InnerException, Is.EqualTo(myException));
40+
}
41+
}
42+
}

src/WampSharp.Tests/WampSharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="Api\PubSubTests.cs" />
6464
<Compile Include="Api\RpcClientTests.cs" />
6565
<Compile Include="Api\RpcServerTests.cs" />
66+
<Compile Include="Api\WampChannelTests.cs" />
6667
<Compile Include="Cra\CraTests.cs" />
6768
<Compile Include="Cra\Helpers\MockWampCraAuthenticaticationBuilder.cs" />
6869
<Compile Include="Cra\Helpers\MockWampCraAuthenticator.cs" />
Lines changed: 106 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,122 @@
1-
using System;
2-
using System.Reactive;
3-
using System.Reactive.Linq;
4-
using System.Reactive.Subjects;
5-
using System.Reactive.Threading.Tasks;
6-
using System.Threading.Tasks;
7-
using WampSharp.Auxiliary.Client;
8-
using WampSharp.Core.Client;
9-
using WampSharp.Core.Contracts.V1;
10-
using WampSharp.Core.Listener;
11-
using WampSharp.PubSub.Client;
12-
using WampSharp.Rpc.Client;
13-
14-
namespace WampSharp
15-
{
16-
public class WampChannel<TMessage> : IWampChannel<TMessage>
17-
{
18-
private readonly IControlledWampConnection<TMessage> mConnection;
19-
private readonly IWampRpcClientFactory<TMessage> mRpcClientFactory;
20-
private readonly IWampPubSubClientFactory<TMessage> mPubSubClientFactory;
21-
private readonly WampServerProxyBuilder<TMessage, IWampClient<TMessage>, IWampServer> mServerProxyBuilder;
22-
private readonly IWampClientConnectionMonitor mConnectionMonitor;
23-
24-
public WampChannel(IControlledWampConnection<TMessage> connection,
25-
IWampRpcClientFactory<TMessage> rpcClientFactory,
26-
IWampPubSubClientFactory<TMessage> pubSubClientFactory,
27-
WampServerProxyBuilder<TMessage, IWampClient<TMessage>, IWampServer> serverProxyBuilder,
28-
IWampAuxiliaryClientFactory<TMessage> connectionMonitorFactory)
29-
{
30-
mConnection = connection;
31-
mRpcClientFactory = rpcClientFactory;
32-
mPubSubClientFactory = pubSubClientFactory;
33-
mServerProxyBuilder = serverProxyBuilder;
34-
mConnectionMonitor = connectionMonitorFactory.CreateMonitor(connection);
35-
}
36-
37-
public IWampServer GetServerProxy(IWampClient<TMessage> callbackClient)
38-
{
39-
return mServerProxyBuilder.Create(callbackClient, mConnection);
40-
}
41-
42-
public TProxy GetRpcProxy<TProxy>() where TProxy : class
43-
{
44-
return mRpcClientFactory.GetClient<TProxy>(mConnection);
45-
}
46-
47-
public dynamic GetDynamicRpcProxy()
48-
{
49-
return mRpcClientFactory.GetDynamicClient(mConnection);
50-
}
51-
52-
public ISubject<T> GetSubject<T>(string topicUri)
53-
{
54-
return mPubSubClientFactory.GetSubject<T>(topicUri, mConnection);
55-
}
56-
57-
public void MapPrefix(string prefix, string uri)
58-
{
59-
mConnectionMonitor.MapPrefix(prefix, uri);
60-
}
61-
62-
public IWampClientConnectionMonitor GetMonitor()
63-
{
64-
return mConnectionMonitor;
65-
}
66-
67-
public void Open()
68-
{
69-
Task task = OpenAsync();
70-
task.Wait();
71-
}
72-
73-
public Task OpenAsync()
74-
{
75-
var connectedObservable =
76-
Observable.FromEventPattern<WampConnectionEstablishedEventArgs>
77-
(x => mConnectionMonitor.ConnectionEstablished += x,
78-
x => mConnectionMonitor.ConnectionEstablished -= x)
79-
.Select(x => Unit.Default);
80-
81-
var errorObservable =
1+
using System;
2+
using System.Reactive;
3+
using System.Reactive.Linq;
4+
using System.Reactive.Subjects;
5+
using System.Reactive.Threading.Tasks;
6+
using System.Threading.Tasks;
7+
using WampSharp.Auxiliary.Client;
8+
using WampSharp.Core.Client;
9+
using WampSharp.Core.Contracts.V1;
10+
using WampSharp.Core.Listener;
11+
using WampSharp.PubSub.Client;
12+
using WampSharp.Rpc.Client;
13+
14+
namespace WampSharp
15+
{
16+
public class WampChannel<TMessage> : IWampChannel<TMessage>
17+
{
18+
private readonly IControlledWampConnection<TMessage> mConnection;
19+
private readonly IWampRpcClientFactory<TMessage> mRpcClientFactory;
20+
private readonly IWampPubSubClientFactory<TMessage> mPubSubClientFactory;
21+
private readonly WampServerProxyBuilder<TMessage, IWampClient<TMessage>, IWampServer> mServerProxyBuilder;
22+
private readonly IWampClientConnectionMonitor mConnectionMonitor;
23+
24+
public WampChannel(IControlledWampConnection<TMessage> connection,
25+
IWampRpcClientFactory<TMessage> rpcClientFactory,
26+
IWampPubSubClientFactory<TMessage> pubSubClientFactory,
27+
WampServerProxyBuilder<TMessage, IWampClient<TMessage>, IWampServer> serverProxyBuilder,
28+
IWampAuxiliaryClientFactory<TMessage> connectionMonitorFactory)
29+
{
30+
mConnection = connection;
31+
mRpcClientFactory = rpcClientFactory;
32+
mPubSubClientFactory = pubSubClientFactory;
33+
mServerProxyBuilder = serverProxyBuilder;
34+
mConnectionMonitor = connectionMonitorFactory.CreateMonitor(connection);
35+
}
36+
37+
public IWampServer GetServerProxy(IWampClient<TMessage> callbackClient)
38+
{
39+
return mServerProxyBuilder.Create(callbackClient, mConnection);
40+
}
41+
42+
public TProxy GetRpcProxy<TProxy>() where TProxy : class
43+
{
44+
return mRpcClientFactory.GetClient<TProxy>(mConnection);
45+
}
46+
47+
public dynamic GetDynamicRpcProxy()
48+
{
49+
return mRpcClientFactory.GetDynamicClient(mConnection);
50+
}
51+
52+
public ISubject<T> GetSubject<T>(string topicUri)
53+
{
54+
return mPubSubClientFactory.GetSubject<T>(topicUri, mConnection);
55+
}
56+
57+
public void MapPrefix(string prefix, string uri)
58+
{
59+
mConnectionMonitor.MapPrefix(prefix, uri);
60+
}
61+
62+
public IWampClientConnectionMonitor GetMonitor()
63+
{
64+
return mConnectionMonitor;
65+
}
66+
67+
public void Open()
68+
{
69+
Task task = OpenAsync();
70+
71+
try
72+
{
73+
task.Wait();
74+
}
75+
catch (AggregateException ex)
76+
{
77+
throw ex.InnerException;
78+
}
79+
}
80+
81+
public Task OpenAsync()
82+
{
83+
var connectedObservable =
84+
Observable.FromEventPattern<WampConnectionEstablishedEventArgs>
85+
(x => mConnectionMonitor.ConnectionEstablished += x,
86+
x => mConnectionMonitor.ConnectionEstablished -= x)
87+
.Select(x => Unit.Default);
88+
89+
var errorObservable =
8290
Observable.FromEventPattern<WampConnectionErrorEventArgs>
8391
(x => mConnectionMonitor.ConnectionError += x,
8492
x => mConnectionMonitor.ConnectionError -= x)
85-
.Select(x => Unit.Default);
93+
.Select(x => Observable.Throw<Unit>(x.EventArgs.Exception))
94+
.SelectMany(x => x);
8695

8796
var completedObservable =
8897
Observable.FromEventPattern
8998
(x => mConnectionMonitor.ConnectionLost += x,
9099
x => mConnectionMonitor.ConnectionLost -= x)
91-
.Select(x => Unit.Default);
100+
.Select(x => Unit.Default);
92101

93102
// Combining the observables and propagating the one that reatcs first
94103
// because we have to complete the task either when a connection is established or
95104
// an error (i.e. exception) occurs.
96-
IObservable<Unit> combined = connectedObservable.Amb(errorObservable).Amb(completedObservable);
105+
var combined = connectedObservable.Amb(errorObservable).Amb(completedObservable);
97106

98-
IObservable<Unit> firstConnectionOrError =
107+
var firstConnectionOrError =
99108
combined.Take(1);
100109

101-
Task task = firstConnectionOrError.ToTask();
102-
103-
mConnection.Connect();
104-
105-
return task;
106-
}
107-
108-
public void Close()
109-
{
110-
mConnection.OnCompleted();
111-
}
112-
}
110+
Task task = firstConnectionOrError.ToTask();
111+
112+
mConnection.Connect();
113+
114+
return task;
115+
}
116+
117+
public void Close()
118+
{
119+
mConnection.OnCompleted();
120+
}
121+
}
113122
}

src/WampSharpFw4/WampSharp.Tests/WampSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<Compile Include="..\..\WampSharp.Tests\Api\RpcServerTests.cs">
7070
<Link>Api\RpcServerTests.cs</Link>
7171
</Compile>
72+
<Compile Include="..\..\WampSharp.Tests\Api\WampChannelTests.cs">
73+
<Link>Api\WampChannelTests.cs</Link>
74+
</Compile>
7275
<Compile Include="..\..\WampSharp.Tests\Cra\CraTests.cs">
7376
<Link>Cra\CraTests.cs</Link>
7477
</Compile>

src/WampSharpMono/WampSharp.Tests/WampSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
<Compile Include="..\..\WampSharp.Tests\Api\RpcServerTests.cs">
7070
<Link>Api\RpcServerTests.cs</Link>
7171
</Compile>
72+
<Compile Include="..\..\WampSharp.Tests\Api\WampChannelTests.cs">
73+
<Link>Api\WampChannelTests.cs</Link>
74+
</Compile>
7275
<Compile Include="..\..\WampSharp.Tests\Cra\CraTests.cs">
7376
<Link>Cra\CraTests.cs</Link>
7477
</Compile>

0 commit comments

Comments
 (0)