Skip to content

Commit

Permalink
Merge pull request #257 from Random-Liu/fix-race-condition
Browse files Browse the repository at this point in the history
Add golang 1.10 and fix a race condition.
  • Loading branch information
feiskyer authored Feb 24, 2018
2 parents d4046a4 + 38f225e commit 506c19b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
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

0 comments on commit 506c19b

Please sign in to comment.