From 56e4986dda0fe19dcee808af4112e234a63f9f8b Mon Sep 17 00:00:00 2001 From: lematech Date: Wed, 16 Jun 2021 16:53:51 +0800 Subject: [PATCH] File download optimization --- .../httprunner4j/helper/OkHttpsHelper.java | 48 +++++++++++-------- .../httprunner4j/testcases/demo/DemoTest.java | 4 +- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/hrun4j-core/src/main/java/vip/lematech/httprunner4j/helper/OkHttpsHelper.java b/hrun4j-core/src/main/java/vip/lematech/httprunner4j/helper/OkHttpsHelper.java index 147aa6b..2a75a45 100644 --- a/hrun4j-core/src/main/java/vip/lematech/httprunner4j/helper/OkHttpsHelper.java +++ b/hrun4j-core/src/main/java/vip/lematech/httprunner4j/helper/OkHttpsHelper.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** @@ -282,27 +283,34 @@ private static ResponseEntity wrapperResponseEntity(HttpResult httpResult if (workDir.endsWith(Constant.DOT_PATH)) { workDir = LittleHelper.replaceLast(workDir, Constant.DOT_PATH, ""); } + CountDownLatch latch = new CountDownLatch(1); String finalWorkDir = workDir; - body.stepRate(0.1) - .setOnProcess((process -> { - LogHelper.info(I18NFactory.getLocaleMessage("file.download.process")); - long totalBytes = process.getTotalBytes(); - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.total.bytes"),totalBytes)); - long doneBytes = process.getDoneBytes(); - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.done.bytes"),doneBytes)); - double rate = process.getRate(); - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.done.rate"),rate * 100)); - boolean isDone = process.isDone(); - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.is.done"),isDone)); - })) - .toFolder(workDir) - .setOnFailure((Download.Failure failure) -> { - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.failure"),failure.getException().getMessage())); - }) - .setOnSuccess((File file) -> { - LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.success"),FileUtil.normalize(finalWorkDir))); - }) - .start(); + body.stepRate(0.1) + .setOnProcess((process -> { + LogHelper.info(I18NFactory.getLocaleMessage("file.download.process")); + long totalBytes = process.getTotalBytes(); + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.total.bytes"),totalBytes)); + long doneBytes = process.getDoneBytes(); + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.done.bytes"),doneBytes)); + double rate = process.getRate(); + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.done.rate"),rate * 100)); + boolean isDone = process.isDone(); + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.is.done"),isDone)); + })) + .toFolder(workDir) + .setOnFailure((Download.Failure failure) -> { + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.failure"),failure.getException().getMessage())); + }) + .setOnSuccess((File file) -> { + LogHelper.info(String.format(I18NFactory.getLocaleMessage("file.success"),FileUtil.normalize(finalWorkDir))); + latch.countDown(); + }) + .start(); + try { + latch.await(); + } catch (InterruptedException e) { + LogHelper.error("Download interrupt exception"); + } } else { String responseContent = body.toString(); if (JsonHelper.isJson(responseContent)) { diff --git a/hrun4j-test-demo/src/test/java/vip/lematech/httprunner4j/testcases/demo/DemoTest.java b/hrun4j-test-demo/src/test/java/vip/lematech/httprunner4j/testcases/demo/DemoTest.java index 70242a9..873902d 100644 --- a/hrun4j-test-demo/src/test/java/vip/lematech/httprunner4j/testcases/demo/DemoTest.java +++ b/hrun4j-test-demo/src/test/java/vip/lematech/httprunner4j/testcases/demo/DemoTest.java @@ -186,9 +186,9 @@ public void testBeanShellDemo(TestCase testCase) { * @param testCase * */ - /*@Test(dataProvider = "dataProvider") + @Test(dataProvider = "dataProvider") public void testFileDownloadDemo(TestCase testCase) { TestCaseExecutorEngine.getInstance().execute(testCase); - }*/ + } }