forked from hashicorp/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing and fixing the file snapshots
- Loading branch information
Showing
2 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,122 @@ | ||
package raft | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestFileSnapshotStoreImpl(t *testing.T) { | ||
var impl interface{} = &FileSnapshotStore{} | ||
if _, ok := impl.(SnapshotStore); !ok { | ||
t.Fatalf("FileSnapshotStore not a SnapshotStore") | ||
} | ||
} | ||
|
||
func TestFileSnapshotSinkImpl(t *testing.T) { | ||
var impl interface{} = &FileSnapshotSink{} | ||
if _, ok := impl.(SnapshotSink); !ok { | ||
t.Fatalf("FileSnapshotSink not a SnapshotSink") | ||
} | ||
} | ||
|
||
func TestFileSS_CreateSnapshot(t *testing.T) { | ||
// Create a test dir | ||
dir, err := ioutil.TempDir("", "raft") | ||
if err != nil { | ||
t.Fatalf("err: %v ", err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
snap, err := NewFileSnapshotStore(dir, 3) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Check no snapshots | ||
snaps, err := snap.List() | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
if len(snaps) != 0 { | ||
t.Fatalf("did not expect any snapshots: %v", snaps) | ||
} | ||
|
||
// Create a new sink | ||
peers := []byte("all my lovely friends") | ||
sink, err := snap.Create(10, 3, peers) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// The sink is not done, should not be in a list! | ||
snaps, err = snap.List() | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
if len(snaps) != 0 { | ||
t.Fatalf("did not expect any snapshots: %v", snaps) | ||
} | ||
|
||
// Write to the sink | ||
_, err = sink.Write([]byte("first\n")) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
_, err = sink.Write([]byte("second\n")) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Done! | ||
err = sink.Close() | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Should have a snapshot! | ||
snaps, err = snap.List() | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
if len(snaps) != 1 { | ||
t.Fatalf("expect a snapshots: %v", snaps) | ||
} | ||
|
||
// Check the latest | ||
latest := snaps[0] | ||
if latest.Index != 10 { | ||
t.Fatalf("bad snapshot: %v", *latest) | ||
} | ||
if latest.Term != 3 { | ||
t.Fatalf("bad snapshot: %v", *latest) | ||
} | ||
if bytes.Compare(latest.Peers, peers) != 0 { | ||
t.Fatalf("bad snapshot: %v", *latest) | ||
} | ||
if latest.Size != 13 { | ||
t.Fatalf("bad snapshot: %v", *latest) | ||
} | ||
|
||
// Read the snapshot | ||
r, err := snap.Open(latest.ID) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Read out everything | ||
var buf bytes.Buffer | ||
if _, err := io.Copy(&buf, r); err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
if err := r.Close(); err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Ensure a match | ||
if bytes.Compare(buf.Bytes(), []byte("first\nsecond\n")) != 0 { | ||
t.Fatalf("content mismatch") | ||
} | ||
} |