-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add long timeout code snippet (#20)
* docs: add long timeout code snippet * fix lint * add timeout
- Loading branch information
1 parent
c0fd064
commit 86fab46
Showing
3 changed files
with
185 additions
and
107 deletions.
There are no files selected for viewing
215 changes: 108 additions & 107 deletions
215
packages/google-cloud-optimization/samples/snippets/resources/sync_request.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
48
packages/google-cloud-optimization/samples/snippets/sync_api_with_long_timeout.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
29
packages/google-cloud-optimization/samples/snippets/sync_api_with_long_timeout_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |