5
5
import asyncio
6
6
import sys
7
7
8
+ from tornado .httpclient import HTTPClientError
9
+ from traitlets .config import Config
10
+
8
11
# Skip this whole module on Windows. The terminal API leads
9
12
# to timeouts on Windows CI.
10
13
if sys .platform .startswith ('win' ):
@@ -30,12 +33,42 @@ def terminal_path(tmp_path):
30
33
shutil .rmtree (str (subdir ), ignore_errors = True )
31
34
32
35
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
+
33
64
async def test_terminal_create (jp_fetch , kill_all ):
34
- await jp_fetch (
65
+ resp = await jp_fetch (
35
66
'api' , 'terminals' ,
36
67
method = 'POST' ,
37
68
allow_nonstandard_methods = True ,
38
69
)
70
+ term = json .loads (resp .body .decode ())
71
+ assert term ['name' ] == "1"
39
72
40
73
resp_list = await jp_fetch (
41
74
'api' , 'terminals' ,
@@ -46,6 +79,7 @@ async def test_terminal_create(jp_fetch, kill_all):
46
79
data = json .loads (resp_list .body .decode ())
47
80
48
81
assert len (data ) == 1
82
+ assert data [0 ] == term
49
83
await kill_all ()
50
84
51
85
@@ -104,3 +138,41 @@ async def test_terminal_create_with_cwd(jp_fetch, jp_ws_fetch, terminal_path):
104
138
ws .close ()
105
139
106
140
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