This commit is contained in:
Filippo Banno 2019-09-30 13:55:18 +01:00
Родитель c22da668ef
Коммит fb87d7f343
1 изменённых файлов: 85 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,85 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Sharing.Matchmaking;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Xunit;
namespace Matchmaking.Local.Test
{
public static class Utils
{
public static int TestTimeoutMs
{
get
{
return Debugger.IsAttached ? Timeout.Infinite : 10000;
}
}
public static void AssertSameDictionary(IReadOnlyDictionary<string, string> a, IReadOnlyDictionary<string, string> b)
{
Assert.Equal(a.Count, b.Count);
foreach (var entry in a)
{
Assert.Equal(entry.Value, b[entry.Key]);
}
}
public class RaiiGuard : IDisposable
{
private Action Quit { get; set; }
public RaiiGuard(Action init, Action quit)
{
Quit = quit;
if (init != null) init();
}
void IDisposable.Dispose()
{
if (Quit != null) Quit();
}
}
// Run a query and wait for the predicate to be satisfied.
// Return the list of rooms which satisfied the predicate or null if canceled before the predicate was satisfied.
public static IEnumerable<IRoom> QueryAndWaitForRoomsPredicate(
IMatchmakingService svc, string type,
Func<IEnumerable<IRoom>, bool> pred, CancellationToken token)
{
using (var result = svc.StartDiscovery(type))
{
var rooms = result.Rooms;
bool predicateResult = pred(rooms);
if (predicateResult)
{
return rooms; // optimistic path
}
using (var wakeUp = new AutoResetEvent(false))
{
Action<IDiscoveryTask> onChange = (IDiscoveryTask sender) => wakeUp.Set();
using (var unregisterCancel = token.Register(() => wakeUp.Set()))
using (var unregisterWatch = new RaiiGuard(() => result.Updated += onChange, () => result.Updated -= onChange))
{
while (true)
{
rooms = result.Rooms;
if (pred(rooms))
{
return rooms;
}
wakeUp.WaitOne(); // wait for cancel or update
if (token.IsCancellationRequested)
{
return null;
}
}
}
}
}
}
}
}