...

Source file src/testing/iotest/reader_test.go

Documentation: testing/iotest

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package iotest
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"io"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  func TestOneByteReader_nonEmptyReader(t *testing.T) {
    16  	msg := "Hello, World!"
    17  	buf := new(bytes.Buffer)
    18  	buf.WriteString(msg)
    19  
    20  	obr := OneByteReader(buf)
    21  	var b []byte
    22  	n, err := obr.Read(b)
    23  	if err != nil || n != 0 {
    24  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
    25  	}
    26  
    27  	b = make([]byte, 3)
    28  	// Read from obr until EOF.
    29  	got := new(strings.Builder)
    30  	for i := 0; ; i++ {
    31  		n, err = obr.Read(b)
    32  		if err != nil {
    33  			break
    34  		}
    35  		if g, w := n, 1; g != w {
    36  			t.Errorf("Iteration #%d read %d bytes, want %d", i, g, w)
    37  		}
    38  		got.Write(b[:n])
    39  	}
    40  	if g, w := err, io.EOF; g != w {
    41  		t.Errorf("Unexpected error after reading all bytes\n\tGot:  %v\n\tWant: %v", g, w)
    42  	}
    43  	if g, w := got.String(), "Hello, World!"; g != w {
    44  		t.Errorf("Read mismatch\n\tGot:  %q\n\tWant: %q", g, w)
    45  	}
    46  }
    47  
    48  func TestOneByteReader_emptyReader(t *testing.T) {
    49  	r := new(bytes.Buffer)
    50  
    51  	obr := OneByteReader(r)
    52  	var b []byte
    53  	if n, err := obr.Read(b); err != nil || n != 0 {
    54  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
    55  	}
    56  
    57  	b = make([]byte, 5)
    58  	n, err := obr.Read(b)
    59  	if g, w := err, io.EOF; g != w {
    60  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
    61  	}
    62  	if g, w := n, 0; g != w {
    63  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
    64  	}
    65  }
    66  
    67  func TestHalfReader_nonEmptyReader(t *testing.T) {
    68  	msg := "Hello, World!"
    69  	buf := new(bytes.Buffer)
    70  	buf.WriteString(msg)
    71  	// empty read buffer
    72  	hr := HalfReader(buf)
    73  	var b []byte
    74  	n, err := hr.Read(b)
    75  	if err != nil || n != 0 {
    76  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
    77  	}
    78  	// non empty read buffer
    79  	b = make([]byte, 2)
    80  	got := new(strings.Builder)
    81  	for i := 0; ; i++ {
    82  		n, err = hr.Read(b)
    83  		if err != nil {
    84  			break
    85  		}
    86  		if g, w := n, 1; g != w {
    87  			t.Errorf("Iteration #%d read %d bytes, want %d", i, g, w)
    88  		}
    89  		got.Write(b[:n])
    90  	}
    91  	if g, w := err, io.EOF; g != w {
    92  		t.Errorf("Unexpected error after reading all bytes\n\tGot:  %v\n\tWant: %v", g, w)
    93  	}
    94  	if g, w := got.String(), "Hello, World!"; g != w {
    95  		t.Errorf("Read mismatch\n\tGot:  %q\n\tWant: %q", g, w)
    96  	}
    97  }
    98  
    99  func TestHalfReader_emptyReader(t *testing.T) {
   100  	r := new(bytes.Buffer)
   101  
   102  	hr := HalfReader(r)
   103  	var b []byte
   104  	if n, err := hr.Read(b); err != nil || n != 0 {
   105  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   106  	}
   107  
   108  	b = make([]byte, 5)
   109  	n, err := hr.Read(b)
   110  	if g, w := err, io.EOF; g != w {
   111  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   112  	}
   113  	if g, w := n, 0; g != w {
   114  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   115  	}
   116  }
   117  
   118  func TestTimeOutReader_nonEmptyReader(t *testing.T) {
   119  	msg := "Hello, World!"
   120  	buf := new(bytes.Buffer)
   121  	buf.WriteString(msg)
   122  	// empty read buffer
   123  	tor := TimeoutReader(buf)
   124  	var b []byte
   125  	n, err := tor.Read(b)
   126  	if err != nil || n != 0 {
   127  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   128  	}
   129  	// Second call should timeout
   130  	n, err = tor.Read(b)
   131  	if g, w := err, ErrTimeout; g != w {
   132  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   133  	}
   134  	if g, w := n, 0; g != w {
   135  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   136  	}
   137  	// non empty read buffer
   138  	tor2 := TimeoutReader(buf)
   139  	b = make([]byte, 3)
   140  	if n, err := tor2.Read(b); err != nil || n == 0 {
   141  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   142  	}
   143  	// Second call should timeout
   144  	n, err = tor2.Read(b)
   145  	if g, w := err, ErrTimeout; g != w {
   146  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   147  	}
   148  	if g, w := n, 0; g != w {
   149  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   150  	}
   151  }
   152  
   153  func TestTimeOutReader_emptyReader(t *testing.T) {
   154  	r := new(bytes.Buffer)
   155  	// empty read buffer
   156  	tor := TimeoutReader(r)
   157  	var b []byte
   158  	if n, err := tor.Read(b); err != nil || n != 0 {
   159  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   160  	}
   161  	// Second call should timeout
   162  	n, err := tor.Read(b)
   163  	if g, w := err, ErrTimeout; g != w {
   164  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   165  	}
   166  	if g, w := n, 0; g != w {
   167  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   168  	}
   169  	// non empty read buffer
   170  	tor2 := TimeoutReader(r)
   171  	b = make([]byte, 5)
   172  	if n, err := tor2.Read(b); err != io.EOF || n != 0 {
   173  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   174  	}
   175  	// Second call should timeout
   176  	n, err = tor2.Read(b)
   177  	if g, w := err, ErrTimeout; g != w {
   178  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   179  	}
   180  	if g, w := n, 0; g != w {
   181  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   182  	}
   183  }
   184  
   185  func TestDataErrReader_nonEmptyReader(t *testing.T) {
   186  	msg := "Hello, World!"
   187  	buf := new(bytes.Buffer)
   188  	buf.WriteString(msg)
   189  
   190  	der := DataErrReader(buf)
   191  
   192  	b := make([]byte, 3)
   193  	got := new(strings.Builder)
   194  	var n int
   195  	var err error
   196  	for {
   197  		n, err = der.Read(b)
   198  		got.Write(b[:n])
   199  		if err != nil {
   200  			break
   201  		}
   202  	}
   203  	if err != io.EOF || n == 0 {
   204  		t.Errorf("Last Read returned n=%d err=%v", n, err)
   205  	}
   206  	if g, w := got.String(), "Hello, World!"; g != w {
   207  		t.Errorf("Read mismatch\n\tGot:  %q\n\tWant: %q", g, w)
   208  	}
   209  }
   210  
   211  func TestDataErrReader_emptyReader(t *testing.T) {
   212  	r := new(bytes.Buffer)
   213  
   214  	der := DataErrReader(r)
   215  	var b []byte
   216  	if n, err := der.Read(b); err != io.EOF || n != 0 {
   217  		t.Errorf("Empty buffer read returned n=%d err=%v", n, err)
   218  	}
   219  
   220  	b = make([]byte, 5)
   221  	n, err := der.Read(b)
   222  	if g, w := err, io.EOF; g != w {
   223  		t.Errorf("Error mismatch\n\tGot:  %v\n\tWant: %v", g, w)
   224  	}
   225  	if g, w := n, 0; g != w {
   226  		t.Errorf("Unexpectedly read %d bytes, wanted %d", g, w)
   227  	}
   228  }
   229  
   230  func TestErrReader(t *testing.T) {
   231  	cases := []struct {
   232  		name string
   233  		err  error
   234  	}{
   235  		{"nil error", nil},
   236  		{"non-nil error", errors.New("io failure")},
   237  		{"io.EOF", io.EOF},
   238  	}
   239  
   240  	for _, tt := range cases {
   241  		tt := tt
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			n, err := ErrReader(tt.err).Read(nil)
   244  			if err != tt.err {
   245  				t.Fatalf("Error mismatch\nGot:  %v\nWant: %v", err, tt.err)
   246  			}
   247  			if n != 0 {
   248  				t.Fatalf("Byte count mismatch: got %d want 0", n)
   249  			}
   250  		})
   251  	}
   252  }
   253  
   254  func TestStringsReader(t *testing.T) {
   255  	const msg = "Now is the time for all good gophers."
   256  
   257  	r := strings.NewReader(msg)
   258  	if err := TestReader(r, []byte(msg)); err != nil {
   259  		t.Fatal(err)
   260  	}
   261  }
   262  

View as plain text