Skip to content

Commit

Permalink
Added a Delay property.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsm committed May 10, 2022
1 parent 19a0e1e commit ddd3e67
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ public void AddListBytes()
[Benchmark]
public void SetUtf8Validation() => _message.Utf8Validation = false;

[Benchmark]
public void SetDelay() => _message.Delay = 1;

[Benchmark]
public void Destroy() => _message.Dispose();

Expand Down
5 changes: 5 additions & 0 deletions Source/Millistream.Streaming/IMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public interface IMessage
/// </summary>
bool Utf8Validation { get; set; }

/// <summary>
/// Gets or sets the intended delay of the message.
/// </summary>
public byte Delay { get; set; }

/// <summary>
/// Adds a new message to the message handle. If the current active message is empty it will be reused to carry this new message.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Millistream.Streaming/MarketDataFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public string Ciphers
public int Timeout => GetInt32Property(MDF_OPTION.MDF_OPT_TIMEOUT);

/// <summary>
/// Enables or disables delay-mode in where the server adds the intended delay to each message sent. This also enables the client to set the intended delay of the messages the client sends to the server. It's disabled by default.
/// Gets or sets a value indicating whether delay-mode is enabled on the connection. It's disabled by default.
/// </summary>
/// <exception cref="InvalidOperationException">The native value of the <see cref="MDF_OPTION.MDF_OPT_HANDLE_DELAY"/> option cannot be fetched or modified.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="MarketDataFeed{TCallbackData,TStatusCallbackData}"/> instance has been disposed.</exception>
Expand Down
23 changes: 22 additions & 1 deletion Source/Millistream.Streaming/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public unsafe sealed partial class Message : IMessage, IDisposable
private readonly NativeImplementation _nativeImplementation;
private CompressionLevel _compressionLevel = CompressionLevel.Z_BEST_SPEED;
private bool _utf8Validation = true;
private byte _delay;
private bool _isDisposed;

/// <summary>
Expand Down Expand Up @@ -53,7 +54,6 @@ private Message(string nativeLibraryPath, bool validateArgument)
/// <summary>
/// Gets or sets the zlib compression level used for the <see cref="AddString(uint, string)"/> and <see cref="AddString(uint, string, int)"/> methods.
/// </summary>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't support setting the zlib compression level.</exception>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_set_property function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
public CompressionLevel CompressionLevel
Expand Down Expand Up @@ -134,6 +134,27 @@ public bool Utf8Validation
}
}

/// <summary>
/// Gets or sets the intended delay of the message.
/// </summary>
/// <exception cref="InvalidOperationException">The installed version of the native library doesn't include the mdf_message_set_property function.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="Message"/> instance has been disposed.</exception>
public byte Delay
{
get
{
ThrowIfDisposed();
return _delay;
}
set
{
ThrowIfDisposed();
ThrowIfNativeFunctionIsMissing(_nativeImplementation.mdf_message_set_property, nameof(_nativeImplementation.mdf_message_set_property));
if (_nativeImplementation.mdf_message_set_property(Handle, MDF_MSG_OPTION.MDF_MSG_OPT_DELAY, value) == 1)
_delay = value;
}
}

internal IntPtr Handle { get; }

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions Tests/Millistream.Streaming.IntegrationTests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void SetUtf8ValidationTest()
Assert.IsFalse(message.Utf8Validation);
}

[TestMethod]
public void SetDelayTest()
{
using Message message = new Message();
Assert.AreEqual(default, message.Delay);
const byte Delay = byte.MaxValue;
message.Delay = Delay;
Assert.AreEqual(Delay, message.Delay);
}

[TestMethod]
public void AddNumericTest()
{
Expand Down
22 changes: 22 additions & 0 deletions Tests/Millistream.Streaming.UnitTests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ public void GetAndSetUtf8ValidationTest()
_nativeImplementation.Verify();
}

[TestMethod]
public void GetAndSetDelayTest()
{
_nativeImplementation.Setup(x => x.mdf_message_set_property(It.IsAny<IntPtr>(), (int)MDF_MSG_OPTION.MDF_MSG_OPT_DELAY, It.IsAny<int>())).Returns(1)
.Verifiable();
using Message message = new();
Assert.AreEqual(default, message.Delay);
const byte Delay = byte.MaxValue - 10;
message.Delay = Delay;
Assert.AreEqual(Delay, message.Delay);
_nativeImplementation.Verify();
}

[TestMethod]
public void AddTest()
{
Expand Down Expand Up @@ -438,6 +451,14 @@ public void DeserializeEmptyStringTest() =>
[ExpectedException(typeof(ObjectDisposedException))]
public void CannotSetUtf8ValidationAfterDisposeTest() => GetDisposedMessage().Utf8Validation = false;

[TestMethod]
[ExpectedException(typeof(ObjectDisposedException))]
public void CannotGetDelayAfterDisposeTest() => _ = GetDisposedMessage().Delay;

[TestMethod]
[ExpectedException(typeof(ObjectDisposedException))]
public void CannotSetDelayAfterDiposeTest() => GetDisposedMessage().Delay = 1;

[TestMethod]
public void CannotCallMethodsAfterDisposeTest()
{
Expand Down Expand Up @@ -535,6 +556,7 @@ public unsafe void MemberThrowsWhenNativeFunctionIsMissingTest()
using Message message = new(nativeImplementation);
CatchInvalidOperationException(() => message.CompressionLevel = CompressionLevel.Z_BEST_COMPRESSION, nameof(nativeImplementation.mdf_message_set_property));
CatchInvalidOperationException(() => message.Utf8Validation = false, nameof(nativeImplementation.mdf_message_set_property));
CatchInvalidOperationException(() => message.Delay = 1, nameof(nativeImplementation.mdf_message_set_property));
CatchInvalidOperationException(() => message.AddInt64(default(uint), default, default), nameof(nativeImplementation.mdf_message_add_int));
CatchInvalidOperationException(() => message.AddInt64(default(Field), default, default), nameof(nativeImplementation.mdf_message_add_int));
CatchInvalidOperationException(() => message.AddUInt64(default(uint), default, default), nameof(nativeImplementation.mdf_message_add_uint));
Expand Down

0 comments on commit ddd3e67

Please sign in to comment.