當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。