From 33e4a56e4c1a8136aec279ad835ce7a585deca00 Mon Sep 17 00:00:00 2001 From: "Dutkowski, Bernd" Date: Thu, 25 Aug 2022 15:57:43 +0200 Subject: [PATCH] FollowTail: adjust test --- sdjournal/journal_test.go | 27 ++++++++++++++++----------- sdjournal/read.go | 24 ++++++++++++------------ 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/sdjournal/journal_test.go b/sdjournal/journal_test.go index d559f91..693f4d6 100755 --- a/sdjournal/journal_test.go +++ b/sdjournal/journal_test.go @@ -24,6 +24,7 @@ import ( "io/ioutil" "math/rand" "os" + "strconv" "strings" "testing" "time" @@ -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, }, }, }) @@ -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: } } diff --git a/sdjournal/read.go b/sdjournal/read.go index 100208c..879bbbd 100644 --- a/sdjournal/read.go +++ b/sdjournal/read.go @@ -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 } } }