Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implemented RecordingLogger and RecordingLoggerFactory for capturing log entries in tests. - Added unit tests for InMemoryChannel, covering constructor behavior, property assignments, channel communication, and disposal. - Created InMemoryTransportOptionsTests to validate default values and customizable options for InMemory transport. - Developed RabbitMqFrameProtocolTests to ensure correct parsing and property creation for RabbitMQ frames. - Added RabbitMqTransportOptionsTests to verify default settings and customization options for RabbitMQ transport. - Updated project files for testing libraries and dependencies.
312 lines
7.2 KiB
C#
312 lines
7.2 KiB
C#
using StellaOps.Router.Common.Enums;
|
|
|
|
namespace StellaOps.Router.Config.Tests;
|
|
|
|
/// <summary>
|
|
/// Unit tests for <see cref="StaticInstanceConfig"/>.
|
|
/// </summary>
|
|
public sealed class StaticInstanceConfigTests
|
|
{
|
|
#region Default Values Tests
|
|
|
|
[Fact]
|
|
public void Constructor_Region_DefaultsToDefault()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Region.Should().Be("default");
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_Transport_DefaultsToTcp()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Transport.Should().Be(TransportType.Tcp);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_Weight_DefaultsTo100()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Weight.Should().Be(100);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_Metadata_DefaultsToEmptyDictionary()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Metadata.Should().NotBeNull();
|
|
config.Metadata.Should().BeEmpty();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Required Properties Tests
|
|
|
|
[Fact]
|
|
public void ServiceName_IsRequired()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "required-service",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.ServiceName.Should().Be("required-service");
|
|
}
|
|
|
|
[Fact]
|
|
public void Version_IsRequired()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "2.3.4",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Version.Should().Be("2.3.4");
|
|
}
|
|
|
|
[Fact]
|
|
public void Host_IsRequired()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "192.168.1.100",
|
|
Port = 8080
|
|
};
|
|
|
|
// Assert
|
|
config.Host.Should().Be("192.168.1.100");
|
|
}
|
|
|
|
[Fact]
|
|
public void Port_IsRequired()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 443
|
|
};
|
|
|
|
// Assert
|
|
config.Port.Should().Be(443);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Property Assignment Tests
|
|
|
|
[Fact]
|
|
public void Region_CanBeSet()
|
|
{
|
|
// Arrange
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Act
|
|
config.Region = "us-west-2";
|
|
|
|
// Assert
|
|
config.Region.Should().Be("us-west-2");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(TransportType.Tcp)]
|
|
[InlineData(TransportType.Certificate)]
|
|
[InlineData(TransportType.Udp)]
|
|
[InlineData(TransportType.InMemory)]
|
|
[InlineData(TransportType.RabbitMq)]
|
|
public void Transport_CanBeSetToAllTypes(TransportType transport)
|
|
{
|
|
// Arrange
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Act
|
|
config.Transport = transport;
|
|
|
|
// Assert
|
|
config.Transport.Should().Be(transport);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(1)]
|
|
[InlineData(50)]
|
|
[InlineData(100)]
|
|
[InlineData(200)]
|
|
[InlineData(1000)]
|
|
public void Weight_CanBeSet(int weight)
|
|
{
|
|
// Arrange
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Act
|
|
config.Weight = weight;
|
|
|
|
// Assert
|
|
config.Weight.Should().Be(weight);
|
|
}
|
|
|
|
[Fact]
|
|
public void Metadata_CanAddEntries()
|
|
{
|
|
// Arrange
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0",
|
|
Host = "localhost",
|
|
Port = 8080
|
|
};
|
|
|
|
// Act
|
|
config.Metadata["environment"] = "production";
|
|
config.Metadata["cluster"] = "primary";
|
|
|
|
// Assert
|
|
config.Metadata.Should().HaveCount(2);
|
|
config.Metadata["environment"].Should().Be("production");
|
|
config.Metadata["cluster"].Should().Be("primary");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Complex Configuration Tests
|
|
|
|
[Fact]
|
|
public void CompleteConfiguration_Works()
|
|
{
|
|
// Arrange & Act
|
|
var config = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "user-service",
|
|
Version = "3.2.1",
|
|
Region = "eu-central-1",
|
|
Host = "user-svc.internal.example.com",
|
|
Port = 8443,
|
|
Transport = TransportType.Certificate,
|
|
Weight = 150,
|
|
Metadata = new Dictionary<string, string>
|
|
{
|
|
["datacenter"] = "dc1",
|
|
["rack"] = "rack-42",
|
|
["shard"] = "primary"
|
|
}
|
|
};
|
|
|
|
// Assert
|
|
config.ServiceName.Should().Be("user-service");
|
|
config.Version.Should().Be("3.2.1");
|
|
config.Region.Should().Be("eu-central-1");
|
|
config.Host.Should().Be("user-svc.internal.example.com");
|
|
config.Port.Should().Be(8443);
|
|
config.Transport.Should().Be(TransportType.Certificate);
|
|
config.Weight.Should().Be(150);
|
|
config.Metadata.Should().HaveCount(3);
|
|
}
|
|
|
|
[Fact]
|
|
public void MultipleInstances_CanHaveDifferentWeights()
|
|
{
|
|
// Arrange & Act
|
|
var primary = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "api",
|
|
Version = "1.0",
|
|
Host = "primary.example.com",
|
|
Port = 8080,
|
|
Weight = 200
|
|
};
|
|
|
|
var secondary = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "api",
|
|
Version = "1.0",
|
|
Host = "secondary.example.com",
|
|
Port = 8080,
|
|
Weight = 100
|
|
};
|
|
|
|
var tertiary = new StaticInstanceConfig
|
|
{
|
|
ServiceName = "api",
|
|
Version = "1.0",
|
|
Host = "tertiary.example.com",
|
|
Port = 8080,
|
|
Weight = 50
|
|
};
|
|
|
|
// Assert
|
|
primary.Weight.Should().BeGreaterThan(secondary.Weight);
|
|
secondary.Weight.Should().BeGreaterThan(tertiary.Weight);
|
|
}
|
|
|
|
#endregion
|
|
}
|