|
| 1 | +using Servus.Akka.Transport; |
| 2 | + |
| 3 | +namespace Servus.Akka.Tests.Transport; |
| 4 | + |
| 5 | +public sealed class PoolConfigRegistrySpec |
| 6 | +{ |
| 7 | + [Fact(Timeout = 5000)] |
| 8 | + public void Constructor_should_set_default_config() |
| 9 | + { |
| 10 | + var defaultConfig = new TcpPoolConfig( |
| 11 | + MaxConnectionsPerHost: 10, |
| 12 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 13 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 14 | + ReuseOnUpstreamFinish: true); |
| 15 | + |
| 16 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 17 | + |
| 18 | + var resolved = registry.Resolve(null); |
| 19 | + Assert.Equal(defaultConfig, resolved); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact(Timeout = 5000)] |
| 23 | + public void Resolve_should_return_default_when_key_is_null() |
| 24 | + { |
| 25 | + var defaultConfig = new TcpPoolConfig( |
| 26 | + MaxConnectionsPerHost: 5, |
| 27 | + IdleTimeout: TimeSpan.FromSeconds(60), |
| 28 | + ConnectionLifetime: TimeSpan.FromMinutes(10), |
| 29 | + ReuseOnUpstreamFinish: false); |
| 30 | + |
| 31 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 32 | + |
| 33 | + var resolved = registry.Resolve(null); |
| 34 | + Assert.Equal(defaultConfig, resolved); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact(Timeout = 5000)] |
| 38 | + public void Resolve_should_return_default_when_key_not_registered() |
| 39 | + { |
| 40 | + var defaultConfig = new TcpPoolConfig( |
| 41 | + MaxConnectionsPerHost: 8, |
| 42 | + IdleTimeout: TimeSpan.FromSeconds(45), |
| 43 | + ConnectionLifetime: TimeSpan.FromMinutes(3), |
| 44 | + ReuseOnUpstreamFinish: true); |
| 45 | + |
| 46 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 47 | + |
| 48 | + var resolved = registry.Resolve("nonexistent-pool"); |
| 49 | + Assert.Equal(defaultConfig, resolved); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact(Timeout = 5000)] |
| 53 | + public void Register_should_store_config_for_key() |
| 54 | + { |
| 55 | + var defaultConfig = new TcpPoolConfig( |
| 56 | + MaxConnectionsPerHost: 10, |
| 57 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 58 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 59 | + ReuseOnUpstreamFinish: true); |
| 60 | + |
| 61 | + var customConfig = new TcpPoolConfig( |
| 62 | + MaxConnectionsPerHost: 20, |
| 63 | + IdleTimeout: TimeSpan.FromSeconds(15), |
| 64 | + ConnectionLifetime: TimeSpan.FromMinutes(2), |
| 65 | + ReuseOnUpstreamFinish: false); |
| 66 | + |
| 67 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 68 | + registry.Register("custom-pool", customConfig); |
| 69 | + |
| 70 | + var resolved = registry.Resolve("custom-pool"); |
| 71 | + Assert.Equal(customConfig, resolved); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact(Timeout = 5000)] |
| 75 | + public void Resolve_should_return_registered_config() |
| 76 | + { |
| 77 | + var defaultConfig = new TcpPoolConfig( |
| 78 | + MaxConnectionsPerHost: 10, |
| 79 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 80 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 81 | + ReuseOnUpstreamFinish: true); |
| 82 | + |
| 83 | + var poolAConfig = new TcpPoolConfig( |
| 84 | + MaxConnectionsPerHost: 5, |
| 85 | + IdleTimeout: TimeSpan.FromSeconds(20), |
| 86 | + ConnectionLifetime: TimeSpan.FromMinutes(1), |
| 87 | + ReuseOnUpstreamFinish: false); |
| 88 | + |
| 89 | + var poolBConfig = new TcpPoolConfig( |
| 90 | + MaxConnectionsPerHost: 50, |
| 91 | + IdleTimeout: TimeSpan.FromSeconds(60), |
| 92 | + ConnectionLifetime: TimeSpan.FromMinutes(10), |
| 93 | + ReuseOnUpstreamFinish: true); |
| 94 | + |
| 95 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 96 | + registry.Register("pool-a", poolAConfig); |
| 97 | + registry.Register("pool-b", poolBConfig); |
| 98 | + |
| 99 | + Assert.Equal(poolAConfig, registry.Resolve("pool-a")); |
| 100 | + Assert.Equal(poolBConfig, registry.Resolve("pool-b")); |
| 101 | + Assert.Equal(defaultConfig, registry.Resolve("pool-c")); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact(Timeout = 5000)] |
| 105 | + public void Register_should_overwrite_existing_key() |
| 106 | + { |
| 107 | + var defaultConfig = new TcpPoolConfig( |
| 108 | + MaxConnectionsPerHost: 10, |
| 109 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 110 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 111 | + ReuseOnUpstreamFinish: true); |
| 112 | + |
| 113 | + var initialConfig = new TcpPoolConfig( |
| 114 | + MaxConnectionsPerHost: 15, |
| 115 | + IdleTimeout: TimeSpan.FromSeconds(25), |
| 116 | + ConnectionLifetime: TimeSpan.FromMinutes(3), |
| 117 | + ReuseOnUpstreamFinish: false); |
| 118 | + |
| 119 | + var overwriteConfig = new TcpPoolConfig( |
| 120 | + MaxConnectionsPerHost: 25, |
| 121 | + IdleTimeout: TimeSpan.FromSeconds(40), |
| 122 | + ConnectionLifetime: TimeSpan.FromMinutes(7), |
| 123 | + ReuseOnUpstreamFinish: true); |
| 124 | + |
| 125 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 126 | + registry.Register("pool", initialConfig); |
| 127 | + |
| 128 | + var resolved1 = registry.Resolve("pool"); |
| 129 | + Assert.Equal(initialConfig, resolved1); |
| 130 | + |
| 131 | + registry.Register("pool", overwriteConfig); |
| 132 | + |
| 133 | + var resolved2 = registry.Resolve("pool"); |
| 134 | + Assert.Equal(overwriteConfig, resolved2); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact(Timeout = 5000)] |
| 138 | + public void Register_should_throw_if_config_is_null() |
| 139 | + { |
| 140 | + var defaultConfig = new TcpPoolConfig( |
| 141 | + MaxConnectionsPerHost: 10, |
| 142 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 143 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 144 | + ReuseOnUpstreamFinish: true); |
| 145 | + |
| 146 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 147 | + |
| 148 | + Assert.Throws<ArgumentNullException>(() => registry.Register("pool", null!)); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact(Timeout = 5000)] |
| 152 | + public void Constructor_should_throw_if_default_config_is_null() |
| 153 | + { |
| 154 | + Assert.Throws<ArgumentNullException>(() => new PoolConfigRegistry(null!)); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact(Timeout = 5000)] |
| 158 | + public void Register_should_support_case_insensitive_keys() |
| 159 | + { |
| 160 | + var defaultConfig = new TcpPoolConfig( |
| 161 | + MaxConnectionsPerHost: 10, |
| 162 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 163 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 164 | + ReuseOnUpstreamFinish: true); |
| 165 | + |
| 166 | + var customConfig = new TcpPoolConfig( |
| 167 | + MaxConnectionsPerHost: 20, |
| 168 | + IdleTimeout: TimeSpan.FromSeconds(15), |
| 169 | + ConnectionLifetime: TimeSpan.FromMinutes(2), |
| 170 | + ReuseOnUpstreamFinish: false); |
| 171 | + |
| 172 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 173 | + registry.Register("MyPool", customConfig); |
| 174 | + |
| 175 | + var resolved1 = registry.Resolve("mypool"); |
| 176 | + var resolved2 = registry.Resolve("MYPOOL"); |
| 177 | + |
| 178 | + Assert.Equal(customConfig, resolved1); |
| 179 | + Assert.Equal(customConfig, resolved2); |
| 180 | + } |
| 181 | + |
| 182 | + [Fact(Timeout = 5000)] |
| 183 | + public void Register_should_return_self_for_fluent_chaining() |
| 184 | + { |
| 185 | + var defaultConfig = new TcpPoolConfig( |
| 186 | + MaxConnectionsPerHost: 10, |
| 187 | + IdleTimeout: TimeSpan.FromSeconds(30), |
| 188 | + ConnectionLifetime: TimeSpan.FromMinutes(5), |
| 189 | + ReuseOnUpstreamFinish: true); |
| 190 | + |
| 191 | + var config1 = new TcpPoolConfig( |
| 192 | + MaxConnectionsPerHost: 5, |
| 193 | + IdleTimeout: TimeSpan.FromSeconds(20), |
| 194 | + ConnectionLifetime: TimeSpan.FromMinutes(1), |
| 195 | + ReuseOnUpstreamFinish: false); |
| 196 | + |
| 197 | + var config2 = new TcpPoolConfig( |
| 198 | + MaxConnectionsPerHost: 15, |
| 199 | + IdleTimeout: TimeSpan.FromSeconds(40), |
| 200 | + ConnectionLifetime: TimeSpan.FromMinutes(4), |
| 201 | + ReuseOnUpstreamFinish: true); |
| 202 | + |
| 203 | + var registry = new PoolConfigRegistry(defaultConfig); |
| 204 | + var result = registry |
| 205 | + .Register("pool1", config1) |
| 206 | + .Register("pool2", config2); |
| 207 | + |
| 208 | + Assert.Same(registry, result); |
| 209 | + Assert.Equal(config1, registry.Resolve("pool1")); |
| 210 | + Assert.Equal(config2, registry.Resolve("pool2")); |
| 211 | + } |
| 212 | +} |
0 commit comments