@@ -46,6 +46,8 @@ namespace Test.Integration
4646 public class TestToxiproxy : IntegrationFixture
4747 {
4848 private readonly TimeSpan _heartbeatTimeout = TimeSpan . FromSeconds ( 1 ) ;
49+ private ToxiproxyManager _toxiproxyManager ;
50+ private int _proxyPort ;
4951
5052 public TestToxiproxy ( ITestOutputHelper output ) : base ( output )
5153 {
@@ -59,7 +61,15 @@ public override Task InitializeAsync()
5961 Assert . Null ( _conn ) ;
6062 Assert . Null ( _channel ) ;
6163
62- return Task . CompletedTask ;
64+ _toxiproxyManager = new ToxiproxyManager ( _testDisplayName , IsRunningInCI , IsWindows ) ;
65+ _proxyPort = _toxiproxyManager . ProxyPort ;
66+ return _toxiproxyManager . InitializeAsync ( ) ;
67+ }
68+
69+ public override async Task DisposeAsync ( )
70+ {
71+ await _toxiproxyManager . DisposeAsync ( ) ;
72+ await base . DisposeAsync ( ) ;
6373 }
6474
6575 [ SkippableFact ]
@@ -68,11 +78,8 @@ public async Task TestCloseConnection()
6878 {
6979 Skip . IfNot ( AreToxiproxyTestsEnabled , "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test" ) ;
7080
71- using var pm = new ToxiproxyManager ( _testDisplayName , IsRunningInCI , IsWindows ) ;
72- await pm . InitializeAsync ( ) ;
73-
7481 ConnectionFactory cf = CreateConnectionFactory ( ) ;
75- cf . Port = pm . ProxyPort ;
82+ cf . Port = _proxyPort ;
7683 cf . AutomaticRecoveryEnabled = true ;
7784 cf . NetworkRecoveryInterval = TimeSpan . FromSeconds ( 1 ) ;
7885 cf . RequestedHeartbeat = TimeSpan . FromSeconds ( 1 ) ;
@@ -172,11 +179,11 @@ async Task PublishLoop()
172179
173180 Assert . True ( await messagePublishedTcs . Task ) ;
174181
175- Task disableProxyTask = pm . DisableAsync ( ) ;
182+ Task disableProxyTask = _toxiproxyManager . DisableAsync ( ) ;
176183
177184 await Task . WhenAll ( disableProxyTask , connectionShutdownTcs . Task ) ;
178185
179- Task enableProxyTask = pm . EnableAsync ( ) ;
186+ Task enableProxyTask = _toxiproxyManager . EnableAsync ( ) ;
180187
181188 Task whenAllTask = Task . WhenAll ( enableProxyTask , recoverySucceededTcs . Task ) ;
182189 await whenAllTask . WaitAsync ( TimeSpan . FromSeconds ( 15 ) ) ;
@@ -193,11 +200,8 @@ public async Task TestThatStoppedSocketResultsInHeartbeatTimeout()
193200 {
194201 Skip . IfNot ( AreToxiproxyTestsEnabled , "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test" ) ;
195202
196- using var pm = new ToxiproxyManager ( _testDisplayName , IsRunningInCI , IsWindows ) ;
197- await pm . InitializeAsync ( ) ;
198-
199203 ConnectionFactory cf = CreateConnectionFactory ( ) ;
200- cf . Port = pm . ProxyPort ;
204+ cf . Port = _proxyPort ;
201205 cf . RequestedHeartbeat = _heartbeatTimeout ;
202206 cf . AutomaticRecoveryEnabled = false ;
203207
@@ -229,7 +233,7 @@ public async Task TestThatStoppedSocketResultsInHeartbeatTimeout()
229233 timeoutToxic . Attributes . Timeout = 0 ;
230234 timeoutToxic . Toxicity = 1.0 ;
231235
232- Task < TimeoutToxic > addToxicTask = pm . AddToxicAsync ( timeoutToxic ) ;
236+ Task < TimeoutToxic > addToxicTask = _toxiproxyManager . AddToxicAsync ( timeoutToxic ) ;
233237
234238 await Assert . ThrowsAsync < AlreadyClosedException > ( ( ) =>
235239 {
@@ -243,11 +247,8 @@ public async Task TestTcpReset_GH1464()
243247 {
244248 Skip . IfNot ( AreToxiproxyTestsEnabled , "RABBITMQ_TOXIPROXY_TESTS is not set, skipping test" ) ;
245249
246- using var pm = new ToxiproxyManager ( _testDisplayName , IsRunningInCI , IsWindows ) ;
247- await pm . InitializeAsync ( ) ;
248-
249250 ConnectionFactory cf = CreateConnectionFactory ( ) ;
250- cf . Endpoint = new AmqpTcpEndpoint ( IPAddress . Loopback . ToString ( ) , pm . ProxyPort ) ;
251+ cf . Endpoint = new AmqpTcpEndpoint ( IPAddress . Loopback . ToString ( ) , _proxyPort ) ;
251252 cf . RequestedHeartbeat = TimeSpan . FromSeconds ( 5 ) ;
252253 cf . AutomaticRecoveryEnabled = true ;
253254
@@ -283,11 +284,11 @@ public async Task TestTcpReset_GH1464()
283284 resetPeerToxic . Attributes . Timeout = 500 ;
284285 resetPeerToxic . Toxicity = 1.0 ;
285286
286- Task < ResetPeerToxic > addToxicTask = pm . AddToxicAsync ( resetPeerToxic ) ;
287+ Task < ResetPeerToxic > addToxicTask = _toxiproxyManager . AddToxicAsync ( resetPeerToxic ) ;
287288
288289 await Task . WhenAll ( addToxicTask , connectionShutdownTcs . Task ) ;
289290
290- await pm . RemoveToxicAsync ( toxicName ) ;
291+ await _toxiproxyManager . RemoveToxicAsync ( toxicName ) ;
291292
292293 await recoveryTask ;
293294 }
@@ -302,11 +303,8 @@ public async Task TestPublisherConfirmationThrottling()
302303 const int MaxOutstandingConfirms = 8 ;
303304 const int BatchSize = MaxOutstandingConfirms * 2 ;
304305
305- using var pm = new ToxiproxyManager ( _testDisplayName , IsRunningInCI , IsWindows ) ;
306- await pm . InitializeAsync ( ) ;
307-
308306 ConnectionFactory cf = CreateConnectionFactory ( ) ;
309- cf . Endpoint = new AmqpTcpEndpoint ( IPAddress . Loopback . ToString ( ) , pm . ProxyPort ) ;
307+ cf . Endpoint = new AmqpTcpEndpoint ( IPAddress . Loopback . ToString ( ) , _proxyPort ) ;
310308 cf . RequestedHeartbeat = TimeSpan . FromSeconds ( 5 ) ;
311309 cf . AutomaticRecoveryEnabled = true ;
312310
@@ -371,7 +369,7 @@ public async Task TestPublisherConfirmationThrottling()
371369
372370 await Task . Delay ( TimeSpan . FromSeconds ( 1 ) ) ;
373371
374- Task < BandwidthToxic > addToxicTask = pm . AddToxicAsync ( bandwidthToxic ) ;
372+ Task < BandwidthToxic > addToxicTask = _toxiproxyManager . AddToxicAsync ( bandwidthToxic ) ;
375373
376374 while ( true )
377375 {
@@ -387,7 +385,7 @@ public async Task TestPublisherConfirmationThrottling()
387385 }
388386
389387 await addToxicTask . WaitAsync ( WaitSpan ) ;
390- await pm . RemoveToxicAsync ( toxicName ) . WaitAsync ( WaitSpan ) ;
388+ await _toxiproxyManager . RemoveToxicAsync ( toxicName ) . WaitAsync ( WaitSpan ) ;
391389
392390 await messagesPublishedTcs . Task . WaitAsync ( WaitSpan ) ;
393391 await publishTask . WaitAsync ( WaitSpan ) ;
0 commit comments