Created
February 19, 2019 02:37
-
-
Save Viridovics/789a24cc5b238b70be68ed155d0e9691 to your computer and use it in GitHub Desktop.
With 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 | |
// fake_data is already initialized | |
// fake_data is already initialized | |
// Summary: Both tests work with common dictionary | |
} | |
static async Task AsyncExecuteAllTests() | |
{ | |
// When we execute Process.setEnableProcessTracing true | |
getDataDict(); | |
//execute tests | |
var t1 = AsyncTest1(); | |
var t2 = AsyncTest2(); | |
// Await both 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