Created
February 19, 2019 02:22
-
-
Save Viridovics/04a45bba5af225811dbf74ef3c4a7807 to your computer and use it in GitHub Desktop.
Without Process.setEnableProcessTracing true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace TestAsyncLocal | |
{ | |
class Program | |
{ | |
static AsyncLocal<Dictionary<string, string>> fake_data = new AsyncLocal<Dictionary<string, string>>(); | |
static Dictionary<string, string> getDataDict() | |
{ | |
if (fake_data.Value == null) | |
{ | |
Console.WriteLine("Init fake_data"); | |
fake_data.Value = new Dictionary<string, string>(); | |
} | |
else | |
{ | |
Console.WriteLine("fake_data is already initialized"); | |
} | |
return fake_data.Value; | |
} | |
static async Task Main(string[] args) | |
{ | |
await AsyncExecuteAllTests(); | |
Console.ReadKey(); | |
// Output: | |
// Init fake_data | |
// Init fake_data | |
} | |
static async Task AsyncExecuteAllTests() | |
{ | |
//execute tests | |
var t1 = AsyncTest1(); | |
var t2 = AsyncTest2(); | |
// Await all tests | |
await t1; | |
await t2; | |
} | |
static async Task AsyncTest1() | |
{ | |
await Task.Delay(100); | |
// Some test with withFakeContext | |
getDataDict(); | |
await Task.Delay(100); | |
} | |
static async Task AsyncTest2() | |
{ | |
await Task.Delay(100); | |
// Some test with withFakeContext | |
getDataDict(); | |
await Task.Delay(100); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment