Skip to content

Commit 82829d4

Browse files
samples: add long timeout sample (#27)
* docs: add long timeout sample * fix lint * rm unused variable * fix package * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent c75d5f0 commit 82829d4

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.optimizationai;
18+
19+
// [START cloudoptimization_long_timeout]
20+
21+
import com.google.cloud.optimization.v1.FleetRoutingClient;
22+
import com.google.cloud.optimization.v1.FleetRoutingSettings;
23+
import com.google.cloud.optimization.v1.OptimizeToursRequest;
24+
import com.google.cloud.optimization.v1.OptimizeToursResponse;
25+
import com.google.protobuf.Duration;
26+
import com.google.protobuf.TextFormat;
27+
import java.io.FileInputStream;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.io.Reader;
31+
32+
/**
33+
* This is an example to send a request to Cloud Fleet Routing synchronous API via Java API Client.
34+
*/
35+
public class SyncApiWithLongTimeout {
36+
public static void longTimeout() throws Exception {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectParent = "projects/{YOUR_GCP_PROJECT_ID}";
39+
String modelPath = "YOUR_MODEL_PATH";
40+
longTimeout(projectParent, modelPath);
41+
}
42+
43+
public static void longTimeout(String projectParent, String modelPath) throws Exception {
44+
int timeoutSeconds = 100;
45+
InputStream modelInputstream = new FileInputStream(modelPath);
46+
Reader modelInputStreamReader = new InputStreamReader(modelInputstream);
47+
OptimizeToursRequest.Builder requestBuilder =
48+
OptimizeToursRequest.newBuilder()
49+
.setTimeout(Duration.newBuilder().setSeconds(timeoutSeconds).build())
50+
.setParent(projectParent);
51+
TextFormat.getParser().merge(modelInputStreamReader, requestBuilder);
52+
53+
// Checks the gRPC connection every 5 mins and keeps it alive.
54+
FleetRoutingClient fleetRoutingClientClient =
55+
FleetRoutingClient.create(
56+
FleetRoutingSettings.newBuilder()
57+
.setTransportChannelProvider(
58+
FleetRoutingSettings.defaultGrpcTransportProviderBuilder()
59+
.setKeepAliveTime(org.threeten.bp.Duration.ofSeconds(300))
60+
.build())
61+
.build());
62+
OptimizeToursResponse response = fleetRoutingClientClient.optimizeTours(requestBuilder.build());
63+
System.out.println(response.toString());
64+
}
65+
}
66+
// [END cloudoptimization_long_timeout]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.optimizationai;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
/** Tests for SyncApiWithLongTimeout sample. */
28+
public class SyncApiWithLongTimeoutTest {
29+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
30+
private static final String PROJECT_PARENT = String.format("projects/%s", PROJECT_ID);
31+
private static final String MODEL_PATH = "resources/sync_request.textproto";
32+
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
private PrintStream originalPrintStream;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
originalPrintStream = System.out;
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
System.out.flush();
48+
System.setOut(originalPrintStream);
49+
}
50+
51+
@Test
52+
public void testSyncApi() throws Exception {
53+
SyncApiWithLongTimeout.longTimeout(PROJECT_PARENT, MODEL_PATH);
54+
String got = bout.toString();
55+
assertThat(got).contains("routes");
56+
}
57+
}

0 commit comments

Comments
 (0)