List of usage examples for java.lang Thread interrupt
public void interrupt()
From source file:io.werval.maven.DevShellMojoIT.java
@Test public void devshellMojoIntegrationTest() throws InterruptedException, IOException { final Holder<Exception> errorHolder = new Holder<>(); Thread devshellThread = new Thread(newRunnable(errorHolder, "werval:devshell"), "maven-werval-devshell-thread"); try {/* ww w .ja v a2 s .c o m*/ devshellThread.start(); await().atMost(60, SECONDS).until(() -> lock.exists()); if (errorHolder.isSet()) { throw new RuntimeException("Error during devhell invocation: " + errorHolder.get().getMessage(), errorHolder.get()); } HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet("http://localhost:23023/"); ResponseHandler<String> handler = new BasicResponseHandler(); assertThat(client.execute(get, handler), containsString("I ran!")); Files.write(new File(tmp.getRoot(), "src/main/java/controllers/Application.java").toPath(), CONTROLLER_CHANGED.getBytes(UTF_8)); // Wait for source code change to be detected Thread.sleep(2000); assertThat(client.execute(get, handler), containsString("I ran changed!")); assertThat(client.execute(new HttpGet("http://localhost:23023/@doc"), handler), containsString("Werval Documentation")); client.getConnectionManager().shutdown(); } finally { devshellThread.interrupt(); } }
From source file:io.werval.gradle.ApplicationPluginIntegTest.java
@Test public void startTaskIntegrationTest() throws InterruptedException, IOException { final Holder<Exception> errorHolder = new Holder<>(); Thread runThread = new Thread(newRunnable(errorHolder, "start"), "gradle-werval-start-thread"); try {/*from w ww .j a v a2s . c om*/ runThread.start(); final HttpClient client = new DefaultHttpClient(); final HttpGet get = new HttpGet("http://localhost:23023/"); final ResponseHandler<String> handler = new BasicResponseHandler(); await().atMost(60, SECONDS).pollInterval(5, SECONDS).until(new Callable<String>() { @Override public String call() throws Exception { try { return client.execute(get, handler); } catch (Exception ex) { return null; } } }, allOf(containsString("I ran!"), containsString("custom"))); client.getConnectionManager().shutdown(); if (errorHolder.isSet()) { throw new RuntimeException( "Error during werval:start invocation: " + errorHolder.get().getMessage(), errorHolder.get()); } } finally { runThread.interrupt(); } }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldDrainUntilError() throws Exception { final Thread t = addToQueue(100, 10, true, false, 1); try {/*w w w . j a v a2s . co m*/ assertThat(resultQueue.isEmpty(), is(false)); final List<Result> drain = new ArrayList<>(); resultQueue.drainTo(drain); assertThat(drain.size(), is(greaterThan(0))); assertThat(readCompleted.isDone(), is(false)); // make sure some more items get added to the queue before assert TimeUnit.MILLISECONDS.sleep(100); assertThat(resultQueue.isEmpty(), is(false)); assertThat(readCompleted.isDone(), is(false)); final Exception theProblem = new Exception(); resultQueue.markError(theProblem); assertThat(readCompleted.isDone(), is(true)); try { resultQueue.drainTo(new ArrayList<>()); fail("Should have thrown an exception"); } catch (Exception ex) { assertEquals(theProblem, ex.getCause()); } } finally { t.interrupt(); } }
From source file:org.apache.jackrabbit.core.integration.InterruptedQueryTest.java
@Test public void testQuery() throws Exception { if (Constants.WINDOWS) { return;/*from www . jav a 2s .com*/ } for (int i = 0; i < 100; i++) { session.getRootNode().addNode("node" + i, "nt:unstructured"); } session.save(); final QueryManager qm = session.getWorkspace().getQueryManager(); final AtomicBoolean stop = new AtomicBoolean(false); final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>()); Thread t = new Thread(new Runnable() { @Override public void run() { while (!stop.get() && exceptions.isEmpty()) { try { // execute query String stmt = "//*[@jcr:primaryType='nt:unstructured']"; qm.createQuery(stmt, Query.XPATH).execute(); } catch (RepositoryException e) { if (Constants.SUN_OS) { // on Solaris it's OK when the root cause // of the exception is an InterruptedIOException // the underlying file is not closed Throwable t = e; while (t.getCause() != null) { t = t.getCause(); } if (!(t instanceof InterruptedIOException)) { exceptions.add(e); } } else { exceptions.add(e); } } } } }); t.start(); for (int i = 0; i < 200 && t.isAlive(); i++) { t.interrupt(); Thread.sleep((long) (100.0 * Math.random())); } stop.set(true); t.join(); if (!exceptions.isEmpty()) { throw exceptions.get(0); } }
From source file:backup.integration.MiniClusterTestBase.java
@Test public void testIntegrationBlockCheckWhenAllBackupStoreBlocksMissing() throws Exception { File hdfsDir = setupHdfsLocalDir(); Configuration conf = setupConfig(hdfsDir); MiniDFSCluster hdfsCluster = new MiniDFSCluster.Builder(conf).build(); Thread thread = null; try (BackupStore backupStore = BackupStore.create(BackupUtil.convert(conf))) { DistributedFileSystem fileSystem = hdfsCluster.getFileSystem(); Path path = new Path("/testing.txt"); writeFile(fileSystem, path);/*from w w w. jav a 2s . c o m*/ Thread.sleep(TimeUnit.SECONDS.toMillis(10)); Set<ExtendedBlock> original = getLastGeneration(toSet(backupStore.getExtendedBlocks())); destroyBackupStoreBlocks(backupStore); NameNode nameNode = hdfsCluster.getNameNode(); NameNodeRestoreProcessor processor = SingletonManager.getManager(NameNodeRestoreProcessor.class) .getInstance(nameNode); processor.runBlockCheck(); Thread.sleep(TimeUnit.SECONDS.toMillis(5)); Set<ExtendedBlock> current = toSet(backupStore.getExtendedBlocks()); assertEquals(original, current); } finally { if (thread != null) { thread.interrupt(); } hdfsCluster.shutdown(); destroyBackupStore(conf); } }
From source file:com.horizondigital.delta.UpdateService.java
private boolean zipadjust(String filenameIn, String filenameOut, long start, long currentOut, long totalOut) { Logger.d("zipadjust [%s] --> [%s]", filenameIn, filenameOut); // checking filesizes in the background as progress, because these // native functions don't have callbacks (yet) to do this (new File(filenameOut)).delete(); Thread progress = getThreadedProgress(filenameOut, (new File(filenameIn)).getName(), start, currentOut, totalOut);//w w w .j a v a2 s.c o m progress.start(); int ok = Native.zipadjust(filenameIn, filenameOut, 1); progress.interrupt(); try { progress.join(); } catch (InterruptedException e) { // We got interrupted in a very short wait, surprising, but not a problem. 'progress' will quit by itself. Logger.ex(e); } Logger.d("zipadjust --> %d", ok); return (ok == 1); }
From source file:eu.chainfire.opendelta.UpdateService.java
private boolean zipadjust(String filenameIn, String filenameOut, long start, long currentOut, long totalOut) { Logger.d("zipadjust [%s] --> [%s]", filenameIn, filenameOut); // checking filesizes in the background as progress, because these // native functions don't have callbacks (yet) to do this (new File(filenameOut)).delete(); Thread progress = getThreadedProgress(filenameOut, (new File(filenameIn)).getName(), start, currentOut, totalOut);//from w w w .j a v a 2 s . co m progress.start(); int ok = Native.zipadjust(filenameIn, filenameOut, 1); progress.interrupt(); try { progress.join(); } catch (InterruptedException e) { // We got interrupted in a very short wait, surprising, but not a // problem. 'progress' will quit by itself. Logger.ex(e); } Logger.d("zipadjust --> %d", ok); return (ok == 1); }
From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java
@Test public void shouldDrainAsItemsArrive() throws Exception { final Thread t = addToQueue(1000, 1, true); try {/* www.j av a 2s . co m*/ final AtomicInteger count1 = new AtomicInteger(0); final AtomicInteger count2 = new AtomicInteger(0); final AtomicInteger count3 = new AtomicInteger(0); final CountDownLatch latch = new CountDownLatch(3); resultQueue.await(500).thenAcceptAsync(r -> { count1.set(r.size()); latch.countDown(); }); resultQueue.await(150).thenAcceptAsync(r -> { count2.set(r.size()); latch.countDown(); }); resultQueue.await(350).thenAcceptAsync(r -> { count3.set(r.size()); latch.countDown(); }); assertThat(latch.await(3000, TimeUnit.MILLISECONDS), is(true)); assertEquals(500, count1.get()); assertEquals(150, count2.get()); assertEquals(350, count3.get()); assertThat(resultQueue.isEmpty(), is(true)); } finally { t.interrupt(); } }
From source file:com.horizondigital.delta.UpdateService.java
private boolean dedelta(String filenameSource, String filenameDelta, String filenameOut, long start, long currentOut, long totalOut) { Logger.d("dedelta [%s] --> [%s] --> [%s]", filenameSource, filenameDelta, filenameOut); // checking filesizes in the background as progress, because these // native functions don't have callbacks (yet) to do this (new File(filenameOut)).delete(); Thread progress = getThreadedProgress(filenameOut, (new File(filenameDelta)).getName(), start, currentOut, totalOut);/*from ww w .ja v a2 s . co m*/ progress.start(); int ok = Native.dedelta(filenameSource, filenameDelta, filenameOut); progress.interrupt(); try { progress.join(); } catch (InterruptedException e) { // We got interrupted in a very short wait, surprising, but not a problem. 'progress' will quit by itself. Logger.ex(e); } Logger.d("dedelta --> %d", ok); return (ok == 1); }
From source file:eu.chainfire.opendelta.UpdateService.java
private boolean dedelta(String filenameSource, String filenameDelta, String filenameOut, long start, long currentOut, long totalOut) { Logger.d("dedelta [%s] --> [%s] --> [%s]", filenameSource, filenameDelta, filenameOut); // checking filesizes in the background as progress, because these // native functions don't have callbacks (yet) to do this (new File(filenameOut)).delete(); Thread progress = getThreadedProgress(filenameOut, (new File(filenameDelta)).getName(), start, currentOut, totalOut);// ww w. ja v a2s .c o m progress.start(); int ok = Native.dedelta(filenameSource, filenameDelta, filenameOut); progress.interrupt(); try { progress.join(); } catch (InterruptedException e) { // We got interrupted in a very short wait, surprising, but not a // problem. 'progress' will quit by itself. Logger.ex(e); } Logger.d("dedelta --> %d", ok); return (ok == 1); }