Skip to content

Commit

Permalink
FollowTail: adjust test
Browse files Browse the repository at this point in the history
  • Loading branch information
nexoscp committed Aug 30, 2022
1 parent 4424ec3 commit 33e4a56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
27 changes: 16 additions & 11 deletions sdjournal/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io/ioutil"
"math/rand"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -86,12 +87,17 @@ func TestJournalFollow(t *testing.T) {
}

func TestJournalFollowTail(t *testing.T) {
documentation := "https://github.com/coreos/go-systemd/"
r, err := NewJournalReader(JournalReaderConfig{
Since: time.Duration(-15) * time.Second,
Matches: []Match{
{
Field: SD_JOURNAL_FIELD_SYSTEMD_UNIT,
Value: "NetworkManager.service",
Field: SD_JOURNAL_FIELD_PRIORITY,
Value: strconv.Itoa(int(journal.PriInfo)),
},
{
Field: "DOCUMENTATION",
Value: documentation,
},
},
})
Expand All @@ -109,35 +115,34 @@ func TestJournalFollowTail(t *testing.T) {
// start writing some test entries
done := make(chan struct{}, 1)
errCh := make(chan error, 1)
defer close(done)
go func() {
for {
select {
case <-done:
return
default:
if perr := journal.Print(journal.PriInfo, "test message %s", time.Now()); err != nil {
vars := make(map[string]string)
vars["DOCUMENTATION"] = documentation
if perr := journal.Send(fmt.Sprintf("test message %s", time.Now()), journal.PriInfo, vars); perr != nil {
errCh <- perr
return
}

time.Sleep(time.Second)
}
}
}()

// and follow the reader synchronously
entries := make(chan *JournalEntry)
timeout := time.Duration(5) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5)*time.Second)
defer cancel()
if err = r.FollowTail(entries, ctx); err != nil {
t.Fatalf("Error during follow: %s", err)
}
go r.FollowTail(entries, errCh, ctx)

select {
case err := <-errCh:
t.Fatalf("Error writing to journal: %s", err)
case entry := <-entries:
t.Log("received: " + entry.Cursor)
return
default:
}
}
Expand Down
24 changes: 12 additions & 12 deletions sdjournal/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,19 @@ func (r *JournalReader) FollowTail(entries chan<- *JournalEntry, errors chan<- e
continue
}

for {
if c, err := r.journal.Next(); err != nil {
errors <- err
} else if c == 0 {
// EOF, should mean we're at the tail
break
}
if c, err := r.journal.Next(); err != nil {
errors <- err
continue
} else if c == 0 {
// EOF, should mean we're at the tail
break
}

if entry, err := r.journal.GetEntry(); err != nil {
errors <- err
} else {
entries <- entry
}
if entry, err := r.journal.GetEntry(); err != nil {
errors <- err
continue
} else {
entries <- entry
}
}
}
Expand Down

0 comments on commit 33e4a56

Please sign in to comment.