From f5a3292a08e35cf1107e01692e2e0b9cc28230bd Mon Sep 17 00:00:00 2001 From: Dan Vargas Date: Fri, 19 Jan 2024 16:06:39 -0700 Subject: [PATCH] fix thread count issue when using karate.options #2444 --- .../src/main/java/com/intuit/karate/Main.java | 5 +- .../main/java/com/intuit/karate/Runner.java | 2 +- .../com/intuit/karate/ThreadCountTest.java | 48 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 karate-core/src/test/java/com/intuit/karate/ThreadCountTest.java diff --git a/karate-core/src/main/java/com/intuit/karate/Main.java b/karate-core/src/main/java/com/intuit/karate/Main.java index 95fd74129..71c6c4912 100644 --- a/karate-core/src/main/java/com/intuit/karate/Main.java +++ b/karate-core/src/main/java/com/intuit/karate/Main.java @@ -94,7 +94,7 @@ public class Main implements Callable { List tags; @Option(names = {"-T", "--threads"}, description = "number of threads when running tests") - int threads = 1; + Integer threads; @Option(names = {"-o", "--output"}, description = "directory where logs and reports are output (default 'target')") String output = FileUtils.getBuildDir(); @@ -329,6 +329,9 @@ public Void call() throws Exception { } return null; } + if (threads == null) { + threads = 1; + } if (paths != null) { Results results = Runner .path(paths).tags(tags).scenarioName(name) diff --git a/karate-core/src/main/java/com/intuit/karate/Runner.java b/karate-core/src/main/java/com/intuit/karate/Runner.java index 75b95c159..16d5e2b67 100644 --- a/karate-core/src/main/java/com/intuit/karate/Runner.java +++ b/karate-core/src/main/java/com/intuit/karate/Runner.java @@ -176,7 +176,7 @@ public List resolveAll() { if (ko.paths != null) { paths = ko.paths; } - if (ko.threads != threadCount) { // 1 by default + if (ko.threads != null) { threadCount = ko.threads; } dryRun = ko.dryRun || dryRun; diff --git a/karate-core/src/test/java/com/intuit/karate/ThreadCountTest.java b/karate-core/src/test/java/com/intuit/karate/ThreadCountTest.java new file mode 100644 index 000000000..54edaddcf --- /dev/null +++ b/karate-core/src/test/java/com/intuit/karate/ThreadCountTest.java @@ -0,0 +1,48 @@ +package com.intuit.karate; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static com.intuit.karate.Constants.KARATE_OPTIONS; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ThreadCountTest { + + private static final int THREAD_COUNT = 20; + + @AfterEach + void clearKarateOptionsProperty() { + System.clearProperty(KARATE_OPTIONS); + } + + @Test + void testThreadCountFromRunner() { + Runner.Builder builder = Runner.builder(); + builder.path("does-not-exist").parallel(THREAD_COUNT); + assertEquals(builder.threadCount, THREAD_COUNT); + } + + @Test + void testThreadCountFromKarateOptionsShortName() { + System.setProperty(KARATE_OPTIONS, "-T" + THREAD_COUNT); + Runner.Builder builder = Runner.builder(); + builder.path("does-not-exist").parallel(1); + assertEquals(builder.threadCount, THREAD_COUNT); + } + + @Test + void testThreadCountFromKarateOptionsLongName() { + System.setProperty(KARATE_OPTIONS, "--threads=" + THREAD_COUNT); + Runner.Builder builder = Runner.builder(); + builder.path("does-not-exist").parallel(1); + assertEquals(builder.threadCount, THREAD_COUNT); + } + + @Test + void testThreadCountFromRunnerAndKarateOptionsWithoutThreadOption() { + System.setProperty(KARATE_OPTIONS, "--tags=@does-not-exist"); + Runner.Builder builder = Runner.builder(); + builder.path("does-not-exist").parallel(THREAD_COUNT); + assertEquals(builder.threadCount, THREAD_COUNT); + } +}