| 325 | } |
| 326 | |
| 327 | func TestDB_ConcurrentAccess(t *testing.T) { |
| 328 | tempDir := t.TempDir() |
| 329 | cfg := &config.Config{ |
| 330 | LevelDB: config.LevelDBConfig{ |
| 331 | CacheSize: 64 * 1024 * 1024, |
| 332 | BlockSize: 4 * 1024, |
| 333 | WriteBufferSize: 4 * 1024 * 1024, |
| 334 | MaxOpenFiles: 1000, |
| 335 | Compression: true, |
| 336 | }, |
| 337 | } |
| 338 | |
| 339 | db, err := Store{}.Open(tempDir, cfg) |
| 340 | require.NoError(t, err) |
| 341 | defer db.Close() |
| 342 | |
| 343 | done := make(chan bool) |
| 344 | numGoroutines := 10 |
| 345 | opsPerGoroutine := 100 |
| 346 | |
| 347 | for i := 0; i < numGoroutines; i++ { |
| 348 | go func(id int) { |
| 349 | for j := 0; j < opsPerGoroutine; j++ { |
| 350 | key := []byte(string(rune(id)) + "-" + string(rune(j))) |
| 351 | value := []byte(string(rune(id)) + "-value-" + string(rune(j))) |
| 352 | |
| 353 | err := db.Put(key, value) |
| 354 | assert.NoError(t, err) |
| 355 | |
| 356 | retrieved, err := db.Get(key) |
| 357 | assert.NoError(t, err) |
| 358 | assert.Equal(t, value, retrieved) |
| 359 | } |
| 360 | done <- true |
| 361 | }(i) |
| 362 | } |
| 363 | |
| 364 | // Wait for all goroutines to complete |
| 365 | for i := 0; i < numGoroutines; i++ { |
| 366 | <-done |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | func TestDB_Repair(t *testing.T) { |
| 371 | tempDir := t.TempDir() |