Skip to content

Commit f640d19

Browse files
committed
Fix TestCursor when cursor is valid
This fixes TestCursor so it returns an error when the ``cursor`` parameter is not the same as the current position. From SD_JOURNAL_GET_CURSOR(3): ``` sd_journal_test_cursor() returns positive if the current entry matches the specified cursor, 0 if it does not match the specified cursor or a negative errno-style error code on failure. ```
1 parent d219646 commit f640d19

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)