88
99package org .sosy_lab .java_smt .test ;
1010
11+ import static com .google .common .truth .Truth .assertThat ;
1112import static com .google .common .truth .TruthJUnit .assume ;
1213import static org .junit .Assert .assertThrows ;
1314
2425import org .sosy_lab .java_smt .SolverContextFactory .Solvers ;
2526import org .sosy_lab .java_smt .api .BasicProverEnvironment ;
2627import org .sosy_lab .java_smt .api .BooleanFormula ;
28+ import org .sosy_lab .java_smt .api .SolverException ;
2729import org .sosy_lab .java_smt .api .Tactic ;
2830import org .sosy_lab .java_smt .solvers .opensmt .Logics ;
2931
@@ -90,43 +92,97 @@ public void testTacticTimeout() {
9092 @ Test (timeout = TIMEOUT_MILLISECONDS )
9193 public void testProverTimeoutInt () throws InterruptedException {
9294 requireIntegers ();
93- testBasicProverTimeoutInt (() -> context .newProverEnvironment ());
95+ testBasicContextTimeoutInt (() -> context .newProverEnvironment ());
9496 }
9597
9698 @ Test (timeout = TIMEOUT_MILLISECONDS )
9799 public void testProverTimeoutBv () throws InterruptedException {
98100 requireBitvectors ();
101+ testBasicContextTimeoutBv (() -> context .newProverEnvironment ());
102+ }
103+
104+ // Test shutdown of prover specific shutdown manager. The context should still be usable!
105+ @ Test (timeout = TIMEOUT_MILLISECONDS )
106+ public void testProverInterruptWithSubsequentNewProverUsageBv ()
107+ throws InterruptedException , SolverException {
108+ requireBitvectors ();
109+ requireIsolatedProverShutdown ();
110+
99111 testBasicProverTimeoutBv (() -> context .newProverEnvironment ());
112+ assertThat (shutdownManager .getNotifier ().shouldShutdown ()).isFalse ();
113+
114+ HardBitvectorFormulaGenerator gen = new HardBitvectorFormulaGenerator (bvmgr , bmgr );
115+ try (BasicProverEnvironment <?> pe = context .newProverEnvironment ()) {
116+ pe .push (gen .generate (8 ));
117+ assertThat (pe .isUnsat ()).isTrue ();
118+ }
119+ }
120+
121+ // Test shutdown of prover specific shutdown manager. The context should still be usable!
122+ @ Test (timeout = TIMEOUT_MILLISECONDS )
123+ public void testProverInterruptWithSubsequentNewProverUsageInt ()
124+ throws InterruptedException , SolverException {
125+ requireIntegers ();
126+ requireIsolatedProverShutdown ();
127+
128+ testBasicProverTimeoutInt (() -> context .newProverEnvironment ());
129+ assertThat (shutdownManager .getNotifier ().shouldShutdown ()).isFalse ();
130+
131+ HardIntegerFormulaGenerator gen = new HardIntegerFormulaGenerator (imgr , bmgr );
132+ try (BasicProverEnvironment <?> pe = context .newProverEnvironment ()) {
133+ pe .push (gen .generate (8 ));
134+ assertThat (pe .isUnsat ()).isTrue ();
135+ }
100136 }
101137
102138 @ Test (timeout = TIMEOUT_MILLISECONDS )
103139 public void testInterpolationProverTimeout () throws InterruptedException {
104140 requireInterpolation ();
105141 requireIntegers ();
106- testBasicProverTimeoutInt (() -> context .newProverEnvironmentWithInterpolation ());
142+ testBasicContextTimeoutInt (() -> context .newProverEnvironmentWithInterpolation ());
107143 }
108144
109145 @ Test (timeout = TIMEOUT_MILLISECONDS )
110146 public void testOptimizationProverTimeout () throws InterruptedException {
111147 requireOptimization ();
112148 requireIntegers ();
113- testBasicProverTimeoutInt (() -> context .newOptimizationProverEnvironment ());
149+ testBasicContextTimeoutInt (() -> context .newOptimizationProverEnvironment ());
114150 }
115151
152+ /** Shuts down the shutdown manager of the context. */
153+ private void testBasicContextTimeoutInt (Supplier <BasicProverEnvironment <?>> proverConstructor )
154+ throws InterruptedException {
155+ HardIntegerFormulaGenerator gen = new HardIntegerFormulaGenerator (imgr , bmgr );
156+ testBasicContextBasedTimeout (proverConstructor , gen .generate (200 ));
157+ }
158+
159+ /** Shuts down the shutdown manager of the context. */
160+ private void testBasicContextTimeoutBv (Supplier <BasicProverEnvironment <?>> proverConstructor )
161+ throws InterruptedException {
162+ HardBitvectorFormulaGenerator gen = new HardBitvectorFormulaGenerator (bvmgr , bmgr );
163+ testBasicContextBasedTimeout (proverConstructor , gen .generate (200 ));
164+ }
165+
166+ /**
167+ * Shuts down the shutdown manager of the prover. Throws an exception for non-supporting solvers.
168+ */
116169 private void testBasicProverTimeoutInt (Supplier <BasicProverEnvironment <?>> proverConstructor )
117170 throws InterruptedException {
118171 HardIntegerFormulaGenerator gen = new HardIntegerFormulaGenerator (imgr , bmgr );
119- testBasicProverTimeout (proverConstructor , gen .generate (200 ));
172+ testBasicProverBasedTimeout (proverConstructor , gen .generate (200 ));
120173 }
121174
175+ /**
176+ * Shuts down the shutdown manager of the prover. Throws an exception for non-supporting solvers.
177+ */
122178 private void testBasicProverTimeoutBv (Supplier <BasicProverEnvironment <?>> proverConstructor )
123179 throws InterruptedException {
124180 HardBitvectorFormulaGenerator gen = new HardBitvectorFormulaGenerator (bvmgr , bmgr );
125- testBasicProverTimeout (proverConstructor , gen .generate (200 ));
181+ testBasicProverBasedTimeout (proverConstructor , gen .generate (200 ));
126182 }
127183
128184 @ SuppressWarnings ("CheckReturnValue" )
129- private void testBasicProverTimeout (
185+ private void testBasicContextBasedTimeout (
130186 Supplier <BasicProverEnvironment <?>> proverConstructor , BooleanFormula instance )
131187 throws InterruptedException {
132188 Thread t =
@@ -146,4 +202,25 @@ private void testBasicProverTimeout(
146202 assertThrows (InterruptedException .class , pe ::isUnsat );
147203 }
148204 }
205+
206+ private void testBasicProverBasedTimeout (
207+ Supplier <BasicProverEnvironment <?>> proverConstructor , BooleanFormula instance )
208+ throws InterruptedException {
209+
210+ try (BasicProverEnvironment <?> pe = proverConstructor .get ()) {
211+ Thread t =
212+ new Thread (
213+ () -> {
214+ try {
215+ Thread .sleep (delay );
216+ pe .getShutdownManagerForProver ().requestShutdown ("Shutdown Request" );
217+ } catch (InterruptedException exception ) {
218+ throw new UnsupportedOperationException ("Unexpected interrupt" , exception );
219+ }
220+ });
221+ pe .push (instance );
222+ t .start ();
223+ assertThrows (InterruptedException .class , pe ::isUnsat );
224+ }
225+ }
149226}
0 commit comments