Search in sources :

Example 1 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method testPutIfUntouched.

@Test
public void testPutIfUntouched() {
    MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    long casId = 5;
    for (String key : new String[] { ONE, null }) {
        byte[] pbKey = makePbKey(key);
        MemcacheSerialization.ValueAndFlags oneVaf = serialize(ONE);
        MemcacheSerialization.ValueAndFlags twoVaf = serialize(TWO);
        MemcacheGetRequest getRequest = MemcacheGetRequest.newBuilder().setNameSpace(namespace).setForCas(true).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse getResponse = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(oneVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setValue(ByteString.copyFrom(oneVaf.value)).setCasId(casId)).build();
        expectAsyncCall("Get", getRequest, getResponse);
        MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setValue(ByteString.copyFrom(twoVaf.value)).setFlags(twoVaf.flags.ordinal()).setKey(ByteString.copyFrom(pbKey)).setExpirationTime(0).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(casId)).build();
        MemcacheSetResponse setResponse = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).build();
        expectAsyncCallWithoutReset("Set", setRequest, setResponse);
        IdentifiableValueImpl v = (IdentifiableValueImpl) memcache.getIdentifiable(key);
        assertThat(v.getCasId()).isEqualTo(casId);
        assertThat(v.getValue()).isEqualTo(ONE);
        assertThat(memcache.putIfUntouched(key, v, TWO)).isTrue();
        verifyAsyncCall("Set", setRequest);
    }
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) 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) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 2 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method expectDeserializationExceptionOnGet.

private void expectDeserializationExceptionOnGet() {
    String key = "deserialize_error_key";
    byte[] pbKey = makePbKey(key);
    Object value = new DeserializationExceptionGenerator();
    MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace("").addKey(ByteString.copyFrom(pbKey)).build();
    MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(pbKey)).setFlags(Flag.OBJECT.ordinal()).setValue(ByteString.copyFrom(serialize(value).value))).build();
    expectAsyncCall("Get", request, response);
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 3 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method oneGetTest.

private void oneGetTest(MemcacheService memcache, String namespace) {
    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).addKey(ByteString.copyFrom(pbKey)).build();
        MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setKey(ByteString.copyFrom(pbKey)).setFlags(value == null ? Flag.OBJECT.ordinal() : Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value))).build();
        expectAsyncCall("Get", request, response);
        assertThat(memcache.get(key)).isEqualTo(value);
    }
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 4 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method containsTest.

private void containsTest(MemcacheService memcache, String namespace) {
    String[] keys = { ONE, null };
    for (String key : keys) {
        byte[] oneKey = makePbKey(key);
        MemcacheGetRequest request = MemcacheGetRequest.newBuilder().setNameSpace(namespace).addKey(ByteString.copyFrom(oneKey)).build();
        MemcacheGetResponse response = MemcacheGetResponse.newBuilder().addItem(MemcacheGetResponse.Item.newBuilder().setFlags(Flag.UTF8.ordinal()).setKey(ByteString.copyFrom(oneKey)).setValue(ByteString.copyFrom(serialize(key).value))).build();
        expectAsyncCall("Get", request, response);
        assertThat(memcache.contains(key)).isTrue();
        verifyAsyncCall("Get", request);
    }
}
Also used : MemcacheGetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse) MemcacheGetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest) ByteString(com.google.protobuf.ByteString)

Example 5 with MemcacheGetRequest

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

the class MemcacheServiceImplTest method errorHandlingPermissiveTest.

private void errorHandlingPermissiveTest(boolean isConsistent, ErrorHandler errorHandler) {
    byte[] oneKey = makePbKey(ONE);
    byte[] oneValue = serialize(ONE).value;
    MemcacheService memcache = new MemcacheServiceImpl("");
    memcache.setErrorHandler(errorHandler);
    // Get calls
    MemcacheGetRequest getRequest = MemcacheGetRequest.newBuilder().setNameSpace("").addKey(ByteString.copyFrom(oneKey)).build();
    expectAsyncCall("Get", getRequest, new ApiProxy.ApplicationException(1, "Error"));
    Object value = memcache.get(ONE);
    assertThat(value).isNull();
    verifyAsyncCall("Get", getRequest);
    expectAsyncCall("Get", getRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThat(memcache.contains(ONE)).isFalse();
    verifyAsyncCall("Get", getRequest);
    expectAsyncCall("Get", getRequest, new byte[] { 10, 20 });
    assertThat(memcache.get(ONE)).isNull();
    verifyAsyncCall("Get", getRequest);
    // Set calls
    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();
    // Because of AllowPartialResults for Set method, Set should almost always succeed and return
    // any error code in the response payload. Application error can occur only for early checks,
    // e.g. out of quota, ShardLock check failure or missing ServingState.
    expectAsyncCall("Set", setRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThat(memcache.put(ONE, "one", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isFalse();
    verifyAsyncCall("Set", setRequest);
    expectAsyncCall("Set", setRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThat(memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isEmpty();
    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 -> logs an error or throws, depending on the error
    // handler used (for backwards compatibility).
    expectAsyncCall("Set", setRequest, MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.ERROR).build());
    if (isConsistent) {
        assertThat(memcache.put(ONE, "one", null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isFalse();
    } else {
        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());
    if (isConsistent) {
        assertThat(memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT)).isEmpty();
    } else {
        assertThrows(MemcacheServiceException.class, () -> memcache.putAll(ImmutableMap.of(ONE, "one"), null, SetPolicy.ADD_ONLY_IF_NOT_PRESENT));
    }
    verifyAsyncCall("Set", setRequest);
    // Increment calls
    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"));
    assertThat(memcache.increment(INT_123, -17)).isEqualTo(null);
    verifyAsyncCall("Increment", incrementRequest);
    expectAsyncCall("Increment", incrementRequest, new ApiProxy.ApplicationException(6, "Error"));
    assertThrows(InvalidValueException.class, () -> memcache.increment(INT_123, -17));
    verifyAsyncCall("Increment", incrementRequest);
    // BatchIncrement calls
    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"));
    assertThat(memcache.incrementAll(Arrays.asList(INT_123), -17)).isEqualTo(Collections.singletonMap(INT_123, null));
    verifyAsyncCall("BatchIncrement", batchIncrementRequest);
    expectAsyncCall("BatchIncrement", batchIncrementRequest, MemcacheBatchIncrementResponse.newBuilder().addItem(MemcacheIncrementResponse.newBuilder().setIncrementStatus(IncrementStatusCode.ERROR)).build());
    assertThat(memcache.incrementAll(Arrays.asList(INT_123), -17)).isEqualTo(Collections.singletonMap(INT_123, null));
    verifyAsyncCall("BatchIncrement", batchIncrementRequest);
    // Delete calls
    MemcacheDeleteRequest deleteRequest = MemcacheDeleteRequest.newBuilder().setNameSpace("").addItem(MemcacheDeleteRequest.Item.newBuilder().setKey(ByteString.copyFrom(oneKey)).setDeleteTime(0)).build();
    expectAsyncCall("Delete", deleteRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThat(memcache.delete(ONE)).isFalse();
    verifyAsyncCall("Delete", deleteRequest);
    expectAsyncCall("Delete", deleteRequest, new ApiProxy.ApplicationException(1, "Error"));
    assertThat(memcache.deleteAll(Arrays.asList(ONE))).isEmpty();
    verifyAsyncCall("Delete", deleteRequest);
    // Flush calls
    MemcacheFlushRequest flushRequest = MemcacheFlushRequest.getDefaultInstance();
    expectAsyncCall("FlushAll", flushRequest, new ApiProxy.ApplicationException(1, "Error"));
    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)

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