Files
cefsharp/CefSharp.Test/Framework/ConcurrentMethodRunnerQueueFacts.cs
T
amaitland 25ab209380 MethodRunnerQueue/ConcurrentMethodRunnerQueue - Remove custom task execution queue
- ConcurrentMethodRunnerQueue we now directly queue tasks on TaskScheduler.Default
- MethodRunnerQueue now uses LimitedConcurrencyLevelTaskScheduler (example from Microsoft) to limit execution to one concurrent task
- MethodRunnerQueue tasks are now run on the ThreadPool directly instead of our own custom thread, should have some threading implications
for user
- IMethodRunnerQueue now implements IDisposable

Resolves #2873
2019-09-06 11:05:18 +10:00

76 lines
2.6 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.Enqueue(methodInvocation);
//Wait a litle for the queue to start processing our Method call
await Task.Delay(500);
var ex = Record.Exception(() => methodRunnerQueue.Dispose());
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.Dispose();
actualResult = args.Result.Result.ToString();
manualResetEvent.Set();
};
methodRunnerQueue.Enqueue(methodInvocation);
manualResetEvent.WaitOne(3000);
Assert.Equal(expectedResult, actualResult);
}
}
}