Skip to content

Commit 7bf450e

Browse files
authored
Fix docker based integration tests to be more robust (#74)
* Fix docker based integration tests to be more robust * Make docker based integration tests manual
1 parent 729c8b5 commit 7bf450e

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

tests/mcpdotnet.Tests/EverythingSseServerFixture.cs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@ namespace McpDotNet.Tests;
44

55
public class EverythingSseServerFixture : IAsyncDisposable
66
{
7-
private Process? _process;
7+
private int _port;
8+
private string _containerName;
9+
10+
public EverythingSseServerFixture(int port)
11+
{
12+
_port = port;
13+
_containerName = $"mcp-everything-server-{_port}";
14+
}
815

916
public async Task StartAsync()
1017
{
1118
var processStartInfo = new ProcessStartInfo
1219
{
1320
FileName = "docker",
14-
Arguments = "run -p 3001:3001 --rm tzolov/mcp-everything-server:v1",
21+
Arguments = $"run -p {_port}:3001 --name {_containerName} --rm tzolov/mcp-everything-server:v1",
1522
RedirectStandardInput = true,
1623
RedirectStandardOutput = true,
1724
RedirectStandardError = true,
1825
UseShellExecute = false,
1926
};
2027

21-
_process = Process.Start(processStartInfo)
28+
_ = Process.Start(processStartInfo)
2229
?? throw new InvalidOperationException($"Could not start process for {processStartInfo.FileName} with '{processStartInfo.Arguments}'.");
2330

2431
// Wait for the server to start
@@ -28,34 +35,18 @@ public async ValueTask DisposeAsync()
2835
{
2936
try
3037
{
31-
// Find the container ID
32-
var psInfo = new ProcessStartInfo
38+
39+
// Stop the container
40+
var stopInfo = new ProcessStartInfo
3341
{
3442
FileName = "docker",
35-
Arguments = "ps -q --filter ancestor=tzolov/mcp-everything-server:v1",
36-
RedirectStandardOutput = true,
43+
Arguments = $"stop {_containerName}",
3744
UseShellExecute = false
3845
};
3946

40-
using var psProcess = Process.Start(psInfo)
41-
?? throw new InvalidOperationException($"Could not start process for {psInfo.FileName} with '{psInfo.Arguments}'.");
42-
string containerId = await psProcess.StandardOutput.ReadToEndAsync();
43-
containerId = containerId.Trim();
44-
45-
if (!string.IsNullOrEmpty(containerId))
46-
{
47-
// Stop the container
48-
var stopInfo = new ProcessStartInfo
49-
{
50-
FileName = "docker",
51-
Arguments = $"stop {containerId}",
52-
UseShellExecute = false
53-
};
54-
55-
using var stopProcess = Process.Start(stopInfo)
56-
?? throw new InvalidOperationException($"Could not start process for {stopInfo.FileName} with '{stopInfo.Arguments}'.");
57-
await stopProcess.WaitForExitAsync();
58-
}
47+
using var stopProcess = Process.Start(stopInfo)
48+
?? throw new InvalidOperationException($"Could not stop process for {stopInfo.FileName} with '{stopInfo.Arguments}'.");
49+
await stopProcess.WaitForExitAsync();
5950
}
6051
catch (Exception ex)
6152
{

tests/mcpdotnet.Tests/SseIntegrationTests.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ public async Task ConnectAndReceiveMessage_InMemoryServer()
5656
}
5757

5858
[Fact]
59+
[Trait("Execution", "Manual")]
5960
public async Task ConnectAndReceiveMessage_EverythingServerWithSse()
6061
{
6162
using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
6263
builder.AddConsole()
6364
.SetMinimumLevel(LogLevel.Debug));
6465

65-
await using var fixture = new EverythingSseServerFixture();
66+
int port = 3001;
67+
68+
await using var fixture = new EverythingSseServerFixture(port);
6669
await fixture.StartAsync();
6770

6871
var defaultOptions = new McpClientOptions
@@ -76,7 +79,7 @@ public async Task ConnectAndReceiveMessage_EverythingServerWithSse()
7679
Name = "Everything",
7780
TransportType = TransportTypes.Sse,
7881
TransportOptions = [],
79-
Location = "http://localhost:3001/sse"
82+
Location = $"http://localhost:{port}/sse"
8083
};
8184

8285
using var factory = new McpClientFactory(
@@ -94,14 +97,17 @@ public async Task ConnectAndReceiveMessage_EverythingServerWithSse()
9497
}
9598

9699
[Fact]
100+
[Trait("Execution", "Manual")]
97101
public async Task Sampling_Sse_EverythingServer()
98102
{
99103
// arrange
100104
using var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder =>
101105
builder.AddConsole()
102106
.SetMinimumLevel(LogLevel.Debug));
103107

104-
await using var fixture = new EverythingSseServerFixture();
108+
int port = 3002;
109+
110+
await using var fixture = new EverythingSseServerFixture(port);
105111
await fixture.StartAsync();
106112

107113
var defaultOptions = new McpClientOptions
@@ -123,7 +129,7 @@ public async Task Sampling_Sse_EverythingServer()
123129
Name = "Everything",
124130
TransportType = TransportTypes.Sse,
125131
TransportOptions = [],
126-
Location = "http://localhost:3001/sse"
132+
Location = $"http://localhost:{port}/sse"
127133
};
128134

129135
using var factory = new McpClientFactory(

0 commit comments

Comments
 (0)