Skip to content

Commit da563e0

Browse files
authored
Merge pull request #239 from adrianlzt/fix/TestCursor
sdjournal: support testing cursor against current position
2 parents d219646 + f640d19 commit da563e0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

sdjournal/journal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ package sdjournal
303303
import "C"
304304
import (
305305
"bytes"
306+
"errors"
306307
"fmt"
307308
"strings"
308309
"sync"
@@ -370,6 +371,12 @@ const (
370371
IndefiniteWait time.Duration = 1<<63 - 1
371372
)
372373

374+
var (
375+
// Error show when using TestCursor function and cursor parameter is not the same
376+
// as the current cursor position
377+
ErrNoTestCursor = errors.New("Cursor parameter is not the same as current position")
378+
)
379+
373380
// Journal is a Go wrapper of an sd_journal structure.
374381
type Journal struct {
375382
cjournal *C.sd_journal
@@ -879,6 +886,8 @@ func (j *Journal) TestCursor(cursor string) error {
879886

880887
if r < 0 {
881888
return fmt.Errorf("failed to test to cursor %q: %d", cursor, syscall.Errno(-r))
889+
} else if r == 0 {
890+
return ErrNoTestCursor
882891
}
883892

884893
return nil

sdjournal/journal_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ func TestJournalCursorGetSeekAndTest(t *testing.T) {
178178
if err != nil {
179179
t.Fatalf("Error testing cursor to journal: %s", err)
180180
}
181+
182+
err = journal.Print(journal.PriInfo, "second message %s", time.Now())
183+
if err != nil {
184+
t.Fatalf("Error writing to journal: %s", err)
185+
}
186+
187+
if err = waitAndNext(j); err != nil {
188+
t.Fatalf(err.Error())
189+
}
190+
191+
err = j.TestCursor(c)
192+
if err != ErrNoTestCursor {
193+
t.Fatalf("Error, TestCursor should fail because current cursor has moved from the previous obtained cursor")
194+
}
181195
}
182196

183197
func TestNewJournalFromDir(t *testing.T) {

0 commit comments

Comments
 (0)