Skip to content

Commit

Permalink
Enable nullables on Common and CommunicationUtilities test projects (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Mar 30, 2022
1 parent 8115ce8 commit e033f6e
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace TestPlatform.Common.UnitTests.Utilities;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.PlatformTests;

[TestClass]
Expand All @@ -24,7 +22,7 @@ public class SocketClientTests : SocketTestsBase, IDisposable

private readonly ICommunicationEndPoint _socketClient;

private TcpClient _tcpClient;
private TcpClient? _tcpClient;

public SocketClientTests()
{
Expand All @@ -34,7 +32,7 @@ public SocketClientTests()
_tcpListener = new TcpListener(endpoint);
}

protected override TcpClient Client => _tcpClient;
protected override TcpClient? Client => _tcpClient;

public void Dispose()
{
Expand Down Expand Up @@ -104,7 +102,7 @@ public void SocketClientShouldRaiseClientDisconnectedEventIfConnectionIsBroken()
var waitEvent = SetupClientDisconnect(out ICommunicationChannel _);

// Close the communication from server side
_tcpClient.GetStream().Dispose();
_tcpClient?.GetStream().Dispose();
#if NETFRAMEWORK
// tcpClient.Close() calls tcpClient.Dispose().
_tcpClient?.Close();
Expand All @@ -125,24 +123,24 @@ public void SocketClientStopShouldStopCommunication()

// Validate that write on server side fails
waitEvent.WaitOne(Timeout);
Assert.ThrowsException<IOException>(() => WriteData(Client));
Assert.ThrowsException<IOException>(() => WriteData(Client!));
}

[TestMethod]
public void SocketClientStopShouldCloseChannel()
{
var waitEvent = SetupClientDisconnect(out ICommunicationChannel channel);
var waitEvent = SetupClientDisconnect(out ICommunicationChannel? channel);

_socketClient.Stop();

waitEvent.WaitOne(Timeout);
Assert.ThrowsException<CommunicationException>(() => channel.Send(Dummydata));
Assert.ThrowsException<CommunicationException>(() => channel!.Send(Dummydata));
}

protected override ICommunicationChannel SetupChannel(out ConnectedEventArgs connectedEvent)
protected override ICommunicationChannel? SetupChannel(out ConnectedEventArgs? connectedEvent)
{
ICommunicationChannel channel = null;
ConnectedEventArgs serverConnectedEvent = null;
ICommunicationChannel? channel = null;
ConnectedEventArgs? serverConnectedEvent = null;
ManualResetEvent waitEvent = new(false);
_socketClient.Connected += (sender, eventArgs) =>
{
Expand All @@ -165,12 +163,12 @@ protected override ICommunicationChannel SetupChannel(out ConnectedEventArgs con
return channel;
}

private ManualResetEvent SetupClientDisconnect(out ICommunicationChannel channel)
private ManualResetEvent SetupClientDisconnect(out ICommunicationChannel? channel)
{
var waitEvent = new ManualResetEvent(false);
_socketClient.Disconnected += (s, e) => waitEvent.Set();
channel = SetupChannel(out ConnectedEventArgs _);
channel.MessageReceived += (sender, args) =>
channel!.MessageReceived += (sender, args) =>
{
};
return waitEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.PlatformTests;

[TestClass]
Expand Down Expand Up @@ -241,7 +239,7 @@ public async Task ReceiveMessageAsyncShouldReceiveDeserializedMessage()
WriteToStream(client.GetStream(), TestDiscoveryStartMessageWithVersionAndPayload);

var message = await _communicationManager.ReceiveMessageAsync(CancellationToken.None);
var versionedMessage = message as VersionedMessage;
var versionedMessage = (VersionedMessage)message;
Assert.AreEqual(MessageType.StartDiscovery, versionedMessage.MessageType);
Assert.AreEqual(DummyPayload, versionedMessage.Payload);
Assert.AreEqual(2, versionedMessage.Version);
Expand Down Expand Up @@ -350,21 +348,21 @@ private async Task<TcpClient> StartServerAndWaitForConnection()
return client;
}

private void WriteOnSocket(Socket socket)
private static void WriteOnSocket(Socket socket)
{
for (int i = 0; i < 10; i++)
{
socket.Send(new byte[2] { 0x1, 0x0 });
}
}

private string ReadFromStream(Stream stream)
private static string ReadFromStream(Stream stream)
{
using var reader = new BinaryReader(stream, Encoding.UTF8, true);
return reader.ReadString();
}

private void WriteToStream(Stream stream, string data)
private static void WriteToStream(Stream stream, string data)
{
using var writer = new BinaryWriter(stream, Encoding.UTF8, true);
writer.Write(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.PlatformTests;

[TestClass]
Expand Down Expand Up @@ -80,7 +78,7 @@ public void SocketServerStopShouldCloseClient()
{
ManualResetEvent waitEvent = new(false);
_socketServer.Disconnected += (s, e) => waitEvent.Set();
SetupChannel(out ConnectedEventArgs clientConnected);
SetupChannel(out ConnectedEventArgs? clientConnected);

_socketServer.Stop();

Expand All @@ -91,14 +89,14 @@ public void SocketServerStopShouldCloseClient()
[TestMethod]
public void SocketServerStopShouldRaiseClientDisconnectedEventOnClientDisconnection()
{
DisconnectedEventArgs disconnected = null;
DisconnectedEventArgs? disconnected = null;
ManualResetEvent waitEvent = new(false);
_socketServer.Disconnected += (s, e) =>
{
disconnected = e;
waitEvent.Set();
};
SetupChannel(out ConnectedEventArgs clientConnected);
SetupChannel(out ConnectedEventArgs? clientConnected);

_socketServer.Stop();

Expand All @@ -111,28 +109,28 @@ public void SocketServerStopShouldRaiseClientDisconnectedEventOnClientDisconnect
public void SocketServerStopShouldCloseChannel()
{
var waitEvent = new ManualResetEventSlim(false);
var channel = SetupChannel(out ConnectedEventArgs clientConnected);
var channel = SetupChannel(out ConnectedEventArgs? clientConnected);
_socketServer.Disconnected += (s, e) => waitEvent.Set();

_socketServer.Stop();

waitEvent.Wait();
Assert.ThrowsException<CommunicationException>(() => channel.Send(Dummydata));
Assert.ThrowsException<CommunicationException>(() => channel!.Send(Dummydata));
}

[TestMethod]
public void SocketServerShouldRaiseClientDisconnectedEventIfConnectionIsBroken()
{
DisconnectedEventArgs clientDisconnected = null;
DisconnectedEventArgs? clientDisconnected = null;
ManualResetEvent waitEvent = new(false);
_socketServer.Disconnected += (sender, eventArgs) =>
{
clientDisconnected = eventArgs;
waitEvent.Set();
};
var channel = SetupChannel(out ConnectedEventArgs clientConnected);
var channel = SetupChannel(out ConnectedEventArgs? clientConnected);

channel.MessageReceived += (sender, args) =>
channel!.MessageReceived += (sender, args) =>
{
};

Expand All @@ -145,23 +143,23 @@ public void SocketServerShouldRaiseClientDisconnectedEventIfConnectionIsBroken()
_tcpClient?.Dispose();
#endif
Assert.IsTrue(waitEvent.WaitOne(1000));
Assert.IsTrue(clientDisconnected.Error is IOException);
Assert.IsTrue(clientDisconnected!.Error is IOException);
}

[TestMethod]
public async Task SocketEndpointShouldInitializeChannelOnServerConnection()
{
var channel = SetupChannel(out ConnectedEventArgs _);

await channel.Send(Dummydata);
await channel!.Send(Dummydata);

Assert.AreEqual(Dummydata, ReadData(Client));
}

protected override ICommunicationChannel SetupChannel(out ConnectedEventArgs connectedEvent)
protected override ICommunicationChannel? SetupChannel(out ConnectedEventArgs? connectedEvent)
{
ICommunicationChannel channel = null;
ConnectedEventArgs clientConnectedEvent = null;
ICommunicationChannel? channel = null;
ConnectedEventArgs? clientConnectedEvent = null;
ManualResetEvent waitEvent = new(false);
_socketServer.Connected += (sender, eventArgs) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.PlatformTests;

[TestClass]
Expand All @@ -19,12 +17,12 @@ public abstract class SocketTestsBase
protected const string Dummydata = "Dummy Data";
protected const int Timeout = 10 * 1000;

protected abstract TcpClient Client { get; }
protected abstract TcpClient? Client { get; }

[TestMethod]
public void SocketEndpointStartShouldRaiseServerConnectedEventOnServerConnection()
{
SetupChannel(out ConnectedEventArgs connectedEventArgs);
SetupChannel(out ConnectedEventArgs? connectedEventArgs);

Assert.IsNotNull(connectedEventArgs);
}
Expand All @@ -34,13 +32,13 @@ public void SocketEndpointShouldNotifyChannelOnDataAvailable()
{
var message = string.Empty;
ManualResetEvent waitForMessage = new(false);
SetupChannel(out ConnectedEventArgs _).MessageReceived += (s, e) =>
SetupChannel(out ConnectedEventArgs? _)!.MessageReceived += (s, e) =>
{
message = e.Data;
waitForMessage.Set();
};

WriteData(Client);
WriteData(Client!);

waitForMessage.WaitOne();
Assert.AreEqual(Dummydata, message);
Expand All @@ -58,5 +56,5 @@ protected static void WriteData(TcpClient client)
writer.Write(Dummydata);
}

protected abstract ICommunicationChannel SetupChannel(out ConnectedEventArgs connectedEventArgs);
protected abstract ICommunicationChannel? SetupChannel(out ConnectedEventArgs? connectedEventArgs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

using Newtonsoft.Json.Linq;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

using Moq;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

using Newtonsoft.Json.Linq;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

using Newtonsoft.Json.Linq;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

using TestResult = Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult;

#nullable disable

namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand All @@ -40,7 +38,7 @@ public void SerializePayloadShouldNotPickDefaultSettings()

var classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(null);
classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(classWithSelfReferencingLoop);
classWithSelfReferencingLoop.InfiniteRefernce.InfiniteRefernce = classWithSelfReferencingLoop;
classWithSelfReferencingLoop.InfiniteRefernce!.InfiniteRefernce = classWithSelfReferencingLoop;

string serializedPayload = _jsonDataSerializer.SerializePayload("dummy", classWithSelfReferencingLoop);
Assert.AreEqual("{\"MessageType\":\"dummy\",\"Payload\":{\"InfiniteRefernce\":{}}}", serializedPayload);
Expand All @@ -66,7 +64,7 @@ public void SerializePayloadShouldSerializeAnObjectWithSelfReferencingLoop()
{
var classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(null);
classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(classWithSelfReferencingLoop);
classWithSelfReferencingLoop.InfiniteRefernce.InfiniteRefernce = classWithSelfReferencingLoop;
classWithSelfReferencingLoop.InfiniteRefernce!.InfiniteRefernce = classWithSelfReferencingLoop;

// This line should not throw exception
_jsonDataSerializer.SerializePayload("dummy", classWithSelfReferencingLoop);
Expand All @@ -77,7 +75,7 @@ public void DeserializeShouldDeserializeAnObjectWhichHadSelfReferencingLoopBefor
{
var classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(null);
classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(classWithSelfReferencingLoop);
classWithSelfReferencingLoop.InfiniteRefernce.InfiniteRefernce = classWithSelfReferencingLoop;
classWithSelfReferencingLoop.InfiniteRefernce!.InfiniteRefernce = classWithSelfReferencingLoop;

var json = _jsonDataSerializer.SerializePayload("dummy", classWithSelfReferencingLoop);

Expand All @@ -91,7 +89,7 @@ public void DeserializeShouldDeserializeAnObjectWhichHadSelfReferencingLoopBefor
[TestMethod]
public void CloneShouldReturnNullForNull()
{
var clonedTestCase = _jsonDataSerializer.Clone<TestCase>(null);
var clonedTestCase = _jsonDataSerializer.Clone<TestCase>(null!);

Assert.IsNull(clonedTestCase);
}
Expand Down Expand Up @@ -169,15 +167,11 @@ private static void VerifyTestCaseClone(TestCase clonedTestCase, TestCase testCa

public class ClassWithSelfReferencingLoop
{
public ClassWithSelfReferencingLoop(ClassWithSelfReferencingLoop ir)
public ClassWithSelfReferencingLoop(ClassWithSelfReferencingLoop? ir)
{
InfiniteRefernce = ir;
}

public ClassWithSelfReferencingLoop InfiniteRefernce
{
get;
set;
}
public ClassWithSelfReferencingLoop? InfiniteRefernce { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;

#nullable disable

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.UnitTests;

[TestClass]
Expand Down

0 comments on commit e033f6e

Please sign in to comment.