Search in sources :

Example 6 with MemcacheIncrementRequest

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

Aggregations

MemcacheIncrementRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest)6 ApiProxy (com.google.apphosting.api.ApiProxy)4 MemcacheIncrementResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementResponse)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 MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)2 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)2 MemcacheSerialization (com.google.appengine.api.memcache.MemcacheSerialization)1 MemcacheBatchIncrementResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheBatchIncrementResponse)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1