Skip to content

Commit 23b2bb5

Browse files
authored
fix: TestObject use ConcurrentDictionary instead of Dictionary (#4450)
1 parent aa01c7f commit 23b2bb5

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/Microsoft.TestPlatform.ObjectModel/TestObject.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
67
using System.ComponentModel;
78
using System.Diagnostics.CodeAnalysis;
@@ -26,7 +27,7 @@ public abstract class TestObject
2627
/// <summary>
2728
/// The store for all the properties registered.
2829
/// </summary>
29-
private readonly Dictionary<TestProperty, object?> _store;
30+
private readonly ConcurrentDictionary<TestProperty, object?> _store = new();
3031

3132
/// <summary>
3233
/// Property used for Json (de)serialization of store dictionary. Serialization of dictionaries
@@ -66,11 +67,6 @@ public abstract class TestObject
6667
return _store;
6768
}
6869

69-
protected TestObject()
70-
{
71-
_store = new Dictionary<TestProperty, object?>();
72-
}
73-
7470
[OnSerializing]
7571
#if FullCLR
7672
private void CacheLazyValuesOnSerializing(StreamingContext context)
@@ -84,11 +80,11 @@ public void CacheLazyValuesOnSerializing(StreamingContext context)
8480
{
8581
var lazyValue = (ILazyPropertyValue?)kvp.Value;
8682
var value = lazyValue?.Value;
87-
_store.Remove(kvp.Key);
83+
_store.TryRemove(kvp.Key, out _);
8884

8985
if (value != null)
9086
{
91-
_store.Add(kvp.Key, value);
87+
_store.TryAdd(kvp.Key, value);
9288
}
9389
}
9490
}
@@ -172,10 +168,7 @@ public void SetPropertyValue(TestProperty property, object? value)
172168
public void RemovePropertyValue(TestProperty property)
173169
{
174170
ValidateArg.NotNull(property, nameof(property));
175-
if (_store.TryGetValue(property, out _))
176-
{
177-
_store.Remove(property);
178-
}
171+
_store.TryRemove(property, out _);
179172
}
180173

181174
/// <summary>

test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/Serialization/TestObjectConverterTests.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ public void TestCaseObjectShouldSerializeCustomProperties()
4545
var json = Serialize(test);
4646

4747
// Use raw deserialization to validate basic properties
48-
var expectedJson = "{\"Properties\":[{\"Key\":{\"Id\":\"1\",\"Label\":\"label1\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Guid\"},\"Value\":\"02048dfd-3da7-475d-a011-8dd1121855ec\"},{\"Key\":{\"Id\":\"2\",\"Label\":\"label2\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Int32\"},\"Value\":29}]}";
49-
Assert.AreEqual(expectedJson, json);
48+
// Because properties are backed up by a ConcurrentDictionary we don't have control over the order of serialization
49+
var expectedJsonWithKey1First = "{\"Properties\":[{\"Key\":{\"Id\":\"1\",\"Label\":\"label1\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Guid\"},\"Value\":\"02048dfd-3da7-475d-a011-8dd1121855ec\"},{\"Key\":{\"Id\":\"2\",\"Label\":\"label2\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Int32\"},\"Value\":29}]}";
50+
var expectedJsonWithKey2First = "{\"Properties\":[{\"Key\":{\"Id\":\"2\",\"Label\":\"label2\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Int32\"},\"Value\":29},{\"Key\":{\"Id\":\"1\",\"Label\":\"label1\",\"Category\":\"\",\"Description\":\"\",\"Attributes\":0,\"ValueType\":\"System.Guid\"},\"Value\":\"02048dfd-3da7-475d-a011-8dd1121855ec\"}]}";
51+
52+
if (json != expectedJsonWithKey1First && json != expectedJsonWithKey2First)
53+
{
54+
Assert.Fail($"Was expecting <{json}> to be either <{expectedJsonWithKey1First}> or <{expectedJsonWithKey2First}>.");
55+
}
5056
}
5157

5258
[TestMethod]
@@ -86,8 +92,8 @@ public void TestObjectShouldDeserializeCustomProperties()
8692

8793
var properties = test.Properties.ToArray();
8894
Assert.AreEqual(2, properties.Length);
89-
Assert.AreEqual(Guid.Parse("02048dfd-3da7-475d-a011-8dd1121855ec"), test.GetPropertyValue(properties[0]));
90-
Assert.AreEqual(29, test.GetPropertyValue(properties[1]));
95+
Assert.AreEqual(Guid.Parse("02048dfd-3da7-475d-a011-8dd1121855ec"), test.GetPropertyValue(properties.First(x => x.Label == "label1")));
96+
Assert.AreEqual(29, test.GetPropertyValue(properties.First(x => x.Label == "label2")));
9197
}
9298

9399
[TestMethod]

0 commit comments

Comments
 (0)