feat: add system event DTOs and JSON source generator context
This commit is contained in:
68
tests/NATS.Server.Tests/EventSystemTests.cs
Normal file
68
tests/NATS.Server.Tests/EventSystemTests.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Text.Json;
|
||||
using NATS.Server.Events;
|
||||
|
||||
namespace NATS.Server.Tests;
|
||||
|
||||
public class EventSystemTests
|
||||
{
|
||||
[Fact]
|
||||
public void ConnectEventMsg_serializes_with_correct_type()
|
||||
{
|
||||
var evt = new ConnectEventMsg
|
||||
{
|
||||
Type = ConnectEventMsg.EventType,
|
||||
Id = "test123",
|
||||
Time = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc),
|
||||
Server = new EventServerInfo { Name = "test-server", Id = "SRV1" },
|
||||
Client = new EventClientInfo { Id = 1, Account = "$G" },
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(evt, EventJsonContext.Default.ConnectEventMsg);
|
||||
json.ShouldContain("\"type\":\"io.nats.server.advisory.v1.client_connect\"");
|
||||
json.ShouldContain("\"server\":");
|
||||
json.ShouldContain("\"client\":");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisconnectEventMsg_serializes_with_reason()
|
||||
{
|
||||
var evt = new DisconnectEventMsg
|
||||
{
|
||||
Type = DisconnectEventMsg.EventType,
|
||||
Id = "test456",
|
||||
Time = DateTime.UtcNow,
|
||||
Server = new EventServerInfo { Name = "test-server", Id = "SRV1" },
|
||||
Client = new EventClientInfo { Id = 2, Account = "myacc" },
|
||||
Reason = "Client Closed",
|
||||
Sent = new DataStats { Msgs = 10, Bytes = 1024 },
|
||||
Received = new DataStats { Msgs = 5, Bytes = 512 },
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(evt, EventJsonContext.Default.DisconnectEventMsg);
|
||||
json.ShouldContain("\"reason\":\"Client Closed\"");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ServerStatsMsg_serializes()
|
||||
{
|
||||
var evt = new ServerStatsMsg
|
||||
{
|
||||
Server = new EventServerInfo { Name = "srv1", Id = "ABC" },
|
||||
Stats = new ServerStatsData
|
||||
{
|
||||
Connections = 10,
|
||||
TotalConnections = 100,
|
||||
InMsgs = 5000,
|
||||
OutMsgs = 4500,
|
||||
InBytes = 1_000_000,
|
||||
OutBytes = 900_000,
|
||||
Mem = 50 * 1024 * 1024,
|
||||
Subscriptions = 42,
|
||||
},
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(evt, EventJsonContext.Default.ServerStatsMsg);
|
||||
json.ShouldContain("\"connections\":10");
|
||||
json.ShouldContain("\"in_msgs\":5000");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user