|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/jonboulle/clockwork" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/config" |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/trace" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNew(t *testing.T) { |
| 15 | + t.Run("defaults", func(t *testing.T) { |
| 16 | + c := New() |
| 17 | + require.NotNil(t, c) |
| 18 | + require.Equal(t, DefaultSessionPoolSizeLimit, c.SizeLimit()) |
| 19 | + require.Equal(t, DefaultSessionPoolCreateSessionTimeout, c.CreateSessionTimeout()) |
| 20 | + require.Equal(t, DefaultSessionPoolDeleteTimeout, c.DeleteTimeout()) |
| 21 | + require.Equal(t, DefaultSessionPoolIdleThreshold, c.IdleThreshold()) |
| 22 | + require.NotNil(t, c.Clock()) |
| 23 | + require.NotNil(t, c.Trace()) |
| 24 | + }) |
| 25 | + |
| 26 | + t.Run("with nil option", func(t *testing.T) { |
| 27 | + c := New(nil) |
| 28 | + require.NotNil(t, c) |
| 29 | + require.Equal(t, DefaultSessionPoolSizeLimit, c.SizeLimit()) |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestWithSizeLimit(t *testing.T) { |
| 34 | + t.Run("positive value", func(t *testing.T) { |
| 35 | + c := New(WithSizeLimit(100)) |
| 36 | + require.Equal(t, 100, c.SizeLimit()) |
| 37 | + }) |
| 38 | + |
| 39 | + t.Run("zero value uses default", func(t *testing.T) { |
| 40 | + c := New(WithSizeLimit(0)) |
| 41 | + require.Equal(t, DefaultSessionPoolSizeLimit, c.SizeLimit()) |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("negative value uses default", func(t *testing.T) { |
| 45 | + c := New(WithSizeLimit(-1)) |
| 46 | + require.Equal(t, DefaultSessionPoolSizeLimit, c.SizeLimit()) |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestWithSessionPoolSessionUsageLimit(t *testing.T) { |
| 51 | + t.Run("uint64 limit", func(t *testing.T) { |
| 52 | + c := New(WithSessionPoolSessionUsageLimit[uint64](1000)) |
| 53 | + require.Equal(t, uint64(1000), c.SessionUsageLimit()) |
| 54 | + require.Equal(t, time.Duration(0), c.SessionUsageTTL()) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("duration limit", func(t *testing.T) { |
| 58 | + ttl := 5 * time.Minute |
| 59 | + c := New(WithSessionPoolSessionUsageLimit(ttl)) |
| 60 | + require.Equal(t, uint64(0), c.SessionUsageLimit()) |
| 61 | + require.Equal(t, ttl, c.SessionUsageTTL()) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestWithKeepAliveMinSize(t *testing.T) { |
| 66 | + t.Run("deprecated function", func(t *testing.T) { |
| 67 | + c := New(WithKeepAliveMinSize(20)) |
| 68 | + // This function is deprecated and does nothing |
| 69 | + require.Equal(t, DefaultKeepAliveMinSize, c.KeepAliveMinSize()) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +func TestWithIdleKeepAliveThreshold(t *testing.T) { |
| 74 | + t.Run("deprecated function", func(t *testing.T) { |
| 75 | + c := New(WithIdleKeepAliveThreshold(5)) |
| 76 | + // This function is deprecated and does nothing |
| 77 | + require.Equal(t, DefaultIdleKeepAliveThreshold, c.IdleKeepAliveThreshold()) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func TestWithIdleThreshold(t *testing.T) { |
| 82 | + t.Run("positive value", func(t *testing.T) { |
| 83 | + threshold := 10 * time.Minute |
| 84 | + c := New(WithIdleThreshold(threshold)) |
| 85 | + require.Equal(t, threshold, c.IdleThreshold()) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("zero value", func(t *testing.T) { |
| 89 | + c := New(WithIdleThreshold(0)) |
| 90 | + require.Equal(t, time.Duration(0), c.IdleThreshold()) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("negative value", func(t *testing.T) { |
| 94 | + c := New(WithIdleThreshold(-1 * time.Minute)) |
| 95 | + require.Equal(t, time.Duration(0), c.IdleThreshold()) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +func TestWithKeepAliveTimeout(t *testing.T) { |
| 100 | + t.Run("deprecated function", func(t *testing.T) { |
| 101 | + c := New(WithKeepAliveTimeout(1 * time.Second)) |
| 102 | + // This function is deprecated and does nothing |
| 103 | + require.Equal(t, DefaultSessionPoolKeepAliveTimeout, c.KeepAliveTimeout()) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func TestWithCreateSessionTimeout(t *testing.T) { |
| 108 | + t.Run("positive value", func(t *testing.T) { |
| 109 | + timeout := 10 * time.Second |
| 110 | + c := New(WithCreateSessionTimeout(timeout)) |
| 111 | + require.Equal(t, timeout, c.CreateSessionTimeout()) |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("zero value", func(t *testing.T) { |
| 115 | + c := New(WithCreateSessionTimeout(0)) |
| 116 | + require.Equal(t, time.Duration(0), c.CreateSessionTimeout()) |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("negative value", func(t *testing.T) { |
| 120 | + c := New(WithCreateSessionTimeout(-1 * time.Second)) |
| 121 | + require.Equal(t, time.Duration(0), c.CreateSessionTimeout()) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func TestWithDeleteTimeout(t *testing.T) { |
| 126 | + t.Run("positive value", func(t *testing.T) { |
| 127 | + timeout := 1 * time.Second |
| 128 | + c := New(WithDeleteTimeout(timeout)) |
| 129 | + require.Equal(t, timeout, c.DeleteTimeout()) |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run("zero value uses default", func(t *testing.T) { |
| 133 | + c := New(WithDeleteTimeout(0)) |
| 134 | + require.Equal(t, DefaultSessionPoolDeleteTimeout, c.DeleteTimeout()) |
| 135 | + }) |
| 136 | + |
| 137 | + t.Run("negative value uses default", func(t *testing.T) { |
| 138 | + c := New(WithDeleteTimeout(-1 * time.Second)) |
| 139 | + require.Equal(t, DefaultSessionPoolDeleteTimeout, c.DeleteTimeout()) |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +func TestWithTrace(t *testing.T) { |
| 144 | + t.Run("add trace", func(t *testing.T) { |
| 145 | + tr := &trace.Table{} |
| 146 | + c := New(WithTrace(tr)) |
| 147 | + require.NotNil(t, c.Trace()) |
| 148 | + }) |
| 149 | + |
| 150 | + t.Run("multiple traces", func(t *testing.T) { |
| 151 | + tr1 := &trace.Table{} |
| 152 | + tr2 := &trace.Table{} |
| 153 | + c := New(WithTrace(tr1), WithTrace(tr2)) |
| 154 | + require.NotNil(t, c.Trace()) |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func TestWithIgnoreTruncated(t *testing.T) { |
| 159 | + t.Run("enable ignore truncated", func(t *testing.T) { |
| 160 | + c := New(WithIgnoreTruncated()) |
| 161 | + require.True(t, c.IgnoreTruncated()) |
| 162 | + }) |
| 163 | + |
| 164 | + t.Run("default is false", func(t *testing.T) { |
| 165 | + c := New() |
| 166 | + require.False(t, c.IgnoreTruncated()) |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +func TestWithMaxRequestMessageSize(t *testing.T) { |
| 171 | + t.Run("set max request message size", func(t *testing.T) { |
| 172 | + size := 1024 * 1024 |
| 173 | + c := New(WithMaxRequestMessageSize(size)) |
| 174 | + require.Equal(t, size, c.MaxRequestMessageSize()) |
| 175 | + }) |
| 176 | + |
| 177 | + t.Run("default is zero", func(t *testing.T) { |
| 178 | + c := New() |
| 179 | + require.Equal(t, 0, c.MaxRequestMessageSize()) |
| 180 | + }) |
| 181 | +} |
| 182 | + |
| 183 | +func TestExecuteDataQueryOverQueryService(t *testing.T) { |
| 184 | + t.Run("enable", func(t *testing.T) { |
| 185 | + c := New(ExecuteDataQueryOverQueryService(true)) |
| 186 | + require.True(t, c.ExecuteDataQueryOverQueryService()) |
| 187 | + require.True(t, c.UseQuerySession()) |
| 188 | + }) |
| 189 | + |
| 190 | + t.Run("disable", func(t *testing.T) { |
| 191 | + c := New(ExecuteDataQueryOverQueryService(false)) |
| 192 | + require.False(t, c.ExecuteDataQueryOverQueryService()) |
| 193 | + }) |
| 194 | + |
| 195 | + t.Run("default is false", func(t *testing.T) { |
| 196 | + c := New() |
| 197 | + require.False(t, c.ExecuteDataQueryOverQueryService()) |
| 198 | + }) |
| 199 | +} |
| 200 | + |
| 201 | +func TestUseQuerySession(t *testing.T) { |
| 202 | + t.Run("enable", func(t *testing.T) { |
| 203 | + c := New(UseQuerySession(true)) |
| 204 | + require.True(t, c.UseQuerySession()) |
| 205 | + }) |
| 206 | + |
| 207 | + t.Run("disable", func(t *testing.T) { |
| 208 | + c := New(UseQuerySession(false)) |
| 209 | + require.False(t, c.UseQuerySession()) |
| 210 | + }) |
| 211 | + |
| 212 | + t.Run("default is false", func(t *testing.T) { |
| 213 | + c := New() |
| 214 | + require.False(t, c.UseQuerySession()) |
| 215 | + }) |
| 216 | +} |
| 217 | + |
| 218 | +func TestWithDisableSessionBalancer(t *testing.T) { |
| 219 | + t.Run("disable session balancer", func(t *testing.T) { |
| 220 | + c := New(WithDisableSessionBalancer()) |
| 221 | + require.NotNil(t, c) |
| 222 | + }) |
| 223 | +} |
| 224 | + |
| 225 | +func TestWithClock(t *testing.T) { |
| 226 | + t.Run("custom clock", func(t *testing.T) { |
| 227 | + fakeClock := clockwork.NewFakeClock() |
| 228 | + c := New(WithClock(fakeClock)) |
| 229 | + require.Equal(t, fakeClock, c.Clock()) |
| 230 | + }) |
| 231 | + |
| 232 | + t.Run("default clock", func(t *testing.T) { |
| 233 | + c := New() |
| 234 | + require.NotNil(t, c.Clock()) |
| 235 | + }) |
| 236 | +} |
| 237 | + |
| 238 | +func TestWith(t *testing.T) { |
| 239 | + t.Run("apply common config", func(t *testing.T) { |
| 240 | + commonCfg := config.Common{} |
| 241 | + c := New(With(commonCfg)) |
| 242 | + require.NotNil(t, c) |
| 243 | + }) |
| 244 | +} |
| 245 | + |
| 246 | +func TestConfigGetters(t *testing.T) { |
| 247 | + t.Run("all getters return expected values", func(t *testing.T) { |
| 248 | + c := New( |
| 249 | + WithSizeLimit(100), |
| 250 | + WithSessionPoolSessionUsageLimit[uint64](500), |
| 251 | + WithIdleThreshold(3*time.Minute), |
| 252 | + WithCreateSessionTimeout(10*time.Second), |
| 253 | + WithDeleteTimeout(1*time.Second), |
| 254 | + WithIgnoreTruncated(), |
| 255 | + WithMaxRequestMessageSize(2048), |
| 256 | + ExecuteDataQueryOverQueryService(true), |
| 257 | + ) |
| 258 | + |
| 259 | + require.Equal(t, 100, c.SizeLimit()) |
| 260 | + require.Equal(t, uint64(500), c.SessionUsageLimit()) |
| 261 | + require.Equal(t, 3*time.Minute, c.IdleThreshold()) |
| 262 | + require.Equal(t, 10*time.Second, c.CreateSessionTimeout()) |
| 263 | + require.Equal(t, 1*time.Second, c.DeleteTimeout()) |
| 264 | + require.True(t, c.IgnoreTruncated()) |
| 265 | + require.Equal(t, 2048, c.MaxRequestMessageSize()) |
| 266 | + require.True(t, c.ExecuteDataQueryOverQueryService()) |
| 267 | + require.True(t, c.UseQuerySession()) |
| 268 | + require.NotNil(t, c.Trace()) |
| 269 | + require.NotNil(t, c.Clock()) |
| 270 | + }) |
| 271 | +} |
0 commit comments