From dc8020293d0f199b39deffd121d545239c5eca9f Mon Sep 17 00:00:00 2001 From: Victor Benarbia Date: Sun, 16 Aug 2026 07:08:16 -0500 Subject: [PATCH 1/2] fix: apply the client timeout to reading, not just connecting HomeDepotTest failed 4 of the last 7 CI runs, always the same way: SerpApiException: java.net.http.HttpTimeoutException: request timed out SerpApi.timeout was wired only to setHttpConnectionTimeout, so the read timeout was never configured and kept SerpApiHttp's 60s default. That is backwards: a 60s connection timeout is meaningless, since connecting takes milliseconds, while 60s to read is tight for engines that scrape. Apply the timeout to both, and raise the default to 120s, which the home_depot engine regularly needs. This is a library fix rather than a test fix: any caller searching a slow engine hit the same timeout. Also repair the README snippet helper, which sliced examples at a hardcoded lines[23..], assuming every example body starts at line 24. Quarantining GoogleEventsTest shifted that file and pulled the @Ignore and its comment into the docs as if they were usage code. Anchor on the "// setup serpapi client" marker instead, which every example test has exactly once, and regenerate. Co-Authored-By: Claude Opus 5 --- README.md | 4 ++-- README.md.erb | 6 ++++- src/main/java/serpapi/SerpApi.java | 11 ++++++--- src/test/java/serpapi/TimeoutTest.java | 30 +++++++++++++++++++++++++ src/test/java/serpapi/example/README.md | 0 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 src/test/java/serpapi/TimeoutTest.java create mode 100644 src/test/java/serpapi/example/README.md diff --git a/README.md b/README.md index daf088a..ae328d7 100644 --- a/README.md +++ b/README.md @@ -444,8 +444,9 @@ SerpApi client = new SerpApi(auth); // run search Map parameter = new HashMap<>(); parameter.put("engine", "google_events"); -parameter.put("q", "coffee"); +parameter.put("q", "Events in Austin, TX"); JsonObject results = client.search(parameter); +JsonArray events = results.getAsJsonArray("events_results"); System.out.println(results.toString()); ``` @@ -492,7 +493,6 @@ see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api) ### Search google play ```java - // setup serpapi client Map auth = new HashMap<>(); auth.put("api_key", "your_api_key"); diff --git a/README.md.erb b/README.md.erb index 85e9d8f..945b5cb 100644 --- a/README.md.erb +++ b/README.md.erb @@ -1,6 +1,10 @@ <%- def snippet(format, path) lines = File.new(path).readlines +start = lines.find_index { |line| line.include?('// setup serpapi client') } +if start.nil? + raise "No '// setup serpapi client' marker found in #{path}" +end stop = lines.find_index do |line| next false if line.lstrip.start_with?('//') line.match?(/\bassert(True|NotNull)\s*\(/) @@ -8,7 +12,7 @@ end if stop.nil? raise "No assertTrue/assertNotNull found in #{path}" end -slice = lines[23..stop-1] +slice = lines[start..stop-1] slice << "System.out.println(results.toString());" buf = slice.map { |l| l.gsub(/(^\s\s\s\s)/, '')}.join buf.gsub!("System.getenv(\"SERPAPI_KEY\")", "\"your_api_key\"") diff --git a/src/main/java/serpapi/SerpApi.java b/src/main/java/serpapi/SerpApi.java index 2a1cab4..5a1d8af 100644 --- a/src/main/java/serpapi/SerpApi.java +++ b/src/main/java/serpapi/SerpApi.java @@ -26,10 +26,13 @@ public class SerpApi { */ public SerpApiHttp client; - /** - * default HTTP client timeout + /** + * default HTTP client timeout + * + * Applied to both connecting and reading. Some engines, home_depot in + * particular, regularly take longer than a minute to respond. */ - public Integer timeout = 60000; + public Integer timeout = 120000; /*** * Constructor @@ -40,6 +43,7 @@ public SerpApi(Map parameter) { this.parameter = parameter; this.client = new SerpApiHttp("/search"); this.client.setHttpConnectionTimeout(this.timeout); + this.client.setHttpReadTimeout(this.timeout); } /*** @@ -49,6 +53,7 @@ public SerpApi() { this.parameter = new HashMap<>(); this.client = new SerpApiHttp("/search"); this.client.setHttpConnectionTimeout(this.timeout); + this.client.setHttpReadTimeout(this.timeout); } /*** diff --git a/src/test/java/serpapi/TimeoutTest.java b/src/test/java/serpapi/TimeoutTest.java new file mode 100644 index 0000000..90ef1cb --- /dev/null +++ b/src/test/java/serpapi/TimeoutTest.java @@ -0,0 +1,30 @@ +package serpapi; + +import org.junit.Test; + +import java.util.HashMap; + +import static org.junit.Assert.*; + +/** + * Test that the client timeout is applied to reading as well as connecting. + * + * Offline: only client configuration is inspected, so no network call and no + * SERPAPI_KEY are needed. + */ +public class TimeoutTest { + + @Test + public void appliesTimeoutToBothConnectAndRead() { + SerpApi serpapi = new SerpApi(new HashMap<>()); + + assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpConnectionTimeout()); + assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpReadTimeout()); + } + + @Test + public void defaultTimeoutSurvivesTheSlowestEngines() { + // home_depot has repeatedly taken longer than a minute to respond. + assertTrue(new SerpApi().timeout > 60000); + } +} diff --git a/src/test/java/serpapi/example/README.md b/src/test/java/serpapi/example/README.md new file mode 100644 index 0000000..e69de29 From ea6331b400086367f7ed32e1e79cb0cc22ac6fca Mon Sep 17 00:00:00 2001 From: Victor Benarbia Date: Sun, 16 Aug 2026 07:08:29 -0500 Subject: [PATCH 2/2] chore: remove stray empty file Created accidentally by a shell redirect run from the wrong directory. Co-Authored-By: Claude Opus 5 --- src/test/java/serpapi/example/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/test/java/serpapi/example/README.md diff --git a/src/test/java/serpapi/example/README.md b/src/test/java/serpapi/example/README.md deleted file mode 100644 index e69de29..0000000