Search in sources :

Example 6 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method testErrorHandlingStrict.

@Test
public void testErrorHandlingStrict() {
    byte[] oneKey = makePbKey(ONE);
    byte[] oneValue = serialize(ONE).value;
    MemcacheService memcache = new MemcacheServiceImpl("");
    memcache.setErrorHandler(ErrorHandlers.getStrict());
    MemcacheGetRequest getRequest = MemcacheGetRequest.newBuilder().setNameSpace("").addKey(ByteString.copyFrom(oneKey)).build();
    expectAsyncCall("Get", getRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.get(ONE));
    verifyAsyncCall("Get", getRequest);
    expectAsyncCall("Get", getRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.contains(ONE));
    verifyAsyncCall("Get", getRequest);
    MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace("").addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(oneKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(oneValue)).setSetPolicy(MemcacheSetRequest.SetPolicy.ADD).setExpirationTime(0)).build();
    expectAsyncCall("Set", setRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.put(ONE, "one", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
    verifyAsyncCall("Set", setRequest);
    expectAsyncCall("Set", setRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
    verifyAsyncCall("Set", setRequest);
    // Successful set -> no error logged or exception thrown.
    expectAsyncCall("Set", setRequest, MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.STORED).build());
    assertThat(memcache.put(ONE, "one", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isTrue();
    verifyAsyncCall("Set", setRequest);
    expectAsyncCall("Set", setRequest, MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.STORED).build());
    assertThat(memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isEqualTo(ImmutableSet.of(ONE));
    verifyAsyncCall("Set", setRequest);
    // Item not stored because of set policy.
    expectAsyncCall("Set", setRequest, MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.ERROR).build());
    assertThrows(MemcacheServiceException.class, () -> memcache.put(ONE, "one", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
    verifyAsyncCall("Set", setRequest);
    expectAsyncCall("Set", setRequest, MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.ERROR).build());
    assertThrows(MemcacheServiceException.class, () -> memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
    verifyAsyncCall("Set", setRequest);
    MemcacheIncrementRequest incrementRequest = MemcacheIncrementRequest.newBuilder().setNameSpace("").setKey(ByteString.copyFrom(makePbKey(INT_123))).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).build();
    expectAsyncCall("Increment", incrementRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.increment(INT_123, -17));
    verifyAsyncCall("Increment", incrementRequest);
    expectAsyncCall("Increment", incrementRequest, new ApiProxy.ApplicationException(6, "Error"));
    assertThrows(InvalidValueException.class, () -> memcache.increment(INT_123, -17));
    verifyAsyncCall("Increment", incrementRequest);
    MemcacheBatchIncrementRequest batchIncrementRequest = MemcacheBatchIncrementRequest.newBuilder().setNameSpace("").addItem(MemcacheIncrementRequest.newBuilder().setKey(ByteString.copyFrom(makePbKey(INT_123))).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT)).build();
    expectAsyncCall("BatchIncrement", batchIncrementRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.incrementAll(Arrays.asList(INT_123), -17));
    verifyAsyncCall("BatchIncrement", batchIncrementRequest);
    // Partial BatchIncrement error doesn't throw, but returns null.
    expectAsyncCall("BatchIncrement", batchIncrementRequest, MemcacheBatchIncrementResponse.newBuilder().addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR)).build());
    assertThat(memcache.incrementAll(Arrays.asList(INT_123), -17)).containsExactly(INT_123, null);
    MemcacheDeleteRequest deleteRequest = MemcacheDeleteRequest.newBuilder().setNameSpace("").addItem(MemcacheDeleteRequest.Item.newBuilder().setKey(ByteString.copyFrom(oneKey)).setDeleteTime(0)).build();
    expectAsyncCall("Delete", deleteRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.delete(ONE));
    verifyAsyncCall("Delete", deleteRequest);
    expectAsyncCall("Delete", deleteRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, () -> memcache.deleteAll(Arrays.asList(ONE)));
    verifyAsyncCall("Delete", deleteRequest);
    MemcacheFlushRequest flushRequest = MemcacheFlushRequest.getDefaultInstance();
    expectAsyncCall("FlushAll", flushRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThrows(MemcacheServiceException.class, memcache::clearAll);
    verifyAsyncCall("FlushAll", flushRequest);
}
Also used : MemcacheDeleteRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteRequest) ApiProxy(com.google.apphosting.api.ApiProxy) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) MemcacheFlushRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest) MemcacheIncrementRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest) MemcacheBatchIncrementRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementRequest) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 7 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method multiGetTest.

private void multiGetTest(MemcacheService memcache, String namespace) {
    byte[] oneKey = makePbKey(ONE);
    byte[] twoKey = makePbKey(TWO);
    byte[] nullKey = makePbKey(null);
    MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).addKey(ByteString.copyFrom(oneKey)).addKey(ByteString.copyFrom(twoKey)).addKey(ByteString.copyFrom(nullKey)).build();
    MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(Flag.INTEGER.ordinal()).setKey(ByteString.copyFrom(twoKey)).setValue(ByteString.copyFrom(serialize(123).value))).addItem(MemcacheGetResponse.Item.newBuilder().setFlags(Flag.OBJECT.ordinal()).setKey(ByteString.copyFrom(nullKey)).setValue(ByteString.copyFrom(serialize(null).value))).build();
    expectAsyncCall("Get", request, response);
    List<String> collection = new ArrayList<>();
    collection.add(ONE);
    collection.add(TWO);
    collection.add(null);
    Map<String, Object> result = memcache.getAll(collection);
    assertThat(result).hasSize(2);
    assertThat(result.get(TWO)).isEqualTo(123);
    assertThat(result).containsKey(null);
    assertThat(result.get(null)).isNull();
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString)

Example 8 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method testGetIdentifiable.

@Test
public void testGetIdentifiable() {
    MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    long casId = 5;
    String[] keys = { ONE, TWO, null, null };
    String[] values = { ONE, null, TWO, null };
    for (int i = 0; i < keys.length; i++) {
        String key = keys[i];
        String value = values[i];
        byte[] pbKey = makePbKey(key);
        MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).setForCas(true).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(value == null ? Flag.OBJECT.ordinal() : Flag.UTF8.ordinal()).setKey(ByteString.copyFrom(pbKey)).setValue(ByteString.copyFrom(serialize(value).value)).setCasId(casId)).build();
        expectAsyncCall("Get", request, response);
        IdentifiableValueImpl v = (IdentifiableValueImpl) memcache.getIdentifiable(key);
        assertThat(v.getCasId()).isEqualTo(casId);
        assertThat(v.getValue()).isEqualTo(value);
        verifyAsyncCall("Get", request);
    }
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 9 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method multiGetIdentifiableTest.

private void multiGetIdentifiableTest(MemcacheService memcache, String namespace) {
    MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).setForCas(true).addKey(ByteString.copyFrom(makePbKey(ONE))).addKey(ByteString.copyFrom(makePbKey(null))).addKey(ByteString.copyFrom(makePbKey("Missing"))).build();
    MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey(ONE))).setCasId(10).setFlags(Flag.OBJECT.ordinal()).setValue(ByteString.copyFrom(serialize(null).value))).addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey(null))).setCasId(20).setFlags(Flag.INTEGER.ordinal()).setValue(ByteString.copyFrom(serialize(456).value))).build();
    expectAsyncCall("Get", request, response);
    ArrayList<String> collection = new ArrayList<>();
    collection.add(ONE);
    collection.add(null);
    collection.add("Missing");
    Map<String, IdentifiableValue> result = memcache.getIdentifiables(collection);
    assertThat(result).hasSize(2);
    assertThat(result.get(ONE).getValue()).isEqualTo(null);
    assertThat(((IdentifiableValueImpl) result.get(ONE)).getCasId()).isEqualTo(10);
    assertThat(result.get(null).getValue()).isEqualTo(456);
    assertThat(((IdentifiableValueImpl) result.get(null)).getCasId()).isEqualTo(20);
    verifyAsyncCall("Get", request);
}
Also used : IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ArrayList(java.util.ArrayList) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ByteString(com.google.protobuf.ByteString)

Example 10 with MemcacheGetRequest

use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest 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

MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)10 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)8 ByteString (com.google.protobuf.ByteString)7 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)4 IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)3 Test (org.junit.Test)3 MemcacheBatchIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementRequest)2 MemcacheDeleteRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheDeleteRequest)2 MemcacheFlushRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest)2 MemcacheIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest)2 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)2 ApiProxy (com.google.apphosting.api.ApiProxy)2 ArrayList (java.util.ArrayList)2 GetAccessTokenRequest (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenRequest)1 GetAccessTokenResponse (com.google.appengine.api.appidentity.AppIdentityServicePb.GetAccessTokenResponse)1 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)1 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)1