History

bugfixes + test case

Dane Strandboge committed 2 years ago

Commit bc23b4e1 parent c3025142 files · +52 −2Browse files

Unified · Split

2 files
  1. wal.go +11 −2
  2. wal_test.go +41 −0
wal.go+11−2
@@ -1011,14 +1011,23 @@ func (l *Log) IsEmpty() (bool, error) { · 1010 unmodified lines
1011// Clear empties the log, removing all entries and resetting1011// Clear empties the log, removing all entries and resetting
1012// the index back to its default value1012// the index back to its default value
1013func (l *Log) Clear() error {1013func (l *Log) Clear() error {
1014 l.mu.Lock()
1015 defer l.mu.Unlock()
1016
1017 // Clean up segment files
1014 for _, segment := range l.segments {1018 for _, segment := range l.segments {
1015 if err := os.Remove(segment.path); err != nil {1019 if err := os.Remove(segment.path); err != nil {
1016 return err1020 return err
1017 }1021 }
1018 }1022 }
1023
1024 // Clear most state
1025 l.segments = []*segment{}
1019 l.sfile = nil1026 l.sfile = nil
1020 l.firstIndex = 01027 l.firstIndex = 1
1021 l.lastIndex = 01028 l.lastIndex = 0
1022 l.clearCache()1029 l.clearCache()
1023 return nil1030
1031 // Load the log as if it were new again
1032 return l.load()
1024}1033}
wal_test.go+41−0
@@ -924,6 +924,47 @@ func TestSimpleTruncateBack(t *testing.T) { · 923 unmodified lines
924 924
925}925}
926 926
927func TestClear(t *testing.T) {
928 os.RemoveAll("testlog")
929
930 l, err := Open("testlog", &Options{
931 NoSync: true,
932 })
933 if err != nil {
934 t.Fatal(err)
935 }
936 defer l.Close()
937
938 // Write some entries
939 for i := 1; i < 50; i++ {
940 err := l.Write(uint64(i), []byte(dataStr(uint64(i))))
941 if err != nil {
942 t.Fatal(err)
943 }
944 }
945
946 // Clear the log
947 if err := l.Clear(); err != nil {
948 t.Fatal(err)
949 }
950
951 // Validate some log information
952 if i, err := l.FirstIndex(); err != nil || i != 0 {
953 t.Fatal(fmt.Errorf("expected first index %d but got %d (error: %w)", 0, i, err))
954 }
955 if i, err := l.LastIndex(); err != nil || i != 0 {
956 t.Fatal(fmt.Errorf("expected last index %d but got %d (error: %w)", 0, i, err))
957 }
958
959 // Ensure we can continue to write after a clear
960 for i := 1; i < 50; i++ {
961 err := l.Write(uint64(i), []byte(dataStr(uint64(i))))
962 if err != nil {
963 t.Fatal(err)
964 }
965 }
966}
967
927func TestConcurrency(t *testing.T) {968func TestConcurrency(t *testing.T) {
928 os.RemoveAll("testlog")969 os.RemoveAll("testlog")
929 970

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t