assertNotNull("Did not found event with id=" + TEST_EVENT_ID_2, event2);
assertEquals("Wrong calendar-event subject",event2.getSubject(), TEST_EVENT_SUBJECT_2);
System.out.println("testConcurrentAddUpdateEvent thread2 addEvent2 DONE");
// enable breakpoint
CodepointClient codepointClient = null;
CodepointRef codepointRef = null;
try {
codepointClient = CodepointClientFactory.createCodepointClient("vm://localhost?broker.persistent=false", CODEPOINT_SERVER_ID);
codepointRef = codepointClient.getCodepoint("org.olat.commons.coordinate.cluster.ClusterSyncer.doInSync-in-sync.org.olat.commons.calendar.ICalFileCalendarManager.addEventTo");
codepointRef.enableBreakpoint();
System.out.println();
} catch (Exception e) {
e.printStackTrace();
fail("Could not initialzed CodepointClient");
}
// thread 1
new Thread(new Runnable() {
public void run() {
try {
// 1. load calendar
CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
// 2. add Event1 => breakpoint hit
System.out.println("testConcurrentAddUpdateEvent thread1 addEvent1");
calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
System.out.println("testConcurrentAddUpdateEvent thread1 addEvent1 DONE");
// 3. check event1 exist
cal = calManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
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 = calManager.getPersonalCalendar(test).getKalendar();
event1 = cal.getEvent(TEST_EVENT_ID_1);
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);
System.out.println("testConcurrentAddUpdateEvent thread1 finished");
} catch (Exception ex) {
exceptionHolder.add(ex);// no exception should happen
}
}}).start();
// thread 2
new Thread(new Runnable() {
public void run() {
try {
CalendarManager calManager = CalendarManagerFactory.getJUnitInstance().getCalendarManager();
Kalendar cal = calManager.getPersonalCalendar(test).getKalendar();
// 2. sleep 1sec
sleep(1000);
// 3. add Event2 (breakpoint of thread1 blocks)
System.out.println("testConcurrentAddUpdateEvent thread2 updateEvent2");
calManager.updateEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
System.out.println("testConcurrentAddUpdateEvent thread1 updateEvent2 DONE");
// 4. check event2 exist
cal = calManager.getPersonalCalendar(test).getKalendar();
KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID_2);
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
cal = calManager.getPersonalCalendar(test).getKalendar();
KalendarEvent event1 = cal.getEvent(TEST_EVENT_ID_1);
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 = calManager.removeEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2_UPDATED, new Date(), 1));
assertTrue(removed);
statusList.add(Boolean.TRUE);
System.out.println("testConcurrentAddUpdateEvent thread2 finished");
} catch (Exception ex) {
exceptionHolder.add(ex);// no exception should happen
}
}}).start();
sleep(2000);
try {
// to see all registered code-points: comment-in next 2 lines
// List<CodepointRef> codepointList = codepointClient.listAllCodepoints();
// System.out.println("codepointList=" + codepointList);
System.out.println("testConcurrentAddUpdateEvent start waiting for breakpoint reached");
TemporaryPausedThread[] threads = codepointRef.waitForBreakpointReached(1000);
assertTrue("Did not reach breakpoint", threads.length > 0);
System.out.println("threads[0].getCodepointRef()=" + threads[0].getCodepointRef());
codepointRef.disableBreakpoint(true);
System.out.println("testConcurrentAddUpdateEvent breakpoint reached => continue");
} catch (BreakpointStateException e) {
e.printStackTrace();
fail("Codepoints: BreakpointStateException=" + e.getMessage());
} catch (CommunicationException e) {
e.printStackTrace();
fail("Codepoints: CommunicationException=" + e.getMessage());
}
// sleep until t1 and t2 should have terminated/excepted
int loopCount = 0;
while ( (statusList.size()<2) && (exceptionHolder.size()<1) && (loopCount<5)) {
sleep(1000);
loopCount++;
}
assertTrue("Threads did not finish in 5sec", loopCount<5);
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
System.out.println("exception: "+exception.getMessage());
exception.printStackTrace();
}
if (exceptionHolder.size() > 0) {
assertTrue("It throws an exception in test => see sysout exception[0]=" + exceptionHolder.get(0).getMessage(), exceptionHolder.size() == 0);
}
codepointClient.close();
System.out.println("testConcurrentAddUpdateEvent finish successful");
}