|
| 1 | +package queuer |
| 2 | + |
| 3 | +import ( |
| 4 | + "queuer/model" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewQueuer(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + maxConcurrency int |
| 15 | + options []*model.OnError |
| 16 | + dbEnvs map[string]string |
| 17 | + expectError bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Valid queuer", |
| 21 | + maxConcurrency: 100, |
| 22 | + options: nil, |
| 23 | + dbEnvs: map[string]string{ |
| 24 | + "QUEUER_DB_HOST": "localhost", |
| 25 | + "QUEUER_DB_PORT": dbPort, |
| 26 | + "QUEUER_DB_DATABASE": "database", |
| 27 | + "QUEUER_DB_USERNAME": "user", |
| 28 | + "QUEUER_DB_PASSWORD": "password", |
| 29 | + "QUEUER_DB_SCHEMA": "public", |
| 30 | + }, |
| 31 | + expectError: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Valid queuer with options", |
| 35 | + maxConcurrency: 100, |
| 36 | + options: []*model.OnError{ |
| 37 | + { |
| 38 | + Timeout: 10.0, |
| 39 | + MaxRetries: 3, |
| 40 | + RetryDelay: 1.0, |
| 41 | + RetryBackoff: model.RETRY_BACKOFF_LINEAR, |
| 42 | + }, |
| 43 | + }, |
| 44 | + dbEnvs: map[string]string{ |
| 45 | + "QUEUER_DB_HOST": "localhost", |
| 46 | + "QUEUER_DB_PORT": dbPort, |
| 47 | + "QUEUER_DB_DATABASE": "database", |
| 48 | + "QUEUER_DB_USERNAME": "user", |
| 49 | + "QUEUER_DB_PASSWORD": "password", |
| 50 | + "QUEUER_DB_SCHEMA": "public", |
| 51 | + }, |
| 52 | + expectError: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Invalid max concurrency", |
| 56 | + maxConcurrency: -1, |
| 57 | + options: nil, |
| 58 | + dbEnvs: map[string]string{ |
| 59 | + "QUEUER_DB_HOST": "localhost", |
| 60 | + "QUEUER_DB_PORT": dbPort, |
| 61 | + "QUEUER_DB_DATABASE": "database", |
| 62 | + "QUEUER_DB_USERNAME": "user", |
| 63 | + "QUEUER_DB_PASSWORD": "password", |
| 64 | + "QUEUER_DB_SCHEMA": "public", |
| 65 | + }, |
| 66 | + expectError: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Invalid options", |
| 70 | + maxConcurrency: 100, |
| 71 | + options: []*model.OnError{ |
| 72 | + { |
| 73 | + Timeout: -10.0, // Invalid timeout value |
| 74 | + MaxRetries: 3, |
| 75 | + RetryDelay: 1.0, |
| 76 | + RetryBackoff: model.RETRY_BACKOFF_LINEAR, |
| 77 | + }, |
| 78 | + }, |
| 79 | + dbEnvs: map[string]string{ |
| 80 | + "QUEUER_DB_HOST": "localhost", |
| 81 | + "QUEUER_DB_PORT": dbPort, |
| 82 | + "QUEUER_DB_DATABASE": "database", |
| 83 | + "QUEUER_DB_USERNAME": "user", |
| 84 | + "QUEUER_DB_PASSWORD": "password", |
| 85 | + "QUEUER_DB_SCHEMA": "public", |
| 86 | + }, |
| 87 | + expectError: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "Missing DB environment variable", |
| 91 | + maxConcurrency: 100, |
| 92 | + options: nil, |
| 93 | + dbEnvs: map[string]string{ |
| 94 | + "QUEUER_DB_HOST": "localhost", |
| 95 | + "QUEUER_DB_PORT": dbPort, |
| 96 | + // "QUEUER_DB_DATABASE": "database", // Intentionally missing |
| 97 | + "QUEUER_DB_USERNAME": "user", |
| 98 | + "QUEUER_DB_PASSWORD": "password", |
| 99 | + "QUEUER_DB_SCHEMA": "public", |
| 100 | + }, |
| 101 | + expectError: true, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, test := range tests { |
| 106 | + t.Run(test.name, func(t *testing.T) { |
| 107 | + for key, value := range test.dbEnvs { |
| 108 | + t.Setenv(key, value) |
| 109 | + } |
| 110 | + |
| 111 | + if test.expectError { |
| 112 | + defer func() { |
| 113 | + if r := recover(); r == nil { |
| 114 | + t.Errorf("Expected panic for %s, but did not get one", test.name) |
| 115 | + } |
| 116 | + }() |
| 117 | + } |
| 118 | + |
| 119 | + queuer := NewQueuer(test.name, test.maxConcurrency, test.options...) |
| 120 | + if !test.expectError { |
| 121 | + require.NotNil(t, queuer, "Expected Queuer to be created successfully") |
| 122 | + assert.Equal(t, test.name, queuer.worker.Name, "Expected Queuer name to match") |
| 123 | + assert.Equal(t, test.maxConcurrency, queuer.worker.MaxConcurrency, "Expected Queuer max concurrency to match") |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestNewQueuerWithoutWorker(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + dbEnvs map[string]string |
| 133 | + expectError bool |
| 134 | + }{ |
| 135 | + { |
| 136 | + name: "Valid queuer without worker", |
| 137 | + dbEnvs: map[string]string{ |
| 138 | + "QUEUER_DB_HOST": "localhost", |
| 139 | + "QUEUER_DB_PORT": dbPort, |
| 140 | + "QUEUER_DB_DATABASE": "database", |
| 141 | + "QUEUER_DB_USERNAME": "user", |
| 142 | + "QUEUER_DB_PASSWORD": "password", |
| 143 | + "QUEUER_DB_SCHEMA": "public", |
| 144 | + }, |
| 145 | + expectError: false, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Missing DB environment variable", |
| 149 | + dbEnvs: map[string]string{ |
| 150 | + "QUEUER_DB_HOST": "localhost", |
| 151 | + "QUEUER_DB_PORT": dbPort, |
| 152 | + // "QUEUER_DB_DATABASE": "database", // Intentionally missing |
| 153 | + "QUEUER_DB_USERNAME": "user", |
| 154 | + "QUEUER_DB_PASSWORD": "password", |
| 155 | + "QUEUER_DB_SCHEMA": "public", |
| 156 | + }, |
| 157 | + expectError: true, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, test := range tests { |
| 162 | + t.Run(test.name, func(t *testing.T) { |
| 163 | + for key, value := range test.dbEnvs { |
| 164 | + t.Setenv(key, value) |
| 165 | + } |
| 166 | + |
| 167 | + if test.expectError { |
| 168 | + defer func() { |
| 169 | + if r := recover(); r == nil { |
| 170 | + t.Errorf("Expected panic for %s, but did not get one", test.name) |
| 171 | + } |
| 172 | + }() |
| 173 | + } |
| 174 | + |
| 175 | + queuer := NewQueuerWithoutWorker() |
| 176 | + if !test.expectError { |
| 177 | + require.NotNil(t, queuer, "Expected Queuer to be created successfully") |
| 178 | + } |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestStop(t *testing.T) { |
| 184 | + envs := map[string]string{ |
| 185 | + "QUEUER_DB_HOST": "localhost", |
| 186 | + "QUEUER_DB_PORT": dbPort, |
| 187 | + "QUEUER_DB_DATABASE": "database", |
| 188 | + "QUEUER_DB_USERNAME": "user", |
| 189 | + "QUEUER_DB_PASSWORD": "password", |
| 190 | + "QUEUER_DB_SCHEMA": "public", |
| 191 | + } |
| 192 | + for key, value := range envs { |
| 193 | + t.Setenv(key, value) |
| 194 | + } |
| 195 | + |
| 196 | + queuer := NewQueuer("test", 10) |
| 197 | + require.NotNil(t, queuer, "Expected Queuer to be created successfully") |
| 198 | + |
| 199 | + err := queuer.Stop() |
| 200 | + assert.NoError(t, err, "Expected Stop to complete without error") |
| 201 | +} |
0 commit comments