// =====================================================================
// Test Expectations
// =====================================================================
ActivatedPolicyRetriever retriever =
new CachingActivatedPolicyRetriever(policyCache, retrieverMock);
Policy actualPolicy;
// ---------------------------------------------------------------------
// First Request
// ---------------------------------------------------------------------
// First request fails to find the policy in the cache so retrieves
// it and stores it in the cache.
// The policy returned by the retriever underlying the provider.
retrieverMock.expects
.retrievePolicy(projectMock, projectRelativeName)
.returns(originalPolicy);
// The current time for the calculation of the expiration time.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(1000));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(originalPolicy, actualPolicy);
expectations.verify();
// ---------------------------------------------------------------------
// Second Request
// ---------------------------------------------------------------------
// Second request finds the policy in the cache and it has not yet
// expired so is returned without invoking the provider.
// The current time used to determine whether the policy has expired.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(1500));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(originalPolicy, actualPolicy);
expectations.verify();
// ---------------------------------------------------------------------
// Third Request
// ---------------------------------------------------------------------
// Third request finds the policy in the cache but it has expired so
// the provider is invoked.
// The current time used to determine whether the policy has expired.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(2500));
// The new policy returned by the retriever underlying the provider.
retrieverMock.expects
.retrievePolicy(projectMock, projectRelativeName)
.returns(refreshedPolicy);
// The current time used to calculate the expiration time of the newly
// retrieved policy. This is different to the above as it will
// happen after the provider has returned the policy which will be
// some time afterwards.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(3000));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(refreshedPolicy, actualPolicy);
expectations.verify();
// ---------------------------------------------------------------------
// Fourth Request
// ---------------------------------------------------------------------
// Fourth request occurs after the policy has expired and retry is
// due but fails.
// Time is after expiry time.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(6500));
retrieverMock.expects
.retrievePolicy(projectMock, projectRelativeName)
.returns(null);
projectMock.expects.getCacheControlDefaults()
.returns(constraints.getDefaultCacheControl());
// Get the time after the retriever has failed to return a policy so
// that it can calculate the expiry time for the retry.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(8000));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(refreshedPolicy, actualPolicy);
expectations.verify();
// ---------------------------------------------------------------------
// Fifth Request
// ---------------------------------------------------------------------
// Fifth request occurs before the retry period is due so should not
// invoke the retriever.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(8999));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(refreshedPolicy, actualPolicy);
expectations.verify();
// ---------------------------------------------------------------------
// Sixth Request
// ---------------------------------------------------------------------
// Sixth request occurs after the retry period has passed and so should
// invoke the retriever.
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(11000));
retrieverMock.expects
.retrievePolicy(projectMock, projectRelativeName)
.returns(originalPolicy);
clockMock.expects.getCurrentTime().returns(Time.inMilliSeconds(12000));
actualPolicy = retriever.retrievePolicy(projectMock,
projectRelativeName);
assertSame(originalPolicy, actualPolicy);
}