Exemplo n.º 1
0
func (t *ReadProxyTest) Destroy() {
	// Arrange a successful wrapped read lease.
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(successfulWrite))

	ExpectCall(rwl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, errors.New("taco")))

	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))

	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(strings.NewReader(contents))
		return
	}

	t.proxy.ReadAt(context.Background(), []byte{}, 0)

	// When we destroy our lease, the wrapped should be revoked.
	ExpectCall(rl, "Revoke")()
	t.proxy.Destroy()
}
Exemplo n.º 2
0
func (t *ReadProxyTest) ReadAt_Successful() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// Write
	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(successfulWrite))

	// ReadAt
	ExpectCall(rwl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, nil))

	// Downgrade
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))

	// Function
	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(strings.NewReader(contents))
		return
	}

	// Call.
	_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectEq(nil, err)
}
Exemplo n.º 3
0
func (t *ReadProxyTest) InitialReadLease_Valid() {
	var err error

	// Set up an initial lease.
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	t.proxy = lease.NewReadProxy(
		t.leaser,
		t.makeRefresher(),
		rl)

	// ReadAt
	ExpectCall(rl, "ReadAt")(Any(), 11).
		WillOnce(Return(0, errors.New("taco"))).
		WillOnce(Return(17, nil))

	_, err = t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectThat(err, Error(HasSubstr("taco")))

	n, err := t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectEq(17, n)

	// Upgrade
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")

	ExpectCall(rl, "Revoke")()
	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, errors.New("taco"))).
		WillOnce(Return(rwl, nil))

	_, err = t.proxy.Upgrade(context.Background())
	ExpectThat(err, Error(HasSubstr("taco")))

	tmp, _ := t.proxy.Upgrade(context.Background())
	ExpectEq(rwl, tmp)
}
Exemplo n.º 4
0
func (t *ReadProxyTest) Upgrade_Error() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// Write
	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Return(0, errors.New("taco")))

	// 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(strings.NewReader(contents))
		return
	}

	// Call.
	_, err := t.proxy.Upgrade(context.Background())
	ExpectThat(err, Error(HasSubstr("taco")))
}
Exemplo n.º 5
0
func (t *ReadProxyTest) WritesCorrectData() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// Write
	var written []byte
	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(func(p []byte) (n int, err error) {
			written = append(written, p...)
			n = len(p)
			return
		}))

	// Read
	ExpectCall(rwl, "ReadAt")(Any(), Any()).
		WillRepeatedly(Return(0, errors.New("")))

	// Downgrade
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))

	// Function
	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(strings.NewReader(contents))
		return
	}

	// Call.
	t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectEq(contents, string(written))
}
Exemplo n.º 6
0
func (t *ReadProxyTest) ContentsAreWrongLength() {
	AssertEq(4, len(contents))

	// 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(strings.NewReader(contents[:3]))
		return
	}

	// Attempt to read.
	_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectThat(err, Error(HasSubstr("Copied 3")))
	ExpectThat(err, Error(HasSubstr("expected 4")))
}
Exemplo n.º 7
0
func (t *ReadProxyTest) ContentsReturnCloseError() {
	// 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 = &closeErrorReader{
			Wrapped: strings.NewReader(contents),
			Err:     errors.New("taco"),
		}

		return
	}

	// Attempt to read.
	_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectThat(err, Error(HasSubstr("Close")))
	ExpectThat(err, Error(HasSubstr("taco")))
}
Exemplo n.º 8
0
func (t *ReadProxyTest) CallsFunc() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// Downgrade and Revoke
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))
	ExpectCall(rl, "Revoke")()

	// Function
	var called bool
	t.f = func() (rc io.ReadCloser, err error) {
		AssertFalse(called)
		called = true

		err = errors.New("")
		return
	}

	// Attempt to read.
	t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectTrue(called)
}
Exemplo n.º 9
0
func (t *ReadProxyTest) WrappedStillValid() {
	var err error

	// Arrange a successful wrapped read lease.
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(successfulWrite))

	ExpectCall(rwl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, errors.New("taco")))

	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))

	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(strings.NewReader(contents))
		return
	}

	t.proxy.ReadAt(context.Background(), []byte{}, 0)

	// ReadAt
	ExpectCall(rl, "ReadAt")(Any(), 11).
		WillOnce(Return(0, errors.New("taco"))).
		WillOnce(Return(17, nil))

	_, err = t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectThat(err, Error(HasSubstr("taco")))

	n, err := t.proxy.ReadAt(context.Background(), []byte{}, 11)
	ExpectEq(17, n)

	// Upgrade
	ExpectCall(rl, "Revoke")()
	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, errors.New("taco"))).
		WillOnce(Return(rwl, nil))

	_, err = t.proxy.Upgrade(context.Background())
	ExpectThat(err, Error(HasSubstr("taco")))

	tmp, _ := t.proxy.Upgrade(context.Background())
	ExpectEq(rwl, tmp)
}
Exemplo n.º 10
0
func (t *ReadProxyTest) FuncReturnsError() {
	// NewFile
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	// 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) {
		err = errors.New("taco")
		return
	}

	// Attempt to read.
	_, err := t.proxy.ReadAt(context.Background(), []byte{}, 0)
	ExpectThat(err, Error(HasSubstr("taco")))
}
Exemplo n.º 11
0
func (t *ReadProxyTest) InitialReadLease_Revoked() {
	// Set up an initial lease.
	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	t.proxy = lease.NewReadProxy(
		t.leaser,
		t.makeRefresher(),
		rl)

	// Simulate it being revoked for all methods.
	ExpectCall(rl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, &lease.RevokedError{}))

	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, &lease.RevokedError{}))

	ExpectCall(t.leaser, "NewFile")().
		Times(2).
		WillRepeatedly(Return(nil, errors.New("")))

	t.proxy.ReadAt(context.Background(), []byte{}, 0)
	t.proxy.Upgrade(context.Background())
}
Exemplo n.º 12
0
func (t *ReadProxyTest) WrappedRevoked() {
	// Arrange a successful wrapped read lease.
	rwl := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(rwl, nil))

	ExpectCall(rwl, "Write")(Any()).
		WillRepeatedly(Invoke(successfulWrite))

	ExpectCall(rwl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, errors.New("taco")))

	rl := mock_lease.NewMockReadLease(t.mockController, "rl")
	ExpectCall(rwl, "Downgrade")().WillOnce(Return(rl))

	t.f = func() (rc io.ReadCloser, err error) {
		rc = ioutil.NopCloser(strings.NewReader(contents))
		return
	}

	t.proxy.ReadAt(context.Background(), []byte{}, 0)

	// Simulate it being revoked for all methods.
	ExpectCall(rl, "ReadAt")(Any(), Any()).
		WillOnce(Return(0, &lease.RevokedError{}))

	ExpectCall(rl, "Upgrade")().
		WillOnce(Return(nil, &lease.RevokedError{}))

	ExpectCall(t.leaser, "NewFile")().
		Times(2).
		WillRepeatedly(Return(nil, errors.New("")))

	t.proxy.ReadAt(context.Background(), []byte{}, 0)
	t.proxy.Upgrade(context.Background())
}