From ae60cae65323433ee16bab0399eea1f58d143810 Mon Sep 17 00:00:00 2001 From: "Edwin Clement (Windows - Adam)" Date: Thu, 7 Jul 2022 04:29:05 +0530 Subject: [PATCH 1/2] Fixed isRunning --- .../Backup/BrowserStackLocal.sln | 34 ++++++++++++ .../BrowserStackLocal/BrowserStackTunnel.cs | 54 +++++++++++++++---- BrowserStackLocal/BrowserStackLocal/Local.cs | 21 +++++--- .../BrowserStackExample.csproj | 6 ++- .../BrowserStackExample/app.config | 3 ++ 5 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 BrowserStackLocal/Backup/BrowserStackLocal.sln create mode 100644 BrowserStackLocalExample/BrowserStackExample/app.config diff --git a/BrowserStackLocal/Backup/BrowserStackLocal.sln b/BrowserStackLocal/Backup/BrowserStackLocal.sln new file mode 100644 index 0000000..ead4ad6 --- /dev/null +++ b/BrowserStackLocal/Backup/BrowserStackLocal.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal", "BrowserStackLocal\BrowserStackLocal.csproj", "{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal Unit Tests", "BrowserStackLocal Unit Tests\BrowserStackLocal Unit Tests.csproj", "{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocalIntegrationTests", "BrowserStackLocalIntegrationTests\BrowserStackLocalIntegrationTests.csproj", "{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.Build.0 = Release|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.Build.0 = Release|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 4d985f1..1ef8070 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -24,7 +24,7 @@ public class BrowserStackTunnel : IDisposable int basePathsIndex = -1; protected string binaryAbsolute = ""; protected string binaryArguments = ""; - + protected StringBuilder output; public LocalState localState; protected string logFilePath = ""; @@ -113,7 +113,7 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str { File.WriteAllText(logFilePath, string.Empty); } - RunProcess(arguments, processType); + RunProcess(arguments, processType); } private void RunProcess(string arguments, string processType) @@ -138,14 +138,40 @@ private void RunProcess(string arguments, string processType) if (e.Data != null) { JObject binaryOutput = null; - try { + try + { binaryOutput = JObject.Parse(e.Data); - } catch (Exception) { + } + catch (Exception) + { + SetTunnelState(LocalState.Error); throw new Exception($"Error while parsing JSON {e.Data}"); } - if(binaryOutput.GetValue("state") != null && !binaryOutput.GetValue("state").ToString().ToLower().Equals("connected")) + + JToken connectionState = binaryOutput.GetValue("state"); + if (connectionState != null) + { + if (connectionState.ToString().ToLower().Equals("connected")) + { + SetTunnelState(LocalState.Connected); + } + else if (connectionState.ToString().ToLower().Equals("disconnected")) + { + SetTunnelState(LocalState.Disconnected); + } + else + { + SetTunnelState(LocalState.Error); + throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data); + } + } + else { - throw new Exception("Eror while executing BrowserStackLocal " + processType + " " + e.Data); + JToken message = binaryOutput.GetValue("message"); + if (message != null && message.Type == JTokenType.String && message.ToString() == "BrowserStackLocal stopped successfully") + { + SetTunnelState(LocalState.Disconnected); + } } } }); @@ -162,13 +188,19 @@ private void RunProcess(string arguments, string processType) process.BeginOutputReadLine(); process.BeginErrorReadLine(); - TunnelStateChanged(LocalState.Idle, LocalState.Connecting); - AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => Kill()); + SetTunnelState(LocalState.Connecting); + AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => + { + Kill(); + }); process.WaitForExit(); } - private void TunnelStateChanged(LocalState prevState, LocalState state) { } + private void SetTunnelState(LocalState newState) + { + localState = newState; + } public bool IsConnected() { @@ -182,13 +214,13 @@ public void Kill() process.Close(); process.Kill(); process = null; - localState = LocalState.Disconnected; + SetTunnelState(LocalState.Disconnected); } } public void Dispose() { - if(process != null) + if (process != null) { Kill(); } diff --git a/BrowserStackLocal/BrowserStackLocal/Local.cs b/BrowserStackLocal/BrowserStackLocal/Local.cs index 26155bf..d20f6c0 100644 --- a/BrowserStackLocal/BrowserStackLocal/Local.cs +++ b/BrowserStackLocal/BrowserStackLocal/Local.cs @@ -92,7 +92,7 @@ private void addArgs(string key, string value) } } } - + public Local() { tunnel = new BrowserStackTunnel(); @@ -111,7 +111,7 @@ public void start(List> options) accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); if (accessKey == null || accessKey.Trim().Length == 0) { - throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. "+ + throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " + "Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY."); } Regex.Replace(this.accessKey, @"\s+", ""); @@ -125,18 +125,27 @@ public void start(List> options) argumentString += "-logFile \"" + customLogPath + "\" "; tunnel.addBinaryPath(customBinaryPath); tunnel.addBinaryArguments(argumentString); - while (true) { + while (true) + { bool except = false; - try { + try + { tunnel.Run(accessKey, folder, customLogPath, "start"); - } catch (Exception) + } + catch (System.ComponentModel.Win32Exception) { except = true; } + catch (Exception e) + { + except = true; + Console.WriteLine(e.ToString()); + } if (except) { tunnel.fallbackPaths(); - } else + } + else { break; } diff --git a/BrowserStackLocalExample/BrowserStackExample/BrowserStackExample.csproj b/BrowserStackLocalExample/BrowserStackExample/BrowserStackExample.csproj index f4bb522..b994146 100644 --- a/BrowserStackLocalExample/BrowserStackExample/BrowserStackExample.csproj +++ b/BrowserStackLocalExample/BrowserStackExample/BrowserStackExample.csproj @@ -9,7 +9,7 @@ Properties BrowserStackExample BrowserStackExample - v4.5 + v4.8 512 publish\ true @@ -26,6 +26,7 @@ false false true + AnyCPU @@ -83,6 +84,9 @@ + + +