Skip to content

Commit c19e8bf

Browse files
committed
add PortManager in the integration tests to hopefully fix the CI once and for all
1 parent db6cf1f commit c19e8bf

File tree

7 files changed

+61
-36
lines changed

7 files changed

+61
-36
lines changed

tests/protobuf-net.Grpc.Test.Integration/GrpcServiceTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static class AdhocConfig
9696

9797
public class GrpcServiceFixture : IAsyncDisposable
9898
{
99-
public const int Port = 10042;
99+
public int Port { get; } = PortManager.GetNextPort();
100100
private readonly Server _server;
101101

102102
private readonly Interceptor _interceptor;
@@ -151,6 +151,8 @@ public GrpcServiceTests(GrpcServiceFixture fixture, ITestOutputHelper log)
151151
if (fixture != null) fixture.Output = log;
152152
}
153153

154+
private int Port => _fixture.Port;
155+
154156
private void Log(string message) => _fixture?.Log(message);
155157

156158
public void Dispose()
@@ -168,7 +170,7 @@ private static readonly ProtoBufMarshallerFactory
168170
public async Task CanCallAllApplyServicesUnaryAsync(bool disableContextual)
169171
{
170172
GrpcClientFactory.AllowUnencryptedHttp2 = true;
171-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
173+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
172174

173175
var request = new Apply { X = 6, Y = 3 };
174176
var marshaller = disableContextual ? DisableContextualSerializer : EnableContextualSerializer;
@@ -220,7 +222,7 @@ public async Task CanCallAllApplyServicesUnaryAsync(bool disableContextual)
220222
public async Task CanCallAllApplyServicesTypedUnaryAsync()
221223
{
222224
GrpcClientFactory.AllowUnencryptedHttp2 = true;
223-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
225+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
224226

225227
var request = new Apply { X = 6, Y = 3 };
226228

@@ -243,7 +245,7 @@ public async Task CanCallAllApplyServicesTypedUnaryAsync()
243245
public void CanCallAllApplyServicesUnarySync()
244246
{
245247
GrpcClientFactory.AllowUnencryptedHttp2 = true;
246-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
248+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
247249

248250
var request = new Apply { X = 6, Y = 3 };
249251

@@ -264,7 +266,7 @@ public void CanCallAllApplyServicesUnarySync()
264266
public void CanCallAllApplyServicesTypedUnarySync()
265267
{
266268
GrpcClientFactory.AllowUnencryptedHttp2 = true;
267-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
269+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
268270

269271
var request = new Apply { X = 6, Y = 3 };
270272

@@ -287,7 +289,7 @@ public void CanCallAllApplyServicesTypedUnarySync()
287289
public void CanCallAdocService()
288290
{
289291
GrpcClientFactory.AllowUnencryptedHttp2 = true;
290-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
292+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
291293

292294
var request = new AdhocRequest { X = 12, Y = 7 };
293295
var client = http.CreateGrpcService<IAdhocService>(AdhocConfig.ClientFactory);
@@ -299,7 +301,7 @@ public void CanCallAdocService()
299301
public async Task CanCallInterceptedService()
300302
{
301303
GrpcClientFactory.AllowUnencryptedHttp2 = true;
302-
using var http = GrpcChannel.ForAddress($"http://localhost:{GrpcServiceFixture.Port}");
304+
using var http = GrpcChannel.ForAddress($"http://localhost:{Port}");
303305

304306
var request = new Apply { X = 6, Y = 3 };
305307

tests/protobuf-net.Grpc.Test.Integration/Issues/Issue100.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if NETCOREAPP3_1 // getting problems from multiple TFMs fighting over ports; just... don't fight it; can test locally fine - is just a problem on CI
2-
using Grpc.Core;
1+
using Grpc.Core;
32
using ProtoBuf;
43
using ProtoBuf.Grpc.Client;
54
using ProtoBuf.Grpc.Configuration;
@@ -23,7 +22,7 @@ public class Issue100 : IClassFixture<Issue100.Issue100ServerFixture>
2322
[Fact]
2423
public async Task MeasuredSerialize()
2524
{
26-
var obj = await _server.GetTestInstance();
25+
var obj = await ((ITest)_server).GetTestInstance();
2726
var ms = new MemoryStream();
2827
Serializer.Serialize(ms, obj); // regular serialize
2928
Assert.Equal(14, ms.Length);
@@ -60,8 +59,6 @@ public async Task MeasuredSerialize()
6059
}
6160
#endif
6261

63-
const int Port = 10046;
64-
6562
[Service]
6663
public interface ITest
6764
{
@@ -71,10 +68,12 @@ public interface ITest
7168

7269
#pragma warning disable IDE0051, IDE0052 // "unused" things; they are, but it depends on the TFM
7370
private readonly ITestOutputHelper _log;
74-
private readonly ITest _server;
71+
private readonly Issue100ServerFixture _server;
7572
private void Log(string message) => _log?.WriteLine(message);
7673
#pragma warning restore IDE0051, IDE0052
7774

75+
private int Port => _server.Port;
76+
7877
public Issue100(Issue100ServerFixture server, ITestOutputHelper log)
7978
{
8079
_server = server;
@@ -84,6 +83,8 @@ public Issue100(Issue100ServerFixture server, ITestOutputHelper log)
8483

8584
public class Issue100ServerFixture : ITest, IDisposable
8685
{
86+
public int Port { get; } = PortManager.GetNextPort();
87+
8788
public void Dispose() => _ = _server.KillAsync();
8889

8990
private readonly Server? _server;
@@ -160,5 +161,4 @@ public class TestThingy : TestBase
160161
public override string SomeText2 { get; set; }
161162
}
162163
}
163-
}
164-
#endif
164+
}

tests/protobuf-net.Grpc.Test.Integration/Issues/Issue109.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Issue109 // Server refuses to die!
99
[Fact(Skip = "definitely fails, but needs Grpc.Core input")]
1010
public async Task Issue_109()
1111
{
12-
var port = 10050;
12+
var port = PortManager.GetNextPort();
1313

1414
var server = new Server()
1515
{

tests/protobuf-net.Grpc.Test.Integration/Issues/Issue75_Exceptions.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if NETCOREAPP3_1 // getting problems from multiple TFMs fighting over ports; since we're testing the unmanaged server, the client TFM doesn't matter - just do one
2-
3-
using Grpc.Core;
1+
using Grpc.Core;
42
using ProtoBuf.Grpc.Client;
53
using ProtoBuf.Grpc.Configuration;
64
using ProtoBuf.Grpc.Server;
@@ -14,8 +12,6 @@ namespace protobuf_net.Grpc.Test.Integration.Issues
1412
{
1513
public class Issue75 : IClassFixture<Issue75.Issue75ServerFixture>
1614
{
17-
const int Port = 10045;
18-
1915
[Service]
2016
public interface IFaultTest
2117
{
@@ -34,14 +30,18 @@ public interface IInterceptedFaultTest
3430
ValueTask Fault();
3531
}
3632

33+
private readonly Issue75ServerFixture _serverFixture;
3734
public Issue75(Issue75ServerFixture serverFixture)
3835
{
39-
if (serverFixture is null) throw new ArgumentNullException(nameof(serverFixture));
36+
_serverFixture = serverFixture ?? throw new ArgumentNullException(nameof(serverFixture));
4037
GrpcClientFactory.AllowUnencryptedHttp2 = true;
4138
}
4239

40+
private int Port => _serverFixture.Port;
41+
4342
public class Issue75ServerFixture : IFaultTest, IInterceptedFaultTest, IDisposable
4443
{
44+
public int Port { get; } = PortManager.GetNextPort();
4545
public void Dispose() => _ = _server.KillAsync();
4646

4747
private readonly Server? _server;
@@ -233,5 +233,4 @@ public async Task ManagedClient_Intercepted_Success()
233233
}
234234
#endif
235235
}
236-
}
237-
#endif
236+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Threading;
2+
3+
namespace protobuf_net.Grpc.Test.Integration
4+
{
5+
internal static class PortManager
6+
{
7+
// Grpc.Core is very reluctant to release ports, so
8+
// we'll be more vigilent about not trying to reuse them
9+
private static int s_Port;
10+
static PortManager()
11+
{
12+
// we have 1024 to 65535 to play with;
13+
// allow 1000 per TFM, half for down-level,
14+
// half for up-level
15+
#if NETCOREAPP3_1
16+
s_Port = 10000;
17+
#elif NETCOREAPP3_0
18+
s_Port = 11000;
19+
#elif NET472
20+
s_Port = 12000;
21+
#else
22+
#error No port range defined for this TFM
23+
#endif
24+
#if PROTOBUFNET_BUFFERS
25+
s_Port += 500;
26+
#endif
27+
}
28+
public static int GetNextPort() => Interlocked.Increment(ref s_Port);
29+
}
30+
}

tests/protobuf-net.Grpc.Test.Integration/StreamTests.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,18 @@ public void Log(string message)
3535
}
3636
public StreamTestsFixture() { }
3737

38-
public int Port { get; private set; }
39-
public void Init(int port)
38+
public int Port { get; } = PortManager.GetNextPort();
39+
public void Init()
4040
{
4141
if (_server == null)
4242
{
43-
Port = port;
4443
_server = new Server
4544
{
4645
Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
4746
};
4847
_server.Services.AddCodeFirst(new StreamServer(this));
4948
_server.Start();
5049
}
51-
else if (Port != port)
52-
{
53-
throw new InvalidOperationException("Cannot change port on the fixture instance");
54-
}
5550
}
5651

5752
public ValueTask DisposeAsync() => _server == null ? default : new ValueTask(_server.ShutdownAsync());
@@ -299,7 +294,7 @@ public class Foo
299294

300295
public class NativeStreamTests : StreamTests
301296
{
302-
public NativeStreamTests(StreamTestsFixture fixture, ITestOutputHelper log) : base(10043, fixture, log) { }
297+
public NativeStreamTests(StreamTestsFixture fixture, ITestOutputHelper log) : base(fixture, log) { }
303298
protected override IAsyncDisposable CreateClient(out IStreamAPI client)
304299
{
305300
var channel = new Channel("localhost", Port, ChannelCredentials.Insecure);
@@ -319,7 +314,7 @@ public DisposableChannel(Channel channel)
319314
public class ManagedStreamTests : StreamTests
320315
{
321316
public override bool IsManagedClient => true;
322-
public ManagedStreamTests(StreamTestsFixture fixture, ITestOutputHelper log) : base(10044, fixture, log) { }
317+
public ManagedStreamTests(StreamTestsFixture fixture, ITestOutputHelper log) : base(fixture, log) { }
323318
protected override IAsyncDisposable CreateClient(out IStreamAPI client)
324319
{
325320
var http = global::Grpc.Net.Client.GrpcChannel.ForAddress($"http://localhost:{Port}");
@@ -375,10 +370,10 @@ public DebugFactAttribute()
375370

376371
protected int Port => _fixture.Port;
377372
private readonly StreamTestsFixture _fixture;
378-
public StreamTests(int port, StreamTestsFixture fixture, ITestOutputHelper log)
373+
public StreamTests(StreamTestsFixture fixture, ITestOutputHelper log)
379374
{
380375
_fixture = fixture;
381-
fixture.Init(port);
376+
fixture.Init();
382377
fixture?.SetOutput(log);
383378
GrpcClientFactory.AllowUnencryptedHttp2 = true;
384379
}

tests/protobuf-net.Grpc.Test.IntegrationUpLevel/protobuf-net.Grpc.Test.IntegrationUpLevel.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<RootNamespace>protobuf_net.Grpc.Test.Integration</RootNamespace>
66
<GenerateDocumentationFile>false</GenerateDocumentationFile>
77
<IsPackable>false</IsPackable>
8-
98
<DefineConstants>$(DefineConstants);PROTOBUFNET_BUFFERS</DefineConstants>
109
</PropertyGroup>
1110

0 commit comments

Comments
 (0)