MCPcopy
hub / github.com/wavetermdev/waveterm / TestCircularWrites

Function TestCircularWrites

pkg/filestore/blockstore_test.go:417–493  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

415}
416
417func TestCircularWrites(t *testing.T) {
418 initDb(t)
419 defer cleanupDb(t)
420 ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
421 defer cancelFn()
422 zoneId := uuid.NewString()
423 err := WFS.MakeFile(ctx, zoneId, "c1", nil, wshrpc.FileOpts{Circular: true, MaxSize: 50})
424 if err != nil {
425 t.Fatalf("error creating file: %v", err)
426 }
427 err = WFS.WriteFile(ctx, zoneId, "c1", []byte("123456789 123456789 123456789 123456789 123456789 "))
428 if err != nil {
429 t.Fatalf("error writing data: %v", err)
430 }
431 checkFileData(t, ctx, zoneId, "c1", "123456789 123456789 123456789 123456789 123456789 ")
432 err = WFS.AppendData(ctx, zoneId, "c1", []byte("apple"))
433 if err != nil {
434 t.Fatalf("error appending data: %v", err)
435 }
436 checkFileData(t, ctx, zoneId, "c1", "6789 123456789 123456789 123456789 123456789 apple")
437 err = WFS.WriteAt(ctx, zoneId, "c1", 0, []byte("foo"))
438 if err != nil {
439 t.Fatalf("error writing data: %v", err)
440 }
441 // content should be unchanged because write is before the beginning of circular offset
442 checkFileData(t, ctx, zoneId, "c1", "6789 123456789 123456789 123456789 123456789 apple")
443 err = WFS.WriteAt(ctx, zoneId, "c1", 5, []byte("a"))
444 if err != nil {
445 t.Fatalf("error writing data: %v", err)
446 }
447 checkFileSize(t, ctx, zoneId, "c1", 55)
448 checkFileData(t, ctx, zoneId, "c1", "a789 123456789 123456789 123456789 123456789 apple")
449 err = WFS.AppendData(ctx, zoneId, "c1", []byte(" banana"))
450 if err != nil {
451 t.Fatalf("error appending data: %v", err)
452 }
453 checkFileSize(t, ctx, zoneId, "c1", 62)
454 checkFileData(t, ctx, zoneId, "c1", "3456789 123456789 123456789 123456789 apple banana")
455 err = WFS.WriteAt(ctx, zoneId, "c1", 20, []byte("foo"))
456 if err != nil {
457 t.Fatalf("error writing data: %v", err)
458 }
459 checkFileSize(t, ctx, zoneId, "c1", 62)
460 checkFileData(t, ctx, zoneId, "c1", "3456789 foo456789 123456789 123456789 apple banana")
461 offset, _, _ := WFS.ReadFile(ctx, zoneId, "c1")
462 if offset != 12 {
463 t.Errorf("offset mismatch: expected 12, got %d", offset)
464 }
465 err = WFS.AppendData(ctx, zoneId, "c1", []byte(" world"))
466 if err != nil {
467 t.Fatalf("error appending data: %v", err)
468 }
469 checkFileSize(t, ctx, zoneId, "c1", 68)
470 offset, _, _ = WFS.ReadFile(ctx, zoneId, "c1")
471 if offset != 18 {
472 t.Errorf("offset mismatch: expected 18, got %d", offset)
473 }
474 checkFileData(t, ctx, zoneId, "c1", "9 foo456789 123456789 123456789 apple banana world")

Callers

nothing calls this directly

Calls 10

initDbFunction · 0.85
cleanupDbFunction · 0.85
checkFileDataFunction · 0.85
withLockFunction · 0.85
MakeFileMethod · 0.80
WriteFileMethod · 0.80
AppendDataMethod · 0.80
WriteAtMethod · 0.80
ReadFileMethod · 0.80
checkFileSizeFunction · 0.70

Tested by

no test coverage detected