|
| 1 | +package sql |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDefaultConfig(t *testing.T) { |
| 12 | + cfg := DefaultConfig("test-consumer", "test-worker") |
| 13 | + |
| 14 | + assert.Equal(t, "test-consumer", cfg.ConsumerGroup) |
| 15 | + assert.Equal(t, "test-worker", cfg.WorkerID) |
| 16 | + assert.Equal(t, 100*time.Millisecond, cfg.PollInterval) |
| 17 | + assert.Equal(t, 10, cfg.BatchSize) |
| 18 | + assert.Equal(t, 60*time.Second, cfg.VisibilityTimeout) |
| 19 | + assert.Equal(t, 10*time.Second, cfg.LeaseRenewalInterval) |
| 20 | + assert.Equal(t, 30*time.Second, cfg.LeaseDuration) |
| 21 | + assert.True(t, cfg.DLQ.Enabled) |
| 22 | + assert.Equal(t, 3, cfg.Retry.MaxAttempts) |
| 23 | + assert.Equal(t, 1*time.Second, cfg.Retry.InitialBackoff) |
| 24 | + assert.Equal(t, 30*time.Second, cfg.Retry.MaxBackoff) |
| 25 | + assert.Equal(t, 2.0, cfg.Retry.BackoffMultiplier) |
| 26 | +} |
| 27 | + |
| 28 | +func TestConfigValidation(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + config Config |
| 32 | + expectError bool |
| 33 | + errorMsg string |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "valid config", |
| 37 | + config: DefaultConfig("test-consumer", "test-worker"), |
| 38 | + expectError: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "empty consumer group", |
| 42 | + config: Config{ |
| 43 | + ConsumerGroup: "", |
| 44 | + WorkerID: "test-worker", |
| 45 | + PollInterval: 100 * time.Millisecond, |
| 46 | + BatchSize: 10, |
| 47 | + VisibilityTimeout: 60 * time.Second, |
| 48 | + LeaseRenewalInterval: 10 * time.Second, |
| 49 | + LeaseDuration: 30 * time.Second, |
| 50 | + Retry: DefaultConfig("dummy", "dummy").Retry, |
| 51 | + }, |
| 52 | + expectError: true, |
| 53 | + errorMsg: "ConsumerGroup is required", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "empty worker ID", |
| 57 | + config: Config{ |
| 58 | + ConsumerGroup: "test", |
| 59 | + WorkerID: "", |
| 60 | + PollInterval: 100 * time.Millisecond, |
| 61 | + BatchSize: 10, |
| 62 | + VisibilityTimeout: 60 * time.Second, |
| 63 | + LeaseRenewalInterval: 10 * time.Second, |
| 64 | + LeaseDuration: 30 * time.Second, |
| 65 | + Retry: DefaultConfig("dummy", "dummy").Retry, |
| 66 | + }, |
| 67 | + expectError: true, |
| 68 | + errorMsg: "WorkerID is required", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "invalid poll interval", |
| 72 | + config: Config{ |
| 73 | + ConsumerGroup: "test", |
| 74 | + WorkerID: "test-worker", |
| 75 | + PollInterval: 0, |
| 76 | + BatchSize: 10, |
| 77 | + VisibilityTimeout: 60 * time.Second, |
| 78 | + LeaseRenewalInterval: 10 * time.Second, |
| 79 | + LeaseDuration: 30 * time.Second, |
| 80 | + Retry: DefaultConfig("dummy", "dummy").Retry, |
| 81 | + }, |
| 82 | + expectError: true, |
| 83 | + errorMsg: "PollInterval must be positive", |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "invalid batch size", |
| 87 | + config: Config{ |
| 88 | + ConsumerGroup: "test", |
| 89 | + WorkerID: "test-worker", |
| 90 | + PollInterval: 100 * time.Millisecond, |
| 91 | + BatchSize: 0, |
| 92 | + VisibilityTimeout: 60 * time.Second, |
| 93 | + LeaseRenewalInterval: 10 * time.Second, |
| 94 | + LeaseDuration: 30 * time.Second, |
| 95 | + Retry: DefaultConfig("dummy", "dummy").Retry, |
| 96 | + }, |
| 97 | + expectError: true, |
| 98 | + errorMsg: "BatchSize must be positive", |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + err := tt.config.Validate() |
| 105 | + if tt.expectError { |
| 106 | + require.Error(t, err) |
| 107 | + assert.Contains(t, err.Error(), tt.errorMsg) |
| 108 | + } else { |
| 109 | + require.NoError(t, err) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments