Skip to content

Commit c246c5a

Browse files
committed
More tests.
1 parent a804594 commit c246c5a

File tree

4 files changed

+316
-2
lines changed

4 files changed

+316
-2
lines changed

IDisposableAnalyzers.Test/IDISP002DisposeMemberTests/Valid.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,5 +1112,113 @@ public class Winform : Form
11121112
}".AssertReplace("this.components.Add(this.stream)", expression);
11131113
RoslynAssert.NoAnalyzerDiagnostics(Analyzer, code);
11141114
}
1115+
1116+
[Test]
1117+
public static void PooledMemoryStream()
1118+
{
1119+
var code = @"
1120+
namespace N
1121+
{
1122+
using System;
1123+
using System.Collections.Concurrent;
1124+
using System.IO;
1125+
1126+
internal class PooledMemoryStream : Stream
1127+
{
1128+
private static readonly ConcurrentQueue<MemoryStream> Pool = new ConcurrentQueue<MemoryStream>();
1129+
private readonly MemoryStream inner;
1130+
1131+
private bool disposed;
1132+
1133+
private PooledMemoryStream(MemoryStream inner)
1134+
{
1135+
this.inner = inner;
1136+
}
1137+
1138+
/// <inheritdoc/>
1139+
public override bool CanRead => !this.disposed;
1140+
1141+
/// <inheritdoc/>
1142+
public override bool CanSeek => !this.disposed;
1143+
1144+
/// <inheritdoc/>
1145+
public override bool CanWrite => !this.disposed;
1146+
1147+
/// <see cref=""MemoryStream.Length""/>
1148+
public override long Length => this.inner.Length;
1149+
1150+
/// <inheritdoc/>
1151+
public override long Position
1152+
{
1153+
get => this.inner.Position;
1154+
set => this.inner.Position = value;
1155+
}
1156+
1157+
/// <inheritdoc/>
1158+
public override void Flush()
1159+
{
1160+
// nop
1161+
}
1162+
1163+
/// <inheritdoc/>
1164+
public override long Seek(long offset, SeekOrigin origin) => this.inner.Seek(offset, origin);
1165+
1166+
/// <inheritdoc/>
1167+
public override void SetLength(long value) => this.inner.SetLength(value);
1168+
1169+
/// <inheritdoc/>
1170+
public override int Read(byte[] buffer, int offset, int count)
1171+
{
1172+
this.CheckDisposed();
1173+
return this.inner.Read(buffer, offset, count);
1174+
}
1175+
1176+
/// <inheritdoc/>
1177+
public override void Write(byte[] buffer, int offset, int count)
1178+
{
1179+
this.CheckDisposed();
1180+
this.inner.Write(buffer, offset, count);
1181+
}
1182+
1183+
internal static PooledMemoryStream Borrow()
1184+
{
1185+
if (Pool.TryDequeue(out var stream))
1186+
{
1187+
return new PooledMemoryStream(stream);
1188+
}
1189+
1190+
return new PooledMemoryStream(new MemoryStream());
1191+
}
1192+
1193+
/// <inheritdoc/>
1194+
protected override void Dispose(bool disposing)
1195+
{
1196+
if (this.disposed)
1197+
{
1198+
return;
1199+
}
1200+
1201+
this.disposed = true;
1202+
if (disposing)
1203+
{
1204+
this.inner.SetLength(0);
1205+
Pool.Enqueue(this.inner);
1206+
}
1207+
1208+
base.Dispose(disposing);
1209+
}
1210+
1211+
private void CheckDisposed()
1212+
{
1213+
if (this.disposed)
1214+
{
1215+
throw new ObjectDisposedException(this.GetType().FullName);
1216+
}
1217+
}
1218+
}
1219+
}
1220+
";
1221+
RoslynAssert.Valid(Analyzer, code);
1222+
}
11151223
}
11161224
}

IDisposableAnalyzers.Test/IDISP004DoNotIgnoreCreatedTests/Valid.Argument.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,5 +793,113 @@ public void Dispose()
793793

794794
RoslynAssert.Valid(Analyzer, pairOfT, staticPairCode, code);
795795
}
796+
797+
[Test]
798+
public static void PooledMemoryStream()
799+
{
800+
var code = @"
801+
namespace N
802+
{
803+
using System;
804+
using System.Collections.Concurrent;
805+
using System.IO;
806+
807+
internal class PooledMemoryStream : Stream
808+
{
809+
private static readonly ConcurrentQueue<MemoryStream> Pool = new ConcurrentQueue<MemoryStream>();
810+
private readonly MemoryStream inner;
811+
812+
private bool disposed;
813+
814+
private PooledMemoryStream(MemoryStream inner)
815+
{
816+
this.inner = inner;
817+
}
818+
819+
/// <inheritdoc/>
820+
public override bool CanRead => !this.disposed;
821+
822+
/// <inheritdoc/>
823+
public override bool CanSeek => !this.disposed;
824+
825+
/// <inheritdoc/>
826+
public override bool CanWrite => !this.disposed;
827+
828+
/// <see cref=""MemoryStream.Length""/>
829+
public override long Length => this.inner.Length;
830+
831+
/// <inheritdoc/>
832+
public override long Position
833+
{
834+
get => this.inner.Position;
835+
set => this.inner.Position = value;
836+
}
837+
838+
/// <inheritdoc/>
839+
public override void Flush()
840+
{
841+
// nop
842+
}
843+
844+
/// <inheritdoc/>
845+
public override long Seek(long offset, SeekOrigin origin) => this.inner.Seek(offset, origin);
846+
847+
/// <inheritdoc/>
848+
public override void SetLength(long value) => this.inner.SetLength(value);
849+
850+
/// <inheritdoc/>
851+
public override int Read(byte[] buffer, int offset, int count)
852+
{
853+
this.CheckDisposed();
854+
return this.inner.Read(buffer, offset, count);
855+
}
856+
857+
/// <inheritdoc/>
858+
public override void Write(byte[] buffer, int offset, int count)
859+
{
860+
this.CheckDisposed();
861+
this.inner.Write(buffer, offset, count);
862+
}
863+
864+
internal static PooledMemoryStream Borrow()
865+
{
866+
if (Pool.TryDequeue(out var stream))
867+
{
868+
return new PooledMemoryStream(stream);
869+
}
870+
871+
return new PooledMemoryStream(new MemoryStream());
872+
}
873+
874+
/// <inheritdoc/>
875+
protected override void Dispose(bool disposing)
876+
{
877+
if (this.disposed)
878+
{
879+
return;
880+
}
881+
882+
this.disposed = true;
883+
if (disposing)
884+
{
885+
this.inner.SetLength(0);
886+
Pool.Enqueue(this.inner);
887+
}
888+
889+
base.Dispose(disposing);
890+
}
891+
892+
private void CheckDisposed()
893+
{
894+
if (this.disposed)
895+
{
896+
throw new ObjectDisposedException(this.GetType().FullName);
897+
}
898+
}
899+
}
900+
}
901+
";
902+
RoslynAssert.Valid(Analyzer, code);
903+
}
796904
}
797905
}

IDisposableAnalyzers/Helpers/DisposableMember.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ internal static bool IsDisposed(FieldOrProperty member, MethodDeclarationSyntax
6464
return true;
6565
}
6666
}
67-
68-
return false;
6967
}
7068

7169
foreach (var candidate in walker.Identifiers)

ValidCode/PooledMemoryStream.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
namespace ValidCode
2+
{
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.IO;
6+
7+
internal class PooledMemoryStream : Stream
8+
{
9+
private static readonly ConcurrentQueue<MemoryStream> Pool = new ConcurrentQueue<MemoryStream>();
10+
private readonly MemoryStream inner;
11+
12+
private bool disposed;
13+
14+
private PooledMemoryStream(MemoryStream inner)
15+
{
16+
this.inner = inner;
17+
}
18+
19+
/// <inheritdoc/>
20+
public override bool CanRead => !this.disposed;
21+
22+
/// <inheritdoc/>
23+
public override bool CanSeek => !this.disposed;
24+
25+
/// <inheritdoc/>
26+
public override bool CanWrite => !this.disposed;
27+
28+
/// <see cref="MemoryStream.Length"/>
29+
public override long Length => this.inner.Length;
30+
31+
/// <inheritdoc/>
32+
public override long Position
33+
{
34+
get => this.inner.Position;
35+
set => this.inner.Position = value;
36+
}
37+
38+
/// <inheritdoc/>
39+
public override void Flush()
40+
{
41+
// nop
42+
}
43+
44+
/// <inheritdoc/>
45+
public override long Seek(long offset, SeekOrigin origin) => this.inner.Seek(offset, origin);
46+
47+
/// <inheritdoc/>
48+
public override void SetLength(long value) => this.inner.SetLength(value);
49+
50+
/// <inheritdoc/>
51+
public override int Read(byte[] buffer, int offset, int count)
52+
{
53+
this.CheckDisposed();
54+
return this.inner.Read(buffer, offset, count);
55+
}
56+
57+
/// <inheritdoc/>
58+
public override void Write(byte[] buffer, int offset, int count)
59+
{
60+
this.CheckDisposed();
61+
this.inner.Write(buffer, offset, count);
62+
}
63+
64+
internal static PooledMemoryStream Borrow()
65+
{
66+
if (Pool.TryDequeue(out var stream))
67+
{
68+
return new PooledMemoryStream(stream);
69+
}
70+
71+
return new PooledMemoryStream(new MemoryStream());
72+
}
73+
74+
/// <inheritdoc/>
75+
protected override void Dispose(bool disposing)
76+
{
77+
if (this.disposed)
78+
{
79+
return;
80+
}
81+
82+
this.disposed = true;
83+
if (disposing)
84+
{
85+
this.inner.SetLength(0);
86+
Pool.Enqueue(this.inner);
87+
}
88+
89+
base.Dispose(disposing);
90+
}
91+
92+
private void CheckDisposed()
93+
{
94+
if (this.disposed)
95+
{
96+
throw new ObjectDisposedException(this.GetType().FullName);
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)