-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Issue2694.cs
158 lines (138 loc) · 5.59 KB
/
Issue2694.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !(NET20 || NET35 || NET40 || PORTABLE || PORTABLE40) || NETSTANDARD1_3 || NETSTANDARD2_0 || NET6_0_OR_GREATER
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
#if DNXCORE50
using System.Reflection;
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif
namespace Newtonsoft.Json.Tests.Issues
{
[TestFixture]
public class Issue2694 : TestFixtureBase
{
#if NET6_0_OR_GREATER
[Test]
public async Task Test_Reader_DisposeAsync()
{
JsonTextReader reader = new JsonTextReader(new StringReader("{}"));
IAsyncDisposable asyncDisposable = reader;
await asyncDisposable.DisposeAsync();
await ExceptionAssert.ThrowsAsync<JsonReaderException>(() => reader.ReadAsync(),
"Unexpected state: Closed. Path '', line 1, position 0.");
}
[Test]
public async Task Test_Writer_DisposeAsync()
{
MemoryStream ms = new MemoryStream();
Stream s = new AsyncOnlyStream(ms);
StreamWriter sr = new StreamWriter(s, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), 2, leaveOpen: true);
await using (JsonTextWriter writer = new JsonTextWriter(sr))
{
await writer.WriteStartObjectAsync();
}
string json = Encoding.UTF8.GetString(ms.ToArray());
Assert.AreEqual("{}", json);
}
#endif
[Test]
public async Task Test_Writer_CloseAsync()
{
MemoryStream ms = new MemoryStream();
Stream s = new AsyncOnlyStream(ms);
StreamWriter sr = new StreamWriter(s, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), 2, leaveOpen: true);
JsonTextWriter writer = new JsonTextWriter(sr);
await writer.WriteStartObjectAsync();
await writer.CloseAsync();
string json = Encoding.UTF8.GetString(ms.ToArray());
Assert.AreEqual("{}", json);
}
public class AsyncOnlyStream : Stream
{
private readonly Stream _innerStream;
private int _unflushedContentLength;
public AsyncOnlyStream(Stream innerStream)
{
_innerStream = innerStream;
}
public override void Flush()
{
// It's ok to call Flush if the content was already processed with FlushAsync.
if (_unflushedContentLength > 0)
{
throw new Exception($"Flush when there is {_unflushedContentLength} bytes buffered.");
}
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
_unflushedContentLength = 0;
return _innerStream.FlushAsync(cancellationToken);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _innerStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_innerStream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
_unflushedContentLength += count;
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
}
public override bool CanRead => _innerStream.CanRead;
public override bool CanSeek => _innerStream.CanSeek;
public override bool CanWrite => _innerStream.CanWrite;
public override long Length => _innerStream.Length;
public override long Position
{
get => _innerStream.Position;
set => _innerStream.Position = value;
}
}
}
}
#endif