(core) Add test_replay for easily replaying data sent to the sandbox purely within python

Summary:
Run JS with a value for SANDBOX_BUFFERS_DIR, then run test_replay in python with the same value to replay just the python code.

See test_replay.py for more info.

Test Plan:
Record some data, e.g. `SANDBOX_BUFFERS_DIR=manual npm start` or `SANDBOX_BUFFERS_DIR=server ./test/testrun.sh server`.

Then run `SANDBOX_BUFFERS_DIR=server python -m unittest test_replay` from within `core/sandbox/grist` to replay the input from the JS.

Sample of the output will look like this:

```
Checking /tmp/sandbox_buffers/server/2021-06-16T15:13:59.958Z
True
Checking /tmp/sandbox_buffers/server/2021-06-16T15:16:37.170Z
True
Checking /tmp/sandbox_buffers/server/2021-06-16T15:14:22.378Z
True
```

Reviewers: paulfitz, dsagal

Reviewed By: dsagal

Differential Revision: https://phab.getgrist.com/D2866
This commit is contained in:
Alex Hall
2021-06-30 15:35:05 +02:00
parent b537539b73
commit 84ddbc448b
4 changed files with 145 additions and 20 deletions

View File

@@ -11,7 +11,6 @@ Usage:
import os
import marshal
import signal
import sys
import traceback
@@ -36,10 +35,16 @@ class Sandbox(object):
DATA = True
EXC = False
def __init__(self):
def __init__(self, external_input, external_output):
self._functions = {}
self._external_input = os.fdopen(3, "rb", 64*1024)
self._external_output = os.fdopen(4, "wb", 64*1024)
self._external_input = external_input
self._external_output = external_output
@classmethod
def connected_to_js_pipes(cls):
external_input = os.fdopen(3, "rb", 64 * 1024)
external_output = os.fdopen(4, "wb", 64 * 1024)
return cls(external_input, external_output)
def _send_to_js(self, msgCode, msgBody):
# (Note that marshal version 2 is the default; we specify it explicitly for clarity. The
@@ -87,14 +92,19 @@ class Sandbox(object):
if break_on_response:
raise Exception("Sandbox disconnected unexpectedly")
default_sandbox = None
sandbox = Sandbox()
def get_default_sandbox():
global default_sandbox
if default_sandbox is None:
default_sandbox = Sandbox.connected_to_js_pipes()
return default_sandbox
def call_external(name, *args):
return sandbox.call_external(name, *args)
return get_default_sandbox().call_external(name, *args)
def register(func_name, func):
sandbox.register(func_name, func)
get_default_sandbox().register(func_name, func)
def run():
sandbox.run()
get_default_sandbox().run()