1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Reactive . Disposables ;
5
+ using System . Reactive . Linq ;
4
6
using System . Threading . Tasks ;
5
7
using NUnit . Framework ;
6
8
using WampSharp . Core . Serialization ;
@@ -64,6 +66,42 @@ public async Task ProgressiveCallsCalleeProxyProgress()
64
66
Assert . That ( result . Result , Is . EqualTo ( 10 ) ) ;
65
67
}
66
68
69
+ [ Test ]
70
+ public async Task ProgressiveCallsCalleeProxyObservable ( )
71
+ {
72
+ WampPlayground playground = new WampPlayground ( ) ;
73
+
74
+ CallerCallee dualChannel = await playground . GetCallerCalleeDualChannel ( ) ;
75
+ IWampChannel calleeChannel = dualChannel . CalleeChannel ;
76
+ IWampChannel callerChannel = dualChannel . CallerChannel ;
77
+
78
+ MyOperation myOperation = new MyOperation ( ) ;
79
+
80
+ await calleeChannel . RealmProxy . RpcCatalog . Register ( myOperation , new RegisterOptions ( ) ) ;
81
+ ILongOpObsService proxy = callerChannel . RealmProxy . Services . GetCalleeProxy < ILongOpObsService > ( ) ;
82
+
83
+ IEnumerable < int > results = proxy . LongOp ( 9 , false ) . ToEnumerable ( ) ; // it will emit one more than asked
84
+
85
+ CollectionAssert . AreEquivalent ( Enumerable . Range ( 0 , 10 ) , results ) ;
86
+ }
87
+
88
+ [ Test ]
89
+ public async Task ProgressiveCallsCalleeProxyObservableError ( )
90
+ {
91
+ WampPlayground playground = new WampPlayground ( ) ;
92
+
93
+ CallerCallee dualChannel = await playground . GetCallerCalleeDualChannel ( ) ;
94
+ IWampChannel calleeChannel = dualChannel . CalleeChannel ;
95
+ IWampChannel callerChannel = dualChannel . CallerChannel ;
96
+
97
+ MyOperation myOperation = new MyOperation ( ) ;
98
+
99
+ await calleeChannel . RealmProxy . RpcCatalog . Register ( myOperation , new RegisterOptions ( ) ) ;
100
+ ILongOpObsService proxy = callerChannel . RealmProxy . Services . GetCalleeProxy < ILongOpObsService > ( ) ;
101
+
102
+ Assert . Throws ( typeof ( WampException ) , ( ) => proxy . LongOp ( 9 , true ) . ToEnumerable ( ) . Count ( ) ) ;
103
+ }
104
+
67
105
public class MyOperation : IWampRpcOperation
68
106
{
69
107
public string Procedure => "com.myapp.longop" ;
@@ -80,16 +118,27 @@ public IWampCancellableInvocation Invoke<TMessage>(IWampRawRpcOperationRouterCal
80
118
TMessage number = arguments [ 0 ] ;
81
119
int n = formatter . Deserialize < int > ( number ) ;
82
120
121
+ bool endWithError = arguments . Length > 1 && formatter . Deserialize < bool > ( arguments [ 1 ] ) ;
122
+
83
123
for ( int i = 0 ; i < n ; i ++ )
84
124
{
85
125
caller . Result ( WampObjectFormatter . Value ,
86
126
new YieldOptions { Progress = true } ,
87
127
new object [ ] { i } ) ;
88
128
}
89
129
90
- caller . Result ( WampObjectFormatter . Value ,
91
- new YieldOptions ( ) ,
92
- new object [ ] { n } ) ;
130
+ if ( endWithError )
131
+ {
132
+ caller . Error ( WampObjectFormatter . Value ,
133
+ new Dictionary < string , string > ( ) ,
134
+ "Something bad happened" ) ;
135
+ }
136
+ else
137
+ {
138
+ caller . Result ( WampObjectFormatter . Value ,
139
+ new YieldOptions ( ) ,
140
+ new object [ ] { n } ) ;
141
+ }
93
142
94
143
return null ;
95
144
}
@@ -122,6 +171,31 @@ public async Task<int> LongOp(int n, IProgress<int> progress)
122
171
}
123
172
}
124
173
174
+ public interface ILongOpObsService
175
+ {
176
+ [ WampProcedure ( "com.myapp.longop" ) ]
177
+ [ WampProgressiveResultProcedure ]
178
+ IObservable < int > LongOp ( int n , bool endWithError ) ;
179
+ }
180
+
181
+ public class LongOpObsService : ILongOpObsService
182
+ {
183
+ public IObservable < int > LongOp ( int n , bool endWithError ) => Observable . Create < int > ( async obs =>
184
+ {
185
+ for ( int i = 0 ; i < n ; i ++ )
186
+ {
187
+ obs . OnNext ( i ) ;
188
+ await Task . Delay ( 100 ) ;
189
+ }
190
+ if ( endWithError )
191
+ obs . OnError ( new WampException ( "wamp.error" , "Something bad happened" ) ) ;
192
+ else
193
+ obs . OnCompleted ( ) ;
194
+
195
+ return Disposable . Empty ;
196
+ } ) ;
197
+ }
198
+
125
199
public class MyCallback : IWampRawRpcOperationClientCallback
126
200
{
127
201
private readonly TaskCompletionSource < int > mTask = new TaskCompletionSource < int > ( ) ;
@@ -187,4 +261,4 @@ public void Report(T value)
187
261
mAction ( value ) ;
188
262
}
189
263
}
190
- }
264
+ }
0 commit comments