Добавлена возможность асинхронного создания снапшотов
А также обновлены зависимости
This commit is contained in:
115
async_test.go
Normal file
115
async_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package agate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSnapshotAsync(t *testing.T) {
|
||||
// Setup temporary work directory
|
||||
workDir, err := os.MkdirTemp("", "agate_async_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(workDir)
|
||||
|
||||
// Initialize Agate
|
||||
opts := AgateOptions{
|
||||
WorkDir: workDir,
|
||||
Logger: nil, // Disable logging for test
|
||||
}
|
||||
|
||||
ag, err := New(opts)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize Agate: %v", err)
|
||||
}
|
||||
defer ag.Close()
|
||||
|
||||
// Get active directory and create some dummy files
|
||||
activeDir := ag.GetActiveDir()
|
||||
if err := os.MkdirAll(activeDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create active dir: %v", err)
|
||||
}
|
||||
|
||||
// Create a large-ish file to ensure it takes some time (though still fast)
|
||||
dummyFile := filepath.Join(activeDir, "data.bin")
|
||||
data := make([]byte, 1024*1024) // 1MB
|
||||
if err := os.WriteFile(dummyFile, data, 0644); err != nil {
|
||||
t.Fatalf("Failed to create dummy file: %v", err)
|
||||
}
|
||||
|
||||
// Start async snapshot
|
||||
ctx := context.Background()
|
||||
snapshotID, err := ag.SnapshotAsync(ctx, "async-snap", "")
|
||||
if err != nil {
|
||||
t.Fatalf("SnapshotAsync failed: %v", err)
|
||||
}
|
||||
|
||||
if snapshotID == "" {
|
||||
t.Fatal("SnapshotAsync returned empty ID")
|
||||
}
|
||||
|
||||
// Check status immediately
|
||||
status, err := ag.GetSnapshotStatus(ctx, snapshotID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSnapshotStatus failed: %v", err)
|
||||
}
|
||||
|
||||
// Status should be pending or running
|
||||
if status.Status != "pending" && status.Status != "running" {
|
||||
t.Errorf("Initial status should be pending or running, got: %s", status.Status)
|
||||
}
|
||||
|
||||
// Poll for completion
|
||||
timeout := time.After(5 * time.Second)
|
||||
ticker := time.NewTicker(10 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
done := false
|
||||
for !done {
|
||||
select {
|
||||
case <-timeout:
|
||||
t.Fatal("Timeout waiting for snapshot completion")
|
||||
case <-ticker.C:
|
||||
status, err := ag.GetSnapshotStatus(ctx, snapshotID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSnapshotStatus failed during polling: %v", err)
|
||||
}
|
||||
|
||||
if status.Status == "done" {
|
||||
done = true
|
||||
if status.Progress != 1.0 {
|
||||
t.Errorf("Expected progress 1.0, got %f", status.Progress)
|
||||
}
|
||||
} else if status.Status == "failed" {
|
||||
t.Fatalf("Snapshot creation failed: %s", status.Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify snapshot exists
|
||||
snaps, err := ag.ListSnapshots(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListSnapshots failed: %v", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, s := range snaps {
|
||||
if s.ID == snapshotID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Errorf("Snapshot %s not found in list", snapshotID)
|
||||
}
|
||||
|
||||
// Verify current snapshot ID is updated
|
||||
if ag.GetCurrentSnapshotID() != snapshotID {
|
||||
t.Errorf("Current snapshot ID not updated. Expected %s, got %s", snapshotID, ag.GetCurrentSnapshotID())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user