From 0a8fe68d0a006f66f8c708842adfccfe0b2f249b Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 10 Aug 2026 16:33:32 -0400 Subject: [PATCH] test(ldap): sequence connection factory for failover tests --- .../SequenceLdapConnectionFactory.cs | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ZB.MOM.WW.Auth/tests/ZB.MOM.WW.Auth.Ldap.Tests/SequenceLdapConnectionFactory.cs diff --git a/ZB.MOM.WW.Auth/tests/ZB.MOM.WW.Auth.Ldap.Tests/SequenceLdapConnectionFactory.cs b/ZB.MOM.WW.Auth/tests/ZB.MOM.WW.Auth.Ldap.Tests/SequenceLdapConnectionFactory.cs new file mode 100644 index 0000000..cf0233c --- /dev/null +++ b/ZB.MOM.WW.Auth/tests/ZB.MOM.WW.Auth.Ldap.Tests/SequenceLdapConnectionFactory.cs @@ -0,0 +1,51 @@ +using ZB.MOM.WW.Auth.Ldap.Internal; + +namespace ZB.MOM.WW.Auth.Ldap.Tests; + +/// +/// Test double for that hands out pre-scripted +/// instances in order, one per call — +/// exactly matching the failover walk, which opens a fresh connection per endpoint attempt. +/// Records every created connection for inspection. When the script runs out, returns a +/// bare unscripted fake (which yields user-not-found) so a miscounted test fails loudly +/// rather than reusing a stale fake. +/// +internal sealed class SequenceLdapConnectionFactory : ILdapConnectionFactory +{ + private readonly Queue _script; + + /// Scripts the connections handed out, in the order the walk will request them. + public SequenceLdapConnectionFactory(params FakeLdapConnection[] fakes) + => _script = new Queue(fakes); + + /// Every connection handed out, in creation order (scripted or overflow). + public List Created { get; } = new(); + + /// + public ILdapConnection Create() + { + var fake = _script.Count > 0 ? _script.Dequeue() : new FakeLdapConnection(); + Created.Add(fake); + return fake; + } +} + +// --------------------------------------------------------------------------- +// Smoke test: verifies the sequencing and overflow behaviour the failover tests rely on. +// --------------------------------------------------------------------------- + +public class SequenceLdapConnectionFactorySmokeTests +{ + [Fact] + public void Create_ReturnsScriptedFakesInOrder_ThenOverflows() + { + var first = new FakeLdapConnection().ThrowOnConnect(); + var second = new FakeLdapConnection(); + var factory = new SequenceLdapConnectionFactory(first, second); + + Assert.Same(first, factory.Create()); + Assert.Same(second, factory.Create()); + Assert.NotSame(second, factory.Create()); // overflow: fresh bare fake + Assert.Equal(3, factory.Created.Count); + } +}