本文整理汇总了Golang中testing/iotest.TimeoutReader函数的典型用法代码示例。如果您正苦于以下问题:Golang TimeoutReader函数的具体用法?Golang TimeoutReader怎么用?Golang TimeoutReader使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TimeoutReader函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: TestLoadTimeoutReader
func TestLoadTimeoutReader(t *testing.T) {
p := make(Properties)
err := p.Load(iotest.TimeoutReader(bytes.NewReader([]byte(source))))
if err == nil {
t.Fail()
}
}
示例3: 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)
}
}
示例4: 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")))
}
示例5: TestReadFailsOverAndThenDies
func TestReadFailsOverAndThenDies(t *testing.T) {
br, _ := getBlockReader(t, "/_test/mobydick.txt")
datanodes := br.datanodes.numRemaining()
for br.datanodes.numRemaining() > 0 {
br.stream.reader = iotest.TimeoutReader(br.stream.reader)
_, err := io.CopyN(ioutil.Discard, br, 1000)
require.NoError(t, err)
require.Equal(t, datanodes-1, br.datanodes.numRemaining())
datanodes--
}
br.stream.reader = iotest.TimeoutReader(br.stream.reader)
_, err := io.Copy(ioutil.Discard, br)
assert.EqualValues(t, iotest.ErrTimeout, err)
}
示例6: TestMultiReaderSourceError
func TestMultiReaderSourceError(t *testing.T) {
t.Parallel()
// This test fails if it doesn't complete quickly.
timer := time.AfterFunc(2*time.Second, func() {
t.Fatalf("Test seems to have hung.")
})
defer timer.Stop()
randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)}
tordr := iotest.TimeoutReader(&randomSrc)
lr := io.LimitReader(tordr, expSize)
r1, _ := newMultiReaderTimeout(lr, 10*time.Millisecond)
b1 := &bytes.Buffer{}
rs := make(chan copyRes, 2)
go bgCopy(b1, r1, rs)
res1 := <-rs
if res1.e != Timeout {
t.Errorf("Expected a timeout, got %v", res1.e)
t.Fail()
}
}
示例7: TestStdCopyTimeoutReader
func TestStdCopyTimeoutReader(t *testing.T) {
var input, stdout, stderr bytes.Buffer
input.Write([]byte{2, 0, 0, 0, 0, 0, 0, 19})
input.Write([]byte("something happened!"))
_, err := stdCopy(&stdout, &stderr, iotest.TimeoutReader(&input))
if err != iotest.ErrTimeout {
t.Errorf("stdCopy: wrong error. Want ErrTimeout. Got %#v.", err)
}
}
示例8: TestReadError
func TestReadError(t *testing.T) {
s := int(1e6)
n, err := ReadZeros(iotest.TimeoutReader(bytes.NewReader(make([]byte, s))))
// first read should be 512 bytes
expected := 512
if err != iotest.ErrTimeout || n != expected {
t.Errorf("expected %d (timeout), got %d (%v)", expected, n, err)
}
}
示例9: TestReadLinesFromFile
func TestReadLinesFromFile(t *testing.T) {
fh := bytes.NewBufferString("1\n2 3\n")
expected := []string{"1", "2 3"}
actual := readLinesFromFile(fh)
assert.Equal(t, "readLinesFromFile bad result", expected, actual)
fh2 := iotest.TimeoutReader(bytes.NewBufferString("1\n2 3\n"))
defer assert.Panics(t, "readLinesFromFile() should panic on failure", "Reading lines failed")
_ = readLinesFromFile(fh2)
}
示例10: TestWriteKeyReaderError
func (s *SessionSuite) TestWriteKeyReaderError(c *C) {
var testErrorReader = iotest.TimeoutReader(strings.NewReader("ABCD"))
key, _ := NewKey("test-key")
defer key.Free()
for res := range s.session.WriteKey(key, testErrorReader, 0, 0) {
c.Assert(res.Error(), Equals, iotest.ErrTimeout)
}
}
示例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: TestReadFailsOver
func TestReadFailsOver(t *testing.T) {
br, dn := getBlockReader(t, "/_test/mobydick.txt")
datanodes := br.datanodes.numRemaining()
br.stream.reader = iotest.TimeoutReader(br.stream.reader)
hash := crc32.NewIEEE()
n, err := io.Copy(hash, br)
require.NoError(t, err)
assert.EqualValues(t, 1048576, n)
assert.EqualValues(t, 0xb35a6a0e, hash.Sum32())
assert.EqualValues(t, datanodes-1, br.datanodes.numRemaining())
_, exist := datanodeFailures[dn]
assert.True(t, exist)
}
示例14: TestFailsOverMidRead
func TestFailsOverMidRead(t *testing.T) {
br, dn := setupFailover(t)
datanodes := br.datanodes.numRemaining()
hash := crc32.NewIEEE()
_, err := io.CopyN(hash, br, 10000)
require.NoError(t, err)
br.stream.reader = iotest.TimeoutReader(br.stream.reader)
n, err := io.Copy(hash, br)
require.NoError(t, err)
assert.EqualValues(t, 1048576-10000, n)
assert.EqualValues(t, 0xb35a6a0e, hash.Sum32())
assert.EqualValues(t, datanodes-1, br.datanodes.numRemaining())
_, exist := datanodeFailures[dn]
assert.True(t, exist)
}
示例15: TestStripCRError
func (s *SSHStreamSuite) TestStripCRError(c *gc.C) {
reader := ssh.StripCRReader(strings.NewReader("One\r\r\rTwo"))
_, err := ioutil.ReadAll(iotest.TimeoutReader(reader))
c.Assert(err.Error(), gc.Equals, "timeout")
}