Search in sources :

Example 1 with Locker

use of org.olat.core.util.coordinate.Locker in project OpenOLAT by OpenOLAT.

the class LockTest method testCreateDeleteAcquire.

@Test
public void testCreateDeleteAcquire() {
    // some setup
    List<Identity> identities = new ArrayList<Identity>();
    for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
        Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
        identities.add(i1);
    }
    dbInstance.closeSession();
    Identity ident = identities.get(0);
    Identity ident2 = identities.get(1);
    OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
    // ------------------ test the clusterlockmanager ----------------------
    // create a lock
    String asset = OresHelper.createStringRepresenting(ores, "locktest");
    LockImpl li = clusterLockManager.createLockImpl(asset, ident);
    clusterLockManager.saveLock(li);
    dbInstance.closeSession();
    // find it
    LockImpl l2 = clusterLockManager.findLock(asset);
    assertNotNull(l2);
    assertEquals(li.getKey(), l2.getKey());
    // delete it
    int deletedLock = clusterLockManager.deleteLock(asset, ident);
    dbInstance.closeSession();
    Assert.assertEquals(1, deletedLock);
    // may not find it again
    LockImpl l3 = clusterLockManager.findLock(asset);
    assertNull(l3);
    // ------------------ test the clusterlocker ----------------------
    // access the cluster locker explicitely
    Locker cl = clusterCoordinator.getLocker();
    // acquire
    LockResult res1 = cl.acquireLock(ores, ident, "abc");
    assertTrue(res1.isSuccess());
    dbInstance.closeSession();
    // reacquire same identity (get from db)
    LockResult res11 = cl.acquireLock(ores, ident, "abc");
    long lock1Ac = res11.getLockAquiredTime();
    assertTrue(res11.isSuccess());
    assertTrue(lock1Ac > 0);
    dbInstance.closeSession();
    // acquire by another identity must fail
    LockResult res2 = cl.acquireLock(ores, ident2, "abc");
    assertFalse(res2.isSuccess());
    dbInstance.closeSession();
    // reacquire same identity
    LockResult res3 = cl.acquireLock(ores, ident, "abc");
    assertTrue(res3.isSuccess());
    dbInstance.closeSession();
    // make sure it is not locked anymore
    boolean lo3 = cl.isLocked(ores, "abc");
    assertTrue(lo3);
    // test the admin
    List<LockEntry> entries = cl.adminOnlyGetLockEntries();
    assertEquals(1, entries.size());
    LockEntry le = entries.get(0);
    // must be original owner
    assertEquals(le.getOwner().getName(), ident.getName());
    // release lock
    cl.releaseLock(res3);
    dbInstance.closeSession();
    // test the admin
    entries = cl.adminOnlyGetLockEntries();
    assertEquals(0, entries.size());
    // make sure it is not locked anymore
    boolean lo = cl.isLocked(ores, "abc");
    assertFalse(lo);
}
Also used : Locker(org.olat.core.util.coordinate.Locker) OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) LockResult(org.olat.core.util.coordinate.LockResult) LockEntry(org.olat.core.util.coordinate.LockEntry) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 2 with Locker

use of org.olat.core.util.coordinate.Locker in project OpenOLAT by OpenOLAT.

the class LockTest method testSaveEvent.

@Test
public void testSaveEvent() {
    Identity identity = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-save-event-");
    dbInstance.closeSession();
    log.info("Created identity=" + identity);
    // The group has no creation date -> commit will fail
    GroupImpl entry = new GroupImpl();
    entry.setName("bar");
    try {
        dbInstance.saveObject(entry);
        dbInstance.commit();
        fail("Should generate an error");
    } catch (DBRuntimeException dre) {
        log.info("DB connection is in error-state");
    }
    // DB transaction must be in error state for this test
    try {
        Locker locker = clusterCoordinator.getLocker();
        assertTrue(locker instanceof ClusterLocker);
        log.info("ClusterLocker created");
        Event event = new SignOnOffEvent(identity, false);
        log.info("START locker.event(event)");
        ((ClusterLocker) locker).event(event);
        log.info("DONE locker.event(event)");
    } catch (Exception ex) {
        log.error("", ex);
        fail("BLOCKER : ClusterLocker.event is not error-safe, db exception could happen and de-register event-listener");
    }
}
Also used : Locker(org.olat.core.util.coordinate.Locker) DBRuntimeException(org.olat.core.logging.DBRuntimeException) GroupImpl(org.olat.basesecurity.model.GroupImpl) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) Event(org.olat.core.gui.control.Event) Identity(org.olat.core.id.Identity) DBRuntimeException(org.olat.core.logging.DBRuntimeException) Test(org.junit.Test)

Example 3 with Locker

use of org.olat.core.util.coordinate.Locker in project openolat by klemens.

the class LockTest method testSaveEvent.

@Test
public void testSaveEvent() {
    Identity identity = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-save-event-");
    dbInstance.closeSession();
    log.info("Created identity=" + identity);
    // The group has no creation date -> commit will fail
    GroupImpl entry = new GroupImpl();
    entry.setName("bar");
    try {
        dbInstance.saveObject(entry);
        dbInstance.commit();
        fail("Should generate an error");
    } catch (DBRuntimeException dre) {
        log.info("DB connection is in error-state");
    }
    // DB transaction must be in error state for this test
    try {
        Locker locker = clusterCoordinator.getLocker();
        assertTrue(locker instanceof ClusterLocker);
        log.info("ClusterLocker created");
        Event event = new SignOnOffEvent(identity, false);
        log.info("START locker.event(event)");
        ((ClusterLocker) locker).event(event);
        log.info("DONE locker.event(event)");
    } catch (Exception ex) {
        log.error("", ex);
        fail("BLOCKER : ClusterLocker.event is not error-safe, db exception could happen and de-register event-listener");
    }
}
Also used : Locker(org.olat.core.util.coordinate.Locker) DBRuntimeException(org.olat.core.logging.DBRuntimeException) GroupImpl(org.olat.basesecurity.model.GroupImpl) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) Event(org.olat.core.gui.control.Event) Identity(org.olat.core.id.Identity) DBRuntimeException(org.olat.core.logging.DBRuntimeException) Test(org.junit.Test)

Example 4 with Locker

use of org.olat.core.util.coordinate.Locker in project openolat by klemens.

the class LockTest method testCreateDeleteAcquire.

@Test
public void testCreateDeleteAcquire() {
    // some setup
    List<Identity> identities = new ArrayList<Identity>();
    for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
        Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
        identities.add(i1);
    }
    dbInstance.closeSession();
    Identity ident = identities.get(0);
    Identity ident2 = identities.get(1);
    OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
    // ------------------ test the clusterlockmanager ----------------------
    // create a lock
    String asset = OresHelper.createStringRepresenting(ores, "locktest");
    LockImpl li = clusterLockManager.createLockImpl(asset, ident);
    clusterLockManager.saveLock(li);
    dbInstance.closeSession();
    // find it
    LockImpl l2 = clusterLockManager.findLock(asset);
    assertNotNull(l2);
    assertEquals(li.getKey(), l2.getKey());
    // delete it
    int deletedLock = clusterLockManager.deleteLock(asset, ident);
    dbInstance.closeSession();
    Assert.assertEquals(1, deletedLock);
    // may not find it again
    LockImpl l3 = clusterLockManager.findLock(asset);
    assertNull(l3);
    // ------------------ test the clusterlocker ----------------------
    // access the cluster locker explicitely
    Locker cl = clusterCoordinator.getLocker();
    // acquire
    LockResult res1 = cl.acquireLock(ores, ident, "abc");
    assertTrue(res1.isSuccess());
    dbInstance.closeSession();
    // reacquire same identity (get from db)
    LockResult res11 = cl.acquireLock(ores, ident, "abc");
    long lock1Ac = res11.getLockAquiredTime();
    assertTrue(res11.isSuccess());
    assertTrue(lock1Ac > 0);
    dbInstance.closeSession();
    // acquire by another identity must fail
    LockResult res2 = cl.acquireLock(ores, ident2, "abc");
    assertFalse(res2.isSuccess());
    dbInstance.closeSession();
    // reacquire same identity
    LockResult res3 = cl.acquireLock(ores, ident, "abc");
    assertTrue(res3.isSuccess());
    dbInstance.closeSession();
    // make sure it is not locked anymore
    boolean lo3 = cl.isLocked(ores, "abc");
    assertTrue(lo3);
    // test the admin
    List<LockEntry> entries = cl.adminOnlyGetLockEntries();
    assertEquals(1, entries.size());
    LockEntry le = entries.get(0);
    // must be original owner
    assertEquals(le.getOwner().getName(), ident.getName());
    // release lock
    cl.releaseLock(res3);
    dbInstance.closeSession();
    // test the admin
    entries = cl.adminOnlyGetLockEntries();
    assertEquals(0, entries.size());
    // make sure it is not locked anymore
    boolean lo = cl.isLocked(ores, "abc");
    assertFalse(lo);
}
Also used : Locker(org.olat.core.util.coordinate.Locker) OLATResourceable(org.olat.core.id.OLATResourceable) ArrayList(java.util.ArrayList) LockResult(org.olat.core.util.coordinate.LockResult) LockEntry(org.olat.core.util.coordinate.LockEntry) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Identity (org.olat.core.id.Identity)4 Locker (org.olat.core.util.coordinate.Locker)4 ArrayList (java.util.ArrayList)2 GroupImpl (org.olat.basesecurity.model.GroupImpl)2 Event (org.olat.core.gui.control.Event)2 OLATResourceable (org.olat.core.id.OLATResourceable)2 DBRuntimeException (org.olat.core.logging.DBRuntimeException)2 SignOnOffEvent (org.olat.core.util.SignOnOffEvent)2 LockEntry (org.olat.core.util.coordinate.LockEntry)2 LockResult (org.olat.core.util.coordinate.LockResult)2