Skip to content

Commit

Permalink
general testing improvements (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Aug 17, 2022
1 parent 6ab6814 commit e931045
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 70 deletions.
24 changes: 12 additions & 12 deletions src/Tests/IntegrationTests/TestBasic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TestBasic(DefaultSuiteContext context) : base(context) { }
[Fact]
public void TestConnectedServer()
{
using (NATSServer.CreateFastAndVerify())
using (NATSServer.CreateFastAndVerifyQuietHandlers())
{
using (var c = Context.OpenConnection())
{
Expand All @@ -54,7 +54,7 @@ public void TestConnectedServer()
[Fact]
public void TestMultipleClose()
{
using (NATSServer.CreateFastAndVerify())
using (NATSServer.CreateFastAndVerifyQuietHandlers())
{
using (var c = Context.OpenConnection())
{
Expand All @@ -73,7 +73,7 @@ public void TestMultipleClose()
[Fact]
public void TestSimplePublish()
{
using (NATSServer.CreateFastAndVerify())
using (NATSServer.CreateFastAndVerifyQuietHandlers())
{
using (IConnection c = Context.OpenConnection())
{
Expand All @@ -85,7 +85,7 @@ public void TestSimplePublish()
[Fact]
public void TestSimplePublishNoData()
{
using (NATSServer.CreateFastAndVerify())
using (NATSServer.CreateFastAndVerifyQuietHandlers())
{
using (IConnection c = Context.OpenConnection())
{
Expand All @@ -109,7 +109,7 @@ public void TestSimplePublishNoData()
[Fact]
public void TestPublishDataWithOffsets()
{
using (NATSServer.CreateFastAndVerify())
using (NATSServer.CreateFastAndVerifyQuietHandlers())
{
using (IConnection c = Context.OpenConnection())
{
Expand Down Expand Up @@ -153,13 +153,13 @@ private bool compare(byte[] p1, byte[] p2)
return true;
}

private bool compare(byte[] payload, Msg m)
private bool Compare(byte[] payload, Msg m)
{
return compare(payload, m.Data);
}

// Compares a subset of expected (offset to count) against all of actual
private bool compare(byte[] expected, int offset, byte[] actual, int count)
private static bool compare(byte[] expected, int offset, byte[] actual, int count)
{
// null case
if (expected == actual)
Expand Down Expand Up @@ -251,12 +251,12 @@ public void TestSyncSubscribe()
c.Publish("foo", omsg);
Msg m = s.NextMessage(1000);

Assert.True(compare(omsg, m), "Messages are not equal.");
Assert.True(Compare(omsg, m), "Messages are not equal.");

c.Publish("foo", omsg, 0, omsg.Length);
m = s.NextMessage(1000);

Assert.True(compare(omsg, m), "Messages are not equal.");
Assert.True(Compare(omsg, m), "Messages are not equal.");

c.Publish("foo", omsg, 2, 3);
m = s.NextMessage(1000);
Expand All @@ -279,12 +279,12 @@ public void TestPubWithReply()
c.Publish("foo", "reply", omsg);
Msg m = s.NextMessage(1000);

Assert.True(compare(omsg, m), "Messages are not equal.");
Assert.True(Compare(omsg, m), "Messages are not equal.");

c.Publish("foo", "reply", omsg, 0, omsg.Length);
m = s.NextMessage(1000);

Assert.True(compare(omsg, m), "Messages are not equal.");
Assert.True(Compare(omsg, m), "Messages are not equal.");

c.Publish("foo", "reply", omsg, 1, 5);
m = s.NextMessage(1000);
Expand Down Expand Up @@ -2096,7 +2096,7 @@ public void TestMessageHeaderNoServerSupport()
c.Publish(m);
}
}

} // class

} // namespace
78 changes: 28 additions & 50 deletions src/Tests/IntegrationTests/TestJetStreamManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using NATS.Client;
using NATS.Client.Internals;
using NATS.Client.JetStream;
using Xunit;
using Xunit.Abstractions;
using static IntegrationTests.JetStreamTestBase;
using static UnitTests.TestBase;

namespace IntegrationTests
{
public class TestJetStreamManagement : TestSuite<JetStreamManagementSuiteContext>
{
private readonly ITestOutputHelper output;

public TestJetStreamManagement(ITestOutputHelper output, JetStreamManagementSuiteContext context) :
base(context)
{
this.output = output;
Console.SetOut(new ConsoleWriter(output));
}
public TestJetStreamManagement(JetStreamManagementSuiteContext context) : base(context) {}

[Fact]
public void TestStreamCreate()
{
Context.RunInJsServer(c =>
{
DateTime utcNow = DateTime.UtcNow;

IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

StreamConfiguration sc = StreamConfiguration.Builder()
Expand All @@ -52,8 +41,6 @@ public void TestStreamCreate()
.Build();

StreamInfo si = jsm.AddStream(sc);
Assert.True(utcNow <= si.Created.ToUniversalTime());

Assert.NotNull(si.Config);
sc = si.Config;
Assert.Equal(STREAM, sc.Name);
Expand Down Expand Up @@ -91,8 +78,6 @@ public void TestStreamCreate()
public void TestStreamCreateWithNoSubject() {
Context.RunInJsServer(c =>
{
DateTime utcNow = DateTime.UtcNow;

IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

StreamConfiguration sc = StreamConfiguration.Builder()
Expand All @@ -101,8 +86,6 @@ public void TestStreamCreateWithNoSubject() {
.Build();

StreamInfo si = jsm.AddStream(sc);
Assert.True(utcNow <= si.Created.ToUniversalTime());

sc = si.Config;
Assert.Equal(STREAM, sc.Name);

Expand Down Expand Up @@ -228,10 +211,10 @@ public void TestAddUpdateStreamInvalids()
Assert.Throws<NATSJetStreamException>(() => jsm.UpdateStream(scMaxCon));

// cannot change RetentionPolicy
StreamConfiguration scReten = GetTestStreamConfigurationBuilder()
StreamConfiguration scRetention = GetTestStreamConfigurationBuilder()
.WithRetentionPolicy(RetentionPolicy.Interest)
.Build();
Assert.Throws<NATSJetStreamException>(() => jsm.UpdateStream(scReten));
Assert.Throws<NATSJetStreamException>(() => jsm.UpdateStream(scRetention));
});
}

Expand Down Expand Up @@ -524,12 +507,12 @@ private void AssertInvalidConsumerUpdate(IJetStreamManagement jsm, ConsumerConfi

private void AssertValidAddOrUpdate(IJetStreamManagement jsm, ConsumerConfiguration cc) {
ConsumerInfo ci = jsm.AddOrUpdateConsumer(STREAM, cc);
ConsumerConfiguration cicc = ci.ConsumerConfiguration;
ConsumerConfiguration ciCc = ci.ConsumerConfiguration;
Assert.Equal(cc.Durable, ci.Name);
Assert.Equal(cc.Durable, cicc.Durable);
Assert.Equal(cc.DeliverSubject, cicc.DeliverSubject);
Assert.Equal(cc.MaxDeliver, cicc.MaxDeliver);
Assert.Equal(cc.DeliverPolicy, cicc.DeliverPolicy);
Assert.Equal(cc.Durable, ciCc.Durable);
Assert.Equal(cc.DeliverSubject, ciCc.DeliverSubject);
Assert.Equal(cc.MaxDeliver, ciCc.MaxDeliver);
Assert.Equal(cc.DeliverPolicy, ciCc.DeliverPolicy);

IList<string> consumers = jsm.GetConsumerNames(STREAM);
Assert.Single(consumers);
Expand Down Expand Up @@ -670,10 +653,8 @@ public void TestDeleteMessage() {
CreateDefaultTestStream(c);
IJetStream js = c.CreateJetStreamContext();

MsgHeader h = new MsgHeader();
h.Add("foo", "bar");
MsgHeader h = new MsgHeader { { "foo", "bar" } };

DateTime beforeCreated = DateTime.UtcNow; //MessageInfo.Time is in UTC
js.Publish(new Msg(SUBJECT, null, h, DataBytes(1)));
js.Publish(new Msg(SUBJECT, null));

Expand All @@ -683,15 +664,13 @@ public void TestDeleteMessage() {
Assert.Equal(SUBJECT, mi.Subject);
Assert.Equal(Data(1), Encoding.ASCII.GetString(mi.Data));
Assert.Equal(1U, mi.Sequence);
Assert.True(SameOrAfter(mi.Time, beforeCreated));
Assert.NotNull(mi.Headers);
Assert.Equal("bar", mi.Headers["foo"]);

mi = jsm.GetMessage(STREAM, 2);
Assert.Equal(SUBJECT, mi.Subject);
Assert.Null(mi.Data);
Assert.Equal(2U, mi.Sequence);
Assert.True(SameOrAfter(mi.Time, beforeCreated));
Assert.Null(mi.Headers);

Assert.True(jsm.DeleteMessage(STREAM, 1, false)); // added coverage for use of erase (no_erase) flag.
Expand Down Expand Up @@ -785,46 +764,46 @@ public void TestGetMessage()
.WithSubjects(Subject(1), Subject(2))
.Build();
StreamInfo si = jsm.AddStream(sc);
Assert.False(si.Config.AllowDirect);

DateTime beforeCreated = DateTime.UtcNow;
js.Publish(Subject(1), Encoding.UTF8.GetBytes("s1-q1"));
js.Publish(Subject(2), Encoding.UTF8.GetBytes("s2-q2"));
js.Publish(Subject(1), Encoding.UTF8.GetBytes("s1-q3"));
js.Publish(Subject(2), Encoding.UTF8.GetBytes("s2-q4"));
js.Publish(Subject(1), Encoding.UTF8.GetBytes("s1-q5"));
js.Publish(Subject(2), Encoding.UTF8.GetBytes("s2-q6"));

ValidateGetMessage(jsm, si, false, beforeCreated);
ValidateGetMessage(jsm);

sc = StreamConfiguration.Builder(si.Config).WithAllowDirect(true).Build();
si = jsm.UpdateStream(sc);
ValidateGetMessage(jsm, si, true, beforeCreated);
Assert.True(si.Config.AllowDirect);
ValidateGetMessage(jsm);

// error case stream doesn't exist
Assert.Throws<NATSJetStreamException>(() => jsm.GetMessage(Stream(999), 1));
});
}

private void ValidateGetMessage(IJetStreamManagement jsm, StreamInfo si, bool allowDirect, DateTime beforeCreated) {
Assert.Equal(allowDirect, si.Config.AllowDirect);
private void ValidateGetMessage(IJetStreamManagement jsm) {

AssertMessageInfo(1, 1, jsm.GetMessage(STREAM, 1), beforeCreated);
AssertMessageInfo(1, 5, jsm.GetLastMessage(STREAM, Subject(1)), beforeCreated);
AssertMessageInfo(2, 6, jsm.GetLastMessage(STREAM, Subject(2)), beforeCreated);
AssertMessageInfo(1, 1, jsm.GetMessage(STREAM, 1));
AssertMessageInfo(1, 5, jsm.GetLastMessage(STREAM, Subject(1)));
AssertMessageInfo(2, 6, jsm.GetLastMessage(STREAM, Subject(2)));

AssertMessageInfo(1, 1, jsm.GetNextMessage(STREAM, 0, Subject(1)), beforeCreated);
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 0, Subject(2)), beforeCreated);
AssertMessageInfo(1, 1, jsm.GetFirstMessage(STREAM, Subject(1)), beforeCreated);
AssertMessageInfo(2, 2, jsm.GetFirstMessage(STREAM, Subject(2)), beforeCreated);
AssertMessageInfo(1, 1, jsm.GetNextMessage(STREAM, 0, Subject(1)));
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 0, Subject(2)));
AssertMessageInfo(1, 1, jsm.GetFirstMessage(STREAM, Subject(1)));
AssertMessageInfo(2, 2, jsm.GetFirstMessage(STREAM, Subject(2)));

AssertMessageInfo(1, 1, jsm.GetNextMessage(STREAM, 1, Subject(1)), beforeCreated);
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 1, Subject(2)), beforeCreated);
AssertMessageInfo(1, 1, jsm.GetNextMessage(STREAM, 1, Subject(1)));
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 1, Subject(2)));

AssertMessageInfo(1, 3, jsm.GetNextMessage(STREAM, 2, Subject(1)), beforeCreated);
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 2, Subject(2)), beforeCreated);
AssertMessageInfo(1, 3, jsm.GetNextMessage(STREAM, 2, Subject(1)));
AssertMessageInfo(2, 2, jsm.GetNextMessage(STREAM, 2, Subject(2)));

AssertMessageInfo(1, 5, jsm.GetNextMessage(STREAM, 5, Subject(1)), beforeCreated);
AssertMessageInfo(2, 6, jsm.GetNextMessage(STREAM, 5, Subject(2)), beforeCreated);
AssertMessageInfo(1, 5, jsm.GetNextMessage(STREAM, 5, Subject(1)));
AssertMessageInfo(2, 6, jsm.GetNextMessage(STREAM, 5, Subject(2)));

AssertStatus(10003, Assert.Throws<NATSJetStreamException>(() => jsm.GetMessage(STREAM, 0)));
AssertStatus(10037, Assert.Throws<NATSJetStreamException>(() => jsm.GetMessage(STREAM, 9)));
Expand All @@ -838,11 +817,10 @@ private void AssertStatus(int apiErrorCode, NATSJetStreamException e) {
Assert.Equal(apiErrorCode, e.ApiErrorCode);
}

private void AssertMessageInfo(int subj, ulong seq, MessageInfo mi, DateTime beforeCreated) {
private void AssertMessageInfo(int subj, ulong seq, MessageInfo mi) {
Assert.Equal(STREAM, mi.Stream);
Assert.Equal(Subject(subj), mi.Subject);
Assert.Equal(seq, mi.Sequence);
Assert.True(SameOrAfter(mi.Time, beforeCreated));
Assert.Equal("s" + subj + "-q" + seq, Encoding.UTF8.GetString(mi.Data));
}

Expand Down
14 changes: 14 additions & 0 deletions src/Tests/IntegrationTests/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ public static NATSServer CreateJetStreamFastAndVerify(int port, string args = nu
public static NATSServer CreateFastAndVerify(string args = null)
=> CreateFastAndVerify(Defaults.Port, args);

public static NATSServer CreateFastAndVerifyQuietHandlers()
=> CreateFastAndVerify(Defaults.Port, null, opts =>
{
opts.ClosedEventHandler = (s, e) => { };
opts.ServerDiscoveredEventHandler = (s, e) => { };
opts.DisconnectedEventHandler = (s, e) => { };
opts.ReconnectedEventHandler = (s, e) => { };
opts.LameDuckModeEventHandler = (s, e) => { };
opts.AsyncErrorEventHandler = (s, e) => { };
opts.HeartbeatAlarmEventHandler = (s, e) => { };
opts.UnhandledStatusEventHandler = (s, e) => { };
opts.FlowControlProcessedEventHandler = (s, e) => { };
});

public static NATSServer CreateFastAndVerify(int port, string args = null, Action<Options> optionsModifier = null)
{
var server = new NATSServer(TimeSpan.Zero, port, args);
Expand Down
10 changes: 2 additions & 8 deletions src/Tests/UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,5 @@ public static byte[] DataBytes() {
public static byte[] DataBytes(int seq) {
return Encoding.ASCII.GetBytes(Data(seq));
}

public static bool Same(DateTime t1, DateTime t2) => t1.ToUniversalTime().CompareTo(t2.ToUniversalTime()) == 0;
public static bool Before(DateTime t1, DateTime t2) => t1.ToUniversalTime().CompareTo(t2.ToUniversalTime()) < 0;
public static bool After(DateTime t1, DateTime t2) => t1.ToUniversalTime().CompareTo(t2.ToUniversalTime()) > 0;
public static bool SameOrBefore(DateTime t1, DateTime t2) => t1.ToUniversalTime().CompareTo(t2.ToUniversalTime()) <= 0;
public static bool SameOrAfter(DateTime t1, DateTime t2) => t1.ToUniversalTime().CompareTo(t2.ToUniversalTime()) >= 0;
}
}
}
}

0 comments on commit e931045

Please sign in to comment.