diff --git a/apps/testing/src/main/java/com/akto/testing/ApiExecutor.java b/apps/testing/src/main/java/com/akto/testing/ApiExecutor.java index 1e6731fb7e..e7f3f8ee6a 100644 --- a/apps/testing/src/main/java/com/akto/testing/ApiExecutor.java +++ b/apps/testing/src/main/java/com/akto/testing/ApiExecutor.java @@ -21,6 +21,13 @@ public class ApiExecutor { private static final LoggerMaker loggerMaker = new LoggerMaker(ApiExecutor.class); + static { + boolean isSaasDeployment = "true".equals(System.getenv("IS_SAAS")); + if (HTTPClientHandler.instance == null) { + HTTPClientHandler.initHttpClientHandler(isSaasDeployment); + } + } + private static OriginalHttpResponse common(Request request, boolean followRedirects) throws Exception { Integer accountId = Context.accountId.get(); @@ -35,11 +42,6 @@ private static OriginalHttpResponse common(Request request, boolean followRedire } } - boolean isSaasDeployment = "true".equals(System.getenv("IS_SAAS")); - - if (HTTPClientHandler.instance == null) { - HTTPClientHandler.initHttpClientHandler(isSaasDeployment); - } OkHttpClient client = HTTPClientHandler.instance.getHTTPClient(followRedirects); if (!Main.SKIP_SSRF_CHECK && !HostDNSLookup.isRequestValid(request.url().host())) { diff --git a/apps/testing/src/main/java/com/akto/testing/TestExecutor.java b/apps/testing/src/main/java/com/akto/testing/TestExecutor.java index f7836b8d38..837ae4c7ca 100644 --- a/apps/testing/src/main/java/com/akto/testing/TestExecutor.java +++ b/apps/testing/src/main/java/com/akto/testing/TestExecutor.java @@ -165,7 +165,7 @@ public void apiWiseInit(TestingRun testingRun, ObjectId summaryId) { CountDownLatch latch = new CountDownLatch(apiInfoKeyList.size()); ExecutorService threadPool = Executors.newFixedThreadPool(maxConcurrentRequests); - List>> futureTestingRunResults = new ArrayList<>(); + List> futureTestingRunResults = new ArrayList<>(); Map hostsToApiCollectionMap = new HashMap<>(); ConcurrentHashMap subCategoryEndpointMap = new ConcurrentHashMap<>(); @@ -200,7 +200,7 @@ public void apiWiseInit(TestingRun testingRun, ObjectId summaryId) { loggerMaker.errorAndAddToDb("Error while finding host: " + e, LogDb.TESTING); } try { - Future> future = threadPool.submit( + Future future = threadPool.submit( () -> startWithLatch(apiInfoKey, testingRun.getTestIdConfig(), testingRun.getId(),testingRun.getTestingRunConfig(), testingUtil, summaryId, @@ -225,20 +225,6 @@ public void apiWiseInit(TestingRun testingRun, ObjectId summaryId) { loggerMaker.infoAndAddToDb("Finished testing", LogDb.TESTING); - int totalResults = 0; - for (Future> future: futureTestingRunResults) { - if (!future.isDone()) continue; - try { - if (!future.get().isEmpty()) { - int resultSize = future.get().size(); - totalResults += resultSize; - } - } catch (InterruptedException | ExecutionException e) { - loggerMaker.errorAndAddToDb("Error while after running test : " + e, LogDb.TESTING); - } - } - - loggerMaker.infoAndAddToDb("Finished adding " + totalResults + " testingRunResults", LogDb.TESTING); } public static void updateTestSummary(ObjectId summaryId){ @@ -463,7 +449,7 @@ public Map generateResponseMap(String payloadStr, Map startWithLatch( + public Void startWithLatch( ApiInfo.ApiInfoKey apiInfoKey, int testIdConfig, ObjectId testRunId, TestingRunConfig testingRunConfig, TestingUtil testingUtil, ObjectId testRunResultSummaryId, int accountId, CountDownLatch latch, int startTime, int timeToKill, Map testConfigMap, TestingRun testingRun, @@ -472,27 +458,12 @@ public List startWithLatch( loggerMaker.infoAndAddToDb("Starting test for " + apiInfoKey, LogDb.TESTING); Context.accountId.set(accountId); - List testingRunResults = new ArrayList<>(); int now = Context.now(); if ( timeToKill <= 0 || now - startTime <= timeToKill) { try { // todo: commented out older one // testingRunResults = start(apiInfoKey, testIdConfig, testRunId, testingRunConfig, testingUtil, testRunResultSummaryId, testConfigMap); - testingRunResults = startTestNew(apiInfoKey, testRunId, testingRunConfig, testingUtil, testRunResultSummaryId, testConfigMap, subCategoryEndpointMap, apiInfoKeyToHostMap); - String size = testingRunResults.size()+""; - loggerMaker.infoAndAddToDb("testingRunResults size: " + size, LogDb.TESTING); - if (!testingRunResults.isEmpty()) { - trim(testingRunResults); - TestingRunResultDao.instance.insertMany(testingRunResults); - loggerMaker.infoAndAddToDb("Inserted testing results", LogDb.TESTING); - //Creating issues from testingRunResults - TestingIssuesHandler handler = new TestingIssuesHandler(); - boolean triggeredByTestEditor = false; - if (testingRun.getTriggeredBy() != null) { - triggeredByTestEditor = testingRun.getTriggeredBy().equals("test_editor"); - } - handler.handleIssuesCreationFromTestingRunResults(testingRunResults, triggeredByTestEditor); // pass new field here - } + startTestNew(apiInfoKey, testRunId, testingRunConfig, testingUtil, testRunResultSummaryId, testConfigMap, subCategoryEndpointMap, apiInfoKeyToHostMap); } catch (Exception e) { e.printStackTrace(); loggerMaker.errorAndAddToDb("error while running tests: " + e, LogDb.TESTING); @@ -500,7 +471,7 @@ public List startWithLatch( } latch.countDown(); - return testingRunResults; + return null; } public static void trim(TestingRunResult testingRunResult) { @@ -535,7 +506,19 @@ public void trim(List testingRunResults) { } } - public List startTestNew(ApiInfo.ApiInfoKey apiInfoKey, ObjectId testRunId, + public void insertResultsAndMakeIssues(List testingRunResults) { + String size = testingRunResults.size() + ""; + loggerMaker.infoAndAddToDb("testingRunResults size: " + size, LogDb.TESTING); + trim(testingRunResults); + TestingRunResultDao.instance.insertMany(testingRunResults); + loggerMaker.infoAndAddToDb("Inserted testing results", LogDb.TESTING); + TestingIssuesHandler handler = new TestingIssuesHandler(); + boolean triggeredByTestEditor = false; + handler.handleIssuesCreationFromTestingRunResults(testingRunResults, triggeredByTestEditor); + testingRunResults.clear(); + } + + public void startTestNew(ApiInfo.ApiInfoKey apiInfoKey, ObjectId testRunId, TestingRunConfig testingRunConfig, TestingUtil testingUtil, ObjectId testRunResultSummaryId, Map testConfigMap, ConcurrentHashMap subCategoryEndpointMap, Map apiInfoKeyToHostMap) { @@ -543,6 +526,7 @@ public List startTestNew(ApiInfo.ApiInfoKey apiInfoKey, Object List testSubCategories = testingRunConfig == null ? new ArrayList<>() : testingRunConfig.getTestSubCategoryList(); + int random = (int) ( ( Math.random() * 10 ) + 4); for (String testSubCategory: testSubCategories) { TestConfig testConfig = testConfigMap.get(testSubCategory); if (testConfig == null) continue; @@ -557,9 +541,16 @@ public List startTestNew(ApiInfo.ApiInfoKey apiInfoKey, Object e.printStackTrace(); } if (testingRunResult != null) testingRunResults.add(testingRunResult); + + if (!testingRunResults.isEmpty() && testingRunResults.size() % random == 0) { + insertResultsAndMakeIssues(testingRunResults); + } + } + + if(!testingRunResults.isEmpty()){ + insertResultsAndMakeIssues(testingRunResults); } - return testingRunResults; } public boolean applyRunOnceCheck(ApiInfoKey apiInfoKey, TestConfig testConfig, ConcurrentHashMap subCategoryEndpointMap, Map apiInfoKeyToHostMap, String testSubCategory) {