Skip to content

Commit

Permalink
DRY up the retry methods in monitoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Temikus committed Jul 22, 2020
1 parent 63826d0 commit a0b2deb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
14 changes: 14 additions & 0 deletions test/helpers/integration_test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "helpers/test_helper"
require "helpers/test_collection"
require "retriable"

# Use :test credentials in ~/.fog for live integration testing
# XXX not sure if this will work on Travis CI or not
Expand Down Expand Up @@ -87,8 +88,21 @@
-----END RSA PRIVATE KEY-----
KEY

# Retry module config - standard retriable gem settings
# see: https://github.com/kamui/retriable
RETRIABLE_TRIES = 3
RETRIABLE_BASE_INTERVAL = 50

class FogIntegrationTest < MiniTest::Test
def namespaced_name
"#{self.class}_#{name}"
end

def retry_on(klass)
Retriable.retriable(on: klass,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
yield
end
end
end
36 changes: 11 additions & 25 deletions test/integration/monitoring/test_timeseries.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require "helpers/integration_test_helper"
require "retriable"

class TestMetricDescriptors < FogIntegrationTest
# Retriable is used to wrap each request in this test due to Stackdriver API being slow with
# metric propagation (sometimes 80+ seconds) and client returning
# Google::Apis::ClientError: badRequest if the metric hasn't yet been created instead of a 404.
RETRIABLE_TRIES = 3
RETRIABLE_BASE_INTERVAL = 50
# This module has a decent amount of retry wrappers as the Stackdriver API
# can be quite slow with metric propagation (sometimes 80+ seconds) and client
# returning errors, e.g. Google::Apis::ClientError: badRequest if the metric
# hasn't yet been created instead of a 404.

TEST_METRIC_PREFIX = "custom.googleapis.com/fog-google-test/timeseries".freeze
LABEL_DESCRIPTORS = [
{
Expand Down Expand Up @@ -51,10 +50,7 @@ def test_timeseries_collection
assert_empty(resp.to_h)

# Wait for metric to be created
# TODO: Dedup retries into a helper method
Retriable.retriable(on: Google::Apis::ClientError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
retry_on(Google::Apis::ClientError) do
@client.list_timeseries(
:filter => "metric.type = \"#{metric_type}\"",
:interval => {
Expand All @@ -67,9 +63,7 @@ def test_timeseries_collection
).time_series
end

series = Retriable.retriable(on: Google::Apis::ClientError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
series = retry_on(Google::Apis::ClientError) do
@client.timeseries_collection.all(
:filter => "metric.type = \"#{metric_type}\"",
:interval => {
Expand Down Expand Up @@ -115,9 +109,7 @@ def test_multiple_timeseries
_some_timeseries(start_time, metric_type, labels)
end

Retriable.retriable(on: Google::Apis::ServerError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
retry_on(Google::Apis::ServerError) do
@client.create_timeseries(:timeseries => timeseries)
end
interval = {
Expand All @@ -132,19 +124,15 @@ def test_multiple_timeseries
# Wait for metric to be created
# Retriable is used instead of wait_for due to API client returning Google::Apis::ClientError: badRequest if the
# metric hasn't yet been created
Retriable.retriable(on: Google::Apis::ClientError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
retry_on(Google::Apis::ClientError) do
@client.list_timeseries(
:filter => "metric.type = \"#{metric_type}\"",
:interval => interval
).time_series
end

# Test page size
resp = Retriable.retriable(on: Google::Apis::ClientError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
resp = retry_on(Google::Apis::ClientError) do
@client.list_timeseries(
:filter => "metric.type = \"#{metric_type}\"",
:interval => interval,
Expand All @@ -170,9 +158,7 @@ def test_multiple_timeseries
"expected different timeseries when using page_token")

# Test filter
series = Retriable.retriable(on: Google::Apis::ClientError,
tries: RETRIABLE_TRIES,
base_interval: RETRIABLE_BASE_INTERVAL) do
series = retry_on(Google::Apis::ClientError) do
@client.timeseries_collection.all(
:filter => %[
metric.type = "#{metric_type}" AND
Expand Down

0 comments on commit a0b2deb

Please sign in to comment.