@@ -17,6 +17,7 @@ package sdjournal
1717
1818import (
1919 "bytes"
20+ "context"
2021 "errors"
2122 "fmt"
2223 "io"
@@ -84,6 +85,63 @@ func TestJournalFollow(t *testing.T) {
8485 }
8586}
8687
88+ func TestJournalFollowTail (t * testing.T ) {
89+ r , err := NewJournalReader (JournalReaderConfig {
90+ Since : time .Duration (- 15 ) * time .Second ,
91+ Matches : []Match {
92+ {
93+ Field : SD_JOURNAL_FIELD_SYSTEMD_UNIT ,
94+ Value : "NetworkManager.service" ,
95+ },
96+ },
97+ })
98+
99+ if err != nil {
100+ t .Fatalf ("Error opening journal: %s" , err )
101+ }
102+
103+ if r == nil {
104+ t .Fatal ("Got a nil reader" )
105+ }
106+
107+ defer r .Close ()
108+
109+ // start writing some test entries
110+ done := make (chan struct {}, 1 )
111+ errCh := make (chan error , 1 )
112+ defer close (done )
113+ go func () {
114+ for {
115+ select {
116+ case <- done :
117+ return
118+ default :
119+ if perr := journal .Print (journal .PriInfo , "test message %s" , time .Now ()); err != nil {
120+ errCh <- perr
121+ return
122+ }
123+
124+ time .Sleep (time .Second )
125+ }
126+ }
127+ }()
128+
129+ // and follow the reader synchronously
130+ entries := make (chan * JournalEntry )
131+ timeout := time .Duration (5 ) * time .Second
132+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
133+ defer cancel ()
134+ if err = r .FollowTail (entries , ctx ); err != nil {
135+ t .Fatalf ("Error during follow: %s" , err )
136+ }
137+
138+ select {
139+ case err := <- errCh :
140+ t .Fatalf ("Error writing to journal: %s" , err )
141+ default :
142+ }
143+ }
144+
87145func TestJournalWait (t * testing.T ) {
88146 id := time .Now ().String ()
89147 j , err := NewJournal ()
0 commit comments