Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 45 additions & 0 deletions src/AngleSharp.Js.Tests/WorkerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace AngleSharp.Js.Tests
{
using AngleSharp.Browser;
using AngleSharp.Dom;
using AngleSharp.Io;
using AngleSharp.Js.Tests.Mocks;
using AngleSharp.Scripting;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

[TestFixture]
Expand Down Expand Up @@ -182,6 +185,48 @@ public async Task SharedWorkerFailingToStartIsAnErrorForEveryScriptCreatingIt()
Assert.AreEqual("a=error,b=error", document.ExecuteScript("log.sort().join(',')")?.ToString());
}

[TestCase(false)]
[TestCase(true)]
public async Task WorkerErrorIsDispatchedAsAnEngineTask(Boolean shared)
{
var document = await OpenWithSourcesAsync(("https://example.com/broken.js", "not valid javascript (")).ConfigureAwait(false);
var instance = document.Context.GetService<JsScriptingService>().GetOrCreateInstance(document);
var dispatched = new TaskCompletionSource<Boolean>();
var loopEntered = new TaskCompletionSource<Boolean>();
var releaseLoop = new TaskCompletionSource<Boolean>();
var loop = document.Context.GetService<IEventLoop>();
loop.Enqueue(_ =>
{
loopEntered.TrySetResult(true);
releaseLoop.Task.GetAwaiter().GetResult();
}, TaskPriority.Normal);

try
{
Assert.AreSame(loopEntered.Task, await Task.WhenAny(loopEntered.Task, Task.Delay(3000)).ConfigureAwait(false));

if (shared)
{
var worker = new AngleSharp.Js.Dom.SharedWorker(document.DefaultView, "broken.js");
worker.Failed += (_, _) => dispatched.TrySetResult(
EngineInstance.Current == instance && Monitor.IsEntered(instance.Jint));
}
else
{
var worker = new AngleSharp.Js.Dom.Worker(document.DefaultView, "broken.js");
worker.Failed += (_, _) => dispatched.TrySetResult(
EngineInstance.Current == instance && Monitor.IsEntered(instance.Jint));
}
}
finally
{
releaseLoop.TrySetResult(true);
}

Assert.AreSame(dispatched.Task, await Task.WhenAny(dispatched.Task, Task.Delay(3000)).ConfigureAwait(false));
Assert.IsTrue(await dispatched.Task.ConfigureAwait(false));
}

[Test]
public async Task SharedWorkerOfAnotherNameIsAnother()
{
Expand Down
10 changes: 8 additions & 2 deletions src/AngleSharp.Js/Dom/SharedWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,14 @@ private void Connect(Connection connection)
if (_startupError != null)
{
var worker = connection.Worker;
var loop = connection.Window.Document?.Context.GetService<IEventLoop>();
Worker.Enqueue(loop, TaskPriority.Normal, () =>
var document = connection.Window.Document;

if (document == null)
{
return;
}

MessageTaskQueue.Enqueue(document, () =>
{
var ev = new ErrorEvent();
ev.Init(_key.Item3, 0, 0, _startupError);
Expand Down
7 changes: 3 additions & 4 deletions src/AngleSharp.Js/Dom/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public sealed class Worker : EventTarget
private readonly IWindow _window;
private readonly IBrowsingContext _workerContext;
private readonly JsScriptingService _scripting;
private readonly IEventLoop _windowLoop;
private readonly IEventLoop _workerLoop;
private readonly Queue<SerializedMessage> _pendingMessages;

Expand Down Expand Up @@ -76,9 +75,9 @@ public Worker(IWindow window, String source, WorkerOptions options)
}

_window = window;
_windowLoop = _window.Document.Context.GetService<IEventLoop>();

var parentContext = _window.Document.Context;
var ownerDocument = _window.Document;
var parentContext = ownerDocument.Context;
_scripting = parentContext.GetService<JsScriptingService>() ?? throw new DomException(DomError.NotSupported);
EnsureLoaderAvailable();
var workerUrl = ResolveUrl(source);
Expand All @@ -100,7 +99,7 @@ public Worker(IWindow window, String source, WorkerOptions options)
catch (Exception ex)
{
_startupError = ex;
Enqueue(_windowLoop, TaskPriority.Normal, () => ReportStartupError(workerUrl, ex));
MessageTaskQueue.Enqueue(ownerDocument, () => ReportStartupError(workerUrl, ex));
}
});
}
Expand Down
Loading