本文整理汇总了Golang中testing/iotest.OneByteReader函数的典型用法代码示例。如果您正苦于以下问题:Golang OneByteReader函数的具体用法?Golang OneByteReader怎么用?Golang OneByteReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了OneByteReader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ContentsReturnReadError
func (t *ReadProxyTest) ContentsReturnReadError() {
// NewFile
rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
ExpectCall(t.leaser, "NewFile")().
WillOnce(Return(rwl, nil))
// Write
ExpectCall(rwl, "Write")(Any()).
WillRepeatedly(Invoke(successfulWrite))
// Downgrade and Revoke
rl := mock_lease.NewMockReadLease(t.mockController, "rl")
ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))
ExpectCall(rl, "Revoke")()
// Function
t.f = func() (rc io.ReadCloser, err error) {
rc = ioutil.NopCloser(
iotest.TimeoutReader(
iotest.OneByteReader(
strings.NewReader(contents))))
return
}
// Attempt to read.
_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
ExpectThat(err, Error(HasSubstr("Copy")))
ExpectThat(err, Error(HasSubstr("timeout")))
}
示例2: TestOneByteReader
func TestOneByteReader(t *testing.T) {
s := int(1e6)
n, err := ReadZeros(iotest.OneByteReader(bytes.NewReader(make([]byte, s))))
if err != nil || n != s {
t.Errorf("expected %d (<nil>), got %d (%v)", s, n, err)
}
}
示例3: testFileListByteReads
// testFileListByteReads tests the decoding of a list of files against
// their expected error and md5sum. It uses a one byte input buffer
// and one byte output buffer for each run of the decoder.
func testFileListByteReads(t *testing.T, files []testFile) {
for _, f := range files {
func() {
fr, err := openTestFile(f.file)
if err != nil {
t.Fatal(err)
}
defer fr.Close()
hash := md5.New()
obr := iotest.OneByteReader(fr)
r, err := xz.NewReader(obr, 0)
if err == nil {
b := make([]byte, 1)
var n int
for err == nil {
n, err = r.Read(b)
if n == 1 {
_, _ = hash.Write(b)
}
}
if err == io.EOF {
err = nil
}
}
if err != f.err {
t.Fatalf("%s: wanted error: %v, got: %v\n", f.file, f.err, err)
}
md5sum := fmt.Sprintf("%x", hash.Sum(nil))
if f.md5sum != md5sum {
t.Fatalf(
"%s: wanted md5: %v, got: %v\n", f.file, f.md5sum, md5sum)
}
}()
}
}
示例4: TestSkipReaderCloseWithPipe
func TestSkipReaderCloseWithPipe(t *testing.T) {
t.Parallel()
var input = []byte{'a', 'b', 'c', 'd'}
var output = []byte{'b', 'c', 'd'}
r, w := io.Pipe()
src, err := SkipReadCloser(r, 1)
if err != nil {
t.Fatal("unexpected error", err)
}
go func() {
if _, err := w.Write(input); err != nil {
t.Fatalf("Unexpected Write error: %s", err)
}
testutils.ShouldntFail(t, w.Close())
}()
defer func() {
testutils.ShouldntFail(t, src.Close())
}()
var result bytes.Buffer
if _, err := result.ReadFrom(iotest.OneByteReader(src)); err != nil {
t.Fatalf("Unexpected ReadFrom error: %s", err)
}
if result.String() != string(output) {
t.Fatalf("Expected to skipread [%s] read [%s]", output, result.String())
}
}
示例5: TestCollisionOrCorrupt
func (s *CollisionSuite) TestCollisionOrCorrupt(c *check.C) {
fooMD5 := "acbd18db4cc2f85cedef654fccc4a4d8"
c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o'}, bytes.NewBufferString("o")),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, nil, bytes.NewBufferString("oo")),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o', 'o'}, nil),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, nil, []byte{}, bytes.NewBufferString("foo")),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("")),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, nil, nil, iotest.NewReadLogger("foo: ", iotest.DataErrReader(iotest.OneByteReader(bytes.NewBufferString("foo"))))),
check.Equals, CollisionError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("bar")),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, nil),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, bytes.NewBufferString("")),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O'}, nil, bytes.NewBufferString("o")),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O', 'o'}, nil, nil),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, []byte{'O'}, nil),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, bytes.NewBufferString("O")),
check.Equals, DiskHashError)
c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, iotest.TimeoutReader(iotest.OneByteReader(bytes.NewBufferString("foo")))),
check.Equals, iotest.ErrTimeout)
}
示例6: TestMediaErrHandling
func TestMediaErrHandling(t *testing.T) {
handler := &myHandler{}
server := httptest.NewServer(handler)
defer server.Close()
client := &http.Client{}
s, err := storage.New(client)
if err != nil {
t.Fatalf("unable to create service: %v", err)
}
s.BasePath = server.URL
const body = "fake media data"
f := strings.NewReader(body)
// The combination of TimeoutReader and OneByteReader causes the first byte to
// be successfully delivered, but then a timeout error is reported. This
// allows us to test the goroutine within the getMediaType function.
r := iotest.TimeoutReader(iotest.OneByteReader(f))
o := &storage.Object{
Bucket: "mybucket",
Name: "filename",
ContentType: "plain/text",
ContentEncoding: "utf-8",
ContentLanguage: "en",
}
_, err = s.Objects.Insert("mybucket", o).Media(r).Do()
if err == nil || !strings.Contains(err.Error(), "timeout") {
t.Errorf("expected timeout error, got %v", err)
}
if handler.err != nil {
t.Errorf("handler err = %v, want nil", handler.err)
}
}
示例7: TestReadHeadersSuccessful
func TestReadHeadersSuccessful(t *testing.T) {
for _, tt := range headerTests {
// when the bytes are {0, 0}, we always return nil.
if tt.m != nil && len(tt.m) == 0 {
continue
}
reader := iotest.OneByteReader(bytes.NewReader(tt.encoding))
got, err := ReadHeaders(reader)
assert.NoError(t, err, "ReadHeaders failed")
assert.Equal(t, tt.m, got, "Map mismatch")
if tt.encoding2 != nil {
reader := iotest.OneByteReader(bytes.NewReader(tt.encoding2))
got, err := ReadHeaders(reader)
assert.NoError(t, err, "ReadHeaders failed")
assert.Equal(t, tt.m, got, "Map mismatch")
}
}
}
示例8: TestReadDelimitedPrematureBodyIncremental
func TestReadDelimitedPrematureBodyIncremental(t *testing.T) {
t.Parallel()
var data = []byte{128, 5, 0, 0, 0} // 256 + 256 + 128
n, err := ReadDelimited(iotest.OneByteReader(bytes.NewReader(data[:])), nil)
if got, want := n, 5; got != want {
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data, got, want)
}
if got, want := err, io.ErrUnexpectedEOF; got != want {
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data, got, want)
}
}
示例9: TestMultipleReads
func (s *ReaderSuite) TestMultipleReads(c *C) {
text := "SEND\ndestination:xxx\n\nPayload\x00\n" +
"SEND\ndestination:yyy\ncontent-length:12\n" +
"dodgy\\c\\n\\cheader:dodgy\\c\\n\\r\\nvalue\\ \\\n\n" +
"123456789AB\x00\x00"
ioreaders := []io.Reader{
strings.NewReader(text),
iotest.DataErrReader(strings.NewReader(text)),
iotest.HalfReader(strings.NewReader(text)),
iotest.OneByteReader(strings.NewReader(text)),
}
for _, ioreader := range ioreaders {
// uncomment the following line to view the bytes being read
//ioreader = iotest.NewReadLogger("RX", ioreader)
reader := NewReader(ioreader)
frame, err := reader.Read()
c.Assert(err, IsNil)
c.Assert(frame, NotNil)
c.Assert(frame.Command, Equals, "SEND")
c.Assert(frame.Header.Len(), Equals, 1)
v := frame.Header.Get("destination")
c.Assert(v, Equals, "xxx")
c.Assert(string(frame.Body), Equals, "Payload")
// now read a heart-beat from the input
frame, err = reader.Read()
c.Assert(err, IsNil)
c.Assert(frame, IsNil)
// this frame has content-length
frame, err = reader.Read()
c.Assert(err, IsNil)
c.Assert(frame, NotNil)
c.Assert(frame.Command, Equals, "SEND")
c.Assert(frame.Header.Len(), Equals, 3)
v = frame.Header.Get("destination")
c.Assert(v, Equals, "yyy")
n, ok, err := frame.Header.ContentLength()
c.Assert(n, Equals, 12)
c.Assert(ok, Equals, true)
c.Assert(err, IsNil)
k, v := frame.Header.GetAt(2)
c.Assert(k, Equals, "dodgy:\n:header")
c.Assert(v, Equals, "dodgy:\n\r\nvalue\\ \\")
c.Assert(string(frame.Body), Equals, "123456789AB\x00")
// ensure we are at the end of input
frame, err = reader.Read()
c.Assert(frame, IsNil)
c.Assert(err, Equals, io.EOF)
}
}
示例10: TestRecordReader
func TestRecordReader(t *testing.T) {
for i, test := range recordReaderTests {
buf := bytes.NewBuffer(test.in)
c := make(chan *record)
go recordReader(c, buf)
matchRecordReaderOutput(t, i, test, c)
buf = bytes.NewBuffer(test.in)
buf2 := iotest.OneByteReader(buf)
c = make(chan *record)
go recordReader(c, buf2)
matchRecordReaderOutput(t, i*2, test, c)
}
}
示例11: ErrorReading
func (t *RetryBucket_CreateObjectTest) ErrorReading() {
var err error
// Pass in a reader that will return an error.
t.req.Contents = ioutil.NopCloser(
iotest.OneByteReader(
iotest.TimeoutReader(
strings.NewReader("foobar"))))
// Call
err = t.call()
ExpectThat(err, Error(HasSubstr("ReadAll")))
ExpectThat(err, Error(HasSubstr("timeout")))
}
示例12: ReaderFails
func (t *RandomReaderTest) ReaderFails() {
// Bucket
r := iotest.OneByteReader(iotest.TimeoutReader(strings.NewReader("xxx")))
rc := ioutil.NopCloser(r)
ExpectCall(t.bucket, "NewReader")(Any(), Any()).
WillOnce(Return(rc, nil))
// Call
buf := make([]byte, 3)
_, err := t.rr.ReadAt(buf, 0)
ExpectThat(err, Error(HasSubstr("readFull")))
ExpectThat(err, Error(HasSubstr(iotest.ErrTimeout.Error())))
}
示例13: TestReadAll
func TestReadAll(t *testing.T) {
orw := bytes.NewReader([]byte("tester"))
rw := iotest.OneByteReader(orw)
data := []byte("aaaaaa")
if err := ReadAll(rw, data); err != nil {
t.Fatal("unexpected error", err)
}
rw = iotest.DataErrReader(orw)
if err := ReadAll(rw, data); err != io.EOF {
t.Fatal("expected io.EOF")
}
}
示例14: TestLLVMIRReadCloser
func TestLLVMIRReadCloser(t *testing.T) {
const input = `;abc\n;def\n;xyz`
var buf bytes.Buffer
buf.WriteString(input)
r := build.NewLLVMIRReader(ioutil.NopCloser(&buf))
b, err := ioutil.ReadAll(iotest.OneByteReader(r))
if err != nil {
t.Errorf("unexpected error: %s", err)
}
str := string(b)
expected := strings.Replace(str, ";", "//", -1)
if str != expected {
t.Errorf("%q != %q", str, expected)
}
}
示例15: TestReadByOneByte
func TestReadByOneByte(t *testing.T) {
r := New(NewSource(1))
b1 := make([]byte, 100)
_, err := io.ReadFull(iotest.OneByteReader(r), b1)
if err != nil {
t.Errorf("read by one byte: %v", err)
}
r = New(NewSource(1))
b2 := make([]byte, 100)
_, err = r.Read(b2)
if err != nil {
t.Errorf("read: %v", err)
}
if !bytes.Equal(b1, b2) {
t.Errorf("read by one byte vs single read:\n%x\n%x", b1, b2)
}
}