Java Code Examples for java.util.concurrent.CountDownLatch#getCount()

The following examples show how to use java.util.concurrent.CountDownLatch#getCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectorTraverserTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testTraverseNoRetry() throws Exception {
  setupConfig(Collections.emptyMap());
  final CountDownLatch countDown = new CountDownLatch(1);
  IndexingConnector failedOnce =
      new AbstractConnector() {
        @Override
        public void traverse() throws IOException, InterruptedException {
          if (countDown.getCount() == 0) {
            fail("Unexpected traverse call.");
          }
          countDown.countDown();
          throw new IOException("Don't call me again.");
        }
      };
  ConnectorTraverser traverser = new ConnectorTraverser.Builder()
      .setConnector(failedOnce)
      .setContext(getContextWithExceptionHandler(1))
      .build();
  traverser.start();
  assertTrue(countDown.await(30, TimeUnit.SECONDS));
  traverser.stop();
}
 
Example 2
Source File: Server.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void stop(long time)
{
  if (time < 0) {
    throw new IllegalArgumentException(String.format("Wait time %d can't be negative", time));
  }
  final CountDownLatch latch = new CountDownLatch(1);
  this.latch = latch;
  eventloop.stop(this);
  final long deadline = System.currentTimeMillis() + time;
  try {
    while (latch.getCount() != 0 && time > 0 && latch.await(time, TimeUnit.MILLISECONDS)) {
      time = deadline - System.currentTimeMillis();
    }
  } catch (InterruptedException e) {
    throw new RuntimeException(e);
  } finally {
    shutdownExecutors(latch.getCount() == 0);
  }
}
 
Example 3
Source File: ConnectorSchedulerTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testTraverseRetryAndSuccess() throws Exception {
  setupConfig(Collections.emptyMap());
  final CountDownLatch counter = new CountDownLatch(4);
  final AtomicBoolean success = new AtomicBoolean(false);
  Connector<ConnectorContext> fail3Times =
      new AbstractConnector() {
        @Override
        public void traverse() throws IOException, InterruptedException {
          assertFalse(success.get());
          if (counter.getCount() == 0) {
            fail("Unexpected traverse call.");
          }
          // Fail for 3 times before success
          if (counter.getCount() > 1) {
            counter.countDown();
            throw new IOException("Try 3 times");
          }
          success.set(true);
          counter.countDown();
        }
      };
  ConnectorScheduler<ConnectorContext> traverser =
      new ConnectorScheduler.Builder()
          .setConnector(fail3Times)
          .setContext(getContextWithExceptionHandler(4))
          .build();
  traverser.start();
  assertTrue(counter.await(30, TimeUnit.SECONDS));
  assertTrue(success.get());
  traverser.stop();
}
 
Example 4
Source File: IncomingSubscribeHandlerTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 5000)
public void test_read_subscribe_context_has_interceptors_timeouts_failure() throws Exception {

    final ClientContextImpl clientContext = new ClientContextImpl(hiveMQExtensions, new ModifiableDefaultPermissionsImpl());

    final List<SubscribeInboundInterceptor> isolatedInterceptors = getIsolatedInterceptor();

    clientContext.addSubscribeInboundInterceptor(isolatedInterceptors.get(2));

    channel.attr(ChannelAttributes.PLUGIN_CLIENT_CONTEXT).set(clientContext);
    channel.attr(ChannelAttributes.MQTT_VERSION).set(ProtocolVersion.MQTTv5);

    final CountDownLatch subackLatch = new CountDownLatch(1);

    channel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {

            if (msg instanceof SUBACK && ((SUBACK) msg).getReasonCodes().get(0).equals(Mqtt5SubAckReasonCode.UNSPECIFIED_ERROR)) {
                subackLatch.countDown();
            }

            super.write(ctx, msg, promise);
        }
    });

    when(hiveMQExtensions.getExtensionForClassloader(any(IsolatedPluginClassloader.class))).thenReturn(plugin);

    channel.writeInbound(new SUBSCRIBE(1, new Topic("topic", QoS.AT_LEAST_ONCE, true, true, Mqtt5RetainHandling.SEND, 1)));

    while (subackLatch.getCount() != 0) {
        channel.runPendingTasks();
        channel.runScheduledPendingTasks();
    }
    assertTrue(subackLatch.await(5, TimeUnit.SECONDS));
}
 
Example 5
Source File: DefaultLifecycleProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void stop() {
	if (this.members.isEmpty()) {
		return;
	}
	if (logger.isDebugEnabled()) {
		logger.debug("Stopping beans in phase " + this.phase);
	}
	this.members.sort(Collections.reverseOrder());
	CountDownLatch latch = new CountDownLatch(this.smartMemberCount);
	Set<String> countDownBeanNames = Collections.synchronizedSet(new LinkedHashSet<>());
	Set<String> lifecycleBeanNames = new HashSet<>(this.lifecycleBeans.keySet());
	for (LifecycleGroupMember member : this.members) {
		if (lifecycleBeanNames.contains(member.name)) {
			doStop(this.lifecycleBeans, member.name, latch, countDownBeanNames);
		}
		else if (member.bean instanceof SmartLifecycle) {
			// Already removed: must have been a dependent bean from another phase
			latch.countDown();
		}
	}
	try {
		latch.await(this.timeout, TimeUnit.MILLISECONDS);
		if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isInfoEnabled()) {
			logger.info("Failed to shut down " + countDownBeanNames.size() + " bean" +
					(countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
					this.phase + " within timeout of " + this.timeout + ": " + countDownBeanNames);
		}
	}
	catch (InterruptedException ex) {
		Thread.currentThread().interrupt();
	}
}
 
Example 6
Source File: FailbackRegistryTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
     * Test method for
     * {@link com.alibaba.dubbo.registry.support.FailbackRegistry#retry()}.
     *
     * @throws Exception
     */
    @Test
    public void testDoRetry() throws Exception {

        final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
        final CountDownLatch latch = new CountDownLatch(3);//All of them are called 3 times. Successful attempts to reduce the failure of 1. subscribe register will not be done again

        NotifyListener listner = new NotifyListener() {
            @Override
            public void notify(List<URL> urls) {
                notified.set(Boolean.TRUE);
            }
        };
        registry = new MockRegistry(registryUrl, latch);
        registry.setBad(true);
        registry.register(serviceUrl);
        registry.unregister(serviceUrl);
        registry.subscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);
        registry.unsubscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);

        //Failure can not be called to listener.
        assertEquals(false, notified.get());
        assertEquals(3, latch.getCount());

        registry.setBad(false);

        for (int i = 0; i < trytimes; i++) {
            System.out.println("failback registry retry ,times:" + i);
            //System.out.println(latch.getCount());
            if (latch.getCount() == 0)
                break;
            Thread.sleep(sleeptime);
        }
//        Thread.sleep(100000);//for debug
        assertEquals(0, latch.getCount());
        //The failedsubcribe corresponding key will be cleared when unsubscribing
        assertEquals(false, notified.get());
    }
 
Example 7
Source File: TestThreadParkEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {
    final CountDownLatch stop = new CountDownLatch(1);
    final Blocker blocker = new Blocker();
    TestThread parkThread = new TestThread(new Runnable() {
        public void run() {
            while (stop.getCount() > 0) {
                LockSupport.park(blocker);
            }
        }
    });

    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS));
    try {
        recording.start();
        parkThread.start();
        ThreadMXBeanTool.waitUntilBlockingOnObject(parkThread, Thread.State.WAITING, blocker);
        // sleep so we know the event is recorded
        Thread.sleep(2 * THRESHOLD_MILLIS);
    } finally {
        stop.countDown();
        LockSupport.unpark(parkThread);
        parkThread.join();
        recording.stop();
    }

    List<RecordedEvent> events = Events.fromRecording(recording);
    Events.hasEvents(events);
    boolean isAnyFound = false;
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
        String klassName = Events.assertField(event, "parkedClass.name").notNull().getValue();
        if (klassName.equals(blocker.getClass().getName().replace('.', '/'))) {
            assertFalse(isAnyFound, "Found more than 1 event");
            isAnyFound = true;
            Events.assertField(event, "timeout").equal(0L);
            Events.assertField(event, "address").notEqual(0L);
            Events.assertEventThread(event, parkThread);
        }
    }
    assertTrue(isAnyFound, "Correct event not found");
}
 
Example 8
Source File: ProcessTools.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Starts a process from its builder.</p>
 * <span>The default redirects of STDOUT and STDERR are started</span>
 * <p>
 * It is possible to wait for the process to get to a warmed-up state
 * via {@linkplain Predicate} condition on the STDOUT
 * </p>
 * @param name The process name
 * @param processBuilder The process builder
 * @param linePredicate The {@linkplain Predicate} to use on the STDOUT
 *                      Used to determine the moment the target app is
 *                      properly warmed-up.
 *                      It can be null - in that case the warmup is skipped.
 * @param timeout The timeout for the warmup waiting
 * @param unit The timeout {@linkplain TimeUnit}
 * @return Returns the initialized {@linkplain Process}
 * @throws IOException
 * @throws InterruptedException
 * @throws TimeoutException
 */
public static Process startProcess(String name,
                                   ProcessBuilder processBuilder,
                                   final Predicate<String> linePredicate,
                                   long timeout,
                                   TimeUnit unit)
throws IOException, InterruptedException, TimeoutException {
    Process p = processBuilder.start();
    StreamPumper stdout = new StreamPumper(p.getInputStream());
    StreamPumper stderr = new StreamPumper(p.getErrorStream());

    stdout.addPump(new LineForwarder(name, System.out));
    stderr.addPump(new LineForwarder(name, System.err));
    CountDownLatch latch = new CountDownLatch(1);
    if (linePredicate != null) {
        StreamPumper.LinePump pump = new StreamPumper.LinePump() {
            @Override
            protected void processLine(String line) {
                if (latch.getCount() > 0 && linePredicate.test(line)) {
                    latch.countDown();
                }
            }
        };
        stdout.addPump(pump);
        stderr.addPump(pump);
    }
    Future<Void> stdoutTask = stdout.process();
    Future<Void> stderrTask = stderr.process();

    try {
        if (timeout > -1) {
            long realTimeout = Math.round(timeout * Utils.TIMEOUT_FACTOR);
            if (!latch.await(realTimeout, unit)) {
                throw new TimeoutException();
            }
        }
    } catch (TimeoutException | InterruptedException e) {
        System.err.println("Failed to start a process (thread dump follows)");
        for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
            printStack(s.getKey(), s.getValue());
        }
        stdoutTask.cancel(true);
        stderrTask.cancel(true);
        throw e;
    }

    return p;
}
 
Example 9
Source File: AbstractTaskQueueTest.java    From besu with Apache License 2.0 4 votes vote down vote up
@Test
public void handlesConcurrentQueuing() throws Exception {
  final int threadCount = 5;
  final int itemsPerThread = 1000;
  final T queue = createQueue();

  final CountDownLatch dequeueingFinished = new CountDownLatch(1);
  final CountDownLatch queuingFinished = new CountDownLatch(threadCount);

  // Start thread for reading values
  final List<Task<Bytes>> dequeued = new ArrayList<>();
  final Thread reader =
      new Thread(
          () -> {
            while (queuingFinished.getCount() > 0 || !queue.isEmpty()) {
              if (!queue.isEmpty()) {
                final Task<Bytes> value = queue.remove();
                value.markCompleted();
                dequeued.add(value);
              }
            }
            dequeueingFinished.countDown();
          });
  reader.start();

  final Function<Bytes, Thread> queueingThreadFactory =
      (value) ->
          new Thread(
              () -> {
                try {
                  for (int i = 0; i < itemsPerThread; i++) {
                    queue.add(value);
                  }
                } finally {
                  queuingFinished.countDown();
                }
              });

  // Start threads to queue values
  for (int i = 0; i < threadCount; i++) {
    queueingThreadFactory.apply(Bytes.of(i)).start();
  }

  queuingFinished.await();
  dequeueingFinished.await();

  assertThat(dequeued.size()).isEqualTo(threadCount * itemsPerThread);
  assertThat(dequeued.stream().filter(Objects::isNull).count()).isEqualTo(0);
  assertThat(queue.size()).isEqualTo(0);
}
 
Example 10
Source File: RouteGuideClient.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Async client-streaming example. Sends {@code numPoints} randomly chosen points from {@code
 * features} with a variable delay in between. Prints the statistics when they are sent from the
 * server.
 */
public void recordRoute(List<Feature> features, int numPoints) throws InterruptedException {
  info("*** RecordRoute");
  final CountDownLatch finishLatch = new CountDownLatch(1);
  StreamObserver<RouteSummary> responseObserver = new StreamObserver<RouteSummary>() {
    @Override
    public void onNext(RouteSummary summary) {
      info("Finished trip with {0} points. Passed {1} features. "
          + "Travelled {2} meters. It took {3} seconds.", summary.getPointCount(),
          summary.getFeatureCount(), summary.getDistance(), summary.getElapsedTime());
      if (testHelper != null) {
        testHelper.onMessage(summary);
      }
    }

    @Override
    public void onError(Throwable t) {
      warning("RecordRoute Failed: {0}", Status.fromThrowable(t));
      if (testHelper != null) {
        testHelper.onRpcError(t);
      }
      finishLatch.countDown();
    }

    @Override
    public void onCompleted() {
      info("Finished RecordRoute");
      finishLatch.countDown();
    }
  };

  StreamObserver<Point> requestObserver = asyncStub.recordRoute(responseObserver);
  try {
    // Send numPoints points randomly selected from the features list.
    for (int i = 0; i < numPoints; ++i) {
      int index = random.nextInt(features.size());
      Point point = features.get(index).getLocation();
      info("Visiting point {0}, {1}", RouteGuideUtil.getLatitude(point),
          RouteGuideUtil.getLongitude(point));
      requestObserver.onNext(point);
      // Sleep for a bit before sending the next one.
      Thread.sleep(random.nextInt(1000) + 500);
      if (finishLatch.getCount() == 0) {
        // RPC completed or errored before we finished sending.
        // Sending further requests won't error, but they will just be thrown away.
        return;
      }
    }
  } catch (RuntimeException e) {
    // Cancel RPC
    requestObserver.onError(e);
    throw e;
  }
  // Mark the end of requests
  requestObserver.onCompleted();

  // Receiving happens asynchronously
  if (!finishLatch.await(1, TimeUnit.MINUTES)) {
    warning("recordRoute can not finish within 1 minutes");
  }
}
 
Example 11
Source File: ConnectorTraverserTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testIncrementalTraversalRetryAndSuccess() throws Exception {
  final CountDownLatch latch = new CountDownLatch(4);
  final AtomicBoolean success = new AtomicBoolean(false);
  IncrementalConnector incremental =
      new IncrementalConnector() {

        @Override
        public void traverse() throws IOException, InterruptedException {
          throw new UnsupportedOperationException("Traversal disabled on start");
        }

        @Override
        public void handleIncrementalChanges() throws IOException, InterruptedException {
          if (latch.getCount() == 0) {
            fail("Unexpected incremental traverse call.");
          }
          try {
            if (latch.getCount() > 1) {
              throw new IOException("Service unavailable");
            } else {
              success.set(true);
            }
          } finally {
            latch.countDown();
          }
        }
      };
  setupConfig(Collections.singletonMap("schedule.performTraversalOnStart", "false"));
  IndexingConnectorContext context =
      new IndexingConnectorContextImpl.Builder()
          .setIndexingService(mockIndexingService)
          .setIncrementalTraversalExceptionHandler(new RetryExceptionHandler(4))
          .build();
  ConnectorTraverser traverser = new ConnectorTraverser.Builder()
      .setConnector(incremental)
      .setContext(context)
      .build();
  traverser.start();
  assertTrue(latch.await(30, TimeUnit.SECONDS));
  assertTrue(success.get());
  traverser.stop();
}
 
Example 12
Source File: HCSMAPITopicSampler.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
/**
 * Runs single load test thread by calling gRPC Consensus service subscribeTopic endpoint. Success is dependant on
 * StreamObserver observing the expected count for historic and incoming messages in the allotted time Returns
 * SamplerResult to client
 *
 * @param messageListener listener properties
 * @return Sampler result representing success and observed message counts
 */
@Override
public HCSSamplerResult subscribeTopic(MessageListener messageListener) {
    log.info("Subscribing to topic using MAPI with {}, {}", () -> TextFormat
            .shortDebugString(request), () -> messageListener);

    CountDownLatch historicMessagesLatch = new CountDownLatch(messageListener.getHistoricMessagesCount());
    CountDownLatch incomingMessagesLatch = new CountDownLatch(messageListener.getFutureMessagesCount());
    TopicID topicId = request.getTopicID();
    HCSMAPISamplerResult result = HCSMAPISamplerResult
            .builder()
            .realmNum(topicId.getRealmNum())
            .topicNum(topicId.getTopicNum())
            .success(true)
            .build();
    MirrorSubscriptionHandle subscription = null;
    ScheduledExecutorService scheduler = null;

    try {
        subscription = mirrorConsensusTopicQuery
                .subscribe(mirrorClient,
                        resp -> {
                            result.onNext(resp);
                            if (result.isHistorical()) {
                                historicMessagesLatch.countDown();
                            } else {
                                incomingMessagesLatch.countDown();
                            }
                        },
                        result::onError);

        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(() -> {
            result.printProgress();
        }, 0, 1, TimeUnit.MINUTES);

        // await some new messages
        if (!historicMessagesLatch.await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("Historic messages latch count is {}, did not reach zero", historicMessagesLatch.getCount());
            result.setSuccess(false);
        }

        if (historicMessagesLatch.getCount() == 0 && !incomingMessagesLatch
                .await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("incomingMessagesLatch count is {}, did not reach zero", incomingMessagesLatch.getCount());
            result.setSuccess(false);
        }

        result.onComplete();
    } catch (Exception ex) {
        log.error("Error subscribing to topic", ex);
        throw ex;
    } finally {
        if (subscription != null) {
            subscription.unsubscribe();
            log.info("Unsubscribed from {}", subscription);
        }

        scheduler.shutdownNow();

        return result;
    }
}
 
Example 13
Source File: ShutDownFitbitGattTest.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void testShutDown() throws InterruptedException {
    FitbitGatt fitbitGatt = FitbitGatt.getInstance();
    CountDownLatch cdl = new CountDownLatch(3);

    NoOpGattCallback cb = spy(new NoOpGattCallback() {

        @Override
        public void onGattClientStarted() {
            super.onGattClientStarted();
            cdl.countDown();
        }

        @Override
        public void onScanStarted() {
            super.onScanStarted();
            cdl.countDown();
        }

        @Override
        public void onGattServerStarted(GattServerConnection serverConnection) {
            super.onGattServerStarted(serverConnection);
            cdl.countDown();
        }
    });

    ArrayList<String> filters = new ArrayList<String>();
    filters.add("TEST");

    fitbitGatt.registerGattEventListener(cb);
    fitbitGatt.initializeScanner(context);
    //replace it with a spy
    fitbitGatt.startGattClient(context);

    //swapping instances to verify interactions
    PeripheralScanner peripheralScanner = fitbitGatt.getPeripheralScanner();
    PeripheralScanner scanner = spy(peripheralScanner);
    fitbitGatt.setPeripheralScanner(scanner);
    fitbitGatt.setDeviceNameScanFilters(filters);
    fitbitGatt.startPeriodicScan(context);
    fitbitGatt.startGattServer(context);

    cdl.await(10, TimeUnit.SECONDS);

    if (cdl.getCount() != 0) {
        fail("Failed to start fully remaining " + cdl.getCount());
    }

    //start verifications
    assertNotNull(fitbitGatt.getServer());
    assertNotNull(fitbitGatt.getClientCallback());
    assertNotNull(fitbitGatt.getServerCallback());
    assertTrue(fitbitGatt.isScanning());


    fitbitGatt.shutdown();

    //verify start interactions
    InOrder callbackOrder = inOrder(cb);
    InOrder scannerOder = inOrder(scanner);
    callbackOrder.verify(cb).onGattClientStarted();
    callbackOrder.verify(cb).onScanStarted();
    verify(cb).onBluetoothPeripheralDiscovered(any());
    callbackOrder.verify(cb).onGattServerStarted(any());

    scannerOder.verify(scanner, times(1)).setDeviceNameFilters(filters);
    //one assertion and one start
    scannerOder.verify(scanner, times(1)).startPeriodicScan(context);


    //when we cancel
    scannerOder.verify(scanner, times(1)).isScanning();
    assertNull(fitbitGatt.getServer());
    assertNull(fitbitGatt.getClientCallback());
    assertNull(fitbitGatt.getServerCallback());
    assertFalse(fitbitGatt.isInitialized());
    assertFalse(fitbitGatt.isScanning());

    //verify shutdown interactions
    scannerOder.verify(scanner, times(1)).onDestroy(context.getApplicationContext());
    scannerOder.verify(scanner, times(1)).cancelPeriodicalScan(context.getApplicationContext());
    scannerOder.verify(scanner, times(1)).isScanning();
    scannerOder.verify(scanner, times(1)).cancelScan(context.getApplicationContext());
    scannerOder.verify(scanner, times(1)).stopScan(context.getApplicationContext());

    verifyNoMoreInteractions(cb);
    verifyNoMoreInteractions(scanner);
}
 
Example 14
Source File: ConnectorSchedulerTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testIncrementalTraversalRetryAndSuccess() throws Exception {
  final CountDownLatch latch = new CountDownLatch(4);
  final AtomicBoolean success = new AtomicBoolean(false);
  IncrementalConnector incremental =
      new IncrementalConnector() {

        @Override
        public void traverse() throws IOException, InterruptedException {
          throw new UnsupportedOperationException("Traversal disabled on start");
        }

        @Override
        public void handleIncrementalChanges() throws IOException, InterruptedException {
          if (latch.getCount() == 0) {
            fail("Unexpected incremental traverse call.");
          }
          try {
            if (latch.getCount() > 1) {
              throw new IOException("Service unavailable");
            } else {
              success.set(true);
            }
          } finally {
            latch.countDown();
          }
        }
      };
  setupConfig(Collections.singletonMap("schedule.performTraversalOnStart", "false"));
  ConnectorContext context =
      new ConnectorContextImpl.Builder()
          .setIncrementalTraversalExceptionHandler(new RetryExceptionHandler(4))
          .build();
  ConnectorScheduler<ConnectorContext> traverser =
      new ConnectorScheduler.Builder().setConnector(incremental).setContext(context).build();
  traverser.start();
  assertTrue(latch.await(30, TimeUnit.SECONDS));
  assertTrue(success.get());
  traverser.stop();
  assertEquals(
      1,
      StatsManager.getInstance()
          .getComponent("IncrementalTraverser")
          .getSuccessCount("complete"));
}
 
Example 15
Source File: HCSDirectStubTopicSampler.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
/**
 * Runs single load test thread by calling gRPC Consensus service subscribeTopic endpoint. Success is dependant on
 * StreamObserver observing the expected count for historic and incoming messages in the allotted time Returns
 * SamplerResult to client
 *
 * @param messageListener listener properties
 * @return Sampler result representing success and observed message counts
 */
@Override
public HCSSamplerResult subscribeTopic(MessageListener messageListener) throws InterruptedException {
    log.info("Subscribing to topic with {}, {}", () -> TextFormat.shortDebugString(request), () -> messageListener);

    CountDownLatch historicMessagesLatch = new CountDownLatch(messageListener.getHistoricMessagesCount());
    CountDownLatch incomingMessagesLatch = new CountDownLatch(messageListener.getFutureMessagesCount());
    TopicID topicId = request.getTopicID();
    HCSDirectStubSamplerResult result = HCSDirectStubSamplerResult
            .builder()
            .realmNum(topicId.getRealmNum())
            .topicNum(topicId.getTopicNum())
            .success(true)
            .build();
    StreamObserver<ConsensusTopicResponse> responseObserver = new StreamObserver<>() {

        @Override
        public void onNext(ConsensusTopicResponse response) {
            result.onNext(response);

            if (result.isHistorical()) {
                historicMessagesLatch.countDown();
            } else {
                incomingMessagesLatch.countDown();
            }
        }

        @SneakyThrows
        @Override
        public void onError(Throwable t) {
            throw t;
        }

        @Override
        public void onCompleted() {
            result.onComplete();
        }
    };
    ScheduledExecutorService scheduler = null;

    try {
        asyncStub.subscribeTopic(request, responseObserver);
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(() -> {
            result.printProgress();
        }, 0, 1, TimeUnit.MINUTES);

        // await some new messages
        if (!historicMessagesLatch.await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("Historic messages latch count is {}, did not reach zero", historicMessagesLatch.getCount());
            result.setSuccess(false);
        }

        if (historicMessagesLatch.getCount() == 0 && !incomingMessagesLatch
                .await(messageListener.getMessagesLatchWaitSeconds(), TimeUnit.SECONDS)) {
            log.error("incomingMessagesLatch count is {}, did not reach zero", incomingMessagesLatch.getCount());
            result.setSuccess(false);
        }
    } catch (Exception ex) {
        log.error("Error subscribing to topic", ex);
        throw ex;
    } finally {
        responseObserver.onCompleted();

        if (canShutdownChannel) {
            shutdown();
        }

        scheduler.shutdownNow();

        return result;
    }
}
 
Example 16
Source File: RecordWriterTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests a fix for FLINK-2089.
 *
 * @see <a href="https://issues.apache.org/jira/browse/FLINK-2089">FLINK-2089</a>
 */
@Test
public void testClearBuffersAfterInterruptDuringBlockingBufferRequest() throws Exception {
	ExecutorService executor = null;

	try {
		executor = Executors.newSingleThreadExecutor();

		final CountDownLatch sync = new CountDownLatch(2);

		final TrackingBufferRecycler recycler = new TrackingBufferRecycler();

		final MemorySegment memorySegment = MemorySegmentFactory.allocateUnpooledSegment(4);

		// Return buffer for first request, but block for all following requests.
		Answer<BufferBuilder> request = new Answer<BufferBuilder>() {
			@Override
			public BufferBuilder answer(InvocationOnMock invocation) throws Throwable {
				sync.countDown();

				if (sync.getCount() == 1) {
					return new BufferBuilder(memorySegment, recycler);
				}

				final Object o = new Object();
				synchronized (o) {
					while (true) {
						o.wait();
					}
				}
			}
		};

		BufferProvider bufferProvider = mock(BufferProvider.class);
		when(bufferProvider.requestBufferBuilderBlocking()).thenAnswer(request);

		ResultPartitionWriter partitionWriter = new RecyclingPartitionWriter(bufferProvider);

		final RecordWriter<IntValue> recordWriter = new RecordWriterBuilder().build(partitionWriter);

		Future<?> result = executor.submit(new Callable<Void>() {
			@Override
			public Void call() throws Exception {
				IntValue val = new IntValue(0);

				try {
					recordWriter.emit(val);
					recordWriter.flushAll();

					recordWriter.emit(val);
				}
				catch (InterruptedException e) {
					recordWriter.clearBuffers();
				}

				return null;
			}
		});

		sync.await();

		// Interrupt the Thread.
		//
		// The second emit call requests a new buffer and blocks the thread.
		// When interrupting the thread at this point, clearing the buffers
		// should not recycle any buffer.
		result.cancel(true);

		recordWriter.clearBuffers();

		// Verify that buffer have been requested twice
		verify(bufferProvider, times(2)).requestBufferBuilderBlocking();

		// Verify that the written out buffer has only been recycled once
		// (by the partition writer).
		assertEquals(1, recycler.getRecycledMemorySegments().size());
		assertEquals(memorySegment, recycler.getRecycledMemorySegments().get(0));
	}
	finally {
		if (executor != null) {
			executor.shutdown();
		}
	}
}
 
Example 17
Source File: NioEndpoint.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected CountDownLatch startLatch(CountDownLatch latch, int cnt) {
    if ( latch == null || latch.getCount() == 0 ) {
        return new CountDownLatch(cnt);
    }
    else throw new IllegalStateException("Latch must be at count 0 or null.");
}
 
Example 18
Source File: IncomingSubscribeHandlerTest.java    From hivemq-community-edition with Apache License 2.0 3 votes vote down vote up
@Test(timeout = 5000)
public void test_read_subscribe_context_has_interceptors_timeouts_failure_mqtt3() throws Exception {

    final ClientContextImpl clientContext = new ClientContextImpl(hiveMQExtensions, new ModifiableDefaultPermissionsImpl());

    final List<SubscribeInboundInterceptor> isolatedInterceptors = getIsolatedInterceptor();

    clientContext.addSubscribeInboundInterceptor(isolatedInterceptors.get(2));

    channel.attr(ChannelAttributes.PLUGIN_CLIENT_CONTEXT).set(clientContext);
    channel.attr(ChannelAttributes.MQTT_VERSION).set(ProtocolVersion.MQTTv3_1_1);

    final CountDownLatch subackLatch = new CountDownLatch(1);

    channel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {

            if (msg instanceof SUBACK && ((SUBACK) msg).getReasonCodes().get(0).equals(Mqtt5SubAckReasonCode.UNSPECIFIED_ERROR)) {
                subackLatch.countDown();
            }

            super.write(ctx, msg, promise);
        }
    });


    when(hiveMQExtensions.getExtensionForClassloader(any(IsolatedPluginClassloader.class))).thenReturn(plugin);

    channel.writeInbound(new SUBSCRIBE(1, new Topic("topic", QoS.AT_LEAST_ONCE, true, true, Mqtt5RetainHandling.SEND, 1)));

    while (subackLatch.getCount() != 0) {
        channel.runPendingTasks();
        channel.runScheduledPendingTasks();
    }

    assertTrue(subackLatch.await(5, TimeUnit.SECONDS));
}
 
Example 19
Source File: PluginAuthorizerServiceImplTest.java    From hivemq-community-edition with Apache License 2.0 3 votes vote down vote up
@Test(timeout = 2000)
public void test_publish_auth_provider_empty_default_processing() throws Exception {

    when(authorizers.areAuthorizersAvailable()).thenReturn(true);
    when(authorizers.getAuthorizerProviderMap()).thenReturn(new HashMap<>());

    final PUBLISH publish = TestMessageUtil.createMqtt5Publish("topic", QoS.AT_LEAST_ONCE);

    clearHandlers();

    pluginAuthorizerService.authorizePublish(channelHandlerContext, publish);

    final CountDownLatch latch = new CountDownLatch(1);

    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(incomingPublishService).processPublish(channelHandlerContext, publish, null);

    while (latch.getCount() != 0) {
        channel.runPendingTasks();
        channel.runScheduledPendingTasks();
    }

    assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));

}
 
Example 20
Source File: IncomingSubscribeHandlerTest.java    From hivemq-community-edition with Apache License 2.0 3 votes vote down vote up
@Test(timeout = 5000)
public void test_read_subscribe_context_has_interceptors_throws_exception() throws Exception {

    final ClientContextImpl clientContext = new ClientContextImpl(hiveMQExtensions, new ModifiableDefaultPermissionsImpl());

    final List<SubscribeInboundInterceptor> isolatedInterceptors = getIsolatedInterceptor();

    clientContext.addSubscribeInboundInterceptor(isolatedInterceptors.get(1));

    channel.attr(ChannelAttributes.PLUGIN_CLIENT_CONTEXT).set(clientContext);
    channel.attr(ChannelAttributes.MQTT_VERSION).set(ProtocolVersion.MQTTv5);

    final CountDownLatch subackLatch = new CountDownLatch(1);

    channel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
        @Override
        public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {

            if (msg instanceof SUBACK && ((SUBACK) msg).getReasonCodes().get(0).equals(Mqtt5SubAckReasonCode.UNSPECIFIED_ERROR)) {
                subackLatch.countDown();
            }

            super.write(ctx, msg, promise);
        }
    });

    when(hiveMQExtensions.getExtensionForClassloader(any(IsolatedPluginClassloader.class))).thenReturn(plugin);

    channel.writeInbound(new SUBSCRIBE(1, new Topic("topic", QoS.AT_LEAST_ONCE, true, true, Mqtt5RetainHandling.SEND, 1)));

    while (subackLatch.getCount() != 0) {
        channel.runPendingTasks();
        channel.runScheduledPendingTasks();
    }

    assertTrue(subackLatch.await(5, TimeUnit.SECONDS));

}