Skip to content

Commit a26d9af

Browse files
committed
Fixing flaky ServiceManagement tests
1 parent 5bbcf62 commit a26d9af

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/ServiceManagement/Common/Commands.Common.Test/Commands.Common.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
</Compile>
210210
<Compile Include="Common\PSTestTracingInterceptor.cs" />
211211
<Compile Include="Resources\ResourceLocator.cs" />
212+
<Compile Include="TestExecutionHelpers.cs" />
212213
</ItemGroup>
213214
<ItemGroup>
214215
<ProjectReference Include="..\..\..\Common\Commands.Common.Authentication\Commands.Common.Authentication.csproj">
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.WindowsAzure.Commands.Common.Test
18+
{
19+
public static class TestExecutionHelpers
20+
{
21+
/// <summary>
22+
/// Retry an action until it succeeds - use for flaky tests
23+
/// </summary>
24+
/// <param name="testAction">Action tor etry</param>
25+
/// <param name="maxTries">The maximum number of times to try the action</param>
26+
public static void RetryAction(Action testAction, int maxTries = 3)
27+
{
28+
var tries = 0;
29+
var succeeded = false;
30+
do
31+
{
32+
try
33+
{
34+
testAction();
35+
succeeded = true;
36+
}
37+
catch (Exception)
38+
{
39+
if (++tries >= maxTries)
40+
{
41+
throw;
42+
}
43+
}
44+
} while (!succeeded);
45+
}
46+
}
47+
}

src/ServiceManagement/Services/Commands.Test/WAPackIaaS/Operations/JobOperationsTests.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using Microsoft.WindowsAzure.Commands.Common.Test;
1617
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1718
using Xunit;
1819
using Microsoft.WindowsAzure.Commands.Test.WAPackIaaS.Mocks;
@@ -35,17 +36,22 @@ public class JobOperationsTests
3536
[Trait("Type", "WAPackIaaS-Unit")]
3637
public void WaitOnJobCompletesImmediately()
3738
{
38-
Guid jobId = Guid.NewGuid();
39+
// Fix test flakiness
40+
TestExecutionHelpers.RetryAction(
41+
() =>
42+
{
43+
Guid jobId = Guid.NewGuid();
3944

40-
MockRequestChannel mockChannel = MockRequestChannel.Create();
41-
mockChannel.AddReturnObject(new Job {Name = "TestJob", ID = jobId, IsCompleted = true});
45+
MockRequestChannel mockChannel = MockRequestChannel.Create();
46+
mockChannel.AddReturnObject(new Job {Name = "TestJob", ID = jobId, IsCompleted = true});
4247

43-
var jobOperations = new JobOperations(new WebClientFactory(
44-
new Subscription(),
45-
mockChannel));
46-
DateTime start = DateTime.Now;
47-
jobOperations.WaitOnJob(jobId);
48-
Assert.True((DateTime.Now - start).TotalMilliseconds < 500);
48+
var jobOperations = new JobOperations(new WebClientFactory(
49+
new Subscription(),
50+
mockChannel));
51+
DateTime start = DateTime.Now;
52+
jobOperations.WaitOnJob(jobId);
53+
Assert.True((DateTime.Now - start).TotalMilliseconds < 500);
54+
});
4955
}
5056

5157
/// <summary>

0 commit comments

Comments
 (0)