Skip to content

Commit

Permalink
Speed up test case by remove sleep code (#427)
Browse files Browse the repository at this point in the history
* Speed up test case by removing sleep code
  • Loading branch information
khotyn authored Apr 16, 2021
1 parent cdbde41 commit ca86846
Show file tree
Hide file tree
Showing 14 changed files with 534 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class DiskReporterImpl extends AbstractDiskReporter {
private SofaTracerStatisticReporter statReporter;

public DiskReporterImpl(String digestLogType, SpanEncoder contextEncoder) {

this(digestLogType, StringUtils.EMPTY_STRING, StringUtils.EMPTY_STRING, contextEncoder,
null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;

/**
* @author qilong.zql
Expand All @@ -28,9 +30,9 @@
public class TestUtil {

public static boolean compareSlotMap(String a, String b) {
Map<String, String> aMap = new HashMap<String, String>();
Map<String, String> aMap = new HashMap<>();
StringUtils.stringToMap(a, aMap);
Map<String, String> bMap = new HashMap<String, String>();
Map<String, String> bMap = new HashMap<>();
StringUtils.stringToMap(b, bMap);
if (aMap.size() != bMap.size()) {
return false;
Expand All @@ -43,9 +45,21 @@ public static boolean compareSlotMap(String a, String b) {
return true;
}

public static void waitForAsyncLog() throws InterruptedException {
// wait flush log to file... (500ms is just expected time)
Thread.sleep(500);
public static void periodicallyAssert(Runnable assertion, long timeout) {
for (int i = 0; i < timeout; i++) {
try {
assertion.run();
return;
} catch (AssertionError error) {
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
// Ignore
}
}
}

assertion.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
import java.io.IOException;

/**
*
* @author khotyn 4/8/14 3:56 PM
*/
public class TimedRollingFileAppenderTest extends AbstractTestBase {
private static final String ROLLING_TEST_FILE_NAME = "rolling-test.log";
private TimedRollingFileAppender timedRollingFileAppender;
private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
private static final String ROLLING_TEST_FILE_NAME = "rolling-test.log";
private TimedRollingFileAppender timedRollingFileAppender;
private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

@Before
public void init() {
Expand All @@ -58,8 +57,8 @@ public void test() throws IOException, InterruptedException {
Resource[] resources = resolver.getResources("file:" + TracerLogRootDaemon.LOG_FILE_DIR
+ File.separator + ROLLING_TEST_FILE_NAME
+ "*");
Assert.assertTrue("The number of files is incorrect. The number of files starting with "
+ ROLLING_TEST_FILE_NAME + " should be 2", resources.length == 2);
Assert.assertEquals("The number of files is incorrect. The number of files starting with "
+ ROLLING_TEST_FILE_NAME + " should be 2", 2, resources.length);

for (Resource resource : resources) {
String c = FileUtils.readFileToString(resource.getFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,34 @@
public class StaticInfoLogTest extends AbstractTestBase {

@Test
public void testLogStaticInfo() throws IOException, InterruptedException, NoSuchFieldException,
IllegalAccessException {
public void testLogStaticInfo() throws NoSuchFieldException,
IllegalAccessException {

//record
reflect();
StaticInfoLog.logStaticInfo();

TestUtil.waitForAsyncLog();
TestUtil.periodicallyAssert(() -> {
try {

List<String> params = new ArrayList<String>();
params.add(TracerUtils.getPID());
params.add(TracerUtils.getInetAddress());
params.add(TracerUtils.getCurrentZone());
params.add(TracerUtils.getDefaultTimeZone());
List<String> contents = FileUtils.readLines(customFileLog("static-info.log"));
Assert.assertFalse("Static information log has no content", contents.isEmpty());
assertTrue(checkResult(params, contents.get(0)));
List<String> params = new ArrayList<>();
params.add(TracerUtils.getPID());
params.add(TracerUtils.getInetAddress());
params.add(TracerUtils.getCurrentZone());
params.add(TracerUtils.getDefaultTimeZone());
List<String> contents = FileUtils.readLines(customFileLog("static-info.log"));
Assert.assertFalse("Static information log has no content", contents.isEmpty());
assertTrue(checkResult(params, contents.get(0)));
} catch (IOException e) {
throw new AssertionError(e);
}
}, 500);
}

private static void reflect() throws NoSuchFieldException, IllegalAccessException {
Field field = StaticInfoLog.class.getDeclaredField("appender");
field.setAccessible(true);
TraceAppender appender = (TraceAppender) field.get(null);
TraceAppender appender;
appender = new TimedRollingFileAppender("static-info.log", true);
field.set(null, appender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.common.tracer.core.appender.manager;

import com.alipay.common.tracer.core.TestUtil;
import com.alipay.common.tracer.core.appender.TraceAppender;
import com.alipay.common.tracer.core.appender.TracerLogRootDaemon;
import com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender;
Expand All @@ -35,7 +36,6 @@
import java.util.concurrent.CountDownLatch;

/**
*
* @author liangen
* @version $Id: ConcurrentConsumerCorrectTest.java, v 0.1 October 23, 2017 3:01 PM liangen Exp $
*/
Expand All @@ -50,7 +50,6 @@ public class ConcurrentConsumerCorrectTest {

@Before
public void before() throws Exception {

cleanDir();
}

Expand All @@ -60,27 +59,27 @@ public void clean() throws Exception {
}

@Test
public void testConcurrentConsumerCorrect() throws InterruptedException, IOException {
/** Logs are not allowed to be lost, and log loss is avoided to affect the correctness of the result check. */
public void testConcurrentConsumerCorrect() throws InterruptedException {
/* Logs are not allowed to be lost, and log loss is avoided to affect the correctness of the result check. */
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_ALLOW_DISCARD, "false");
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_ALLOW_DISCARD, "false");

final AsyncCommonDigestAppenderManager asyncCommonDigestAppenderManager = new AsyncCommonDigestAppenderManager(
1024);
1024);
asyncCommonDigestAppenderManager.start("ConcurrentConsumerCorrectTest");

ClientSpanEncoder encoder = new ClientSpanEncoder();

TraceAppender traceAppender1 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName1, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName1, "", "");
TraceAppender traceAppender2 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName2, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName2, "", "");
TraceAppender traceAppender3 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName3, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName3, "", "");
TraceAppender traceAppender4 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName4, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName4, "", "");
TraceAppender traceAppender5 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName5, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName5, "", "");

asyncCommonDigestAppenderManager.addAppender("logType1", traceAppender1, encoder);
asyncCommonDigestAppenderManager.addAppender("logType2", traceAppender2, encoder);
Expand All @@ -90,7 +89,7 @@ public void testConcurrentConsumerCorrect() throws InterruptedException, IOExcep

final CountDownLatch countDownLatch = new CountDownLatch(30);
for (int i = 0; i < 20; i++) {
new Thread(()->{
new Thread(() -> {
SofaTracerSpan span1 = ManagerTestUtil.createSofaTracerSpan(1);
for (int j = 0; j < 30; j++) {
asyncCommonDigestAppenderManager.append(span1);
Expand Down Expand Up @@ -121,7 +120,7 @@ public void testConcurrentConsumerCorrect() throws InterruptedException, IOExcep
}

for (int i = 0; i < 10; i++) {
new Thread(()->{
new Thread(() -> {
SofaTracerSpan span1 = ManagerTestUtil.createSofaTracerSpan(1);
SofaTracerSpan span2 = ManagerTestUtil.createSofaTracerSpan(2);
SofaTracerSpan span3 = ManagerTestUtil.createSofaTracerSpan(3);
Expand All @@ -138,16 +137,19 @@ public void testConcurrentConsumerCorrect() throws InterruptedException, IOExcep
}).start();
}

/** check */
/* check */
countDownLatch.await();
Thread.sleep(3000);

assertFile(fileName1, 1000, "traceID1");
assertFile(fileName2, 1200, "traceID2");
assertFile(fileName3, 1400, "traceID3");
assertFile(fileName4, 1600, "traceID4");
assertFile(fileName5, 1800, "traceID5");

TestUtil.periodicallyAssert(() -> {
try {
assertFile(fileName1, 1000, "traceID1");
assertFile(fileName2, 1200, "traceID2");
assertFile(fileName3, 1400, "traceID3");
assertFile(fileName4, 1600, "traceID4");
assertFile(fileName5, 1800, "traceID5");
} catch (IOException e) {
throw new AssertionError(e);
}
}, 3000);
}

public void assertFile(String fileName, int expectedNum, String expectedContent)
Expand All @@ -157,7 +159,7 @@ public void assertFile(String fileName, int expectedNum, String expectedContent)
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(
fileNameRoot + fileName)));

String line = null;
String line;
while ((line = reader.readLine()) != null) {
Assert.assertTrue(line.contains(expectedContent));
actualNum++;
Expand All @@ -167,7 +169,6 @@ public void assertFile(String fileName, int expectedNum, String expectedContent)
}

private void cleanDir() throws Exception {
Thread.sleep(2000);
File file = new File(System.getProperty("user.home") + File.separator + "logs/tracelog"
+ File.separator + "append-manager.log");
if (file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.alipay.common.tracer.core.appender.manager;

import com.alipay.common.tracer.core.TestUtil;
import com.alipay.common.tracer.core.appender.TraceAppender;
import com.alipay.common.tracer.core.appender.TracerLogRootDaemon;
import com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender;
Expand All @@ -38,7 +39,6 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
*
* @author liangen
* @version $Id: ConcurrentDiscardTest.java, v 0.1 October 23, 2017 8:22 PM liangen Exp $
*/
Expand Down Expand Up @@ -83,32 +83,32 @@ private void cleanDir() throws Exception {
}

@Test
public void testConcurrentDiscard() throws InterruptedException, IOException {
public void testConcurrentDiscard() throws InterruptedException {
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_ALLOW_DISCARD, "true");
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_ALLOW_DISCARD, "true");
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_IS_OUT_DISCARD_NUMBER, "true");
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_IS_OUT_DISCARD_NUMBER, "true");
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_IS_OUT_DISCARD_ID, "true");
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_IS_OUT_DISCARD_ID, "true");
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_DISCARD_OUT_THRESHOLD, "500");
SofaTracerConfiguration.TRACER_ASYNC_APPENDER_DISCARD_OUT_THRESHOLD, "500");

final AsyncCommonDigestAppenderManager asyncCommonDigestAppenderManager = new AsyncCommonDigestAppenderManager(
1024);
1024);
asyncCommonDigestAppenderManager.start("ConcurrentDiscardTest");

ClientSpanEncoder encoder = new ClientSpanEncoder();

TraceAppender traceAppender1 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName1, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName1, "", "");
TraceAppender traceAppender2 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName2, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName2, "", "");
TraceAppender traceAppender3 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName3, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName3, "", "");
TraceAppender traceAppender4 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName4, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName4, "", "");
TraceAppender traceAppender5 = LoadTestAwareAppender
.createLoadTestAwareTimedRollingFileAppender(fileName5, "", "");
.createLoadTestAwareTimedRollingFileAppender(fileName5, "", "");

asyncCommonDigestAppenderManager.addAppender("logType1", traceAppender1, encoder);
asyncCommonDigestAppenderManager.addAppender("logType2", traceAppender2, encoder);
Expand All @@ -120,7 +120,7 @@ public void testConcurrentDiscard() throws InterruptedException, IOException {

final CountDownLatch countDownLatch = new CountDownLatch(50);
for (int i = 0; i < 50; i++) {
new Thread(()-> {
new Thread(() -> {
SofaTracerSpan span1 = ManagerTestUtil.createSofaTracerSpan(1);
SofaTracerSpan span2 = ManagerTestUtil.createSofaTracerSpan(2);
SofaTracerSpan span3 = ManagerTestUtil.createSofaTracerSpan(3);
Expand Down Expand Up @@ -153,32 +153,36 @@ public void testConcurrentDiscard() throws InterruptedException, IOException {
}

countDownLatch.await();
Thread.sleep(3000);
SynchronizingSelfLog.flush();

/**check*/
int log1Num = getLineNum(fileName1);
int log2Num = getLineNum(fileName2);
int log3Num = getLineNum(fileName3);
int log4Num = getLineNum(fileName4);
int log5Num = getLineNum(fileName5);
int allNum = log1Num + log2Num + log3Num + log4Num + log5Num;

/**Landing log + Lost log = Print log*/
SelfLog.info("Landing log:" + allNum);
SelfLog.info("Lost log:" + discardNum.get());
Assert.assertEquals(25000, allNum + discardNum.get());
/** Sync.log lost log data is less than the actual number of lost */
int logDiscard = getDiscardNumFromTracerSelfLog();
SelfLog.info("Sync.log records the number of lost logs:" + logDiscard);
Assert.assertTrue(logDiscard <= discardNum.get());
/** Accuracy of the specific lost log data recorded by sync.log: the difference from the true lost number should be less than 500 */
int allTraceIdDiscard = traceId1Discard + traceId2Discard + traceId3Discard
+ traceId4Discard + traceId5Discard;
SelfLog.info("The number of specific lost log data with traceId recorded by sync.log:" + allTraceIdDiscard);
Assert.assertTrue((discardNum.get() == allTraceIdDiscard)
|| (discardNum.get() - allTraceIdDiscard) < 500);

TestUtil.periodicallyAssert(() -> {
try {
SynchronizingSelfLog.flush();

/*check*/
int log1Num = getLineNum(fileName1);
int log2Num = getLineNum(fileName2);
int log3Num = getLineNum(fileName3);
int log4Num = getLineNum(fileName4);
int log5Num = getLineNum(fileName5);
int allNum = log1Num + log2Num + log3Num + log4Num + log5Num;

/*Landing log + Lost log = Print log*/
SelfLog.info("Landing log:" + allNum);
SelfLog.info("Lost log:" + discardNum.get());
Assert.assertEquals(25000, allNum + discardNum.get());
/* Sync.log lost log data is less than the actual number of lost */
int logDiscard = getDiscardNumFromTracerSelfLog();
SelfLog.info("Sync.log records the number of lost logs:" + logDiscard);
Assert.assertTrue(logDiscard <= discardNum.get());
/* Accuracy of the specific lost log data recorded by sync.log: the difference from the true lost number should be less than 500 */
int allTraceIdDiscard = traceId1Discard + traceId2Discard + traceId3Discard
+ traceId4Discard + traceId5Discard;
SelfLog.info("The number of specific lost log data with traceId recorded by sync.log:" + allTraceIdDiscard);
Assert.assertTrue((discardNum.get() == allTraceIdDiscard)
|| (discardNum.get() - allTraceIdDiscard) < 500);
} catch (IOException e) {
throw new AssertionError(e);
}
}, 3000);
}

public int getLineNum(String fileName) throws IOException {
Expand Down
Loading

0 comments on commit ca86846

Please sign in to comment.