use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method incrementTest.
private void incrementTest(MemcacheService memcache, String namespace, Long initialValue) {
for (Integer key : new Integer[] { INT_123, null }) {
byte[] sha1bytes = makePbKey(key);
MemcacheIncrementRequest.Builder requestBuilder = MemcacheIncrementRequest.newBuilder().setNameSpace(namespace).setKey(ByteString.copyFrom(sha1bytes)).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT);
if (initialValue != null) {
requestBuilder.setInitialValue(initialValue);
requestBuilder.setInitialFlags(MemcacheSerialization.Flag.LONG.ordinal());
}
MemcacheIncrementRequest request = requestBuilder.build();
MemcacheIncrementResponse response = MemcacheIncrementResponse.newBuilder().setNewValue(25).build();
expectAsyncCall("Increment", request, response);
if (initialValue == null) {
assertThat(memcache.increment(key, -17)).isEqualTo(Long.valueOf(25));
} else {
assertThat(memcache.increment(key, -17, initialValue)).isEqualTo(Long.valueOf(25));
}
}
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest in project appengine-java-standard by GoogleCloudPlatform.
the class LocalMemcacheService method batchIncrement.
public MemcacheBatchIncrementResponse batchIncrement(Status status, MemcacheBatchIncrementRequest batchReq) {
MemcacheBatchIncrementResponse.Builder result = MemcacheBatchIncrementResponse.newBuilder();
String namespace = batchReq.getNameSpace();
synchronized (mockCache) {
// only increment offers atomicity
for (MemcacheIncrementRequest req : batchReq.getItemList()) {
MemcacheIncrementResponse.Builder resp = MemcacheIncrementResponse.newBuilder();
Key key = new Key(req.getKey().toByteArray());
long delta = req.getDelta();
if (req.getDirection() == Direction.DECREMENT) {
delta = -delta;
}
CacheEntry ce = getWithExpiration(namespace, key);
long newvalue;
if (ce == null) {
if (req.hasInitialValue()) {
MemcacheSerialization.ValueAndFlags value;
try {
value = MemcacheSerialization.serialize(req.getInitialValue());
} catch (IOException e) {
throw new ApiProxy.UnknownException("Serialzation error: " + e);
}
ce = new CacheEntry(namespace, key, value.value, value.flags.ordinal(), 0);
} else {
stats.recordMiss();
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
}
stats.recordHit(ce);
Long longval;
try {
longval = Long.parseLong(new String(ce.value, UTF8));
} catch (NumberFormatException e) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
} catch (UnsupportedEncodingException e) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
if (longval < 0) {
resp.setIncrementStatus(IncrementStatusCode.NOT_CHANGED);
result.addItem(resp);
continue;
}
newvalue = longval;
newvalue += delta;
if (delta < 0 && newvalue < 0) {
newvalue = 0;
}
stats.recordDelete(ce);
try {
ce.value = Long.toString(newvalue).getBytes(UTF8);
} catch (UnsupportedEncodingException e) {
// Shouldn't happen.
throw new ApiProxy.UnknownException(UTF8 + " encoding was not found.");
}
// don't change the flags; it keeps its original size/type
ce.bytes = key.getBytes().length + ce.value.length;
Map<Key, CacheEntry> namespaceMap = getOrMakeSubMap(mockCache, namespace);
namespaceMap.remove(key);
namespaceMap.put(key, ce);
stats.recordAdd(ce);
resp.setIncrementStatus(IncrementStatusCode.OK);
resp.setNewValue(newvalue);
result.addItem(resp);
}
}
status.setSuccessful(true);
return result.build();
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest 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);
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementInvalidType.
@Test
public void testIncrementInvalidType() {
MemcacheIncrementRequest request = MemcacheIncrementRequest.newBuilder().setNameSpace("").setKey(ByteString.copyFrom(makePbKey(INT_123))).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).build();
expectAsyncCall("Increment", request, new ApiProxy.ApplicationException(6, "Error"));
assertThrows(InvalidValueException.class, () -> new MemcacheServiceImpl(null).increment(INT_123, -17));
}
use of com.google.appengine.api.memcache.MemcacheServicePb.MemcacheIncrementRequest in project appengine-java-standard by GoogleCloudPlatform.
the class MemcacheServiceImplTest method testIncrementMissing.
@Test
public void testIncrementMissing() {
MemcacheIncrementRequest request = MemcacheIncrementRequest.newBuilder().setNameSpace("").setKey(ByteString.copyFrom(makePbKey(INT_123))).setDelta(17).setDirection(MemcacheIncrementRequest.Direction.DECREMENT).build();
MemcacheIncrementResponse response = MemcacheIncrementResponse.getDefaultInstance();
expectAsyncCall("Increment", request, response);
Long result = new MemcacheServiceImpl(null).increment(INT_123, -17);
assertThat(result).isEqualTo(null);
verifyAsyncCall("Increment", request);
}
Aggregations