11
11
using System . Collections . Generic ;
12
12
using System . Linq ;
13
13
using System . Threading ;
14
+ using System . Threading . Tasks ;
14
15
15
16
namespace Akka . TestKit . Internal
16
17
{
@@ -20,54 +21,46 @@ namespace Akka.TestKit.Internal
20
21
/// <remarks>Note! Part of internal API. Breaking changes may occur without notice. Use at own risk.</remarks>
21
22
/// </summary>
22
23
/// <typeparam name="T">The type of item to store.</typeparam>
23
- public class BlockingQueue < T >
24
+ public class BlockingQueue < T > : ITestQueue < T >
24
25
{
25
26
private readonly BlockingCollection < Positioned > _collection = new BlockingCollection < Positioned > ( new QueueWithAddFirst ( ) ) ;
26
27
27
- /// <summary>
28
- /// The number of items that are currently in the queue.
29
- /// </summary>
28
+ /// <inheritdoc cref="Count"/>
30
29
public int Count { get { return _collection . Count ; } }
31
30
32
- /// <summary>
33
- /// Adds the specified item to the end of the queue.
34
- /// </summary>
35
- /// <param name="item">The item to add to the queue.</param>
31
+ /// <inheritdoc cref="Enqueue"/>
36
32
public void Enqueue ( T item )
37
33
{
38
34
if ( ! _collection . TryAdd ( new Positioned ( item ) ) )
39
35
throw new InvalidOperationException ( "Failed to enqueue item into the queue." ) ;
40
36
}
41
37
42
- /// <summary>
43
- /// Adds the specified item to the front of the queue.
44
- /// </summary>
45
- /// <param name="item">The item to add to the queue.</param>
38
+ /// <inheritdoc cref="EnqueueAsync"/>
39
+ public async ValueTask EnqueueAsync ( T item )
40
+ {
41
+ Enqueue ( item ) ;
42
+ }
43
+
46
44
[ Obsolete ( "This method will be removed from the public API in the future" ) ]
47
45
public void AddFirst ( T item )
48
46
{
49
47
if ( ! _collection . TryAdd ( new Positioned ( item , first : true ) ) )
50
48
throw new InvalidOperationException ( "Failed to enqueue item into the head of the queue." ) ;
51
49
}
52
50
53
- /// <summary>
54
- /// Tries to add the specified item to the end of the queue within the specified time period.
55
- /// A token can be provided to cancel the operation if needed.
56
- /// </summary>
57
- /// <param name="item">The item to add to the queue.</param>
58
- /// <param name="millisecondsTimeout">The number of milliseconds to wait for the add to complete.</param>
59
- /// <param name="cancellationToken">The cancellation token that can be used to cancel the operation.</param>
60
- /// <returns><c>true</c> if the add completed within the specified timeout; otherwise, <c>false</c>.</returns>
51
+ /// <inheritdoc cref="TryEnqueue"/>
61
52
public bool TryEnqueue ( T item , int millisecondsTimeout , CancellationToken cancellationToken )
62
53
{
63
54
return _collection . TryAdd ( new Positioned ( item ) , millisecondsTimeout , cancellationToken ) ;
64
55
}
65
56
66
- /// <summary>
67
- /// Tries to remove the specified item from the queue.
68
- /// </summary>
69
- /// <param name="item">The item to remove from the queue.</param>
70
- /// <returns><c>true</c> if the item was removed; otherwise, <c>false</c>.</returns>
57
+ /// <inheritdoc cref="TryEnqueueAsync"/>
58
+ public async ValueTask < bool > TryEnqueueAsync ( T item , int millisecondsTimeout , CancellationToken cancellationToken )
59
+ {
60
+ return TryEnqueue ( item , millisecondsTimeout , cancellationToken ) ;
61
+ }
62
+
63
+ /// <inheritdoc cref="TryTake(out T)"/>
71
64
public bool TryTake ( out T item )
72
65
{
73
66
if ( _collection . TryTake ( out var p ) )
@@ -79,14 +72,14 @@ public bool TryTake(out T item)
79
72
return false ;
80
73
}
81
74
82
- /// <summary >
83
- /// Tries to remove the specified item from the queue within the specified time period.
84
- /// A token can be provided to cancel the operation if needed.
85
- /// </summary>
86
- /// <param name="item">The item to remove from the queue.</param>
87
- /// <param name="millisecondsTimeout">The number of milliseconds to wait for the remove to complete.</param>
88
- /// <param name="cancellationToken">The cancellation token that can be used to cancel the operation.</param>
89
- /// <returns><c>true</c> if the remove completed within the specified timeout; otherwise, <c>false</c>.</returns >
75
+ /// <inheritdoc cref="TryTakeAsync()"/ >
76
+ public async ValueTask < ( bool success , T item ) > TryTakeAsync ( )
77
+ {
78
+ var result = TryTake ( out var item ) ;
79
+ return ( result , item ) ;
80
+ }
81
+
82
+ /// <inheritdoc cref="TryTake(out T, int, CancellationToken)"/ >
90
83
public bool TryTake ( out T item , int millisecondsTimeout , CancellationToken cancellationToken )
91
84
{
92
85
if ( _collection . TryTake ( out var p , millisecondsTimeout , cancellationToken ) )
@@ -98,27 +91,29 @@ public bool TryTake(out T item, int millisecondsTimeout, CancellationToken cance
98
91
return false ;
99
92
}
100
93
101
- /// <summary >
102
- /// Removes an item from the collection.
103
- /// </summary>
104
- /// <param name="cancellationToken">The cancellation token that can be used to cancel the operation.</param>
105
- /// <exception cref="OperationCanceledException">
106
- /// This exception is thrown when the operation is canceled.
107
- /// </exception>
108
- /// <returns>The item removed from the collection.</returns >
94
+ /// <inheritdoc cref="TryTakeAsync(int, CancellationToken)"/ >
95
+ public async ValueTask < ( bool success , T item ) > TryTakeAsync ( int millisecondsTimeout , CancellationToken cancellationToken )
96
+ {
97
+ var result = TryTake ( out var item , millisecondsTimeout , cancellationToken ) ;
98
+ return ( result , item ) ;
99
+ }
100
+
101
+ /// <inheritdoc cref="Take"/ >
109
102
public T Take ( CancellationToken cancellationToken )
110
103
{
111
104
var p = _collection . Take ( cancellationToken ) ;
112
105
return p . Value ;
113
106
}
114
107
108
+ /// <inheritdoc cref="TakeAsync"/>
109
+ public async ValueTask < T > TakeAsync ( CancellationToken cancellationToken )
110
+ {
111
+ return _collection . Take ( cancellationToken ) . Value ;
112
+ }
113
+
115
114
#region Peek methods
116
115
117
- /// <summary>
118
- /// Tries to remove the specified item from the queue.
119
- /// </summary>
120
- /// <param name="item">The item to remove from the queue.</param>
121
- /// <returns><c>true</c> if the item was removed; otherwise, <c>false</c>.</returns>
116
+ /// <inheritdoc cref="TryPeek(out T)"/>
122
117
public bool TryPeek ( out T item )
123
118
{
124
119
if ( _collection . TryTake ( out var p ) )
@@ -131,14 +126,19 @@ public bool TryPeek(out T item)
131
126
return false ;
132
127
}
133
128
134
- /// <summary>
135
- /// Tries to remove the specified item from the queue within the specified time period.
136
- /// A token can be provided to cancel the operation if needed.
137
- /// </summary>
138
- /// <param name="item">The item to remove from the queue.</param>
139
- /// <param name="millisecondsTimeout">The number of milliseconds to wait for the remove to complete.</param>
140
- /// <param name="cancellationToken">The cancellation token that can be used to cancel the operation.</param>
141
- /// <returns><c>true</c> if the remove completed within the specified timeout; otherwise, <c>false</c>.</returns>
129
+ /// <inheritdoc cref="TryPeekAsync()"/>
130
+ public async ValueTask < ( bool success , T item ) > TryPeekAsync ( )
131
+ {
132
+ if ( _collection . TryTake ( out var p ) )
133
+ {
134
+ var item = p . Value ;
135
+ AddFirst ( item ) ;
136
+ return ( true , item ) ;
137
+ }
138
+ return ( false , default ) ;
139
+ }
140
+
141
+ /// <inheritdoc cref="TryPeek(out T, int, CancellationToken)"/>
142
142
public bool TryPeek ( out T item , int millisecondsTimeout , CancellationToken cancellationToken )
143
143
{
144
144
if ( _collection . TryTake ( out var p , millisecondsTimeout , cancellationToken ) )
@@ -151,27 +151,36 @@ public bool TryPeek(out T item, int millisecondsTimeout, CancellationToken cance
151
151
return false ;
152
152
}
153
153
154
- /// <summary>
155
- /// Removes an item from the collection.
156
- /// </summary>
157
- /// <param name="cancellationToken">The cancellation token that can be used to cancel the operation.</param>
158
- /// <exception cref="OperationCanceledException">
159
- /// This exception is thrown when the operation is canceled.
160
- /// </exception>
161
- /// <returns>The item removed from the collection.</returns>
154
+ /// <inheritdoc cref="TryPeekAsync(int, CancellationToken)"/>
155
+ public async ValueTask < ( bool success , T item ) > TryPeekAsync ( int millisecondsTimeout , CancellationToken cancellationToken )
156
+ {
157
+ if ( _collection . TryTake ( out var p , millisecondsTimeout , cancellationToken ) )
158
+ {
159
+ var item = p . Value ;
160
+ AddFirst ( item ) ;
161
+ return ( true , item ) ;
162
+ }
163
+ return ( false , default ) ;
164
+ }
165
+
166
+ /// <inheritdoc cref="Peek"/>
162
167
public T Peek ( CancellationToken cancellationToken )
163
168
{
164
169
var p = _collection . Take ( cancellationToken ) ;
165
170
AddFirst ( p . Value ) ;
166
171
return p . Value ;
167
172
}
168
173
174
+ /// <inheritdoc cref="PeekAsync"/>
175
+ public async ValueTask < T > PeekAsync ( CancellationToken cancellationToken )
176
+ {
177
+ var val = _collection . Take ( cancellationToken ) . Value ;
178
+ AddFirst ( val ) ;
179
+ return val ;
180
+ }
169
181
#endregion
170
182
171
- /// <summary>
172
- /// Copies the items from the <see cref="BlockingQueue{T}"/> instance into a new <see cref="List{T}"/>.
173
- /// </summary>
174
- /// <returns>A <see cref="List{T}"/> containing copies of the elements of the collection</returns>
183
+ /// <inheritdoc cref="ToList"/>
175
184
public List < T > ToList ( )
176
185
{
177
186
var positionArray = _collection . ToArray ( ) ;
0 commit comments