|
| 1 | +package io.quarkus.dev.testing; |
| 2 | + |
| 3 | +import java.util.concurrent.CopyOnWriteArraySet; |
| 4 | +import java.util.function.Consumer; |
| 5 | + |
| 6 | +public class ContinuousTestingSharedStateManager { |
| 7 | + |
| 8 | + private static final CopyOnWriteArraySet<Consumer<State>> stateListeners = new CopyOnWriteArraySet<>(); |
| 9 | + public static final State INITIAL_STATE = new State(-1, false, false, 0, 0, 0, 0, 0, 0, 0, false, false, false, true); |
| 10 | + private static volatile State lastState = INITIAL_STATE; |
| 11 | + |
| 12 | + public static void addStateListener(Consumer<State> stateListener) { |
| 13 | + stateListeners.add(stateListener); |
| 14 | + if (lastState != null) { |
| 15 | + stateListener.accept(lastState); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public static void removeStateListener(Consumer<State> stateListener) { |
| 20 | + stateListeners.remove(stateListener); |
| 21 | + } |
| 22 | + |
| 23 | + public static void reset() { |
| 24 | + setLastState(INITIAL_STATE); |
| 25 | + } |
| 26 | + |
| 27 | + public static void setLastState(State state) { |
| 28 | + lastState = state; |
| 29 | + for (var sl : stateListeners) { |
| 30 | + sl.accept(state); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static State getLastState() { |
| 35 | + return lastState; |
| 36 | + } |
| 37 | + |
| 38 | + public static void setInProgress(boolean inProgress) { |
| 39 | + State state = lastState; |
| 40 | + if (state != null) { |
| 41 | + setLastState( |
| 42 | + new State(state.lastRun, state.running, inProgress, state.run, state.passed, state.failed, state.skipped, |
| 43 | + state.currentPassed, state.currentFailed, state.currentSkipped, state.isBrokenOnly, |
| 44 | + state.isTestOutput, state.isInstrumentationBasedReload, state.isLiveReload)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static void setRunning(boolean running) { |
| 49 | + State state = lastState; |
| 50 | + if (state != null) { |
| 51 | + setLastState(new State(state.lastRun, running, running && state.inProgress, state.run, state.passed, state.failed, |
| 52 | + state.skipped, |
| 53 | + state.currentPassed, state.currentFailed, state.currentSkipped, state.isBrokenOnly, state.isTestOutput, |
| 54 | + state.isInstrumentationBasedReload, state.isLiveReload)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static void setBrokenOnly(boolean brokenOnly) { |
| 59 | + State state = lastState; |
| 60 | + if (state != null) { |
| 61 | + setLastState(new State(state.lastRun, state.running, state.inProgress, state.run, state.passed, state.failed, |
| 62 | + state.skipped, |
| 63 | + state.currentPassed, state.currentFailed, state.currentSkipped, brokenOnly, state.isTestOutput, |
| 64 | + state.isInstrumentationBasedReload, state.isLiveReload)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static void setTestOutput(boolean testOutput) { |
| 69 | + State state = lastState; |
| 70 | + if (state != null) { |
| 71 | + setLastState(new State(state.lastRun, state.running, state.inProgress, state.run, state.passed, state.failed, |
| 72 | + state.skipped, |
| 73 | + state.currentPassed, state.currentFailed, state.currentSkipped, state.isBrokenOnly, testOutput, |
| 74 | + state.isInstrumentationBasedReload, state.isLiveReload)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static void setInstrumentationBasedReload(boolean instrumentationBasedReload) { |
| 79 | + State state = lastState; |
| 80 | + if (state != null) { |
| 81 | + setLastState(new State(state.lastRun, state.running, state.inProgress, state.run, state.passed, state.failed, |
| 82 | + state.skipped, |
| 83 | + state.currentPassed, state.currentFailed, state.currentSkipped, state.isBrokenOnly, state.isTestOutput, |
| 84 | + instrumentationBasedReload, state.isLiveReload)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static void setLiveReloadEnabled(boolean liveReload) { |
| 89 | + State state = lastState; |
| 90 | + if (state != null) { |
| 91 | + setLastState(new State(state.lastRun, state.running, state.inProgress, state.run, state.passed, state.failed, |
| 92 | + state.skipped, |
| 93 | + state.currentPassed, state.currentFailed, state.currentSkipped, state.isBrokenOnly, state.isTestOutput, |
| 94 | + state.isInstrumentationBasedReload, liveReload)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static class State { |
| 99 | + public final long lastRun; |
| 100 | + public final boolean running; |
| 101 | + public final boolean inProgress; |
| 102 | + public final long run; |
| 103 | + public final long passed; |
| 104 | + public final long failed; |
| 105 | + public final long skipped; |
| 106 | + public final long currentPassed; |
| 107 | + public final long currentFailed; |
| 108 | + public final long currentSkipped; |
| 109 | + public final boolean isBrokenOnly; |
| 110 | + public final boolean isTestOutput; |
| 111 | + public final boolean isInstrumentationBasedReload; |
| 112 | + public final boolean isLiveReload; |
| 113 | + |
| 114 | + public State(long lastRun, boolean running, boolean inProgress, long run, long passed, long failed, long skipped, |
| 115 | + long currentPassed, long currentFailed, long currentSkipped, boolean isBrokenOnly, boolean isTestOutput, |
| 116 | + boolean isInstrumentationBasedReload, boolean isLiveReload) { |
| 117 | + this.lastRun = lastRun; |
| 118 | + this.running = running; |
| 119 | + this.inProgress = inProgress; |
| 120 | + this.run = run; |
| 121 | + this.passed = passed; |
| 122 | + this.failed = failed; |
| 123 | + this.skipped = skipped; |
| 124 | + this.currentPassed = currentPassed; |
| 125 | + this.currentFailed = currentFailed; |
| 126 | + this.currentSkipped = currentSkipped; |
| 127 | + this.isBrokenOnly = isBrokenOnly; |
| 128 | + this.isTestOutput = isTestOutput; |
| 129 | + this.isInstrumentationBasedReload = isInstrumentationBasedReload; |
| 130 | + this.isLiveReload = isLiveReload; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String toString() { |
| 135 | + return "State{" + |
| 136 | + "lastRun=" + lastRun + |
| 137 | + ", running=" + running + |
| 138 | + ", inProgress=" + inProgress + |
| 139 | + ", run=" + run + |
| 140 | + ", passed=" + passed + |
| 141 | + ", failed=" + failed + |
| 142 | + ", skipped=" + skipped + |
| 143 | + ", isBrokenOnly=" + isBrokenOnly + |
| 144 | + ", isTestOutput=" + isTestOutput + |
| 145 | + ", isInstrumentationBasedReload=" + isInstrumentationBasedReload + |
| 146 | + ", isLiveReload=" + isLiveReload + |
| 147 | + '}'; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments