Skip to content

Commit

Permalink
Merge pull request #896 from drewnoakes/more-annotations
Browse files Browse the repository at this point in the history
Further nullability annotations
  • Loading branch information
somdoron authored May 27, 2020
2 parents 72f6af5 + 76278aa commit 43428d0
Show file tree
Hide file tree
Showing 43 changed files with 273 additions and 257 deletions.
11 changes: 5 additions & 6 deletions src/NetMQ.Tests/EventDelegatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System;
using System;
using Xunit;

namespace NetMQ.Tests
Expand All @@ -13,12 +11,12 @@ private class Args<T> : EventArgs
public T Value { get; }
}

private event EventHandler<Args<int>> Source;
private event EventHandler<Args<int>>? Source;

[Fact]
public void Basics()
{
EventHandler<Args<int>> sourceHandler = null;
EventHandler<Args<int>>? sourceHandler = null;

var delegator = new EventDelegator<Args<double>>(
() => Source += sourceHandler,
Expand All @@ -31,7 +29,7 @@ public void Basics()
var value = 0.0;
var callCount = 0;

void DelegatorHandler(object sender, Args<double> args)
void DelegatorHandler(object? sender, Args<double> args)
{
value = args.Value;
callCount++;
Expand All @@ -40,6 +38,7 @@ void DelegatorHandler(object sender, Args<double> args)
delegator.Event += DelegatorHandler;

Assert.NotNull(Source);
Assumes.NotNull(Source);

Assert.Equal(0, callCount);

Expand Down
4 changes: 1 addition & 3 deletions src/NetMQ.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable disable

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Xunit;
Expand Down Expand Up @@ -105,7 +103,7 @@ private static void RoundTrip(NetMQException before)
Assert.Equal(before.Message, after.Message);
}

private static T Clone<T>(T source)
private static T Clone<T>(T source) where T : class
{
return Deserialise<T>(Serialise(source));
}
Expand Down
6 changes: 2 additions & 4 deletions src/NetMQ.Tests/MsgTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System;
using System;
using Xunit;

namespace NetMQ.Tests
Expand Down Expand Up @@ -210,7 +208,7 @@ public void InitPool()
Assert.Equal(MsgType.Pool, msg.MsgType);
Assert.Equal(MsgFlags.None, msg.Flags);
Assert.NotNull(msg.UnsafeData);
Assert.Equal(100, msg.UnsafeData.Length);
Assert.Equal(100, msg.UnsafeData!.Length);
Assert.False(msg.HasMore);
Assert.False(msg.IsDelimiter);
Assert.False(msg.IsIdentity);
Expand Down
43 changes: 27 additions & 16 deletions src/NetMQ.Tests/OutgoingSocketExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System;
using System;
using Xunit;

namespace NetMQ.Tests
Expand Down Expand Up @@ -34,16 +32,18 @@ public void SendMultipartBytesTest()
if (count == 0)
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;
}
else
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(2, msg.UnsafeData[0]);
Assert.Equal(2, msg.UnsafeData![0]);
Assert.False(more);
count++;
}
Expand All @@ -65,16 +65,18 @@ public void TrySendMultipartBytesWithTimeoutTest()
if (count == 0)
{
Assert.Equal(TimeSpan.FromSeconds(1), timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;
}
else
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(2, msg.UnsafeData[0]);
Assert.Equal(2, msg.UnsafeData![0]);
Assert.False(more);
count++;
}
Expand All @@ -95,8 +97,9 @@ public void TrySendMultipartBytesWithTimeoutTestFailed()
{

Assert.Equal(TimeSpan.FromSeconds(1), timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;

Expand All @@ -117,16 +120,18 @@ public void TrySendMultipartBytesTest()
if (count == 0)
{
Assert.Equal(TimeSpan.FromSeconds(0), timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;
}
else
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(2, msg.UnsafeData[0]);
Assert.Equal(2, msg.UnsafeData![0]);
Assert.False(more);
count++;
}
Expand All @@ -148,16 +153,18 @@ public void TrySendMultipartMessageTest()
if (count == 0)
{
Assert.Equal(TimeSpan.FromSeconds(0), timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;
}
else
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(2, msg.UnsafeData[0]);
Assert.Equal(2, msg.UnsafeData![0]);
Assert.False(more);
count++;
}
Expand All @@ -181,8 +188,9 @@ public void TrySendMultipartMessageFailed()
var socket = new MockOutgoingSocket((ref Msg msg, TimeSpan timeout, bool more) =>
{
Assert.Equal(TimeSpan.FromSeconds(0), timeout);
Assert.NotNull(msg.UnsafeData);
Assert.Single(msg.UnsafeData);
Assert.Equal(1, msg.UnsafeData[0]);
Assert.Equal(1, msg.UnsafeData![0]);
Assert.True(more);
count++;

Expand Down Expand Up @@ -261,7 +269,8 @@ public void SignalTest()
var socket = new MockOutgoingSocket((ref Msg msg, TimeSpan timeout, bool more) =>
{
Assert.Equal(SendReceiveConstants.InfiniteTimeout, timeout);
Assert.Equal(8, msg.UnsafeData.Length);
Assert.NotNull(msg.UnsafeData);
Assert.Equal(8, msg.UnsafeData!.Length);

var value = NetworkOrderBitsConverter.ToInt64(msg.UnsafeData);

Expand All @@ -280,7 +289,8 @@ public void TrySignalTest()
var socket = new MockOutgoingSocket((ref Msg msg, TimeSpan timeout, bool more) =>
{
Assert.Equal(TimeSpan.Zero, timeout);
Assert.Equal(8, msg.UnsafeData.Length);
Assert.NotNull(msg.UnsafeData);
Assert.Equal(8, msg.UnsafeData!.Length);

var value = NetworkOrderBitsConverter.ToInt64(msg.UnsafeData);

Expand All @@ -299,7 +309,8 @@ public void TrySignalFailedTest()
var socket = new MockOutgoingSocket((ref Msg msg, TimeSpan timeout, bool more) =>
{
Assert.Equal(TimeSpan.Zero, timeout);
Assert.Equal(8, msg.UnsafeData.Length);
Assert.NotNull(msg.UnsafeData);
Assert.Equal(8, msg.UnsafeData!.Length);

var value = NetworkOrderBitsConverter.ToInt64(msg.UnsafeData);

Expand Down
8 changes: 3 additions & 5 deletions src/NetMQ.Tests/ReceivingSocketExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -89,7 +87,7 @@ public void TryReceiveFrameBytes()
{
var expected = m_socket.PushFrame("Hello");

Assert.True(m_socket.TryReceiveFrameBytes(out byte[] actual));
Assert.True(m_socket.TryReceiveFrameBytes(out byte[]? actual));

Assert.Equal(TimeSpan.Zero, m_socket.LastTimeout);
Assert.True(actual.SequenceEqual(expected));
Expand All @@ -107,7 +105,7 @@ public void TryReceiveFrameBytesWithMore()
var expected1 = m_socket.PushFrame("Hello");
var expected2 = m_socket.PushFrame("World");

Assert.True(m_socket.TryReceiveFrameBytes(out byte[] actual, out bool more));
Assert.True(m_socket.TryReceiveFrameBytes(out byte[]? actual, out bool more));

Assert.Equal(TimeSpan.Zero, m_socket.LastTimeout);
Assert.True(actual.SequenceEqual(expected1));
Expand Down
8 changes: 3 additions & 5 deletions src/NetMQ.Tests/RequestWithRetryTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

#if !NET35
#if !NET35
using System;
using System.Diagnostics;
using NetMQ.Sockets;
Expand Down Expand Up @@ -44,7 +42,7 @@ public void RequestResponseMultipartMessageWithRetrySucceedsFirstTry()
var responseMessage = RequestSocket.RequestResponseMultipartMessageWithRetry(address,
requestMessage, numTries, requestTimeout, progressPublisher);
Assert.NotNull(responseMessage);
Assert.Equal(1, responseMessage.FrameCount);
Assert.Equal(1, responseMessage!.FrameCount);
var responseString = responseMessage.First.ConvertToString();
Assert.Equal("Hi", responseString);
}
Expand Down Expand Up @@ -125,7 +123,7 @@ public void RequestResponseMultipartMessageWithRetrySucceedsNotOnFirstTry()
var responseMessage = RequestSocket.RequestResponseMultipartMessageWithRetry(address,
requestMessage, numTries, requestTimeout, progressPublisher);
Assert.NotNull(responseMessage);
Assert.Equal(1, responseMessage.FrameCount);
Assert.Equal(1, responseMessage!.FrameCount);
var responseString = responseMessage.First.ConvertToString();
Assert.Equal("Hi", responseString);
}
Expand Down
14 changes: 6 additions & 8 deletions src/NetMQ.Tests/SocketTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
Expand Down Expand Up @@ -122,12 +120,12 @@ public void ReceiveMessageWithTimeout()
Thread.Sleep(100);
pubSync.Set();

NetMQMessage msg = null;
NetMQMessage? msg = null;
Assert.False(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(100), ref msg));

Assert.True(subSocket.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(waitTime), ref msg));
Assert.NotNull(msg);
Assert.Equal(1, msg.FrameCount);
Assert.Equal(1, msg!.FrameCount);
Assert.Equal(300, msg.First.MessageSize);
pubSync.Set();
}
Expand Down Expand Up @@ -752,9 +750,9 @@ public void InprocRouterDealerTest()
{
for (int i = 0; i < 2; i++)
{
void ThreadMethod(object state)
void ThreadMethod(object? state)
{
byte[] routerId = (byte[]) state;
byte[]? routerId = (byte[]?) state;
byte[] workerId = Guid.NewGuid().ToByteArray();
using (var workerSocket = new DealerSocket())
{
Expand Down Expand Up @@ -891,7 +889,7 @@ public static void Unbind(this NetMQSocket sub)

Assert.NotNull(sub.Options.LastEndpoint);

sub.Unbind(sub.Options.LastEndpoint);
sub.Unbind(sub.Options.LastEndpoint!);
closed.Wait(1000);

monitor.Stop();
Expand Down
10 changes: 5 additions & 5 deletions src/NetMQ.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using Xunit;
using Xunit;

using NetMQ.Sockets;

Expand All @@ -19,7 +17,9 @@ public void StreamToStream()
var port = server.BindRandomPort("tcp://*");
client.Connect("tcp://127.0.0.1:" + port);

byte[] clientId = client.Options.Identity;
byte[]? clientId = client.Options.Identity;

Assert.NotNull(clientId);

const string request = "GET /\r\n";

Expand All @@ -28,7 +28,7 @@ public void StreamToStream()
"\r\n" +
"Hello, World!";

client.SendMoreFrame(clientId).SendFrame(request);
client.SendMoreFrame(clientId!).SendFrame(request);

byte[] serverId = server.ReceiveFrameBytes();
Assert.Equal(request, server.ReceiveFrameString());
Expand Down
7 changes: 3 additions & 4 deletions src/NetMQ.Tests/XPubSubTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable disable

using System.Linq;
using System.Linq;
using System.Threading;
using Xunit;
using NetMQ.Sockets;
Expand Down Expand Up @@ -315,7 +313,8 @@ public void Unsubscribe()
pub.SendMoreFrame("A");
pub.SendFrame("Hello");

Assert.False(sub.TryReceiveFrameString(out string str));
Assert.False(sub.TryReceiveFrameString(out string? str));
Assert.Null(str);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/NetMQ/Core/Mechanisms/CurveClientMechanism.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable disable

using System;
using System.Diagnostics;
using System.Security.Cryptography;
Expand All @@ -26,8 +24,8 @@ enum State

private byte[] m_cnSecretKey;
private byte[] m_cnPublicKey;
private byte[] m_cnServerKey;
private byte[] m_cnCookie;
private byte[]? m_cnServerKey;
private byte[]? m_cnCookie;

private State m_state;

Expand Down Expand Up @@ -246,6 +244,8 @@ PushMsgResult ProcessReady(ref Msg msg)
msg.Slice(6, 8).CopyTo(readyNonce.Slice(16));
m_peerNonce = NetworkOrderBitsConverter.ToUInt64(msg, 6);

Assumes.NotNull(m_box);

bool isDecrypted = m_box.TryDecrypt(readyPlaintext, readyBox, readyNonce);
if (!isDecrypted)
return PushMsgResult.Error;
Expand Down
Loading

0 comments on commit 43428d0

Please sign in to comment.