Search in sources :

Example 6 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method singlePutTest.

private void singlePutTest(MemcacheService memcache, String namespace, Object key, String value) {
    byte[] keyValue = makePbKey(key);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(keyValue)).setFlags(value == null ? Flag.OBJECT.ordinal() : Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value)).setSetPolicy(MemcacheSetRequest.SetPolicy.SET).setExpirationTime(0)).build();
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).build();
    expectAsyncCall("Set", request, response);
    assertThat(memcache.put(key, value, null, SetPolicy.SET_ALWAYS)).isTrue();
    verifyAsyncCall("Set", request);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)

Example 7 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method multiPutTest.

private void multiPutTest(MemcacheService memcache, String namespace, int expirySecs, SetPolicy setPolicy, Object... expectedPuts) {
    byte[] nullKey = makePbKey(null);
    byte[] twoKey = makePbKey(TWO);
    byte[] threeKey = makePbKey(THREE);
    // Assume that responses[1] (i.e., TWO) is in the cache and the
    // others aren't.
    MemcacheSetResponse.SetStatusCode[] responses = null;
    MemcacheSetRequest.SetPolicy realPolicy = null;
    if (setPolicy == null || setPolicy == SetPolicy.SET_ALWAYS) {
        realPolicy = MemcacheSetRequest.SetPolicy.SET;
        responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.STORED };
    } else if (setPolicy == SetPolicy.ADD_ONLY_IF_NOT_PRESENT) {
        realPolicy = MemcacheSetRequest.SetPolicy.ADD;
        responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.NOT_STORED, MemcacheSetResponse.SetStatusCode.STORED };
    } else if (setPolicy == SetPolicy.REPLACE_ONLY_IF_PRESENT) {
        realPolicy = MemcacheSetRequest.SetPolicy.REPLACE;
        responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.NOT_STORED, MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.NOT_STORED };
    } else {
        throw new RuntimeException("Should'nt hit this - New MemcacheService.SetPolicy value?");
    }
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(nullKey)).setFlags(Flag.BYTES.ordinal()).setValue(ByteString.copyFrom(INT_123_BYTES)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(twoKey)).setFlags(Flag.OBJECT.ordinal()).setValue(ByteString.copyFrom(serialize(THREE).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(threeKey)).setFlags(Flag.OBJECT.ordinal()).setValue(ByteString.copyFrom(serialize(null).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(responses[0]).addSetStatus(responses[1]).addSetStatus(responses[2]).build();
    expectAsyncCall("Set", request, response);
    // use a LinkedHashMap so the order of insertion matches the request PB
    Map<Object, Object> collection = new LinkedHashMap<>();
    collection.put(null, INT_123_BYTES);
    collection.put(TWO, THREE);
    collection.put(THREE, null);
    Expiration expiration;
    if (expirySecs == 0) {
        expiration = null;
    } else {
        expiration = Expiration.onDate(new Date((long) expirySecs * 1000));
    }
    if (setPolicy == null) {
        if (expirySecs == 0) {
            memcache.putAll(collection);
        } else {
            memcache.putAll(collection, expiration);
        }
    } else {
        Set<Object> added = memcache.putAll(collection, expiration, setPolicy);
        assertThat(added).hasSize(expectedPuts.length);
        assertThat(added).containsExactlyElementsIn(expectedPuts);
    }
    verifyAsyncCall("Set", request);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetStatusCode(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with MemcacheSetResponse

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse in project appengine-java-standard by GoogleCloudPlatform.

the class AppIdentityServiceImplTest method doTestGetAccessTokenCached.

private void doTestGetAccessTokenCached(boolean shouldGetFail, MemcacheSetResponse.SetStatusCode setStatusCode) throws IOException {
    GetAccessTokenRequest.Builder requestBuilder = GetAccessTokenRequest.newBuilder();
    requestBuilder.addScope("scope1");
    requestBuilder.addScope("scope2");
    GetAccessTokenResponse.Builder responseBuilder = GetAccessTokenResponse.newBuilder();
    responseBuilder.setAccessToken(ACCESS_TOKEN);
    responseBuilder.setExpirationTime(EXPIRATION_TIME_SEC);
    byte[] memcacheKey = MemcacheSerialization.makePbKey("_ah_app_identity_['scope1','scope2']");
    byte[] memcacheValue = MemcacheSerialization.serialize(new AppIdentityService.GetAccessTokenResult(ACCESS_TOKEN, EXPIRATION_DATE)).value;
    // Surely there's a less fragile way to mock out memcache than at the apiproxy level.
    MemcacheGetRequest memcacheRequest = MemcacheGetRequest.newBuilder().setNameSpace("_ah_").addKey(ByteString.copyFrom(memcacheKey)).build();
    MemcacheGetResponse memcacheEmptyResponse = MemcacheGetResponse.getDefaultInstance();
    MemcacheSetRequest memcacheSetRequest = MemcacheSetRequest.newBuilder().setNameSpace("_ah_").addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(memcacheKey)).setFlags(// Flag.OBJECT.ordinal()
    2).setValue(ByteString.copyFrom(memcacheValue)).setSetPolicy(MemcacheSetRequest.SetPolicy.SET).setExpirationTime((int) (EXPIRATION_TIME_SEC - 360)).build()).build();
    MemcacheSetResponse memcacheSetResponse = MemcacheSetResponse.newBuilder().addSetStatus(setStatusCode).build();
    if (shouldGetFail) {
        expectAsyncMemcacheCallFailure("Get", memcacheRequest);
    } else {
        expectAsyncMemcacheCall("Get", memcacheRequest, memcacheEmptyResponse);
    }
    when(mockDelegate.makeSyncCall(same(ApiProxy.getCurrentEnvironment()), eq(AppIdentityServiceImpl.PACKAGE_NAME), eq(AppIdentityServiceImpl.GET_ACCESS_TOKEN_METHOD_NAME), eq(requestBuilder.build().toByteArray()))).thenReturn(responseBuilder.build().toByteArray());
    expectAsyncMemcacheCall("Set", memcacheSetRequest, memcacheSetResponse);
    AppIdentityService.GetAccessTokenResult getAccessTokenResult = service.getAccessToken(Lists.newArrayList("scope1", "scope2"));
    assertThat(getAccessTokenResult.getAccessToken()).isEqualTo(ACCESS_TOKEN);
    assertThat(getAccessTokenResult.getExpirationTime()).isEqualTo(EXPIRATION_DATE);
    // Now it should be in the cache. Try again!
    MemcacheGetResponse memcacheResponse;
    memcacheResponse = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(memcacheKey)).setValue(ByteString.copyFrom(memcacheValue)).setFlags(2).build()).build();
    expectAsyncMemcacheCall("Get", memcacheRequest, memcacheResponse);
    // clear local cache, so we take it from memcache
    AppIdentityServiceImpl.clearCache();
    getAccessTokenResult = service.getAccessToken(Lists.newArrayList("scope1", "scope2"));
    assertThat(getAccessTokenResult.getAccessToken()).isEqualTo(ACCESS_TOKEN);
    assertThat(getAccessTokenResult.getExpirationTime()).isEqualTo(EXPIRATION_DATE);
    // Now, check local cache
    getAccessTokenResult = service.getAccessToken(Lists.newArrayList("scope1", "scope2"));
    assertThat(getAccessTokenResult.getAccessToken()).isEqualTo(ACCESS_TOKEN);
    assertThat(getAccessTokenResult.getExpirationTime()).isEqualTo(EXPIRATION_DATE);
}
Also used : GetAccessTokenResponse(com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse) MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) GetAccessTokenRequest(com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)

Aggregations

MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)8 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)8 ByteString (com.google.protobuf.ByteString)5 Date (java.util.Date)4 Test (org.junit.Test)3 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)2 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)2 SetPolicy (com.google.appengine.api.memcache.MemcacheService.SetPolicy)2 MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)2 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)2 SetStatusCode (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode)2 LinkedHashMap (java.util.LinkedHashMap)2 GetAccessTokenRequest (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest)1 GetAccessTokenResponse (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse)1 CasValues (com.google.appengine.api.memcache.MemcacheService.CasValues)1 SetPolicy (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest.SetPolicy)1 Serializable (java.io.Serializable)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1