30
30
//---------------------------------------------------------------------------
31
31
32
32
using System ;
33
- using System . Collections . Generic ;
34
33
using System . Threading ;
35
34
using System . Threading . Tasks ;
36
35
using RabbitMQ . Client ;
@@ -49,14 +48,6 @@ public TestConnectionBlockedChannelLeak(ITestOutputHelper output) : base(output)
49
48
public override async Task InitializeAsync ( )
50
49
{
51
50
await UnblockAsync ( ) ;
52
- _connFactory = new ConnectionFactory
53
- {
54
- AutomaticRecoveryEnabled = true ,
55
- ClientProvidedName = _testDisplayName ,
56
- ContinuationTimeout = TimeSpan . FromSeconds ( 2 )
57
- } ;
58
- _conn = await _connFactory . CreateConnectionAsync ( ) ;
59
- _channel = await _conn . CreateChannelAsync ( ) ;
60
51
}
61
52
62
53
public override async Task DisposeAsync ( )
@@ -68,49 +59,55 @@ public override async Task DisposeAsync()
68
59
[ Fact ]
69
60
public async Task TestConnectionBlockedChannelLeak_GH1573 ( )
70
61
{
71
- string exchangeName = GenerateExchangeName ( ) ;
62
+ await BlockAsync ( ) ;
72
63
73
- var tcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
64
+ var connectionBlockedTcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
65
+ var connectionUnblockedTcs = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
74
66
75
67
using var cts = new CancellationTokenSource ( WaitSpan ) ;
76
68
using CancellationTokenRegistration ctr = cts . Token . Register ( ( ) =>
77
69
{
78
- tcs . TrySetCanceled ( ) ;
70
+ connectionBlockedTcs . TrySetCanceled ( ) ;
71
+ connectionUnblockedTcs . TrySetCanceled ( ) ;
79
72
} ) ;
80
73
74
+ _connFactory = new ConnectionFactory
75
+ {
76
+ AutomaticRecoveryEnabled = true ,
77
+ ClientProvidedName = _testDisplayName ,
78
+ ContinuationTimeout = TimeSpan . FromSeconds ( 2 )
79
+ } ;
80
+ _conn = await _connFactory . CreateConnectionAsync ( ) ;
81
+ _channel = await _conn . CreateChannelAsync ( ) ;
82
+
83
+ string exchangeName = GenerateExchangeName ( ) ;
84
+
81
85
_conn . ConnectionBlocked += ( object sender , ConnectionBlockedEventArgs args ) =>
82
86
{
83
- UnblockAsync ( ) ;
87
+ connectionBlockedTcs . SetResult ( true ) ;
84
88
} ;
85
89
86
90
_conn . ConnectionUnblocked += ( object sender , EventArgs ea ) =>
87
91
{
88
- tcs . SetResult ( true ) ;
92
+ connectionUnblockedTcs . SetResult ( true ) ;
89
93
} ;
90
94
91
- await BlockAsync ( _channel ) ;
92
-
93
- using ( IChannel publishChannel = await _conn . CreateChannelAsync ( ) )
95
+ async Task ExchangeDeclareAndPublish ( )
94
96
{
95
- await publishChannel . ExchangeDeclareAsync ( exchangeName , ExchangeType . Direct , autoDelete : true ) ;
96
- await publishChannel . BasicPublishAsync ( exchangeName , exchangeName , GetRandomBody ( ) , mandatory : true ) ;
97
- await publishChannel . CloseAsync ( ) ;
97
+ using ( IChannel publishChannel = await _conn . CreateChannelAsync ( ) )
98
+ {
99
+ await publishChannel . ExchangeDeclareAsync ( exchangeName , ExchangeType . Direct , autoDelete : true ) ;
100
+ await publishChannel . BasicPublishAsync ( exchangeName , exchangeName , GetRandomBody ( ) , mandatory : true ) ;
101
+ await publishChannel . CloseAsync ( ) ;
102
+ }
98
103
}
104
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ExchangeDeclareAndPublish ) ;
99
105
100
- var channels = new List < IChannel > ( ) ;
101
106
for ( int i = 1 ; i <= 5 ; i ++ )
102
107
{
103
- IChannel c = await _conn . CreateChannelAsync ( ) ;
104
- channels . Add ( c ) ;
108
+ await Assert . ThrowsAnyAsync < OperationCanceledException > ( ( ) => _conn . CreateChannelAsync ( ) ) ;
105
109
}
106
110
107
- /*
108
- * Note:
109
- * This wait probably isn't necessary, if the above CreateChannelAsync
110
- * calls were to timeout, we'd get exceptions on the await
111
- */
112
- await Task . Delay ( TimeSpan . FromSeconds ( 5 ) ) ;
113
-
114
111
// Note: debugging
115
112
// var rmq = new RabbitMQCtl(_output);
116
113
// string output = await rmq.ExecRabbitMQCtlAsync("list_channels");
@@ -121,7 +118,8 @@ public async Task TestConnectionBlockedChannelLeak_GH1573()
121
118
// output = await rmq.ExecRabbitMQCtlAsync("list_channels");
122
119
// _output.WriteLine("CHANNELS 1: {0}", output);
123
120
124
- Assert . True ( await tcs . Task , "Unblock notification not received." ) ;
121
+ Assert . True ( await connectionBlockedTcs . Task , "Blocked notification not received." ) ;
122
+ Assert . True ( await connectionUnblockedTcs . Task , "Unblocked notification not received." ) ;
125
123
}
126
124
}
127
125
}
0 commit comments