@@ -58,13 +58,14 @@ public OAuth2Options(Mode mode)
5858 public int TokenExpiresInSeconds => 60 ;
5959 }
6060
61- public class TestOAuth2
61+ public class TestOAuth2 : IAsyncLifetime
6262 {
6363 private const string Exchange = "test_direct" ;
6464
6565 private readonly AutoResetEvent _doneEvent = new AutoResetEvent ( false ) ;
6666 private readonly ITestOutputHelper _testOutputHelper ;
67- private readonly IConnection _connection ;
67+ private readonly IConnectionFactory _connectionFactory ;
68+ private IConnection _connection ;
6869 private readonly int _tokenExpiresInSeconds ;
6970
7071 public TestOAuth2 ( ITestOutputHelper testOutputHelper )
@@ -75,61 +76,76 @@ public TestOAuth2(ITestOutputHelper testOutputHelper)
7576 Mode mode = ( Mode ) Enum . Parse ( typeof ( Mode ) , modeStr . ToLowerInvariant ( ) ) ;
7677 var options = new OAuth2Options ( mode ) ;
7778
78- var connectionFactory = new ConnectionFactory
79+ _connectionFactory = new ConnectionFactory
7980 {
8081 AutomaticRecoveryEnabled = true ,
82+ DispatchConsumersAsync = true ,
8183 CredentialsProvider = GetCredentialsProvider ( options ) ,
8284 CredentialsRefresher = GetCredentialsRefresher ( ) ,
8385 ClientProvidedName = nameof ( TestOAuth2 )
8486 } ;
8587
86- _connection = connectionFactory . CreateConnection ( ) ;
8788 _tokenExpiresInSeconds = options . TokenExpiresInSeconds ;
8889 }
8990
91+ public async Task InitializeAsync ( )
92+ {
93+ _connection = await _connectionFactory . CreateConnectionAsync ( ) ;
94+ }
95+
96+ public async Task DisposeAsync ( )
97+ {
98+ await _connection . CloseAsync ( ) ;
99+ _connection . Dispose ( ) ;
100+ }
101+
90102 [ Fact ]
91103 public async void IntegrationTest ( )
92104 {
93- using ( _connection )
105+ using ( IChannel publishChannel = await DeclarePublisherAsync ( ) )
106+ using ( IChannel consumeChannel = await DeclareConsumerAsync ( ) )
94107 {
95- using ( IChannel publisher = declarePublisher ( ) )
96- using ( IChannel subscriber = await declareConsumer ( ) )
97- {
98- await Publish ( publisher ) ;
99- Consume ( subscriber ) ;
108+ await PublishAsync ( publishChannel ) ;
109+ Consume ( consumeChannel ) ;
100110
101- if ( _tokenExpiresInSeconds > 0 )
111+ if ( _tokenExpiresInSeconds > 0 )
112+ {
113+ for ( int i = 0 ; i < 4 ; i ++ )
102114 {
103- for ( int i = 0 ; i < 4 ; i ++ )
104- {
105- _testOutputHelper . WriteLine ( "Wait until Token expires. Attempt #" + ( i + 1 ) ) ;
115+ _testOutputHelper . WriteLine ( "Wait until Token expires. Attempt #" + ( i + 1 ) ) ;
106116
107- await Task . Delay ( TimeSpan . FromSeconds ( _tokenExpiresInSeconds + 10 ) ) ;
108- _testOutputHelper . WriteLine ( "Resuming .." ) ;
117+ await Task . Delay ( TimeSpan . FromSeconds ( _tokenExpiresInSeconds + 10 ) ) ;
118+ _testOutputHelper . WriteLine ( "Resuming .." ) ;
109119
110- await Publish ( publisher ) ;
111- _doneEvent . Reset ( ) ;
120+ await PublishAsync ( publishChannel ) ;
121+ _doneEvent . Reset ( ) ;
112122
113- Consume ( subscriber ) ;
114- }
115- }
116- else
117- {
118- throw new InvalidOperationException ( ) ;
123+ Consume ( consumeChannel ) ;
119124 }
120125 }
126+ else
127+ {
128+ Assert . Fail ( "_tokenExpiresInSeconds is NOT greater than 0" ) ;
129+ }
121130 }
122131 }
123132
124- private IChannel declarePublisher ( )
133+ [ Fact ]
134+ public async void SecondConnectionCrashes_GH1429 ( )
135+ {
136+ // https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1429
137+ using IConnection secondConnection = await _connectionFactory . CreateConnectionAsync ( ) ;
138+ }
139+
140+ private async Task < IChannel > DeclarePublisherAsync ( )
125141 {
126- IChannel publisher = _connection . CreateChannel ( ) ;
127- publisher . ConfirmSelect ( ) ;
128- publisher . ExchangeDeclare ( "test_direct" , ExchangeType . Direct , true , false ) ;
142+ IChannel publisher = await _connection . CreateChannelAsync ( ) ;
143+ await publisher . ConfirmSelectAsync ( ) ;
144+ await publisher . ExchangeDeclareAsync ( "test_direct" , ExchangeType . Direct , true , false ) ;
129145 return publisher ;
130146 }
131147
132- private async Task Publish ( IChannel publisher )
148+ private async Task PublishAsync ( IChannel publisher )
133149 {
134150 const string message = "Hello World!" ;
135151
@@ -146,7 +162,7 @@ private async Task Publish(IChannel publisher)
146162 _testOutputHelper . WriteLine ( "Confirmed Sent message" ) ;
147163 }
148164
149- private async ValueTask < IChannel > declareConsumer ( )
165+ private async ValueTask < IChannel > DeclareConsumerAsync ( )
150166 {
151167 IChannel subscriber = _connection . CreateChannel ( ) ;
152168 await subscriber . QueueDeclareAsync ( queue : "testqueue" , passive : false , true , false , false , arguments : null ) ;
0 commit comments