Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/eventlog/eventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func Open(path string) (*Log, error) {
func (l *Log) Append(typ string, data []byte) (uint64, error) {
l.mu.Lock()
defer l.mu.Unlock()
l.last++
e := Entry{Seq: l.last, Type: typ, Data: append([]byte(nil), data...), TS: time.Now().UnixMilli()}
e := Entry{Seq: l.last + 1, Type: typ, Data: append([]byte(nil), data...), TS: time.Now().UnixMilli()}
b, err := json.Marshal(e)
if err != nil {
return 0, err
Expand All @@ -86,6 +85,7 @@ func (l *Log) Append(typ string, data []byte) (uint64, error) {
if err := l.w.Flush(); err != nil {
return 0, err
}
l.last = e.Seq
l.entries = append(l.entries, e)
return e.Seq, nil
}
Expand Down
30 changes: 30 additions & 0 deletions internal/eventlog/eventlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,33 @@ func TestOpenTruncatesCorruptTail(t *testing.T) {
}
}
}

// A failed Append must not advance LastSeq() past a write Since() cannot answer.
func TestFailedAppendDoesNotAdvanceLastSeq(t *testing.T) {
p := filepath.Join(t.TempDir(), "s.log")
l, err := Open(p)
if err != nil {
t.Fatal(err)
}
if s, err := l.Append("output", []byte("a")); err != nil || s != 1 {
t.Fatalf("first Append = %d, %v, want 1, nil", s, err)
}

// Close the underlying file to force the next write to fail deterministically.
l.f.Close()

if _, err := l.Append("output", []byte("b")); err == nil {
t.Fatal("Append over a closed file returned nil error, want a write failure")
}

if got := l.LastSeq(); got != 1 {
t.Fatalf("LastSeq() after failed Append = %d, want 1 (unchanged)", got)
}
got, err := l.Since(0)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Seq != 1 {
t.Fatalf("Since(0) after failed Append = %+v, want exactly seq 1", got)
}
}