Skip to content

Commit

Permalink
fix: users for alias not balanced across threads (#90)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ewingjm authored Jun 29, 2021
1 parent 2f7a932 commit 213ab54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Capgemini.PowerApps.SpecFlowBindings.Extensions;
using YamlDotNet.Serialization;

/// <summary>
Expand Down Expand Up @@ -82,7 +83,7 @@ private Dictionary<string, IEnumerator<UserConfiguration>> UserEnumerators
.Distinct()
.ToDictionary(
alias => alias,
alias => this.Users.Where(u => u.Alias == alias).GetEnumerator());
alias => this.GetUserEnumerator(alias));
}
}

Expand Down Expand Up @@ -119,7 +120,7 @@ public UserConfiguration GetUser(string userAlias, bool useCurrentUser = true)
var aliasEnumerator = this.UserEnumerators[userAlias];
if (!aliasEnumerator.MoveNext())
{
this.UserEnumerators[userAlias] = this.Users.Where(u => u.Alias == userAlias).GetEnumerator();
this.UserEnumerators[userAlias] = this.GetUserEnumerator(userAlias);
aliasEnumerator = this.UserEnumerators[userAlias];
aliasEnumerator.MoveNext();
}
Expand Down Expand Up @@ -149,5 +150,10 @@ internal void Flush()
{
CurrentUsers.Clear();
}

private IEnumerator<UserConfiguration> GetUserEnumerator(string alias)
{
return this.Users.Where(u => u.Alias == alias).ToList().Shuffle().GetEnumerator();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Capgemini.PowerApps.SpecFlowBindings.Extensions
{
using System;
using System.Collections.Generic;

/// <summary>
/// Extensions for <see cref="IList{T}"/>.
/// </summary>
internal static class IListExtensions
{
private static readonly Random Rand = new Random();

/// <summary>
/// Randomises the order of a list.
/// </summary>
/// <typeparam name="T">The type of list items.</typeparam>
/// <param name="list">The list.</param>
/// <returns>The shuffled list.</returns>
internal static IList<T> Shuffle<T>(this IList<T> list)
{
var n = list.Count;

while (n > 1)
{
n--;
var k = Rand.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}

return list;
}
}
}

0 comments on commit 213ab54

Please sign in to comment.