Search in sources :

Example 61 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class WriteWriteTXConcurrencyTest method simpleWWTest.

@Test
public void simpleWWTest() {
    //Instantiate a Corfu Stream named "A" dedicated to an SMRmap object.
    SMRMap<String, Integer> map = (SMRMap<String, Integer>) instantiateCorfuObject(new TypeToken<SMRMap<String, Integer>>() {
    }, "A");
    AtomicInteger valA = new AtomicInteger(0), valB = new AtomicInteger(0);
    t(0, () -> WWTXBegin());
    t(0, () -> map.put("a", 1));
    t(0, () -> map.put("b", 1));
    t(1, () -> WWTXBegin());
    t(1, () -> {
        Integer ga = map.get("a");
        if (ga != null)
            valA.set(ga);
    });
    t(1, () -> {
        Integer gb = map.get("b");
        if (gb != null)
            valB.set(gb);
    });
    t(1, () -> TXEnd());
    t(0, () -> TXEnd());
    assertThat(valA.get()).isEqualTo(valB.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SMRMap(org.corfudb.runtime.collections.SMRMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeToken(com.google.common.reflect.TypeToken) Test(org.junit.Test)

Example 62 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class SMRMapTest method readSetDiffFromWriteSet.

@Test
@SuppressWarnings("unchecked")
public void readSetDiffFromWriteSet() throws Exception {
    Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test1").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    Map<String, String> testMap2 = getRuntime().getObjectsView().build().setStreamName("test2").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    testMap.put("a", "b");
    testMap2.put("a", "c");
    Semaphore s1 = new Semaphore(0);
    Semaphore s2 = new Semaphore(0);
    scheduleConcurrently(1, threadNumber -> {
        s1.tryAcquire(PARAMETERS.TIMEOUT_NORMAL.toMillis(), TimeUnit.MILLISECONDS);
        getRuntime().getObjectsView().TXBegin();
        testMap2.put("a", "d");
        getRuntime().getObjectsView().TXEnd();
        s2.release();
    });
    scheduleConcurrently(1, threadNumber -> {
        getRuntime().getObjectsView().TXBegin();
        testMap.compute("b", (k, v) -> testMap2.get("a"));
        s1.release();
        s2.tryAcquire(PARAMETERS.TIMEOUT_NORMAL.toMillis(), TimeUnit.MILLISECONDS);
        assertThatThrownBy(() -> getRuntime().getObjectsView().TXEnd()).isInstanceOf(TransactionAbortedException.class);
    });
    executeScheduled(PARAMETERS.CONCURRENCY_TWO, PARAMETERS.TIMEOUT_NORMAL);
}
Also used : TypeToken(com.google.common.reflect.TypeToken) ToString(lombok.ToString) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 63 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class SMRMapTest method canWriteScanAndFilterToSingle.

@Test
@SuppressWarnings("unchecked")
public void canWriteScanAndFilterToSingle() throws Exception {
    Map<String, String> corfuInstancesMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    corfuInstancesMap.clear();
    assertThat(corfuInstancesMap.put("a", "CorfuServer")).isNull();
    assertThat(corfuInstancesMap.put("b", "CorfuClient")).isNull();
    assertThat(corfuInstancesMap.put("c", "CorfuClient")).isNull();
    assertThat(corfuInstancesMap.put("d", "CorfuServer")).isNull();
    List<String> corfuServerList = ((SMRMap) corfuInstancesMap).scanAndFilter(p -> p.equals("CorfuServer"));
    assertThat(corfuServerList.size()).isEqualTo(2);
    for (String corfuInstance : corfuServerList) {
        assertThat(corfuInstance).isEqualTo("CorfuServer");
    }
}
Also used : TypeToken(com.google.common.reflect.TypeToken) ToString(lombok.ToString) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 64 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class CompileProxyTest method testObjectCounterReadConcurrency.

/**
     * the test interleaves reads and writes to a shared counter.
     *
     * The test uses the interleaving engine to interleave numTasks*threads state machines.
     * A simple state-machine is built. It randomly chooses to either read or append the counter.
     * On a read, the last written value is expected.
     *
     * @throws Exception
     */
@Test
public void testObjectCounterReadConcurrency() throws Exception {
    CorfuSharedCounter sharedCounter = getDefaultRuntime().getObjectsView().build().setStreamName("my stream").setTypeToken(new TypeToken<CorfuSharedCounter>() {
    }).open();
    int numTasks = PARAMETERS.NUM_ITERATIONS_LOW;
    Random r = new Random(PARAMETERS.SEED);
    sharedCounter.setValue(-1);
    AtomicInteger lastUpdate = new AtomicInteger(-1);
    assertThat(sharedCounter.getValue()).isEqualTo(lastUpdate.get());
    // only one step: randomly choose between read/append of the shared counter
    addTestStep((task_num) -> {
        if (r.nextBoolean()) {
            sharedCounter.setValue(task_num);
            // remember the last written value
            lastUpdate.set(task_num);
        } else {
            // expect to read the value in lastUpdate
            assertThat(sharedCounter.getValue()).isEqualTo(lastUpdate.get());
        }
    });
    // invoke the interleaving engine
    scheduleInterleaved(PARAMETERS.CONCURRENCY_SOME, PARAMETERS.CONCURRENCY_SOME * numTasks);
}
Also used : Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TypeToken(com.google.common.reflect.TypeToken) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Example 65 with TypeToken

use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.

the class CorfuSMRObjectProxyTest method canOpenObjectWithTwoRuntimes.

@Test
public void canOpenObjectWithTwoRuntimes() throws Exception {
    getDefaultRuntime();
    final int TEST_VALUE = 42;
    TestClass testClass = (TestClass) instantiateCorfuObject(new TypeToken<TestClass>() {
    }, "test");
    testClass.set(TEST_VALUE);
    assertThat(testClass.get()).isEqualTo(TEST_VALUE);
    CorfuRuntime runtime2 = new CorfuRuntime(getDefaultEndpoint());
    runtime2.connect();
    TestClass testClass2 = (TestClass) instantiateCorfuObject(runtime2, new TypeToken<TestClass>() {
    }, "test");
    assertThat(testClass2.get()).isEqualTo(TEST_VALUE);
}
Also used : TypeToken(com.google.common.reflect.TypeToken) CorfuRuntime(org.corfudb.runtime.CorfuRuntime) Test(org.junit.Test)

Aggregations

TypeToken (com.google.common.reflect.TypeToken)135 Test (org.junit.Test)60 HttpResponse (co.cask.common.http.HttpResponse)26 URL (java.net.URL)24 ServiceResponse (com.microsoft.rest.ServiceResponse)22 Response (retrofit2.Response)22 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)18 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)17 PipedInputStream (java.io.PipedInputStream)17 PipedOutputStream (java.io.PipedOutputStream)17 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)16 List (java.util.List)16 Map (java.util.Map)11 ImmutableList (com.google.common.collect.ImmutableList)9 Type (java.lang.reflect.Type)9 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)9 NotFoundException (co.cask.cdap.common.NotFoundException)8 VirtualMachineScaleSetVMInstanceIDs (com.microsoft.azure.management.compute.VirtualMachineScaleSetVMInstanceIDs)8 Gson (com.google.gson.Gson)7 JsonObject (com.google.gson.JsonObject)7