predixy/test/msetnx_atomicity.py
Julien Letessier 542749b0d3 Reject cross-shard MSETNX
Prevent MSETNX from being split across shards to preserve atomicity.

Adds msetnx_atomicity test and runs it in the harness.
2026-01-15 09:14:41 +01:00

44 lines
1.2 KiB
Python

#!/usr/bin/env python
#
# Verify MSETNX does not partially apply changes when it returns 0
#
import argparse
import sys
import redis
def run_test(host, port):
c = redis.StrictRedis(host=host, port=port)
c.delete("msetnx_k1", "msetnx_k2")
c.set("msetnx_k1", "existing")
try:
r = c.execute_command("MSETNX", "msetnx_k1", "v1", "msetnx_k2", "v2")
except Exception as exc:
# If proxy rejects cross-shard MSETNX, this is acceptable.
return True
if r not in (0, b"0", False):
print("FAIL: expected MSETNX result 0, got:", r)
return False
if c.get("msetnx_k2") is not None:
print("FAIL: MSETNX partially applied when it returned 0")
return False
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser(conflict_handler='resolve', description="MSETNX atomicity test")
parser.add_argument("-h", "--host", default="127.0.0.1")
parser.add_argument("-p", "--port", type=int, default=7617)
args = parser.parse_args()
if run_test(args.host, args.port):
print("PASS: MSETNX atomicity")
sys.exit(0)
print("FAIL: MSETNX atomicity")
sys.exit(1)