List of usage examples for java.util.concurrent Semaphore Semaphore
public Semaphore(int permits)
From source file:com.thoughtworks.go.agent.bootstrapper.AgentBootstrapperTest.java
@Test public void shouldNotDieWhenInvocationOfLauncherRaisesException_butCreationOfLauncherWentThrough() throws InterruptedException { final Semaphore waitForLauncherInvocation = new Semaphore(1); waitForLauncherInvocation.acquire(); final AgentBootstrapper bootstrapper = new AgentBootstrapper() { @Override/*from www. j a va2 s . c o m*/ AgentLauncherCreator getLauncherCreator() { return new AgentLauncherCreator() { public AgentLauncher createLauncher() { return new AgentLauncher() { public int launch(AgentLaunchDescriptor descriptor) { try { throw new RuntimeException("fail!!! i say."); } finally { if (waitForLauncherInvocation.availablePermits() == 0) { waitForLauncherInvocation.release(); } } } }; } @Override public void close() { } }; } }; final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper); Thread stopLoopThd = new Thread(new Runnable() { public void run() { try { waitForLauncherInvocation.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } ReflectionUtil.setField(spyBootstrapper, "loop", false); } }); stopLoopThd.start(); try { spyBootstrapper.go(true, new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null, AgentBootstrapperArgs.SslMode.NONE)); stopLoopThd.join(); } catch (Exception e) { fail("should not have propagated exception thrown while invoking the launcher"); } }
From source file:com.parse.ParseConfigTest.java
@Test public void testGetInBackgroundWithCallbackSuccess() throws Exception { final Map<String, Object> params = new HashMap<>(); params.put("string", "value"); ParseConfig config = new ParseConfig(params); ParseConfigController controller = mockParseConfigControllerWithResponse(config); ParseCorePlugins.getInstance().registerConfigController(controller); final Semaphore done = new Semaphore(0); ParseConfig.getInBackground(new ConfigCallback() { @Override/* w ww .j a v a2 s .c o m*/ public void done(ParseConfig config, ParseException e) { assertEquals(1, config.params.size()); assertEquals("value", config.params.get("string")); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).getAsync(anyString()); }
From source file:org.jboss.pnc.environment.docker.DockerEnvironmentDriverRemoteTest.java
@Test public void buildDestroyEnvironmentTest() throws EnvironmentDriverException, InterruptedException { final Semaphore mutex = new Semaphore(0); // Create container final DockerStartedEnvironment startedEnv = (DockerStartedEnvironment) dockerEnvDriver .buildEnvironment(BuildType.JAVA, DUMMY_REPOSITORY_CONFIGURATION); Consumer<RunningEnvironment> onComplete = (generalRunningEnv) -> { DockerRunningEnvironment runningEnv = (DockerRunningEnvironment) generalRunningEnv; boolean containerDestroyed = false; try {//from ww w . j ava2 s . c om testRunningContainer(runningEnv, true, "Environment wasn't successfully built."); testRunningEnvContainer(runningEnv, true, "Environment wasn't set up correctly."); // Destroy container dockerEnvDriver.destroyEnvironment(runningEnv.getId()); containerDestroyed = true; testRunningContainer(runningEnv, false, "Environment wasn't successfully destroyed."); mutex.release(); } catch (Throwable e) { fail(e.getMessage()); } finally { if (!containerDestroyed) destroyEnvironmentWithReport(runningEnv.getId()); } }; Consumer<Exception> onError = (e) -> { destroyEnvironmentWithReport(startedEnv.getId()); fail("Failed to init docker container. " + e.getMessage()); }; startedEnv.monitorInitialization(onComplete, onError); mutex.tryAcquire(MAX_TEST_DURATION, TimeUnit.SECONDS); }
From source file:org.commoncrawl.service.listcrawler.DataTransferAgent.java
static int uploadSingeFile(CCBridgeServerMapping mapping, FileSystem fs, Configuration conf, Path hdfsFilePath, String uploadName, EventLoop eventLoop) throws IOException { final FileStatus fileStatus = fs.getFileStatus(hdfsFilePath); LOG.info("Uploading:" + uploadName + " size:" + fileStatus.getLen() + " to:" + mapping._internalName); {/* w w w . ja v a2 s.c o m*/ // construct url URL fePostURL = new URL("http://" + mapping._externalName + ":8090/"); LOG.info("POST URL IS:" + fePostURL.toString()); // open input stream final FSDataInputStream is = fs.open(hdfsFilePath); final Semaphore blockingSemaphore = new Semaphore(0); NIOHttpConnection connection = null; try { // create connection connection = new NIOHttpConnection(fePostURL, eventLoop.getSelector(), eventLoop.getResolver(), null); // set listener connection.setListener(new Listener() { @Override public void HttpConnectionStateChanged(NIOHttpConnection theConnection, State oldState, State state) { LOG.info("Connection State Changed to:" + state.toString()); if (state == State.DONE || state == State.ERROR) { //LOG.info("Connection Transition to Done or Error"); //LOG.info("Response Headers:" + theConnection.getResponseHeaders().toString()); blockingSemaphore.release(); } } @Override public void HttpContentAvailable(NIOHttpConnection theConnection, NIOBufferList contentBuffer) { // TODO Auto-generated method stub } }); // set headers connection.getRequestHeaders().reset(); connection.getRequestHeaders().prepend("PUT /put?src=" + uploadName + " HTTP/1.1", null); connection.getRequestHeaders().set("Host", mapping._internalName + ":8090"); connection.getRequestHeaders().set("Content-Length", Long.toString(fileStatus.getLen())); connection.getRequestHeaders().set("Connection", "keep-alive"); connection.setPopulateDefaultHeaderItems(false); final LinkedBlockingDeque<BufferStruct> _loaderQueue = new LinkedBlockingDeque<BufferStruct>(20); final AtomicBoolean eof = new AtomicBoolean(); final ByteBuffer sentinel = ByteBuffer.allocate(4096); sentinel.position(sentinel.position()); final Thread loaderThread = new Thread(new Runnable() { int _id = 0; @Override public void run() { int bytesRead; byte incomingBuffer[] = new byte[4096 * 10]; try { while ((bytesRead = is.read(incomingBuffer)) != -1) { ByteBuffer buffer = ByteBuffer.wrap(incomingBuffer, 0, bytesRead); buffer.position(bytesRead); //LOG.info("Loader Thread Read:"+ bytesRead + " Buffer:" + ++_id); try { _loaderQueue.put(new BufferStruct(buffer, _id)); } catch (InterruptedException e) { LOG.error(CCStringUtils.stringifyException(e)); break; } incomingBuffer = new byte[4096 * 10]; } try { _loaderQueue.put(new BufferStruct(sentinel, ++_id)); } catch (InterruptedException e) { } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); return; } } }); loaderThread.start(); // set data source ... connection.setDataSource(new DataSource() { int bytesTransferred = 0; @Override public boolean read(NIOBufferList dataBuffer) throws IOException { if (eof.get()) return true; //LOG.info("Connect read callback triggered"); BufferStruct buffer = _loaderQueue.poll(); if (buffer != null) { if (buffer._buffer != sentinel) { //LOG.info("Got Buffer:"+ buffer._id); if (buffer._id == 1) { //LOG.info("Inital Buffer Bytes:" + new String(buffer._buffer.array(),0,10).toString()); } dataBuffer.write(buffer._buffer); bytesTransferred += buffer._buffer.limit(); //LOG.info("Read:" + buffer._buffer.limit() + " Transfered:" + bytesTransferred); return false; } else { //LOG.info("EOF Condition"); dataBuffer.write(sentinel); eof.set(true); return true; } } return false; } }); // open connection connection.open(); // wait for connection to complete ... blockingSemaphore.acquireUninterruptibly(); // kill loader thread loaderThread.interrupt(); try { LOG.info("Waiting for Loader Thread"); loaderThread.join(); LOG.info("Done Waiting for Loader Thread"); } catch (InterruptedException e) { } } finally { is.close(); if (connection != null) { connection.close(); LOG.info("Response Code for File:" + uploadName + "to Host: " + mapping._internalName + " is:" + connection.getResponseHeaders().getHttpResponseCode()); return connection.getResponseHeaders().getHttpResponseCode(); /* if (connection.getResponseHeaders().getHttpResponseCode() != 200) { throw new IOException("Failed to upload file:" + dataFile.getName() + " responseCode:" + connection.getResponseHeaders().getHttpResponseCode()); } */ } } } // something went wrong ??? LOG.error("Failed to upload file:" + uploadName + " unknown response code"); return 500; }
From source file:com.netflix.curator.framework.imps.TestFailedDeleteManager.java
@Test public void testWithNamespaceAndLostSessionAlt() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()) .retryPolicy(new ExponentialBackoffRetry(100, 3)).build(); try {/*from w w w. j av a 2 s.c o m*/ client.start(); CuratorFramework namespaceClient = client.usingNamespace("foo"); namespaceClient.create().forPath("/test-me"); final CountDownLatch latch = new CountDownLatch(1); final Semaphore semaphore = new Semaphore(0); ConnectionStateListener listener = new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) { semaphore.release(); } else if (newState == ConnectionState.RECONNECTED) { latch.countDown(); } } }; namespaceClient.getConnectionStateListenable().addListener(listener); server.stop(); Assert.assertTrue(timing.acquireSemaphore(semaphore)); try { namespaceClient.delete().guaranteed().forPath("/test-me"); Assert.fail(); } catch (KeeperException.ConnectionLossException e) { // expected } Assert.assertTrue(timing.acquireSemaphore(semaphore)); timing.sleepABit(); server = new TestingServer(server.getPort(), server.getTempDirectory()); Assert.assertTrue(timing.awaitLatch(latch)); timing.sleepABit(); Assert.assertNull(namespaceClient.checkExists().forPath("/test-me")); } finally { IOUtils.closeQuietly(client); } }
From source file:org.commoncrawl.util.MapReduceJobStatsWriter.java
/** close and flush the log file **/ public void close(final Callback optionalAsyncCallback) { if (_eventLoop != null) { // allocate a blocking semaphore in case async callback was not specified final Semaphore blockingCallSemaphore = new Semaphore(0); // perform shutdown in worker thread ... _eventLoop.setTimer(new Timer(0, false, new Timer.Callback() { @Override//from www .j ava 2 s . c o m public void timerFired(Timer timer) { try { try { if (_writer != null) { _writer.close(); } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); _lastLogWriteException = e; } finally { _writer = null; try { if (_outputStream != null) { _outputStream.flush(); _outputStream.close(); } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); _lastLogWriteException = e; } finally { _outputStream = null; } } // now figure out if everything went smoothly or not if (_entryCount != 0 && _lastLogWriteException == null) { // ok so far so good... time to copy the local log file to hdfs ... Path hdfsPath = new Path(Environment.HDFS_LOGCOLLECTOR_BASEDIR, _logFamily + "/" + _groupingKey + "/" + Long.toString(_uniqueKey)); try { // delete the remote file if it exists _remoteFileSystem.delete(hdfsPath, false); // ensure parent path _remoteFileSystem.mkdirs(hdfsPath.getParent()); // now if the local file exists and has data if (_tempFileName.exists() && _tempFileName.length() != 0) { // copy the file to hdfs _remoteFileSystem.copyFromLocalFile(new Path(_tempFileName.getAbsolutePath()), hdfsPath); } } catch (IOException e) { LOG.error(CCStringUtils.stringifyException(e)); _lastLogWriteException = e; } } } finally { // always delete the temp file ... _tempFileName.delete(); // release semaphore blockingCallSemaphore.release(); // if callback was specified , call it now if (optionalAsyncCallback != null) { optionalAsyncCallback.execute(); } // stop the event loop ... _eventLoop.stop(); _eventLoop = null; } } })); // now if callback was not specified... wait for blocking semaphore to signal ... if (optionalAsyncCallback == null) { blockingCallSemaphore.acquireUninterruptibly(); } } }
From source file:com.amazonaws.services.sqs.buffered.SendQueueBuffer.java
SendQueueBuffer(AmazonSQS sqsClient, Executor executor, QueueBufferConfig paramConfig, String url) { this.sqsClient = sqsClient; this.executor = executor; this.config = paramConfig; qUrl = url;// w w w . ja va 2s . c om int maxBatch = config.getMaxInflightOutboundBatches(); //must allow at least one outbound batch. maxBatch = maxBatch > 0 ? maxBatch : 1; this.inflightSendMessageBatches = new Semaphore(maxBatch); this.inflightDeleteMessageBatches = new Semaphore(maxBatch); this.inflightChangeMessageVisibilityBatches = new Semaphore(maxBatch); }
From source file:org.apache.geode.internal.net.SSLSocketIntegrationTest.java
@Test public void configureClientSSLSocketCanTimeOut() throws Exception { final Semaphore serverCoordination = new Semaphore(0); // configure a non-SSL server socket. We will connect // a client SSL socket to it and demonstrate that the // handshake times out final ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(SocketCreator.getLocalHost(), 0)); Thread serverThread = new Thread() { public void run() { serverCoordination.release(); try (Socket clientSocket = serverSocket.accept()) { System.out.println("server thread accepted a connection"); serverCoordination.acquire(); } catch (Exception e) { System.err.println("accept failed"); e.printStackTrace();//w ww.j a v a2 s .c om } try { serverSocket.close(); } catch (IOException e) { // ignored } System.out.println("server thread is exiting"); } }; serverThread.setName("SocketCreatorJUnitTest serverSocket thread"); serverThread.setDaemon(true); serverThread.start(); serverCoordination.acquire(); SocketCreator socketCreator = SocketCreatorFactory .getSocketCreatorForComponent(SecurableCommunicationChannel.SERVER); int serverSocketPort = serverSocket.getLocalPort(); try { Awaitility.await("connect to server socket").atMost(30, TimeUnit.SECONDS).until(() -> { try { Socket clientSocket = socketCreator.connectForClient( SocketCreator.getLocalHost().getHostAddress(), serverSocketPort, 2000); clientSocket.close(); System.err.println( "client successfully connected to server but should not have been able to do so"); return false; } catch (SocketTimeoutException e) { // we need to verify that this timed out in the handshake // code System.out.println("client connect attempt timed out - checking stack trace"); StackTraceElement[] trace = e.getStackTrace(); for (StackTraceElement element : trace) { if (element.getMethodName().equals("configureClientSSLSocket")) { System.out.println("client connect attempt timed out in the appropriate method"); return true; } } // it wasn't in the configuration method so we need to try again } catch (IOException e) { // server socket may not be in accept() yet, causing a connection-refused // exception } return false; }); } finally { serverCoordination.release(); } }
From source file:functionaltests.RestSmartProxyTest.java
@Test(timeout = TEN_MINUTES) public void testInErrorEventsReception() throws Exception { TaskFlowJob job = createInErrorJob(); final Semaphore semaphore = new Semaphore(0); printJobXmlRepresentation(job);/*from ww w . ja v a 2 s . c o m*/ final MutableBoolean taskHasBeenInError = new MutableBoolean(false); final MutableBoolean restartedFromErrorEventReceived = new MutableBoolean(false); SchedulerEventListenerExtended listener = new SchedulerEventListenerExtended() { @Override public void schedulerStateUpdatedEvent(SchedulerEvent eventType) { System.out.println("RestSmartProxyTest.schedulerStateUpdatedEvent " + eventType); } @Override public void jobSubmittedEvent(JobState job) { System.out.println("RestSmartProxyTest.jobSubmittedEvent"); } @Override public void jobStateUpdatedEvent(NotificationData<JobInfo> notification) { JobStatus status = notification.getData().getStatus(); System.out.println("RestSmartProxyTest.jobStateUpdatedEvent, eventType=" + notification.getEventType() + ", jobStatus=" + status); if (notification.getEventType() == SchedulerEvent.JOB_RESTARTED_FROM_ERROR) { restartedFromErrorEventReceived.setTrue(); } if (status == JobStatus.IN_ERROR) { semaphore.release(); } } @Override public void taskStateUpdatedEvent(NotificationData<TaskInfo> notification) { TaskStatus status = notification.getData().getStatus(); System.out.println("RestSmartProxyTest.taskStateUpdatedEvent, taskStatus=" + status); if (status == TaskStatus.WAITING_ON_ERROR || status == TaskStatus.IN_ERROR) { // IN_ERROR previously taskHasBeenInError.setTrue(); } } @Override public void usersUpdatedEvent(NotificationData<UserIdentification> notification) { System.out.println("RestSmartProxyTest.usersUpdatedEvent " + notification.getData()); } @Override public void pullDataFinished(String jobId, String taskName, String localFolderPath) { System.out.println("RestSmartProxyTest.pullDataFinished"); } @Override public void pullDataFailed(String jobId, String taskName, String remoteFolder_URL, Throwable t) { System.out.println("RestSmartProxyTest.pullDataFailed"); } @Override public void jobUpdatedFullDataEvent(JobState job) { System.out.println("RestSmartProxyTest.jobUpdatedFullDataEvent"); } }; restSmartProxy.addEventListener(listener); JobId jobId = restSmartProxy.submit(job, inputLocalFolder.getAbsolutePath(), pushUrl, outputLocalFolder.getAbsolutePath(), pullUrl, false, false); // the next line blocks until jobStateUpdatedEvent is called on the // listener // with job status set to IN_ERROR semaphore.acquire(); String jobIdAsString = jobId.value(); System.out.println("Restarting all In-Error tasks"); restSmartProxy.restartAllInErrorTasks(jobIdAsString); assertThat(restartedFromErrorEventReceived.booleanValue()).isTrue(); assertThat(taskHasBeenInError.booleanValue()).isTrue(); }
From source file:com.smartitengineering.loadtest.engine.impl.LoadTestEngineImpl.java
private void initializeInternals(String testName) { engineBatchListener = new EngineBatchListener(); semaphore = new Semaphore(getPermits()); transitionListener = new TestCaseStateTransitionMonitor(); caseStateChangeListener = new TestCaseStateListenerImpl(); caseRecords = new HashMap<TestCase, UnitTestInstanceRecord>(); setTestName(testName);/*from w ww.j a va 2 s .c o m*/ addTestCaseTransitionListener(transitionListener); }