Skip to content

LOC-1340 Fixed isRunning #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 2, 2022
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
34 changes: 34 additions & 0 deletions BrowserStackLocal/Backup/BrowserStackLocal.sln
Original file line number Diff line number Diff line change
@@ -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
54 changes: 43 additions & 11 deletions BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class BrowserStackTunnel : IDisposable
public int basePathsIndex = -1;
protected string binaryAbsolute = "";
protected string binaryArguments = "";

protected StringBuilder output;
public LocalState localState;
protected string logFilePath = "";
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
}
}
});
Expand All @@ -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()
{
Expand All @@ -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();
}
Expand Down
20 changes: 15 additions & 5 deletions BrowserStackLocal/BrowserStackLocal/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void addArgs(string key, string value)
}
}
}

public static string GetVersionString(string pVersionString)
{
string tVersion = "Unknown";
Expand Down Expand Up @@ -160,7 +161,7 @@ public void start(List<KeyValuePair<string, string>> 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+", "");
Expand All @@ -175,18 +176,27 @@ public void start(List<KeyValuePair<string, string>> options)
argumentString += "--source \"c-sharp:" + bindingVersion + "\" ";
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;
}
Expand Down