当前位置: 首页>>代码示例>>Golang>>正文


Golang iotest.OneByteReader函数代码示例

本文整理汇总了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")))
}
开发者ID:mbookman,项目名称:gcsfuse,代码行数:30,代码来源:read_proxy_test.go

示例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)
	}
}
开发者ID:tg,项目名称:nocontent,代码行数:7,代码来源:reader_test.go

示例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)
			}
		}()
	}
}
开发者ID:xi2,项目名称:xz,代码行数:38,代码来源:reader_test.go

示例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())
	}
}
开发者ID:golang-alex-alex2006hw,项目名称:nedomi,代码行数:27,代码来源:readers_test.go

示例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)
}
开发者ID:Kunde21,项目名称:arvados,代码行数:34,代码来源:collision_test.go

示例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)
	}
}
开发者ID:sivarajankumar,项目名称:google-api-go-client,代码行数:33,代码来源:storage_test.go

示例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")
		}
	}
}
开发者ID:gosuper,项目名称:tchannel-go,代码行数:20,代码来源:headers_test.go

示例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)
	}
}
开发者ID:Rudloff,项目名称:platform,代码行数:11,代码来源:decode_test.go

示例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)
	}
}
开发者ID:kolonse,项目名称:stomp,代码行数:54,代码来源:reader_test.go

示例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)
	}
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:14,代码来源:record_read_test.go

示例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")))
}
开发者ID:okdave,项目名称:gcloud,代码行数:15,代码来源:retry_test.go

示例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())))
}
开发者ID:horzadome,项目名称:gcsfuse,代码行数:15,代码来源:random_reader_test.go

示例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")
	}

}
开发者ID:boljen,项目名称:msgconn,代码行数:16,代码来源:encoding_test.go

示例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)
	}
}
开发者ID:payco,项目名称:llgo,代码行数:17,代码来源:build_test.go

示例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)
	}
}
开发者ID:Harvey-OS,项目名称:go,代码行数:17,代码来源:rand_test.go


注:本文中的testing/iotest.OneByteReader函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。