Skip to content

Commit

Permalink
Fix concurrent issues in DataSerializationHelper (#998)
Browse files Browse the repository at this point in the history
* Fix concurrent issues in DataSerializationHelper

Co-authored-by: Alexander Alexeev <Alexander.Alexeev@kaspersky.com>
Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 18, 2021
1 parent 0a57466 commit 391116c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
41 changes: 12 additions & 29 deletions src/Adapter/MSTest.CoreAdapter/Helpers/DataSerializationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers
{
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;

internal static class DataSerializationHelper
{
private static readonly Dictionary<Type, DataContractJsonSerializer> SerializerCache = new Dictionary<Type, DataContractJsonSerializer>();
private static readonly ConcurrentDictionary<string, DataContractJsonSerializer> SerializerCache = new ConcurrentDictionary<string, DataContractJsonSerializer>();
private static readonly DataContractJsonSerializerSettings SerializerSettings = new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true,
Expand All @@ -35,12 +33,10 @@ public static string[] Serialize(object[] data)
}

var serializedData = new string[data.Length * 2];

for (int i = 0; i < data.Length; i++)
{
var typeIndex = i * 2;
var dataIndex = typeIndex + 1;

if (data[i] == null)
{
serializedData[typeIndex] = null;
Expand Down Expand Up @@ -86,16 +82,16 @@ public static object[] Deserialize(string[] serializedData)
for (int i = 0; i < length; i++)
{
var typeIndex = i * 2;
var typeName = serializedData[typeIndex];
var assemblyQualifiedName = serializedData[typeIndex];
var serializedValue = serializedData[typeIndex + 1];

if (serializedValue == null || typeName == null)
if (serializedValue == null || assemblyQualifiedName == null)
{
data[i] = null;
continue;
}

var serializer = GetSerializer(typeName);
var serializer = GetSerializer(assemblyQualifiedName);

var serialzedDataBytes = Encoding.UTF8.GetBytes(serializedValue);
using (var memoryStream = new MemoryStream(serialzedDataBytes))
Expand All @@ -107,31 +103,18 @@ public static object[] Deserialize(string[] serializedData)
return data;
}

private static DataContractJsonSerializer GetSerializer(string typeName)
private static DataContractJsonSerializer GetSerializer(string assemblyQualifiedName)
{
var serializer = SerializerCache.SingleOrDefault(i => i.Key.FullName == typeName);
if (serializer.Value != null)
{
return serializer.Value;
}

var type = Type.GetType(typeName);
if (type != null)
{
return GetSerializer(type);
}

return GetSerializer(typeof(object));
return SerializerCache.GetOrAdd(
assemblyQualifiedName,
_ => new DataContractJsonSerializer(Type.GetType(assemblyQualifiedName) ?? typeof(object), SerializerSettings));
}

private static DataContractJsonSerializer GetSerializer(Type type)
{
if (SerializerCache.ContainsKey(type))
{
return SerializerCache[type];
}

return SerializerCache[type] = new DataContractJsonSerializer(type, SerializerSettings);
return SerializerCache.GetOrAdd(
type.AssemblyQualifiedName,
_ => new DataContractJsonSerializer(type, SerializerSettings));
}
}
}
2 changes: 1 addition & 1 deletion test/E2ETests/Automation.CLI/CLITestBase.common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class CLITestBase
private const string PackagesFolder = "packages";

// This value is automatically updated by "build.ps1" script.
private const string TestPlatformCLIPackage = @"Microsoft.TestPlatform\17.0.0-preview-20210730-03";
private const string TestPlatformCLIPackage = @"Microsoft.TestPlatform\17.0.0-release-20210923-02";
private const string VstestConsoleRelativePath = @"tools\net451\Common7\IDE\Extensions\TestPlatform\vstest.console.exe";

/// <summary>
Expand Down

0 comments on commit 391116c

Please # to comment.