@Test(timeout = 1000)
public void testCancel() throws Exception {

    IsotopeFactory ifac = Isotopes.getInstance();
    IIsotope c = ifac.getMajorIsotope("C");
    IIsotope h = ifac.getMajorIsotope("H");
    IIsotope n = ifac.getMajorIsotope("N");
    IIsotope o = ifac.getMajorIsotope("O");
    IIsotope p = ifac.getMajorIsotope("P");
    IIsotope s = ifac.getMajorIsotope("S");
    MolecularFormulaRange mfRange = new MolecularFormulaRange();
    mfRange.addIsotope(c, 0, 1000);
    mfRange.addIsotope(h, 0, 1000);
    mfRange.addIsotope(o, 0, 1000);
    mfRange.addIsotope(n, 0, 1000);
    mfRange.addIsotope(p, 0, 1000);
    mfRange.addIsotope(s, 0, 1000);
    double minMass = 100000.0;
    double maxMass = 100000.001;
    final MolecularFormulaGenerator gen = new MolecularFormulaGenerator(
            builder, minMass, maxMass, mfRange);
    Runnable cancelThread = new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            gen.cancel();
        }
    };
    new Thread(cancelThread).run();
    // We will get stuck in the next method call until the cancel thread
    // calls the cancel() method
    gen.getAllFormulas();
    // Next getNextFormula() call should return null
    IMolecularFormula f = gen.getNextFormula();
    Assert.assertNull(f);
}