public void testNewReference_withInitialValue() throws Exception { assertEquals(null, Atomics.newReference(null).get()); assertEquals(OBJECT, Atomics.newReference(OBJECT).get()); }
public void testNewReferenceArray_withNegativeLength() throws Exception { try { Atomics.newReferenceArray(-1); fail(); } catch (NegativeArraySizeException expected) { } }
@Test public void testRefreshUpdatesAtomicReferenceOnRecheckPending() { NodeMetadata pending = new NodeMetadataBuilder().id("myid").status(Status.PENDING).build(); GetNodeMetadataStrategy computeService = createMock(GetNodeMetadataStrategy.class); expect(computeService.getNode("myid")).andReturn(pending); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(pending); Assert.assertFalse(nodeRunning.apply(reference)); Assert.assertEquals(reference.get(), pending); verify(computeService); }
@Test public void testNodeRunningReturnsTrueWhenRunning() { expect(node.getStatus()).andReturn(Status.RUNNING).atLeastOnce(); expect(node.getBackendStatus()).andReturn(null).atLeastOnce(); replay(node); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(node); Assert.assertTrue(nodeRunning.apply(reference)); Assert.assertEquals(reference.get(), node); }
/** * * @param id * @return node that was deleted or null if it wasn't found */ @Nullable protected NodeMetadata doDestroyNode(final String id) { checkNotNull(id, "id"); logger.debug(">> destroying node(%s)", id); NodeMetadata nodeMetadata = destroyNodeStrategy.destroyNode(id); if (nodeMetadata == null) return null; final AtomicReference<NodeMetadata> node = Atomics.newReference(nodeMetadata); boolean successful = node.get() == null || nodeTerminated.apply(node); if (successful) credentialStore.remove("node#" + id); logger.debug("<< destroyed node(%s) success(%s)", id, successful); return nodeMetadata; }
@Test public void testNormal() { AtomicReference<AuthorizationException> authException = Atomics.newReference(); assertEquals( new SetAndThrowAuthorizationExceptionSupplier<String>(Suppliers.ofInstance("foo"), authException).get(), "foo"); assertEquals(authException.get(), null); }
ConnectionSupplier conns, TableReference tableRef, Function<DbWriteTable, T> runner) { log.debug("Running in a new thread to turn autocommit on for write"); AtomicReference<T> result = Atomics.newReference(); Thread writeThread = new Thread(() -> { SqlConnection freshConn = conns.getFresh(); try { result.set(runner.apply(dbTables.createWrite(tableRef, new ConnectionSupplier(Suppliers.ofInstance(freshConn))))); } finally { try { throw Throwables.rewrapAndThrowUncheckedException(e); return result.get();
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage) public void testOnlyRam() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class); expect(optionsProvider.get()).andReturn(new TemplateOptions()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minRam(4096); templateBuilder.build(); }
protected void assertOperationDoneSuccessfully(Operation operation) { AtomicReference<Operation> ref = Atomics.newReference(checkNotNull(operation, "operation")); checkState(operationDone.apply(ref), "Timeout waiting for operation: %s", operation); assertEquals(ref.get().status(), Operation.Status.DONE); assertTrue(ref.get().error().errors().isEmpty()); }
+ 1); // for the main thread final ExecutorService executor = Executors.newFixedThreadPool(barrier.getParties()); final AtomicReference<AbstractFuture<String>> currentFuture = Atomics.newReference(); final AtomicBoolean setFutureSuccess = new AtomicBoolean(); final AtomicBoolean cancellationSucess = new AtomicBoolean(); Collections.shuffle(allTasks); final AbstractFuture<String> future = new AbstractFuture<String>() {}; currentFuture.set(future); for (Callable<?> task : allTasks) { @SuppressWarnings("unused") // go/futurereturn-lsc
private void waitOperationDone(Operation operation) { AtomicReference<Operation> operationRef = Atomics.newReference(operation); // wait for the operation to complete if (!retryOperationDonePredicate.apply(operationRef)) { throw new UncheckedTimeoutException("operation did not reach DONE state" + operationRef.get()); } // check if the operation failed if (operationRef.get().getHttpError().isPresent()) { HttpResponse response = operationRef.get().getHttpError().get(); throw new IllegalStateException("operation failed. Http Error Code: " + response.getStatusCode() + " HttpError: " + response.getMessage()); } }
/** * {@inheritDoc} */ @Override public void rebootNode(String id) { checkNotNull(id, "id"); logger.debug(">> rebooting node(%s)", id); AtomicReference<NodeMetadata> node = Atomics.newReference(rebootNodeStrategy.rebootNode(id)); boolean successful = nodeRunning.apply(node); logger.debug("<< rebooted node(%s) success(%s)", id, successful); }
@Override public boolean apply(T input) { if (!nodeRunning.apply(newReference(node))) { throw new IllegalStateException(node.getId() + " is no longer running; aborting socket open loop"); } return false; }
@Test(expectedExceptions = NullPointerException.class) public void testRegisterNullImageIsNotAllowed() { ImageCacheSupplier imageCache = new ImageCacheSupplier(Suppliers.<Set<? extends Image>> ofInstance(images), 60, Atomics.<AuthorizationException> newReference(), Providers.of(getImageStrategy)); imageCache.registerImage(null); }
private void blockUntilRunningAndAssignElasticIpsToInstancesOrPutIntoBadMap(Set<RunningInstance> input, Map<NodeMetadata, Exception> badNodes) { Map<RegionAndName, RunningInstance> instancesById = Maps.uniqueIndex(input, instanceToRegionAndName); for (Map.Entry<RegionAndName, RunningInstance> entry : instancesById.entrySet()) { RegionAndName id = entry.getKey(); RunningInstance instance = entry.getValue(); try { logger.debug("<< allocating elastic IP instance(%s)", id); String ip = client.getElasticIPAddressServices().allocateAddressInRegion(id.getRegion()); // block until instance is running logger.debug(">> awaiting status running instance(%s)", id); AtomicReference<NodeMetadata> node = newReference(runningInstanceToNodeMetadata .apply(instance)); nodeRunning.apply(node); logger.trace("<< running instance(%s)", id); logger.debug(">> associating elastic IP %s to instance %s", ip, id); client.getElasticIPAddressServices().associateAddressInRegion(id.getRegion(), ip, id.getName()); logger.trace("<< associated elastic IP %s to instance %s", ip, id); // add mapping of instance to ip into the cache elasticIpCache.put(id, ip); } catch (RuntimeException e) { badNodes.put(runningInstanceToNodeMetadata.apply(instancesById.get(id)), e); } } }
@Test public void testNoUpdatesAtomicReferenceOnPass() { NodeMetadata running = new NodeMetadataBuilder().id("myid").status(Status.RUNNING).build(); GetNodeMetadataStrategy computeService = createMock(GetNodeMetadataStrategy.class); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(running); Assert.assertTrue(nodeRunning.apply(reference)); Assert.assertEquals(reference.get(), running); verify(computeService); }
@Test(expectedExceptions = IllegalStateException.class) public void testNodeRunningFailsOnTerminated() { expect(node.getStatus()).andReturn(Status.TERMINATED).atLeastOnce(); expect(node.getBackendStatus()).andReturn(null).atLeastOnce(); replay(node); replay(computeService); AtomicNodeRunning nodeRunning = new AtomicNodeRunning(computeService); AtomicReference<NodeMetadata> reference = Atomics.newReference(node); nodeRunning.apply(reference); Assert.assertEquals(reference.get(), node); }
/** * * @param id * @return node that was deleted or null if it wasn't found */ @Nullable protected NodeMetadata doDestroyNode(final String id) { checkNotNull(id, "id"); logger.debug(">> destroying node(%s)", id); NodeMetadata nodeMetadata = destroyNodeStrategy.destroyNode(id); if (nodeMetadata == null) return null; final AtomicReference<NodeMetadata> node = Atomics.newReference(nodeMetadata); boolean successful = node.get() == null || nodeTerminated.apply(node); if (successful) credentialStore.remove("node#" + id); logger.debug("<< destroyed node(%s) success(%s)", id, successful); return nodeMetadata; }
@Test public void testLoaderNormal() { AtomicReference<AuthorizationException> authException = newReference(); assertEquals(new SetAndThrowAuthorizationExceptionSupplierBackedLoader<String>(ofInstance("foo"), authException).load("KEY").get(), "foo"); assertEquals(authException.get(), null); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = errorMessage) public void testOnlyCores() { Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet .of(region)); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(image)); Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet .<Hardware> of(hardware)); Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); GetImageStrategy getImageStrategy = createMock(GetImageStrategy.class); expect(optionsProvider.get()).andReturn(new TemplateOptions()); expect(getImageStrategy.getImage(anyObject(String.class))).andReturn(null); replay(defaultTemplate, optionsProvider, templateBuilderProvider, getImageStrategy); TemplateBuilderImpl templateBuilder = new ArbitraryCpuRamTemplateBuilderImpl(locations, new ImageCacheSupplier(images, 60, Atomics.<AuthorizationException>newReference(), Providers.of(getImageStrategy)), hardwares, Suppliers.ofInstance(region), optionsProvider, templateBuilderProvider); templateBuilder.minCores(4); templateBuilder.build(); }