6
6
//-----------------------------------------------------------------------
7
7
8
8
using System ;
9
+ using System . Threading ;
10
+ using System . Threading . Tasks ;
9
11
using Akka . TestKit ;
10
12
using Akka . Actor ;
11
13
using Akka . Streams . Implementation ;
@@ -76,30 +78,57 @@ public void Cancel()
76
78
PublisherProbe . Ref . Tell ( new TestPublisher . CancelSubscription ( this ) ) ;
77
79
}
78
80
79
- public void ExpectRequest ( long n )
81
+ public void ExpectRequest ( long n , CancellationToken cancellationToken = default )
80
82
{
81
- PublisherProbe . ExpectMsg < TestPublisher . RequestMore > (
82
- x => x . NrOfElements == n && Equals ( x . Subscription , this ) ) ;
83
+ ExpectRequestAsync ( n , cancellationToken )
84
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
83
85
}
84
86
85
- public long ExpectRequest ( )
87
+ public async Task ExpectRequestAsync ( long n , CancellationToken cancellationToken = default )
86
88
{
87
- return
88
- PublisherProbe . ExpectMsg < TestPublisher . RequestMore > ( x => Equals ( this , x . Subscription ) ) . NrOfElements ;
89
+ await PublisherProbe . ExpectMsgAsync < TestPublisher . RequestMore > (
90
+ isMessage : x => x . NrOfElements == n && Equals ( x . Subscription , this ) ,
91
+ cancellationToken : cancellationToken )
92
+ . ConfigureAwait ( false ) ;
89
93
}
90
94
91
- public void ExpectCancellation ( )
95
+ public long ExpectRequest ( CancellationToken cancellationToken = default )
92
96
{
93
- PublisherProbe . FishForMessage ( msg =>
94
- {
95
- if ( msg is TestPublisher . CancelSubscription &&
96
- Equals ( ( ( TestPublisher . CancelSubscription ) msg ) . Subscription , this ) ) return true ;
97
- if ( msg is TestPublisher . RequestMore && Equals ( ( ( TestPublisher . RequestMore ) msg ) . Subscription , this ) )
98
- return false ;
99
- return false ;
100
- } ) ;
97
+ return ExpectRequestAsync ( cancellationToken )
98
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
101
99
}
102
100
101
+ public async Task < long > ExpectRequestAsync ( CancellationToken cancellationToken = default )
102
+ {
103
+ var msg = await PublisherProbe . ExpectMsgAsync < TestPublisher . RequestMore > (
104
+ isMessage : x => Equals ( this , x . Subscription ) ,
105
+ cancellationToken : cancellationToken )
106
+ . ConfigureAwait ( false ) ;
107
+ return msg . NrOfElements ;
108
+ }
109
+
110
+ public void ExpectCancellation ( CancellationToken cancellationToken = default )
111
+ {
112
+ ExpectCancellationAsync ( cancellationToken )
113
+ . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
114
+ }
115
+
116
+ public async Task ExpectCancellationAsync ( CancellationToken cancellationToken = default )
117
+ {
118
+ await PublisherProbe . FishForMessageAsync (
119
+ isMessage : msg =>
120
+ {
121
+ return msg switch
122
+ {
123
+ TestPublisher . CancelSubscription cancel when Equals ( cancel . Subscription , this ) => true ,
124
+ TestPublisher . RequestMore more when Equals ( more . Subscription , this ) => false ,
125
+ _ => false
126
+ } ;
127
+ } ,
128
+ cancellationToken : cancellationToken )
129
+ . ConfigureAwait ( false ) ;
130
+ }
131
+
103
132
public void SendNext ( T element ) => Subscriber . OnNext ( element ) ;
104
133
105
134
public void SendComplete ( ) => Subscriber . OnComplete ( ) ;
@@ -112,15 +141,14 @@ public void ExpectCancellation()
112
141
internal sealed class ProbeSource < T > : SourceModule < T , TestPublisher . Probe < T > >
113
142
{
114
143
private readonly TestKitBase _testKit ;
115
- private readonly Attributes _attributes ;
116
144
117
145
public ProbeSource ( TestKitBase testKit , Attributes attributes , SourceShape < T > shape ) : base ( shape )
118
146
{
119
147
_testKit = testKit ;
120
- _attributes = attributes ;
148
+ Attributes = attributes ;
121
149
}
122
150
123
- public override Attributes Attributes => _attributes ;
151
+ public override Attributes Attributes { get ; }
124
152
125
153
public override IModule WithAttributes ( Attributes attributes )
126
154
{
@@ -129,7 +157,7 @@ public override IModule WithAttributes(Attributes attributes)
129
157
130
158
protected override SourceModule < T , TestPublisher . Probe < T > > NewInstance ( SourceShape < T > shape )
131
159
{
132
- return new ProbeSource < T > ( _testKit , _attributes , shape ) ;
160
+ return new ProbeSource < T > ( _testKit , Attributes , shape ) ;
133
161
}
134
162
135
163
public override IPublisher < T > Create ( MaterializationContext context , out TestPublisher . Probe < T > materializer )
@@ -142,15 +170,14 @@ public override IPublisher<T> Create(MaterializationContext context, out TestPub
142
170
internal sealed class ProbeSink < T > : SinkModule < T , TestSubscriber . Probe < T > >
143
171
{
144
172
private readonly TestKitBase _testKit ;
145
- private readonly Attributes _attributes ;
146
173
147
174
public ProbeSink ( TestKitBase testKit , Attributes attributes , SinkShape < T > shape ) : base ( shape )
148
175
{
149
176
_testKit = testKit ;
150
- _attributes = attributes ;
177
+ Attributes = attributes ;
151
178
}
152
179
153
- public override Attributes Attributes => _attributes ;
180
+ public override Attributes Attributes { get ; }
154
181
155
182
public override IModule WithAttributes ( Attributes attributes )
156
183
{
@@ -159,7 +186,7 @@ public override IModule WithAttributes(Attributes attributes)
159
186
160
187
protected override SinkModule < T , TestSubscriber . Probe < T > > NewInstance ( SinkShape < T > shape )
161
188
{
162
- return new ProbeSink < T > ( _testKit , _attributes , shape ) ;
189
+ return new ProbeSink < T > ( _testKit , Attributes , shape ) ;
163
190
}
164
191
165
192
public override object Create ( MaterializationContext context , out TestSubscriber . Probe < T > materializer )
0 commit comments