github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/pkg/ioutils/readers_test.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"golang.org/x/net/context"
    11  )
    12  
    13  // Implement io.Reader
    14  type errorReader struct{}
    15  
    16  func (r *errorReader) Read(p []byte) (int, error) {
    17  	return 0, fmt.Errorf("Error reader always fail.")
    18  }
    19  
    20  func TestReadCloserWrapperClose(t *testing.T) {
    21  	reader := strings.NewReader("A string reader")
    22  	wrapper := NewReadCloserWrapper(reader, func() error {
    23  		return fmt.Errorf("This will be called when closing")
    24  	})
    25  	err := wrapper.Close()
    26  	if err == nil || !strings.Contains(err.Error(), "This will be called when closing") {
    27  		t.Fatalf("readCloserWrapper should have call the anonymous func and thus, fail.")
    28  	}
    29  }
    30  
    31  func TestReaderErrWrapperReadOnError(t *testing.T) {
    32  	called := false
    33  	reader := &errorReader{}
    34  	wrapper := NewReaderErrWrapper(reader, func() {
    35  		called = true
    36  	})
    37  	_, err := wrapper.Read([]byte{})
    38  	if err == nil || !strings.Contains(err.Error(), "Error reader always fail.") {
    39  		t.Fatalf("readErrWrapper should returned an error")
    40  	}
    41  	if !called {
    42  		t.Fatalf("readErrWrapper should have call the anonymous function on failure")
    43  	}
    44  }
    45  
    46  func TestReaderErrWrapperRead(t *testing.T) {
    47  	reader := strings.NewReader("a string reader.")
    48  	wrapper := NewReaderErrWrapper(reader, func() {
    49  		t.Fatalf("readErrWrapper should not have called the anonymous function")
    50  	})
    51  	// Read 20 byte (should be ok with the string above)
    52  	num, err := wrapper.Read(make([]byte, 20))
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	if num != 16 {
    57  		t.Fatalf("readerErrWrapper should have read 16 byte, but read %d", num)
    58  	}
    59  }
    60  
    61  func TestHashData(t *testing.T) {
    62  	reader := strings.NewReader("hash-me")
    63  	actual, err := HashData(reader)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	expected := "sha256:4d11186aed035cc624d553e10db358492c84a7cd6b9670d92123c144930450aa"
    68  	if actual != expected {
    69  		t.Fatalf("Expecting %s, got %s", expected, actual)
    70  	}
    71  }
    72  
    73  type perpetualReader struct{}
    74  
    75  func (p *perpetualReader) Read(buf []byte) (n int, err error) {
    76  	for i := 0; i != len(buf); i++ {
    77  		buf[i] = 'a'
    78  	}
    79  	return len(buf), nil
    80  }
    81  
    82  func TestCancelReadCloser(t *testing.T) {
    83  	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
    84  	cancelReadCloser := NewCancelReadCloser(ctx, ioutil.NopCloser(&perpetualReader{}))
    85  	for {
    86  		var buf [128]byte
    87  		_, err := cancelReadCloser.Read(buf[:])
    88  		if err == context.DeadlineExceeded {
    89  			break
    90  		} else if err != nil {
    91  			t.Fatalf("got unexpected error: %v", err)
    92  		}
    93  	}
    94  }