This repository was archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 347
Add container attach #128
Merged
Random-Liu
merged 2 commits into
containerd:master
from
Random-Liu:add-container-attach
Aug 24, 2017
Merged
Add container attach #128
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
| 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") | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no-op test for the no-op write closer :-)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We mainly want to test |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.