Skip to content

Commit

Permalink
Better SessionFactoryTest UT
Browse files Browse the repository at this point in the history
and clean up the existing UTs

 connamara#739
  • Loading branch information
gbirchmeier committed Jan 23, 2025
1 parent 7b7ea8a commit 12634a6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 71 deletions.
17 changes: 10 additions & 7 deletions QuickFIXn/CharEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
/// so we don't have to alter a bunch of other classes to pass
/// a CharEncoding reference around.
/// </summary>
public static class CharEncoding
{
public static class CharEncoding {
public const string DefaultCharEncoding = "iso-8859-1";

public static System.Text.Encoding SelectedEncoding { get; private set; }

static CharEncoding()
{
static CharEncoding() {
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
SelectedEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
SelectedEncoding = System.Text.Encoding.GetEncoding(DefaultCharEncoding);
}

public static void ResetToDefaultEncoding() {
SetEncoding(DefaultCharEncoding);
}

public static void SetEncoding(string encoding)
{
public static void SetEncoding(string encoding) {
SelectedEncoding = System.Text.Encoding.GetEncoding(encoding);
}

Expand Down
1 change: 0 additions & 1 deletion UnitTests/SessionDynamicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ private static SettingsDictionary CreateSessionConfig(bool isInitiator)
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
settings.SetString(SessionSettings.HEARTBTINT, "300");
settings.SetString(SessionSettings.ENCODING, "windows-1251");
return settings;
}

Expand Down
131 changes: 68 additions & 63 deletions UnitTests/SessionFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using NUnit.Framework;
using NUnit.Framework;
using QuickFix;
using QuickFix.Fields.Converters;
using QuickFix.Store;
Expand All @@ -10,46 +8,52 @@ namespace UnitTests
[TestFixture]
public class SessionFactoryTest
{
[SetUp,TearDown]
public void TearDown() {
CharEncoding.ResetToDefaultEncoding();
}

private SettingsDictionary CreateMinimalSettings() {
SettingsDictionary rv = new();
rv.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
rv.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
rv.SetString(SessionSettings.HEARTBTINT, "30");
rv.SetString(SessionSettings.START_TIME, "12:00:00");
rv.SetString(SessionSettings.END_TIME, "12:00:00");
return rv;
}

[Test]
public void TestPersistMessages()
{
IApplication app = new NullApplication();
IMessageStoreFactory storeFactory = new MemoryStoreFactory();
SessionFactory factory = new SessionFactory(app, storeFactory);

SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = new SettingsDictionary();
settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
settings.SetString(SessionSettings.HEARTBTINT, "30");
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
Session session = factory.Create(sessionID, settings);
SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();
Session session = factory.Create(sessionId, settings);

//true by default
Assert.That(session.PersistMessages);

settings.SetBool(SessionSettings.PERSIST_MESSAGES, false);
session = factory.Create(sessionID, settings);
session = factory.Create(sessionId, settings);

Assert.That(!session.PersistMessages);
}

[Test]
public void ValidConfiguration()
{
IApplication app = new NullApplication();
IMessageStoreFactory storeFactory = new MemoryStoreFactory();
SessionFactory factory = new SessionFactory(app, storeFactory);

SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = new SettingsDictionary();
settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
settings.SetString(SessionSettings.HEARTBTINT, "30");
SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();

Assert.DoesNotThrow(delegate { factory.Create(sessionID, settings); });
Assert.DoesNotThrow(delegate { factory.Create(sessionId, settings); });
}

[Test]
Expand All @@ -59,17 +63,12 @@ public void StartDayAndEndDayAreDifferent()
IMessageStoreFactory storeFactory = new MemoryStoreFactory();
SessionFactory factory = new SessionFactory(app, storeFactory);

SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = new SettingsDictionary();
settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();
settings.SetString(SessionSettings.START_DAY, "Sun");
settings.SetString(SessionSettings.END_DAY, "Mon");
settings.SetString(SessionSettings.HEARTBTINT, "30");

Assert.DoesNotThrow(delegate { factory.Create(sessionID, settings); });
Assert.DoesNotThrow(delegate { factory.Create(sessionId, settings); });
}

[Test]
Expand All @@ -79,22 +78,17 @@ public void TestExtendedSettings()
IMessageStoreFactory storeFactory = new MemoryStoreFactory();
SessionFactory factory = new SessionFactory(app, storeFactory);

SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = new SettingsDictionary();
settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();
settings.SetString(SessionSettings.SEND_REDUNDANT_RESENDREQUESTS, "Y");
settings.SetString(SessionSettings.RESEND_SESSION_LEVEL_REJECTS, "Y");
settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "millis");
settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
settings.SetString(SessionSettings.HEARTBTINT, "30");
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
settings.SetString(SessionSettings.ENABLE_LAST_MSG_SEQ_NUM_PROCESSED, "Y");
settings.SetString(SessionSettings.MAX_MESSAGES_IN_RESEND_REQUEST, "2500");
settings.SetString(SessionSettings.SEND_LOGOUT_BEFORE_TIMEOUT_DISCONNECT, "Y");
settings.SetString(SessionSettings.IGNORE_POSSDUP_RESEND_REQUESTS, "Y");

Session session = factory.Create(sessionID, settings);
Session session = factory.Create(sessionId, settings);

Assert.That(session.SendRedundantResendRequests);
Assert.That(session.ResendSessionLevelRejects);
Expand All @@ -103,7 +97,7 @@ public void TestExtendedSettings()
settings.SetString(SessionSettings.SEND_REDUNDANT_RESENDREQUESTS, "N");
settings.SetString(SessionSettings.RESEND_SESSION_LEVEL_REJECTS, "N");
settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "nanos");
session = factory.Create(sessionID, settings);
session = factory.Create(sessionId, settings);

Assert.That(!session.SendRedundantResendRequests);
Assert.That(!session.ResendSessionLevelRejects);
Expand All @@ -121,52 +115,63 @@ public void TestTimeStampPrecisionSettings()
new NullApplication(),
new MemoryStoreFactory());

SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = new SettingsDictionary();
settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator");
settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N");
settings.SetString(SessionSettings.HEARTBTINT, "30");
settings.SetString(SessionSettings.START_TIME, "12:00:00");
settings.SetString(SessionSettings.END_TIME, "12:00:00");
SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();

// check default precision
Session session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond );
Session session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond );

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "MiCrOsEcOnD");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Microsecond );
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Microsecond );

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Micro");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Microsecond);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Microsecond);

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Millisecond");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond);

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Milli");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond);

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Nanosecond");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Nanosecond);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Nanosecond);

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Nano");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Nanosecond);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Nanosecond);

settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Second");
session = factory.Create(sessionID, settings);
Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Second);
session = factory.Create(sessionId, settings);
Assert.That(session.TimeStampPrecision == TimeStampPrecision.Second);
}

[Test]
public void TestEncodingSettings(){
public void TestCharEncoding() {
IApplication app = new NullApplication();
IMessageStoreFactory storeFactory = new MemoryStoreFactory();
SessionFactory factory = new SessionFactory(app, storeFactory);

SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET");
SettingsDictionary settings = CreateMinimalSettings();
settings.SetString(SessionSettings.ENCODING, "windows-1251");

// Yes, CharEncoding is a global static at this time. Sorry.

Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo(System.Text.Encoding.GetEncoding("windows-1251")));
// CharEncoding is set to default before this session is created
Assert.That(CharEncoding.DefaultCharEncoding, Is.EqualTo("iso-8859-1"));
Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo(
System.Text.Encoding.GetEncoding(CharEncoding.DefaultCharEncoding)));

// When a session changes the Encoding, it's a global change:
factory.Create(sessionId, settings);
Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo(
System.Text.Encoding.GetEncoding("windows-1251")));
}
}
}

0 comments on commit 12634a6

Please sign in to comment.