Skip to content

Commit 39ab6cd

Browse files
Separate ASP.NET Core tests into a distinct project and make the main test project netfx compatible. (#254)
* Separate ASP.NET Core tests into a distinct project and make the main test project netfx compatible. * Fix merge conflicts. * Update tests/ModelContextProtocol.Tests/ModelContextProtocol.Tests.csproj Co-authored-by: Stephen Halter <halter73@gmail.com> * Revert "Fix merge conflicts." This reverts commit 852169b. * Fix merge conflicts --------- Co-authored-by: Stephen Halter <halter73@gmail.com>
1 parent 08f9cdb commit 39ab6cd

32 files changed

+324
-63
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@
6666
<PackageVersion Include="System.Linq.AsyncEnumerable" Version="$(System10Version)" />
6767
<PackageVersion Include="xunit.v3" Version="2.0.1" />
6868
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.2" />
69+
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
6970
</ItemGroup>
7071
</Project>

ModelContextProtocol.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EverythingServer", "samples
5454
EndProject
5555
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelContextProtocol.AspNetCore", "src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj", "{37B6A5E0-9995-497D-8B43-3BC6870CC716}"
5656
EndProject
57+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelContextProtocol.AspNetCore.Tests", "tests\ModelContextProtocol.AspNetCore.Tests\ModelContextProtocol.AspNetCore.Tests.csproj", "{85557BA6-3D29-4C95-A646-2A972B1C2F25}"
58+
EndProject
5759
Global
5860
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5961
Debug|Any CPU = Debug|Any CPU
@@ -104,6 +106,10 @@ Global
104106
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Debug|Any CPU.Build.0 = Debug|Any CPU
105107
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Release|Any CPU.ActiveCfg = Release|Any CPU
106108
{37B6A5E0-9995-497D-8B43-3BC6870CC716}.Release|Any CPU.Build.0 = Release|Any CPU
109+
{85557BA6-3D29-4C95-A646-2A972B1C2F25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
110+
{85557BA6-3D29-4C95-A646-2A972B1C2F25}.Debug|Any CPU.Build.0 = Debug|Any CPU
111+
{85557BA6-3D29-4C95-A646-2A972B1C2F25}.Release|Any CPU.ActiveCfg = Release|Any CPU
112+
{85557BA6-3D29-4C95-A646-2A972B1C2F25}.Release|Any CPU.Build.0 = Release|Any CPU
107113
EndGlobalSection
108114
GlobalSection(SolutionProperties) = preSolution
109115
HideSolutionNode = FALSE
@@ -121,6 +127,7 @@ Global
121127
{0D1552DC-E6ED-4AAC-5562-12F8352F46AA} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
122128
{17B8453F-AB72-99C5-E5EA-D0B065A6AE65} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
123129
{37B6A5E0-9995-497D-8B43-3BC6870CC716} = {A2F1F52A-9107-4BF8-8C3F-2F6670E7D0AD}
130+
{85557BA6-3D29-4C95-A646-2A972B1C2F25} = {2A77AF5C-138A-4EBB-9A13-9205DCD67928}
124131
EndGlobalSection
125132
GlobalSection(ExtensibilityGlobals) = postSolution
126133
SolutionGuid = {384A3888-751F-4D75-9AE5-587330582D89}

samples/TestServerWithHosting/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
}
3636
finally
3737
{
38-
await Log.CloseAndFlushAsync();
38+
Log.CloseAndFlush();
3939
}

samples/TestServerWithHosting/TestServerWithHosting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
5+
<TargetFrameworks>net9.0;net8.0;net472</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<!--

src/Common/Polyfills/System/Diagnostics/ProcessExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,28 @@ public static void Kill(this Process process, bool entireProcessTree)
1010
_ = entireProcessTree;
1111
process.Kill();
1212
}
13+
14+
public static async Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
15+
{
16+
if (process.HasExited)
17+
{
18+
return;
19+
}
20+
21+
var tcs = new TaskCompletionSource<bool>();
22+
void ProcessExitedHandler(object? sender, EventArgs e) => tcs.TrySetResult(true);
23+
24+
try
25+
{
26+
process.EnableRaisingEvents = true;
27+
process.Exited += ProcessExitedHandler;
28+
29+
using var _ = cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken));
30+
await tcs.Task.ConfigureAwait(false);
31+
}
32+
finally
33+
{
34+
process.Exited -= ProcessExitedHandler;
35+
}
36+
}
1337
}

src/Common/Polyfills/System/IO/TextReaderExtensions.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,7 @@ internal static class TextReaderExtensions
44
{
55
public static Task<string> ReadLineAsync(this TextReader reader, CancellationToken cancellationToken)
66
{
7-
if (cancellationToken.IsCancellationRequested)
8-
{
9-
return Task.FromCanceled<string>(cancellationToken);
10-
}
11-
7+
cancellationToken.ThrowIfCancellationRequested();
128
return reader.ReadLineAsync();
139
}
14-
15-
public static Task<string> ReadToEndAsync(this TextReader reader, CancellationToken cancellationToken)
16-
{
17-
if (cancellationToken.IsCancellationRequested)
18-
{
19-
return Task.FromCanceled<string>(cancellationToken);
20-
}
21-
22-
return reader.ReadToEndAsync();
23-
}
2410
}

src/Common/Polyfills/System/IO/TextWriterExtensions.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,6 @@ namespace System.IO;
55

66
internal static class TextWriterExtensions
77
{
8-
public static async Task WriteLineAsync(this TextWriter writer, ReadOnlyMemory<char> value, CancellationToken cancellationToken)
9-
{
10-
Throw.IfNull(writer);
11-
12-
if (value.IsEmpty)
13-
{
14-
return;
15-
}
16-
17-
cancellationToken.ThrowIfCancellationRequested();
18-
19-
if (MemoryMarshal.TryGetString(value, out string str, out int start, out int length) &&
20-
start == 0 && length == str.Length)
21-
{
22-
await writer.WriteLineAsync(str).ConfigureAwait(false);
23-
}
24-
else if (MemoryMarshal.TryGetArray(value, out ArraySegment<char> array) &&
25-
array.Array is not null && array.Offset == 0 && array.Count == array.Array.Length)
26-
{
27-
await writer.WriteLineAsync(array.Array).ConfigureAwait(false);
28-
}
29-
else
30-
{
31-
await writer.WriteLineAsync(value.ToArray()).ConfigureAwait(false);
32-
}
33-
}
34-
358
public static async Task FlushAsync(this TextWriter writer, CancellationToken cancellationToken)
369
{
3710
cancellationToken.ThrowIfCancellationRequested();

src/Common/Polyfills/System/Threading/Tasks/TaskExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ public static Task WaitAsync(this Task task, CancellationToken cancellationToken
99
return WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
1010
}
1111

12-
public static async Task<T> WaitAsync<T>(this Task<T> task, CancellationToken cancellationToken)
12+
public static Task<T> WaitAsync<T>(this Task<T> task, CancellationToken cancellationToken)
1313
{
14-
await WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
14+
return WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);
15+
}
16+
17+
public static async Task<T> WaitAsync<T>(this Task<T> task, TimeSpan timeout, CancellationToken cancellationToken = default)
18+
{
19+
await WaitAsync((Task)task, timeout, cancellationToken).ConfigureAwait(false);
1520
return task.Result;
1621
}
1722

18-
public static async Task WaitAsync(this Task task, TimeSpan timeout, CancellationToken cancellationToken)
23+
public static async Task WaitAsync(this Task task, TimeSpan timeout, CancellationToken cancellationToken = default)
1924
{
2025
Throw.IfNull(task);
2126

File renamed without changes.

src/ModelContextProtocol/ModelContextProtocol.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<IsAotCompatible>true</IsAotCompatible>
1515
</PropertyGroup>
1616

17+
<ItemGroup>
18+
<Compile Include="..\Common\Throw.cs" Link="Utils\Throw.cs" />
19+
</ItemGroup>
20+
1721
<!-- Dependencies only needed by netstandard2.0 -->
1822
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
1923
<Compile Include="..\Common\Polyfills\**\*.cs" />

0 commit comments

Comments
 (0)