Skip to content

Commit e64cce6

Browse files
authored
[wasm] Add task marshaling tests. (#61324)
Add various tests for marshaling task to JavaScript promise. - Sync and async. - Task and ValueTask. - Generic and non-generic. - Successful and failed.
1 parent 3dce93b commit e64cce6

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

Diff for: src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/HelperMarshal.cs

+64
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.InteropServices.JavaScript;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Threading.Tasks;
78
using Xunit;
89

910
namespace System.Runtime.InteropServices.JavaScript.Tests
@@ -641,6 +642,69 @@ private static Func<Array, Action<Array>> CreateFunctionAcceptingArray()
641642
};
642643
}
643644

645+
public static Task SynchronousTask()
646+
{
647+
return Task.CompletedTask;
648+
}
649+
650+
public static async Task AsynchronousTask()
651+
{
652+
await Task.Yield();
653+
}
654+
655+
public static Task<int> SynchronousTaskInt(int i)
656+
{
657+
return Task.FromResult(i);
658+
}
659+
660+
public static async Task<int> AsynchronousTaskInt(int i)
661+
{
662+
await Task.Yield();
663+
return i;
664+
}
665+
666+
public static Task FailedSynchronousTask()
667+
{
668+
return Task.FromException(new Exception());
669+
}
670+
671+
public static async Task FailedAsynchronousTask()
672+
{
673+
await Task.Yield();
674+
throw new Exception();
675+
}
676+
677+
public static async ValueTask AsynchronousValueTask()
678+
{
679+
await Task.Yield();
680+
}
681+
682+
public static ValueTask SynchronousValueTask()
683+
{
684+
return ValueTask.CompletedTask;
685+
}
686+
687+
public static ValueTask<int> SynchronousValueTaskInt(int i)
688+
{
689+
return ValueTask.FromResult(i);
690+
}
691+
692+
public static async ValueTask<int> AsynchronousValueTaskInt(int i)
693+
{
694+
await Task.Yield();
695+
return i;
696+
}
697+
698+
public static ValueTask FailedSynchronousValueTask()
699+
{
700+
return ValueTask.FromException(new Exception());
701+
}
702+
703+
public static async ValueTask FailedAsynchronousValueTask()
704+
{
705+
await Task.Yield();
706+
throw new Exception();
707+
}
644708
}
645709

646710
public enum TestEnum : uint {

Diff for: src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/MarshalTests.cs

+119
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using System.Runtime.InteropServices.JavaScript;
55
using System.Collections.Generic;
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using Xunit;
79

810
namespace System.Runtime.InteropServices.JavaScript.Tests
@@ -899,5 +901,122 @@ public static void InvokeJSNotInGlobalScope()
899901
var result = Runtime.InvokeJS(@"var test_local_variable_name = 5; globalThis.test_local_variable_name");
900902
Assert.Null(result);
901903
}
904+
905+
private static async Task<bool> MarshalTask(string helperMethodName, string helperMethodArgs = "", string resolvedBody = "")
906+
{
907+
Runtime.InvokeJS(
908+
@"globalThis.__test_promise_completed = false; " +
909+
@"globalThis.__test_promise_resolved = false; " +
910+
@"globalThis.__test_promise_failed = false; " +
911+
$@"var t = App.call_test_method ('{helperMethodName}', [ {helperMethodArgs} ], 'i'); " +
912+
"t.finally(result => { globalThis.__test_promise_completed = true; }); " +
913+
"t.then(result => { globalThis.__test_promise_resolved = true; " + resolvedBody + " }); " +
914+
"t.catch(e => { console.log(e); globalThis.__test_promise_failed = true; }); "
915+
);
916+
917+
await Task.Delay(1);
918+
919+
var completed = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_completed"));
920+
Assert.True(completed, "JavasScript promise did not completed.");
921+
922+
var resolved = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_resolved"));
923+
return resolved;
924+
}
925+
926+
private static async Task MarshalTaskReturningInt(string helperMethodName)
927+
{
928+
HelperMarshal._intValue = 0;
929+
930+
bool success = await MarshalTask(helperMethodName, "7", "App.call_test_method ('InvokeInt', [ result ], 'i');");
931+
932+
Assert.True(success, $"{helperMethodName} didn't succeeded.");
933+
Assert.Equal(7, HelperMarshal._intValue);
934+
}
935+
936+
[Fact]
937+
public static async Task MarshalSynchronousTask()
938+
{
939+
bool success = await MarshalTask("SynchronousTask");
940+
Assert.True(success, "SynchronousTask didn't succeeded.");
941+
}
942+
943+
[Fact]
944+
public static async Task MarshalAsynchronousTask()
945+
{
946+
bool success = await MarshalTask("AsynchronousTask");
947+
Assert.True(success, "AsynchronousTask didn't succeeded.");
948+
}
949+
950+
[Fact]
951+
public static Task MarshalSynchronousTaskInt()
952+
{
953+
return MarshalTaskReturningInt("SynchronousTaskInt");
954+
}
955+
956+
[Fact]
957+
public static Task MarshalAsynchronousTaskInt()
958+
{
959+
return MarshalTaskReturningInt("AsynchronousTaskInt");
960+
}
961+
962+
[Fact]
963+
public static async Task MarshalFailedSynchronousTask()
964+
{
965+
bool success = await MarshalTask("FailedSynchronousTask");
966+
Assert.False(success, "FailedSynchronousTask didn't failed.");
967+
}
968+
969+
[Fact]
970+
public static async Task MarshalFailedAsynchronousTask()
971+
{
972+
bool success = await MarshalTask("FailedAsynchronousTask");
973+
Assert.False(success, "FailedAsynchronousTask didn't failed.");
974+
}
975+
976+
[Fact]
977+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
978+
public static async Task MarshalSynchronousValueTaskDoesNotWorkYet()
979+
{
980+
bool success = await MarshalTask("SynchronousValueTask");
981+
Assert.True(success, "SynchronousValueTask didn't succeeded.");
982+
}
983+
984+
[Fact]
985+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
986+
public static async Task MarshalAsynchronousValueTaskDoesNotWorkYet()
987+
{
988+
bool success = await MarshalTask("AsynchronousValueTask");
989+
Assert.True(success, "AsynchronousValueTask didn't succeeded.");
990+
}
991+
992+
[Fact]
993+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
994+
public static Task MarshalSynchronousValueTaskIntDoesNotWorkYet()
995+
{
996+
return MarshalTaskReturningInt("SynchronousValueTaskInt");
997+
}
998+
999+
[Fact]
1000+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
1001+
public static Task MarshalAsynchronousValueTaskIntDoesNotWorkYet()
1002+
{
1003+
return MarshalTaskReturningInt("AsynchronousValueTaskInt");
1004+
}
1005+
1006+
[Fact]
1007+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
1008+
public static async Task MarshalFailedSynchronousValueTaskDoesNotWorkYet()
1009+
{
1010+
bool success = await MarshalTask("FailedSynchronousValueTask");
1011+
Assert.False(success, "FailedSynchronousValueTask didn't failed.");
1012+
}
1013+
1014+
[Fact]
1015+
[ActiveIssue("https://github.com/dotnet/runtime/issues/61368")]
1016+
public static async Task MarshalFailedAsynchronousValueTaskDoesNotWorkYet()
1017+
{
1018+
bool success = await MarshalTask("FailedAsynchronousValueTask");
1019+
Assert.False(success, "FailedAsynchronousValueTask didn't failed.");
1020+
}
9021021
}
9031022
}

0 commit comments

Comments
 (0)