Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[wasm] Add task marshaling tests.
Add various tests for marshaling task to JavaScript promise.

- Sync and async.
- Generic and non-generic.
- Successful and failed.
  • Loading branch information
maraf committed Nov 8, 2021
commit 207f97b4f0bc9215dc091c0e0c4e9694db1af717
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices.JavaScript;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace System.Runtime.InteropServices.JavaScript.Tests
Expand Down Expand Up @@ -641,6 +642,37 @@ private static Func<Array, Action<Array>> CreateFunctionAcceptingArray()
};
}

public static Task SynchronousTask()
{
return Task.CompletedTask;
}

public static async Task AsynchronousTask()
{
await Task.Yield();
}

public static Task<int> SynchronousTaskInt(int i)
{
return Task.FromResult(i);
}

public static async Task<int> AsynchronousTaskInt(int i)
{
await Task.Yield();
return i;
}

public static Task FailedSynchronousTask()
{
return Task.FromException(new Exception());
}

public static async Task FailedAsynchronousTask()
{
await Task.Yield();
throw new Exception();
}
}

public enum TestEnum : uint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Runtime.InteropServices.JavaScript;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Runtime.InteropServices.JavaScript.Tests
Expand Down Expand Up @@ -899,5 +901,78 @@ public static void InvokeJSNotInGlobalScope()
var result = Runtime.InvokeJS(@"var test_local_variable_name = 5; globalThis.test_local_variable_name");
Assert.Null(result);
}

private static async Task<bool> MarshalTask(string helperMethodName, string helperMethodArgs = "", string resolvedBody = "")
{
Runtime.InvokeJS(
@"globalThis.__test_promise_completed = false; " +
@"globalThis.__test_promise_resolved = false; " +
@"globalThis.__test_promise_failed = false; " +
$@"var t = App.call_test_method ('{helperMethodName}', [ {helperMethodArgs} ], 'i'); " +
"t.finally(result => { globalThis.__test_promise_completed = true; }); " +
"t.then(result => { globalThis.__test_promise_resolved = true; " + resolvedBody + " }); " +
"t.catch(e => { console.log(e); globalThis.__test_promise_failed = true; }); "
);

await Task.Delay(1);

var completed = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_completed"));
Assert.True(completed, "JavasScript promise did not completed.");

var resolved = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_resolved"));
return resolved;
}

[Fact]
public static async Task MarshalSynchronousTask()
{
bool success = await MarshalTask("SynchronousTask");
Assert.True(success, "SynchronousTask didn't succeeded.");
}

[Fact]
public static async Task MarshalAsynchronousTask()
{
bool success = await MarshalTask("AsynchronousTask");
Assert.True(success, "AsynchronousTask didn't succeeded.");
}

[Fact]
public static async Task MarshalSynchronousTaskInt()
{
HelperMarshal._intValue = 0;

bool success = await MarshalTask("SynchronousTaskInt", "7", "App.call_test_method ('InvokeInt', [ result ], 'i');");

Assert.True(success, "SynchronousTask didn't succeeded.");
Assert.Equal(7, HelperMarshal._intValue);
}

[Fact]
public static async Task MarshalAsynchronousTaskInt()
{
HelperMarshal._intValue = 0;

bool success = await MarshalTask("AsynchronousTaskInt", "7", "App.call_test_method ('InvokeInt', [ result ], 'i');");

Assert.True(success, "AsynchronousTask didn't succeeded.");
Assert.Equal(7, HelperMarshal._intValue);
}

[Fact]
public static async Task MarshalFailedSynchronousTask()
{
bool success = await MarshalTask("FailedSynchronousTask");

Assert.False(success, "FailedSynchronousTask didn't failed.");
}

[Fact]
public static async Task MarshalFailedAsynchronousTask()
{
bool success = await MarshalTask("FailedAsynchronousTask");

Assert.False(success, "FailedAsynchronousTask didn't failed.");
}
}
}