Skip to content

Commit 213ab54

Browse files
authored
fix: users for alias not balanced across threads (#90)
This is required due to the fact that, if tests are running with process or AppDomain isolation, all of the tests will enumerate through the users in the same order. Shuffling the enumerators is an approximation that saves having to find a way to synchronise between AppDomains or processes.
1 parent 2f7a932 commit 213ab54

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

bindings/src/Capgemini.PowerApps.SpecFlowBindings/Configuration/TestConfiguration.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Configuration;
66
using System.Linq;
7+
using Capgemini.PowerApps.SpecFlowBindings.Extensions;
78
using YamlDotNet.Serialization;
89

910
/// <summary>
@@ -82,7 +83,7 @@ private Dictionary<string, IEnumerator<UserConfiguration>> UserEnumerators
8283
.Distinct()
8384
.ToDictionary(
8485
alias => alias,
85-
alias => this.Users.Where(u => u.Alias == alias).GetEnumerator());
86+
alias => this.GetUserEnumerator(alias));
8687
}
8788
}
8889

@@ -119,7 +120,7 @@ public UserConfiguration GetUser(string userAlias, bool useCurrentUser = true)
119120
var aliasEnumerator = this.UserEnumerators[userAlias];
120121
if (!aliasEnumerator.MoveNext())
121122
{
122-
this.UserEnumerators[userAlias] = this.Users.Where(u => u.Alias == userAlias).GetEnumerator();
123+
this.UserEnumerators[userAlias] = this.GetUserEnumerator(userAlias);
123124
aliasEnumerator = this.UserEnumerators[userAlias];
124125
aliasEnumerator.MoveNext();
125126
}
@@ -149,5 +150,10 @@ internal void Flush()
149150
{
150151
CurrentUsers.Clear();
151152
}
153+
154+
private IEnumerator<UserConfiguration> GetUserEnumerator(string alias)
155+
{
156+
return this.Users.Where(u => u.Alias == alias).ToList().Shuffle().GetEnumerator();
157+
}
152158
}
153159
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Capgemini.PowerApps.SpecFlowBindings.Extensions
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
6+
/// <summary>
7+
/// Extensions for <see cref="IList{T}"/>.
8+
/// </summary>
9+
internal static class IListExtensions
10+
{
11+
private static readonly Random Rand = new Random();
12+
13+
/// <summary>
14+
/// Randomises the order of a list.
15+
/// </summary>
16+
/// <typeparam name="T">The type of list items.</typeparam>
17+
/// <param name="list">The list.</param>
18+
/// <returns>The shuffled list.</returns>
19+
internal static IList<T> Shuffle<T>(this IList<T> list)
20+
{
21+
var n = list.Count;
22+
23+
while (n > 1)
24+
{
25+
n--;
26+
var k = Rand.Next(n + 1);
27+
T value = list[k];
28+
list[k] = list[n];
29+
list[n] = value;
30+
}
31+
32+
return list;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)