Skip to main content



      Home
Home » Eclipse Projects » Equinox » Synchonizing with EventAdmin
Synchonizing with EventAdmin [message #1764819] Fri, 02 June 2017 08:33 Go to next message
Eclipse UserFriend
Hi,

I am currently trying to write a JUnit test for existing code which (description very simplified ;-)) listens to resource changes in the workspace and issues events using EventAdmin.postEvent().

In the test case I want to perform a change in the workspace and then check the state after all asynchronous events have been delivered. So, I need some (possibly hacky?) way to wait until the EventAdmin implementation (I can safely assume that we only use the Equinox implementation) has delivered all events.

Any ideas on how to achieve this? (other than just waiting for 10s and keep the fingers crossed that all events are delivered by then ;-))

Thanks in advance for any help!
Stefan



Re: Synchonizing with EventAdmin [message #1764823 is a reply to message #1764819] Fri, 02 June 2017 09:00 Go to previous message
Eclipse UserFriend
Ok, this is what I have achieved so far.
  public static void waitAllEventsProcessed()
  {
    while (isEventThreadBusy())
    {
      readAndDispatchEvents();
    }
  }

  private static boolean isEventThreadBusy()
  {
    Thread thread = Thread.getAllStackTraces().keySet().stream()
        .filter(t -> t.getName().equals("EventAdmin Async Event Dispatcher Thread"))
        .findAny()
        .orElseThrow(() -> new IllegalStateException("Event Thread not found"));

    // if thread is not waiting, it is busy
    if (thread.getState() != State.WAITING)
    {
      return true;
    }

    // if thread's queue is not empty, it is busy
    Field headField;
    try
    {
      headField = thread.getClass().getDeclaredField("head");
      headField.setAccessible(true);
      if (headField.get(thread) != null)
      {
        return true;
      }
    }
    catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e)
    {
      // fall through and ignore head field...
    }

    // if thread is not waiting for getNextEvent(), it is busy
    StackTraceElement[] stack = thread.getStackTrace();
    if (stack.length > 2)
    {
      StackTraceElement getNextEventExpected = stack[2];
      if (!getNextEventExpected.getMethodName().equals("getNextEvent"))
      {
        return true;
      }
    }

    // thread is really unoccupied
    return false;
  }


It seems to work, but it's not nice :-(

Any better ideas?

Stefan
Previous Topic:Can't find org.eclipse.equinox.security.win32.x86 plugin
Next Topic:p2 mechanism cannot work as expected
Goto Forum:
  


Current Time: Sat Apr 19 08:45:55 EDT 2025

Powered by FUDForum. Page generated in 0.02897 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top