Skip to content

Commit

Permalink
Tweaked Stuff Around Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DelnarErsike committed May 9, 2024
1 parent 480a469 commit c36241b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
8 changes: 2 additions & 6 deletions Chummer.Tests/ChummerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ public static void Initialize(TestContext context)
{
Utils.IsUnitTest = true;
Utils.IsUnitTestForUI = false;
}

public ChummerTest()
{
Utils.CreateSynchronizationContext();
}

Expand Down Expand Up @@ -153,10 +149,10 @@ async Task CacheCommonFile(string strFile)
if (!GlobalSettings.Language.Equals(
GlobalSettings.DefaultLanguage, StringComparison.OrdinalIgnoreCase))
{
await XmlManager.LoadXPathAsync(strFile, null, GlobalSettings.DefaultLanguage);
await XmlManager.LoadXPathAsync(strFile, null, GlobalSettings.DefaultLanguage).ConfigureAwait(false);
}

await XmlManager.LoadXPathAsync(strFile);
await XmlManager.LoadXPathAsync(strFile).ConfigureAwait(false);
}
}
catch (Exception ex)
Expand Down
38 changes: 28 additions & 10 deletions Chummer/Backend/Static/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,38 @@ public static void BreakOnErrorIfDebug()

private static SynchronizationContext MySynchronizationContext { get; set; }

private static JoinableTaskContext MyJoinableTaskContext { get; set; }
private static JoinableTaskContext MyJoinableTaskContext => s_objJoinableTaskContext;

public static JoinableTaskContext CreateSynchronizationContext()
{
if (Program.IsMainThread)
if (!Program.IsMainThread)
throw new InvalidOperationException("Cannot call CreateSynchronizationContext outside of the main thread.");

JoinableTaskContext objReturn;
if (s_objJoinableTaskFactory.IsValueCreated)
{
using (new DummyForm()) // New Form needs to be created (or Application.Run() called) before Synchronization.Current is set
JoinableTaskContext objNewContext = new JoinableTaskContext();
objReturn = Interlocked.CompareExchange(ref s_objJoinableTaskContext, objNewContext, default);
if (objReturn != default)
{
MySynchronizationContext = SynchronizationContext.Current;
return MyJoinableTaskContext
= new JoinableTaskContext(Thread.CurrentThread, SynchronizationContext.Current);
objNewContext.Dispose();
return objReturn;
}
}

return default;
using (new DummyForm()) // New Form needs to be created (or Application.Run() called) before Synchronization.Current is set
{
JoinableTaskContext objNewContext = new JoinableTaskContext();
objReturn = Interlocked.CompareExchange(ref s_objJoinableTaskContext, objNewContext, default);
if (objReturn != default)
{
objNewContext.Dispose();
return objReturn;
}

MySynchronizationContext = SynchronizationContext.Current;
return objNewContext;
}
}

// Need this as a Lazy, otherwise it won't fire properly in the designer if we just cache it, and the check itself is also quite expensive
Expand Down Expand Up @@ -351,9 +368,8 @@ public static void TryCacheExpression(string xpath, CancellationToken token = de

private static readonly Lazy<JoinableTaskFactory> s_objJoinableTaskFactory
= new Lazy<JoinableTaskFactory>(() => IsRunningInVisualStudio
? new JoinableTaskFactory(new JoinableTaskContext())
: new JoinableTaskFactory(
MyJoinableTaskContext ?? CreateSynchronizationContext()));
? new JoinableTaskFactory(new JoinableTaskContext())
: new JoinableTaskFactory(MyJoinableTaskContext ?? CreateSynchronizationContext()));

public static JoinableTaskFactory JoinableTaskFactory => s_objJoinableTaskFactory.Value;

Expand Down Expand Up @@ -2640,6 +2656,8 @@ public static string GetTempPath()
MaximumRetained = DefaultPoolSize
};

private static JoinableTaskContext s_objJoinableTaskContext;

/// <summary>
/// Memory Pool for empty StringBuilder objects. A bit slower up-front than a simple allocation, but reduces memory allocations, which saves on CPU used for Garbage Collection.
/// </summary>
Expand Down
23 changes: 14 additions & 9 deletions Chummer/Forms/Utility Forms/CharacterRoster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,20 @@ CharacterCache objSelectedCache

try
{
await Task.WhenAll(tskNewRecentlyUsedsRefresh.Yield()
.Concat(tskNewWatchFolderRefresh.Yield())
.Concat((await Program.PluginLoader
.GetMyActivePluginsAsync(
objTemp.Token)
.ConfigureAwait(false))
.Select(x => RefreshPluginNodesAsync(
x, _objGenericToken))))
.ConfigureAwait(false);
List<Task> lstTasks =
new List<Task>(2 + await Program.PluginLoader.MyPlugins.GetCountAsync(objTemp.Token)
.ConfigureAwait(false))
{
tskNewRecentlyUsedsRefresh,
tskNewWatchFolderRefresh
};
foreach (IPlugin objPlugin in await Program.PluginLoader
.GetMyActivePluginsAsync(objTemp.Token)
.ConfigureAwait(false))
{
lstTasks.Add(RefreshPluginNodesAsync(objPlugin, objTemp.Token));
}
await Task.WhenAll(lstTasks).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
Expand Down

0 comments on commit c36241b

Please sign in to comment.