Search in sources :

Example 1 with Key

use of org.infinispan.test.data.Key in project infinispan by infinispan.

the class BaseStoreTest method testLoadAndStoreBytesValues.

public void testLoadAndStoreBytesValues() throws PersistenceException, IOException, InterruptedException {
    assertIsEmpty();
    SerializationContext ctx = ProtobufUtil.newSerializationContext();
    SerializationContextInitializer sci = TestDataSCI.INSTANCE;
    sci.registerSchema(ctx);
    sci.registerMarshallers(ctx);
    Marshaller userMarshaller = new ProtoStreamMarshaller(ctx);
    WrappedBytes key = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Key("key")));
    WrappedBytes key2 = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Key("key2")));
    WrappedBytes value = new WrappedByteArray(userMarshaller.objectToByteBuffer(new Person()));
    assertFalse(cl.contains(key));
    PersistenceMarshaller persistenceMarshaller = getMarshaller();
    cl.write(MarshalledEntryUtil.create(key, value, persistenceMarshaller));
    assertEquals(value, cl.loadEntry(key).getValue());
    MarshallableEntry entry = cl.loadEntry(key);
    assertTrue("Expected an immortalEntry", entry.getMetadata() == null || entry.expiryTime() == -1 || entry.getMetadata().maxIdle() == -1);
    assertContains(key, true);
    assertFalse(cl.delete(key2));
    assertTrue(cl.delete(key));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) TestObjectStreamMarshaller(org.infinispan.marshall.TestObjectStreamMarshaller) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) Marshaller(org.infinispan.commons.marshall.Marshaller) WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes) ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) PersistenceMarshaller(org.infinispan.marshall.persistence.PersistenceMarshaller) SerializationContextInitializer(org.infinispan.protostream.SerializationContextInitializer) Person(org.infinispan.test.data.Person) Key(org.infinispan.test.data.Key)

Example 2 with Key

use of org.infinispan.test.data.Key in project infinispan by infinispan.

the class APINonTxOffHeapTest method testCustomObjectKey.

public void testCustomObjectKey() {
    Key ck = new Key("a");
    assertNull(cache.get(ck));
    cache.put(ck, "blah");
    assertEquals("blah", cache.get(ck));
}
Also used : Key(org.infinispan.test.data.Key)

Example 3 with Key

use of org.infinispan.test.data.Key in project infinispan by infinispan.

the class MemoryBasedEvictionFunctionalStoreAsBinaryTest method testCustomClass.

public void testCustomClass() throws Exception {
    long numberInserted = CACHE_SIZE / 10;
    Random random = new Random();
    // More than likely there will be a few hundred byte overhead
    for (float i = 0; i < numberInserted; i++) {
        cache.put(new Key(randomStringFullOfInt(random, 10)), new Key(randomStringFullOfInt(random, 10)));
    }
    assertTrue(cache.getAdvancedCache().getDataContainer().size() < numberInserted);
}
Also used : Random(java.util.Random) Key(org.infinispan.test.data.Key)

Example 4 with Key

use of org.infinispan.test.data.Key in project infinispan by infinispan.

the class MarshalledValueContextTest method testContentsOfContext.

public void testContentsOfContext() throws Exception {
    Cache<Key, String> c = cacheManager.getCache();
    ContextExtractingInterceptor cex = new ContextExtractingInterceptor();
    assertTrue(c.getAdvancedCache().getAsyncInterceptorChain().addInterceptorAfter(cex, InvocationContextInterceptor.class));
    c.put(new Key("k"), "v");
    assertEquals("v", c.get(new Key("k")));
    TransactionManager tm = c.getAdvancedCache().getTransactionManager();
    tm.begin();
    c.getAdvancedCache().lock(new Key("k"));
    LockManager lockManager = TestingUtil.extractComponent(c, LockManager.class);
    assertTrue(cex.ctx instanceof LocalTxInvocationContext);
    assertEquals("Looked up key should not be in transactional invocation context " + "as we don't perform any changes", 0, cex.ctx.lookedUpEntriesCount());
    assertEquals("Only one lock should be held", 1, lockManager.getNumberOfLocksHeld());
    c.put(new Key("k"), "v2");
    assertEquals("Still should only be one entry in the context", 1, cex.ctx.lookedUpEntriesCount());
    assertEquals("Only one lock should be held", 1, lockManager.getNumberOfLocksHeld());
    tm.commit();
    assertEquals("No locks should be held anymore", 0, lockManager.getNumberOfLocksHeld());
    assertEquals("v2", c.get(new Key("k")));
}
Also used : LockManager(org.infinispan.util.concurrent.locks.LockManager) TransactionManager(javax.transaction.TransactionManager) LocalTxInvocationContext(org.infinispan.context.impl.LocalTxInvocationContext) InvocationContextInterceptor(org.infinispan.interceptors.impl.InvocationContextInterceptor) Key(org.infinispan.test.data.Key)

Example 5 with Key

use of org.infinispan.test.data.Key in project infinispan by infinispan.

the class AsyncAPITxSyncReplTest method testAsyncTxMethods.

public void testAsyncTxMethods() throws Exception {
    Cache<Object, String> c1 = cache(0);
    Cache c2 = cache(1);
    String v = "v";
    String v2 = "v2";
    String v3 = "v3";
    String v4 = "v4";
    String v5 = "v5";
    String v6 = "v6";
    String v_null = "v_nonexistent";
    Key key = new Key("k");
    TransactionManager tm = TestingUtil.getTransactionManager(c1);
    // put
    tm.begin();
    Future<String> f = c1.putAsync(key, v);
    assert f != null;
    Transaction t = tm.suspend();
    assert c2.get(key) == null;
    tm.resume(t);
    assert f.get() == null;
    tm.commit();
    assertOnAllCaches(key, v, c1, c2);
    tm.begin();
    f = c1.putAsync(key, v2);
    assert f != null;
    t = tm.suspend();
    assert c2.get(key).equals(v);
    tm.resume(t);
    assert !f.isCancelled();
    assert f.get().equals(v);
    tm.commit();
    assertOnAllCaches(key, v2, c1, c2);
    // putAll
    tm.begin();
    final Future<Void> f2 = c1.putAllAsync(Collections.singletonMap(key, v3));
    assert f2 != null;
    eventually(f2::isDone);
    t = tm.suspend();
    assert c2.get(key).equals(v2);
    tm.resume(t);
    assert !f2.isCancelled();
    assert f2.get() == null;
    tm.commit();
    assertOnAllCaches(key, v3, c1, c2);
    // putIfAbsent
    tm.begin();
    final Future f1 = c1.putIfAbsentAsync(key, v4);
    assert f1 != null;
    eventually(f1::isDone);
    t = tm.suspend();
    assert c2.get(key).equals(v3);
    tm.resume(t);
    assert !f1.isCancelled();
    assert f1.get().equals(v3);
    tm.commit();
    assertOnAllCaches(key, v3, c1, c2);
    // remove
    tm.begin();
    final Future f3 = c1.removeAsync(key);
    assert f3 != null;
    eventually(f3::isDone);
    t = tm.suspend();
    assert c2.get(key).equals(v3);
    tm.resume(t);
    assert !f3.isCancelled();
    assert f3.get().equals(v3);
    tm.commit();
    assertOnAllCaches(key, null, c1, c2);
    // putIfAbsent again
    tm.begin();
    final Future f4 = c1.putIfAbsentAsync(key, v4);
    assert f4 != null;
    eventually(f4::isDone);
    assert f4.isDone();
    t = tm.suspend();
    assert c2.get(key) == null;
    tm.resume(t);
    assert !f4.isCancelled();
    assert f4.get() == null;
    tm.commit();
    assertOnAllCaches(key, v4, c1, c2);
    // removecond
    tm.begin();
    Future<Boolean> f5 = c1.removeAsync(key, v_null);
    assert f5 != null;
    assert !f5.isCancelled();
    assert f5.get().equals(false);
    assert f5.isDone();
    tm.commit();
    assertOnAllCaches(key, v4, c1, c2);
    tm.begin();
    final Future f6 = c1.removeAsync(key, v4);
    assert f6 != null;
    eventually(f6::isDone);
    assert f6.isDone();
    t = tm.suspend();
    assert c2.get(key).equals(v4);
    tm.resume(t);
    assert !f6.isCancelled();
    assert f6.get().equals(true);
    tm.commit();
    assertOnAllCaches(key, null, c1, c2);
    // replace
    tm.begin();
    final Future f7 = c1.replaceAsync(key, v5);
    assert f7 != null;
    assert !f7.isCancelled();
    assert f7.get() == null;
    assert f7.isDone();
    tm.commit();
    assertOnAllCaches(key, null, c1, c2);
    tm.begin();
    c1.put(key, v);
    tm.commit();
    tm.begin();
    final Future f8 = c1.replaceAsync(key, v5);
    assert f8 != null;
    eventually(f8::isDone);
    t = tm.suspend();
    assert c2.get(key).equals(v);
    tm.resume(t);
    assert !f8.isCancelled();
    assert f8.get().equals(v);
    tm.commit();
    assertOnAllCaches(key, v5, c1, c2);
    // replace2
    tm.begin();
    final Future f9 = c1.replaceAsync(key, v_null, v6);
    assert f9 != null;
    assert !f9.isCancelled();
    assert f9.get().equals(false);
    assert f9.isDone();
    tm.commit();
    assertOnAllCaches(key, v5, c1, c2);
    tm.begin();
    final Future f10 = c1.replaceAsync(key, v5, v6);
    assert f10 != null;
    eventually(f10::isDone);
    t = tm.suspend();
    assert c2.get(key).equals(v5);
    tm.resume(t);
    assert !f10.isCancelled();
    assert f10.get().equals(true);
    tm.commit();
    assertOnAllCaches(key, v6, c1, c2);
}
Also used : Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) Future(java.util.concurrent.Future) Key(org.infinispan.test.data.Key) Cache(org.infinispan.Cache)

Aggregations

Key (org.infinispan.test.data.Key)7 TransactionManager (javax.transaction.TransactionManager)2 Marshaller (org.infinispan.commons.marshall.Marshaller)2 ProtoStreamMarshaller (org.infinispan.commons.marshall.ProtoStreamMarshaller)2 WrappedByteArray (org.infinispan.commons.marshall.WrappedByteArray)2 WrappedBytes (org.infinispan.commons.marshall.WrappedBytes)2 TestObjectStreamMarshaller (org.infinispan.marshall.TestObjectStreamMarshaller)2 PersistenceMarshaller (org.infinispan.marshall.persistence.PersistenceMarshaller)2 SerializationContext (org.infinispan.protostream.SerializationContext)2 SerializationContextInitializer (org.infinispan.protostream.SerializationContextInitializer)2 Person (org.infinispan.test.data.Person)2 Random (java.util.Random)1 UUID (java.util.UUID)1 Future (java.util.concurrent.Future)1 Transaction (javax.transaction.Transaction)1 Cache (org.infinispan.Cache)1 LocalTxInvocationContext (org.infinispan.context.impl.LocalTxInvocationContext)1 InvocationContextInterceptor (org.infinispan.interceptors.impl.InvocationContextInterceptor)1 MarshallableEntry (org.infinispan.persistence.spi.MarshallableEntry)1 LockManager (org.infinispan.util.concurrent.locks.LockManager)1