Skip to content

Commit 07d5fa0

Browse files
committed
feat: Add logic to run the different nodejs servers before the tests. To avoid making a lot of changes in the tests, the logic is performed by using AssemblyInitialize and the AssemblyCleanup instead of TestInitialize and TestCleanup (but could be done if desired). In the future, the tests could be separated in different projects (e.g. UnitTests and IntegrationTests), so, doing the configuration of the different servers should be done only by running the integration tests.
1 parent 34fbd1a commit 07d5fa0

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
5+
namespace SocketIOClient.Test.Helpers
6+
{
7+
public static class CommandHelper
8+
{
9+
public static Process RunCommand(string fileName, string argument, string workingDirectory = null)
10+
{
11+
if (string.IsNullOrEmpty(workingDirectory))
12+
{
13+
workingDirectory = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
14+
}
15+
16+
var processStartInfo = new ProcessStartInfo()
17+
{
18+
FileName = fileName,
19+
Arguments = argument,
20+
WorkingDirectory = workingDirectory,
21+
UseShellExecute = true,
22+
WindowStyle = ProcessWindowStyle.Minimized,
23+
};
24+
25+
var process = Process.Start(processStartInfo);
26+
27+
if (process == null)
28+
{
29+
throw new Exception("Process should not be null.");
30+
}
31+
32+
return process;
33+
}
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SocketIOClient.Test.SocketIOTests.V4;
3+
using System.Collections.Generic;
4+
using System.Threading;
5+
6+
namespace SocketIOClient.Test.SocketIOTests
7+
{
8+
[TestClass]
9+
10+
public class AssemblyIntegrationTest
11+
{
12+
private static readonly IEnumerable<IServerManager> Servers = new List<IServerManager>()
13+
{
14+
new ServerV2Manager(),
15+
new ServerV3Manager(),
16+
new ServerV4Manager()
17+
};
18+
19+
[AssemblyInitialize]
20+
public static void Initialize(TestContext context)
21+
{
22+
foreach (var server in Servers)
23+
{
24+
server.Create();
25+
}
26+
27+
// Give some time to be sure that servers are running.
28+
Thread.Sleep(5000);
29+
}
30+
31+
[AssemblyCleanup]
32+
public static void Cleanup()
33+
{
34+
foreach (var server in Servers)
35+
{
36+
server.Destroy();
37+
}
38+
}
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using SocketIOClient.Test.Helpers;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Threading;
7+
8+
namespace SocketIOClient.Test.SocketIOTests
9+
{
10+
public class BaseServerManager
11+
{
12+
private readonly string directory;
13+
private List<Process> processesToKill;
14+
15+
public BaseServerManager(string directory)
16+
{
17+
this.directory = directory;
18+
}
19+
20+
public void Create()
21+
{
22+
var installProcess = CommandHelper.RunCommand("npm", "install", Path.GetFullPath(directory));
23+
24+
if (!installProcess.WaitForExit(60000))
25+
{
26+
throw new System.SystemException("Failed restoring packages of server");
27+
}
28+
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);
33+
}
34+
35+
public void Destroy()
36+
{
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;
48+
}
49+
}
50+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SocketIOClient.Test.SocketIOTests
2+
{
3+
public interface IServerManager
4+
{
5+
public void Create();
6+
public void Destroy();
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SocketIOClient.Test.SocketIOTests.V4
2+
{
3+
public class ServerV2Manager : BaseServerManager, IServerManager
4+
{
5+
public ServerV2Manager()
6+
: base(@"..\..\..\..\socket.io-server-v2")
7+
{
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SocketIOClient.Test.SocketIOTests.V4
2+
{
3+
public class ServerV3Manager : BaseServerManager, IServerManager
4+
{
5+
public ServerV3Manager()
6+
: base(@"..\..\..\..\socket.io-server-v3")
7+
{
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SocketIOClient.Test.SocketIOTests.V4
2+
{
3+
public class ServerV4Manager : BaseServerManager, IServerManager
4+
{
5+
public ServerV4Manager()
6+
: base(@"..\..\..\..\socket.io-server-v4")
7+
{
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)