-
Notifications
You must be signed in to change notification settings - Fork 1
fix: persist snapshot state across restarts to prevent incomplete win… #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
peatey
wants to merge
3
commits into
develop
Choose a base branch
from
fix/snapshot-state-persistence
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package emitter | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestSnapshotStatePersistAndRecover(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir, err := os.MkdirTemp("", "snapshot-state-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| // Create a snapshot config with the temp directory | ||
| config := &SnapshotConfig{ | ||
| ScratchDir: tempDir, | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| // Create first provider and set a timestamp | ||
| provider1 := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
| testTime := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC) | ||
| provider1.lastSnapshot = testTime | ||
|
|
||
| // Persist the state | ||
| if err := provider1.PersistState(); err != nil { | ||
| t.Fatalf("Failed to persist state: %v", err) | ||
| } | ||
|
|
||
| // Verify the state file was created | ||
| stateFile := filepath.Join(tempDir, snapshotStateFilename) | ||
| if _, err := os.Stat(stateFile); os.IsNotExist(err) { | ||
| t.Fatalf("State file was not created") | ||
| } | ||
|
|
||
| // Create a new provider and verify it recovers the state | ||
| provider2 := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
| if !provider2.lastSnapshot.Equal(testTime) { | ||
| t.Errorf("Expected recovered timestamp %v, got %v", testTime, provider2.lastSnapshot) | ||
| } | ||
| } | ||
|
|
||
| func TestSnapshotStateRecoverMissingFile(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir, err := os.MkdirTemp("", "snapshot-state-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| config := &SnapshotConfig{ | ||
| ScratchDir: tempDir, | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| // Create provider without any existing state file | ||
| provider := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
|
|
||
| // Verify lastSnapshot is zero (cold start) | ||
| if !provider.lastSnapshot.IsZero() { | ||
| t.Errorf("Expected zero timestamp on cold start, got %v", provider.lastSnapshot) | ||
| } | ||
| } | ||
|
|
||
| func TestSnapshotStateRecoverCorruptFile(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir, err := os.MkdirTemp("", "snapshot-state-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| // Write corrupt data to the state file | ||
| stateFile := filepath.Join(tempDir, snapshotStateFilename) | ||
| if err := os.WriteFile(stateFile, []byte("invalid json {{{"), 0644); err != nil { | ||
| t.Fatalf("Failed to write corrupt state file: %v", err) | ||
| } | ||
|
|
||
| config := &SnapshotConfig{ | ||
| ScratchDir: tempDir, | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| // Create provider with corrupt state file | ||
| provider := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
|
|
||
| // Verify lastSnapshot is zero (cold start due to corrupt file) | ||
| if !provider.lastSnapshot.IsZero() { | ||
| t.Errorf("Expected zero timestamp on corrupt file, got %v", provider.lastSnapshot) | ||
| } | ||
| } | ||
|
|
||
| func TestSnapshotStatePersistWithoutScratchDir(t *testing.T) { | ||
| config := &SnapshotConfig{ | ||
| ScratchDir: "", // No scratch directory configured | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| provider := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
| provider.lastSnapshot = time.Now() | ||
|
|
||
| // Attempt to persist should return an error | ||
| err := provider.PersistState() | ||
| if err == nil { | ||
| t.Error("Expected error when persisting without scratch directory, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestSnapshotStateFileFormat(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir, err := os.MkdirTemp("", "snapshot-state-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| config := &SnapshotConfig{ | ||
| ScratchDir: tempDir, | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| provider := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
| testTime := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC) | ||
| provider.lastSnapshot = testTime | ||
|
|
||
| // Persist the state | ||
| if err := provider.PersistState(); err != nil { | ||
| t.Fatalf("Failed to persist state: %v", err) | ||
| } | ||
|
|
||
| // Read and verify the file format | ||
| stateFile := filepath.Join(tempDir, snapshotStateFilename) | ||
| data, err := os.ReadFile(stateFile) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read state file: %v", err) | ||
| } | ||
|
|
||
| var state SnapshotState | ||
| if err := json.Unmarshal(data, &state); err != nil { | ||
| t.Fatalf("Failed to unmarshal state file: %v", err) | ||
| } | ||
|
|
||
| if !state.LastSnapshot.Equal(testTime) { | ||
| t.Errorf("Expected timestamp %v in file, got %v", testTime, state.LastSnapshot) | ||
| } | ||
| } | ||
|
|
||
| func TestSnapshotStateMultiplePersists(t *testing.T) { | ||
| // Create a temporary directory for testing | ||
| tempDir, err := os.MkdirTemp("", "snapshot-state-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tempDir) | ||
|
|
||
| config := &SnapshotConfig{ | ||
| ScratchDir: tempDir, | ||
| Now: defaultNow, | ||
| } | ||
|
|
||
| provider := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
|
|
||
| // Persist multiple times with different timestamps | ||
| time1 := time.Date(2024, 1, 15, 10, 0, 0, 0, time.UTC) | ||
| provider.lastSnapshot = time1 | ||
| if err := provider.PersistState(); err != nil { | ||
| t.Fatalf("Failed to persist state (first): %v", err) | ||
| } | ||
|
|
||
| time2 := time.Date(2024, 1, 15, 11, 0, 0, 0, time.UTC) | ||
| provider.lastSnapshot = time2 | ||
| if err := provider.PersistState(); err != nil { | ||
| t.Fatalf("Failed to persist state (second): %v", err) | ||
| } | ||
|
|
||
| // Create new provider and verify it has the latest timestamp | ||
| provider2 := NewConcurrentSnapshotProvider(config).(*ConcurrentSnapshotProvider) | ||
| if !provider2.lastSnapshot.Equal(time2) { | ||
| t.Errorf("Expected recovered timestamp %v, got %v", time2, provider2.lastSnapshot) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.