Skip to content

Commit bfb791b

Browse files
authored
Logging Cleanup (#30)
1 parent ba4aa3f commit bfb791b

38 files changed

+482
-276
lines changed

src/NexNet.IntegrationTests/BaseTests.cs

+28-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Net.Sockets;
55
using System.Security.Authentication;
66
using System.Security.Cryptography.X509Certificates;
7+
using NexNet.IntegrationTests.Pipes;
78
using NexNet.IntegrationTests.TestInterfaces;
9+
using NexNet.Logging;
810
using NexNet.Quic;
911
using NexNet.Transports;
1012
using NUnit.Framework;
@@ -13,7 +15,7 @@
1315

1416
namespace NexNet.IntegrationTests;
1517

16-
public class BaseTests
18+
internal class BaseTests
1719
{
1820
public enum Type
1921
{
@@ -26,20 +28,18 @@ public enum Type
2628
private int _counter;
2729
private DirectoryInfo? _socketDirectory;
2830
protected UnixDomainSocketEndPoint? CurrentPath;
29-
//private ConsoleLogger _logger;
3031
protected int? CurrentTcpPort;
3132
protected int? CurrentUdpPort;
32-
//protected List<ConsoleLogger> Loggers = new List<ConsoleLogger>();
3333
protected List<INexusServer> Servers = new List<INexusServer>();
3434
protected List<INexusClient> Clients = new List<INexusClient>();
35-
private ConsoleLogger _logger = null!;
35+
private RollingLogger _logger = null!;
36+
private BasePipeTests.LogMode _loggerMode;
3637
public bool BlockForClose { get; set; }
37-
public ConsoleLogger Logger => _logger;
38+
public RollingLogger Logger => _logger;
3839

3940
[OneTimeSetUp]
4041
public void OneTimeSetUp()
4142
{
42-
//_logger = new ConsoleLogger();
4343
Trace.Listeners.Add(new ConsoleTraceListener());
4444
_socketDirectory = Directory.CreateTempSubdirectory("socketTests");
4545

@@ -48,25 +48,33 @@ public void OneTimeSetUp()
4848
[OneTimeTearDown]
4949
public virtual void OneTimeTearDown()
5050
{
51+
_loggerMode = BasePipeTests.LogMode.None;
5152
_socketDirectory?.Delete(true);
5253
Trace.Flush();
5354
}
5455

5556
[SetUp]
5657
public virtual void SetUp()
5758
{
58-
_logger = new ConsoleLogger();
59+
_logger = new RollingLogger();
5960
BlockForClose = false;
6061
}
6162

6263
[TearDown]
6364
public virtual void TearDown()
6465
{
65-
if (TestContext.CurrentContext.Result.Outcome != ResultState.Success)
66+
if (_loggerMode == BasePipeTests.LogMode.OnTestFail)
67+
{
68+
if (TestContext.CurrentContext.Result.Outcome != ResultState.Success)
69+
{
70+
_logger.Flush(TestContext.Out);
71+
}
72+
}
73+
if (_loggerMode == BasePipeTests.LogMode.Always)
6674
{
6775
_logger.Flush(TestContext.Out);
6876
}
69-
77+
7078
CurrentPath = null;
7179
CurrentTcpPort = null;
7280
CurrentUdpPort = null;
@@ -166,9 +174,12 @@ protected ServerConfig CreateServerConfigWithLog(Type type, INexusLogger? logger
166174
throw new InvalidOperationException();
167175
}
168176

169-
protected ServerConfig CreateServerConfig(Type type, bool log = false)
177+
protected ServerConfig CreateServerConfig(Type type, BasePipeTests.LogMode log = BasePipeTests.LogMode.None)
170178
{
171-
var logger = log ? _logger.CreateLogger(null, "SV") : null;
179+
_loggerMode = log;
180+
var logger = log != BasePipeTests.LogMode.None
181+
? _logger.CreatePrefixedLogger(null, "SV")
182+
: null;
172183
return CreateServerConfigWithLog(type, logger);
173184
}
174185

@@ -234,9 +245,13 @@ protected ClientConfig CreateClientConfigWithLog(Type type, INexusLogger? logger
234245
throw new InvalidOperationException();
235246
}
236247

237-
protected ClientConfig CreateClientConfig(Type type, bool log = false)
248+
protected ClientConfig CreateClientConfig(Type type, BasePipeTests.LogMode log = BasePipeTests.LogMode.None)
238249
{
239-
var logger = log ? _logger.CreateLogger(null, "CL") : null;
250+
_loggerMode = log;
251+
252+
var logger = log != BasePipeTests.LogMode.None
253+
? _logger.CreatePrefixedLogger(null, "CL")
254+
: null;
240255

241256
return CreateClientConfigWithLog(type, logger);
242257
}

src/NexNet.IntegrationTests/ConsoleLogger.cs

-91
This file was deleted.

src/NexNet.IntegrationTests/Pipes/BasePipeTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace NexNet.IntegrationTests.Pipes;
44

55
internal class BasePipeTests : BaseTests
66
{
7+
8+
public enum LogMode
9+
{
10+
None,
11+
OnTestFail,
12+
Always
13+
}
714
protected byte[] Data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
815

916
protected async Task<(
@@ -13,7 +20,7 @@ internal class BasePipeTests : BaseTests
1320
NexusClient<ClientNexus, ClientNexus.ServerProxy> client,
1421
ClientNexus clientNexus,
1522
TaskCompletionSource tcs
16-
)> Setup(Type type, bool log = false)
23+
)> Setup(Type type, LogMode log = LogMode.None)
1724
{
1825
var tcs = new TaskCompletionSource();
1926

src/NexNet.IntegrationTests/Pipes/NexusChannelReaderWriterTestBase.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Buffers;
22
using NexNet.Internals;
3+
using NexNet.Logging;
34
using NexNet.Messages;
45
using NexNet.Pipes;
56
using NUnit.Framework;

src/NexNet.IntegrationTests/Pipes/NexusClientTests_NexusDuplexPipe.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ internal class NexusClientTests_NexusDuplexPipe : BasePipeTests
1313
[TestCase(Type.Quic)]
1414
public async Task Client_PipeReaderReceivesDataMultipleTimes(Type type)
1515
{
16-
var (_, sNexus, _, cNexus, tcs) = await Setup(type, true);
16+
var (_, sNexus, _, cNexus, tcs) = await Setup(type);
1717
int count = 0;
1818

1919
// TODO: Review adding a test for increased iterations as this has been found to sometimes fail on CI.
20-
const int iterations = 1000;
20+
const int iterations = 21;
2121
cNexus.ClientTaskValueWithDuplexPipeEvent = async (nexus, pipe) =>
2222
{
2323
var result = await pipe.Input.ReadAsync().Timeout(1);
@@ -105,7 +105,7 @@ public async Task Client_PipeReaderReceivesData(Type type)
105105
[TestCase(Type.Quic)]
106106
public async Task Client_PipeWriterSendsData(Type type)
107107
{
108-
var (_, sNexus, _, cNexus, _) = await Setup(type, true);
108+
var (_, sNexus, _, cNexus, _) = await Setup(type);
109109

110110
cNexus.ClientTaskValueWithDuplexPipeEvent = async (nexus, pipe) =>
111111
{

src/NexNet.IntegrationTests/Pipes/NexusDuplexPipeReaderTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private NexusPipeReader CreateReader(IPipeStateManager? stateManager = null)
1212
stateManager ??= new PipeStateManagerStub();
1313
var reader = new NexusPipeReader(
1414
stateManager,
15-
null,//new ConsoleLogger(""),
15+
null,
1616
true,
1717
1024 * 128,
1818
1024 * 1024,
@@ -376,7 +376,7 @@ public async Task ReaderNotifiesBackPressure_HighWaterCutoff()
376376
stateManager ??= new PipeStateManagerStub();
377377
var reader = new NexusPipeReader(
378378
stateManager,
379-
null,//new ConsoleLogger(""),
379+
null,
380380
true,
381381
1024 * 128,
382382
1024 * 1024,
@@ -397,7 +397,7 @@ public async Task ReadAsyncNotifiesBackPressure_ReachesLowWaterMark()
397397
stateManager ??= new PipeStateManagerStub();
398398
var reader = new NexusPipeReader(
399399
stateManager,
400-
null,//new ConsoleLogger(""),
400+
null,
401401
true,
402402
1024 * 128,
403403
1024 * 1024,

src/NexNet.IntegrationTests/Pipes/NexusServerTests_NexusDuplexPipe.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class NexusServerTests_NexusDuplexPipe : BasePipeTests
1212
[TestCase(Type.Quic)]
1313
public async Task Server_PipeReaderReceivesDataMultipleTimes(Type type)
1414
{
15-
var (_, sNexus, _, cNexus, tcs) = await Setup(type, true);
15+
var (_, sNexus, _, cNexus, tcs) = await Setup(type);
1616
var count = 0;
1717

1818
// TODO: Review adding a test for increased iterations as this has been found to sometimes fail on CI.
@@ -49,7 +49,7 @@ public async Task Server_PipeReaderReceivesDataMultipleTimes(Type type)
4949
[TestCase(Type.Quic)]
5050
public async Task Server_PipeReaderReceivesDataMultipleTimesWithLargeData(Type type)
5151
{
52-
var (_, sNexus, _, cNexus, tcs) = await Setup(type, true);
52+
var (_, sNexus, _, cNexus, tcs) = await Setup(type);
5353
var count = 0;
5454
var largeData = new byte[1024 * 32];
5555
// TODO: Review adding a test for increased iterations as this has been found to sometimes fail on CI.
@@ -80,7 +80,7 @@ public async Task Server_PipeReaderReceivesDataMultipleTimesWithLargeData(Type t
8080
[TestCase(Type.Quic)]
8181
public async Task Server_PipeReaderCreatesAndDestroysPipeMultipleTimes(Type type)
8282
{
83-
var (_, sNexus, _, cNexus, tcs) = await Setup(type, true);
83+
var (_, sNexus, _, cNexus, tcs) = await Setup(type);
8484
var count = 0;
8585

8686
// TODO: Review adding a test for increased iterations as this has been found to sometimes fail on CI.

src/NexNet.Quic/QuicTransportListener.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Security;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using NexNet.Logging;
78
using NexNet.Transports;
89

910
#pragma warning disable CA1416

src/NexNet.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.3.3</Version>
3+
<Version>0.4.0</Version>
44
<Nullable>enable</Nullable>
55
<LangVersion>latest</LangVersion>
66
<TargetFramework>net7.0</TargetFramework>

src/NexNet/Internals/INexusSession.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using NexNet.Cache;
33
using NexNet.Invocation;
4+
using NexNet.Logging;
45
using NexNet.Pipes;
56
using NexNet.Transports;
67

src/NexNet/Internals/NexusSession.Receiving.cs

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Runtime.CompilerServices;
88
using System.Threading;
9+
using NexNet.Logging;
910

1011
namespace NexNet.Internals;
1112

@@ -356,6 +357,7 @@ static async void InvokeOnConnected(object? sessionObj)
356357

357358
using var serverGreeting = _cacheManager.Rent<ServerGreetingMessage>();
358359
serverGreeting.Version = 0;
360+
serverGreeting.ClientId = Id;
359361

360362
await SendMessage(serverGreeting).ConfigureAwait(false);
361363

@@ -373,6 +375,9 @@ static async void InvokeOnConnected(object? sessionObj)
373375
if (IsServer)
374376
return DisconnectReason.ProtocolError;
375377

378+
// Set the server assigned client id.
379+
Id = message.As<ServerGreetingMessage>().ClientId;
380+
376381
_ = Task.Factory.StartNew(InvokeOnConnected, this);
377382

378383
_readyTaskCompletionSource?.TrySetResult();

src/NexNet/Internals/NexusSession.Sending.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using MemoryPack;
99
using Pipelines.Sockets.Unofficial.Arenas;
1010
using System.Diagnostics;
11+
using NexNet.Logging;
1112

1213
namespace NexNet.Internals;
1314

0 commit comments

Comments
 (0)