Source:
<code>public ScriptPair call() throws Exception { try { if (abortScheduled) { return new ScriptPair(); } controller.connect(); PrepareCommand prepare = new PrepareCommand(); prepare.setNames(names); prepare.setOverriddenScriptProperties(overridenScriptProperties); controller.writeCommand(prepare); boolean abortWritten = false; String expectedScript = null; String observedScript = null; boolean finishedReceived = false; while (true) { try { CommandEvent event = controller.readEvent(200, MILLISECONDS); switch (event.getKind()) { case PREPARED: PreparedEvent prepared = (PreparedEvent) event; expectedScript = prepared.getScript(); for (String barrier : prepared.getBarriers()) { barriers.put(barrier, new CountDownLatch(1)); } latch.notifyPrepared(); latch.awaitStartable(); if (abortScheduled && !abortWritten) { sendAbortCommand(); abortWritten = true; } else { StartCommand start = new StartCommand(); controller.writeCommand(start); } break; case STARTED: break; case NOTIFIED: NotifiedEvent notifiedEvent = (NotifiedEvent) event; String barrier = notifiedEvent.getBarrier(); CountDownLatch notifiedLatch = barriers.get(barrier); notifiedLatch.countDown(); break; case ERROR: ErrorEvent error = (ErrorEvent) event; throw new SpecificationException(format("%s:%s", error.getSummary(), error.getDescription())); case FINISHED: FinishedEvent finished = (FinishedEvent) event; observedScript = finished.getScript(); finishedReceived = true; break; default: throw new IllegalArgumentException("Unrecognized event kind: " + event.getKind()); } } catch (SocketTimeoutException e) { <START> if (finishedReceived) <END> return new ScriptPair(expectedScript, observedScript); if (abortScheduled && !abortWritten) { sendAbortCommand(); abortWritten = true; } if (Thread.interrupted()) { throw new InterruptedException(); } } } } catch (ConnectException e) { Exception exception = new Exception("Failed to connect. Is K3PO ready?", e); exception.fillInStackTrace(); latch.notifyException(exception); throw e; } catch (Exception e) { latch.notifyException(e); throw e; } finally { latch.notifyFinished(); } }</code><technical_language>this require cases get a timeout exception? properly track state changes instead</technical_language>

Target:
public ScriptPair call() throws Exception { try { if (abortScheduled) { return new ScriptPair(); } controller.connect(); PrepareCommand prepare = new PrepareCommand(); prepare.setNames(names); prepare.setOverriddenScriptProperties(overridenScriptProperties); controller.writeCommand(prepare); boolean abortWritten = false; String expectedScript = null; while (true) { try { CommandEvent event = controller.readEvent(200, MILLISECONDS); switch (event.getKind()) { case PREPARED: PreparedEvent prepared = (PreparedEvent) event; expectedScript = prepared.getScript(); for (String barrier : prepared.getBarriers()) { barriers.put(barrier, new CountDownLatch(1)); } latch.notifyPrepared(); latch.awaitStartable(); if (abortScheduled && !abortWritten) { sendAbortCommand(); abortWritten = true; } else { StartCommand start = new StartCommand(); controller.writeCommand(start); } break; case STARTED: break; case NOTIFIED: NotifiedEvent notifiedEvent = (NotifiedEvent) event; String barrier = notifiedEvent.getBarrier(); CountDownLatch notifiedLatch = barriers.get(barrier); notifiedLatch.countDown(); break; case ERROR: ErrorEvent error = (ErrorEvent) event; throw new SpecificationException(format("%s:%s", error.getSummary(), error.getDescription())); case FINISHED: FinishedEvent finished = (FinishedEvent) event; notifyBarriers(finished); String observedScript = finished.getScript(); return new ScriptPair(expectedScript, observedScript); default: throw new IllegalArgumentException("Unrecognized event kind: " + event.getKind()); } } catch (SocketTimeoutException e) { if (abortScheduled && !abortWritten) { sendAbortCommand(); abortWritten = true; } if (Thread.interrupted()) { throw new InterruptedException(); } } } } catch (ConnectException e) { Exception exception = new Exception("Failed to connect. Is K3PO ready?", e); exception.fillInStackTrace(); latch.notifyException(exception); throw e; } catch (Exception e) { latch.notifyException(e); throw e; } finally { latch.notifyFinished(); } }