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

Add golang 1.10 and fix a race condition. #257

Merged
merged 1 commit into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dist: trusty

go:
- 1.9.x
- 1.10.x

go_import_path: github.com/kubernetes-incubator/cri-tools

Expand Down
38 changes: 30 additions & 8 deletions pkg/validate/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"net/url"
"os"
"sync"
"time"

"github.com/kubernetes-incubator/cri-tools/pkg/framework"
Expand Down Expand Up @@ -190,20 +191,42 @@ func createDefaultAttach(c internalapi.RuntimeService, containerID string) strin
return resp.Url
}

// safeBuffer is a goroutine safe bytes.Buffer
type safeBuffer struct {
mu sync.Mutex
buffer bytes.Buffer
}

// Write appends the contents of p to the buffer, growing the buffer as needed. It returns
// the number of bytes written.
func (s *safeBuffer) Write(p []byte) (n int, err error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.buffer.Write(p)
}

// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
func (s *safeBuffer) String() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.buffer.String()
}

func checkAttach(c internalapi.RuntimeService, attachServerURL string) {
localOut := &bytes.Buffer{}
localOut := &safeBuffer{buffer: bytes.Buffer{}}
localErr := &bytes.Buffer{}
reader, writer := io.Pipe()
var out string

go func() {
defer GinkgoRecover()
defer writer.Close()
writer.Write([]byte("echo hello\n"))
Eventually(func() string {
out = localOut.String()
return out
}, time.Minute, time.Second).ShouldNot(BeEmpty())
writer.Close()
return localOut.String()
}, time.Minute, time.Second).Should(Equal("hello\n"), "The stdout of attach should be hello")
Consistently(func() string {
return localOut.String()
}, 3*time.Second, time.Second).Should(Equal("hello\n"), "The stdout of attach should not contain other things")
}()

// Only http is supported now.
Expand All @@ -220,7 +243,6 @@ func checkAttach(c internalapi.RuntimeService, attachServerURL string) {
})
framework.ExpectNoError(err, "failed to open streamer for %q", attachServerURL)

Expect(out).To(Equal("hello\n"), "The stdout of exec should be hello")
Expect(localErr.String()).To(BeEmpty(), "The stderr of attach should be empty")
framework.Logf("Check attach url %q succeed", attachServerURL)
}
Expand Down