Skip to content

Commit f00be8e

Browse files
authored
docs: add long timeout code snippet (#20)
* docs: add long timeout code snippet * fix lint * add timeout
1 parent 8fe6675 commit f00be8e

File tree

3 files changed

+185
-107
lines changed

3 files changed

+185
-107
lines changed
Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,114 @@
11
{
2-
"parent": "projects/${YOUR_GCP_PROJECT_ID}",
3-
"model": {
4-
"shipments": [
5-
{
6-
"deliveries": [
7-
{
8-
"arrivalLocation": {
9-
"latitude": 48.880942,
10-
"longitude": 2.323866
11-
},
12-
"duration": "250s",
13-
"timeWindows": [
14-
{
15-
"endTime": "1970-01-01T01:06:40Z",
16-
"startTime": "1970-01-01T00:50:00Z"
17-
}
18-
]
19-
}
20-
],
21-
"loadDemands": {
22-
"weight": {
23-
"amount": "10"
24-
}
25-
},
26-
"pickups": [
27-
{
28-
"arrivalLocation": {
29-
"latitude": 48.874507,
30-
"longitude": 2.30361
31-
},
32-
"duration": "150s",
33-
"timeWindows": [
34-
{
35-
"endTime": "1970-01-01T00:33:20Z",
36-
"startTime": "1970-01-01T00:16:40Z"
37-
}
38-
]
39-
}
40-
]
2+
"parent": "projects/${YOUR_GCP_PROJECT_ID}",
3+
"timeout": "15s",
4+
"model": {
5+
"shipments": [
6+
{
7+
"deliveries": [
8+
{
9+
"arrivalLocation": {
10+
"latitude": 48.880942,
11+
"longitude": 2.323866
12+
},
13+
"duration": "250s",
14+
"timeWindows": [
15+
{
16+
"endTime": "1970-01-01T01:06:40Z",
17+
"startTime": "1970-01-01T00:50:00Z"
18+
}
19+
]
20+
}
21+
],
22+
"loadDemands": {
23+
"weight": {
24+
"amount": "10"
25+
}
4126
},
42-
{
43-
"deliveries": [
44-
{
45-
"arrivalLocation": {
46-
"latitude": 48.88094,
47-
"longitude": 2.323844
48-
},
49-
"duration": "251s",
50-
"timeWindows": [
51-
{
52-
"endTime": "1970-01-01T01:06:41Z",
53-
"startTime": "1970-01-01T00:50:01Z"
54-
}
55-
]
56-
}
57-
],
58-
"loadDemands": {
59-
"weight": {
60-
"amount": "20"
61-
}
62-
},
63-
"pickups": [
64-
{
65-
"arrivalLocation": {
66-
"latitude": 48.880943,
67-
"longitude": 2.323867
68-
},
69-
"duration": "151s",
70-
"timeWindows": [
71-
{
72-
"endTime": "1970-01-01T00:33:21Z",
73-
"startTime": "1970-01-01T00:16:41Z"
74-
}
75-
]
76-
}
77-
]
78-
}
79-
],
80-
"vehicles": [
81-
{
82-
"loadLimits": {
83-
"weight": {
84-
"maxLoad": 50
85-
}
86-
},
87-
"endLocation": {
88-
"latitude": 48.86311,
89-
"longitude": 2.341205
90-
},
91-
"startLocation": {
92-
"latitude": 48.863102,
93-
"longitude": 2.341204
27+
"pickups": [
28+
{
29+
"arrivalLocation": {
30+
"latitude": 48.874507,
31+
"longitude": 2.30361
32+
},
33+
"duration": "150s",
34+
"timeWindows": [
35+
{
36+
"endTime": "1970-01-01T00:33:20Z",
37+
"startTime": "1970-01-01T00:16:40Z"
38+
}
39+
]
40+
}
41+
]
42+
},
43+
{
44+
"deliveries": [
45+
{
46+
"arrivalLocation": {
47+
"latitude": 48.88094,
48+
"longitude": 2.323844
49+
},
50+
"duration": "251s",
51+
"timeWindows": [
52+
{
53+
"endTime": "1970-01-01T01:06:41Z",
54+
"startTime": "1970-01-01T00:50:01Z"
55+
}
56+
]
57+
}
58+
],
59+
"loadDemands": {
60+
"weight": {
61+
"amount": "20"
62+
}
63+
},
64+
"pickups": [
65+
{
66+
"arrivalLocation": {
67+
"latitude": 48.880943,
68+
"longitude": 2.323867
69+
},
70+
"duration": "151s",
71+
"timeWindows": [
72+
{
73+
"endTime": "1970-01-01T00:33:21Z",
74+
"startTime": "1970-01-01T00:16:41Z"
75+
}
76+
]
77+
}
78+
]
79+
}
80+
],
81+
"vehicles": [
82+
{
83+
"loadLimits": {
84+
"weight": {
85+
"maxLoad": 50
9486
}
9587
},
96-
{
97-
"loadLimits": {
98-
"weight": {
99-
"maxLoad": 60
100-
}
101-
},
102-
"endLocation": {
103-
"latitude": 48.86312,
104-
"longitude": 2.341215
105-
},
106-
"startLocation": {
107-
"latitude": 48.863112,
108-
"longitude": 2.341214
88+
"endLocation": {
89+
"latitude": 48.86311,
90+
"longitude": 2.341205
91+
},
92+
"startLocation": {
93+
"latitude": 48.863102,
94+
"longitude": 2.341204
95+
}
96+
},
97+
{
98+
"loadLimits": {
99+
"weight": {
100+
"maxLoad": 60
109101
}
102+
},
103+
"endLocation": {
104+
"latitude": 48.86312,
105+
"longitude": 2.341215
106+
},
107+
"startLocation": {
108+
"latitude": 48.863112,
109+
"longitude": 2.341214
110110
}
111-
]
112-
}
113-
}
111+
}
112+
]
113+
}
114+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START cloudoptimization_long_timeout]
16+
17+
from google.cloud import optimization_v1
18+
from google.cloud.optimization_v1.services import fleet_routing
19+
from google.cloud.optimization_v1.services.fleet_routing import transports
20+
from google.cloud.optimization_v1.services.fleet_routing.transports import grpc as fleet_routing_grpc
21+
22+
# TODO(developer): Uncomment these variables before running the sample.
23+
# project_id= 'YOUR_PROJECT_ID'
24+
25+
26+
def long_timeout(request_file_name: str, project_id: str) -> None:
27+
with open(request_file_name, 'r') as f:
28+
fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read())
29+
fleet_routing_request.parent = f"projects/{project_id}"
30+
31+
# Create a channel to provide a connection to the Fleet Routing servers with
32+
# custom behavior. The `grpc.keepalive_time_ms` channel argument modifies
33+
# the channel behavior in order to send keep-alive pings every 5 minutes.
34+
channel = fleet_routing_grpc.FleetRoutingGrpcTransport.create_channel(
35+
options=[
36+
('grpc.keepalive_time_ms', 500),
37+
('grpc.max_send_message_length', -1),
38+
('grpc.max_receive_message_length', -1),
39+
],
40+
)
41+
# Keep-alive pings are sent on the transport. Create the transport using the
42+
# custom channel The transport is essentially a wrapper to the channel.
43+
transport = transports.FleetRoutingGrpcTransport(channel=channel)
44+
client = fleet_routing.client.FleetRoutingClient(transport=transport)
45+
fleet_routing_response = client.optimize_tours(fleet_routing_request)
46+
print(fleet_routing_response)
47+
48+
# [END cloudoptimization_long_timeout]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import google.auth
16+
import pytest
17+
18+
from samples.snippets import sync_api_with_long_timeout
19+
20+
21+
def test_long_timeout(capsys: pytest.LogCaptureFixture) -> None:
22+
request_file_name = "resources/sync_request.json"
23+
_, project_id = google.auth.default()
24+
sync_api_with_long_timeout.long_timeout(request_file_name, project_id)
25+
out, _ = capsys.readouterr()
26+
27+
expected_strings = ["routes", "visits", "transitions", "metrics"]
28+
for expected_string in expected_strings:
29+
assert expected_string in out

0 commit comments

Comments
 (0)