-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathBinaryResourceWriterUnitTest.cs
585 lines (503 loc) · 26.7 KB
/
BinaryResourceWriterUnitTest.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.Resources.Extensions.Tests
{
public class PreserializedResourceWriterTests
{
public static bool AllowsCustomResourceTypes => AppContext.TryGetSwitch("System.Resources.ResourceManager.AllowCustomResourceTypes", out bool isEnabled) ? isEnabled : true;
[Fact]
public static void ExceptionforNullStream()
{
Assert.Throws<ArgumentNullException>("stream", () => new PreserializedResourceWriter((Stream)null));
}
[Fact]
public static void ExceptionforNullFile()
{
Assert.Throws<ArgumentNullException>("fileName", () => new PreserializedResourceWriter((string)null));
}
[Fact]
public static void ExceptionforReadOnlyStream()
{
AssertExtensions.Throws<ArgumentException>(null, () =>
{
using (var readOnlyStream = new MemoryStream(new byte[1], false))
{
new PreserializedResourceWriter(readOnlyStream);
}
});
}
[Fact]
public static void ExceptionforNullResourceId()
{
using (var writer = new PreserializedResourceWriter(new MemoryStream()))
{
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, "value"));
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, new object()));
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, new byte[0]));
using (var stream = new MemoryStream())
{
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, stream));
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, stream, true));
Assert.Throws<ArgumentNullException>("name", () => writer.AddActivatorResource(null, stream, "System.DayOfWeek", false));
}
Assert.Throws<ArgumentNullException>("name", () => writer.AddBinaryFormattedResource(null, new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentNullException>("name", () => writer.AddTypeConverterResource(null, new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentNullException>("name", () => writer.AddResource(null, "Monday", "System.DayOfWeek"));
}
}
[Fact]
public static void ExceptionforDuplicateKey()
{
using (var writer = new PreserializedResourceWriter(new MemoryStream()))
{
writer.AddResource("duplicate", "value");
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "value"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new object()));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new byte[0]));
using (var stream = new MemoryStream())
{
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream, true));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
}
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("Duplicate", "value"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("dUplicate", new object()));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duPlicate", new byte[0]));
using (var stream = new MemoryStream())
{
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("dupLicate", stream));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplIcate", stream, true));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddActivatorResource("dupliCate", stream, "System.DayOfWeek", false));
}
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicAte", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicaTe", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicatE", "Monday", "System.DayOfWeek"));
}
}
[Fact]
public static void ExceptionForAddAfterGenerate()
{
using (var writer = new PreserializedResourceWriter(new MemoryStream()))
{
writer.AddResource("duplicate", "value");
writer.Generate();
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", "value"));
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", new object()));
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", new byte[0]));
using (var stream = new MemoryStream())
{
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", stream));
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", stream, true));
Assert.Throws<InvalidOperationException>(() => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
}
Assert.Throws<InvalidOperationException>(() => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
Assert.Throws<InvalidOperationException>(() => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
Assert.Throws<InvalidOperationException>(() => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));
}
}
[Fact]
public static void EmptyResources()
{
byte[] writerBuffer, binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (ResourceWriter writer = new ResourceWriter(ms))
{
writer.Generate();
writerBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
Assert.Equal(writerBuffer, binaryWriterBuffer);
}
[Fact]
public static void PrimitiveResources()
{
IReadOnlyDictionary<string,object> values = TestData.Primitive;
Action<IResourceWriter> addData = (writer) =>
{
foreach (var pair in values)
{
writer.AddResource(pair.Key, pair.Value);
}
};
byte[] writerBuffer, binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (ResourceWriter writer = new ResourceWriter(ms))
{
addData(writer);
writer.Generate();
writerBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
addData(writer);
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
// PreserializedResourceWriter should write ResourceWriter/ResourceReader format
Assert.Equal(writerBuffer, binaryWriterBuffer);
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (ResourceReader reader = new ResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
}
}
// DeserializingResourceReader can read ResourceReader format
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[Fact]
public static void PrimitiveResourcesAsStrings()
{
IReadOnlyDictionary<string, object> values = TestData.Primitive;
byte[] writerBuffer, binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (ResourceWriter writer = new ResourceWriter(ms))
{
foreach (var pair in values)
{
writer.AddResource(pair.Key, pair.Value);
}
writer.Generate();
writerBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
foreach (var pair in values)
{
writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType()));
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
// PreserializedResourceWriter should write ResourceWriter/ResourceReader format
Assert.Equal(writerBuffer, binaryWriterBuffer);
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (ResourceReader reader = new ResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
}
}
// DeserializingResourceReader can read ResourceReader format
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34495", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34008", TestPlatforms.Linux, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50934", TestPlatforms.Android)]
public static void BinaryFormattedResources()
{
var values = TestData.BinaryFormatted;
byte[] binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
foreach (var pair in values)
{
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, pair.Value);
writer.AddBinaryFormattedResource(pair.Key, memoryStream.ToArray(), TestData.GetSerializationTypeName(pair.Value.GetType()));
}
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
// DeserializingResourceReader can read BinaryFormatted resources with type names.
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34495", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34008", TestPlatforms.Linux, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50934", TestPlatforms.Android)]
public static void BinaryFormattedResourcesWithoutTypeName()
{
var values = TestData.BinaryFormatted;
byte[] binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
foreach (var pair in values)
{
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, pair.Value);
writer.AddBinaryFormattedResource(pair.Key, memoryStream.ToArray());
}
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
// DeserializingResourceReader can read ResourceReader format
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[ConditionalFact(nameof(AllowsCustomResourceTypes))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34495", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public static void TypeConverterByteArrayResources()
{
var values = TestData.ByteArrayConverter;
byte[] binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
foreach (var pair in values)
{
TypeConverter converter = TypeDescriptor.GetConverter(pair.Value.GetType());
byte[] buffer = (byte[])converter.ConvertTo(pair.Value, typeof(byte[]));
writer.AddTypeConverterResource(pair.Key, buffer, TestData.GetSerializationTypeName(pair.Value.GetType()));
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[ConditionalFact(nameof(AllowsCustomResourceTypes))]
public static void TypeConverterStringResources()
{
var values = TestData.StringConverter;
byte[] binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
foreach (var pair in values)
{
writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType()));
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value);
}
}
}
[ConditionalFact(nameof(AllowsCustomResourceTypes))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34495", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34008", TestPlatforms.Linux, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public static void StreamResources()
{
var values = TestData.Activator;
byte[] binaryWriterBuffer;
using (MemoryStream ms = new MemoryStream())
using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
{
foreach (var pair in values)
{
pair.Value.stream.Seek(0, SeekOrigin.Begin);
writer.AddActivatorResource(pair.Key, pair.Value.stream, TestData.GetSerializationTypeName(pair.Value.type), false);
}
writer.Generate();
binaryWriterBuffer = ms.ToArray();
}
using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
{
IDictionaryEnumerator dictEnum = reader.GetEnumerator();
while (dictEnum.MoveNext())
{
var expectedTuple = values[(string)dictEnum.Key];
expectedTuple.stream.Seek(0, SeekOrigin.Begin);
object expected = Activator.CreateInstance(expectedTuple.type, new object[] { expectedTuple.stream });
ResourceValueEquals(expected, dictEnum.Value);
}
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50934", TestPlatforms.Android)]
public static void CanReadViaResourceManager()
{
ResourceManager resourceManager = new ResourceManager(typeof(TestData));
IEnumerable<KeyValuePair<string, object>> objectPairs = TestData.Primitive
.Concat(TestData.PrimitiveAsString)
.Concat(TestData.BinaryFormattedWithoutDrawing)
.Concat(TestData.BinaryFormattedWithoutDrawingNoType)
.Concat(TestData.ByteArrayConverterWithoutDrawing)
.Concat(TestData.StringConverterWithoutDrawing);
foreach (KeyValuePair<string, object> pair in objectPairs)
{
var actualValue = resourceManager.GetObject(pair.Key);
Assert.Equal(pair.Value, actualValue);
}
foreach (KeyValuePair<string, (Type type, Stream stream)> pair in TestData.ActivatorWithoutDrawing)
{
pair.Value.stream.Seek(0, SeekOrigin.Begin);
var expectedValue = Activator.CreateInstance(pair.Value.type, pair.Value.stream);
var actualValue = resourceManager.GetObject(pair.Key);
Assert.Equal(expectedValue, actualValue);
}
}
[ConditionalFact(nameof(AllowsCustomResourceTypes))]
public static void ResourceManagerLoadsCorrectReader()
{
ResourceManager resourceManager = new ResourceManager(typeof(TestData));
ResourceSet resSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true);
IResourceReader reader = (IResourceReader)resSet.GetType().GetField("_defaultReader", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(resSet);
Assert.IsType<DeserializingResourceReader>(reader);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50934", TestPlatforms.Android)]
public static void EmbeddedResourcesAreUpToDate()
{
// this is meant to catch a case where our embedded test resources are out of date with respect to the current writer.
// that could be intentional, or accidental. Regardless we want to know.
using (Stream resourcesStream = typeof(TestData).Assembly.GetManifestResourceStream("System.Resources.Extensions.Tests.TestData.resources"))
using (MemoryStream actualData = new MemoryStream(), expectedData = new MemoryStream())
{
TestData.WriteResourcesStream(actualData);
resourcesStream.CopyTo(expectedData);
if (!PlatformDetection.IsNetFramework)
{
// Some types rely on SerializationInfo.SetType on .NETCore
// which result in a different binary format
Assert.Equal(expectedData.ToArray(), actualData.ToArray());
}
}
}
/// <summary>
/// This test has multiple threads simultaneously loop over the keys of a moderately-sized resx using
/// <see cref="ResourceManager"/> and call <see cref="ResourceManager.GetString(string)"/> for each key.
/// This has historically been prone to thread-safety bugs because of the shared cache state and internal
/// method calls between RuntimeResourceSet and <see cref="DeserializingResourceReader"/>.
///
/// Running with <paramref name="useEnumeratorEntry"/> TRUE replicates https://github.com/dotnet/runtime/issues/74868,
/// while running with FALSE replicates the error from https://github.com/dotnet/runtime/issues/74052.
/// </summary>
/// <param name="useEnumeratorEntry">
/// Whether to use <see cref="IDictionaryEnumerator.Entry"/> vs. <see cref="IDictionaryEnumerator.Key"/> when enumerating;
/// these follow fairly different code paths.
/// </param>]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[InlineData(false)]
[InlineData(true)]
public static void TestResourceManagerIsSafeForConcurrentAccessAndEnumeration(bool useEnumeratorEntry)
{
ResourceManager manager = new(
typeof(TestData).FullName,
typeof(TestData).Assembly,
typeof(DeserializingResourceReader).Assembly.GetType("System.Resources.Extensions.RuntimeResourceSet", throwOnError: true));
const int Threads = 10;
using Barrier barrier = new(Threads);
Task task = Task.WhenAll(Enumerable.Range(0, Threads).Select(_ => Task.Run(WaitForBarrierThenEnumerateResources)));
Assert.True(task.Wait(TimeSpan.FromSeconds(30)));
void WaitForBarrierThenEnumerateResources()
{
barrier.SignalAndWait();
ResourceSet set = manager.GetResourceSet(CultureInfo.InvariantCulture, createIfNotExists: true, tryParents: true);
IDictionaryEnumerator enumerator = set.GetEnumerator();
while (enumerator.MoveNext())
{
object key = useEnumeratorEntry ? enumerator.Entry.Key : enumerator.Key;
manager.GetObject((string)key);
Thread.Sleep(1);
}
}
}
private static void ResourceValueEquals(object expected, object actual)
{
if (actual is Bitmap bitmap)
{
BitmapEquals((Bitmap)expected, bitmap);
}
else if (actual is Icon icon)
{
BitmapEquals(((Icon)expected).ToBitmap(), icon.ToBitmap());
}
else if (actual is Font font)
{
Font expectedFont = (Font)expected;
Assert.Equal(expectedFont.FontFamily, font.FontFamily);
Assert.Equal(expectedFont.Size, font.Size);
Assert.Equal(expectedFont.Style, font.Style);
Assert.Equal(expectedFont.Unit, font.Unit);
}
else
{
Assert.Equal(expected, actual);
}
}
private static void BitmapEquals(Bitmap left, Bitmap right)
{
Assert.Equal(left.Size, right.Size);
for (int x = 0; x < left.Width; ++x)
{
for (int y = 0; y < left.Height; ++y)
{
Assert.Equal(left.GetPixel(x, y), right.GetPixel(x, y));
}
}
}
}
}