< Summary

Information
Class: dotnet_etcd.helper.AsyncHelper
Assembly: dotnet-etcd
File(s): /home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/helper/AsyncHelper.cs
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 47
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
RunSync(...)100%11100%

File(s)

/home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/helper/AsyncHelper.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Globalization;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace dotnet_etcd.helper;
 10
 11/// <summary>
 12///     Helps with running async code in sync methods
 13/// </summary>
 14/// <remarks>
 15///     Based on the AsyncHelper of the Microsoft.AspNet.Identity project
 16///     (https://web.archive.org/web/20200411071640/https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.A
 17/// </remarks>
 18public static class AsyncHelper
 19{
 120    private static readonly TaskFactory MyTaskFactory = new(CancellationToken.None,
 121        TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);
 22
 23    /// <summary>
 24    ///     Runs the provided async function provided in <paramref name="func" /> in a synchronous way
 25    /// </summary>
 26    /// <param name="func">A call to the async method</param>
 27    /// <example>
 28    ///     <code>
 29    /// AsyncHelper.RunSync(SomeFunctionAsync)
 30    /// </code>
 31    ///     or
 32    ///     <code>
 33    /// AsyncHelper.RunSync(async () => await SomeFunctionAsync())
 34    /// </code>
 35    /// </example>
 36    public static void RunSync(Func<Task> func)
 537    {
 538        CultureInfo cultureUi = CultureInfo.CurrentUICulture;
 539        CultureInfo culture = CultureInfo.CurrentCulture;
 540        MyTaskFactory.StartNew(() =>
 541        {
 542            Thread.CurrentThread.CurrentCulture = culture;
 543            Thread.CurrentThread.CurrentUICulture = cultureUi;
 544            return func();
 945        }).Unwrap().GetAwaiter().GetResult();
 346    }
 47}