Skip to content

Commit 16696d7

Browse files
committed
refactor: Simplified logic to close node server.
1 parent 10102a4 commit 16696d7

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
using SocketIOClient.Test.Helpers;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.IO;
5-
using System.Linq;
6-
using System.Threading;
74

85
namespace SocketIOClient.Test.SocketIOTests
96
{
107
public class BaseServerManager
118
{
129
private readonly string directory;
13-
private List<Process> processesToKill;
10+
private Process nodeProcess;
1411

1512
public BaseServerManager(string directory)
1613
{
@@ -19,32 +16,22 @@ public BaseServerManager(string directory)
1916

2017
public void Create()
2118
{
22-
var installProcess = CommandHelper.RunCommand("npm", "install", Path.GetFullPath(directory));
19+
var workingDirectory = Path.GetFullPath(directory);
20+
var npmInstallProcess = CommandHelper.RunCommand("npm", "install", workingDirectory);
2321

24-
if (!installProcess.WaitForExit(60000))
22+
// We wait until the installation process is finished (or we get a timeout).
23+
if (!npmInstallProcess.WaitForExit(60000))
2524
{
2625
throw new System.SystemException("Failed restoring packages of server");
2726
}
2827

29-
var before = Process.GetProcesses().ToList().Select(x => x.Id);
30-
var startProcess = CommandHelper.RunCommand("npm", "start", Path.GetFullPath(directory));
31-
var after = Process.GetProcesses().ToList();
32-
this.processesToKill = this.GetProcessesToKill(before, after);
28+
// Time to run the node server.
29+
this.nodeProcess = CommandHelper.RunCommand("node", "app.js", workingDirectory);
3330
}
3431

3532
public void Destroy()
3633
{
37-
foreach (var process in processesToKill)
38-
{
39-
process.Kill();
40-
}
41-
}
42-
43-
private List<Process> GetProcessesToKill(IEnumerable<int> before, List<Process> after)
44-
{
45-
var processesToKill = after;
46-
processesToKill.RemoveAll(x => before.Contains(x.Id) && !x.ProcessName.Contains("npm"));
47-
return processesToKill;
34+
nodeProcess?.Kill();
4835
}
4936
}
5037
}

0 commit comments

Comments
 (0)