Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.
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
2 changes: 1 addition & 1 deletion hack/test-cri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ source $(dirname "${BASH_SOURCE[0]}")/test-utils.sh
# FOCUS focuses the test to run.
FOCUS=${FOCUS:-}
# SKIP skips the test to skip.
SKIP=${SKIP:-"attach|RunAsUser"}
SKIP=${SKIP:-"RunAsUser"}
REPORT_DIR=${REPORT_DIR:-"/tmp/test-cri"}

if [[ -z "${GOPATH}" ]]; then
Expand Down
68 changes: 68 additions & 0 deletions pkg/ioutil/write_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2017 The Kubernetes 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 ioutil

import "io"

// writeCloseInformer wraps passed in write closer with a close channel.
// Caller could wait on the close channel for the write closer to be
// closed.
type writeCloseInformer struct {
close chan struct{}
wc io.WriteCloser
}

// NewWriteCloseInformer creates the writeCloseInformer from a write closer.
func NewWriteCloseInformer(wc io.WriteCloser) (io.WriteCloser, <-chan struct{}) {
close := make(chan struct{})
return &writeCloseInformer{
close: close,
wc: wc,
}, close
}

// Write passes through the data into the internal write closer.
func (w *writeCloseInformer) Write(p []byte) (int, error) {
return w.wc.Write(p)
}

// Close closes the internal write closer and inform the close channel.
func (w *writeCloseInformer) Close() error {
err := w.wc.Close()
close(w.close)
return err
}

// nopWriteCloser wraps passed in writer with a nop close function.
type nopWriteCloser struct {
w io.Writer
}

// NewNopWriteCloser creates the nopWriteCloser from a writer.
func NewNopWriteCloser(w io.Writer) io.WriteCloser {
return &nopWriteCloser{w: w}
}

// Write passes through the data into the internal writer.
func (n *nopWriteCloser) Write(p []byte) (int, error) {
return n.w.Write(p)
}

// Close is a nop close function.
func (n *nopWriteCloser) Close() error {
return nil
}
49 changes: 49 additions & 0 deletions pkg/ioutil/write_closer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2017 The Kubernetes 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 ioutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWriteCloseInformer(t *testing.T) {
original := &writeCloser{}
wci, close := NewWriteCloseInformer(original)
data := "test"

n, err := wci.Write([]byte(data))
assert.Equal(t, len(data), n)
assert.Equal(t, data, original.buf.String())
assert.NoError(t, err)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use another thread here to test the wait function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why another thread is required? One thread seems more deterministic here.

select {
case <-close:
assert.Fail(t, "write closer closed")
default:
}

wci.Close()
assert.True(t, original.closed)

select {
case <-close:
default:
assert.Fail(t, "write closer not closed")
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-op test for the no-op write closer :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We mainly want to test WriteCloseInformer will close the inform channel when it is closed. And the test proves that it does. :)

103 changes: 103 additions & 0 deletions pkg/ioutil/writer_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2017 The Kubernetes 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 ioutil

import (
"errors"
"io"
"sync"

"github.com/golang/glog"
)

// WriterGroup is a group of writers. Writer could be dynamically
// added and removed.
type WriterGroup struct {
mu sync.Mutex
writers map[string]io.WriteCloser
closed bool
}

var _ io.Writer = &WriterGroup{}

// NewWriterGroup creates an empty writer group.
func NewWriterGroup() *WriterGroup {
return &WriterGroup{
writers: make(map[string]io.WriteCloser),
}
}

// Add adds a writer into the group, returns an error when writer
// group is closed.
func (g *WriterGroup) Add(key string, w io.WriteCloser) error {
g.mu.Lock()
defer g.mu.Unlock()
if g.closed {
return errors.New("wait group closed")
}
g.writers[key] = w
return nil
}

// Remove removes a writer from the group.
func (g *WriterGroup) Remove(key string) {
g.mu.Lock()
defer g.mu.Unlock()
w, ok := g.writers[key]
if !ok {
return
}
w.Close()
delete(g.writers, key)
}

// Write writes data into each writer. If a writer returns error,
// it will be closed and removed from the writer group. It returns
// error if writer group is empty.
func (g *WriterGroup) Write(p []byte) (int, error) {
g.mu.Lock()
defer g.mu.Unlock()
for k, w := range g.writers {
n, err := w.Write(p)
if err != nil {
glog.Errorf("Writer %q write error: %v", k, err)
} else if len(p) != n {
glog.Errorf("Writer %q short write error", k)
} else {
continue
}
// The writer is closed or in bad state, remove it.
w.Close()
delete(g.writers, k)
}
if len(g.writers) == 0 {
return 0, errors.New("writer group is empty")
}
return len(p), nil
}

// Close closes the writer group. Write or Add will return error
// after closed.
func (g *WriterGroup) Close() {
g.mu.Lock()
defer g.mu.Unlock()
for _, w := range g.writers {
w.Close()
}
g.writers = nil
g.closed = true
}
94 changes: 94 additions & 0 deletions pkg/ioutil/writer_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2017 The Kubernetes 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 ioutil

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

type writeCloser struct {
buf bytes.Buffer
closed bool
}

func (wc *writeCloser) Write(p []byte) (int, error) {
return wc.buf.Write(p)
}

func (wc *writeCloser) Close() error {
wc.closed = true
return nil
}

func TestEmptyWriterGroup(t *testing.T) {
wg := NewWriterGroup()
_, err := wg.Write([]byte("test"))
assert.Error(t, err)
}

func TestClosedWriterGroup(t *testing.T) {
wg := NewWriterGroup()
wc := &writeCloser{}
key, data := "test key", "test data"

err := wg.Add(key, wc)
assert.NoError(t, err)

n, err := wg.Write([]byte(data))
assert.Equal(t, len(data), n)
assert.Equal(t, data, wc.buf.String())
assert.NoError(t, err)

wg.Close()
assert.True(t, wc.closed)

err = wg.Add(key, &writeCloser{})
assert.Error(t, err)

_, err = wg.Write([]byte(data))
assert.Error(t, err)
}

func TestAddRemoveWriter(t *testing.T) {
wg := NewWriterGroup()
wc1, wc2 := &writeCloser{}, &writeCloser{}
key1, key2 := "test key 1", "test key 2"

err := wg.Add(key1, wc1)
assert.NoError(t, err)
_, err = wg.Write([]byte("test data 1"))
assert.NoError(t, err)
assert.Equal(t, "test data 1", wc1.buf.String())

err = wg.Add(key2, wc2)
assert.NoError(t, err)
_, err = wg.Write([]byte("test data 2"))
assert.NoError(t, err)
assert.Equal(t, "test data 1test data 2", wc1.buf.String())
assert.Equal(t, "test data 2", wc2.buf.String())

wg.Remove(key1)
_, err = wg.Write([]byte("test data 3"))
assert.NoError(t, err)
assert.Equal(t, "test data 1test data 2", wc1.buf.String())
assert.Equal(t, "test data 2test data 3", wc2.buf.String())

wg.Close()
}
51 changes: 0 additions & 51 deletions pkg/server/agents/agents.go

This file was deleted.

Loading