Skip to content

Commit 1122bf6

Browse files
committed
make poll sleep interval configurable
Set poll sleep interval to 0 in tests which makes tests invoked by mvn verify 4 times faster!
1 parent fae414b commit 1122bf6

36 files changed

Lines changed: 186 additions & 84 deletions

src/main/java/com/gooddata/AbstractService.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,43 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25-
import java.net.URI;
2625
import java.util.concurrent.TimeUnit;
2726

2827
/**
2928
* Parent for GoodData services providing helpers for REST API calls and polling.
3029
*/
3130
public abstract class AbstractService {
3231

33-
public static final Integer WAIT_BEFORE_RETRY_IN_MILLIS = 5 * 1000;
34-
3532
protected final RestTemplate restTemplate;
3633

34+
private final GoodDataSettings settings;
35+
3736
protected final ObjectMapper mapper = new ObjectMapper();
3837

3938
private final ResponseExtractor<ClientHttpResponse> reusableResponseExtractor = ReusableClientHttpResponse::new;
4039

40+
/**
41+
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
42+
* this abstract one.
43+
*
44+
* @param restTemplate RESTful HTTP Spring template
45+
* @param settings settings
46+
*/
47+
public AbstractService(final RestTemplate restTemplate, final GoodDataSettings settings) {
48+
this.restTemplate = notNull(restTemplate, "restTemplate");
49+
this.settings = notNull(settings, "settings");
50+
}
4151

4252
/**
4353
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
4454
* this abstract one.
4555
*
4656
* @param restTemplate RESTful HTTP Spring template
57+
* @deprecated use {@link #AbstractService(RestTemplate, GoodDataSettings)}
4758
*/
59+
@Deprecated
4860
public AbstractService(RestTemplate restTemplate) {
49-
this.restTemplate = notNull(restTemplate, "restTemplate");
61+
this(restTemplate, new GoodDataSettings());
5062
}
5163

5264
final <R> R poll(final PollHandler<?,R> handler, long timeout, final TimeUnit unit) {
@@ -61,7 +73,7 @@ final <R> R poll(final PollHandler<?,R> handler, long timeout, final TimeUnit un
6173
}
6274

6375
try {
64-
Thread.sleep(WAIT_BEFORE_RETRY_IN_MILLIS);
76+
Thread.sleep(settings.getPollSleep());
6577
} catch (InterruptedException e) {
6678
throw new GoodDataException("interrupted");
6779
}

src/main/java/com/gooddata/GoodData.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,24 +206,24 @@ protected GoodData(GoodDataEndpoint endpoint, Authentication authentication, Goo
206206

207207
restTemplate = createRestTemplate(endpoint, httpClient);
208208

209-
accountService = new AccountService(getRestTemplate());
210-
projectService = new ProjectService(getRestTemplate(), accountService);
211-
metadataService = new MetadataService(getRestTemplate());
212-
modelService = new ModelService(getRestTemplate());
213-
gdcService = new GdcService(getRestTemplate());
209+
accountService = new AccountService(getRestTemplate(), settings);
210+
projectService = new ProjectService(getRestTemplate(), accountService, settings);
211+
metadataService = new MetadataService(getRestTemplate(), settings);
212+
modelService = new ModelService(getRestTemplate(), settings);
213+
gdcService = new GdcService(getRestTemplate(), settings);
214214
dataStoreService = new DataStoreService(getHttpClient(), getRestTemplate(), gdcService, endpoint.toUri());
215-
datasetService = new DatasetService(getRestTemplate(), dataStoreService);
216-
exportService = new ExportService(getRestTemplate(), endpoint);
217-
reportService = new ReportService(exportService, getRestTemplate());
218-
processService = new ProcessService(getRestTemplate(), accountService, dataStoreService);
219-
warehouseService = new WarehouseService(getRestTemplate());
220-
connectorService = new ConnectorService(getRestTemplate(), projectService);
221-
notificationService = new NotificationService(getRestTemplate());
222-
exportImportService = new ExportImportService(getRestTemplate());
223-
featureFlagService = new FeatureFlagService(getRestTemplate());
224-
outputStageService = new OutputStageService(getRestTemplate());
225-
projectTemplateService = new ProjectTemplateService(getRestTemplate());
226-
auditEventService = new AuditEventService(getRestTemplate(), accountService);
215+
datasetService = new DatasetService(getRestTemplate(), dataStoreService, settings);
216+
exportService = new ExportService(getRestTemplate(), endpoint, settings);
217+
reportService = new ReportService(exportService, getRestTemplate(), settings);
218+
processService = new ProcessService(getRestTemplate(), accountService, dataStoreService, settings);
219+
warehouseService = new WarehouseService(getRestTemplate(), settings);
220+
connectorService = new ConnectorService(getRestTemplate(), projectService, settings);
221+
notificationService = new NotificationService(getRestTemplate(), settings);
222+
exportImportService = new ExportImportService(getRestTemplate(), settings);
223+
featureFlagService = new FeatureFlagService(getRestTemplate(), settings);
224+
outputStageService = new OutputStageService(getRestTemplate(), settings);
225+
projectTemplateService = new ProjectTemplateService(getRestTemplate(), settings);
226+
auditEventService = new AuditEventService(getRestTemplate(), accountService, settings);
227227
}
228228

229229
static RestTemplate createRestTemplate(GoodDataEndpoint endpoint, HttpClient httpClient) {

src/main/java/com/gooddata/GoodDataSettings.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
package com.gooddata;
77

8-
import org.apache.commons.lang.StringUtils;
8+
import com.gooddata.util.GoodDataToStringBuilder;
99

1010
import java.util.concurrent.TimeUnit;
1111

@@ -24,6 +24,7 @@ public class GoodDataSettings {
2424
private int connectionTimeout = secondsToMillis(10);
2525
private int connectionRequestTimeout = secondsToMillis(10);
2626
private int socketTimeout = secondsToMillis(60);
27+
private int pollSleep = secondsToMillis(5);
2728
private String userAgent;
2829

2930

@@ -158,6 +159,36 @@ public int getSocketTimeout() {
158159
return socketTimeout;
159160
}
160161

162+
/**
163+
* Get sleep time in milliseconds between poll retries
164+
*
165+
* @see AbstractService#poll(PollHandler, long, TimeUnit)
166+
*/
167+
public int getPollSleep() {
168+
return pollSleep;
169+
}
170+
171+
/**
172+
* Set sleep time between poll retries
173+
*
174+
* @param pollSleep sleep milliseconds
175+
* @see AbstractService#poll(PollHandler, long, TimeUnit)
176+
*/
177+
public void setPollSleep(final int pollSleep) {
178+
isTrue(pollSleep >= 0, "pollSleep must be not negative");
179+
this.pollSleep = pollSleep;
180+
}
181+
182+
/**
183+
* Set sleep time between poll retries
184+
*
185+
* @param pollSleep sleep seconds
186+
* @see AbstractService#poll(PollHandler, long, TimeUnit)
187+
*/
188+
public void setPollSleepSeconds(final int pollSleep) {
189+
setPollSleep(secondsToMillis(pollSleep));
190+
}
191+
161192
/**
162193
* User agent
163194
* @return user agent string
@@ -184,8 +215,9 @@ public boolean equals(final Object o) {
184215
if (maxConnections != that.maxConnections) return false;
185216
if (connectionTimeout != that.connectionTimeout) return false;
186217
if (connectionRequestTimeout != that.connectionRequestTimeout) return false;
187-
return socketTimeout == that.socketTimeout;
188-
218+
if (socketTimeout != that.socketTimeout) return false;
219+
if (pollSleep != that.pollSleep) return false;
220+
return userAgent != null ? userAgent.equals(that.userAgent) : that.userAgent == null;
189221
}
190222

191223
@Override
@@ -194,17 +226,14 @@ public int hashCode() {
194226
result = 31 * result + connectionTimeout;
195227
result = 31 * result + connectionRequestTimeout;
196228
result = 31 * result + socketTimeout;
229+
result = 31 * result + pollSleep;
230+
result = 31 * result + (userAgent != null ? userAgent.hashCode() : 0);
197231
return result;
198232
}
199233

200234
@Override
201235
public String toString() {
202-
return "GoodDataSettings{" +
203-
"connectionRequestTimeout=" + connectionRequestTimeout +
204-
", maxConnections=" + maxConnections +
205-
", connectionTimeout=" + connectionTimeout +
206-
", socketTimeout=" + socketTimeout +
207-
'}';
236+
return GoodDataToStringBuilder.defaultToString(this);
208237
}
209238

210239
private static int secondsToMillis(int seconds) {

src/main/java/com/gooddata/account/AccountService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.gooddata.AbstractService;
99
import com.gooddata.GoodDataException;
1010
import com.gooddata.GoodDataRestException;
11+
import com.gooddata.GoodDataSettings;
1112
import com.gooddata.gdc.UriResponse;
1213
import org.springframework.http.HttpStatus;
1314
import org.springframework.http.converter.json.MappingJacksonValue;
@@ -26,9 +27,10 @@ public class AccountService extends AbstractService {
2627
* Constructs service for GoodData account management.
2728
*
2829
* @param restTemplate RESTful HTTP Spring template
30+
* @param settings settings
2931
*/
30-
public AccountService(RestTemplate restTemplate) {
31-
super(restTemplate);
32+
public AccountService(final RestTemplate restTemplate, final GoodDataSettings settings) {
33+
super(restTemplate, settings);
3234
}
3335

3436
/**

src/main/java/com/gooddata/auditevent/AuditEventService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.gooddata.AbstractService;
77
import com.gooddata.GoodDataException;
88
import com.gooddata.GoodDataRestException;
9+
import com.gooddata.GoodDataSettings;
910
import com.gooddata.account.Account;
1011
import com.gooddata.account.AccountService;
1112
import com.gooddata.collections.MultiPageList;
@@ -30,9 +31,10 @@ public class AuditEventService extends AbstractService {
3031
* Service for audit events
3132
* @param restTemplate rest template
3233
* @param accountService account service
34+
* @param settings settings
3335
*/
34-
public AuditEventService(final RestTemplate restTemplate, final AccountService accountService) {
35-
super(restTemplate);
36+
public AuditEventService(final RestTemplate restTemplate, final AccountService accountService, final GoodDataSettings settings) {
37+
super(restTemplate, settings);
3638
this.accountService = notNull(accountService, "account service");
3739
}
3840

src/main/java/com/gooddata/connector/ConnectorService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.gooddata.AbstractService;
99
import com.gooddata.FutureResult;
10+
import com.gooddata.GoodDataSettings;
1011
import com.gooddata.PollResult;
1112
import com.gooddata.GoodDataException;
1213
import com.gooddata.GoodDataRestException;
@@ -35,8 +36,8 @@ public class ConnectorService extends AbstractService {
3536

3637
private final ProjectService projectService;
3738

38-
public ConnectorService(final RestTemplate restTemplate, final ProjectService projectService) {
39-
super(restTemplate);
39+
public ConnectorService(final RestTemplate restTemplate, final ProjectService projectService, final GoodDataSettings settings) {
40+
super(restTemplate, settings);
4041
this.projectService = notNull(projectService, "projectService");
4142
}
4243

src/main/java/com/gooddata/dataload/OutputStageService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.gooddata.AbstractService;
1313
import com.gooddata.GoodDataException;
1414
import com.gooddata.GoodDataRestException;
15+
import com.gooddata.GoodDataSettings;
1516
import com.gooddata.project.Project;
1617
import org.springframework.http.HttpEntity;
1718
import org.springframework.http.HttpMethod;
@@ -28,9 +29,10 @@ public class OutputStageService extends AbstractService {
2829
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
2930
* this abstract one.
3031
* @param restTemplate RESTful HTTP Spring template
32+
* @param settings settings
3133
*/
32-
public OutputStageService(final RestTemplate restTemplate) {
33-
super(restTemplate);
34+
public OutputStageService(final RestTemplate restTemplate, final GoodDataSettings settings) {
35+
super(restTemplate, settings);
3436
}
3537

3638
/**

src/main/java/com/gooddata/dataload/processes/ProcessService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.gooddata.AbstractPollHandler;
99
import com.gooddata.AbstractService;
1010
import com.gooddata.FutureResult;
11+
import com.gooddata.GoodDataSettings;
1112
import com.gooddata.PollResult;
1213
import com.gooddata.GoodDataException;
1314
import com.gooddata.GoodDataRestException;
@@ -64,9 +65,11 @@ public class ProcessService extends AbstractService {
6465
* @param restTemplate RESTful HTTP Spring template
6566
* @param accountService service to access accounts
6667
* @param dataStoreService service for upload process data
68+
* @param settings settings
6769
*/
68-
public ProcessService(RestTemplate restTemplate, AccountService accountService, DataStoreService dataStoreService) {
69-
super(restTemplate);
70+
public ProcessService(final RestTemplate restTemplate, final AccountService accountService,
71+
final DataStoreService dataStoreService, final GoodDataSettings settings) {
72+
super(restTemplate, settings);
7073
this.dataStoreService = dataStoreService;
7174
this.accountService = notNull(accountService, "accountService");
7275
}

src/main/java/com/gooddata/dataset/DatasetService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.gooddata.FutureResult;
1111
import com.gooddata.GoodDataException;
1212
import com.gooddata.GoodDataRestException;
13+
import com.gooddata.GoodDataSettings;
1314
import com.gooddata.PollResult;
1415
import com.gooddata.gdc.AboutLinks.Link;
1516
import com.gooddata.gdc.DataStoreException;
@@ -50,8 +51,9 @@ public class DatasetService extends AbstractService {
5051

5152
private final DataStoreService dataStoreService;
5253

53-
public DatasetService(RestTemplate restTemplate, DataStoreService dataStoreService) {
54-
super(restTemplate);
54+
public DatasetService(final RestTemplate restTemplate, final DataStoreService dataStoreService,
55+
final GoodDataSettings settings) {
56+
super(restTemplate, settings);
5557
this.dataStoreService = notNull(dataStoreService, "dataStoreService");
5658
}
5759

src/main/java/com/gooddata/export/ExportService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.gooddata.GoodDataEndpoint;
1313
import com.gooddata.GoodDataException;
1414
import com.gooddata.GoodDataRestException;
15+
import com.gooddata.GoodDataSettings;
1516
import com.gooddata.PollResult;
1617
import com.gooddata.SimplePollHandler;
1718
import com.gooddata.gdc.AsyncTask;
@@ -55,9 +56,10 @@ public class ExportService extends AbstractService {
5556
* Service for data export
5657
* @param restTemplate REST template
5758
* @param endpoint GoodData Endpoint
59+
* @param settings settings
5860
*/
59-
public ExportService(final RestTemplate restTemplate, final GoodDataEndpoint endpoint) {
60-
super(restTemplate);
61+
public ExportService(final RestTemplate restTemplate, final GoodDataEndpoint endpoint, final GoodDataSettings settings) {
62+
super(restTemplate, settings);
6163
this.endpoint = notNull(endpoint, "endpoint");
6264
}
6365

0 commit comments

Comments
 (0)