Skip to content

Commit

Permalink
Add basic test case for FollowTail
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Herm authored and nexoscp committed Aug 25, 2022
1 parent 8ffac0e commit 56bddae
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions sdjournal/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sdjournal

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -84,6 +85,63 @@ func TestJournalFollow(t *testing.T) {
}
}

func TestJournalFollowTail(t *testing.T) {
r, err := NewJournalReader(JournalReaderConfig{
Since: time.Duration(-15) * time.Second,
Matches: []Match{
{
Field: SD_JOURNAL_FIELD_SYSTEMD_UNIT,
Value: "NetworkManager.service",
},
},
})

if err != nil {
t.Fatalf("Error opening journal: %s", err)
}

if r == nil {
t.Fatal("Got a nil reader")
}

defer r.Close()

// 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 {
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)
defer cancel()
if err = r.FollowTail(entries, ctx); err != nil {
t.Fatalf("Error during follow: %s", err)
}

select {
case err := <-errCh:
t.Fatalf("Error writing to journal: %s", err)
default:
}
}

func TestJournalWait(t *testing.T) {
id := time.Now().String()
j, err := NewJournal()
Expand Down

0 comments on commit 56bddae

Please sign in to comment.