-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
SyncTextWriter.cs
65 lines (53 loc) · 1.64 KB
/
SyncTextWriter.cs
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
// 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Xunit;
public class SyncTextWriter
{
// single-threaded WASM bypasses SyncTextWriter for faster startup
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void SyncTextWriterLockedOnThis()
{
TextWriter oldWriter = Console.Out;
try
{
var newWriter = new CallbackTextWriter();
Console.SetOut(newWriter);
TextWriter syncWriter = Console.Out;
newWriter.Callback = () =>
{
Assert.True(Monitor.IsEntered(syncWriter));
};
Console.Write("c");
Assert.True(newWriter.WriteCharCalled);
Console.Write("{0}", 32);
Assert.True(newWriter.WriteFormatCalled);
}
finally
{
Console.SetOut(oldWriter);
}
}
private sealed class CallbackTextWriter : TextWriter
{
internal Action Callback;
public bool WriteCharCalled;
public bool WriteFormatCalled;
public override Encoding Encoding { get { return Encoding.UTF8; } }
public override void Write(char value)
{
WriteCharCalled = true;
Callback();
}
public override void Write(string format, object arg0)
{
WriteFormatCalled = true;
Callback();
}
}
}