Skip to content

Commit 1e63ebd

Browse files
committed
Fix slow mocked tests for service management networking
1 parent 55eab97 commit 1e63ebd

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Common/Commands.ScenarioTests.Common/Mocks/MockClientFactory.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using System.Net;
2020
using System.Net.Http;
2121
using System.Net.Http.Headers;
22+
using System.Threading;
23+
using System.Threading.Tasks;
2224
using Hyak.Common;
2325
using Microsoft.Azure.Test.HttpRecorder;
2426
using Microsoft.Azure.Common;
@@ -96,7 +98,7 @@ public TClient CreateCustomClient<TClient>(params object[] parameters) where TCl
9698
{
9799
throw new ArgumentException(
98100
string.Format("TestManagementClientHelper class wasn't initialized with the {0} client.",
99-
typeof(TClient).Name));
101+
typeof (TClient).Name));
100102
}
101103
else
102104
{
@@ -107,6 +109,13 @@ public TClient CreateCustomClient<TClient>(params object[] parameters) where TCl
107109
return newRealClient;
108110
}
109111
}
112+
else
113+
{
114+
// Use the WithHandler method to create an extra reference to the http client
115+
// this will prevent the httpClient from being disposed in a long-runnign test using
116+
// the same client for multiple cmdlets
117+
client = client.WithHandler(new PassThroughDelegatingHandler());
118+
}
110119

111120
return client;
112121
}
@@ -162,5 +171,17 @@ public void AddUserAgent(string productName)
162171
}
163172

164173
public HashSet<ProductInfoHeaderValue> UserAgents { get; set; }
174+
175+
/// <summary>
176+
/// This class exists to allow adding an additional reference to the httpClient to prevent the client
177+
/// from being disposed. Should not be used execpt in this mocked context
178+
/// </summary>
179+
class PassThroughDelegatingHandler : DelegatingHandler
180+
{
181+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
182+
{
183+
return base.SendAsync(request, cancellationToken);
184+
}
185+
}
165186
}
166187
}

src/ServiceManagement/Network/Commands.Network.Test/ScenarioTests/MultiVip/MultiVip.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.WindowsAzure.Management.Compute;
16+
using Microsoft.WindowsAzure.Management.Storage;
17+
1518
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.ScenarioTests
1619
{
1720
using Microsoft.Azure.Common.Authentication;
@@ -66,7 +69,9 @@ protected void SetupManagementClients()
6669
{
6770
var client = TestBase.GetServiceClient<NetworkManagementClient>(new RDFETestEnvironmentFactory());
6871
var client2 = TestBase.GetServiceClient<ManagementClient>(new RDFETestEnvironmentFactory());
69-
helper.SetupSomeOfManagementClients(client, client2);
72+
var client3 = TestBase.GetServiceClient<StorageManagementClient>(new RDFETestEnvironmentFactory());
73+
var client4 = TestBase.GetServiceClient<ComputeManagementClient>(new RDFETestEnvironmentFactory());
74+
helper.SetupManagementClients(client, client2, client3, client4);
7075
}
7176

7277
protected void RunPowerShellTest(params string[] scripts)

src/ServiceManagement/Network/Commands.Network.Test/ScenarioTests/ReservedIPs/ReservedIP.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.WindowsAzure.Management.Compute;
16+
using Microsoft.WindowsAzure.Management.Storage;
17+
1518
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.ScenarioTests
1619
{
1720
using Microsoft.Azure.Common.Authentication;
@@ -73,7 +76,9 @@ protected void SetupManagementClients()
7376
{
7477
var client = TestBase.GetServiceClient<NetworkManagementClient>(new RDFETestEnvironmentFactory());
7578
var client2 = TestBase.GetServiceClient<ManagementClient>(new RDFETestEnvironmentFactory());
76-
helper.SetupSomeOfManagementClients(client, client2);
79+
var client3 = TestBase.GetServiceClient<ComputeManagementClient>(new RDFETestEnvironmentFactory());
80+
var client4 = TestBase.GetServiceClient<StorageManagementClient>(new RDFETestEnvironmentFactory());
81+
helper.SetupManagementClients(client, client2, client3, client4);
7782
}
7883

7984
protected void RunPowerShellTest(params string[] scripts)

0 commit comments

Comments
 (0)