Search in sources :

Example 1 with SetPolicy

use of com.google.appengine.api.memcache.MemcacheService.SetPolicy in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testMultiPutWithOversizeValue.

@Test
public void testMultiPutWithOversizeValue() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    NamespaceManager.set("");
    int expirySecs = (int) (System.currentTimeMillis() / 1000) + 600;
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    String namespace = "";
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date((long) expirySecs * 1000));
    byte[] oneKey = makePbKey(ONE);
    byte[] twoKey = makePbKey(TWO);
    byte[] threeKey = makePbKey(THREE);
    String oneValue = "first value";
    String theVeryLongValue = Strings.repeat("x", 110 * AsyncMemcacheServiceImpl.ITEM_SIZE_LIMIT / 100);
    String threeValue = "third value";
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(oneKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(oneValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(twoKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(theVeryLongValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(threeKey)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(threeValue).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    expectAsyncCall("Set", request, new ApiProxy.ApplicationException(MemcacheServiceError.ErrorCode.UNSPECIFIED_ERROR_VALUE, "Error"));
    // ImmutableMap has deterministic ordering so the order of insertion matches the request PB
    Map<Serializable, String> collection = ImmutableMap.of(ONE, oneValue, TWO, theVeryLongValue, THREE, threeValue);
    // Make sure that an RPC application error of INVALID_REQUEST will result
    // in an exception with a message containing "bigger than maximum allowed"
    memcache.setErrorHandler(ErrorHandlers.getStrict());
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.putAll(collection, expiration, setPolicy));
    assertWithMessage("Expected exception to mention that maximum value exceeded but got " + ex).that(ex).hasMessageThat().contains("bigger than maximum allowed");
}
Also used : Serializable(java.io.Serializable) ApiProxy(com.google.apphosting.api.ApiProxy) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Example 2 with SetPolicy

use of com.google.appengine.api.memcache.MemcacheService.SetPolicy in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeSingleItemError.

@Test
public void testSetTooLargeSingleItemError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String testkey = "testkey";
    byte[] key = makePbKey(testkey);
    String value = Strings.repeat("v", (AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key.length + 1));
    int itemSize = value.length() + key.length;
    String expectedMessage = String.format("Memcache put: Item may not be more than %d bytes in " + "length; received %d bytes.", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE, itemSize);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.ERROR).build();
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.put(key, value, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) Test(org.junit.Test)

Example 3 with SetPolicy

use of com.google.appengine.api.memcache.MemcacheService.SetPolicy in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testSetTooLargeMultiError.

@Test
public void testSetTooLargeMultiError() {
    MemcacheService memcache = new MemcacheServiceImpl(null);
    String namespace = "";
    NamespaceManager.set(namespace);
    int expirySecs = (int) (MILLISECONDS.toSeconds(System.currentTimeMillis()) + 600);
    SetPolicy setPolicy = SetPolicy.SET_ALWAYS;
    MemcacheSetRequest.SetPolicy realPolicy = MemcacheSetRequest.SetPolicy.SET;
    Expiration expiration = Expiration.onDate(new Date(SECONDS.toMillis(expirySecs)));
    String expectedMessage = "Memcache put: 1 items failed for exceeding " + AsyncMemcacheServiceImpl.MAX_ITEM_SIZE + " bytes; keys: two. ";
    byte[] key1 = makePbKey(ONE);
    byte[] key2 = makePbKey(TWO);
    byte[] key3 = makePbKey(THREE);
    // Only the second item will fail.
    String value1 = Strings.repeat("v", 1000);
    String value2 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key2.length + 1);
    String value3 = Strings.repeat("v", AsyncMemcacheServiceImpl.MAX_ITEM_SIZE - key3.length);
    MemcacheSetRequest request = MemcacheSetRequest.newBuilder().setNameSpace(namespace).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key1)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value1).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key2)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value2).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(key3)).setFlags(Flag.UTF8.ordinal()).setValue(ByteString.copyFrom(serialize(value3).value)).setExpirationTime(expirySecs).setSetPolicy(realPolicy)).build();
    MemcacheSetResponse.SetStatusCode[] responses = new MemcacheSetResponse.SetStatusCode[] { MemcacheSetResponse.SetStatusCode.STORED, MemcacheSetResponse.SetStatusCode.ERROR, MemcacheSetResponse.SetStatusCode.STORED };
    MemcacheSetResponse response = MemcacheSetResponse.newBuilder().addSetStatus(responses[0]).addSetStatus(responses[1]).addSetStatus(responses[2]).build();
    Map<Serializable, String> collection = ImmutableMap.of(ONE, value1, TWO, value2, THREE, value3);
    expectAsyncCall("Set", request, response);
    MemcacheServiceException ex = assertThrows(MemcacheServiceException.class, () -> memcache.putAll(collection, expiration, setPolicy));
    assertThat(ex).hasMessageThat().isEqualTo(expectedMessage);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) Serializable(java.io.Serializable) SetStatusCode(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Example 4 with SetPolicy

use of com.google.appengine.api.memcache.MemcacheService.SetPolicy in project appengine-java-standard by GoogleCloudPlatform.

the class LocalMemcacheServiceTest method testSettingSameObjectDoesNotLeak.

@Test
public void testSettingSameObjectDoesNotLeak() {
    // Verify that the LRU starts out empty
    assertThat(getLocalMemcacheService().getLRU().getChainLength()).isEqualTo(0);
    for (int i = 0; i < 10; i++) {
        memcache.put("this", "that");
    }
    // There is only 1 key in the cache so the LRU should only have 1 entry.
    assertThat(getLocalMemcacheService().getLRU().getChainLength()).isEqualTo(1);
    // Now repeat the test but with different values, expiration values, and set policies.
    for (int i = 0; i < 10; i++) {
        for (SetPolicy policy : SetPolicy.values()) {
            memcache.put("this", "that" + i, Expiration.byDeltaSeconds(60), policy);
        }
    }
    // It's all the same key so the LRU should still only have 1 entry.
    assertThat(getLocalMemcacheService().getLRU().getChainLength()).isEqualTo(1);
}
Also used : SetPolicy(com.google.appengine.api.memcache.MemcacheService.SetPolicy) Test(org.junit.Test)

Aggregations

SetPolicy (com.google.appengine.api.memcache.MemcacheService.SetPolicy)4 Test (org.junit.Test)4 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)3 ByteString (com.google.protobuf.ByteString)3 Date (java.util.Date)3 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)2 Serializable (java.io.Serializable)2 SetStatusCode (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse.SetStatusCode)1 ApiProxy (com.google.apphosting.api.ApiProxy)1