Skip to content

Commit d6c1715

Browse files
committed
Fix all Tests
1 parent 97ca30f commit d6c1715

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2209
-1290
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
matrix:
1515
os: [ubuntu-latest, windows-latest, macos-latest]
1616
dotnet-version: [ '6.0', '7.0', '8.0' ]
17+
fail-fast: false
1718
runs-on: ${{ matrix.os }}
1819

1920
steps:

ArtNetSharp/ArtNet.cs

Lines changed: 158 additions & 88 deletions
Large diffs are not rendered by default.

ArtNetSharp/Communication/AbstractInstance.cs

Lines changed: 201 additions & 246 deletions
Large diffs are not rendered by default.

ArtNetSharp/Communication/ConfigInstance.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace ArtNetSharp.Communication
77
{
88
public class ConfigInstance : AbstractInstance
99
{
10+
public ConfigInstance(ArtNet _artnet) : base(_artnet)
11+
{
12+
}
13+
1014
public sealed override EStCodes EstCodes => EStCodes.StConfig;
1115
protected sealed override bool SendArtPollBroadcast => true;
1216
protected sealed override bool SendArtPollTargeted => false;

ArtNetSharp/Communication/ControllerInstance.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace ArtNetSharp.Communication
77
{
88
public class ControllerInstance : AbstractInstance
99
{
10+
public ControllerInstance(ArtNet _artnet) : base(_artnet)
11+
{
12+
}
13+
1014
public sealed override EStCodes EstCodes => EStCodes.StController;
1115
protected sealed override bool SendArtPollBroadcast => false;
1216
protected sealed override bool SendArtPollTargeted => true;

ArtNetSharp/Communication/NodeInstance.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace ArtNetSharp.Communication
66
{
77
public class NodeInstance : AbstractInstance
88
{
9+
public NodeInstance(ArtNet _artnet) : base(_artnet)
10+
{
11+
}
12+
913
public sealed override EStCodes EstCodes => EStCodes.StNode;
1014
protected sealed override bool SendArtPollBroadcast => false;
1115
protected sealed override bool SendArtPollTargeted => true;

ArtNetSharp/Communication/PortConfig.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ArtNetSharp.Communication
1111
{
1212
public class PortConfig
1313
{
14-
private static ILogger Logger = ApplicationLogging.CreateLogger<PortConfig>();
14+
private static readonly ILogger Logger = ApplicationLogging.CreateLogger<PortConfig>();
1515
public virtual PortAddress PortAddress { get; set; }
1616
public Net Net { get => PortAddress.Net; }
1717
public Subnet Subnet { get => PortAddress.Subnet; }
@@ -29,13 +29,13 @@ public class PortConfig
2929

3030
public virtual bool ForceBroadcast { get; set; }
3131

32-
private List<IPv4Address> additionalIPEndpoints;
32+
private readonly List<IPv4Address> additionalIPEndpoints;
3333
public IReadOnlyCollection<IPv4Address> AdditionalIPEndpoints { get; private set; }
3434

3535

36-
private ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> discoveredRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
36+
private readonly ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> discoveredRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
3737
public IReadOnlyCollection<RDMUID_ReceivedBag> DiscoveredRDMUIDs;
38-
private ConcurrentDictionary<RDMUID, RDMUID> additionalRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID>();
38+
private readonly ConcurrentDictionary<RDMUID, RDMUID> additionalRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID>();
3939
public IReadOnlyCollection<RDMUID> AdditionalRDMUIDs;
4040
public event EventHandler<RDMUID_ReceivedBag> RDMUIDReceived;
4141
//public event EventHandler<RDMMessage> RDMMessageReceived;
@@ -100,15 +100,14 @@ internal void AddDiscoveredRdmUIDs(params RDMUID[] rdmuids)
100100

101101
foreach (RDMUID rdmuid in rdmuids)
102102
{
103-
RDMUID_ReceivedBag bag;
104-
if (discoveredRDMUIDs.TryGetValue(rdmuid, out bag))
103+
if (discoveredRDMUIDs.TryGetValue(rdmuid, out RDMUID_ReceivedBag bag))
105104
bag.Seen();
106105
else
107106
{
108107
bag = new RDMUID_ReceivedBag(rdmuid);
109108
if (discoveredRDMUIDs.TryAdd(rdmuid, bag))
110109
{
111-
RDMUIDReceived?.Invoke(this, bag);
110+
RDMUIDReceived?.InvokeFailSafe(this, bag);
112111
Logger.LogTrace($"#{BindIndex} PortAddress: {PortAddress.Combined:x4} Cached UID: {bag.Uid}");
113112
}
114113
}

ArtNetSharp/Communication/RemoteClient.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ArtNetSharp.Communication
1212
{
1313
public sealed class RemoteClient : INotifyPropertyChanged
1414
{
15-
private static ILogger Logger = ApplicationLogging.CreateLogger<RemoteClient>();
15+
private static readonly ILogger Logger = ApplicationLogging.CreateLogger<RemoteClient>();
1616
public readonly string ID;
1717
public readonly MACAddress MacAddress;
1818
private IPv4Address ipAddress;
@@ -195,13 +195,13 @@ public ArtPollReply Root
195195
this.IsSACNCapable = root.Status.NodeSupportArtNet_sACN_Switching;
196196
}
197197
}
198-
private ConcurrentDictionary<int, RemoteClientPort> ports = new ConcurrentDictionary<int, RemoteClientPort>();
198+
private readonly ConcurrentDictionary<int, RemoteClientPort> ports = new ConcurrentDictionary<int, RemoteClientPort>();
199199
public IReadOnlyCollection<RemoteClientPort> Ports { get; private set; }
200200
public event EventHandler<RemoteClientPort> PortDiscovered;
201201
public event EventHandler<RemoteClientPort> PortTimedOut;
202202

203-
private ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
204-
private ConcurrentDictionary<EDataRequest, object> artDataCache = new ConcurrentDictionary<EDataRequest, object>();
203+
private readonly ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
204+
private readonly ConcurrentDictionary<EDataRequest, object> artDataCache = new ConcurrentDictionary<EDataRequest, object>();
205205
public IReadOnlyCollection<RDMUID_ReceivedBag> KnownRDMUIDs;
206206
public event EventHandler<RDMUID_ReceivedBag> RDMUIDReceived;
207207

@@ -214,7 +214,7 @@ private void onPropertyChanged(PropertyChangedEventArgs eventArgs)
214214
{
215215
try
216216
{
217-
PropertyChanged?.Invoke(this, eventArgs);
217+
PropertyChanged?.InvokeFailSafe(this, eventArgs);
218218
}
219219
catch (Exception e)
220220
{
@@ -284,15 +284,14 @@ public void processArtPollReply(ArtPollReply artPollReply)
284284
for (byte portIndex = 0; portIndex < artPollReply.Ports; portIndex++)
285285
{
286286
int physicalPort = (Math.Max(0, artPollReply.BindIndex - 1) * artPollReply.Ports) + portIndex;
287-
RemoteClientPort port = null;
288-
if (ports.TryGetValue(physicalPort, out port))
287+
if (ports.TryGetValue(physicalPort, out RemoteClientPort port))
289288
port.processArtPollReply(artPollReply);
290289
else
291290
{
292291
port = new RemoteClientPort(artPollReply, portIndex);
293292
if (ports.TryAdd(physicalPort, port))
294293
{
295-
PortDiscovered?.Invoke(this, port);
294+
PortDiscovered?.InvokeFailSafe(this, port);
296295
port.RDMUIDReceived += Port_RDMUIDReceived;
297296
}
298297
}
@@ -311,7 +310,7 @@ public void processArtPollReply(ArtPollReply artPollReply)
311310
foreach (var port in timoutedPorts)
312311
{
313312
ports.TryRemove(port.Key, out _);
314-
PortTimedOut?.Invoke(this, port.Value);
313+
PortTimedOut?.InvokeFailSafe(this, port.Value);
315314
}
316315
}
317316
Ports = ports.Select(p => p.Value).ToList().AsReadOnly();
@@ -333,26 +332,26 @@ private async Task PollArtData()
333332
if (Instance is ControllerInstance)
334333
{
335334
using ArtData artData = new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode);
336-
await ArtNet.Instance.TrySendPacket(artData, IpAddress);
335+
await Instance.ArtNetInstance.TrySendPacket(artData, IpAddress);
337336
}
338337
}
339338
private async Task QueryArtData()
340339
{
341-
if (!(Instance is ControllerInstance))
340+
if (Instance is not ControllerInstance)
342341
return;
343342

344343
EDataRequest[] todo = new[] { EDataRequest.UrlProduct, EDataRequest.UrlSupport, EDataRequest.UrlUserGuide, EDataRequest.UrlPersUdr, EDataRequest.UrlPersGdtf };
345344
foreach (EDataRequest req in todo)
346345
{
347346
using ArtData artData = new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode, req);
348-
await ArtNet.Instance.TrySendPacket(artData, IpAddress);
347+
await Instance.ArtNetInstance.TrySendPacket(artData, IpAddress);
349348
}
350349
}
351350

352351
private void Port_RDMUIDReceived(object sender, RDMUID_ReceivedBag bag)
353352
{
354353
knownRDMUIDs.AddOrUpdate(bag.Uid, bag, (x, y) => bag);
355-
RDMUIDReceived?.Invoke(this, bag);
354+
RDMUIDReceived?.InvokeFailSafe(this, bag);
356355
}
357356
public void RemoveOutdatedRdmUIDs()
358357
{

ArtNetSharp/Communication/RemoteClientPort.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ArtNetSharp.Communication
1111
{
1212
public sealed class RemoteClientPort : INotifyPropertyChanged
1313
{
14-
private static ILogger Logger = ApplicationLogging.CreateLogger<RemoteClientPort>();
14+
private static readonly ILogger Logger = ApplicationLogging.CreateLogger<RemoteClientPort>();
1515
public readonly IPv4Address IpAddress;
1616
public readonly string ID;
1717
public readonly byte BindIndex;
@@ -130,9 +130,9 @@ private set
130130
}
131131
}
132132

133-
private ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownControllerRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
133+
private readonly ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownControllerRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
134134
public IReadOnlyCollection<RDMUID_ReceivedBag> KnownControllerRDMUIDs;
135-
private ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownResponderRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
135+
private readonly ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag> knownResponderRDMUIDs = new ConcurrentDictionary<RDMUID, RDMUID_ReceivedBag>();
136136
public IReadOnlyCollection<RDMUID_ReceivedBag> KnownResponderRDMUIDs;
137137
public event EventHandler<RDMUID_ReceivedBag> RDMUIDReceived;
138138

@@ -146,25 +146,14 @@ private void onPropertyChanged(PropertyChangedEventArgs eventArgs)
146146
{
147147
try
148148
{
149-
PropertyChanged?.Invoke(this, eventArgs);
149+
PropertyChanged?.InvokeFailSafe(this, eventArgs);
150150
}
151151
catch (Exception e)
152152
{
153153
Logger.LogError(e);
154154
}
155155
}
156156

157-
158-
private byte sequence = byte.MaxValue;
159-
internal byte Sequence
160-
{
161-
get
162-
{
163-
sequence++;
164-
return sequence;
165-
}
166-
}
167-
168157
public RemoteClientPort(in ArtPollReply artPollReply, byte portIndex = 0)
169158
{
170159
ID = getIDOf(artPollReply, portIndex);
@@ -223,8 +212,7 @@ public void processArtPollReply(ArtPollReply artPollReply)
223212
}
224213
private void addControllerRdmUID(RDMUID rdmuid)
225214
{
226-
RDMUID_ReceivedBag bag;
227-
if (knownControllerRDMUIDs.TryGetValue(rdmuid, out bag))
215+
if (knownControllerRDMUIDs.TryGetValue(rdmuid, out RDMUID_ReceivedBag bag))
228216
bag.Seen();
229217
else
230218
{
@@ -241,15 +229,14 @@ internal void AddResponderRdmUIDs(params RDMUID[] rdmuids)
241229

242230
foreach (RDMUID rdmuid in rdmuids)
243231
{
244-
RDMUID_ReceivedBag bag;
245-
if (knownResponderRDMUIDs.TryGetValue(rdmuid, out bag))
232+
if (knownResponderRDMUIDs.TryGetValue(rdmuid, out RDMUID_ReceivedBag bag))
246233
bag.Seen();
247234
else
248235
{
249236
bag = new RDMUID_ReceivedBag(rdmuid);
250237
if (knownResponderRDMUIDs.TryAdd(rdmuid, bag))
251238
{
252-
RDMUIDReceived?.Invoke(this, bag);
239+
RDMUIDReceived?.InvokeFailSafe(this, bag);
253240
Logger.LogTrace($"{IpAddress}#{BindIndex} Cached Responder UID: {bag.Uid}");
254241
}
255242
}

ArtNetSharp/FileProvider.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ internal class FileProvider : ILoggerProvider
1818
private static readonly string fileDirectory = getOsDirectory();
1919

2020
private static readonly string filePath = Path.Combine(fileDirectory, "log.txt");
21-
private static SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
21+
private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
2222

2323
private readonly ConcurrentQueue<string> queue= new ConcurrentQueue<string>();
24-
private readonly Thread thread;
2524
private bool isDisposing = false;
2625
private static string getOsDirectory()
2726
{
@@ -59,30 +58,23 @@ public FileProvider()
5958
if (File.Exists(filePath))
6059
File.Delete(filePath);
6160

62-
using (var file = File.Create(filePath))
63-
{
64-
65-
}
61+
using var file = File.Create(filePath);
6662
}
6763
catch
6864
{
6965

7066
}
71-
finally {
72-
FileProvider.semaphore.Release();
73-
thread = new Thread(runFileThread);
74-
thread.Name = $"{nameof(FileProvider)}-Logging Thread";
75-
thread.IsBackground = true ;
76-
thread.Priority = ThreadPriority.BelowNormal;
77-
thread.Start();
67+
finally {
68+
_ = runFileThread();
7869
}
7970
}
8071

81-
private void runFileThread()
72+
private async Task runFileThread()
8273
{
8374
while (!isDisposing)
8475
{
85-
if(queue.TryDequeue(out var message))
76+
await Task.Delay(1);
77+
while (queue.TryDequeue(out var message))
8678
{
8779
try
8880
{
@@ -98,7 +90,10 @@ private void runFileThread()
9890

9991
private void Log(string message)
10092
{
101-
queue.Enqueue(message);
93+
Task.Run(() =>
94+
{
95+
queue.Enqueue(message);
96+
});
10297
}
10398

10499
public ILogger CreateLogger(string categoryName)
@@ -133,14 +128,14 @@ public bool IsEnabled(LogLevel logLevel)
133128
return true;
134129
}
135130

136-
public async void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
131+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
137132
{
138133
if (string.IsNullOrWhiteSpace(FileProvider.fileDirectory))
139134
return;
140135
if (!Directory.Exists(fileDirectory))
141136
return;
142137

143-
await Task.Run(async () =>
138+
Task.Run(() =>
144139
{
145140
StringBuilder stringBuilder = new StringBuilder();
146141
stringBuilder.AppendLine($"{DateTime.UtcNow} [{logLevel}] <{CategoryName}> {formatter?.Invoke(state, exception)}");

0 commit comments

Comments
 (0)