Skip to content

Commit c61c299

Browse files
authored
Raise CCov to 91%; add tests, rm unused namespaces (#31)
1 parent a74d528 commit c61c299

File tree

9 files changed

+188
-36
lines changed

9 files changed

+188
-36
lines changed

AMT.Extensions.Logging/IP/Internal/NullExternalScopeProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
using Microsoft.Extensions.Logging;
88
using System;
9+
using System.Diagnostics.CodeAnalysis;
910

1011
namespace AMT.Extensions.Logging.IP
1112
{
1213
/// <summary>
1314
/// Scope provider that does nothing.
1415
/// </summary>
15-
internal class NullExternalScopeProvider : IExternalScopeProvider
16+
[ExcludeFromCodeCoverage]
17+
public class NullExternalScopeProvider : IExternalScopeProvider
1618
{
1719
private NullExternalScopeProvider()
1820
{

AMT.Extensions.Logging/IP/Internal/NullScope.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
// due to it being internal access there.
66

77
using System;
8+
using System.Diagnostics.CodeAnalysis;
89

910
namespace AMT.Extensions.Logging.IP
1011
{
1112
/// <summary>
1213
/// An empty scope without any logic
1314
/// </summary>
15+
[ExcludeFromCodeCoverage]
1416
internal class NullScope : IDisposable
1517
{
1618
public static NullScope Instance { get; } = new NullScope();

AMT.Extensions.Logging/IP/UdpLoggerProcessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ public virtual void EnqueueMessage(LogMessageEntry message)
4949
}
5050

5151
// Adding is completed so just log the message
52-
try
53-
{
54-
WriteMessage(message);
55-
}
56-
catch (Exception) { }
52+
WriteMessage(message);
5753
}
5854

5955
internal virtual void WriteMessage(LogMessageEntry entry)
@@ -96,13 +92,17 @@ public void Dispose()
9692
_messageQueue.CompleteAdding();
9793
_shutdown = true;
9894

95+
// Give the sender time to complete its tasks
9996
try
10097
{
98+
// TODO: use a config value for timeout
10199
_outputThread.Join(1500); // with timeout in-case Console is locked by user input
102100
}
103101
catch (ThreadStateException) { }
104102

105103
_udpSender.Dispose();
104+
// Clear any messages
105+
_messageQueue.Dispose();
106106
}
107107

108108
#endregion IDisposable impl

AMT.Extensions.Logging/IP/UdpLoggerProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ public ILogger CreateLogger(string category)
3737

3838
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
3939
{
40-
_scopeProvider = scopeProvider;
40+
if (null == scopeProvider)
41+
{
42+
_scopeProvider = NullExternalScopeProvider.Instance;
43+
} else
44+
{
45+
_scopeProvider = scopeProvider;
46+
}
4147
}
4248

4349
#endregion ISupportExternalScope impl
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) AltaModa Technologies. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Ext = AMT.Extensions.Logging.IP;
5+
using FluentAssertions;
6+
using System;
7+
using System.Diagnostics.CodeAnalysis;
8+
using Xunit;
9+
using AMT.Extensions.Logging.IP;
10+
11+
12+
namespace Test.AMT.Extensions.Logging.IP
13+
{
14+
[ExcludeFromCodeCoverage]
15+
public class UdpLoggerProcessorTests
16+
{
17+
18+
[Fact]
19+
public void excp_on_null_options()
20+
{
21+
Action act = () => new Ext.UdpLoggerProcessor(null);
22+
act.Should().Throw<ArgumentNullException>();
23+
}
24+
25+
26+
#region IDisposable tests
27+
28+
[Fact]
29+
public void verify_disposable()
30+
{
31+
using (var proc = new Ext.UdpLoggerProcessor(_opts))
32+
{
33+
var udpProv = new Ext.UdpLoggerProvider(_opts);
34+
}
35+
36+
}
37+
38+
#endregion IDisposable tests
39+
40+
41+
private static readonly UdpLoggerOptions _opts = new UdpLoggerOptions();
42+
}
43+
44+
}

Test.AMT.Extensions.Logging/IP/UdpLoggerProviderTests.cs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Xunit;
99
using AMT.Extensions.Logging.IP;
1010
using Microsoft.Extensions.Logging;
11-
using System.Configuration.Provider;
1211

1312

1413
namespace Test.AMT.Extensions.Logging.IP
@@ -17,6 +16,16 @@ namespace Test.AMT.Extensions.Logging.IP
1716
public class UdpLoggerProviderTests
1817
{
1918

19+
[Fact]
20+
public void excp_on_null_category()
21+
{
22+
var provider = new Ext.UdpLoggerProvider(_opts);
23+
Action act = () => provider.CreateLogger(null);
24+
25+
act.Should().Throw<ArgumentNullException>();
26+
}
27+
28+
2029
[Fact]
2130
public void excp_on_null_options()
2231
{
@@ -28,21 +37,48 @@ public void excp_on_null_options()
2837
#region ILoggerProvider tests
2938

3039
[Fact]
31-
public void foo()
40+
public void can_log_to_category()
3241
{
33-
var udpProv = new Ext.UdpLoggerProvider(_opts);
42+
using (var udpProv = new Ext.UdpLoggerProvider(_opts))
43+
{
44+
ILoggerProvider prov = udpProv as ILoggerProvider;
45+
Assert.NotNull(prov);
46+
47+
ILogger l = prov.CreateLogger("randomCategory");
48+
Assert.NotNull(l);
49+
50+
l.LogInformation("using ILogger.LogInformation...");
51+
}
52+
}
53+
54+
#endregion ILoggerProvider tests
3455

35-
ILoggerProvider prov = udpProv as ILoggerProvider;
36-
Assert.NotNull(prov);
3756

38-
ILogger l = prov.CreateLogger("randomCategory");
39-
Assert.NotNull(l);
57+
#region ISupportExternalScope tests
4058

41-
l.LogInformation("using ILogger.LogInformation...");
59+
[Fact]
60+
public void can_set_scope_provider()
61+
{
62+
var testEsps = new IExternalScopeProvider[] {
63+
null,
64+
NullExternalScopeProvider.Instance
65+
};
66+
// TODO: consider disabling null
67+
foreach (IExternalScopeProvider esp in testEsps ) {
68+
using (var udpProv = new Ext.UdpLoggerProvider(_opts))
69+
{
70+
udpProv.SetScopeProvider(esp);
4271

72+
ILogger l = udpProv.CreateLogger("randomCategory");
73+
Assert.NotNull(l);
74+
75+
l.LogInformation("using ILogger.LogInformation...");
76+
}
77+
}
4378
}
4479

45-
#endregion ILoggerProvider tests
80+
#endregion ISupportExternalScope tests
81+
4682

4783

4884
private static readonly UdpLoggerOptions _opts = new UdpLoggerOptions();

Test.AMT.Extensions.Logging/IP/UdpLoggerTests.cs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Diagnostics.CodeAnalysis;
99
using System.Linq;
1010
using Xunit;
11+
using System.Net;
12+
using System.Runtime.CompilerServices;
1113

1214

1315
namespace Test.AMT.Extensions.Logging.IP
@@ -19,30 +21,31 @@ public class UdpLoggerTests
1921
[Fact]
2022
public void verify_each_loglevel()
2123
{
22-
// Prepare listener to receive messages
23-
var opts = new Ext.UdpLoggerOptions();
24+
// Prepare listener to receive messages; isolate port for message count
25+
var opts = new Ext.UdpLoggerOptions(new IPEndPoint(IPAddress.Loopback, 17460));
2426

2527
using (_receiver = new UdpReceiver())
2628
{
2729
_receiver.Start(opts);
2830

2931
// Add options to provider before create logger
30-
_provider = new Ext.UdpLoggerProvider(opts);
31-
var logger = _provider.CreateLogger("test") as Ext.UdpLogger;
32-
33-
// Log a message of each LogLevel
34-
foreach (int level in _logLevels)
35-
{
36-
logger.Log((LogLevel)level, DEFAULT_EVENTID, DEFAULT_STATE, DEFAULT_EXCEPTION, setStringFormatter);
32+
using (_provider = new Ext.UdpLoggerProvider(opts)) {
33+
_logger = _provider.CreateLogger("test") as Ext.UdpLogger;
34+
35+
// Log a message of each LogLevel
36+
foreach (int level in _logLevels)
37+
{
38+
_logger.Log((LogLevel)level, DEFAULT_EVENTID, DEFAULT_STATE, DEFAULT_EXCEPTION, setStringFormatter);
39+
}
40+
41+
// Brief wait, then stop listener
42+
System.Threading.Thread.Sleep(100);
43+
_receiver.Stop();
44+
45+
// NOTE: RetrieveMessages returns a _consuming_ enumerator, so it can only be used once.
46+
// LogLevel.None filtered, so reduce by 1.
47+
_receiver.RetrieveMessages().Count().Should().Be(_logLevels.Length - 1);
3748
}
38-
39-
// Brief wait, then stop listener
40-
System.Threading.Thread.Sleep(100);
41-
_receiver.Stop();
42-
43-
// NOTE: RetrieveMessages returns a _consuming_ enumerator, so it can only be used once.
44-
// 2024.09.17, JB: previously None was filtered, but it seems that changed in logging fx.
45-
_receiver.RetrieveMessages().Count().Should().Be(_logLevels.Length);
4649
}
4750
}
4851

@@ -119,6 +122,36 @@ public void verify_few_log_messages()
119122
}
120123

121124

125+
[Fact]
126+
public void verify_begin_scope()
127+
{
128+
// Prepare listener to receive messages
129+
var opts = new Ext.UdpLoggerOptions();
130+
131+
using (_receiver = new UdpReceiver())
132+
{
133+
_receiver.Start(opts);
134+
135+
// Add options to provider before create logger
136+
using (_provider = new Ext.UdpLoggerProvider(opts)) {
137+
_logger = _provider.CreateLogger("test") as Ext.UdpLogger;
138+
139+
using (_logger.BeginScope("SCOPE L1: ", null))
140+
{
141+
_logger.Log(LogLevel.Trace, "some log message");
142+
using (_logger.BeginScope("SCOPE L2: ", null))
143+
{
144+
_logger.Log(LogLevel.Trace, "some log message");
145+
}
146+
147+
// TODO: confirm scope messages?
148+
}
149+
150+
}
151+
}
152+
}
153+
154+
122155
private LogLevel GetRandomLogLevel(bool excludeNone = true)
123156
{
124157
return (LogLevel)_randomizer.Next(0, excludeNone ? _logLevels.Length - 1 : _logLevels.Length);
@@ -139,6 +172,7 @@ public UdpLoggerTests(Xunit.Abstractions.ITestOutputHelper outputHelper)
139172

140173

141174
private ILoggerProvider _provider;
175+
private ILogger _logger;
142176
private UdpReceiver _receiver;
143177
private readonly object DEFAULT_STATE = "### Default state for testing ###";
144178
private readonly object NULL_STATE = "[null-state]"; // TODO: use null;

Test.AMT.Extensions.Logging/IP/Utils/UdpReceiver.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Test.AMT.Extensions.Logging.IP
1717
internal class UdpReceiver : IDisposable
1818
{
1919
private static int _maxQueuedMessages = 1024;
20-
private readonly BlockingCollection<string> _messageQueue = new BlockingCollection<string>(_maxQueuedMessages);
20+
private BlockingCollection<string> _messageQueue = new BlockingCollection<string>(_maxQueuedMessages);
2121

2222
private bool _stopListener = false;
2323
private UdpClient _client;
@@ -54,14 +54,27 @@ public void Stop()
5454
}
5555

5656

57+
private static IEnumerable<string> emptyList = new List<string>();
5758
public IEnumerable<string> RetrieveMessages()
5859
{
59-
return _messageQueue.GetConsumingEnumerable();
60+
if (_messageQueue.Count > 0)
61+
{
62+
return _messageQueue.GetConsumingEnumerable();
63+
} else
64+
{
65+
return emptyList;
66+
}
6067
}
6168

6269
#region IDisposable impl
6370
public void Dispose()
6471
{
72+
// Drain the queue
73+
if (_messageQueue != null)
74+
{
75+
_messageQueue = null;
76+
}
77+
6578
if (null != _client)
6679
{
6780
_client.Close();
@@ -71,6 +84,5 @@ public void Dispose()
7184
}
7285
#endregion IDisposable impl
7386

74-
7587
}
7688
}

Test.AMT.Extensions.System/ConvertTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public void excp_on_null()
6767
};
6868

6969
act.Should().Throw<ArgumentNullException>();
70+
71+
72+
// Verify proper exception when encoded is null
73+
act = () => {
74+
Ext.Convert.FromBase64UrlString(null);
75+
};
76+
77+
act.Should().Throw<ArgumentNullException>();
7078
}
7179

7280

@@ -79,6 +87,14 @@ public void excp_on_empty_string()
7987
};
8088

8189
act.Should().Throw<ArgumentOutOfRangeException>();
90+
91+
92+
// Verify proper exception when encoded is empty
93+
act = () => {
94+
Ext.Convert.FromBase64UrlString(string.Empty);
95+
};
96+
97+
act.Should().Throw<ArgumentOutOfRangeException>();
8298
}
8399

84100

0 commit comments

Comments
 (0)