Skip to content

Commit 8d075cd

Browse files
committed
fix(test): reuse ReadLogFileAndSearchString for auto-port log; throttle poll loop
Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
1 parent b9495a3 commit 8d075cd

File tree

3 files changed

+61
-36
lines changed

3 files changed

+61
-36
lines changed

pkg/api/controller_test.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -588,31 +588,17 @@ func TestAutoPortSelection(t *testing.T) {
588588

589589
defer cm.StopServer()
590590

591-
scanner := bufio.NewScanner(logFile)
592-
593-
var contents bytes.Buffer
594-
595-
deadline := time.Now().Add(30 * time.Second)
596-
597-
for scanner.Scan() {
598-
text := scanner.Text()
599-
contents.WriteString(text)
600-
601-
if strings.Contains(text, "port is unspecified") {
602-
break
603-
}
604-
605-
if time.Now().After(deadline) {
606-
t.Fatalf("timed out waiting for kernel-chosen port log line")
607-
}
608-
}
591+
found, err := test.ReadLogFileAndSearchString(logFile.Name(), "port is unspecified", 30*time.Second)
592+
So(err, ShouldBeNil)
593+
So(found, ShouldBeTrue)
609594

610-
So(scanner.Err(), ShouldBeNil)
611-
So(contents.String(), ShouldContainSubstring,
595+
contents, err := os.ReadFile(logFile.Name())
596+
So(err, ShouldBeNil)
597+
So(string(contents), ShouldContainSubstring,
612598
"port is unspecified, listening on kernel chosen port",
613599
)
614-
So(contents.String(), ShouldContainSubstring, "\"address\":\"127.0.0.1\"")
615-
So(contents.String(), ShouldContainSubstring, "\"port\":")
600+
So(string(contents), ShouldContainSubstring, "\"address\":\"127.0.0.1\"")
601+
So(string(contents), ShouldContainSubstring, "\"port\":")
616602

617603
So(ctlr.GetPort(), ShouldBeGreaterThan, 0)
618604
So(ctlr.GetPort(), ShouldBeLessThan, 65536)

pkg/test/common/fs.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,29 @@ func ReadLogFileAndSearchString(logPath string, stringToMatch string, timeout ti
179179
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
180180
defer cancelFunc()
181181

182+
ticker := time.NewTicker(10 * time.Millisecond)
183+
defer ticker.Stop()
184+
182185
for {
183186
select {
184187
case <-ctx.Done():
185188
return false, nil
186189
default:
187-
content, err := os.ReadFile(logPath)
188-
if err != nil {
189-
return false, err
190-
}
190+
}
191191

192-
if strings.Contains(string(content), stringToMatch) {
193-
return true, nil
194-
}
192+
content, err := os.ReadFile(logPath)
193+
if err != nil {
194+
return false, err
195+
}
196+
197+
if strings.Contains(string(content), stringToMatch) {
198+
return true, nil
199+
}
200+
201+
select {
202+
case <-ctx.Done():
203+
return false, nil
204+
case <-ticker.C:
195205
}
196206
}
197207
}
@@ -202,19 +212,29 @@ func ReadLogFileAndCountStringOccurence(logPath string, stringToMatch string,
202212
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
203213
defer cancelFunc()
204214

215+
ticker := time.NewTicker(10 * time.Millisecond)
216+
defer ticker.Stop()
217+
205218
for {
206219
select {
207220
case <-ctx.Done():
208221
return false, nil
209222
default:
210-
content, err := os.ReadFile(logPath)
211-
if err != nil {
212-
return false, err
213-
}
223+
}
214224

215-
if strings.Count(string(content), stringToMatch) >= count {
216-
return true, nil
217-
}
225+
content, err := os.ReadFile(logPath)
226+
if err != nil {
227+
return false, err
228+
}
229+
230+
if strings.Count(string(content), stringToMatch) >= count {
231+
return true, nil
232+
}
233+
234+
select {
235+
case <-ctx.Done():
236+
return false, nil
237+
case <-ticker.C:
218238
}
219239
}
220240
}

pkg/test/common/fs_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ func TestReadLogFileAndSearchString(t *testing.T) {
145145
So(err, ShouldBeNil)
146146
So(ok, ShouldBeFalse)
147147
})
148+
149+
Convey("substring appears after append", t, func() {
150+
logPath := path.Join(t.TempDir(), "log.txt")
151+
So(os.WriteFile(logPath, []byte("header\n"), 0o600), ShouldBeNil)
152+
153+
go func() {
154+
time.Sleep(40 * time.Millisecond)
155+
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, 0o600)
156+
if err != nil {
157+
return
158+
}
159+
defer f.Close()
160+
_, _ = f.WriteString("line with port is unspecified\n")
161+
}()
162+
163+
ok, err := tcommon.ReadLogFileAndSearchString(logPath, "port is unspecified", 500*time.Millisecond)
164+
So(err, ShouldBeNil)
165+
So(ok, ShouldBeTrue)
166+
})
148167
}
149168

150169
func TestReadLogFileAndCountStringOccurence(t *testing.T) {

0 commit comments

Comments
 (0)