New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to be executed in a user-defined thread #20

Closed
debruyckere opened this Issue Nov 30, 2015 · 5 comments

Comments

Projects
None yet
4 participants
@debruyckere

debruyckere commented Nov 30, 2015

Overview

In JUnit 4, we use a specific test runner that runs the test code on a dedicated UI thread, such as the AWT/Swing thread (or the JavaFX thread). This avoids cluttering your code with EventQueue.invokeAndWait all over the place. As far as I understood, the current extensions proposal does not allow to do such thing.

The below runner simply wraps all calls to before, after, and test methods in an EventQueue.invokeAndWait call. Tests simply have to declare @RunWith(AWTThreadRunner.class) to get this behavior.

    public class AWTThreadRunner extends BlockJUnit4ClassRunner {
      public AWTThreadRunner(Class<?> klass) throws InitializationError {
        super(klass);
      }

      @Override
      protected Statement classBlock(RunNotifier notifier) {
        return onAWTThreadStatement(super.classBlock(notifier));
      }

      @Override
      protected Statement methodBlock(FrameworkMethod method) {
        return onAWTThreadStatement(super.methodBlock(method));
      }

      private static Statement onAWTThreadStatement(final Statement aStatement) {
        return new Statement() {
          @Override
          public void evaluate() throws Throwable {
            EventQueue.invokeAndWait(new Runnable() {
              public void run() {
                aStatement.evaluate();
                // left out exception handling code improve readability
              }
            });
          }
        };
      }
    }

Note: the actual implementation is a bit more complex in that it does proper exception handling and refuses JUnit's time-outs as that doesn't work together with running on a dedicated thread.

Related Issues

@sbrannen sbrannen changed the title from Allow to run tests on a dedicated (UI) thread to Allow tests to be executed in a user-defined thread Nov 30, 2015

@sbrannen

This comment has been minimized.

Member

sbrannen commented Nov 30, 2015

Hi @debruyckere,

Thanks for raising this issue.

We are already considering providing a mechanism for test extensions to influence the way tests are physically executed.

Once we begin work on this front, we'll post back here.

Regards,

Sam

@marcphilipp marcphilipp added this to the 5.0 M2 milestone Apr 29, 2016

@sbrannen sbrannen modified the milestones: 5.0 M2, 5.0 M3, 5.0 Backlog, 5.0 M4 Jul 15, 2016

@sbrannen sbrannen modified the milestones: 5.0 M4, 5.0 M5 Jul 25, 2016

@marcphilipp marcphilipp modified the milestones: 5.0 M5, 5.0 M6 May 6, 2017

@akozlova

This comment has been minimized.

akozlova commented Jun 30, 2017

Looks like relates to #157, I tried to have a custom engine for this issue.

This engine delegates to jupiter like:

public class EDTAwareEngine implements TestEngine {
  private JupiterTestEngine myEngine = new JupiterTestEngine();

  @Override
  public String getId() {
    return "junit-jupiter-edt";
  }

  @Override
  public TestDescriptor discover(EngineDiscoveryRequest request, UniqueId id) {
    return myEngine.discover(request, id);
  }

  @Override
  public void execute(ExecutionRequest request) {
    try {
      SwingUtilities.invokeAndWait(() -> myEngine.execute(request));
    }
    catch (Throwable e) {
      e.printStackTrace();
    }
  }
}

works well by itself but as discovery mechanism is the same, all tests are discovered twice. This problem could be solved with EngineFilters but unfortunately engine filters are not provided as extensions: one can register them on launcher request or for JUnitPlatform runner. Even if IDE would allow to take this parameter from some configuration, it won't work good (cause this configuration can't be retrieved from the code). Frankly saying I don't want to invent my own annotations with all discovery and execution staff. Any ideas how to deal with this or should we just wait for this issue to be resolved?

@sbrannen

This comment has been minimized.

Member

sbrannen commented Jun 30, 2017

Any ideas how to deal with this or should we just wait for this issue to be resolved?

I think it's best to wait for this issue to be resolved.

@marcphilipp marcphilipp modified the milestones: 5.0 M6, 5.0 RC1 Jul 18, 2017

@marcphilipp marcphilipp modified the milestones: 5.0 RC1, 5.1 Backlog Jul 30, 2017

@marcphilipp marcphilipp modified the milestones: 5.x Backlog, 5.1 M1, 5.1 Backlog Sep 22, 2017

@marcphilipp marcphilipp modified the milestones: 5.1 Backlog, 5.1 M2 Nov 17, 2017

@marcphilipp marcphilipp modified the milestones: 5.1 M2, 5.1 Backlog Jan 8, 2018

@sbrannen

This comment has been minimized.

Member

sbrannen commented Jun 3, 2018

Note: this is very closely related to #157 and potentially a duplicate.

@sbrannen

This comment has been minimized.

Member

sbrannen commented Jun 3, 2018

Closing this issue as superseded by #157.

Please continue the discussion on this topic there.

@sbrannen sbrannen closed this Jun 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment