@Test
public void testSaturateTabledGoals() throws Exception {

	final int MAX = 1024;
	// Set the cache size very small just for this test
	System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", "" + MAX);
	try {
		Graph data = Factory.createGraphMem();
		data.add(new Triple(a, ty, C1));
		List<Rule> rules = Rule
				.parseRules("[r1:  (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"
						+ "[r2:  (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]");
		FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules)
				.bind(data);
		LPBRuleEngine engine = getEngineForGraph(infgraph);
		assertEquals(0, engine.activeInterpreters.size());
		assertEquals(0, engine.tabledGoals.size());
		// JENA-901
		// Let's ask about lots of unknown subjects
		for (int i = 0; i < MAX * 128; i++) {
			Node test = NodeFactory.createURI("test" + i);
			ExtendedIterator<Triple> it = infgraph.find(test, ty, C2);
			assertFalse(it.hasNext());
			it.close();
		}
		// Let's see how many were cached - should be MAX or less (less if a GC freed weak values in the cache). 
		assertTrue(engine.tabledGoals.size() <= MAX);
		// and no leaks of activeInterpreters (this will happen if we forget
		// to call hasNext above)
		assertEquals(0, engine.activeInterpreters.size());
	} finally {
		System.clearProperty("jena.rulesys.lp.max_cached_tabled_goals");
	}
}