Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: users for alias not balanced across threads #90

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}