예제 #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()
}
예제 #2
0
func (t *mutableContentTest) SetUp(ti *TestInfo) {
	t.ctx = ti.Ctx

	// Set up the mock initial contents, including a default size.
	t.initialContent = mock_lease.NewMockReadProxy(
		ti.MockController,
		"initialContent")

	ExpectCall(t.initialContent, "Size")().
		WillRepeatedly(Return(initialContentSize))

	// Set up a mock read/write lease.
	t.rwl = mock_lease.NewMockReadWriteLease(
		ti.MockController,
		"rwl")

	// Ignore uninteresting calls.
	ExpectCall(t.initialContent, "CheckInvariants")().
		WillRepeatedly(Return())

	// Set up the clock.
	t.clock.SetTime(time.Date(2012, 8, 15, 22, 56, 0, 0, time.Local))

	// And the mutable content.
	t.mc.ctx = ti.Ctx
	t.mc.wrapped = mutable.NewContent(
		t.initialContent,
		&t.clock)
}
예제 #3
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)
}
예제 #4
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)
}
예제 #5
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")))
}
예제 #6
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))
}
예제 #7
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")))
}
예제 #8
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")))
}
예제 #9
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)
}
예제 #10
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)
}
예제 #11
0
func (t *ReadProxyTest) Upgrade_Successful() {
	// NewFile
	expected := mock_lease.NewMockReadWriteLease(t.mockController, "rwl")
	ExpectCall(t.leaser, "NewFile")().
		WillOnce(Return(expected, nil))

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

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

	// Call.
	rwl, err := t.proxy.Upgrade(context.Background())
	AssertEq(nil, err)
	ExpectEq(expected, rwl)
}
예제 #12
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")))
}
예제 #13
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())
}