github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/progress/progressreader_test.go (about) 1 package progress // import "github.com/Prakhar-Agarwal-byte/moby/pkg/progress" 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 ) 8 9 func TestOutputOnPrematureClose(t *testing.T) { 10 content := []byte("TESTING") 11 reader := io.NopCloser(bytes.NewReader(content)) 12 progressChan := make(chan Progress, 10) 13 14 pr := NewProgressReader(reader, ChanOutput(progressChan), int64(len(content)), "Test", "Read") 15 16 part := make([]byte, 4) 17 _, err := io.ReadFull(pr, part) 18 if err != nil { 19 pr.Close() 20 t.Fatal(err) 21 } 22 23 drainLoop: 24 for { 25 select { 26 case <-progressChan: 27 default: 28 break drainLoop 29 } 30 } 31 32 pr.Close() 33 34 select { 35 case <-progressChan: 36 default: 37 t.Fatalf("Expected some output when closing prematurely") 38 } 39 } 40 41 func TestCompleteSilently(t *testing.T) { 42 content := []byte("TESTING") 43 reader := io.NopCloser(bytes.NewReader(content)) 44 progressChan := make(chan Progress, 10) 45 46 pr := NewProgressReader(reader, ChanOutput(progressChan), int64(len(content)), "Test", "Read") 47 48 out, err := io.ReadAll(pr) 49 if err != nil { 50 pr.Close() 51 t.Fatal(err) 52 } 53 if string(out) != "TESTING" { 54 pr.Close() 55 t.Fatalf("Unexpected output %q from reader", string(out)) 56 } 57 58 drainLoop: 59 for { 60 select { 61 case <-progressChan: 62 default: 63 break drainLoop 64 } 65 } 66 67 pr.Close() 68 69 select { 70 case <-progressChan: 71 t.Fatalf("Should have closed silently when read is complete") 72 default: 73 } 74 }