From 39ce3ed8783d4b9fed0f50d7fe8b0f078ac59d83 Mon Sep 17 00:00:00 2001 From: Dummy Date: Sun, 17 Mar 2019 12:58:39 +0100 Subject: [PATCH] fix for bugs all around. --- ...luffySpoon.Automation.Web.Puppeteer.csproj | 2 +- ...PuppeteerWebAutomationFrameworkInstance.cs | 6 +- .../Program.cs | 2 +- .../FluffySpoon.Automation.Web.Tests.csproj | 2 +- .../Fluent/Context/MethodChainContext.cs | 240 +++++++++--------- .../Fluent/Wait/WaitMethodChainNode.cs | 2 +- .../WebAutomationEngine.cs | 8 +- 7 files changed, 128 insertions(+), 134 deletions(-) diff --git a/src/FluffySpoon.Automation.Web.Puppeteer/FluffySpoon.Automation.Web.Puppeteer.csproj b/src/FluffySpoon.Automation.Web.Puppeteer/FluffySpoon.Automation.Web.Puppeteer.csproj index 6d976f0..61ff11e 100644 --- a/src/FluffySpoon.Automation.Web.Puppeteer/FluffySpoon.Automation.Web.Puppeteer.csproj +++ b/src/FluffySpoon.Automation.Web.Puppeteer/FluffySpoon.Automation.Web.Puppeteer.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/FluffySpoon.Automation.Web.Puppeteer/PuppeteerWebAutomationFrameworkInstance.cs b/src/FluffySpoon.Automation.Web.Puppeteer/PuppeteerWebAutomationFrameworkInstance.cs index febd1c3..1bc9933 100644 --- a/src/FluffySpoon.Automation.Web.Puppeteer/PuppeteerWebAutomationFrameworkInstance.cs +++ b/src/FluffySpoon.Automation.Web.Puppeteer/PuppeteerWebAutomationFrameworkInstance.cs @@ -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) diff --git a/src/FluffySpoon.Automation.Web.Sample/Program.cs b/src/FluffySpoon.Automation.Web.Sample/Program.cs index 1912f8b..1789ae4 100644 --- a/src/FluffySpoon.Automation.Web.Sample/Program.cs +++ b/src/FluffySpoon.Automation.Web.Sample/Program.cs @@ -24,7 +24,7 @@ static async Task Main(string[] args) serviceCollection.AddJQueryDomSelector(); serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetFirefoxDriverAsync); - serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync); + //serviceCollection.AddSeleniumWebAutomationFrameworkInstance(GetEdgeDriverAsync); serviceCollection.AddPuppeteerWebAutomationFrameworkInstance(GetPuppeteerDriverAsync); diff --git a/src/FluffySpoon.Automation.Web.Tests/FluffySpoon.Automation.Web.Tests.csproj b/src/FluffySpoon.Automation.Web.Tests/FluffySpoon.Automation.Web.Tests.csproj index cc9d944..13aff82 100644 --- a/src/FluffySpoon.Automation.Web.Tests/FluffySpoon.Automation.Web.Tests.csproj +++ b/src/FluffySpoon.Automation.Web.Tests/FluffySpoon.Automation.Web.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/FluffySpoon.Automation.Web/Fluent/Context/MethodChainContext.cs b/src/FluffySpoon.Automation.Web/Fluent/Context/MethodChainContext.cs index 761a275..fb35437 100644 --- a/src/FluffySpoon.Automation.Web/Fluent/Context/MethodChainContext.cs +++ b/src/FluffySpoon.Automation.Web/Fluent/Context/MethodChainContext.cs @@ -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 AllNodes { get; } - public Queue PendingNodesToRun { get; } - - public MethodChain() - { - AllNodes = new LinkedList(); - PendingNodesToRun = new Queue(); - } - } - - 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 AllNodes { get; } + public Queue PendingNodesToRun { get; } + + public MethodChain() + { + AllNodes = new LinkedList(); + PendingNodesToRun = new Queue(); + } + } + + private Task _cachedRunAllTask; + private readonly SemaphoreSlim _runNextSemaphore; - private readonly SemaphoreSlim _runAllSemaphore; - + private readonly SemaphoreSlim _runAllSemaphore; + private readonly IDictionary _userAgentMethodChainQueue; public IWebAutomationEngine AutomationEngine { get; private set; } @@ -35,29 +35,29 @@ public IEnumerable Frameworks { get; private set; - } - - public int NodeCount { get; private set; } - - public MethodChainContext( - IEnumerable frameworks, - IWebAutomationEngine automationEngine) - { - _userAgentMethodChainQueue = new Dictionary(); - - _runNextSemaphore = new SemaphoreSlim(1); - _runAllSemaphore = new SemaphoreSlim(1); - + } + + public int NodeCount { get; private set; } + + public MethodChainContext( + IEnumerable frameworks, + IWebAutomationEngine automationEngine) + { + _userAgentMethodChainQueue = new Dictionary(); + + _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(); @@ -77,7 +77,7 @@ public async Task RunAllAsync() finally { _runAllSemaphore.Release(1); - } + } } private async Task ExecuteRunAllAsync() @@ -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(); @@ -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 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 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; @@ -160,24 +158,24 @@ public TMethodChainNode Enqueue(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) @@ -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(); + } + } } \ No newline at end of file diff --git a/src/FluffySpoon.Automation.Web/Fluent/Wait/WaitMethodChainNode.cs b/src/FluffySpoon.Automation.Web/Fluent/Wait/WaitMethodChainNode.cs index 0d40d4c..96249b6 100644 --- a/src/FluffySpoon.Automation.Web/Fluent/Wait/WaitMethodChainNode.cs +++ b/src/FluffySpoon.Automation.Web/Fluent/Wait/WaitMethodChainNode.cs @@ -18,7 +18,7 @@ public WaitMethodChainNode(Func> predicate) protected override async Task OnExecuteAsync(IWebAutomationFrameworkInstance framework) { while (!await _predicate()) - await Task.Delay(1); + await Task.Delay(100); await base.OnExecuteAsync(framework); } diff --git a/src/FluffySpoon.Automation.Web/WebAutomationEngine.cs b/src/FluffySpoon.Automation.Web/WebAutomationEngine.cs index 6af56ea..395ca6c 100644 --- a/src/FluffySpoon.Automation.Web/WebAutomationEngine.cs +++ b/src/FluffySpoon.Automation.Web/WebAutomationEngine.cs @@ -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( @@ -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); }