Skip to content

Commit

Permalink
mcrouter
Browse files Browse the repository at this point in the history
Reviewed By: jermenkoo

Differential Revision: D65467162

fbshipit-source-id: 7a43f9d6436593ad069eaf0fc21e529383c128e9
  • Loading branch information
generatedunixname89002005287564 authored and facebook-github-bot committed Nov 5, 2024
1 parent 51c7304 commit 69097ec
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 105 deletions.
51 changes: 24 additions & 27 deletions mcrouter/test/MCProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def __del__(self):


def MCPopen(cmd, stdout=None, stderr=None, env=None, pass_fds=()):
if sys.version_info >= (3, 2): # Python 3.2 supports pass_fds
return subprocess.Popen(
cmd, stdout=stdout, stderr=stderr, env=env, pass_fds=pass_fds
)
return subprocess.Popen(
cmd, stdout=stdout, stderr=stderr, env=env, pass_fds=pass_fds
)
return subprocess.Popen(cmd, stdout=stdout, stderr=stderr, env=env)


Expand Down Expand Up @@ -98,9 +97,9 @@ def get_log(self):
if hasattr(self, "log"):
print(self.base_dir)
try:
with open(self.log, "r") as log_f:
with open(self.log) as log_f:
log = log_f.read()
except IOError:
except OSError:
log = ""
else:
log = ""
Expand All @@ -118,11 +117,11 @@ def dump(self):

log = self.get_log()
if log:
print("{} ({}) log:\n{}".format(self, self.cmd_line, log))
print(f"{self} ({self.cmd_line}) log:\n{log}")
if stdout:
print("{} ({}) stdout:\n{}".format(self, self.cmd_line, stdout.decode()))
print(f"{self} ({self.cmd_line}) stdout:\n{stdout.decode()}")
if stderr:
print("{} ({}) stderr:\n{}".format(self, self.cmd_line, stderr.decode()))
print(f"{self} ({self.cmd_line}) stderr:\n{stderr.decode()}")


class MCProcess(ProcessBase):
Expand All @@ -144,7 +143,7 @@ def __init__(
self.versionPing = versionPing
if cmd is not None and "-s" in cmd:
if os.path.exists(addr):
raise Exception("file path {} already exists".format(addr))
raise Exception(f"file path {addr} already exists")
self.addr = addr
self.port = 0
self.addr_family = socket.AF_UNIX
Expand Down Expand Up @@ -259,7 +258,7 @@ def ensure_connected(self):
if res == Result.OK:
return
except Exception as e:
print("Error on sending mcVersion in Thrift: {}".format(e))
print(f"Error on sending mcVersion in Thrift: {e}")
retry_count += 1
if self.max_retries and retry_count >= self.max_retries:
raise RuntimeError(
Expand All @@ -271,12 +270,12 @@ def disconnect(self):
try:
if self.socket:
self.socket.close()
except IOError:
except OSError:
pass
try:
if self.fd:
self.fd.close()
except IOError:
except OSError:
pass
self.fd = self.socket = None

Expand Down Expand Up @@ -338,7 +337,7 @@ def _get(self, cmd, keys, expect_cas, return_all_info):
return line
else:
self.connect()
raise Exception('Unexpected response "{}" ({})'.format(line, keys))
raise Exception(f'Unexpected response "{line}" ({keys})')

def get(self, keys, return_all_info=False):
return self._get("get", keys, expect_cas=False, return_all_info=return_all_info)
Expand Down Expand Up @@ -366,7 +365,7 @@ def metaget(self, keys):
# multi = False
# keys = [keys]
res = {}
self._sendall("metaget {}\r\n".format(keys))
self._sendall(f"metaget {keys}\r\n")

while True:
line = self._fdreadline().strip()
Expand Down Expand Up @@ -409,7 +408,7 @@ def expectNoReply(self):
try:
self.socket.recv(1)
return False
except socket.timeout:
except TimeoutError:
pass
return True

Expand Down Expand Up @@ -474,7 +473,7 @@ def replace(self, key, value, replicate=False, noreply=False):
def delete(self, key, exptime=None, noreply=False):
exptime_str = ""
if exptime is not None:
exptime_str = " {}".format(exptime)
exptime_str = f" {exptime}"
self._sendall(
"delete {}{}{}\r\n".format(
key, exptime_str, (" noreply" if noreply else "")
Expand Down Expand Up @@ -590,7 +589,7 @@ def cas(self, key, value, cas_token):
def stats(self, spec=None):
q = "stats\r\n"
if spec:
q = "stats {spec}\r\n".format(spec=spec)
q = f"stats {spec}\r\n"
self._sendall(q)

s = {}
Expand All @@ -611,7 +610,7 @@ def stats(self, spec=None):
def raw_stats(self, spec=None):
q = "stats\r\n"
if spec:
q = "stats {spec}\r\n".format(spec=spec)
q = f"stats {spec}\r\n"
self._sendall(q)

s = []
Expand Down Expand Up @@ -666,7 +665,7 @@ def flush_all(self, delay=None):
if delay is None:
self._sendall("flush_all\r\n")
else:
self._sendall("flush_all {}\r\n".format(delay))
self._sendall(f"flush_all {delay}\r\n")
return self._fdreadline().rstrip()


Expand Down Expand Up @@ -698,9 +697,7 @@ def sub_port(s, substitute_ports, port_map):
port_map[port] = substitute_ports[len(port_map)]
else:
if port not in substitute_ports:
raise Exception(
"Port {} not in substitute port map".format(port)
)
raise Exception(f"Port {port} not in substitute port map")
port_map[port] = substitute_ports[port]
else:
raise Exception(
Expand Down Expand Up @@ -854,14 +851,14 @@ def __init__(
base_dir = BaseDirectory("mcrouter")

if replace_map:
with open(config, "r") as config_file:
with open(config) as config_file:
replaced_config = replace_strings(config_file.read(), replace_map)
(_, config) = tempfile.mkstemp(dir=base_dir.path)
with open(config, "w") as config_file:
config_file.write(replaced_config)

if substitute_config_ports:
with open(config, "r") as config_file:
with open(config) as config_file:
replaced_config = replace_ports(
config_file.read(), substitute_config_ports
)
Expand Down Expand Up @@ -902,14 +899,14 @@ def terminate():
def is_alive():
pid = get_pid()
if pid:
return os.path.exists("/proc/{}".format(pid))
return os.path.exists(f"/proc/{pid}")
return False

self.terminate = terminate
self.is_alive = is_alive

if substitute_config_smc_ports and sr_mock_smc_config:
with open(sr_mock_smc_config, "r") as config_file:
with open(sr_mock_smc_config) as config_file:
replaced_config = replace_ports(
config_file.read(), substitute_config_smc_ports
)
Expand Down
4 changes: 2 additions & 2 deletions mcrouter/test/mock_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def run(self):
client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.runServer(client, address)
client.close()
except IOError as e:
except OSError as e:
if e.errno == errno.EWOULDBLOCK:
time.sleep(0.1)
else:
Expand Down Expand Up @@ -179,7 +179,7 @@ def runServer(self, client_socket, client_address):
if self.step % (2 * self.period) >= self.period:
time.sleep(self.tmo)
self.step += 1
if cmd.startswith("get {}\r\n".format(self.hitcmd)):
if cmd.startswith(f"get {self.hitcmd}\r\n"):
msg = "VALUE hit 0 {}\r\n{}\r\nEND\r\n".format(
len(str(self.port)),
str(self.port),
Expand Down
2 changes: 1 addition & 1 deletion mcrouter/test/test_ascii_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExtraValueServer(MockServer):
additional reply"""

def __init__(self, expected_key):
super(ExtraValueServer, self).__init__()
super().__init__()
self.expected_key = expected_key
self.expected_bytes = len("get " + expected_key + "\r\n")

Expand Down
12 changes: 4 additions & 8 deletions mcrouter/test/test_async_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ def check_stats(self, stats_dir, retries=20, sleep_interval=1):
# Sleep between retry intervals
time.sleep(sleep_interval)

self.assertTrue(fileStatExists, "{} doesn't exist".format(file_stat))
self.assertTrue(
fileStartupExists, "{} doesn't exist".format(file_startup_options)
)
self.assertTrue(
fileConfigExists, "{} doesn't exist".format(file_config_sources)
)
self.assertTrue(fileStatExists, f"{file_stat} doesn't exist")
self.assertTrue(fileStartupExists, f"{file_startup_options} doesn't exist")
self.assertTrue(fileConfigExists, f"{file_config_sources} doesn't exist")

return (file_stat, file_startup_options, file_config_sources)

Expand Down Expand Up @@ -85,7 +81,7 @@ def test_async_files(self):
self.assertEqual(len(asynclog_files), 1)
foundPool = False

with open(asynclog_files[0], "r") as f:
with open(asynclog_files[0]) as f:
for line in f.readlines():
if self.asynclog_name in line:
foundPool = True
Expand Down
10 changes: 5 additions & 5 deletions mcrouter/test/test_async_files_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def check_stats(self, stats_dir):

self.assertTrue(
self.wait_for_file(file_stat, retries=10, interval=1),
"{} doesn't exist".format(file_stat),
f"{file_stat} doesn't exist",
)
self.assertTrue(
self.wait_for_file(file_startup_options, retries=10, interval=1),
"{} doesn't exist".format(file_startup_options),
f"{file_startup_options} doesn't exist",
)
self.assertTrue(
self.wait_for_file(file_config_sources, retries=10, interval=1),
"{} doesn't exist".format(file_config_sources),
f"{file_config_sources} doesn't exist",
)

return (file_stat, file_startup_options, file_config_sources)
Expand All @@ -64,7 +64,7 @@ def test_async_files_attr(self):
spool_dir = mcrouter.get_async_spool_dir()
self.assertTrue(
self.wait_noempty_dir(spool_dir, retries=10),
"Not found any async log files under {}".format(spool_dir),
f"Not found any async log files under {spool_dir}",
)
# check async spool for failed delete
asynclog_files = []
Expand All @@ -75,7 +75,7 @@ def test_async_files_attr(self):
self.assertEqual(len(asynclog_files), 1)

# asynclog v2 should have attributes
with open(asynclog_files[0], "r") as f:
with open(asynclog_files[0]) as f:
file_json = json.load(f)
self.assertEqual(file_json[3]["a"]["a1"], 1000)
self.assertEqual(file_json[3]["a"]["a2"], 2000)
Expand Down
2 changes: 1 addition & 1 deletion mcrouter/test/test_bad_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def timeout_handler(signum, frame):
line = line.decode()
stderr += line
if re.search(bad, line):
self.fail("bad regex matched '{}'".format(line.strip()))
self.fail(f"bad regex matched '{line.strip()}'")

if re.search(good, line):
signal.alarm(0)
Expand Down
Loading

0 comments on commit 69097ec

Please sign in to comment.