| 269 | } |
| 270 | |
| 271 | func TestWriteBatch_Concurrent(t *testing.T) { |
| 272 | tempDir := t.TempDir() |
| 273 | cfg := &config.Config{ |
| 274 | LevelDB: config.LevelDBConfig{ |
| 275 | CacheSize: 64 * 1024 * 1024, |
| 276 | BlockSize: 4 * 1024, |
| 277 | WriteBufferSize: 4 * 1024 * 1024, |
| 278 | MaxOpenFiles: 1000, |
| 279 | Compression: true, |
| 280 | }, |
| 281 | } |
| 282 | |
| 283 | db, err := Store{}.Open(tempDir, cfg) |
| 284 | require.NoError(t, err) |
| 285 | defer db.Close() |
| 286 | |
| 287 | numBatches := 10 |
| 288 | opsPerBatch := 100 |
| 289 | done := make(chan bool, numBatches) |
| 290 | |
| 291 | for i := 0; i < numBatches; i++ { |
| 292 | go func(batchID int) { |
| 293 | wb := db.NewWriteBatch() |
| 294 | defer wb.Close() |
| 295 | |
| 296 | for j := 0; j < opsPerBatch; j++ { |
| 297 | key := []byte(string(rune(batchID)) + "-" + string(rune(j))) |
| 298 | value := []byte(string(rune(batchID)) + "-value-" + string(rune(j))) |
| 299 | |
| 300 | wb.Put(key, value) |
| 301 | } |
| 302 | |
| 303 | err := wb.Commit() |
| 304 | assert.NoError(t, err) |
| 305 | done <- true |
| 306 | }(i) |
| 307 | } |
| 308 | |
| 309 | // Wait for all batches to complete |
| 310 | for i := 0; i < numBatches; i++ { |
| 311 | <-done |
| 312 | } |
| 313 | |
| 314 | // Verify all data was written |
| 315 | for i := 0; i < numBatches; i++ { |
| 316 | for j := 0; j < opsPerBatch; j++ { |
| 317 | key := []byte(string(rune(i)) + "-" + string(rune(j))) |
| 318 | expectedValue := []byte(string(rune(i)) + "-value-" + string(rune(j))) |
| 319 | |
| 320 | value, err := db.Get(key) |
| 321 | require.NoError(t, err) |
| 322 | assert.Equal(t, expectedValue, value) |
| 323 | } |
| 324 | } |
| 325 | } |