List of usage examples for java.util.concurrent.atomic AtomicBoolean set
public final void set(boolean newValue)
From source file:org.apache.hadoop.hbase.executor.TestExecutorService.java
@Test public void testExecutorService() throws Exception { int maxThreads = 5; int maxTries = 10; int sleepInterval = 10; Server mockedServer = mock(Server.class); when(mockedServer.getConfiguration()).thenReturn(HBaseConfiguration.create()); // Start an executor service pool with max 5 threads ExecutorService executorService = new ExecutorService("unit_test"); executorService.startExecutorService(ExecutorType.MASTER_SERVER_OPERATIONS, maxThreads); Executor executor = executorService.getExecutor(ExecutorType.MASTER_SERVER_OPERATIONS); ThreadPoolExecutor pool = executor.threadPoolExecutor; // Assert no threads yet assertEquals(0, pool.getPoolSize()); AtomicBoolean lock = new AtomicBoolean(true); AtomicInteger counter = new AtomicInteger(0); // Submit maxThreads executors. for (int i = 0; i < maxThreads; i++) { executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); }/*w w w . j a va2 s . c o m*/ // The TestEventHandler will increment counter when it starts. int tries = 0; while (counter.get() < maxThreads && tries < maxTries) { LOG.info("Waiting for all event handlers to start..."); Thread.sleep(sleepInterval); tries++; } // Assert that pool is at max threads. assertEquals(maxThreads, counter.get()); assertEquals(maxThreads, pool.getPoolSize()); ExecutorStatus status = executor.getStatus(); assertTrue(status.queuedEvents.isEmpty()); assertEquals(5, status.running.size()); checkStatusDump(status); // Now interrupt the running Executor synchronized (lock) { lock.set(false); lock.notifyAll(); } // Executor increments counter again on way out so.... test that happened. while (counter.get() < (maxThreads * 2) && tries < maxTries) { System.out.println("Waiting for all event handlers to finish..."); Thread.sleep(sleepInterval); tries++; } assertEquals(maxThreads * 2, counter.get()); assertEquals(maxThreads, pool.getPoolSize()); // Add more than the number of threads items. // Make sure we don't get RejectedExecutionException. for (int i = 0; i < (2 * maxThreads); i++) { executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); } // Now interrupt the running Executor synchronized (lock) { lock.set(false); lock.notifyAll(); } // Make sure threads are still around even after their timetolive expires. Thread.sleep(ExecutorService.Executor.keepAliveTimeInMillis * 2); assertEquals(maxThreads, pool.getPoolSize()); executorService.shutdown(); assertEquals(0, executorService.getAllExecutorStatuses().size()); // Test that submit doesn't throw NPEs executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); }
From source file:com.alibaba.wasp.executor.TestExecutorService.java
@Test public void testExecutorService() throws Exception { int maxThreads = 5; int maxTries = 10; int sleepInterval = 10; Server mockedServer = mock(Server.class); when(mockedServer.getConfiguration()).thenReturn(conf); // Start an executor service pool with max 5 threads ExecutorService executorService = new ExecutorService("unit_test"); executorService.startExecutorService(ExecutorType.MASTER_SERVER_OPERATIONS, maxThreads); Executor executor = executorService.getExecutor(ExecutorType.MASTER_SERVER_OPERATIONS); ThreadPoolExecutor pool = executor.threadPoolExecutor; // Assert no threads yet assertEquals(0, pool.getPoolSize()); AtomicBoolean lock = new AtomicBoolean(true); AtomicInteger counter = new AtomicInteger(0); // Submit maxThreads executors. for (int i = 0; i < maxThreads; i++) { executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); }/*from ww w . ja va2 s . c om*/ // The TestEventHandler will increment counter when it starts. int tries = 0; while (counter.get() < maxThreads && tries < maxTries) { LOG.info("Waiting for all event handlers to start..."); Thread.sleep(sleepInterval); tries++; } // Assert that pool is at max threads. assertEquals(maxThreads, counter.get()); assertEquals(maxThreads, pool.getPoolSize()); ExecutorStatus status = executor.getStatus(); assertTrue(status.queuedEvents.isEmpty()); assertEquals(5, status.running.size()); checkStatusDump(status); // Now interrupt the running Executor synchronized (lock) { lock.set(false); lock.notifyAll(); } // Executor increments counter again on way out so.... test that happened. while (counter.get() < (maxThreads * 2) && tries < maxTries) { System.out.println("Waiting for all event handlers to finish..."); Thread.sleep(sleepInterval); tries++; } assertEquals(maxThreads * 2, counter.get()); assertEquals(maxThreads, pool.getPoolSize()); // Add more than the number of threads items. // Make sure we don't get RejectedExecutionException. for (int i = 0; i < (2 * maxThreads); i++) { executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); } // Now interrupt the running Executor synchronized (lock) { lock.set(false); lock.notifyAll(); } // Make sure threads are still around even after their timetolive expires. Thread.sleep(ExecutorService.Executor.keepAliveTimeInMillis * 2); assertEquals(maxThreads, pool.getPoolSize()); executorService.shutdown(); assertEquals(0, executorService.getAllExecutorStatuses().size()); // Test that submit doesn't throw NPEs executorService.submit(new TestEventHandler(mockedServer, EventType.M_SERVER_SHUTDOWN, lock, counter)); }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTest.java
@Test public void shouldAllowVariableReuseAcrossThreads() throws Exception { final BasicThreadFactory testingThreadFactory = new BasicThreadFactory.Builder() .namingPattern("test-gremlin-scriptengine-%d").build(); final ExecutorService service = Executors.newFixedThreadPool(8, testingThreadFactory); final GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(); final AtomicBoolean failed = new AtomicBoolean(false); final int max = 512; final List<Pair<Integer, List<Integer>>> futures = Collections.synchronizedList(new ArrayList<>(max)); IntStream.range(0, max).forEach(i -> { final int yValue = i * 2; final int zValue = i * -1; final Bindings b = new SimpleBindings(); b.put("x", i); b.put("y", yValue); final String script = "z=" + zValue + ";[x,y,z]"; try {//from ww w. j a v a 2 s . co m service.submit(() -> { try { final List<Integer> result = (List<Integer>) scriptEngine.eval(script, b); futures.add(Pair.with(i, result)); } catch (Exception ex) { failed.set(true); } }); } catch (Exception ex) { throw new RuntimeException(ex); } }); service.shutdown(); assertThat(service.awaitTermination(120000, TimeUnit.MILLISECONDS), is(true)); // likely a concurrency exception if it occurs - and if it does then we've messed up because that's what this // test is partially designed to protected against. assertThat(failed.get(), is(false)); assertEquals(max, futures.size()); futures.forEach(t -> { assertEquals(t.getValue0(), t.getValue1().get(0)); assertEquals(t.getValue0() * 2, t.getValue1().get(1).intValue()); assertEquals(t.getValue0() * -1, t.getValue1().get(2).intValue()); }); }
From source file:com.netflix.dyno.connectionpool.impl.lb.CircularListTest.java
@Test public void testSingleThreadWithElementRemove() throws Exception { final AtomicBoolean stop = new AtomicBoolean(false); Future<Map<Integer, Integer>> future = threadPool.submit(new Callable<Map<Integer, Integer>>() { @Override/* w w w . j av a 2 s. c o m*/ public Map<Integer, Integer> call() throws Exception { TestWorker worker = new TestWorker(); while (!stop.get()) { worker.process(); } return worker.map; } }); Thread.sleep(200); List<Integer> newList = new ArrayList<Integer>(); newList.addAll(iList); final List<Integer> removedElements = new ArrayList<Integer>(); removedElements.add(newList.remove(2)); removedElements.add(newList.remove(5)); removedElements.add(newList.remove(6)); cList.swapWithList(newList); Thread.sleep(200); stop.set(true); Map<Integer, Integer> result = future.get(); Map<Integer, Integer> subMap = CollectionUtils.filterKeys(result, new Predicate<Integer>() { @Override public boolean apply(Integer input) { return !removedElements.contains(input); } }); checkValues(new ArrayList<Integer>(subMap.values())); }
From source file:org.apache.tinkerpop.gremlin.server.GremlinServerIntegrateTest.java
@Test public void shouldRespectHighWaterMarkSettingAndSucceed() throws Exception { // the highwatermark should get exceeded on the server and thus pause the writes, but have no problem catching // itself up - this is a tricky tests to get passing on all environments so this assumption will deny the // test for most cases assumeThat("Set the 'assertNonDeterministic' property to true to execute this test", System.getProperty("assertNonDeterministic"), is("true")); final Cluster cluster = Cluster.open(); final Client client = cluster.connect(); try {/*from w ww . j av a 2s. com*/ final int resultCountToGenerate = 1000; final int batchSize = 3; final String fatty = IntStream.range(0, 175).mapToObj(String::valueOf).collect(Collectors.joining()); final String fattyX = "['" + fatty + "'] * " + resultCountToGenerate; // don't allow the thread to proceed until all results are accounted for final CountDownLatch latch = new CountDownLatch(resultCountToGenerate); final AtomicBoolean expected = new AtomicBoolean(false); final AtomicBoolean faulty = new AtomicBoolean(false); final RequestMessage request = RequestMessage.build(Tokens.OPS_EVAL) .addArg(Tokens.ARGS_BATCH_SIZE, batchSize).addArg(Tokens.ARGS_GREMLIN, fattyX).create(); client.submitAsync(request).thenAcceptAsync(r -> { r.stream().forEach(item -> { try { final String aFattyResult = item.getString(); expected.set(aFattyResult.equals(fatty)); } catch (Exception ex) { ex.printStackTrace(); faulty.set(true); } finally { latch.countDown(); } }); }); assertTrue(latch.await(30000, TimeUnit.MILLISECONDS)); assertEquals(0, latch.getCount()); assertFalse(faulty.get()); assertTrue(expected.get()); assertTrue(recordingAppender.getMessages().stream() .anyMatch(m -> m.contains("Pausing response writing as writeBufferHighWaterMark exceeded on"))); } catch (Exception ex) { fail("Shouldn't have tossed an exception"); } finally { cluster.close(); } }
From source file:org.apache.blur.command.ShardCommandManagerTest.java
@Test public void testShardCommandManagerNormalWithCancel() throws IOException, TimeoutException, ExceptionCollector, BlurException, InterruptedException { String commandExecutionId = "TEST_COMMAND_ID1"; BlurObjectSerDe serDe = new BlurObjectSerDe(); WaitForSeconds waitForSeconds = new WaitForSeconds(); waitForSeconds.setTable("test"); waitForSeconds.setSeconds(5);//from w w w. j a va2 s .c om waitForSeconds.setCommandExecutionId(commandExecutionId); Arguments arguments = CommandUtil.toArguments(waitForSeconds, serDe); BlurObject args = CommandUtil.toBlurObject(arguments); System.out.println(args.toString(1)); final ArgumentOverlay argumentOverlay = new ArgumentOverlay(args, serDe); final AtomicBoolean fail = new AtomicBoolean(); final AtomicBoolean running = new AtomicBoolean(true); new Thread(new Runnable() { @Override public void run() { TableContextFactory tableContextFactory = getTableContextFactory(); Long instanceExecutionId = null; while (true) { try { Response response; if (instanceExecutionId == null) { response = _manager.execute(tableContextFactory, "wait", argumentOverlay); } else { response = _manager.reconnect(instanceExecutionId); } fail.set(true); System.out.println(response); return; } catch (IOException e) { if (e.getCause() instanceof CancellationException) { return; } e.printStackTrace(); fail.set(true); return; } catch (TimeoutException e) { instanceExecutionId = e.getInstanceExecutionId(); } catch (Exception e) { e.printStackTrace(); fail.set(true); return; } finally { running.set(false); } } } }).start(); Thread.sleep(1000); _manager.cancelCommand(commandExecutionId); Thread.sleep(5000); if (fail.get() || running.get()) { fail("Fail [" + fail.get() + "] Running [" + running.get() + "]"); } }
From source file:org.apache.hadoop.contrib.bkjournal.BookKeeperJournalManager.java
/** * Pre-creating bookkeeper metadata path in zookeeper. *///from w w w .j av a 2 s . c om private void prepareBookKeeperEnv() throws IOException { // create bookie available path in zookeeper if it doesn't exists final String zkAvailablePath = conf.get(BKJM_ZK_LEDGERS_AVAILABLE_PATH, BKJM_ZK_LEDGERS_AVAILABLE_PATH_DEFAULT); final CountDownLatch zkPathLatch = new CountDownLatch(1); final AtomicBoolean success = new AtomicBoolean(false); StringCallback callback = new StringCallback() { @Override public void processResult(int rc, String path, Object ctx, String name) { if (KeeperException.Code.OK.intValue() == rc || KeeperException.Code.NODEEXISTS.intValue() == rc) { LOG.info("Successfully created bookie available path : " + zkAvailablePath); success.set(true); } else { KeeperException.Code code = KeeperException.Code.get(rc); LOG.error("Error : " + KeeperException.create(code, path).getMessage() + ", failed to create bookie available path : " + zkAvailablePath); } zkPathLatch.countDown(); } }; ZkUtils.asyncCreateFullPathOptimistic(zkc, zkAvailablePath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, callback, null); try { if (!zkPathLatch.await(zkc.getSessionTimeout(), TimeUnit.MILLISECONDS) || !success.get()) { throw new IOException("Couldn't create bookie available path :" + zkAvailablePath + ", timed out " + zkc.getSessionTimeout() + " millis"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Interrupted when creating the bookie available path : " + zkAvailablePath, e); } }
From source file:org.apache.hadoop.hbase.security.visibility.TestVisibilityLabelsWithDefaultVisLabelService.java
@Test(timeout = 60 * 1000) public void testAddVisibilityLabelsOnRSRestart() throws Exception { List<RegionServerThread> regionServerThreads = TEST_UTIL.getHBaseCluster().getRegionServerThreads(); for (RegionServerThread rsThread : regionServerThreads) { rsThread.getRegionServer().abort("Aborting "); }/*from w w w . j av a 2 s . c o m*/ // Start one new RS RegionServerThread rs = TEST_UTIL.getHBaseCluster().startRegionServer(); waitForLabelsRegionAvailability(rs.getRegionServer()); final AtomicBoolean vcInitialized = new AtomicBoolean(true); do { PrivilegedExceptionAction<VisibilityLabelsResponse> action = new PrivilegedExceptionAction<VisibilityLabelsResponse>() { public VisibilityLabelsResponse run() throws Exception { String[] labels = { SECRET, CONFIDENTIAL, PRIVATE, "ABC", "XYZ" }; try (Connection conn = ConnectionFactory.createConnection(conf)) { VisibilityLabelsResponse resp = VisibilityClient.addLabels(conn, labels); List<RegionActionResult> results = resp.getResultList(); if (results.get(0).hasException()) { NameBytesPair pair = results.get(0).getException(); Throwable t = ProtobufUtil.toException(pair); LOG.debug("Got exception writing labels", t); if (t instanceof VisibilityControllerNotReadyException) { vcInitialized.set(false); LOG.warn("VisibilityController was not yet initialized"); Threads.sleep(10); } else { vcInitialized.set(true); } } else LOG.debug("new labels added: " + resp); } catch (Throwable t) { throw new IOException(t); } return null; } }; SUPERUSER.runAs(action); } while (!vcInitialized.get()); // Scan the visibility label Scan s = new Scan(); s.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL)); int i = 0; try (Table ht = TEST_UTIL.getConnection().getTable(LABELS_TABLE_NAME); ResultScanner scanner = ht.getScanner(s)) { while (true) { Result next = scanner.next(); if (next == null) { break; } i++; } } // One label is the "system" label. Assert.assertEquals("The count should be 13", 13, i); }
From source file:eu.europa.ec.markt.dss.validation102853.pades.PAdESSignature.java
private boolean hasDSSDictionary() { boolean hasDSSDictionary; final PDFSignatureService pdfTimestampSignatureService = PdfObjFactory.getInstance() .newTimestampSignatureService(); try {/*www . j a v a2 s .co m*/ final AtomicBoolean atomicHasDSSDictionnary = new AtomicBoolean(false); pdfTimestampSignatureService.validateSignatures(document.openStream(), new SignatureValidationCallback() { @Override public void validate(PdfDict catalog, PdfDict outerCatalog, X509Certificate signingCert, Date signingDate, Certificate[] certs, PdfDict signatureDictionary, PdfSignatureInfo pk) { PdfDict _catalog = outerCatalog != null ? outerCatalog : pdfCatalog; if (_catalog != null) { atomicHasDSSDictionnary.set((getCertificateSource() != null) && (getCertificateSource().getCertificates() != null) && (!getCertificateSource().getCertificates().isEmpty())); } } }); hasDSSDictionary = atomicHasDSSDictionnary.get(); } catch (IOException e) { throw new DSSException(e); } catch (SignatureException e) { throw new DSSException(e); } return hasDSSDictionary; }
From source file:org.apache.bookkeeper.replication.TestLedgerUnderreplicationManager.java
/** * Test that as the hierarchy gets cleaned up, it doesn't interfere * with the marking of other ledgers as underreplicated */// w w w. j a v a 2 s . c om @Test(timeout = 90000) public void testHierarchyCleanupInterference() throws Exception { final LedgerUnderreplicationManager replicaMgr1 = lmf1.newLedgerUnderreplicationManager(); final LedgerUnderreplicationManager replicaMgr2 = lmf2.newLedgerUnderreplicationManager(); final int iterations = 100; final AtomicBoolean threadFailed = new AtomicBoolean(false); Thread markUnder = new Thread() { public void run() { long l = 1; try { for (int i = 0; i < iterations; i++) { replicaMgr1.markLedgerUnderreplicated(l, "localhost:3181"); l += 10000; } } catch (Exception e) { LOG.error("markUnder Thread failed with exception", e); threadFailed.set(true); return; } } }; final AtomicInteger processed = new AtomicInteger(0); Thread markRepl = new Thread() { public void run() { try { for (int i = 0; i < iterations; i++) { long l = replicaMgr2.getLedgerToRereplicate(); replicaMgr2.markLedgerReplicated(l); processed.incrementAndGet(); } } catch (Exception e) { LOG.error("markRepl Thread failed with exception", e); threadFailed.set(true); return; } } }; markRepl.setDaemon(true); markUnder.setDaemon(true); markRepl.start(); markUnder.start(); markUnder.join(); assertFalse("Thread failed to complete", threadFailed.get()); int lastProcessed = 0; while (true) { markRepl.join(10000); if (!markRepl.isAlive()) { break; } assertFalse("markRepl thread not progressing", lastProcessed == processed.get()); } assertFalse("Thread failed to complete", threadFailed.get()); List<String> children = zkc1.getChildren(urLedgerPath, false); for (String s : children) { LOG.info("s: {}", s); } assertEquals("All hierarchies should be cleaned up", 0, children.size()); }