void () { //check for 'allKeys' only, because all them have same superclass final Deque<LookupAllKeysTrace> tracesStack = new ArrayDeque<>(); try { for (int i = 0; i < MAX_LOOKUP_DEPTH; i++) { final LookupAllKeysTrace trace = lookupAllKeysStarted(INDEX_ID); if (!tracesStack.isEmpty()) { assertNotSame( "Each lookupStarted() call should return unique Trace instance", tracesStack.peek(), trace ); } tracesStack.push(trace); } assertEquals( "All traces up to MAX_DEPTH must be different", new HashSet<>(tracesStack).size(), MAX_LOOKUP_DEPTH ); } finally {//unwind stack back to not affect other tests: for (int i = 0; i < MAX_LOOKUP_DEPTH; i++) { TRACE_OF_ALL_KEYS_LOOKUP.get().lookupFinished(); } } }