forked from dotnet/corefx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncTextWriter.cs
More file actions
48 lines (39 loc) · 1.12 KB
/
SyncTextWriter.cs
File metadata and controls
48 lines (39 loc) · 1.12 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Text;
using System.Threading;
using Xunit;
public class SyncTextWriter
{
[Fact]
public void SyncTextWriterLockedOnThis()
{
TextWriter oldWriter = Console.Out;
try
{
var newWriter = new CallbackTextWriter();
Console.SetOut(newWriter);
TextWriter syncWriter = Console.Out;
bool called = false;
newWriter.Callback = _ =>
{
Assert.True(Monitor.IsEntered(syncWriter));
called = true;
};
Console.Write("c");
Assert.True(called);
}
finally
{
Console.SetOut(oldWriter);
}
}
private sealed class CallbackTextWriter : TextWriter
{
internal Action<char> Callback;
public override Encoding Encoding { get { return Encoding.UTF8; } }
public override void Write(char value) { Callback(value); }
}
}