MCPcopy Index your code
hub / github.com/nutsdb/nutsdb / TestWriteBatch_ConcurrentWithUpdate

Function TestWriteBatch_ConcurrentWithUpdate

batch_test.go:285–351  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

283}
284
285func TestWriteBatch_ConcurrentWithUpdate(t *testing.T) {
286 db, err := Open(
287 DefaultOptions,
288 WithDir(t.TempDir()),
289 )
290 require.NoError(t, err)
291 defer func() { require.NoError(t, db.Close()) }()
292
293 txCreateBucket(t, db, DataStructureBTree, "test-bucket", nil)
294
295 const iterations = 1000
296 const goroutines = 2
297 var ready sync.WaitGroup
298 var start sync.WaitGroup
299 done := make(chan error, goroutines)
300
301 ready.Add(goroutines)
302 start.Add(1)
303
304 // WriteBatch goroutine
305 go func() {
306 ready.Done()
307 start.Wait()
308 for i := 0; i < iterations; i++ {
309 wb, err := db.NewWriteBatch()
310 if err != nil {
311 done <- err
312 return
313 }
314 key := []byte(fmt.Sprintf("wb-key-%d", i))
315 if err := wb.Put("test-bucket", key, []byte("value"), 0); err != nil {
316 _ = wb.Cancel()
317 done <- err
318 return
319 }
320 if err := wb.Flush(); err != nil {
321 done <- err
322 return
323 }
324 }
325 done <- nil
326 }()
327
328 // db.Update goroutine
329 go func() {
330 ready.Done()
331 start.Wait()
332 for i := 0; i < iterations; i++ {
333 err := db.Update(func(tx *Tx) error {
334 key := []byte(fmt.Sprintf("update-key-%d", i))
335 return tx.Put("test-bucket", key, []byte("value"), 0)
336 })
337 if err != nil {
338 done <- err
339 return
340 }
341 }
342 done <- nil

Callers

nothing calls this directly

Calls 13

PutMethod · 0.95
CancelMethod · 0.95
FlushMethod · 0.95
OpenFunction · 0.85
WithDirFunction · 0.85
txCreateBucketFunction · 0.85
WaitMethod · 0.80
NewWriteBatchMethod · 0.80
UpdateMethod · 0.80
CloseMethod · 0.65
AddMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected