Skip to content

Commit

Permalink
Refactor of fire_requests method
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolar committed Nov 2, 2023
1 parent 6a5c705 commit 1649753
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 38 deletions.
8 changes: 8 additions & 0 deletions testsuite/httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ def request(
if len(e.args) > 0 and any("Name or service not known" in arg for arg in e.args):
raise UnexpectedResponse("Didn't expect 'Name or service not known' error", None) from e
raise

def get_many(self, url, count, *, params=None, headers=None, auth=None) -> list[Response]:
"""Send multiple `GET` requests."""
responses = []
for _ in range(count):
responses.append(self.get(url, params=params, headers=headers, auth=auth))

return responses
2 changes: 1 addition & 1 deletion testsuite/openshift/objects/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def _policy_is_ready(obj):
assert success

# https://github.com/Kuadrant/kuadrant-operator/issues/140
sleep(60)
sleep(90)

return result
3 changes: 2 additions & 1 deletion testsuite/tests/kuadrant/limitador/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Conftest for all limitador tests"""
"""Conftest for all limitation tests"""

import pytest


Expand Down
8 changes: 6 additions & 2 deletions testsuite/tests/kuadrant/limitador/test_basic_limit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
Tests that a single limit is enforced as expected over one iteration
"""

import pytest

from testsuite.openshift.objects.rate_limit import Limit
from testsuite.utils import fire_requests


@pytest.fixture(
Expand Down Expand Up @@ -36,4 +36,8 @@ def rate_limit(rate_limit, limit):

def test_limit(client, limit):
"""Tests that simple limit is applied successfully"""
fire_requests(client, limit, grace_requests=1)
responses = client.get_many("/get", limit.limit)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
assert client.get("/get").status_code == 429
11 changes: 9 additions & 2 deletions testsuite/tests/kuadrant/limitador/test_multiple_iterations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
Tests that a single limit is enforced as expected over multiple iterations
"""
from time import sleep

import pytest

from testsuite.openshift.objects.rate_limit import Limit
from testsuite.utils import fire_requests


@pytest.fixture(scope="module")
Expand All @@ -16,4 +17,10 @@ def rate_limit(rate_limit):

def test_multiple_iterations(client):
"""Tests that simple limit is applied successfully and works for multiple iterations"""
fire_requests(client, Limit(5, 10), iterations=10)
for _ in range(10):
responses = client.get_many("/get", 5)
assert all(
r.status_code == 200 for r in responses
), f"Rate Limited resource unexpectedly rejected requests {responses}"
assert client.get("/get").status_code == 429
sleep(10)
33 changes: 1 addition & 32 deletions testsuite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
from collections.abc import Collection
from importlib import resources
from io import StringIO
from time import sleep
from typing import Dict, Union
from urllib.parse import urlparse, ParseResult

import httpx
from weakget import weakget

from testsuite.certificates import Certificate, CFSSLClient, CertInfo
from testsuite.config import settings

if typing.TYPE_CHECKING:
from testsuite.openshift.objects.rate_limit import Limit
pass

MESSAGE_1KB = resources.files("testsuite.resources.performance.files").joinpath("message_1kb.txt")

Expand Down Expand Up @@ -116,35 +114,6 @@ def create_csv_file(rows: list) -> StringIO:
return file


def fire_requests(client, limit: "Limit", grace_requests=0, iterations=1, path="/get"):
"""
Fires requests meant to test if it is correctly rate-limited
:param client: Client instance
:param limit: Expected rate limit
:param grace_requests: Number of requests on top of max_requests
which will be made but not checked, improves stability
:param iterations: Number of periods to tests
:param path: URL path of the request
:return:
"""
period = limit.duration
max_requests = limit.limit
url = f"{client.base_url}/{path}"
for iteration in range(iterations):
sleep(period)
for i in range(max_requests):
assert (
httpx.get(url).status_code == 200
), f"{i + 1}/{max_requests} request from {iteration + 1} iteration failed"

for i in range(grace_requests):
httpx.get(url)

assert httpx.get(url).status_code == 429, f"Iteration {iteration + 1} failed to start limiting"
sleep(period)
assert httpx.get(url).status_code == 200, f"Iteration {iteration + 1} failed to reset limits"


def extract_response(response, header="Simple", key="data"):
"""
Extracts response added by Authorino from header
Expand Down

0 comments on commit 1649753

Please sign in to comment.