Skip to content

Commit

Permalink
Merge pull request #258 from jsmolar/limit_fix
Browse files Browse the repository at this point in the history
Refactor of fire_requests method
  • Loading branch information
pehala authored Nov 6, 2023
2 parents ad5cd2f + aba5ef0 commit 407a717
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 41 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 rate limit 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)
35 changes: 0 additions & 35 deletions testsuite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
import json
import os
import secrets
import typing
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

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


Expand Down Expand Up @@ -116,35 +110,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 407a717

Please sign in to comment.