Examples of CodepointClient


Examples of org.olat.testutils.codepoints.client.CodepointClient

    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));

    // 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("testConcurrentAddEvent thread1 addEvent1");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
          System.out.println("testConcurrentAddEvent 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("testConcurrentAddEvent thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          // 1. load calendar
          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("testConcurrentAddEvent thread2 addEvent2");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
          System.out.println("testConcurrentAddEvent thread1 addEvent2 DONE");
          // 4. check event2 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent event2 = cal.getEvent(TEST_EVENT_ID_2);
          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 = 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);
          statusList.add(Boolean.TRUE);
          System.out.println("testConcurrentAddEvent 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("testConcurrentAddEvent 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("testConcurrentAddEvent breakpoint reached => continue");
    } catch (BreakpointStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Codepoints: BreakpointStateException=" + e.getMessage());
    } catch (CommunicationException e) {
      // TODO Auto-generated catch block
      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("testConcurrentAddEvent finish successful");
  }
View Full Code Here

Examples of org.olat.testutils.codepoints.client.CodepointClient

    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");
  }
View Full Code Here

Examples of org.olat.testutils.codepoints.client.CodepointClient

    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("testConcurrentAddRemoveEvent 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 CoepointClient");
    }

    // 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("testConcurrentAddRemoveEvent thread1 addEvent1");
          calManager.addEventTo(cal, new KalendarEvent(TEST_EVENT_ID_1,TEST_EVENT_SUBJECT_1, new Date(), 1));
          System.out.println("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent thread2 removeEvent2");
          boolean removed = calManager.removeEventFrom(cal, new KalendarEvent(TEST_EVENT_ID_2,TEST_EVENT_SUBJECT_2, new Date(), 1));
          assertTrue(removed);
          System.out.println("testConcurrentAddRemoveEvent thread1 removeEvent2 DONE");
          // 4. check event2 exist
          cal = calManager.getPersonalCalendar(test).getKalendar();
          KalendarEvent updatedEvent = cal.getEvent(TEST_EVENT_ID_2);
          assertNull("Still found deleted event with id=" + TEST_EVENT_ID_2, updatedEvent);
          // 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);
          statusList.add(Boolean.TRUE);
          System.out.println("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent 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("testConcurrentAddRemoveEvent finish successful");
  }
View Full Code Here

Examples of org.olat.testutils.codepoints.client.CodepointClient

    final int access = 4;
    DBFactory.getInstance().closeSession();
    assertEquals("Launch counter was not 0", 0, repositoryEntry.getLaunchCounter() );

    // 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.repository.async.IncrementDownloadCounterBackgroundTask.executeTask-before-update");
      codepointRef.enableBreakpoint();
    } catch (Exception e) {
      e.printStackTrace();
      fail("Could not initialzed CodepointClient");
    }
    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          Thread.currentThread().sleep(100);
          System.out.println("Thread 1 starts with lookup");
          RepositoryEntry repositoryEntryT1 = RepositoryManager.getInstance().lookupRepositoryEntry(keyRepo);
          System.out.println("Thread 1 starts does inc downloadcounter");
          RepositoryManager.getInstance().incrementDownloadCounter(repositoryEntryT1);
          System.out.println("testConcurrentIncrementLaunchCounterWithCodePoints: Thread1 incremented download-counter");
        } catch (Exception ex) {
          ex.printStackTrace(System.out);
          exceptionHolder.add(ex);// no exception should happen
        } finally {
          System.out.println("Thread 1 quits");
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          Thread.currentThread().sleep(300);
          System.out.println("Thread 2 starts with lookup");
          RepositoryEntry repositoryEntryT2 = RepositoryManager.getInstance().lookupRepositoryEntry(keyRepo);
          System.out.println("Thread 2 starts does inc downloadcounter");
          RepositoryManager.getInstance().incrementDownloadCounter(repositoryEntryT2);
          System.out.println("testConcurrentIncrementLaunchCounterWithCodePoints: Thread2 incremented download-counter");
        } catch (Exception ex) {
          ex.printStackTrace(System.out);
          exceptionHolder.add(ex);// no exception should happen
        } finally {
          System.out.println("Thread 2 quits");
        }
      }}).start();

    // thread 3
    new Thread(new Runnable() {
      public void run() {
        try {
          Thread.currentThread().sleep(200);
          System.out.println("Thread 3 starts with lookup");
          RepositoryEntry repositoryEntryT3 = RepositoryManager.getInstance().lookupRepositoryEntry(keyRepo);
          // change repository directly and not via RepositoryManager.setAccess(...) for testing
          System.out.println("Thread 3 changes repository entry directly, setAccess...");
          repositoryEntryT3.setAccess(access);
          System.out.println("Thread 3 changes repository entry directly, updateRepositoryEntry");
          RepositoryManager.getInstance().updateRepositoryEntry(repositoryEntryT3);
          System.out.println("Thread 3 changes repository entry directly, commit");
          DBFactory.getInstance().closeSession();
          System.out.println("testConcurrentIncrementLaunchCounterWithCodePoints: Thread3 setAccess DONE");
        } catch (Exception ex) {
          ex.printStackTrace(System.out);
          exceptionHolder.add(ex);// no exception should happen
        } finally {
          System.out.println("Thread 3 quits");
        }
      }}).start();

    try {
      System.out.println("testConcurrentIncrementLaunchCounterWithCodePoints: main thread sleep 500msec");
          Thread.currentThread().sleep(500);
        } catch (InterruptedException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
    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("testConcurrentIncrementLaunchCounterWithCodePoints 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("testConcurrentIncrementLaunchCounterWithCodePoints breakpoint reached => continue");
    } catch (BreakpointStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Codepoints: BreakpointStateException=" + e.getMessage());
    } catch (CommunicationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Codepoints: CommunicationException=" + e.getMessage());
    }
    sleep(500);
    RepositoryEntry repositoryEntry2 = RepositoryManager.getInstance().lookupRepositoryEntry(keyRepo);
    assertEquals("Wrong value of incrementLaunch counter",2,repositoryEntry2.getDownloadCounter());
    assertEquals("Wrong access value",access,repositoryEntry2.getAccess());
    assertEquals("No exception should occur: "+exceptionHolder.size(), 0, exceptionHolder.size());
   
    codepointClient.close();
    System.out.println("testConcurrentIncrementLaunchCounterWithCodePoints finish successful");   
  }
View Full Code Here

Examples of org.olat.testutils.codepoints.client.CodepointClient

    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<Boolean> statusList = Collections.synchronizedList(new ArrayList<Boolean>(1));

    // 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.notifications.NotificationsManagerImpl.findOrCreatePublisher");
      codepointRef.enableBreakpoint();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Could not initialzed CodepointClient");
    }
   
    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          NotificationsManager.getInstance().subscribe(identity, sc, pd);
          DBFactory.getInstance().closeSession();
          statusList.add(Boolean.TRUE);
          System.out.println("testConcurrentFindOrCreatePublisher thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          sleep(1000);
          NotificationsManager.getInstance().subscribe(identity2, sc, pd);
          DBFactory.getInstance().closeSession();
          statusList.add(Boolean.TRUE);
          System.out.println("testConcurrentFindOrCreatePublisher thread2 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();

    sleep(2000);
    // check thread 2 should not finished
    assertEquals("Thread already finished => synchronization did not work",0,statusList.size());
    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("testConcurrentFindOrCreatePublisher 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("testConcurrentFindOrCreatePublisher 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)
    }
    assertEquals("Thread(s) did not finish",2, statusList.size());
    assertTrue("Subscriber does not exists for identity=" + identity,  NotificationsManager.getInstance().isSubscribed(identity, sc));
    assertTrue("Subscriber does not exists for identity=" + identity2, NotificationsManager.getInstance().isSubscribed(identity2, sc));
    codepointClient.close();
    System.out.println("testConcurrentFindOrCreatePublisher finish successful");
   
  }
View Full Code Here

Examples of org.olat.testutils.codepoints.client.CodepointClient

    final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
    final List<OLATResource> statusList = Collections.synchronizedList(new ArrayList<OLATResource>(1));

    // 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.resource.OLATResourceManager.findOrPersistResourceable");
      codepointRef.enableBreakpoint();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      fail("Could not initialzed CodepointClient");
    }
   
    // thread 1
    new Thread(new Runnable() {
      public void run() {
        try {
          OLATResource ores = OLATResourceManager.getInstance().findOrPersistResourceable(new TestResourceable());
          assertNotNull(ores);
          statusList.add(ores);
          System.out.println("testConcurrentFindOrPersistResourceable thread1 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();
   
    // thread 2
    new Thread(new Runnable() {
      public void run() {
        try {
          sleep(1000);
          OLATResource ores = OLATResourceManager.getInstance().findOrPersistResourceable(new TestResourceable());
          assertNotNull(ores);
          statusList.add(ores);
          System.out.println("testConcurrentFindOrPersistResourceable thread2 finished");
        } catch (Exception ex) {
          exceptionHolder.add(ex);// no exception should happen
        }
      }}).start();

    sleep(2000);
    // check thread 2 should not finished
    assertEquals("Thread already finished => synchronization did not work",0,statusList.size());
    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("testConcurrentFindOrPersistResourceable 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("testConcurrentFindOrPersistResourceable 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)
    }
    assertEquals("Missing created OresResource in statusList",2, statusList.size());
    assertEquals("Created OresResource has not same key",statusList.get(0).getKey(), statusList.get(1).getKey());
    codepointClient.close();
    System.out.println("testConcurrentFindOrPersistResourceable finish successful");
   
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.