forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSafety.cs
More file actions
128 lines (111 loc) · 3.86 KB
/
ThreadSafety.cs
File metadata and controls
128 lines (111 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
public class ThreadSafety
{
const int NumberOfIterations = 100;
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void OpenStandardXXXCanBeCalledConcurrently()
{
Parallel.For(0, NumberOfIterations, i =>
{
using (Stream s = Console.OpenStandardInput())
{
Assert.NotNull(s);
}
});
Parallel.For(0, NumberOfIterations, i =>
{
using (Stream s = Console.OpenStandardOutput())
{
Assert.NotNull(s);
}
});
Parallel.For(0, NumberOfIterations, i =>
{
using (Stream s = Console.OpenStandardError())
{
Assert.NotNull(s);
}
});
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void SetStandardXXXCanBeCalledConcurrently()
{
TextReader savedStandardInput = Console.In;
TextWriter savedStandardOutput = Console.Out;
TextWriter savedStandardError = Console.Error;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamReader sr = new StreamReader(memStream))
{
using (StreamWriter sw = new StreamWriter(memStream))
{
Parallel.For(0, NumberOfIterations, i =>
{
Console.SetIn(sr);
});
Parallel.For(0, NumberOfIterations, i =>
{
Console.SetOut(sw);
});
Parallel.For(0, NumberOfIterations, i =>
{
Console.SetOut(sw);
});
}
}
}
}
finally
{
Console.SetIn(savedStandardInput);
Console.SetOut(savedStandardOutput);
Console.SetError(savedStandardError);
}
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
public static void ReadMayBeCalledConcurrently()
{
const char TestChar = '+';
TextReader savedStandardInput = Console.In;
try
{
using (MemoryStream memStream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(memStream))
{
for (int i = 0; i < NumberOfIterations; i++)
{
sw.Write(TestChar);
}
sw.Flush();
memStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memStream))
{
Console.SetIn(sr);
Parallel.For(0, NumberOfIterations, i =>
{
Assert.Equal(TestChar, Console.Read());
});
// We should be at EOF now.
Assert.Equal(-1, Console.Read());
}
}
}
}
finally
{
Console.SetIn(savedStandardInput);
}
}
}