Something went wrong. Try again.
A rewrite of the Plan 9 rc shell in Goose for UNIX-like systems and with structured CLI args
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061"""Check that repeated evaluation does not retain dead shell state."""import osfrom pathlib import Pathimport resourceimport subprocessimport sysimport tempfile
shell = str(Path(sys.argv[1] if len(sys.argv) > 1 else "rc2").resolve())ITERATIONS = 150000MAX_RSS = 96 * 1024 * 1024TIMEOUT = float(os.environ.get("RC2_MEMORY_TIMEOUT", "60"))script = "x=kept; fn keep {echo $x}; for (i in " + "x " * ITERATIONS + ") {eval 'y=stable'; fn f {echo replaced}}; keep; f; echo $y"script += "; items=(" + "x " * 12000 + "); fn churn {for (i in $items) {eval 'new=value'; fn replaced {echo new}}}"script += "; x=outer; x=inner {churn; echo $x}; echo $x; echo $new; replaced"script += "; fn active {fn active {echo replacement}; churn; echo original}; active; active"script += "; switch (match) {case match; churn; echo selected; case *; echo wrong}"script += "; /usr/bin/true; echo $status"script += "; fn doc {/bin/cat <<END\nretained $x\nEND\n}; churn; doc"with tempfile.TemporaryDirectory(prefix="rc2-memory-") as directory: path = Path(directory) / "memory.rc" path.write_text(script) result = subprocess.run([shell, str(path)], capture_output=True, timeout=TIMEOUT) assert result.returncode == 0, result.stderr assert result.stdout == b"kept\nreplaced\nstable\ninner\nouter\nvalue\nnew\noriginal\nreplacement\nselected\n\nretained outer\n", result.stdout rss = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss if sys.platform != "darwin": rss *= 1024 assert rss < MAX_RSS, f"The shell retained {rss} bytes after repeated evaluation" print(f"Memory test passed: peak RSS {rss // (1024 * 1024)} MiB")
stream = b"x=latest; fn last {echo $x}\n" * ITERATIONS + b"last\n"result = subprocess.run([shell, "-I"], input=stream, capture_output=True, timeout=TIMEOUT)assert result.returncode == 0 and result.stdout == b"latest\n", (result.stdout, result.stderr)rss = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrssif sys.platform != "darwin": rss *= 1024assert rss < MAX_RSS, f"The stream retained {rss} bytes of dead shell state"print("Stream memory test passed")
temporary = "t" * 4096script = "for (i in " + "x " * 30000 + ") eval '" + temporary + "=temporary ~ x x'; echo scoped"with tempfile.TemporaryDirectory(prefix="rc2-scope-memory-") as directory: path = Path(directory) / "scopes.rc" path.write_text(script) result = subprocess.run([shell, str(path)], capture_output=True, timeout=TIMEOUT) assert result.returncode == 0 and result.stdout == b"scoped\n", result.stderr rss = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss if sys.platform != "darwin": rss *= 1024 assert rss < MAX_RSS, f"Temporary names retained {rss} bytes after scope exit"print("Scope memory test passed")
script = "fn many {" + "x=a;" * 100000 + "}; echo defined\nmany; echo $x"with tempfile.TemporaryDirectory(prefix="rc2-function-memory-") as directory: path = Path(directory) / "many.rc" path.write_text(script) result = subprocess.run([shell, str(path)], capture_output=True, timeout=TIMEOUT) assert result.returncode == 0 and result.stdout == b"defined\na\n", (result.returncode, result.stderr)print("Large function memory test passed")