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 1 commit
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
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
143 changes: 143 additions & 0 deletions src/NATS.Client/IConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
sspates marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
sspates marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/NATS.Client/ISubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public interface ISubscription : IDisposable
/// <summary>
/// Gets the <see cref="NATS.Client.Connection"/> associated with this instance.
/// </summary>
Connection Connection { get; }
IConnection Connection { get; }
sspates marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets a value indicating whether or not the <see cref="ISubscription"/> is still valid.
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client/Msg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public void Respond(byte[] data)
throw new NATSException("No Reply subject");
}

Connection conn = ArrivalSubscription?.Connection;
IConnection conn = ArrivalSubscription?.Connection;
sspates marked this conversation as resolved.
Show resolved Hide resolved
if (conn == null)
{
throw new NATSException("Message is not bound to a subscription");
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ internal string SubName() => subject + "(" + sid + ")" +
/// <summary>
/// Gets the <see cref="Connection"/> associated with this instance.
/// </summary>
public Connection Connection => conn;
public IConnection Connection => conn;
sspates marked this conversation as resolved.
Show resolved Hide resolved

//caller must lock
internal bool tallyMessage(long bytes)
Expand Down