Search in sources :

Example 1 with DefaultAtomicSemaphoreService

use of io.atomix.core.semaphore.impl.DefaultAtomicSemaphoreService in project atomix by atomix.

the class AtomicSemaphoreTest method testSnapshot.

@Test
public void testSnapshot() throws Exception {
    AbstractAtomicSemaphoreService service = new DefaultAtomicSemaphoreService(new AtomicSemaphoreServiceConfig().setInitialCapacity(10));
    Field available = AbstractAtomicSemaphoreService.class.getDeclaredField("available");
    available.setAccessible(true);
    Field holders = AbstractAtomicSemaphoreService.class.getDeclaredField("holders");
    holders.setAccessible(true);
    Field waiterQueue = AbstractAtomicSemaphoreService.class.getDeclaredField("waiterQueue");
    waiterQueue.setAccessible(true);
    Field timers = AbstractAtomicSemaphoreService.class.getDeclaredField("timers");
    timers.setAccessible(true);
    available.set(service, 10);
    Map<Long, Integer> holdersMap = new HashMap<>();
    holdersMap.put((long) 1, 2);
    holdersMap.put((long) 3, 4);
    holdersMap.put((long) 5, 6);
    holders.set(service, holdersMap);
    // Class<?> waiter = Class.forName("io.atomix.core.semaphore.impl.DistributedSemaphoreService$Waiter");
    // LinkedList<Object> waiterLinkedList = new LinkedList<>();
    // 
    // waiterLinkedList.add(waiter.getConstructors()[0].newInstance(service,10L, 20L, 30L, 40, Long.MAX_VALUE));
    // waiterQueue.set(service, waiterLinkedList);
    Buffer buffer = HeapBuffer.allocate();
    service.backup(new DefaultBackupOutput(buffer, service.serializer()));
    AbstractAtomicSemaphoreService serviceRestore = new DefaultAtomicSemaphoreService(new AtomicSemaphoreServiceConfig().setInitialCapacity(10));
    serviceRestore.restore(new DefaultBackupInput(buffer.flip(), service.serializer()));
    assertEquals(10, available.get(serviceRestore));
    assertEquals(holdersMap, holders.get(serviceRestore));
// assertEquals(waiterQueue.get(serviceRestore), waiterLinkedList);
// assertEquals(1, ((Map) (timers.get(serviceRestore))).keySet().size());
}
Also used : AbstractAtomicSemaphoreService(io.atomix.core.semaphore.impl.AbstractAtomicSemaphoreService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Buffer(io.atomix.storage.buffer.Buffer) HeapBuffer(io.atomix.storage.buffer.HeapBuffer) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) AtomicSemaphoreServiceConfig(io.atomix.core.semaphore.impl.AtomicSemaphoreServiceConfig) DefaultBackupOutput(io.atomix.primitive.service.impl.DefaultBackupOutput) DefaultAtomicSemaphoreService(io.atomix.core.semaphore.impl.DefaultAtomicSemaphoreService) DefaultBackupInput(io.atomix.primitive.service.impl.DefaultBackupInput) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)1 AbstractAtomicSemaphoreService (io.atomix.core.semaphore.impl.AbstractAtomicSemaphoreService)1 AtomicSemaphoreServiceConfig (io.atomix.core.semaphore.impl.AtomicSemaphoreServiceConfig)1 DefaultAtomicSemaphoreService (io.atomix.core.semaphore.impl.DefaultAtomicSemaphoreService)1 DefaultBackupInput (io.atomix.primitive.service.impl.DefaultBackupInput)1 DefaultBackupOutput (io.atomix.primitive.service.impl.DefaultBackupOutput)1 Buffer (io.atomix.storage.buffer.Buffer)1 HeapBuffer (io.atomix.storage.buffer.HeapBuffer)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1