Skip to content

Commit

Permalink
countDownlatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Bughue committed Feb 20, 2024
1 parent 1ee0980 commit 3029b08
Showing 1 changed file with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
*/
package org.apache.seata.common;

import org.apache.commons.lang.ObjectUtils;
import org.apache.seata.config.ConfigurationCache;
import org.apache.seata.config.ConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* the type ConfigurationTestHelper
**/
public class ConfigurationTestHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationTestHelper.class);
private static final long PUT_CONFIG_TIMEOUT = 30000L;
private static final long PUT_CONFIG_CHECK_GAP = 500L;

public static void removeConfig(String dataId) {
putConfig(dataId, null);
}

public static void putConfig(String dataId, String content) {
ConfigurationCache.addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL);
CountDownLatch countDownLatch = new CountDownLatch(1);
ConfigurationCache.addConfigListener(ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL, event -> countDownLatch.countDown());
if (content == null) {
System.clearProperty(dataId);
ConfigurationFactory.getInstance().removeConfig(dataId);
Expand All @@ -46,18 +48,12 @@ public static void putConfig(String dataId, String content) {
System.setProperty(dataId, content);
ConfigurationFactory.getInstance().putConfig(dataId, content);

long start = System.currentTimeMillis();
while (!ObjectUtils.equals(content, ConfigurationFactory.getInstance().getConfig(dataId))) {
if (PUT_CONFIG_TIMEOUT < System.currentTimeMillis() - start) {
LOGGER.error("putConfig timeout, dataId={}, timeout={}ms", dataId, PUT_CONFIG_TIMEOUT);
return;
}
try {
Thread.sleep(PUT_CONFIG_CHECK_GAP);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
try {
countDownLatch.await(PUT_CONFIG_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
LOGGER.info("putConfig ok, dataId={}, cost {}ms", dataId, System.currentTimeMillis() - start);

LOGGER.info("putConfig ok, dataId={}", dataId);
}
}

0 comments on commit 3029b08

Please sign in to comment.