mirror of
https://github.com/docker/compose.git
synced 2026-11-17 13:45:39 +00:00
fix(events): treat io.EOF as a normal end of a bounded stream
The Engine client sends io.EOF over EventsResult.Err once the stream has been read completely, which is what happens when `events --until` reaches its deadline. Service.Events returned that error verbatim, so the command printed "EOF" and exited 1 even though every event had been delivered. Return nil for io.EOF and keep propagating real stream errors. Signed-off-by: Zhuoming Wang <zhuomingwang0901@outlook.com>
This commit is contained in:
parent
5bceecf985
commit
a74e89a7cc
2 changed files with 77 additions and 0 deletions
|
|
@ -18,6 +18,8 @@ package inspect
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -76,6 +78,12 @@ func (s *Service) Events(ctx context.Context, projectName string, options api.Ev
|
|||
}
|
||||
|
||||
case err := <-res.Err:
|
||||
// A bounded stream (e.g. events --until) ends with io.EOF once the
|
||||
// daemon has sent every matching event. That is a normal completion,
|
||||
// not a command failure.
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
69
internal/inspect/events_test.go
Normal file
69
internal/inspect/events_test.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
Copyright 2020 Docker Compose CLI authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package inspect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/api/types/events"
|
||||
"github.com/moby/moby/client"
|
||||
"go.uber.org/mock/gomock"
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/mocks"
|
||||
)
|
||||
|
||||
func TestEventsTreatsEOFAsCompletion(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
apiClient := mocks.NewMockAPIClient(ctrl)
|
||||
streamErr := make(chan error, 1)
|
||||
streamErr <- io.EOF
|
||||
apiClient.EXPECT().Events(gomock.Any(), gomock.Any()).Return(client.EventsResult{
|
||||
Messages: make(chan events.Message),
|
||||
Err: streamErr,
|
||||
})
|
||||
service := New(&fakeBackend{apiClient: apiClient})
|
||||
|
||||
err := service.Events(context.Background(), "project", api.EventsOptions{
|
||||
Consumer: func(api.Event) error { return nil },
|
||||
})
|
||||
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func TestEventsPropagatesStreamErrors(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
apiClient := mocks.NewMockAPIClient(ctrl)
|
||||
streamErr := make(chan error, 1)
|
||||
boom := errors.New("stream failed")
|
||||
streamErr <- boom
|
||||
apiClient.EXPECT().Events(gomock.Any(), gomock.Any()).Return(client.EventsResult{
|
||||
Messages: make(chan events.Message),
|
||||
Err: streamErr,
|
||||
})
|
||||
service := New(&fakeBackend{apiClient: apiClient})
|
||||
|
||||
err := service.Events(context.Background(), "project", api.EventsOptions{
|
||||
Consumer: func(api.Event) error { return nil },
|
||||
})
|
||||
|
||||
assert.Equal(t, err, boom)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue