Repository navigation
Expand file tree
/
Copy pathDisposableTest.cs
More file actions
181 lines (167 loc) · 5.61 KB
/
Copy pathDisposableTest.cs
File metadata and controls
181 lines (167 loc) · 5.61 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Cuemon.Assets;
using Cuemon.Extensions.IO;
using Codebelt.Extensions.Xunit;
using Cuemon.IO;
using Cuemon.Threading;
using Xunit;
namespace Cuemon;
public class DisposableTest : Test
{
public DisposableTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public void SafeInvoke_ShouldAbideRuleCA2000()
{
var guid = Guid.NewGuid();
var called = 0;
var stream = Patterns.SafeInvoke(() => new MemoryStream(), ms =>
{
called++;
ms.WriteByte(1);
ms.Position = 0;
return ms;
});
Assert.NotNull(stream);
Assert.Equal(1, called);
Assert.Equal(1, stream.Length);
MemoryStream msRef = null;
called = 0;
stream = Patterns.SafeInvoke(() => new MemoryStream(), (ms, g) =>
{
msRef = ms;
Assert.Equal(guid, g);
throw new InvalidOperationException();
}, guid, (exception, g) =>
{
Assert.Equal(guid, g);
Assert.True(exception is InvalidOperationException);
});
Assert.Equal(0, called);
Assert.Null(stream);
Assert.Throws<ObjectDisposedException>(() => msRef.Length);
stream = Patterns.SafeInvoke(() => new MemoryStream(), (ms, n1, n2, n3, n4, n5) =>
{
called++;
ms.WriteAllAsync(Decorator.Enclose($"{n1}{n2}{n3}{n4}{n5}").ToByteArray()).GetAwaiter().GetResult();
ms.Position = 0;
return ms;
}, 1, 2, 3, 4, 5);
Assert.NotNull(stream);
Assert.Equal(1, called);
Assert.Equal(5, stream.Length);
Assert.Equal("12345", Decorator.Enclose(stream).ToEncodedString());
}
[Fact]
public async Task SafeInvokeAsync_ShouldAbideRuleCA2000()
{
var guid = Guid.NewGuid();
var called = 0;
var stream = await AsyncPatterns.SafeInvokeAsync(() => new MemoryStream(), async (ms, ct) =>
{
called++;
await ms.WriteAllAsync(new byte[] { 1 }, ct);
ms.Position = 0;
return ms;
});
Assert.NotNull(stream);
Assert.Equal(1, called);
Assert.Equal(1, stream.Length);
MemoryStream msRef = null;
called = 0;
stream = await AsyncPatterns.SafeInvokeAsync(() => new MemoryStream(), (ms, g, ct) =>
{
msRef = ms;
Assert.Equal(guid, g);
throw new InvalidOperationException();
}, guid, (exception, g, ct) =>
{
Assert.Equal(guid, g);
Assert.True(exception is InvalidOperationException);
return Task.CompletedTask;
}, default);
Assert.Equal(0, called);
Assert.Null(stream);
Assert.Throws<ObjectDisposedException>(() => msRef.Length);
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
{
var ctsShouldFail = new CancellationTokenSource(TimeSpan.FromMilliseconds(5));
msRef = null;
called = 0;
stream = await AsyncPatterns.SafeInvokeAsync(() => new MemoryStream(), async (ms, g, ct) =>
{
msRef = ms;
Assert.Equal(guid, g);
await Task.Delay(TimeSpan.FromSeconds(1));
await ms.WriteAllAsync(new byte[] { 1 }, ct);
ms.Position = 0;
return ms;
}, guid, (exception, g, ct) =>
{
Assert.Equal(guid, g);
Assert.True(exception is TaskCanceledException);
return Task.CompletedTask;
}, ctsShouldFail.Token);
Assert.Equal(0, called);
Assert.Null(stream);
Assert.Throws<ObjectDisposedException>(() => msRef.Length);
});
stream = await AsyncPatterns.SafeInvokeAsync(() => new MemoryStream(), async (ms, n1, n2, n3, n4, n5, ct) =>
{
called++;
var bytes = Decorator.Enclose($"{n1}{n2}{n3}{n4}{n5}").ToByteArray();
await ms.WriteAllAsync(bytes, ct);
ms.Position = 0;
return ms;
}, 1, 2, 3, 4, 5, default);
Assert.NotNull(stream);
Assert.Equal(1, called);
Assert.Equal(5, stream.Length);
Assert.Equal("12345", Decorator.Enclose(stream).ToEncodedString());
}
[Fact]
public void ManagedDisposable_VerifyThatAssetIsBeingDisposed()
{
ManagedDisposable mdRef = null;
using (var md = new ManagedDisposable())
{
mdRef = md;
Assert.NotNull(md.Stream);
Assert.Equal(0, md.Stream.Length);
Assert.False(mdRef.Disposed);
}
Assert.NotNull(mdRef);
Assert.Null(mdRef.Stream);
Assert.True(mdRef.Disposed);
}
private WeakReference<UnmanagedDisposable> unmanaged = null;
[Fact]
public void UnmanagedDisposable_VerifyThatAssetIsBeingDisposedOnFinalize()
{
Action body = () =>
{
var o = new UnmanagedDisposable();
Assert.NotEqual(IntPtr.Zero, o._libHandle);
Assert.NotEqual(IntPtr.Zero, o._handle);
unmanaged = new WeakReference<UnmanagedDisposable>(o, true);
};
try
{
body();
}
finally
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
Task.Delay(500).Wait(); // Add a small delay
}
if (unmanaged.TryGetTarget(out var ud2))
{
Assert.True(ud2.Disposed);
}
}
}