55import asyncio
66import sys
77
8+ from tornado .httpclient import HTTPClientError
9+ from traitlets .config import Config
10+
811# Skip this whole module on Windows. The terminal API leads
912# to timeouts on Windows CI.
1013if sys .platform .startswith ('win' ):
@@ -30,12 +33,42 @@ def terminal_path(tmp_path):
3033 shutil .rmtree (str (subdir ), ignore_errors = True )
3134
3235
36+ CULL_TIMEOUT = 2
37+ CULL_INTERVAL = 3
38+
39+
40+ @pytest .fixture
41+ def jp_server_config ():
42+ return Config ({
43+ 'ServerApp' : {
44+ 'TerminalManager' : {
45+ 'cull_inactive_timeout' : CULL_TIMEOUT ,
46+ 'cull_interval' : CULL_INTERVAL
47+ }
48+ }
49+ })
50+
51+
52+ async def test_no_terminals (jp_fetch ):
53+ resp_list = await jp_fetch (
54+ 'api' , 'terminals' ,
55+ method = 'GET' ,
56+ allow_nonstandard_methods = True ,
57+ )
58+
59+ data = json .loads (resp_list .body .decode ())
60+
61+ assert len (data ) == 0
62+
63+
3364async def test_terminal_create (jp_fetch , kill_all ):
34- await jp_fetch (
65+ resp = await jp_fetch (
3566 'api' , 'terminals' ,
3667 method = 'POST' ,
3768 allow_nonstandard_methods = True ,
3869 )
70+ term = json .loads (resp .body .decode ())
71+ assert term ['name' ] == "1"
3972
4073 resp_list = await jp_fetch (
4174 'api' , 'terminals' ,
@@ -46,6 +79,7 @@ async def test_terminal_create(jp_fetch, kill_all):
4679 data = json .loads (resp_list .body .decode ())
4780
4881 assert len (data ) == 1
82+ assert data [0 ] == term
4983 await kill_all ()
5084
5185
@@ -104,3 +138,41 @@ async def test_terminal_create_with_cwd(jp_fetch, jp_ws_fetch, terminal_path):
104138 ws .close ()
105139
106140 assert str (terminal_path ) in message_stdout
141+
142+
143+ async def test_culling_config (jp_server_config , jp_configurable_serverapp ):
144+ terminal_mgr_config = jp_configurable_serverapp ().config .ServerApp .TerminalManager
145+ assert terminal_mgr_config .cull_inactive_timeout == CULL_TIMEOUT
146+ assert terminal_mgr_config .cull_interval == CULL_INTERVAL
147+ terminal_mgr_settings = jp_configurable_serverapp ().web_app .settings ['terminal_manager' ]
148+ assert terminal_mgr_settings .cull_inactive_timeout == CULL_TIMEOUT
149+ assert terminal_mgr_settings .cull_interval == CULL_INTERVAL
150+
151+
152+ async def test_culling (jp_server_config , jp_fetch ):
153+ # POST request
154+ resp = await jp_fetch (
155+ 'api' , 'terminals' ,
156+ method = 'POST' ,
157+ allow_nonstandard_methods = True ,
158+ )
159+ term = json .loads (resp .body .decode ())
160+ term_1 = term ['name' ]
161+ last_activity = term ['last_activity' ]
162+
163+ culled = False
164+ for i in range (10 ): # Culling should occur in a few seconds
165+ try :
166+ resp = await jp_fetch (
167+ 'api' , 'terminals' , term_1 ,
168+ method = 'GET' ,
169+ allow_nonstandard_methods = True ,
170+ )
171+ except HTTPClientError as e :
172+ assert e .code == 404
173+ culled = True
174+ break
175+ else :
176+ await asyncio .sleep (1 )
177+
178+ assert culled
0 commit comments