Skip to content

Commit

Permalink
Merge pull request #62 from appleboy/patch
Browse files Browse the repository at this point in the history
support timeout for NewLogStrategy
  • Loading branch information
gianarb authored May 7, 2019
2 parents 255fa69 + ce8f82b commit 0416f14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
21 changes: 21 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,27 @@ func TestContainerCreationTimesOutWithHttp(t *testing.T) {
}
}

func TestContainerCreationWaitsForLogContextTimeout(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Image: "mysql:latest",
ExposedPorts: []string{"3306/tcp", "33060/tcp"},
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
"MYSQL_DATABASE": "database",
},
WaitingFor: wait.ForLog("test context timeout").WithStartupTimeout(1 * time.Second),
}
_, err := GenericContainer(ctx, GenericContainerRequest{
ContainerRequest: req,
Started: true,
})

if err == nil {
t.Error("Expected timeout")
}
}

func TestContainerCreationWaitsForLog(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Expand Down
31 changes: 19 additions & 12 deletions wait/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,26 @@ func (ws *LogStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget
ctx, cancelContext := context.WithTimeout(ctx, ws.startupTimeout)
defer cancelContext()

LOOP:
for {
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) {
break
} else {
time.Sleep(ws.PollInterval)
continue
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) {
break LOOP
} else {
time.Sleep(ws.PollInterval)
continue
}
}
}

Expand Down

0 comments on commit 0416f14

Please sign in to comment.