|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace PuppeteerSharp.Tests |
| 8 | +{ |
| 9 | + public class PuppeteerLoaderFixture : IDisposable |
| 10 | + { |
| 11 | + Process _webServerProcess = null; |
| 12 | + |
| 13 | + public PuppeteerLoaderFixture() |
| 14 | + { |
| 15 | + SetupAsync().GetAwaiter().GetResult(); |
| 16 | + } |
| 17 | + |
| 18 | + public void Dispose() |
| 19 | + { |
| 20 | + int processId = 0; |
| 21 | + |
| 22 | + try |
| 23 | + { |
| 24 | + processId = _webServerProcess.Id; |
| 25 | + } |
| 26 | + catch (InvalidOperationException) |
| 27 | + { |
| 28 | + //We might get an InvalidOperationException if the process is in an invalid state |
| 29 | + } |
| 30 | + catch (Exception ex) |
| 31 | + { |
| 32 | + throw new Exception("Unable to kill webserver", ex); |
| 33 | + } |
| 34 | + |
| 35 | + if (processId != 0 && Process.GetProcessById(processId) != null) |
| 36 | + { |
| 37 | + _webServerProcess.Kill(); |
| 38 | + } |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + private async Task SetupAsync() |
| 43 | + { |
| 44 | + var downloaderTask = Downloader.CreateDefault().DownloadRevisionAsync(TestConstants.ChromiumRevision); |
| 45 | + var serverTask = StartWebServerAsync(); |
| 46 | + |
| 47 | + await Task.WhenAll(downloaderTask, serverTask); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + private async Task StartWebServerAsync() |
| 52 | + { |
| 53 | + var taskWrapper = new TaskCompletionSource<bool>(); |
| 54 | + const int timeout = 2000; |
| 55 | + |
| 56 | + var build = Directory.GetCurrentDirectory().Contains("Debug") ? "Debug" : "Release"; |
| 57 | + var webServerPath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", |
| 58 | + "PuppeteerSharp.TestServer"); |
| 59 | + |
| 60 | + _webServerProcess = new Process(); |
| 61 | + _webServerProcess.StartInfo.FileName = "dotnet"; |
| 62 | + _webServerProcess.StartInfo.WorkingDirectory = webServerPath; |
| 63 | + _webServerProcess.StartInfo.Arguments = $"./bin/{build}/netcoreapp2.0/PuppeteerSharp.TestServer.dll"; |
| 64 | + |
| 65 | + _webServerProcess.StartInfo.RedirectStandardOutput = true; |
| 66 | + _webServerProcess.StartInfo.RedirectStandardError = true; |
| 67 | + |
| 68 | + _webServerProcess.OutputDataReceived += (sender, e) => |
| 69 | + { |
| 70 | + Console.WriteLine(e.Data); |
| 71 | + if (e.Data != null && |
| 72 | + taskWrapper.Task.Status != TaskStatus.RanToCompletion && |
| 73 | + //Though this is not bulletproof for the purpose of local testing |
| 74 | + //We assume that if the address is already in use is because we have another |
| 75 | + //process hosting the site |
| 76 | + (e.Data.Contains("Now listening on") || e.Data.Contains("ADDRINUSE"))) |
| 77 | + { |
| 78 | + taskWrapper.SetResult(true); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + _webServerProcess.Exited += (sender, e) => |
| 83 | + { |
| 84 | + taskWrapper.SetException(new Exception("Unable to start web server")); |
| 85 | + }; |
| 86 | + |
| 87 | + Timer timer = null; |
| 88 | + //We have to declare timer before initializing it because if we don't do this |
| 89 | + //we can't dispose it in the action created in the constructor |
| 90 | + timer = new Timer((state) => |
| 91 | + { |
| 92 | + if (taskWrapper.Task.Status != TaskStatus.RanToCompletion) |
| 93 | + { |
| 94 | + taskWrapper.SetException( |
| 95 | + new Exception($"Timed out after {timeout} ms while trying to connect to WebServer! ")); |
| 96 | + } |
| 97 | + timer.Dispose(); |
| 98 | + }, null, timeout, 0); |
| 99 | + |
| 100 | + _webServerProcess.Start(); |
| 101 | + _webServerProcess.BeginOutputReadLine(); |
| 102 | + |
| 103 | + await taskWrapper.Task; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments