Repository navigation
Expand file tree
/
Copy pathclient_lifecycle.rs
More file actions
367 lines (336 loc) · 13.3 KB
/
Copy pathclient_lifecycle.rs
File metadata and controls
367 lines (336 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#[cfg(windows)]
use github_copilot_sdk::CliProgram;
use github_copilot_sdk::SessionLifecycleEventType;
use serde_json::json;
use super::support::{wait_for_lifecycle_event, with_e2e_context};
#[tokio::test]
async fn should_receive_session_created_lifecycle_event() {
with_e2e_context(
"client_lifecycle",
"should_receive_session_created_lifecycle_event",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let created = client.subscribe_lifecycle();
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let event =
wait_for_lifecycle_event(created, "session.created lifecycle event", |event| {
event.event_type == SessionLifecycleEventType::Created
})
.await;
assert_eq!(event.event_type, SessionLifecycleEventType::Created);
assert_eq!(&event.session_id, session.id());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_filter_session_lifecycle_events_by_type() {
with_e2e_context(
"client_lifecycle",
"should_filter_session_lifecycle_events_by_type",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let created = client.subscribe_lifecycle();
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let event = wait_for_lifecycle_event(
created,
"filtered session.created lifecycle event",
|event| event.event_type == SessionLifecycleEventType::Created,
)
.await;
assert_eq!(&event.session_id, session.id());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn disposing_lifecycle_subscription_stops_receiving_events() {
with_e2e_context(
"client_lifecycle",
"disposing_lifecycle_subscription_stops_receiving_events",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
drop(client.subscribe_lifecycle());
let created = client.subscribe_lifecycle();
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let event = wait_for_lifecycle_event(
created,
"active session.created lifecycle event",
|event| event.event_type == SessionLifecycleEventType::Created,
)
.await;
assert_eq!(event.session_id, *session.id());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn dispose_disconnects_client_and_disposes_rpc_surface_async() {
with_e2e_context(
"client_lifecycle",
"dispose_disconnects_client_and_disposes_rpc_surface_async_true",
|ctx| {
Box::pin(async move {
let client = ctx.start_client().await;
client.stop().await.expect("stop client");
assert!(
client.call("rpc.ping", Some(json!({}))).await.is_err(),
"stopped client should reject RPC calls"
);
})
},
)
.await;
}
#[tokio::test]
async fn dispose_disconnects_client_and_disposes_rpc_surface_drop() {
with_e2e_context(
"client_lifecycle",
"dispose_disconnects_client_and_disposes_rpc_surface_async_false",
|ctx| {
Box::pin(async move {
let client = ctx.start_client().await;
client.force_stop();
assert!(
client.call("rpc.ping", Some(json!({}))).await.is_err(),
"force-stopped client should reject RPC calls"
);
})
},
)
.await;
}
// This test represents github/app#2303: the SDK-hosting GitHub Copilot app
// process exits abruptly, so Client cleanup never runs. The helper starts a
// real CLI client, is terminated through `TerminateProcess`, and relies only
// on Job Object kill-on-close behavior to terminate the CLI.
#[cfg(windows)]
#[tokio::test]
async fn abrupt_host_termination_still_kills_cli_via_job_object() {
with_e2e_context(
"client_lifecycle",
"abrupt_host_termination_still_kills_cli_via_job_object",
|ctx| {
Box::pin(async move {
let options = ctx.client_options();
let program = match &options.program {
CliProgram::Path(path) => path
.to_str()
.expect("CLI program path is valid UTF-8")
.to_owned(),
CliProgram::Resolve => {
panic!("E2E client options should resolve to an explicit CLI path")
}
};
let prefix_args: Vec<String> = options
.prefix_args
.iter()
.map(|arg| arg.to_str().expect("prefix arg is valid UTF-8").to_owned())
.collect();
let env_pairs: Vec<(String, String)> = options
.env
.iter()
.map(|(k, v)| {
(
k.to_str().expect("env key is valid UTF-8").to_owned(),
v.to_str().expect("env value is valid UTF-8").to_owned(),
)
})
.collect();
let cwd = options
.working_directory
.to_str()
.expect("cwd is valid UTF-8")
.to_owned();
let pid_file = ctx.work_dir().join("host-crash-fixture-cli.pid");
let mut host =
std::process::Command::new(env!("CARGO_BIN_EXE_copilot-host-crash-fixture"))
.env("HOST_CRASH_FIXTURE_PROGRAM", &program)
.env(
"HOST_CRASH_FIXTURE_PREFIX_ARGS_JSON",
serde_json::to_string(&prefix_args).expect("serialize prefix args"),
)
.env("HOST_CRASH_FIXTURE_CWD", &cwd)
.env(
"HOST_CRASH_FIXTURE_ENV_JSON",
serde_json::to_string(&env_pairs).expect("serialize env pairs"),
)
.env("HOST_CRASH_FIXTURE_PID_FILE", &pid_file)
.spawn()
.expect("spawn host-crash fixture process");
let cli_pid = wait_for_pid_file_windows(&pid_file).await;
assert!(
process_alive_windows(cli_pid),
"CLI should be alive before its host process is terminated"
);
// `Child::kill` maps to `TerminateProcess`, which runs none
// of the target process's cleanup code.
host.kill().expect("terminate host-crash fixture process");
host.wait().expect("reap host-crash fixture process");
let cli_exited = wait_for_process_exit_windows(cli_pid).await;
if !cli_exited {
kill_process_windows(cli_pid);
}
assert!(
cli_exited,
"CLI survived its abruptly terminated host process; Job Object \
kill-on-close did not terminate it"
);
})
},
)
.await;
}
#[cfg(windows)]
async fn wait_for_pid_file_windows(path: &std::path::Path) -> u32 {
super::support::wait_for_condition("host-crash fixture CLI pid file", || async {
path.exists()
})
.await;
std::fs::read_to_string(path)
.expect("read host-crash fixture CLI pid")
.trim()
.parse()
.expect("parse host-crash fixture CLI pid")
}
#[cfg(windows)]
async fn wait_for_process_exit_windows(pid: u32) -> bool {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
while process_alive_windows(pid) {
if std::time::Instant::now() >= deadline {
return false;
}
tokio::time::sleep(std::time::Duration::from_millis(25)).await;
}
true
}
#[cfg(windows)]
fn process_alive_windows(pid: u32) -> bool {
use windows_sys::Win32::Foundation::{CloseHandle, WAIT_TIMEOUT};
use windows_sys::Win32::System::Threading::{
OpenProcess, PROCESS_SYNCHRONIZE, WaitForSingleObject,
};
// SAFETY: the process handle is closed before returning.
unsafe {
let process = OpenProcess(PROCESS_SYNCHRONIZE, 0, pid);
if process.is_null() {
return false;
}
let alive = WaitForSingleObject(process, 0) == WAIT_TIMEOUT;
CloseHandle(process);
alive
}
}
#[cfg(windows)]
fn kill_process_windows(pid: u32) {
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_TERMINATE, TerminateProcess};
// SAFETY: the pid came from this test's controlled fixture-spawned CLI.
unsafe {
let process = OpenProcess(PROCESS_TERMINATE, 0, pid);
if !process.is_null() {
TerminateProcess(process, 1);
CloseHandle(process);
}
}
}
#[tokio::test]
async fn should_receive_session_updated_lifecycle_event_for_non_ephemeral_activity() {
with_e2e_context(
"client_lifecycle",
"should_receive_session_updated_lifecycle_event_for_non_ephemeral_activity",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let updated = client.subscribe_lifecycle();
session
.client()
.call(
"session.mode.set",
Some(json!({
"sessionId": session.id().as_str(),
"mode": "plan",
})),
)
.await
.expect("set session mode");
let event =
wait_for_lifecycle_event(updated, "session.updated lifecycle event", |event| {
event.event_type == SessionLifecycleEventType::Updated
&& event.session_id == *session.id()
})
.await;
assert_eq!(event.event_type, SessionLifecycleEventType::Updated);
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_receive_session_deleted_lifecycle_event_when_deleted() {
with_e2e_context(
"client_lifecycle",
"should_receive_session_deleted_lifecycle_event_when_deleted",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let session_id = session.id().clone();
session
.send_and_wait("Say SESSION_DELETED_OK exactly.")
.await
.expect("send");
let deleted = client.subscribe_lifecycle();
client
.delete_session(&session_id)
.await
.expect("delete session");
let event =
wait_for_lifecycle_event(deleted, "session.deleted lifecycle event", |event| {
event.event_type == SessionLifecycleEventType::Deleted
&& event.session_id == session_id
})
.await;
assert_eq!(event.event_type, SessionLifecycleEventType::Deleted);
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}