Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustments to interfaces to improve the mockability and faking of NATS for unit testing in consumer apps. #654

Merged
merged 6 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/NATS.Client/AsyncSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ internal override bool processMsg(Msg msg)
{
if (localHandler != null)
{
var msgHandlerEventArgs = new MsgHandlerEventArgs();
msgHandlerEventArgs.msg = msg;
var msgHandlerEventArgs = new MsgHandlerEventArgs(msg);

localHandler(this, msgHandlerEventArgs);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace NATS.Client
/// <summary>
/// Provides factory methods to create connections to NATS Servers.
/// </summary>
public sealed class ConnectionFactory
public sealed class ConnectionFactory : IConnectionFactory
{
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionFactory"/> class,
Expand Down
8 changes: 4 additions & 4 deletions src/NATS.Client/EncodedConn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void msgHandlerToEncoderHandler(Object sender, MsgHandlerEventArgs args)
EncodedMessageEventArgs ehev = new EncodedMessageEventArgs();
try
{
byte[] data = args.msg != null ? args.msg.Data : null;
byte[] data = args.Message != null ? args.Message.Data : null;
ehev.obj = c.onDeserialize(data);
}
catch (Exception ex)
Expand All @@ -243,9 +243,9 @@ public void msgHandlerToEncoderHandler(Object sender, MsgHandlerEventArgs args)
return;
}

ehev.subject = args.msg.Subject;
ehev.reply = args.msg.Reply;
ehev.msg = args.msg;
ehev.subject = args.Message.Subject;
ehev.reply = args.Message.Reply;
ehev.msg = args.Message;

try
{
Expand Down
156 changes: 156 additions & 0 deletions src/NATS.Client/IConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2015-2022 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;

namespace NATS.Client
{
public interface IConnectionFactory
{
/// <summary>
/// Attempt to connect to the NATS server referenced by <paramref name="url"/>.
/// </summary>
/// <remarks>
/// <para><paramref name="url"/> can contain username/password semantics.
/// Comma seperated arrays are also supported, e.g. <c>&quot;urlA, urlB&quot;</c>.</para>
/// </remarks>
/// <param name="url">A string containing the URL (or URLs) to the NATS Server. See the Remarks
/// section for more information.</param>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IConnection CreateConnection(string url);

/// <summary>
/// Attempt to connect to the NATS server referenced by <paramref name="url"/> with NATS 2.0 credentials.
/// </summary>
/// <remarks>
/// <para><paramref name="url"/>
/// Comma seperated arrays are also supported, e.g. <c>&quot;urlA, urlB&quot;</c>.</para>
/// </remarks>
/// <param name="url">A string containing the URL (or URLs) to the NATS Server. See the Remarks
/// section for more information.</param>
/// <param name="credentialsPath">The full path to a chained credentials file.</param>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IConnection CreateConnection(string url, string credentialsPath);

/// <summary>
/// Attempt to connect to the NATS server referenced by <paramref name="url"/> with NATS 2.0 credentials.
/// </summary>
/// <remarks>
/// <para><paramref name="url"/>
/// Comma seperated arrays are also supported, e.g. <c>&quot;urlA, urlB&quot;</c>.</para>
/// </remarks>
/// <param name="url">A string containing the URL (or URLs) to the NATS Server. See the Remarks
/// section for more information.</param>
/// <param name="jwt">The path to a user's public JWT credentials.</param>
/// <param name="privateNkey">The path to a file for user user's private Nkey seed.</param>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IConnection CreateConnection(string url, string jwt, string privateNkey);

/// <summary>
/// Create a connection to the NATs server using the default options.
/// </summary>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
/// <seealso cref="ConnectionFactory.GetDefaultOptions"/>
IConnection CreateConnection();

/// <summary>
/// Create a connection to a NATS Server defined by the given options.
/// </summary>
/// <param name="opts">The NATS client options to use for this connection.</param>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IConnection CreateConnection(Options opts);

/// <summary>
/// Attempt to connect to the NATS server using TLS referenced by <paramref name="url"/>.
/// </summary>
/// <remarks>
/// <para><paramref name="url"/> can contain username/password semantics.
/// Comma seperated arrays are also supported, e.g. urlA, urlB.</para>
/// </remarks>
/// <param name="url">A string containing the URL (or URLs) to the NATS Server. See the Remarks
/// section for more information.</param>
/// <returns>An <see cref="IConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IConnection CreateSecureConnection(string url);

/// <summary>
/// Attempt to connect to the NATS server, with an encoded connection, using the default options.
/// </summary>
/// <returns>An <see cref="IEncodedConnection"/> object connected to the NATS server.</returns>
/// <seealso cref="ConnectionFactory.GetDefaultOptions"/>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IEncodedConnection CreateEncodedConnection();

/// <summary>
/// Attempt to connect to the NATS server, with an encoded connection, referenced by <paramref name="url"/>.
/// </summary>
/// <remarks>
/// <para><paramref name="url"/> can contain username/password semantics.
/// Comma seperated arrays are also supported, e.g. urlA, urlB.</para>
/// </remarks>
/// <param name="url">A string containing the URL (or URLs) to the NATS Server. See the Remarks
/// section for more information.</param>
/// <returns>An <see cref="IEncodedConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IEncodedConnection CreateEncodedConnection(string url);

/// <summary>
/// Attempt to connect to the NATS server, with an encoded connection, using the given options.
/// </summary>
/// <param name="opts">The NATS client options to use for this connection.</param>
/// <returns>An <see cref="IEncodedConnection"/> object connected to the NATS server.</returns>
/// <exception cref="NATSNoServersException">No connection to a NATS Server could be established.</exception>
/// <exception cref="NATSConnectionException"><para>A timeout occurred connecting to a NATS Server.</para>
/// <para>-or-</para>
/// <para>An exception was encountered while connecting to a NATS Server. See <see cref="Exception.InnerException"/> for more
/// details.</para></exception>
IEncodedConnection CreateEncodedConnection(Options opts);
}
}
2 changes: 1 addition & 1 deletion src/NATS.Client/KeyValue/KeyValueWatchSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public KeyValueWatchSubscription(KeyValue kv, string keyPattern,

EventHandler<MsgHandlerEventArgs> handler = (sender, args) =>
{
KeyValueEntry kve = new KeyValueEntry(args.msg);
KeyValueEntry kve = new KeyValueEntry(args.Message);
if (includeDeletes || kve.Operation.Equals(KeyValueOperation.Put))
{
watcher.Watch(kve);
Expand Down
11 changes: 6 additions & 5 deletions src/NATS.Client/NATS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,15 +496,16 @@ internal class IC
/// </summary>
public class MsgHandlerEventArgs : EventArgs
{
internal Msg msg = null;
public MsgHandlerEventArgs(Msg message)
{
Message = message;
}


/// <summary>
/// Retrieves the message.
/// </summary>
public Msg Message
{
get { return msg; }
}
public Msg Message { get; }
}

// Borrowed from: https://stackoverflow.com/a/7135008
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/IntegrationTestsInternal/TestOrderedConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ void DummyTestHandler(object sender, MsgHandlerEventArgs args) { }
void TestHandler(object sender, MsgHandlerEventArgs args)
{
int i = received.Increment() - 1;
ssFlags[i] = new InterlockedLong((long)args.msg.MetaData.StreamSequence);
csFlags[i] = new InterlockedLong((long)args.msg.MetaData.ConsumerSequence);
ssFlags[i] = new InterlockedLong((long)args.Message.MetaData.StreamSequence);
csFlags[i] = new InterlockedLong((long)args.Message.MetaData.ConsumerSequence);
latch.Signal();
}

Expand Down