Skip to content

Commit 761f27c

Browse files
committed
Improved a interop tests
1 parent a47f37a commit 761f27c

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "7.0.201"
3+
"version": "7.0.202"
44
}
55
}

test/JavaScriptEngineSwitcher.Tests/Interop/BundleTable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public static class BundleTable
44
{
55
private static bool _enableOptimizations = true;
66

7+
public static object SyncRoot = new object();
8+
79
public static bool EnableOptimizations
810
{
911
get

test/JavaScriptEngineSwitcher.Tests/Interop/Logging/DefaultLogger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
public class DefaultLogger
44
{
5+
public static object SyncRoot = new object();
56
public static ILogger Current = new NullLogger();
67
}
78
}

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,9 @@ public virtual void EmbeddingOfCustomReferenceTypeWithField()
11231123
// Arrange
11241124
Type defaultLoggerType = typeof(DefaultLogger);
11251125
Type throwExceptionLoggerType = typeof(ThrowExceptionLogger);
1126-
const string updateCode = "DefaultLogger.Current = new ThrowExceptionLogger();";
1126+
const string updateCode = @"var oldLogger = DefaultLogger.Current;
1127+
DefaultLogger.Current = new ThrowExceptionLogger();";
1128+
const string rollbackCode = "DefaultLogger.Current = oldLogger;";
11271129

11281130
const string input = "DefaultLogger.Current.ToString()";
11291131
const string targetOutput = "[throw exception logger]";
@@ -1135,9 +1137,13 @@ public virtual void EmbeddingOfCustomReferenceTypeWithField()
11351137
{
11361138
jsEngine.EmbedHostType("DefaultLogger", defaultLoggerType);
11371139
jsEngine.EmbedHostType("ThrowExceptionLogger", throwExceptionLoggerType);
1138-
jsEngine.Execute(updateCode);
11391140

1140-
output = jsEngine.Evaluate<string>(input);
1141+
lock (DefaultLogger.SyncRoot)
1142+
{
1143+
jsEngine.Execute(updateCode);
1144+
output = jsEngine.Evaluate<string>(input);
1145+
jsEngine.Execute(rollbackCode);
1146+
}
11411147
}
11421148

11431149
// Assert
@@ -1230,7 +1236,9 @@ public virtual void EmbeddingOfCustomReferenceTypeWithProperty()
12301236
{
12311237
// Arrange
12321238
Type bundleTableType = typeof(BundleTable);
1233-
const string updateCode = "BundleTable.EnableOptimizations = false;";
1239+
const string updateCode = @"var oldEnableOptimizationsValue = BundleTable.EnableOptimizations;
1240+
BundleTable.EnableOptimizations = false;";
1241+
const string rollbackCode = "BundleTable.EnableOptimizations = oldEnableOptimizationsValue;";
12341242

12351243
const string input = "BundleTable.EnableOptimizations";
12361244
const bool targetOutput = false;
@@ -1241,9 +1249,13 @@ public virtual void EmbeddingOfCustomReferenceTypeWithProperty()
12411249
using (var jsEngine = CreateJsEngine())
12421250
{
12431251
jsEngine.EmbedHostType("BundleTable", bundleTableType);
1244-
jsEngine.Execute(updateCode);
12451252

1246-
output = jsEngine.Evaluate<bool>(input);
1253+
lock (BundleTable.SyncRoot)
1254+
{
1255+
jsEngine.Execute(updateCode);
1256+
output = jsEngine.Evaluate<bool>(input);
1257+
jsEngine.Execute(rollbackCode);
1258+
}
12471259
}
12481260

12491261
// Assert

0 commit comments

Comments
 (0)