github.com/akerouanton/docker@v1.11.0-rc3/pkg/progress/progressreader_test.go (about)

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