Skip to content

Commit

Permalink
nullable-ize UnitTests project
Browse files Browse the repository at this point in the history
also deprecate a AcceptorSocketDescriptor ctor due to unused param
  • Loading branch information
gbirchmeier committed Sep 26, 2024
1 parent 6bc3f0b commit 2311a45
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 136 deletions.
12 changes: 10 additions & 2 deletions QuickFIXn/AcceptorSocketDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Net;
using QuickFix.Logger;
Expand All @@ -14,13 +15,20 @@ internal class AcceptorSocketDescriptor
public AcceptorSocketDescriptor(
IPEndPoint socketEndPoint,
SocketSettings socketSettings,
SettingsDictionary sessionDict,
NonSessionLog nonSessionLog)
{
Address = socketEndPoint;
SocketReactor = new ThreadedSocketReactor(Address, socketSettings, sessionDict, this, nonSessionLog);
SocketReactor = new ThreadedSocketReactor(Address, socketSettings, this, nonSessionLog);
}

[Obsolete("Param 'sessionDict' is unused. Use the alt constructor without it.")]
public AcceptorSocketDescriptor(
IPEndPoint socketEndPoint,
SocketSettings socketSettings,
SettingsDictionary sessionDict,
NonSessionLog nonSessionLog) : this(socketEndPoint, socketSettings, nonSessionLog)
{ }

internal void AcceptSession(Session session)
{
lock (_acceptedSessions)
Expand Down
8 changes: 4 additions & 4 deletions QuickFIXn/Fields/Converters/AsciiConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ public static class AsciiValidator
public const int ASCII_NINE = 57;
public const int ASCII_MINUS = 45;

/// <summary>
/// TODO can we use NumberFormatInfo or NumberStyles to avoid this bit of ASCII hackery?
/// Validates that a string looks like number (for use before conversion to an int, ulong, etc.).
/// <summary>
/// Validates that a string looks like a number (for use before conversion to an int, ulong, etc.).
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static void Validate(string i)
{
if ((null == i) || (i.Length < 1))
if (i is null || i.Length < 1)
throw new FieldConvertError("The argument string cannot be null or empty");
int asciiValOfFirstChar = System.Convert.ToInt32(i[0]);
if ((asciiValOfFirstChar < ASCII_ZERO) || (asciiValOfFirstChar > ASCII_NINE))
if (asciiValOfFirstChar < ASCII_ZERO || asciiValOfFirstChar > ASCII_NINE)
if (asciiValOfFirstChar != ASCII_MINUS)
throw new FieldConvertError("Could not convert string to int (" + i + "): The first character must be a digit or a minus sign");
}
Expand Down
2 changes: 1 addition & 1 deletion QuickFIXn/ThreadedSocketAcceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private AcceptorSocketDescriptor GetAcceptorSocketDescriptor(SettingsDictionary

if (!_socketDescriptorForAddress.TryGetValue(socketEndPoint, out var descriptor))
{
descriptor = new AcceptorSocketDescriptor(socketEndPoint, socketSettings, dict, _nonSessionLog);
descriptor = new AcceptorSocketDescriptor(socketEndPoint, socketSettings, _nonSessionLog);
_socketDescriptorForAddress[socketEndPoint] = descriptor;
}

Expand Down
1 change: 0 additions & 1 deletion QuickFIXn/ThreadedSocketReactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public State ReactorState
internal ThreadedSocketReactor(
IPEndPoint serverSocketEndPoint,
SocketSettings socketSettings,
SettingsDictionary sessionDict,
AcceptorSocketDescriptor? acceptorSocketDescriptor,
NonSessionLog nonSessionLog)
{
Expand Down
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ What's New
* deprecate <Foo>Field.Obj (renamed to Value)
* deprecate <Foo>Field.getValue/setValue (just use Value getter/setter)
* #889 - nullable-ize Examples and fix deprecations (gbirchmeier)
* #892 - nullable-ize UnitTests project (gbirchmeier)
* also deprecate a AcceptorSocketDescriptor ctor due to unused param

### v1.12.0

Expand Down
14 changes: 7 additions & 7 deletions UnitTests/DataDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private static XmlNode MakeNode(string xmlString)
if (xmlString.StartsWith('<'))
{
doc.LoadXml(xmlString);
return doc.DocumentElement;
return doc.DocumentElement!;
}
return doc.CreateTextNode(xmlString);
}
Expand All @@ -392,18 +392,18 @@ public void VerifyChildNodeAndReturnNameAtt() {
MakeNode("<sometag name='qty'/>"), parentNode));

DictionaryParseException dpx = Assert.Throws<DictionaryParseException>(
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("foo"), parentNode); });
Assert.AreEqual("Malformed data dictionary: Found text-only node containing 'foo'", dpx!.Message);
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("foo"), parentNode); })!;
Assert.AreEqual("Malformed data dictionary: Found text-only node containing 'foo'", dpx.Message);

dpx = Assert.Throws<DictionaryParseException>(
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("<field>qty</field>"), parentNode); });
Assert.AreEqual("Malformed data dictionary: Found 'field' node without 'name' within parent 'parentnode/Daddy'", dpx!.Message);
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("<field>qty</field>"), parentNode); })!;
Assert.AreEqual("Malformed data dictionary: Found 'field' node without 'name' within parent 'parentnode/Daddy'", dpx.Message);

// alt error message, where parent has no name
parentNode = MakeNode("<parentnode/>");
dpx = Assert.Throws<DictionaryParseException>(
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("<field>qty</field>"), parentNode); });
Assert.AreEqual("Malformed data dictionary: Found 'field' node without 'name' within parent 'parentnode/parentnode'", dpx!.Message);
delegate { DataDictionary.VerifyChildNodeAndReturnNameAtt(MakeNode("<field>qty</field>"), parentNode); })!;
Assert.AreEqual("Malformed data dictionary: Found 'field' node without 'name' within parent 'parentnode/parentnode'", dpx.Message);
}
}
}
22 changes: 11 additions & 11 deletions UnitTests/Fields/Converters/DecimalConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ public class DecimalConverterTests {
[Test]
public void Convert()
{
Assert.That(DecimalConverter.Convert(new Decimal(4.23322)), Is.EqualTo("4.23322"));
Assert.That(DecimalConverter.Convert(new Decimal(-4.23322)), Is.EqualTo("-4.23322"));
Assert.That(DecimalConverter.Convert(4.23322m), Is.EqualTo("4.23322"));
Assert.That(DecimalConverter.Convert(-4.23322m), Is.EqualTo("-4.23322"));
Assert.That(DecimalConverter.Convert("4332.33"), Is.EqualTo(new Decimal(4332.33)));
Assert.That(DecimalConverter.Convert("3.000000000021874E-4"), Is.EqualTo(0.0003000000000021874M));
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert("2.32a34"); });
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert("+1.2"); });
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert("(1.2)"); });
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert(""); });
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert(null); });
Assert.Throws(typeof(FieldConvertError), delegate { DecimalConverter.Convert(null!); });

// check for a different culture than en-XX
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo( "tr-TR" );
Assert.That( DecimalConverter.Convert( "4332.33" ), Is.EqualTo( new Decimal( 4332.33 ) ) );
Assert.That( DecimalConverter.Convert( "-2.33" ), Is.EqualTo( new Decimal( -2.33 ) ) );
Assert.That( DecimalConverter.Convert( new Decimal( 4.23322 ) ), Is.EqualTo( "4.23322" ) );
Assert.That( DecimalConverter.Convert( new Decimal( -4.23322 ) ), Is.EqualTo( "-4.23322" ) );
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("tr-TR");
Assert.That(DecimalConverter.Convert("4332.33"), Is.EqualTo(4332.33m));
Assert.That(DecimalConverter.Convert("-2.33"), Is.EqualTo(-2.33m));
Assert.That(DecimalConverter.Convert(4.23322m), Is.EqualTo("4.23322"));
Assert.That(DecimalConverter.Convert(-4.23322m), Is.EqualTo("-4.23322"));
}

[Test]
public void Convert_WithoutLeadingTrailingZeros()
{
Assert.That(DecimalConverter.Convert("23."), Is.EqualTo(new Decimal(23)));
Assert.That(DecimalConverter.Convert(".23"), Is.EqualTo(new Decimal(0.23)));
Assert.That(DecimalConverter.Convert("-.23"), Is.EqualTo(new Decimal(-0.23)));
Assert.That(DecimalConverter.Convert("23."), Is.EqualTo(23m));
Assert.That(DecimalConverter.Convert(".23"), Is.EqualTo(0.23m));
Assert.That(DecimalConverter.Convert("-.23"), Is.EqualTo(-0.23m));
}
}
2 changes: 1 addition & 1 deletion UnitTests/Fields/Converters/IntConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public void Convert()
Assert.Throws(typeof(FieldConvertError), delegate { IntConverter.Convert("AB"); });
Assert.Throws(typeof(FieldConvertError), delegate { IntConverter.Convert("2.3234"); });
Assert.Throws(typeof(FieldConvertError), delegate { IntConverter.Convert(""); });
Assert.Throws(typeof(FieldConvertError), delegate { IntConverter.Convert(null); });
Assert.Throws(typeof(FieldConvertError), delegate { IntConverter.Convert(null!); });
}
}
43 changes: 20 additions & 23 deletions UnitTests/FileStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
using System.IO;
using NUnit.Framework;
using System.Threading;
using QuickFix;
using QuickFix.Store;

namespace UnitTests
{
[TestFixture]
public class FileStoreTests
{
private FileStore _store;
private FileStoreFactory _factory;
private FileStore? _store;
private FileStoreFactory? _factory;

private QuickFix.SessionSettings _settings;
private QuickFix.SessionID _sessionID;
private QuickFix.SessionSettings _settings = new();
private QuickFix.SessionID _sessionId = new("unset", "unset", "unset");

private string _storeDirectory;
private string _storeDirectory = "unset";

[SetUp]
public void Setup()
Expand All @@ -26,34 +27,30 @@ public void Setup()
if (System.IO.Directory.Exists(_storeDirectory))
System.IO.Directory.Delete(_storeDirectory, true);

_sessionID = new QuickFix.SessionID("FIX.4.2", "SENDERCOMP", "TARGETCOMP");
_sessionId = new QuickFix.SessionID("FIX.4.2", "SENDERCOMP", "TARGETCOMP");

QuickFix.SettingsDictionary config = new QuickFix.SettingsDictionary();
config.SetString(QuickFix.SessionSettings.CONNECTION_TYPE, "initiator");
config.SetString(QuickFix.SessionSettings.FILE_STORE_PATH, _storeDirectory);

_settings = new QuickFix.SessionSettings();
_settings.Set(_sessionID, config);
_settings.Set(_sessionId, config);
_factory = new FileStoreFactory(_settings);

_store = (FileStore)_factory.Create(_sessionID);
_store = (FileStore)_factory.Create(_sessionId);
}

void RebuildStore()
{
if(_store != null)
{
_store.Dispose();
}

_store = (FileStore)_factory.Create(_sessionID);
_store?.Dispose();
_store = (FileStore)_factory!.Create(_sessionId);
}


[TearDown]
public void Teardown()
{
_store.Dispose();
_store!.Dispose();
Directory.Delete(_storeDirectory, true);
}

Expand All @@ -79,7 +76,7 @@ public void GenerateFileNamesTest()
[Test]
public void NextSenderMsgSeqNumTest()
{
Assert.AreEqual(1, _store.NextSenderMsgSeqNum);
Assert.AreEqual(1, _store!.NextSenderMsgSeqNum);
_store.NextSenderMsgSeqNum = 5;
Assert.AreEqual(5, _store.NextSenderMsgSeqNum);
RebuildStore();
Expand All @@ -89,7 +86,7 @@ public void NextSenderMsgSeqNumTest()
[Test]
public void IncNextSenderMsgSeqNumTest()
{
_store.IncrNextSenderMsgSeqNum();
_store!.IncrNextSenderMsgSeqNum();
Assert.AreEqual(2, _store.NextSenderMsgSeqNum);
RebuildStore();
Assert.AreEqual(2, _store.NextSenderMsgSeqNum);
Expand All @@ -98,7 +95,7 @@ public void IncNextSenderMsgSeqNumTest()
[Test]
public void NextTargetMsgSeqNumTest()
{
Assert.AreEqual(1, _store.NextTargetMsgSeqNum);
Assert.AreEqual(1, _store!.NextTargetMsgSeqNum);
_store.NextTargetMsgSeqNum = 6;
Assert.AreEqual(6, _store.NextTargetMsgSeqNum);
RebuildStore();
Expand All @@ -108,7 +105,7 @@ public void NextTargetMsgSeqNumTest()
[Test]
public void IncNextTargetMsgSeqNumTest()
{
_store.IncrNextTargetMsgSeqNum();
_store!.IncrNextTargetMsgSeqNum();
Assert.AreEqual(2, _store.NextTargetMsgSeqNum);
RebuildStore();
Assert.AreEqual(2, _store.NextTargetMsgSeqNum);
Expand All @@ -119,7 +116,7 @@ public void IncNextTargetMsgSeqNumTest()
public void TestSeqNumLimitsForContinuousMarkets()
{
// Given the next seqnums are UInt64.MaxValue - 1
_store.NextSenderMsgSeqNum = System.UInt64.MaxValue - 1;
_store!.NextSenderMsgSeqNum = System.UInt64.MaxValue - 1;
_store.NextTargetMsgSeqNum = _store.NextSenderMsgSeqNum;

// When the next seqnums are incremented
Expand Down Expand Up @@ -157,7 +154,7 @@ public void TestSeqNumLimitsForContinuousMarkets()
public void ResetTest()
{
// seq nums reset
_store.NextTargetMsgSeqNum = 5;
_store!.NextTargetMsgSeqNum = 5;
_store.NextSenderMsgSeqNum = 4;
_store.Reset();
Assert.AreEqual(1, _store.NextTargetMsgSeqNum);
Expand All @@ -179,7 +176,7 @@ public void ResetTest()
[Test]
public void CreationTimeTest()
{
DateTime d1 = _store.CreationTime.Value;
DateTime d1 = _store!.CreationTime!.Value;
RebuildStore();
DateTime d2 = _store.CreationTime.Value;
Util.UtcDateTimeSerializerTests.AssertHackyDateTimeEquality(d1, d2);
Expand All @@ -194,7 +191,7 @@ public void CreationTimeTest()
[Test]
public void GetTest()
{
_store.Set(1, "dude");
_store!.Set(1, "dude");
_store.Set(2, "pude");
_store.Set(3, "ok");
_store.Set(4, "ohai");
Expand Down
43 changes: 19 additions & 24 deletions UnitTests/GroupTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework;
using QuickFix;
using QuickFix.Fields;

Expand All @@ -15,40 +12,38 @@ public void SubGroup()
{
// issue #11 bug, as reported by karabiberoglu's further-down post

QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup noParty = new QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup();
noParty.PartyID = new QuickFix.Fields.PartyID("ABC");
noParty.PartyIDSource = new QuickFix.Fields.PartyIDSource(QuickFix.Fields.PartyIDSource.PROPRIETARY_CUSTOM_CODE);
noParty.PartyRole = new QuickFix.Fields.PartyRole(QuickFix.Fields.PartyRole.CLEARING_FIRM);
QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup noParty = new();
noParty.PartyID = new PartyID("ABC");
noParty.PartyIDSource = new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE);
noParty.PartyRole = new PartyRole(PartyRole.CLEARING_FIRM);

// group in group
QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup.NoPartySubIDsGroup noPartySub = new QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup.NoPartySubIDsGroup();
noPartySub.PartySubID = new QuickFix.Fields.PartySubID("subABC");
noPartySub.PartySubIDType = new QuickFix.Fields.PartySubIDType(QuickFix.Fields.PartySubIDType.FIRM);
QuickFix.FIX44.CollateralInquiry.NoPartyIDsGroup.NoPartySubIDsGroup noPartySub = new();
noPartySub.PartySubID = new PartySubID("subABC");
noPartySub.PartySubIDType = new PartySubIDType(PartySubIDType.FIRM);
noParty.AddGroup(noPartySub);
noPartySub.PartySubID = new QuickFix.Fields.PartySubID("subDEF");
noPartySub.PartySubIDType = new QuickFix.Fields.PartySubIDType(QuickFix.Fields.PartySubIDType.LOCATION);
noPartySub.PartySubID = new PartySubID("subDEF");
noPartySub.PartySubIDType = new PartySubIDType(PartySubIDType.LOCATION);
noParty.AddGroup(noPartySub);

string msgString = noParty.ToString();
string expected = String.Join(Message.SOH, new string[] {
"448=ABC","447=D","452=4",
"802=2", //NoPartySubIDs
"523=subABC","803=1",
"523=subDEF","803=31"
});
string expected = "448=ABC|447=D|452=4|"
+ "802=2|" //NoPartySubIDs
+ "523=subABC|803=1|"
+ "523=subDEF|803=31|";

//Console.WriteLine(msgString);
StringAssert.Contains(expected, msgString);
StringAssert.Contains(expected, msgString.Replace(Message.SOH, '|'));
}

[Test]
public void GroupClone()
{
QuickFix.FIX42.News.LinesOfTextGroup linesGroup = new QuickFix.FIX42.News.LinesOfTextGroup();
linesGroup.Text = new QuickFix.Fields.Text("foo");
linesGroup.EncodedText = new QuickFix.Fields.EncodedText("bar");
QuickFix.FIX42.News.LinesOfTextGroup linesGroup = new();
linesGroup.Text = new Text("foo");
linesGroup.EncodedText = new EncodedText("bar");

QuickFix.FIX42.News.LinesOfTextGroup clone = linesGroup.Clone() as QuickFix.FIX42.News.LinesOfTextGroup;
QuickFix.FIX42.News.LinesOfTextGroup clone = (linesGroup.Clone() as QuickFix.FIX42.News.LinesOfTextGroup)!;

Assert.AreEqual(linesGroup.Text.Value, clone.Text.Value);
Assert.AreEqual(linesGroup.EncodedText.Value, clone.EncodedText.Value);
Expand Down
Loading

0 comments on commit 2311a45

Please sign in to comment.