Skip to content

Commit

Permalink
chore: add retries to cloud-tasks tests (GoogleCloudPlatform#10167)
Browse files Browse the repository at this point in the history
  • Loading branch information
kweinmeister authored Jun 29, 2023
1 parent 6e459ee commit cb055da
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
52 changes: 28 additions & 24 deletions cloud_tasks/snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,47 @@
[![Open in Cloud Shell][shell_img]][shell_link]

[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=tasks/README.md
[shell_link]:
https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=tasks/README.md

This sample demonstrates how to use the
[Cloud Tasks](https://cloud.google.com/tasks/docs/) client library.
This sample demonstrates how to use the [Cloud
Tasks](https://cloud.google.com/tasks/docs/) client library.

`create_http_task.py` is a simple command-line program to create
tasks to be pushed to an URL endpoint.
`create_http_task.py` is a simple command-line program to create tasks to be
pushed to an URL endpoint.

`create_http_task_with_token.py` is a simple command-line program to create
tasks to be pushed to an URL endpoint with authorization header.

## Prerequisites to run locally:
## Prerequisites to run locally

Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup).
Please refer to [Setting Up a Python Development
Environment](https://cloud.google.com/python/setup).

## Authentication

To set up authentication, please refer to our
[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started).
To set up authentication, please refer to our [authentication getting started
guide](https://cloud.google.com/docs/authentication/getting-started).

## Install Dependencies

To install the dependencies for this sample, use the following command:

```
```sh
pip install -r requirements.txt
```

This sample uses the common protos in the [googleapis](https://github.com/googleapis/googleapis)
repository. For more info, see
[Protocol Buffer Basics](https://developers.google.com/protocol-buffers/docs/pythontutorial).
This sample uses the common protos in the
[googleapis](https://github.com/googleapis/googleapis) repository. For more
info, see [Protocol Buffer
Basics](https://developers.google.com/protocol-buffers/docs/pythontutorial).

## Creating a queue

To create a queue (named `my-queue`) using the Cloud SDK, use the following
gcloud command:

```
```sh
gcloud tasks queues create my-queue
```

Expand All @@ -50,38 +53,39 @@ Set environment variables:

First, your project ID:

```
```sh
export PROJECT_ID=my-project-id
```

Then the queue ID, as specified at queue creation time. Queue IDs already
created can be listed with `gcloud tasks queues list`.

```
```sh
export QUEUE_ID=my-queue
```

And finally the location ID, which can be discovered with
`gcloud tasks queues describe my-queue`, with the location embedded in
the "name" value (for instance, if the name is
"projects/my-project/locations/us-central1/queues/my-queue", then the
location is "us-central1").
And finally the location ID, which can be discovered with `gcloud tasks queues
describe my-queue`, with the location embedded in the "name" value (for
instance, if the name is
"projects/my-project/locations/us-central1/queues/my-queue", then the location
is "us-central1").

```
```sh
export LOCATION_ID=us-central1
```

### Creating Tasks with HTTP Targets

Set an environment variable for the endpoint to your task handler. This is an
example url:
```

```sh
export URL=https://example.com/task_handler
```

Running the sample will create a task and send the task to the specific URL
endpoint, with a payload specified:

```
```sh
python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello
```
2 changes: 2 additions & 0 deletions cloud_tasks/snippets/create_http_task_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Generator
import uuid

from google.api_core.retry import Retry
from google.cloud import tasks_v2
import pytest

Expand All @@ -44,6 +45,7 @@ def test_queue() -> Generator[tasks_v2.Queue, None, None]:
client.delete_queue(request={"name": queue.name})


@Retry()
def test_create_http_task(test_queue: tasks_v2.Queue) -> None:
task = create_http_task.create_http_task(
TEST_PROJECT_ID,
Expand Down
2 changes: 2 additions & 0 deletions cloud_tasks/snippets/create_http_task_with_token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Generator
import uuid

from google.api_core.retry import Retry
from google.cloud import tasks_v2
import pytest

Expand Down Expand Up @@ -47,6 +48,7 @@ def test_queue() -> Generator[tasks_v2.Queue, None, None]:
client.delete_queue(tasks_v2.DeleteQueueRequest(name=queue.name))


@Retry()
def test_create_http_task_with_token(test_queue: tasks_v2.Queue) -> None:
task = create_http_task_with_token(
TEST_PROJECT_ID,
Expand Down
2 changes: 2 additions & 0 deletions cloud_tasks/snippets/create_queue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import uuid

from google.api_core.retry import Retry
from google.cloud import tasks_v2

import create_queue
Expand All @@ -24,6 +25,7 @@
TEST_QUEUE_ID = f"my-queue-{uuid.uuid4().hex}"


@Retry()
def test_create_queue() -> None:
client = tasks_v2.CloudTasksClient()

Expand Down
2 changes: 2 additions & 0 deletions cloud_tasks/snippets/list_queues_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os
import uuid

from google.api_core.retry import Retry
from google.cloud import tasks_v2

import list_queues
Expand All @@ -23,6 +24,7 @@
TEST_LOCATION = os.getenv("TEST_QUEUE_LOCATION", "us-central1")


@Retry()
def test_list_queues() -> None:
client = tasks_v2.CloudTasksClient()

Expand Down

0 comments on commit cb055da

Please sign in to comment.