aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/java/com/google/testing/junit/runner/internal/junit4/CancellableRequestFactory.java
blob: 74ee5de8df9a0a41a262b49d70792b9f7cf2beb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2012 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.runner.internal.junit4;

import com.google.testing.junit.junit4.runner.MemoizingRequest;
import com.google.testing.junit.junit4.runner.RunNotifierWrapper;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.junit.runner.Description;
import org.junit.runner.Request;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;

/**
 * Creates requests that can be cancelled.
 */
@Singleton
public class CancellableRequestFactory {
  private boolean requestCreated;
  private volatile ThreadSafeRunNotifier currentNotifier;
  private volatile boolean cancelRequested = false;

  @Inject
  public CancellableRequestFactory() {}

  /**
   * Creates a request that can be cancelled. Can only be called once.
   *
   * @param delegate request to wrap
   */
  public Request createRequest(Request delegate) {
    if (requestCreated) {
      throw new IllegalStateException("a request was already created");
    }
    return new MemoizingRequest(delegate) {
      @Override
      protected Runner createRunner(Request delegate) {
        return new CancellableRunner(delegate.getRunner());
      }
    };
  }

  /**
   * Cancels the request created by this request factory.
   */
  public void cancelRun() {
    cancelRequested = true;
    RunNotifier notifier = currentNotifier;
    if (notifier != null) {
      notifier.pleaseStop();
    }
  }


  private class CancellableRunner extends Runner {
    private final Runner delegate;

    public CancellableRunner(Runner delegate) {
      this.delegate = delegate;
    }

    @Override
    public Description getDescription() {
      return delegate.getDescription();
    }

    @Override
    public void run(RunNotifier notifier) {
      currentNotifier = new ThreadSafeRunNotifier(notifier);
      if (cancelRequested) {
        currentNotifier.pleaseStop();
      }

      try {
        delegate.run(currentNotifier);
      } catch (StoppedByUserException e) {
        if (cancelRequested) {
          throw new RuntimeException("Test run interrupted", e);
        }
        throw e;
      }
    }
  }


  private static class ThreadSafeRunNotifier extends RunNotifierWrapper {
    private volatile boolean stopRequested;

    public ThreadSafeRunNotifier(RunNotifier delegate) {
      super(delegate);
    }

    /**
     * {@inheritDoc}<p>
     *
     * The implementation is almost an exact copy of the version in
     * {@code RunNotifier} but is thread-safe.
     */
    @Override
    public void fireTestStarted(Description description) throws StoppedByUserException {
      if (stopRequested) {
        throw new StoppedByUserException();
      }
      getDelegate().fireTestStarted(description);
    }

    /**
     * {@inheritDoc}<p>
     *
     * This method is thread-safe.
     */
    @Override
    public void pleaseStop() {
      stopRequested = true;
    }
  }
}