2018-02-05 16:05:59 -05:00
|
|
|
package progress // import "github.com/docker/docker/pkg/progress"
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOutputOnPrematureClose(t *testing.T) {
|
|
|
|
content := []byte("TESTING")
|
2021-08-24 06:10:50 -04:00
|
|
|
reader := io.NopCloser(bytes.NewReader(content))
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan Progress, 10)
|
|
|
|
|
|
|
|
pr := NewProgressReader(reader, ChanOutput(progressChan), int64(len(content)), "Test", "Read")
|
|
|
|
|
2017-03-30 05:26:16 -04:00
|
|
|
part := make([]byte, 4)
|
2015-11-13 19:59:01 -05:00
|
|
|
_, err := io.ReadFull(pr, part)
|
|
|
|
if err != nil {
|
|
|
|
pr.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
drainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-progressChan:
|
|
|
|
default:
|
|
|
|
break drainLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.Close()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-progressChan:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Expected some output when closing prematurely")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompleteSilently(t *testing.T) {
|
|
|
|
content := []byte("TESTING")
|
2021-08-24 06:10:50 -04:00
|
|
|
reader := io.NopCloser(bytes.NewReader(content))
|
2015-11-13 19:59:01 -05:00
|
|
|
progressChan := make(chan Progress, 10)
|
|
|
|
|
|
|
|
pr := NewProgressReader(reader, ChanOutput(progressChan), int64(len(content)), "Test", "Read")
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
out, err := io.ReadAll(pr)
|
2015-11-13 19:59:01 -05:00
|
|
|
if err != nil {
|
|
|
|
pr.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(out) != "TESTING" {
|
|
|
|
pr.Close()
|
|
|
|
t.Fatalf("Unexpected output %q from reader", string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
drainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-progressChan:
|
|
|
|
default:
|
|
|
|
break drainLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.Close()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-progressChan:
|
|
|
|
t.Fatalf("Should have closed silently when read is complete")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|