public async Task CanCompleteAsyncCallsWithErrorsDuringDeserialization() { // Arrange var runtime = new TestJSRuntime(); // Act/Assert: Tasks not initially completed var unrelatedTask = runtime.InvokeAsync <string>("unrelated call", Array.Empty <object>()); var task = runtime.InvokeAsync <int>("test identifier", Array.Empty <object>()); Assert.False(unrelatedTask.IsCompleted); Assert.False(task.IsCompleted); using var jsonDocument = JsonDocument.Parse("\"Not a string\""); // Act/Assert: Task can be failed runtime.OnEndInvoke( runtime.BeginInvokeCalls[1].AsyncHandle, /* succeeded: */ true, new JSAsyncCallResult(jsonDocument, jsonDocument.RootElement)); Assert.False(unrelatedTask.IsCompleted); var jsException = await Assert.ThrowsAsync <JSException>(() => task); Assert.IsType <JsonException>(jsException.InnerException); // Verify we've disposed the JsonDocument. Assert.Throws <ObjectDisposedException>(() => jsonDocument.RootElement.Type); }
public void CanCompleteAsyncCallsAsFailure() { // Arrange var runtime = new TestJSRuntime(); // Act/Assert: Tasks not initially completed var unrelatedTask = runtime.InvokeAsync <string>("unrelated call", Array.Empty <object>()); var task = runtime.InvokeAsync <string>("test identifier", Array.Empty <object>()); Assert.False(unrelatedTask.IsCompleted); Assert.False(task.IsCompleted); using var jsonDocument = JsonDocument.Parse("\"This is a test exception\""); // Act/Assert: Task can be failed runtime.OnEndInvoke( runtime.BeginInvokeCalls[1].AsyncHandle, /* succeeded: */ false, new JSAsyncCallResult(jsonDocument, jsonDocument.RootElement)); Assert.False(unrelatedTask.IsCompleted); Assert.True(task.IsCompleted); Assert.IsType <AggregateException>(task.Exception); Assert.IsType <JSException>(task.Exception.InnerException); Assert.Equal("This is a test exception", ((JSException)task.Exception.InnerException).Message); }
public async Task CompletingSameAsyncCallMoreThanOnce_IgnoresSecondResultAsync() { // Arrange var runtime = new TestJSRuntime(); // Act/Assert var task = runtime.InvokeAsync <string>("test identifier", Array.Empty <object>()); var asyncHandle = runtime.BeginInvokeCalls[0].AsyncHandle; runtime.OnEndInvoke(asyncHandle, true, new JSAsyncCallResult(JsonDocument.Parse("{}"), JsonDocument.Parse("{\"Message\": \"Some data\"}").RootElement.GetProperty("Message"))); runtime.OnEndInvoke(asyncHandle, false, new JSAsyncCallResult(null, JsonDocument.Parse("{\"Message\": \"Exception\"}").RootElement.GetProperty("Message"))); var result = await task; Assert.Equal("Some data", result); }
public void CannotCompleteSameAsyncCallMoreThanOnce() { // Arrange var runtime = new TestJSRuntime(); // Act/Assert runtime.InvokeAsync <string>("test identifier", Array.Empty <object>()); var asyncHandle = runtime.BeginInvokeCalls[0].AsyncHandle; runtime.OnEndInvoke(asyncHandle, true, null); var ex = Assert.Throws <ArgumentException>(() => { // Second "end invoke" will fail runtime.OnEndInvoke(asyncHandle, true, null); }); Assert.Equal($"There is no pending task with handle '{asyncHandle}'.", ex.Message); }
public void CanCompleteAsyncCallsAsSuccess() { // Arrange var runtime = new TestJSRuntime(); // Act/Assert: Tasks not initially completed var unrelatedTask = runtime.InvokeAsync <string>("unrelated call", Array.Empty <object>()); var task = runtime.InvokeAsync <string>("test identifier", Array.Empty <object>()); Assert.False(unrelatedTask.IsCompleted); Assert.False(task.IsCompleted); // Act/Assert: Task can be completed runtime.OnEndInvoke( runtime.BeginInvokeCalls[1].AsyncHandle, /* succeeded: */ true, "my result"); Assert.False(unrelatedTask.IsCompleted); Assert.True(task.IsCompleted); Assert.Equal("my result", task.Result); }