Example #1
0
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)
}
Example #2
0
func TestLoadTimeoutReader(t *testing.T) {
	p := make(Properties)
	err := p.Load(iotest.TimeoutReader(bytes.NewReader([]byte(source))))
	if err == nil {
		t.Fail()
	}
}
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)
	}
}
Example #4
0
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")))
}
Example #5
0
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)
}
Example #6
0
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()
	}
}
Example #7
0
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)
	}
}
Example #8
0
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)
	}
}
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)
}
Example #10
0
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)
	}
}
Example #11
0
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")))
}
Example #12
0
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())))
}
Example #13
0
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)
}
Example #14
0
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)
}
Example #15
0
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")
}
Example #16
0
func TestCheckResponse(t *testing.T) {
	res := &http.Response{}
	res.StatusCode = 200
	err := checkResponse(res)
	if err != nil {
		t.Errorf("checkResponse({StatusCode: 200}) expected no error; got %#v.", err)
	}

	res = &http.Response{}
	res.StatusCode = 300
	err = checkResponse(res)
	if err, ok := err.(*UnexpectedError); !ok {
		t.Errorf("checkResponse({StatusCode: 300}) expected an UnexpectedError; got %#v.", err)
	}

	res = &http.Response{}
	res.StatusCode = 500
	err = checkResponse(res)
	if err, ok := err.(*UnexpectedError); !ok {
		t.Errorf("checkResponse({StatusCode: 500}) expected an UnexpectedError; got %#v.", err)
	}

	res = &http.Response{}
	res.StatusCode = 500
	res.Body = ioutil.NopCloser(bytes.NewBufferString("db-error"))
	if got, want := checkResponse(res), &(UnexpectedError{}); !reflect.DeepEqual(got, want) {
		t.Errorf("checkResponse({StatusCode: 500, No Content-Type, Body:\"db-error\"}) returned %v, want %v", got, want)
	}

	res = &http.Response{}
	res.StatusCode = 500
	res.Header = http.Header{}
	res.Header.Add("Content-Type", "text/plain")
	res.Body = ioutil.NopCloser(bytes.NewBufferString("db-error"))
	if got, want := checkResponse(res), &(Error{"db-error"}); !reflect.DeepEqual(got, want) {
		t.Errorf("checkResponse({StatusCode: 500, \"text/plain\", Body:\"db-error\"}) returned %#v, want %#v", got, want)
	}

	res = &http.Response{}
	res.StatusCode = 500
	res.Header = http.Header{}
	res.Header.Add("Content-Type", "text/plain")
	res.Body = ioutil.NopCloser(iotest.TimeoutReader(bytes.NewBufferString("db-error")))
	if err := checkResponse(res); err == nil {
		t.Error("checkResponse({Timout error}) expected error to be returned.")
	}

	res = &http.Response{}
	res.StatusCode = 500
	res.Header = http.Header{}
	res.Header.Set("Content-Type", "application/json")
	res.Body = ioutil.NopCloser(bytes.NewBufferString("db-error"))
	if err, ok := checkResponse(res).(*json.SyntaxError); !ok {
		t.Errorf("checkResponse({StatusCode: 500, \"application/json\", Body:<invalid json>}) expected a *json.SyntaxError error, got %#v", err)
	}

	res = &http.Response{}
	res.StatusCode = 500
	res.Header = http.Header{}
	res.Header.Set("Content-Type", "application/json")
	res.Body = ioutil.NopCloser(bytes.NewBufferString("{\"reason\":\"db-error\"}"))
	if got, want := checkResponse(res), &(Error{"db-error"}); !reflect.DeepEqual(got, want) {
		t.Errorf("checkResponse({StatusCode: 500, \"application/json\", Body:\"{\"reason\":\"db-error\"}\"}) returned %v, want %v", got, want)
	}
}
Example #17
0
func TestReaderFeed_Error(t *testing.T) {
	var f Fields
	if Reader(iotest.TimeoutReader(bytes.NewBufferString(stream))).Feed(&f) == nil {
		t.Errorf("err = nil, want not nil")
	}
}
Example #18
0
func TestDecoder_Decode_Timeout(t *testing.T) {
	m := &decodeCase{}
	if err := NewDecoder(iotest.TimeoutReader(bytes.NewReader(raw0))).Decode(m); err == nil {
		t.Errorf("expected error, got success.")
	}
}