8000 Improve code coverage · hub4j/github-api@af54d2c · GitHub
[go: up one dir, main page]

Skip to content

Commit af54d2c

Browse files
committed
Improve code coverage
1 parent 38ca010 commit af54d2c

File tree

4 files changed

+51
-63
lines changed

4 files changed

+51
-63
lines changed

src/main/java/org/kohsuke/github/AbuseLimitHandler.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import org.kohsuke.github.connector.GitHubConnectorResponse;
44

55
import java.io.IOException;
6-
import java.io.InterruptedIOException;
76
import java.net.HttpURLConnection;
8-
import java.time.ZonedDateTime;
9-
import java.time.format.DateTimeFormatter;
10-
import java.time.temporal.ChronoUnit;
117

128
import javax.annotation.Nonnull;
139

@@ -88,13 +84,8 @@ public void onError(@Nonnull GitHubConnectorResponse connectorResponse) throws I
8884
public static final AbuseLimitHandler WAIT = new AbuseLimitHandler() {
8985
@Override
9086
public void onError(IOException e, HttpURLConnection uc) throws IOException {
91-
try {
92-
Thread.sleep(parseWaitTime(uc));
93-
} catch (InterruptedException ex) {
94-
throw (InterruptedIOException) new InterruptedIOException().initCause(e);
95-
}
87+
sleep(parseWaitTime(uc));
9688
}
97-
9889
};
9990

10091
/**
@@ -116,19 +107,6 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
116107
* number or a date (the spec allows both). If no header is found, wait for a reasonably amount of time.
117108
*/
118109
long parseWaitTime(HttpURLConnection uc) {
119-
String v = uc.getHeaderField("Retry-After");
120-
if (v == null) {
121-
// can't tell, wait for unambiguously over one minute per GitHub guidance
122-
return DEFAULT_WAIT_MILLIS;
123-
}
124-
125-
try {
126-
return Math.max(1000, Long.parseLong(v) * 1000);
127-
} catch (NumberFormatException nfe) {
128-
// The retry-after header could be a number in seconds, or an http-date
129-
ZonedDateTime zdt = ZonedDateTime.parse(v, DateTimeFormatter.RFC_1123_DATE_TIME);
130-
return ChronoUnit.MILLIS.between(ZonedDateTime.now(), zdt);
131- }
110+
return parseWaitTime(uc.getHeaderField("Retry-After"), null, DEFAULT_WAIT_MILLIS, 1000);
132111
}
133-
134112
}

src/main/java/org/kohsuke/github/GitHubAbuseLimitHandler.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
import org.kohsuke.github.connector.GitHubConnectorResponse;
44

55
import java.io.IOException;
6-
import java.io.InterruptedIOException;
76
import java.net.HttpURLConnection;
87
import java.time.Duration;
9-
import java.time.ZonedDateTime;
10-
import java.time.format.DateTimeFormatter;
11-
import java.time.temporal.ChronoUnit;
128

139
import javax.annotation.Nonnull;
1410

@@ -125,11 +121,7 @@ private boolean hasHeader(GitHubConnectorResponse connectorResponse, String head
125121
public static final GitHubAbuseLimitHandler WAIT = new GitHubAbuseLimitHandler() {
126122
@Override
127123
public void onError(GitHubConnectorResponse connectorResponse) throws IOException {
128-
try {
129-
Thread.sleep(parseWaitTime(connectorResponse));
130-
} catch (InterruptedException ex) {
131-
throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
132-
}
124+
sleep(parseWaitTime(connectorResponse));
133125
}
134126
};
135127

@@ -155,30 +147,10 @@ public void onError(GitHubConnectorResponse connectorResponse) throws IOExceptio
155147
* number or a date (the spec allows both). If no header is found, wait for a reasonably amount of time.
156148
*/
157149
static long parseWaitTime(GitHubConnectorResponse connectorResponse) {
158-
String v = connectorResponse.header("Retry-After");
159-
if (v == null) {
160-
return DEFAULT_WAIT_MILLIS;
161-
}
162-
163-
try {
164-
return Math.max(MINIMUM_ABUSE_RETRY_MILLIS, Duration.ofSeconds(Long.parseLong(v)).toMillis());
165-
} catch (NumberFormatException nfe) {
166-
// The retry-after header could be a number in seconds, or an http-date
167-
// We know it was a date if we got a number format exception :)
168-
169-
// Don't use ZonedDateTime.now(), because the local and remote server times may not be in sync
170-
// Instead, we can take advantage of the Date field in the response to see what time the remote server
171-
// thinks it is
172-
String dateField = connectorResponse.header("Date");
173-
ZonedDateTime now;
174-
if (dateField != null) {
175-
now = ZonedDateTime.parse(dateField, DateTimeFormatter.RFC_1123_DATE_TIME);
176-
} else {
177-
now = ZonedDateTime.now();
178-
}
179-
ZonedDateTime zdt = ZonedDateTime.parse(v, DateTimeFormatter.RFC_1123_DATE_TIME);
180-
return Math.max(MINIMUM_ABUSE_RETRY_MILLIS, ChronoUnit.MILLIS.between(now, zdt));
181-
}
150+
return parseWaitTime(connectorResponse.header("Retry-After"),
151+
connectorResponse.header("Date"),
152+
DEFAULT_WAIT_MILLIS,
153+
MINIMUM_ABUSE_RETRY_MILLIS);
182154
}
183155

184156
}

src/main/java/org/kohsuke/github/GitHubConnectorResponseErrorHandler.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import java.io.FileNotFoundException;
88
import java.io.IOException;
99
import java.io.InputStreamReader;
10+
import java.io.InterruptedIOException;
1011
import java.nio.charset.StandardCharsets;
12+
import java.time.Duration;
13+
import java.time.ZonedDateTime;
14+
import java.time.format.DateTimeFormatter;
15+
import java.time.temporal.ChronoUnit;
1116

1217
import javax.annotation.Nonnull;
1318

@@ -111,4 +116,38 @@ private boolean isServiceDown(GitHubConnectorResponse connectorResponse) throws
111116
return false;
112117
}
113118
};
119+
120+
static void sleep(long waitMillis) throws IOException {
121+
try {
122+
Thread.sleep(waitMillis);
123+
} catch (InterruptedException ex) {
124+
throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
125+
}
126+
}
127+
128+
static long parseWaitTime(String waitHeader, String dateHeader, long defaultMillis, long minimumMillis) {
129+
if (waitHeader == null) {
130+
return defaultMillis;
131+
}
132+
133+
try {
134+
return Math.max(minimumMillis, Duration.ofSeconds(Long.parseLong(waitHeader)).toMillis());
135+
} catch (NumberFormatException nfe) {
136+
// The wait header could be a number in seconds, or an http-date
137+
// We know it was a date if we got a number format exception :)
138+
139+
// Try not to use ZonedDateTime.now(), because the local and remote server times may not be in sync
140+
// Instead, we can take advantage of the Date field in the response to see what time the remote server
141+
// thinks it is
142+
String dateField = dateHeader;
143+
ZonedDateTime now;
144+
if (dateField != null) {
145+
now = ZonedDateTime.parse(dateField, DateTimeFormatter.RFC_1123_DATE_TIME);
146+
} else {
147+
now = ZonedDateTime.now();
148+
}
149+
ZonedDateTime zdt = ZonedDateTime.parse(waitHeader, DateTimeFormatter.RFC_1123_DATE_TIME);
150+
return Math.max(minimumMillis, ChronoUnit.MILLIS.between(now, zdt));
151+
}
152+
}
114153
}

src/main/java/org/kohsuke/github/GitHubRateLimitHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.kohsuke.github.connector.GitHubConnectorResponse;
55

66
import java.io.IOException;
7-
import java.io.InterruptedIOException;
87
import java.net.HttpURLConnection;
98
import java.time.Duration;
109
import java.time.ZonedDateTime;
@@ -72,11 +71,7 @@ boolean isError(@NotNull GitHubConnectorResponse connectorResponse) throws IOExc
7271
public static final GitHubRateLimitHandler WAIT = new GitHubRateLimitHandler() {
7372
@Override
7473
public void onError(GitHubConnectorResponse connectorResponse) throws IOException {
75-
try {
76-
Thread.sleep(parseWaitTime(connectorResponse));
77-
} catch (InterruptedException ex) {
78-
throw (InterruptedIOException) new InterruptedIOException().initCause(ex);
79-
}
74+
sleep(parseWaitTime(connectorResponse));
8075
}
8176
};
8277

@@ -100,6 +95,10 @@ long parseWaitTime(GitHubConnectorResponse connectorResponse) {
10095
now = ZonedDateTime.now();
10196
}
10297
return Math.max(MINIMUM_RATE_LIMIT_RETRY_MILLIS, (Long.parseLong(v) - now.toInstant().getEpochSecond()) * 1000);
98+
// return parseWaitTime(connectorResponse.header("X-RateLimit-Reset"),
99+
// connectorResponse.header("Date"),
100+
// Duration.ofMinutes(1).toMillis(),
101+
// MINIMUM_RATE_LIMIT_RETRY_MILLIS);
103102
}
104103

105104
/**

0 commit comments

Comments
 (0)
0