Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
docs: add long timeout code snippet (#20)
Browse files Browse the repository at this point in the history
* docs: add long timeout code snippet

* fix lint

* add timeout
  • Loading branch information
changsongd committed Apr 11, 2022
1 parent 60b35a0 commit 9f57450
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 107 deletions.
215 changes: 108 additions & 107 deletions samples/snippets/resources/sync_request.json
@@ -1,113 +1,114 @@
{
"parent": "projects/${YOUR_GCP_PROJECT_ID}",
"model": {
"shipments": [
{
"deliveries": [
{
"arrivalLocation": {
"latitude": 48.880942,
"longitude": 2.323866
},
"duration": "250s",
"timeWindows": [
{
"endTime": "1970-01-01T01:06:40Z",
"startTime": "1970-01-01T00:50:00Z"
}
]
}
],
"loadDemands": {
"weight": {
"amount": "10"
}
},
"pickups": [
{
"arrivalLocation": {
"latitude": 48.874507,
"longitude": 2.30361
},
"duration": "150s",
"timeWindows": [
{
"endTime": "1970-01-01T00:33:20Z",
"startTime": "1970-01-01T00:16:40Z"
}
]
}
]
"parent": "projects/${YOUR_GCP_PROJECT_ID}",
"timeout": "15s",
"model": {
"shipments": [
{
"deliveries": [
{
"arrivalLocation": {
"latitude": 48.880942,
"longitude": 2.323866
},
"duration": "250s",
"timeWindows": [
{
"endTime": "1970-01-01T01:06:40Z",
"startTime": "1970-01-01T00:50:00Z"
}
]
}
],
"loadDemands": {
"weight": {
"amount": "10"
}
},
{
"deliveries": [
{
"arrivalLocation": {
"latitude": 48.88094,
"longitude": 2.323844
},
"duration": "251s",
"timeWindows": [
{
"endTime": "1970-01-01T01:06:41Z",
"startTime": "1970-01-01T00:50:01Z"
}
]
}
],
"loadDemands": {
"weight": {
"amount": "20"
}
},
"pickups": [
{
"arrivalLocation": {
"latitude": 48.880943,
"longitude": 2.323867
},
"duration": "151s",
"timeWindows": [
{
"endTime": "1970-01-01T00:33:21Z",
"startTime": "1970-01-01T00:16:41Z"
}
]
}
]
}
],
"vehicles": [
{
"loadLimits": {
"weight": {
"maxLoad": 50
}
},
"endLocation": {
"latitude": 48.86311,
"longitude": 2.341205
},
"startLocation": {
"latitude": 48.863102,
"longitude": 2.341204
"pickups": [
{
"arrivalLocation": {
"latitude": 48.874507,
"longitude": 2.30361
},
"duration": "150s",
"timeWindows": [
{
"endTime": "1970-01-01T00:33:20Z",
"startTime": "1970-01-01T00:16:40Z"
}
]
}
]
},
{
"deliveries": [
{
"arrivalLocation": {
"latitude": 48.88094,
"longitude": 2.323844
},
"duration": "251s",
"timeWindows": [
{
"endTime": "1970-01-01T01:06:41Z",
"startTime": "1970-01-01T00:50:01Z"
}
]
}
],
"loadDemands": {
"weight": {
"amount": "20"
}
},
"pickups": [
{
"arrivalLocation": {
"latitude": 48.880943,
"longitude": 2.323867
},
"duration": "151s",
"timeWindows": [
{
"endTime": "1970-01-01T00:33:21Z",
"startTime": "1970-01-01T00:16:41Z"
}
]
}
]
}
],
"vehicles": [
{
"loadLimits": {
"weight": {
"maxLoad": 50
}
},
{
"loadLimits": {
"weight": {
"maxLoad": 60
}
},
"endLocation": {
"latitude": 48.86312,
"longitude": 2.341215
},
"startLocation": {
"latitude": 48.863112,
"longitude": 2.341214
"endLocation": {
"latitude": 48.86311,
"longitude": 2.341205
},
"startLocation": {
"latitude": 48.863102,
"longitude": 2.341204
}
},
{
"loadLimits": {
"weight": {
"maxLoad": 60
}
},
"endLocation": {
"latitude": 48.86312,
"longitude": 2.341215
},
"startLocation": {
"latitude": 48.863112,
"longitude": 2.341214
}
]
}
}
}
]
}
}
48 changes: 48 additions & 0 deletions samples/snippets/sync_api_with_long_timeout.py
@@ -0,0 +1,48 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START cloudoptimization_long_timeout]

from google.cloud import optimization_v1
from google.cloud.optimization_v1.services import fleet_routing
from google.cloud.optimization_v1.services.fleet_routing import transports
from google.cloud.optimization_v1.services.fleet_routing.transports import grpc as fleet_routing_grpc

# TODO(developer): Uncomment these variables before running the sample.
# project_id= 'YOUR_PROJECT_ID'


def long_timeout(request_file_name: str, project_id: str) -> None:
with open(request_file_name, 'r') as f:
fleet_routing_request = optimization_v1.OptimizeToursRequest.from_json(f.read())
fleet_routing_request.parent = f"projects/{project_id}"

# Create a channel to provide a connection to the Fleet Routing servers with
# custom behavior. The `grpc.keepalive_time_ms` channel argument modifies
# the channel behavior in order to send keep-alive pings every 5 minutes.
channel = fleet_routing_grpc.FleetRoutingGrpcTransport.create_channel(
options=[
('grpc.keepalive_time_ms', 500),
('grpc.max_send_message_length', -1),
('grpc.max_receive_message_length', -1),
],
)
# Keep-alive pings are sent on the transport. Create the transport using the
# custom channel The transport is essentially a wrapper to the channel.
transport = transports.FleetRoutingGrpcTransport(channel=channel)
client = fleet_routing.client.FleetRoutingClient(transport=transport)
fleet_routing_response = client.optimize_tours(fleet_routing_request)
print(fleet_routing_response)

# [END cloudoptimization_long_timeout]
29 changes: 29 additions & 0 deletions samples/snippets/sync_api_with_long_timeout_test.py
@@ -0,0 +1,29 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import google.auth
import pytest

from samples.snippets import sync_api_with_long_timeout


def test_long_timeout(capsys: pytest.LogCaptureFixture) -> None:
request_file_name = "resources/sync_request.json"
_, project_id = google.auth.default()
sync_api_with_long_timeout.long_timeout(request_file_name, project_id)
out, _ = capsys.readouterr()

expected_strings = ["routes", "visits", "transitions", "metrics"]
for expected_string in expected_strings:
assert expected_string in out

0 comments on commit 9f57450

Please sign in to comment.