use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testConcurrentAddUpdateEvent.
/**
* Test concurrent add/update event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddUpdateEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddUpdateEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddUpdateEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final String TEST_EVENT_SUBJECT_2_UPDATED = "testUpdatedEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-3-");
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
// Generate event for update
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
log.info("testConcurrentAddUpdateEvent thread2 addEvent2 DONE");
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddUpdateEvent thread1 addEvent1");
calendarManager.addEventTo(currentCalendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddUpdateEvent thread1 addEvent1 DONE");
// 3. check event1 exist
currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = currentCalendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
// 4. sleep 2sec
// 5. check event1 still exist (event2 added in meantime)
currentCalendar = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = currentCalendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddUpdateEvent thread1 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
// thread 2
Thread thread2 = new Thread() {
public void run() {
try {
Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 3. add Event2 (breakpoint of thread1 blocks)
log.info("testConcurrentAddUpdateEvent thread2 updateEvent2");
calendarManager.updateEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
log.info("testConcurrentAddUpdateEvent thread1 updateEvent2 DONE");
// 4. check event2 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, updatedEvent);
assertEquals("Wrong calendar-event subject", updatedEvent.getSubject(), TEST_EVENT_SUBJECT_2_UPDATED);
// 5. check event1 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
// Delete Event
boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
assertTrue(removed);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddUpdateEvent thread2 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
thread1.start();
thread2.start();
try {
boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
assertTrue("Test takes too long (more than 10s)", interrupt);
} catch (InterruptedException e) {
fail("" + e.getMessage());
}
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
log.info("exception: " + exception.getMessage());
exception.printStackTrace();
}
assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
log.info("testConcurrentAddUpdateEvent finish successful");
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testAddChangeEvent_v2.
@Test
public void testAddChangeEvent_v2() {
Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
String TEST_EVENT_ID = "id-testAddEvent";
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 1. Test Add Event
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
Date start = calendar.getTime();
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date end = calendar.getTime();
KalendarEvent testEvent = new KalendarEvent(TEST_EVENT_ID, null, "testEvent", start, end);
calendarManager.addEventTo(cal, testEvent);
// empty the cache
emptyCalendarCache();
Kalendar reloadedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent reloadedEvent = reloadedCal.getEvent(TEST_EVENT_ID, null);
Assert.assertNotNull("Could not found added event", reloadedEvent);
Assert.assertEquals("Added event has wrong subject", testEvent.getSubject(), reloadedEvent.getSubject());
Assert.assertEquals(reloadedEvent.getBegin(), start);
Assert.assertEquals(reloadedEvent.getEnd(), end);
// 2. Test Change event
calendar.add(Calendar.HOUR_OF_DAY, 1);
Date updatedEnd = calendar.getTime();
calendar.add(Calendar.HOUR_OF_DAY, -4);
Date updatedStart = calendar.getTime();
reloadedEvent.setSubject("testEvent changed");
reloadedEvent.setBegin(updatedStart);
reloadedEvent.setEnd(updatedEnd);
calendarManager.updateEventFrom(cal, reloadedEvent);
// empty the cache
emptyCalendarCache();
Kalendar updatedCal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = updatedCal.getEvent(TEST_EVENT_ID, null);
Assert.assertNotNull("Could not found updated event", updatedEvent);
Assert.assertEquals("Added event has wrong subject", "testEvent changed", updatedEvent.getSubject());
Assert.assertEquals(updatedStart, updatedEvent.getBegin());
Assert.assertEquals(updatedEnd, updatedEvent.getEnd());
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testListEventsForPeriod.
@Test
public void testListEventsForPeriod() {
final int numEvents = 10000;
// maximum of 14 days duration
final int maxEventDuratio = 1000 * 60 * 60 * 24 * 14;
final int oneYearSec = 60 * 60 * 24 * 365;
final int goBackNumYears = 1;
final long kalendarStart = new Date().getTime() - (((long) goBackNumYears * oneYearSec) * 1000);
Identity test = JunitTestHelper.createAndPersistIdentityAsUser("test");
Kalendar kalendar = new Kalendar("test", CalendarManager.TYPE_USER);
log.info("*** Starting test with the following configuration:");
log.info("*** Number of events: " + numEvents);
log.info("*** Maximum event duration (ms): " + maxEventDuratio);
log.info("*** Generate events in between " + new Date(kalendarStart) + " and " + new Date(kalendarStart + (1000 * ((long) goBackNumYears * oneYearSec))));
Random rand = new Random();
long startUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
for (int i = 0; i < numEvents; i++) {
long begin = kalendarStart + (1000 * ((long) rand.nextInt(goBackNumYears * oneYearSec)));
String eventId = "id" + i;
KalendarEvent event = new KalendarEvent(eventId, "test" + i, new Date(begin), rand.nextInt(maxEventDuratio));
kalendar.addEvent(event);
}
long stopUsed = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
log.info("*** SETUP: Kalendar structure uses approx. " + (stopUsed - startUsed) / 1024 + " kb memory.");
calendarManager.persistCalendar(kalendar);
log.info("*** Load calendar...");
long start = System.currentTimeMillis();
calendarManager.getPersonalCalendar(test).getKalendar();
long stop = System.currentTimeMillis();
log.info("Duration load: " + (stop - start) + " ms.");
log.info("*** Find events within period...");
start = System.currentTimeMillis();
List<KalendarEvent> events = calendarManager.getEvents(kalendar, new Date(kalendarStart), new Date(kalendarStart + (1000 * ((long) (goBackNumYears * oneYearSec)))), true);
stop = System.currentTimeMillis();
log.info("Duration find: " + (stop - start) + " ms.");
log.info("Found " + events.size() + " events out of " + kalendar.getEvents().size() + " total events.");
assertEquals(kalendar.getEvents().size(), events.size());
log.info("*** Save calendar...");
start = System.currentTimeMillis();
calendarManager.persistCalendar(kalendar);
stop = System.currentTimeMillis();
log.info("Duration save: " + (stop - start) + " ms.");
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testConcurrentAddEvent.
/**
* Test concurrent add event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-2-");
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddEvent thread1 addEvent1");
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddEvent thread1 addEvent1 DONE");
// 3. check event1 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
// 4. sleep 2sec
// 5. check event1 still exist (event2 added in meantime)
cal = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddEvent thread1 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
// thread 2
Thread thread2 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
// 3. add Event2 (breakpoint of thread1 blocks)
log.info("testConcurrentAddEvent thread2 addEvent2");
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
log.info("testConcurrentAddEvent thread1 addEvent2 DONE");
// 4. check event2 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
// 5. check event1 exist
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddEvent thread2 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
thread1.start();
thread2.start();
try {
boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
assertTrue("Test takes too long (more than 10s)", interrupt);
} catch (InterruptedException e) {
fail("" + e.getMessage());
}
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
log.info("exception: " + exception.getMessage());
exception.printStackTrace();
}
assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
log.info("testConcurrentAddEvent finish successful");
}
use of org.olat.commons.calendar.model.Kalendar in project openolat by klemens.
the class ICalFileCalendarManagerTest method testConcurrentAddRemoveEvent.
/**
* Test concurrent add/delete event with two threads and code-point to control concurrency.
*/
@Test
public void testConcurrentAddRemoveEvent() {
final String TEST_EVENT_ID_1 = "id-testConcurrentAddRemoveEvent-1";
final String TEST_EVENT_SUBJECT_1 = "testEvent1";
final String TEST_EVENT_ID_2 = "id-testConcurrentAddRemoveEvent-2";
final String TEST_EVENT_SUBJECT_2 = "testEvent2";
final Identity test = JunitTestHelper.createAndPersistIdentityAsRndUser("ical-1-");
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));
// Generate event for update
Kalendar cal = calendarManager.getPersonalCalendar(test).getKalendar();
calendarManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
cal = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
assertEquals("Wrong calendar-event subject", event2.getSubject(), TEST_EVENT_SUBJECT_2);
log.info("testConcurrentAddRemoveEvent thread2 addEvent2 DONE");
final CountDownLatch doneSignal = new CountDownLatch(2);
// thread 1
Thread thread1 = new Thread() {
public void run() {
try {
// 1. load calendar
Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
log.info("testConcurrentAddRemoveEvent thread1 addEvent1");
calendarManager.addEventTo(calendar, new KalendarEvent(TEST_EVENT_ID_1, TEST_EVENT_SUBJECT_1, new Date(), 1));
log.info("testConcurrentAddRemoveEvent thread1 addEvent1 DONE");
// 3. check event1 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
// 4. sleep 2sec
// 5. check event1 still exist (event2 added in meantime)
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddRemoveEvent thread1 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
// thread 2
Thread thread2 = new Thread() {
public void run() {
try {
Kalendar calendar = calendarManager.getPersonalCalendar(test).getKalendar();
// 3. add Event2 (breakpoint of thread1 blocks)
log.info("testConcurrentAddRemoveEvent thread2 removeEvent2");
boolean removed = calendarManager.removeEventFrom(calendar, new KalendarEvent(TEST_EVENT_ID_2, TEST_EVENT_SUBJECT_2, new Date(), 1));
assertTrue(removed);
log.info("testConcurrentAddRemoveEvent thread1 removeEvent2 DONE");
// 4. check event2 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = calendar.getEvent(TEST_EVENT_ID_2, null);
assertNull("Still found deleted event with id=" + TEST_EVENT_ID_2, updatedEvent);
// 5. check event1 exist
calendar = calendarManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = calendar.getEvent(TEST_EVENT_ID_1, null);
assertNotNull("Did not found event with id=" + TEST_EVENT_ID_1, event1);
assertEquals("Wrong calendar-event subject", event1.getSubject(), TEST_EVENT_SUBJECT_1);
statusList.add(Boolean.TRUE);
log.info("testConcurrentAddRemoveEvent thread2 finished");
} catch (Exception ex) {
// no exception should happen
exceptionHolder.add(ex);
} finally {
doneSignal.countDown();
DBFactory.getInstance().commitAndCloseSession();
}
}
};
thread1.start();
thread2.start();
try {
boolean interrupt = doneSignal.await(10, TimeUnit.SECONDS);
assertTrue("Test takes too long (more than 10s)", interrupt);
} catch (InterruptedException e) {
fail("" + e.getMessage());
}
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
log.info("exception: " + exception.getMessage());
exception.printStackTrace();
}
assertTrue("It throws an exception in test => see sysout", exceptionHolder.isEmpty());
log.info("testConcurrentAddRemoveEvent finish successful");
}
Aggregations