@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "node\\(id\\) didn't achieve the status running; aborting after 0 seconds with final status: PENDING") public void testIllegalStateExceptionWhenNodeStillPending() { final NodeMetadata pendingNode = new NodeMetadataBuilder().ids("id").status(Status.PENDING).build(); Predicate<AtomicReference<NodeMetadata>> nodeRunning = new Predicate<AtomicReference<NodeMetadata>>() { @Override public boolean apply(AtomicReference<NodeMetadata> input) { assertEquals(input.get(), pendingNode); return false; } }; AtomicReference<NodeMetadata> atomicNode = Atomics.newReference(pendingNode); try { new PollNodeRunning(nodeRunning).apply(atomicNode); } finally { assertEquals(atomicNode.get(), pendingNode); } }
@Override public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node) throws IllegalStateException { String originalId = node.get().getId(); NodeMetadata originalNode = node.get(); try { Stopwatch stopwatch = Stopwatch.createStarted(); if (!nodeRunning.apply(node)) { long timeWaited = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (node.get() == null) { node.set(originalNode); throw new IllegalStateException(format("api response for node(%s) was null", originalId)); } else { throw new IllegalStateException(format( "node(%s) didn't achieve the status running; aborting after %d seconds with final status: %s", originalId, timeWaited / 1000, formatStatus(node.get()))); } } } catch (IllegalStateException e) { if (node.get().getStatus() == Status.TERMINATED) { throw new IllegalStateException(format("node(%s) terminated", originalId)); } else { throw propagate(e); } } return node; }
PollNodeRunning implements Function<AtomicReference<NodeMetadata>, AtomicReference<NodeMetadata>> { @Override public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node) throws IllegalStateException { String originalId = node.get().getId(); NodeMetadata originalNode = node.get(); try { Stopwatch stopwatch = Stopwatch.createStarted(); if (!nodeRunning.apply(node)) { long timeWaited = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (node.get() == null) { node.set(originalNode); throw new IllegalStateException(format("api response for node(%s) was null", originalId)); } else { throw new IllegalStateException(format( "node(%s) didn't achieve the status running; aborting after %d seconds with final status: %s", originalId, timeWaited / 1000, formatStatus(node.get()))); } } } catch (IllegalStateException e) { if (node.get().getStatus() == Status.TERMINATED) { throw new IllegalStateException(format("node(%s) terminated", originalId)); } else { throw propagate(e); } } return node; } }
PollNodeRunning implements Function<AtomicReference<NodeMetadata>, AtomicReference<NodeMetadata>> { @Override public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node) throws IllegalStateException { String originalId = node.get().getId(); NodeMetadata originalNode = node.get(); try { Stopwatch stopwatch = Stopwatch.createStarted(); if (!nodeRunning.apply(node)) { long timeWaited = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (node.get() == null) { node.set(originalNode); throw new IllegalStateException(format("api response for node(%s) was null", originalId)); } else { throw new IllegalStateException(format( "node(%s) didn't achieve the status running; aborting after %d seconds with final status: %s", originalId, timeWaited / 1000, formatStatus(node.get()))); } } } catch (IllegalStateException e) { if (node.get().getStatus() == Status.TERMINATED) { throw new IllegalStateException(format("node(%s) terminated", originalId)); } else { throw propagate(e); } } return node; } @Inject  PollNodeRunning(@Named(TIMEOUT_NODE_RUNNING) Predicate<AtomicReference<NodeMetadata>> nodeRunning); }
PollNodeRunning implements Function<AtomicReference<NodeMetadata>, AtomicReference<NodeMetadata>> { @Override public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node) throws IllegalStateException { String originalId = node.get().getId(); NodeMetadata originalNode = node.get(); try { Stopwatch stopwatch = Stopwatch.createStarted(); if (!nodeRunning.apply(node)) { long timeWaited = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (node.get() == null) { node.set(originalNode); throw new IllegalStateException(format("api response for node(%s) was null", originalId)); } else { throw new IllegalStateException(format( "node(%s) didn't achieve the status running; aborting after %d seconds with final status: %s", originalId, timeWaited / 1000, formatStatus(node.get()))); } } } catch (IllegalStateException e) { if (node.get().getStatus() == Status.TERMINATED) { throw new IllegalStateException(format("node(%s) terminated", originalId)); } else { throw propagate(e); } } return node; } @Inject  PollNodeRunning(@Named(TIMEOUT_NODE_RUNNING) Predicate<AtomicReference<NodeMetadata>> nodeRunning); @Override AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node); }
PollNodeRunning implements Function<AtomicReference<NodeMetadata>, AtomicReference<NodeMetadata>> { @Override public AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node) throws IllegalStateException { String originalId = node.get().getId(); NodeMetadata originalNode = node.get(); try { Stopwatch stopwatch = Stopwatch.createStarted(); if (!nodeRunning.apply(node)) { long timeWaited = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (node.get() == null) { node.set(originalNode); throw new IllegalStateException(format("api response for node(%s) was null", originalId)); } else { throw new IllegalStateException(format( "node(%s) didn't achieve the status running; aborting after %d seconds with final status: %s", originalId, timeWaited / 1000, formatStatus(node.get()))); } } } catch (IllegalStateException e) { if (node.get().getStatus() == Status.TERMINATED) { throw new IllegalStateException(format("node(%s) terminated", originalId)); } else { throw propagate(e); } } return node; } @Inject  PollNodeRunning(@Named(TIMEOUT_NODE_RUNNING) Predicate<AtomicReference<NodeMetadata>> nodeRunning); @Override AtomicReference<NodeMetadata> apply(AtomicReference<NodeMetadata> node);  }