Skip to content

Commit

Permalink
fix for bugs all around.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dummy committed Mar 17, 2019
1 parent 46a9daf commit 39ce3ed
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PuppeteerSharp" Version="1.11.2" />
<PackageReference Include="PuppeteerSharp" Version="1.12.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ public async Task InitializeAsync()
var pages = await _browser.PagesAsync();
_page = pages.Single();

_page.Request += PageRequest;
await _page.SetCacheEnabledAsync(false);

_page.Request += PageRequest;
_page.RequestFinished += PageRequestFinished;
_page.RequestFailed += PageRequestFinished;

await _page.SetCacheEnabledAsync(false);
}

private void PageRequestFinished(object sender, RequestEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion src/FluffySpoon.Automation.Web.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static async Task Main(string[] args)
serviceCollection.AddJQueryDomSelector();

serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync);
serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync);
//serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync);

serviceCollection.AddPuppeteerWebAutomationFrameworkInstance(GetPuppeteerDriverAsync);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>
Expand Down
240 changes: 119 additions & 121 deletions src/FluffySpoon.Automation.Web/Fluent/Context/MethodChainContext.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using FluffySpoon.Automation.Web.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Fluent.Context
{
class MethodChainContext : IMethodChainContext
{
private class MethodChain
{
public LinkedList<IBaseMethodChainNode> AllNodes { get; }
public Queue<IBaseMethodChainNode> PendingNodesToRun { get; }

public MethodChain()
{
AllNodes = new LinkedList<IBaseMethodChainNode>();
PendingNodesToRun = new Queue<IBaseMethodChainNode>();
}
}

private Task _cachedRunAllTask;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace FluffySpoon.Automation.Web.Fluent.Context
{
class MethodChainContext : IMethodChainContext
{
private class MethodChain
{
public LinkedList<IBaseMethodChainNode> AllNodes { get; }
public Queue<IBaseMethodChainNode> PendingNodesToRun { get; }

public MethodChain()
{
AllNodes = new LinkedList<IBaseMethodChainNode>();
PendingNodesToRun = new Queue<IBaseMethodChainNode>();
}
}

private Task _cachedRunAllTask;

private readonly SemaphoreSlim _runNextSemaphore;
private readonly SemaphoreSlim _runAllSemaphore;

private readonly SemaphoreSlim _runAllSemaphore;

private readonly IDictionary<IWebAutomationFrameworkInstance, MethodChain> _userAgentMethodChainQueue;

public IWebAutomationEngine AutomationEngine { get; private set; }
Expand All @@ -35,29 +35,29 @@ public IEnumerable<IWebAutomationFrameworkInstance> Frameworks
{
get;
private set;
}

public int NodeCount { get; private set; }

public MethodChainContext(
IEnumerable<IWebAutomationFrameworkInstance> frameworks,
IWebAutomationEngine automationEngine)
{
_userAgentMethodChainQueue = new Dictionary<IWebAutomationFrameworkInstance, MethodChain>();

_runNextSemaphore = new SemaphoreSlim(1);
_runAllSemaphore = new SemaphoreSlim(1);

}

public int NodeCount { get; private set; }

public MethodChainContext(
IEnumerable<IWebAutomationFrameworkInstance> frameworks,
IWebAutomationEngine automationEngine)
{
_userAgentMethodChainQueue = new Dictionary<IWebAutomationFrameworkInstance, MethodChain>();

_runNextSemaphore = new SemaphoreSlim(1);
_runAllSemaphore = new SemaphoreSlim(1);

Frameworks = frameworks;
AutomationEngine = automationEngine;

foreach (var framework in Frameworks)
{
_userAgentMethodChainQueue.Add(framework, new MethodChain());
}
}

public async Task RunAllAsync()
AutomationEngine = automationEngine;

foreach (var framework in Frameworks)
{
_userAgentMethodChainQueue.Add(framework, new MethodChain());
}
}

public async Task RunAllAsync()
{
await _runAllSemaphore.WaitAsync();

Expand All @@ -77,7 +77,7 @@ public async Task RunAllAsync()
finally
{
_runAllSemaphore.Release(1);
}
}
}

private async Task ExecuteRunAllAsync()
Expand All @@ -86,16 +86,16 @@ private async Task ExecuteRunAllAsync()
await RunNextAsync();
}

public async Task RunNextAsync()
{
try
{
await _runNextSemaphore.WaitAsync();

try
{
if (NodeCount > 0)
{
public async Task RunNextAsync()
{
try
{
await _runNextSemaphore.WaitAsync();

try
{
if (NodeCount > 0)
{
NodeCount--;

var tasks = new List<Task>();
Expand All @@ -106,45 +106,43 @@ public async Task RunNextAsync()
var next = methodChainQueue.PendingNodesToRun.Dequeue();
var nextNext = methodChainQueue.PendingNodesToRun.Count > 0 ?
methodChainQueue.PendingNodesToRun.Peek() :
null;

tasks.Add(Task.Factory.StartNew(
async () => await next.ExecuteAsync(framework),
TaskCreationOptions.LongRunning));
}

await Task.WhenAll(tasks);
}

}
catch (AggregateException ex)
null;

tasks.Add(next.ExecuteAsync(framework));
}

await Task.WhenAll(tasks);
}

}
catch (AggregateException ex)
{
if (ex.InnerExceptions.Count == 1)
throw GetExceptionToThrow(ex.InnerExceptions.Single());

throw GetExceptionToThrow(ex);
}
catch (Exception ex)
{
throw GetExceptionToThrow(ex);
}
}
finally
{
_runNextSemaphore.Release(1);
}
}

public TMethodChainNode Enqueue<TMethodChainNode>(TMethodChainNode node) where TMethodChainNode : IBaseMethodChainNode
{
try
{
_runNextSemaphore.Wait();

node.MethodChainContext = this;

var isQueueEmpty = NodeCount == 0;
foreach (var framework in Frameworks)
throw GetExceptionToThrow(ex.InnerExceptions.Single());

throw GetExceptionToThrow(ex);
}
catch (Exception ex)
{
throw GetExceptionToThrow(ex);
}
}
finally
{
_runNextSemaphore.Release(1);
}
}

public TMethodChainNode Enqueue<TMethodChainNode>(TMethodChainNode node) where TMethodChainNode : IBaseMethodChainNode
{
try
{
_runNextSemaphore.Wait();

node.MethodChainContext = this;

var isQueueEmpty = NodeCount == 0;
foreach (var framework in Frameworks)
{
var methodChainQueue = _userAgentMethodChainQueue[framework];
var allNodes = methodChainQueue.AllNodes;
Expand All @@ -160,24 +158,24 @@ public TMethodChainNode Enqueue<TMethodChainNode>(TMethodChainNode node) where T
node.SetParent(parentNode);
}

methodChainQueue
.PendingNodesToRun
.Enqueue(newNode);
}

NodeCount++;

return node;
}
finally
{
_runNextSemaphore.Release(1);
}
}

methodChainQueue
.PendingNodesToRun
.Enqueue(newNode);
}

NodeCount++;

return node;
}
finally
{
_runNextSemaphore.Release(1);
}
}

private Exception GetExceptionToThrow(Exception exception)
{
if (exception == null)
if (exception == null)
return null;

if (exception is ExpectationNotMetException ex)
Expand All @@ -186,12 +184,12 @@ private Exception GetExceptionToThrow(Exception exception)
return new ApplicationException(
"An error occured while performing one of the automation operations.",
exception);
}

public TaskAwaiter GetAwaiter()
{
return RunAllAsync()
.GetAwaiter();
}
}
}

public TaskAwaiter GetAwaiter()
{
return RunAllAsync()
.GetAwaiter();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public WaitMethodChainNode(Func<Task<bool>> predicate)
protected override async Task OnExecuteAsync(IWebAutomationFrameworkInstance framework)
{
while (!await _predicate())
await Task.Delay(1);
await Task.Delay(100);

await base.OnExecuteAsync(framework);
}
Expand Down
8 changes: 2 additions & 6 deletions src/FluffySpoon.Automation.Web/WebAutomationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public async Task InitializeAsync()
_isInitializing = true;

var tasks = _frameworks
.Select(x => Task.Factory.StartNew(
async () => await x.InitializeAsync(),
TaskCreationOptions.LongRunning))
.Select(x => x.InitializeAsync())
.ToArray();

await Task.WhenAll(
Expand Down Expand Up @@ -116,9 +114,7 @@ private IMethodChainContext CreateNewQueue()
public void Dispose()
{
var tasks = _frameworks
.Select(x => Task.Factory.StartNew(
async () => await x.DisposeAsync(),
TaskCreationOptions.LongRunning))
.Select(x => x.DisposeAsync())
.ToArray();
Task.WaitAll(tasks);
}
Expand Down

0 comments on commit 39ce3ed

Please sign in to comment.