Skip to content

Commit 33e4a56

Browse files
committed
FollowTail: adjust test
1 parent 4424ec3 commit 33e4a56

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

sdjournal/journal_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io/ioutil"
2525
"math/rand"
2626
"os"
27+
"strconv"
2728
"strings"
2829
"testing"
2930
"time"
@@ -86,12 +87,17 @@ func TestJournalFollow(t *testing.T) {
8687
}
8788

8889
func TestJournalFollowTail(t *testing.T) {
90+
documentation := "https://github.com/coreos/go-systemd/"
8991
r, err := NewJournalReader(JournalReaderConfig{
9092
Since: time.Duration(-15) * time.Second,
9193
Matches: []Match{
9294
{
93-
Field: SD_JOURNAL_FIELD_SYSTEMD_UNIT,
94-
Value: "NetworkManager.service",
95+
Field: SD_JOURNAL_FIELD_PRIORITY,
96+
Value: strconv.Itoa(int(journal.PriInfo)),
97+
},
98+
{
99+
Field: "DOCUMENTATION",
100+
Value: documentation,
95101
},
96102
},
97103
})
@@ -109,35 +115,34 @@ func TestJournalFollowTail(t *testing.T) {
109115
// start writing some test entries
110116
done := make(chan struct{}, 1)
111117
errCh := make(chan error, 1)
112-
defer close(done)
113118
go func() {
114119
for {
115120
select {
116121
case <-done:
117122
return
118123
default:
119-
if perr := journal.Print(journal.PriInfo, "test message %s", time.Now()); err != nil {
124+
vars := make(map[string]string)
125+
vars["DOCUMENTATION"] = documentation
126+
if perr := journal.Send(fmt.Sprintf("test message %s", time.Now()), journal.PriInfo, vars); perr != nil {
120127
errCh <- perr
121128
return
122129
}
123-
124130
time.Sleep(time.Second)
125131
}
126132
}
127133
}()
128134

129-
// and follow the reader synchronously
130135
entries := make(chan *JournalEntry)
131-
timeout := time.Duration(5) * time.Second
132-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
136+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5)*time.Second)
133137
defer cancel()
134-
if err = r.FollowTail(entries, ctx); err != nil {
135-
t.Fatalf("Error during follow: %s", err)
136-
}
138+
go r.FollowTail(entries, errCh, ctx)
137139

138140
select {
139141
case err := <-errCh:
140142
t.Fatalf("Error writing to journal: %s", err)
143+
case entry := <-entries:
144+
t.Log("received: " + entry.Cursor)
145+
return
141146
default:
142147
}
143148
}

sdjournal/read.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,19 @@ func (r *JournalReader) FollowTail(entries chan<- *JournalEntry, errors chan<- e
294294
continue
295295
}
296296

297-
for {
298-
if c, err := r.journal.Next(); err != nil {
299-
errors <- err
300-
} else if c == 0 {
301-
// EOF, should mean we're at the tail
302-
break
303-
}
297+
if c, err := r.journal.Next(); err != nil {
298+
errors <- err
299+
continue
300+
} else if c == 0 {
301+
// EOF, should mean we're at the tail
302+
break
303+
}
304304

305-
if entry, err := r.journal.GetEntry(); err != nil {
306-
errors <- err
307-
} else {
308-
entries <- entry
309-
}
305+
if entry, err := r.journal.GetEntry(); err != nil {
306+
errors <- err
307+
continue
308+
} else {
309+
entries <- entry
310310
}
311311
}
312312
}

0 commit comments

Comments
 (0)