Skip to content

Commit dc80202

Browse files
jvmvikclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 7913f2e commit dc80202

5 files changed

Lines changed: 45 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,9 @@ SerpApi client = new SerpApi(auth);
444444
// run search
445445
Map<String, String> parameter = new HashMap<>();
446446
parameter.put("engine", "google_events");
447-
parameter.put("q", "coffee");
447+
parameter.put("q", "Events in Austin, TX");
448448
JsonObject results = client.search(parameter);
449+
JsonArray events = results.getAsJsonArray("events_results");
449450
System.out.println(results.toString());
450451
```
451452

@@ -492,7 +493,6 @@ see: [https://serpapi.com/google-jobs-api](https://serpapi.com/google-jobs-api)
492493

493494
### Search google play
494495
```java
495-
496496
// setup serpapi client
497497
Map<String, String> auth = new HashMap<>();
498498
auth.put("api_key", "your_api_key");

README.md.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<%-
22
def snippet(format, path)
33
lines = File.new(path).readlines
4+
start = lines.find_index { |line| line.include?('// setup serpapi client') }
5+
if start.nil?
6+
raise "No '// setup serpapi client' marker found in #{path}"
7+
end
48
stop = lines.find_index do |line|
59
next false if line.lstrip.start_with?('//')
610
line.match?(/\bassert(True|NotNull)\s*\(/)
711
end
812
if stop.nil?
913
raise "No assertTrue/assertNotNull found in #{path}"
1014
end
11-
slice = lines[23..stop-1]
15+
slice = lines[start..stop-1]
1216
slice << "System.out.println(results.toString());"
1317
buf = slice.map { |l| l.gsub(/(^\s\s\s\s)/, '')}.join
1418
buf.gsub!("System.getenv(\"SERPAPI_KEY\")", "\"your_api_key\"")

src/main/java/serpapi/SerpApi.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public class SerpApi {
2626
*/
2727
public SerpApiHttp client;
2828

29-
/**
30-
* default HTTP client timeout
29+
/**
30+
* default HTTP client timeout
31+
*
32+
* Applied to both connecting and reading. Some engines, home_depot in
33+
* particular, regularly take longer than a minute to respond.
3134
*/
32-
public Integer timeout = 60000;
35+
public Integer timeout = 120000;
3336

3437
/***
3538
* Constructor
@@ -40,6 +43,7 @@ public SerpApi(Map<String, String> parameter) {
4043
this.parameter = parameter;
4144
this.client = new SerpApiHttp("/search");
4245
this.client.setHttpConnectionTimeout(this.timeout);
46+
this.client.setHttpReadTimeout(this.timeout);
4347
}
4448

4549
/***
@@ -49,6 +53,7 @@ public SerpApi() {
4953
this.parameter = new HashMap<>();
5054
this.client = new SerpApiHttp("/search");
5155
this.client.setHttpConnectionTimeout(this.timeout);
56+
this.client.setHttpReadTimeout(this.timeout);
5257
}
5358

5459
/***
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package serpapi;
2+
3+
import org.junit.Test;
4+
5+
import java.util.HashMap;
6+
7+
import static org.junit.Assert.*;
8+
9+
/**
10+
* Test that the client timeout is applied to reading as well as connecting.
11+
*
12+
* Offline: only client configuration is inspected, so no network call and no
13+
* SERPAPI_KEY are needed.
14+
*/
15+
public class TimeoutTest {
16+
17+
@Test
18+
public void appliesTimeoutToBothConnectAndRead() {
19+
SerpApi serpapi = new SerpApi(new HashMap<>());
20+
21+
assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpConnectionTimeout());
22+
assertEquals(serpapi.timeout.intValue(), serpapi.getClient().getHttpReadTimeout());
23+
}
24+
25+
@Test
26+
public void defaultTimeoutSurvivesTheSlowestEngines() {
27+
// home_depot has repeatedly taken longer than a minute to respond.
28+
assertTrue(new SerpApi().timeout > 60000);
29+
}
30+
}

src/test/java/serpapi/example/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)