Start work on prewarming

The prewarm process and its controller are implemented with some basic
tests.
This commit is contained in:
Kovid Goyal
2022-06-06 20:39:36 +05:30
parent dec62b1929
commit 98f46f8bd7
4 changed files with 507 additions and 3 deletions

View File

@@ -177,7 +177,7 @@ class BaseTest(TestCase):
s = Screen(c, lines, cols, scrollback, cell_width, cell_height, 0, c)
return s
def create_pty(self, argv, cols=80, lines=100, scrollback=100, cell_width=10, cell_height=20, options=None, cwd=None, env=None):
def create_pty(self, argv=None, cols=80, lines=100, scrollback=100, cell_width=10, cell_height=20, options=None, cwd=None, env=None):
self.set_options(options)
return PTY(argv, lines, cols, scrollback, cell_width, cell_height, cwd, env)
@@ -228,9 +228,11 @@ class PTY:
def __del__(self):
if not self.is_child:
fd = self.master_fd
os.close(self.master_fd)
if hasattr(self, 'slave_fd'):
os.close(self.slave_fd)
del self.slave_fd
del self.master_fd
os.close(fd)
def write_to_child(self, data):
if isinstance(data, str):

48
kitty_tests/prewarm.py Normal file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
import json
import os
import tempfile
from kitty.constants import kitty_exe
from . import BaseTest
class Prewarm(BaseTest):
maxDiff = None
def test_prewarming(self):
from kittens.prewarm.main import PrewarmProcess
p = PrewarmProcess(create_file_to_read_from_worker=True)
cwd = tempfile.gettempdir()
env = {'TEST_ENV_PASS': 'xyz'}
cols = 117
stdin_data = 'from_stdin'
pty = self.create_pty(cols=cols)
ttyname = os.ttyname(pty.slave_fd)
child = p(pty.slave_fd, [kitty_exe(), '+runpy', """import os, json; from kitty.utils import *; print(json.dumps({
'cterm': os.ctermid(),
'ttyname': os.ttyname(sys.stdout.fileno()),
'cols': read_screen_size().cols,
'cwd': os.getcwd(),
'env': os.environ.get('TEST_ENV_PASS'),
'pid': os.getpid(),
'stdin': sys.stdin.read(),
'done': 'hello',
}, indent=2))"""], cwd=cwd, env=env, stdin_data=stdin_data)
self.assertFalse(pty.screen_contents().strip())
p.mark_child_as_ready(child.child_id)
pty.wait_till(lambda: 'hello' in pty.screen_contents())
data = json.loads(pty.screen_contents())
self.ae(data['cols'], cols)
self.assertTrue(data['cterm'])
self.ae(data['ttyname'], ttyname)
self.ae(data['cwd'], cwd)
self.ae(data['env'], env['TEST_ENV_PASS'])
self.ae(int(p.from_worker.readline()), data['pid'])