# Testing code that is executed when solve reaches non-optimal status

**URL:** <https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293>\
**Category:** Optimization (Mathematical)\
**Created:** [September 25, 2020, 11:07pm UTC](https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293 "2020-09-25T23:07:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtjeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vtjeng/32/3787_2.png) [@vtjeng](https://discourse.julialang.org/u/vtjeng)\
**Post date:** [September 25, 2020, 11:07pm UTC](https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293/1 "2020-09-25T23:07:24Z")

</div>

I’m looking for advice on the best way to test code that is executed only when the termination status for optimization returns a status of [`MathOptInterface.TIME_LIMIT`](https://github.com/vtjeng/MIPVerify.jl/pull/67/files#diff-4ea67b7f9924bb92cbf40affe25624c4R95-R96).

The first approach I tried was to construct models that are difficult to deal with for the optimizer (but easy to understand for the human tester) and setting a short time limit (`seconds => 1` with `Cbc`) to try to cause `MathOptInterface.TIME_LIMIT` to be hit. I haven’t had much success as the solver heuristics seem quite good.

I’ve also considered mocking the function call to `JuMP.termination_status` via ([GitHub - invenia/Mocking.jl: Allows Julia function calls to be temporarily overloaded for purpose of testing](https://github.com/invenia/Mocking.jl) or [https://github.com/burrowsa/Fixtures.jl](https://github.com/burrowsa/Fixtures.jl)).

Is there any option I’m missing?

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [September 26, 2020, 1:42am UTC](https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293/2 "2020-09-26T01:42:53Z")

</div>

Do you need the solver to have found a feasible solution before hitting the time limit?  
If not, you can always set a time limit of 0s.

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [September 26, 2020, 8:58am UTC](https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293/3 "2020-09-26T08:58:04Z")

</div>

Another approach is if you structure your code so that those actions are performed by a separate function which you just feed the right input in the test.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [September 27, 2020, 10:51pm UTC](https://discourse.julialang.org/t/testing-code-that-is-executed-when-solve-reaches-non-optimal-status/47293/4 "2020-09-27T22:51:50Z")

</div>

Look at MockOptimizer in MOI and how it’s used in JuMP:  
[https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/mockoptimizer.jl](https://github.com/jump-dev/MathOptInterface.jl/blob/master/src/Utilities/mockoptimizer.jl)  
[https://github.com/jump-dev/JuMP.jl/blob/eaa22fa5d01d43046192dfd225c0335884974658/test/generate\_and\_solve.jl#L51](https://github.com/jump-dev/JuMP.jl/blob/eaa22fa5d01d43046192dfd225c0335884974658/test/generate_and_solve.jl#L51)
