gpt4 book ai didi

org.testcontainers.containers.wait.strategy.WaitStrategyTarget类的使用及代码示例

转载 作者:知者 更新时间:2024-03-27 08:31:05 27 4
gpt4 key购买 nike

本文整理了Java中org.testcontainers.containers.wait.strategy.WaitStrategyTarget类的一些代码示例,展示了WaitStrategyTarget类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WaitStrategyTarget类的具体详情如下:
包路径:org.testcontainers.containers.wait.strategy.WaitStrategyTarget
类名称:WaitStrategyTarget

WaitStrategyTarget介绍

暂无

代码示例

代码示例来源:origin: testcontainers/testcontainers-java

@Override
protected void waitUntilReady() {
  WaitingConsumer waitingConsumer = new WaitingConsumer();
  LogUtils.followOutput(DockerClientFactory.instance().client(), waitStrategyTarget.getContainerId(), waitingConsumer);
  Predicate<OutputFrame> waitPredicate = outputFrame ->
    // (?s) enables line terminator matching (equivalent to Pattern.DOTALL)
    outputFrame.getUtf8String().matches("(?s)" + regEx);
  try {
    waitingConsumer.waitUntil(waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, times);
  } catch (TimeoutException e) {
    throw new ContainerLaunchException("Timed out waiting for log output matching '" + regEx + "'");
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

@Override
protected void waitUntilReady() {
  final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
  if (externalLivenessCheckPorts.isEmpty()) {
    log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName());
    return;
  }
  @SuppressWarnings("unchecked")
  List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
  final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts);
  Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts);
  Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts);
  try {
    Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS,
      () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call()));
  } catch (TimeoutException e) {
    throw new ContainerLaunchException("Timed out waiting for container port to open (" +
        waitStrategyTarget.getContainerIpAddress() +
        " ports: " +
        externalLivenessCheckPorts +
        " should be listening)");
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

/**
   * @return the ports on which to check if the container is ready
   */
  default Set<Integer> getLivenessCheckPortNumbers() {
    final Set<Integer> result = getExposedPorts().stream()
      .map(this::getMappedPort).distinct().collect(Collectors.toSet());
    result.addAll(getBoundPortNumbers());
    return result;
  }
}

代码示例来源:origin: Playtika/testcontainers-spring-boot

@Override
protected boolean isReady() {
  String containerId = waitStrategyTarget.getContainerId();
  log.debug("Check Aerospike container {} status", containerId);
  InspectContainerResponse containerInfo = waitStrategyTarget.getContainerInfo();
  if (containerInfo == null) {
    log.debug("Aerospike container[{}] doesn't contain info. Abnormal situation, should not happen.", containerId);
    return false;
  }
  int port = getMappedPort(containerInfo.getNetworkSettings(), properties.port);
  String host = DockerClientFactory.instance().dockerHostIpAddress();
  //TODO: Remove dependency to client https://www.aerospike.com/docs/tools/asmonitor/common_tasks.html
  try (AerospikeClient client = new AerospikeClient(host, port)) {
    return client.isConnected();
  } catch (AerospikeException.Connection e) {
    log.debug("Aerospike container: {} not yet started. {}", containerId, e.getMessage());
  }
  return false;
}

代码示例来源:origin: testcontainers/testcontainers-java

private void tryPort(Integer internalPort) {
    String[][] commands = {
        {"/bin/sh", "-c", format("cat /proc/net/tcp{,6} | awk '{print $2}' | grep -i :%x && echo %s", internalPort, SUCCESS_MARKER)},
        {"/bin/sh", "-c", format("nc -vz -w 1 localhost %d && echo %s", internalPort, SUCCESS_MARKER)},
        {"/bin/bash", "-c", format("</dev/tcp/localhost/%d && echo %s", internalPort, SUCCESS_MARKER)}
    };

    for (String[] command : commands) {
      try {
        if (ExecInContainerPattern.execInContainer(waitStrategyTarget.getContainerInfo(), command).getStdout().contains(SUCCESS_MARKER)) {
          return;
        }
      } catch (Exception e) {
        throw new IllegalStateException(e);
      }
    }

    throw new IllegalStateException("Socket not listening yet: " + internalPort);
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

/**
 * Build the URI on which to check if the container is ready.
 *
 * @param livenessCheckPort the liveness port
 * @return the liveness URI
 */
private URI buildLivenessUri(int livenessCheckPort) {
  final String scheme = (tlsEnabled ? "https" : "http") + "://";
  final String host = waitStrategyTarget.getContainerIpAddress();
  final String portSuffix;
  if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) {
    portSuffix = "";
  } else {
    portSuffix = ":" + String.valueOf(livenessCheckPort);
  }
  return URI.create(scheme + host + portSuffix + path);
}

代码示例来源:origin: testcontainers/testcontainers-java

/**
 * @return the ports on which to check if the container is ready
 */
protected Set<Integer> getLivenessCheckPorts() {
  return waitStrategyTarget.getLivenessCheckPortNumbers();
}

代码示例来源:origin: testcontainers/testcontainers-java

private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, List<Integer> exposedPorts) {
    return exposedPorts.stream()
        .filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it)))
        .collect(Collectors.toSet());
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

@Override
protected void waitUntilReady() {
  final String containerName = waitStrategyTarget.getContainerInfo().getName();

代码示例来源:origin: testcontainers/testcontainers-java

@Before
public void setUp() throws Exception {
  listeningSocket1 = new ServerSocket(0);
  listeningSocket2 = new ServerSocket(0);
  nonListeningSocket = new ServerSocket(0);
  nonListeningSocket.close();
  mockContainer = mock(WaitStrategyTarget.class);
  when(mockContainer.getContainerIpAddress()).thenReturn("127.0.0.1");
}

代码示例来源:origin: testcontainers/testcontainers-java

/**
 * @return the ports on which to check if the container is ready
 * @deprecated use {@link #getLivenessCheckPortNumbers()} instead
 */
@NotNull
@NonNull
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
  final Set<Integer> result = WaitStrategyTarget.super.getLivenessCheckPortNumbers();
  // for backwards compatibility
  if (this.getLivenessCheckPort() != null) {
    result.add(this.getLivenessCheckPort());
  }
  return result;
}

代码示例来源:origin: org.testcontainers/testcontainers

private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, List<Integer> exposedPorts) {
    return exposedPorts.stream().filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it))).collect(Collectors.toSet());
  }
}

代码示例来源:origin: Playtika/testcontainers-spring-boot

@Override
protected void waitUntilReady() {
  long seconds = startupTimeout.getSeconds();
  try {
    Unreliables.retryUntilTrue((int) seconds, TimeUnit.SECONDS,
        () -> getRateLimiter().getWhenReady(this::isReady));
  } catch (TimeoutException e) {
    throw new ContainerLaunchException(
        format("[%s] notifies that container[%s] is not ready after [%d] seconds, container cannot be started.",
            getContainerType(), waitStrategyTarget.getContainerId(), seconds));
  }
}

代码示例来源:origin: org.testcontainers/testcontainers

@Override
protected void waitUntilReady() {
  final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
  if (externalLivenessCheckPorts.isEmpty()) {
    log.debug("Liveness check ports of {} is empty. Not waiting.", waitStrategyTarget.getContainerInfo().getName());
    return;
  }
  @SuppressWarnings("unchecked")
  List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
  final Set<Integer> internalPorts = getInternalPorts(externalLivenessCheckPorts, exposedPorts);
  Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, internalPorts);
  Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalLivenessCheckPorts);
  try {
    Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call()));
  } catch (TimeoutException e) {
    throw new ContainerLaunchException("Timed out waiting for container port to open (" + waitStrategyTarget.getContainerIpAddress() + " ports: " + externalLivenessCheckPorts + " should be listening)");
  }
}

代码示例来源:origin: org.testcontainers/testcontainers

private void tryPort(Integer internalPort) {
  String[][] commands = {{"/bin/sh", "-c", format("cat /proc/net/tcp{,6} | awk \'{print $2}\' | grep -i :%x && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/sh", "-c", format("nc -vz -w 1 localhost %d && echo %s", internalPort, SUCCESS_MARKER)}, {"/bin/bash", "-c", format("</dev/tcp/localhost/%d && echo %s", internalPort, SUCCESS_MARKER)}};
  for (String[] command : commands) {
    try {
      if (ExecInContainerPattern.execInContainer(waitStrategyTarget.getContainerInfo(), command).getStdout().contains(SUCCESS_MARKER)) {
        return;
      }
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }
  }
  throw new IllegalStateException("Socket not listening yet: " + internalPort);
}

代码示例来源:origin: org.testcontainers/testcontainers

/**
   * @return the ports on which to check if the container is ready
   */
  default Set<Integer> getLivenessCheckPortNumbers() {
    final Set<Integer> result = getExposedPorts().stream()
      .map(this::getMappedPort).distinct().collect(Collectors.toSet());
    result.addAll(getBoundPortNumbers());
    return result;
  }
}

代码示例来源:origin: org.testcontainers/testcontainers

/**
 * Build the URI on which to check if the container is ready.
 *
 * @param livenessCheckPort the liveness port
 * @return the liveness URI
 */
private URI buildLivenessUri(int livenessCheckPort) {
  final String scheme = (tlsEnabled ? "https" : "http") + "://";
  final String host = waitStrategyTarget.getContainerIpAddress();
  final String portSuffix;
  if ((tlsEnabled && 443 == livenessCheckPort) || (!tlsEnabled && 80 == livenessCheckPort)) {
    portSuffix = "";
  } else {
    portSuffix = ":" + String.valueOf(livenessCheckPort);
  }
  return URI.create(scheme + host + portSuffix + path);
}

代码示例来源:origin: org.testcontainers/testcontainers

/**
 * @return the ports on which to check if the container is ready
 */
protected Set<Integer> getLivenessCheckPorts() {
  return waitStrategyTarget.getLivenessCheckPortNumbers();
}

代码示例来源:origin: Playtika/testcontainers-spring-boot

protected boolean isReady() {
    String commandName = getContainerType();
    String containerId = waitStrategyTarget.getContainerId();
    log.debug("{} execution of command {} for container id: {} ", commandName, containerId);

    ExecCmdResult healthCheckCmdResult =
        ContainerUtils.execCmd(DockerClientFactory.instance().client(), containerId, getCheckCommand());

    log.debug("{} executed with exitCode: {}, output: {}",
        commandName, healthCheckCmdResult.getExitCode(), healthCheckCmdResult.getOutput());

    if (healthCheckCmdResult.getExitCode() != 0) {
      log.debug("{} executed with exitCode !=0, considering status as unknown", commandName);
      return false;
    }
    log.debug("{} command executed, considering container {} successfully started", commandName, containerId);
    return true;
  }
}

代码示例来源:origin: org.testcontainers/testcontainers

@Override
protected void waitUntilReady() {
  final String containerName = waitStrategyTarget.getContainerInfo().getName();
  final Integer livenessCheckPort = livenessPort.map(waitStrategyTarget::getMappedPort).orElseGet(() -> {
    final Set<Integer> livenessCheckPorts = getLivenessCheckPorts();

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com