Files
cefsharp/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs
T
Alex Maitland fe98124c9b Async JavascriptBinding - Add support for returning Task (#2771)
* Async JavascriptBinding - Add support for returning Task

Remove Parallel Task execution code from MethodRunnerQueue
Add ParallelMethodRunnerQueue
  - For the new code path the Func is executed, if the Result is a generic Task we wait for it's completion
  - If the task is not generic then we return immediately

This is only enabled when CefSharpSettings.ConcurrentTaskExecution = true;
* MethodRunnerQueue - Display error message when method returns Task
* ParallelMethodRunnerQueue - Rename to ConcurrentMethodRunnerQueue
* MethodRunnerQueue - Improve formatting of error message when method returns Task
* Async JavascriptBinding Return Task - Add Basic xUnit Test

Very quick test, needs to be refined a little. The basics are working as expected.

* Add Binding Test Async Task Example to the WPF Example menu

* Async JSB Task - Add additional tests and improve error messaging
2019-06-08 17:21:49 +10:00

78 lines
2.7 KiB
C#

// Copyright © 2019 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System.Threading;
using System.Threading.Tasks;
using CefSharp.Example.JavascriptBinding;
using CefSharp.Internals;
using Xunit;
using Xunit.Abstractions;
namespace CefSharp.Test.Framework
{
public class ConcurrentMethodRunnerQueueFacts
{
private readonly ITestOutputHelper output;
public ConcurrentMethodRunnerQueueFacts(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public async Task StopConcurrentMethodRunnerQueueWhenMethodRunning()
{
var boundObject = new AsyncBoundObject();
var objectRepository = new JavascriptObjectRepository();
objectRepository.Register("testObject", boundObject, true, new BindingOptions { CamelCaseJavascriptNames = false });
var methodInvocation = new MethodInvocation(1, 1, 1, nameof(boundObject.AsyncWaitTwoSeconds), 1);
methodInvocation.Parameters.Add("Echo Me!");
var methodRunnerQueue = new ConcurrentMethodRunnerQueue(objectRepository);
methodRunnerQueue.Start();
methodRunnerQueue.Enqueue(methodInvocation);
//Wait a litle for the queue to start processing our Method call
await Task.Delay(500);
var ex = Record.Exception(() => methodRunnerQueue.Stop());
Assert.Null(ex);
}
[Fact]
public void ValidateAsyncTaskMethodOutput()
{
const string expectedResult = "Echo Me!";
var boundObject = new AsyncBoundObject();
var objectRepository = new JavascriptObjectRepository();
objectRepository.Register("testObject", boundObject, true, new BindingOptions { CamelCaseJavascriptNames = false });
var methodInvocation = new MethodInvocation(1, 1, 1, nameof(boundObject.AsyncWaitTwoSeconds), 1);
methodInvocation.Parameters.Add(expectedResult);
var methodRunnerQueue = new ConcurrentMethodRunnerQueue(objectRepository);
var manualResetEvent = new ManualResetEvent(false);
var actualResult = "";
methodRunnerQueue.MethodInvocationComplete += (sender, args) =>
{
methodRunnerQueue.Stop();
actualResult = args.Result.Result.ToString();
manualResetEvent.Set();
};
methodRunnerQueue.Start();
methodRunnerQueue.Enqueue(methodInvocation);
manualResetEvent.WaitOne(3000);
Assert.Equal(expectedResult, actualResult);
}
}
}