Search in sources :

Example 1 with IdentifiableValueImpl

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

use of com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testCasValues.

@Test
public void testCasValues() {
    IdentifiableValue idVal1 = new IdentifiableValueImpl("oldVal1", 1);
    CasValues casVal = new CasValues(idVal1, "newVal1");
    assertThat(new CasValues(idVal1, "newVal1")).isEqualTo(casVal);
    assertThat(new CasValues(idVal1, "newVal1").hashCode()).isEqualTo(casVal.hashCode());
    // Explicitly call equals() to guarantee that CasValues.equals() is being invoked.
    new EqualsTester().addEqualityGroup(casVal).addEqualityGroup("str3").testEquals();
    assertThat(new CasValues(idVal1, null)).isNotEqualTo(casVal);
    assertThat(new CasValues(idVal1, null).hashCode()).isNotEqualTo(casVal.hashCode());
    IdentifiableValue idVal2 = new IdentifiableValueImpl("oldVal1", 2);
    assertThat(new CasValues(idVal2, "newVal1")).isNotEqualTo(casVal);
    // Test expiration
    Expiration exp1 = Expiration.byDeltaMillis(10);
    casVal = new CasValues(idVal1, "newVal1", exp1);
    assertThat(new CasValues(idVal1, "newVal1")).isNotEqualTo(casVal);
    assertThat(new CasValues(idVal1, "newVal1").hashCode()).isNotEqualTo(casVal.hashCode());
    assertThat(new CasValues(idVal1, "newVal1", exp1)).isEqualTo(casVal);
    assertThat(new CasValues(idVal1, "newVal1", exp1).hashCode()).isEqualTo(casVal.hashCode());
}
Also used : IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) CasValues(com.google.appengine.api.memcache.MemcacheService.CasValues) EqualsTester(com.google.common.testing.EqualsTester) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) Test(org.junit.Test)

Example 3 with IdentifiableValueImpl

use of com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method testIdentifiableValueImpl.

@Test
public void testIdentifiableValueImpl() {
    IdentifiableValue val = new IdentifiableValueImpl("str1", 1);
    assertThat(new IdentifiableValueImpl("str1", 1)).isEqualTo(val);
    assertThat(new IdentifiableValueImpl("str1", 1).hashCode()).isEqualTo(val.hashCode());
    // Explicitly call equals() to guarantee that IdentifiableValueImpl.equals() is being invoked.
    new EqualsTester().addEqualityGroup(val).addEqualityGroup("str").testEquals();
    assertThat(new IdentifiableValueImpl("str1", 2)).isNotEqualTo(val);
    assertThat(new IdentifiableValueImpl("str1", 2).hashCode()).isNotEqualTo(val.hashCode());
    assertThat(new IdentifiableValueImpl("str2", 1)).isNotEqualTo(val);
    assertThat(new IdentifiableValueImpl("str2", 1).hashCode()).isNotEqualTo(val.hashCode());
    // Test null value.
    val = new IdentifiableValueImpl(null, 1);
    assertThat(new IdentifiableValueImpl(null, 1)).isEqualTo(val);
    assertThat(new IdentifiableValueImpl(null, 1).hashCode()).isEqualTo(val.hashCode());
}
Also used : IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) EqualsTester(com.google.common.testing.EqualsTester) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) Test(org.junit.Test)

Example 4 with IdentifiableValueImpl

use of com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl in project appengine-java-standard by GoogleCloudPlatform.

the class MemcacheServiceImplTest method multiPutIfUntouched.

private void multiPutIfUntouched(String ns, Expiration requestExpiration) {
    CasValues one = new CasValues(new IdentifiableValueImpl(ONE, 10), "new." + ONE, Expiration.onDate(new Date(200)));
    ValueAndFlags oneValueAndFlag = serialize(one.getNewValue());
    CasValues two = new CasValues(new IdentifiableValueImpl(TWO, 20), null);
    ValueAndFlags twoValueAndFlag = serialize(two.getNewValue());
    MemcacheServiceImpl memcache = new MemcacheServiceImpl(null);
    MemcacheSetRequest setRequest = MemcacheSetRequest.newBuilder().setNameSpace(ns).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey(null))).setValue(ByteString.copyFrom(oneValueAndFlag.value)).setFlags(oneValueAndFlag.flags.ordinal()).setExpirationTime(one.getExipration().getSecondsValue()).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(10)).addItem(MemcacheSetRequest.Item.newBuilder().setKey(ByteString.copyFrom(makePbKey("2"))).setValue(ByteString.copyFrom(twoValueAndFlag.value)).setFlags(twoValueAndFlag.flags.ordinal()).setExpirationTime(requestExpiration == null ? 0 : requestExpiration.getSecondsValue()).setSetPolicy(MemcacheSetRequest.SetPolicy.CAS).setCasId(20)).build();
    MemcacheSetResponse setResponse = MemcacheSetResponse.newBuilder().addSetStatus(MemcacheSetResponse.SetStatusCode.STORED).addSetStatus(MemcacheSetResponse.SetStatusCode.EXISTS).build();
    expectAsyncCall("Set", setRequest, setResponse);
    Map<String, CasValues> values = new LinkedHashMap<>();
    values.put(null, one);
    values.put("2", two);
    Set<String> response = memcache.putIfUntouched(values, requestExpiration);
    assertThat(response).hasSize(1);
    assertThat(response).contains(null);
    verifyAsyncCall("Set", setRequest);
}
Also used : MemcacheSetResponse(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse) CasValues(com.google.appengine.api.memcache.MemcacheService.CasValues) IdentifiableValueImpl(com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl) ValueAndFlags(com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags) ByteString(com.google.protobuf.ByteString) Date(java.util.Date) MemcacheSetRequest(com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with IdentifiableValueImpl

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

Aggregations

IdentifiableValueImpl (com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.IdentifiableValueImpl)6 ByteString (com.google.protobuf.ByteString)4 Test (org.junit.Test)4 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)3 MemcacheGetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest)3 MemcacheGetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetResponse)3 ValueAndFlags (com.google.appengine.api.memcache.MemcacheSerialization.ValueAndFlags)2 CasValues (com.google.appengine.api.memcache.MemcacheService.CasValues)2 MemcacheSetRequest (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetRequest)2 MemcacheSetResponse (com.google.appengine.api.memcache.MemcacheServicePb.MemcacheSetResponse)2 EqualsTester (com.google.common.testing.EqualsTester)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1