Skip to content

Commit

Permalink
test: Add ?namespace=... to redis:// uri for parallel testing
Browse files Browse the repository at this point in the history
This will make it safer to run tests in parallel as the
tests will have separate service registration and coordination locks.
To do that, this adds a namespace= query param to the redis url
that includes the slot number that pants creates to allow tests to run in parallel more safely.
The namespace is used by the tooz library's redis driver as a prefix
for all the redis keys. Thus, multiple tests can use the same
redis database without clobbering each other's keys.
  • Loading branch information
cognifloyd committed Nov 9, 2024
1 parent 16b3782 commit 8942c33
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion conf/st2.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protocol = udp
# / cross server functionality
#url = redis://localhost
#url = kazoo://localhost
url = redis://127.0.0.1:6379
url = redis://127.0.0.1:6379?namespace=_st2_dev

[webui]
# webui_base_url = https://mywebhost.domain
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/uses_services/redis_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class UsesRedisRequest:

@property
def coord_url(self) -> str:
return f"redis://{self.host}:{self.port}"
return f"redis://{self.host}:{self.port}?namespace=_st2_test"


@dataclass(frozen=True)
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/uses_services/scripts/is_redis_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _is_redis_running(coord_url: str) -> bool:

# unit tests do not use redis, they use use an in-memory coordinator: "zake://"
# integration tests use this url with a conf file derived from conf/st2.dev.conf
coord_url = args.get(1, "redis://127.0.0.1:6379")
coord_url = args.get(1, "redis://127.0.0.1:6379?namespace=_st2_test")

is_running = _is_redis_running(coord_url)
exit_code = 0 if is_running else 1
Expand Down
4 changes: 3 additions & 1 deletion st2tests/st2tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ def _override_coordinator_opts(noop=False):
redis_host = os.environ.get("ST2TESTS_REDIS_HOST", False)
if redis_host:
redis_port = os.environ.get("ST2TESTS_REDIS_PORT", "6379")
driver = f"redis://{redis_host}:{redis_port}"
# namespace= is the tooz redis driver's key prefix (default is "_tooz")
namespace = f"_st2_test{os.environ.get('ST2TESTS_PARALLEL_SLOT', '')}"
driver = f"redis://{redis_host}:{redis_port}?namespace={namespace}"

CONF.set_override(name="url", override=driver, group="coordination")
CONF.set_override(name="lock_timeout", override=1, group="coordination")
Expand Down
2 changes: 1 addition & 1 deletion tools/launchdev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function init()
ST2VARS+=("ST2_SYSTEM_USER__USER")
fi
if [ -n "${ST2TESTS_REDIS_HOST}" ] && [ -n "${ST2TESTS_REDIS_PORT}"]; then
export ST2_COORDINATION__URL="${ST2_COORDINATION__URL:-redis://${ST2TESTS_REDIS_HOST}:${ST2TESTS_REDIS_PORT}}"
export ST2_COORDINATION__URL="${ST2_COORDINATION__URL:-redis://${ST2TESTS_REDIS_HOST}:${ST2TESTS_REDIS_PORT}}?namespace=_st2_dev"
ST2VARS+=("ST2_COORDINATION__URL")
fi

Expand Down

0 comments on commit 8942c33

Please sign in to comment.