Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wait log exact number occurences #298

Conversation

FlorianLehmann
Copy link
Contributor

@FlorianLehmann FlorianLehmann commented Mar 20, 2021

#297

Describe the bug
When I define

NewLogStrategy("").
WithStartupTimeout(100 * time.Microsecond).
WithOccurrence(X)

for a container, it does not wait for X occurences. One occurence is enough to continue.

To Reproduce

func TestWaitShouldFailWithExactNumberOfOccurrences(t *testing.T) {
	target := noopStrategyTarget{
		ioReaderCloser: ioutil.NopCloser(bytes.NewReader([]byte("hello\r\ndude"))),
	}
	wg := NewLogStrategy("dude").
		WithStartupTimeout(100 * time.Microsecond).
		WithOccurrence(2)
	err := wg.WaitUntilReady(context.Background(), target)
	if err == nil {
		t.Fatal("expected error")
	}
}

Expected behavior
it must wait for X occurences in the logs.

Additional context

currentOccurence := 0
LOOP:
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
reader, err := target.Logs(ctx)
if err != nil {
time.Sleep(ws.PollInterval)
continue
}
b, err := ioutil.ReadAll(reader)
logs := string(b)
if strings.Contains(logs, ws.Log) {
currentOccurence++
if ws.Occurrence == 0 || currentOccurence >= ws.Occurrence-1 {
break LOOP
}

There is a variable currentOccurence defined before the loop. This variable is incremented in the loop each time the expected log appears. However, logs are read from the beginning. Therefore, the condition based on the number of occurences is always true if the expected log is present only once.

@codecov
Copy link

codecov bot commented Mar 20, 2021

Codecov Report

Merging #298 (0b93d46) into master (6467d9b) will decrease coverage by 1.21%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #298      +/-   ##
==========================================
- Coverage   64.04%   62.82%   -1.22%     
==========================================
  Files          13       13              
  Lines         865      877      +12     
==========================================
- Hits          554      551       -3     
- Misses        226      242      +16     
+ Partials       85       84       -1     
Impacted Files Coverage Δ
wait/log.go 75.75% <100.00%> (-2.03%) ⬇️
wait/host_port.go 56.14% <0.00%> (-4.28%) ⬇️
wait/http.go 56.04% <0.00%> (-1.43%) ⬇️
docker.go 65.20% <0.00%> (-0.62%) ⬇️
wait/sql.go 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6467d9b...0b93d46. Read the comment docs.

@gianarb gianarb merged commit 9556f2f into testcontainers:master Apr 7, 2021
@gianarb
Copy link
Collaborator

gianarb commented Apr 7, 2021

Thanks a lot for your work and sorry if it took me some time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants