Skip to content

Commit 9ad69a4

Browse files
committed
Merge pull request #597 from markcowl/signing
Shortening mock tests for servicemanagement networking
2 parents 0d479bd + 257ada5 commit 9ad69a4

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-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-running 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,13 +12,16 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
1516
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.ScenarioTests
1617
{
1718
using Microsoft.Azure.Common.Authentication;
1819
using Microsoft.Azure.Test;
1920
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2021
using Microsoft.WindowsAzure.Management;
22+
using Microsoft.WindowsAzure.Management.Compute;
2123
using Microsoft.WindowsAzure.Management.Network;
24+
using Microsoft.WindowsAzure.Management.Storage;
2225
using System.Collections.Generic;
2326
using System.IO;
2427
using System.Linq;
@@ -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)

src/ServiceManagement/Services/Commands.Test/Profile/ProfileClientHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Net;
19+
using System.Net.Http;
1920
using System.Threading.Tasks;
21+
using Hyak.Common;
22+
using Microsoft.Azure.Subscriptions.Rdfe;
2023

2124
namespace Microsoft.WindowsAzure.Commands.Test.Profile
2225
{
@@ -31,6 +34,16 @@ public IList<String> ReturnedSubscriptions
3134
set { this._subscriptions = value; }
3235
}
3336

37+
protected override SubscriptionClient WithHandler(ServiceClient<SubscriptionClient> newClient, DelegatingHandler handler)
38+
{
39+
return newClient as SubscriptionClient;
40+
}
41+
42+
public override SubscriptionClient WithHandler(DelegatingHandler handler)
43+
{
44+
return this;
45+
}
46+
3447
public override Microsoft.Azure.Subscriptions.Rdfe.ISubscriptionOperations Subscriptions
3548
{
3649
get { return MockRdfeSubscriptionOperations.Create(this.ReturnedSubscriptions, this.Tenant); }
@@ -128,6 +141,11 @@ public override Azure.Subscriptions.Csm.ITenantOperations Tenants
128141
return MockCsmTenantOperations.Create(this.ReturnedTenants);
129142
}
130143
}
144+
145+
public override Azure.Subscriptions.Csm.SubscriptionClient WithHandler(DelegatingHandler handler)
146+
{
147+
return this;
148+
}
131149
}
132150

133151
public class MockCsmTenantOperations : Microsoft.Azure.Subscriptions.Csm.ITenantOperations

0 commit comments

Comments
 (0)