process-wrap 10.0.0

Wrap a Command, to spawn processes in a group or session or job etc
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#![cfg(all(any(feature = "std", feature = "tokio1"), any(unix, windows)))]

macro_rules! spawn_with_child_tests {
	(
		$module:ident,
		$command:path,
		$child:path,
		$command_wrap:path,
		$command_wrapper:path,
		$child_wrapper:path,
		$runtime:expr
	) => {
		mod $module {
			use std::{
				any::TypeId,
				io,
				panic::{AssertUnwindSafe, catch_unwind},
				sync::{Arc, Mutex},
				thread::sleep,
				time::{Duration, Instant},
			};

			use $child as Child;
			use $child_wrapper as ChildWrapper;
			use $command as Command;
			use $command_wrap as CommandWrap;
			use $command_wrapper as CommandWrapper;

			const EXIT_TIMEOUT: Duration = Duration::from_secs(5);

			#[derive(Clone, Copy, Debug, Eq, PartialEq)]
			enum Event {
				Pre(&'static str),
				Spawn,
				Post(&'static str),
				Wrap(&'static str),
			}

			#[derive(Debug)]
			struct First(Arc<Mutex<Vec<Event>>>);

			impl CommandWrapper for First {
				fn pre_spawn(
					&mut self,
					_command: &mut Command,
					_core: &CommandWrap,
				) -> io::Result<()> {
					self.0.lock().unwrap().push(Event::Pre("first"));
					Ok(())
				}

				fn post_spawn(
					&mut self,
					_command: &mut Command,
					_child: &mut Child,
					_core: &CommandWrap,
				) -> io::Result<()> {
					self.0.lock().unwrap().push(Event::Post("first"));
					Ok(())
				}

				fn wrap_child(
					&mut self,
					child: Box<dyn ChildWrapper>,
					_core: &CommandWrap,
				) -> io::Result<Box<dyn ChildWrapper>> {
					self.0.lock().unwrap().push(Event::Wrap("first"));
					Ok(Box::new(FirstChild(child)))
				}
			}

			#[derive(Debug)]
			struct Second(Arc<Mutex<Vec<Event>>>);

			impl CommandWrapper for Second {
				fn pre_spawn(
					&mut self,
					_command: &mut Command,
					_core: &CommandWrap,
				) -> io::Result<()> {
					self.0.lock().unwrap().push(Event::Pre("second"));
					Ok(())
				}

				fn post_spawn(
					&mut self,
					_command: &mut Command,
					_child: &mut Child,
					_core: &CommandWrap,
				) -> io::Result<()> {
					self.0.lock().unwrap().push(Event::Post("second"));
					Ok(())
				}

				fn wrap_child(
					&mut self,
					child: Box<dyn ChildWrapper>,
					_core: &CommandWrap,
				) -> io::Result<Box<dyn ChildWrapper>> {
					self.0.lock().unwrap().push(Event::Wrap("second"));
					Ok(Box::new(SecondChild(child)))
				}
			}

			#[derive(Debug)]
			struct FirstChild(Box<dyn ChildWrapper>);

			impl ChildWrapper for FirstChild {
				fn inner(&self) -> &dyn ChildWrapper {
					self.0.as_ref()
				}

				fn inner_mut(&mut self) -> &mut dyn ChildWrapper {
					self.0.as_mut()
				}

				fn into_inner(self: Box<Self>) -> Box<dyn ChildWrapper> {
					self.0
				}

				#[cfg(windows)]
				fn process_handle(&self) -> Option<std::os::windows::io::BorrowedHandle<'_>> {
					self.0.process_handle()
				}
			}

			#[derive(Debug)]
			struct SecondChild(Box<dyn ChildWrapper>);

			impl ChildWrapper for SecondChild {
				fn inner(&self) -> &dyn ChildWrapper {
					self.0.as_ref()
				}

				fn inner_mut(&mut self) -> &mut dyn ChildWrapper {
					self.0.as_mut()
				}

				fn into_inner(self: Box<Self>) -> Box<dyn ChildWrapper> {
					self.0
				}

				#[cfg(windows)]
				fn process_handle(&self) -> Option<std::os::windows::io::BorrowedHandle<'_>> {
					self.0.process_handle()
				}
			}

			#[derive(Debug)]
			struct CustomLeaf;

			impl ChildWrapper for CustomLeaf {
				fn inner(&self) -> &dyn ChildWrapper {
					self
				}

				fn inner_mut(&mut self) -> &mut dyn ChildWrapper {
					self
				}

				fn into_inner(self: Box<Self>) -> Box<dyn ChildWrapper> {
					self
				}
			}

			#[derive(Clone, Copy, Debug, Eq, PartialEq)]
			enum Phase {
				Pre,
				Wrap,
			}

			#[derive(Clone, Copy, Debug, Eq, PartialEq)]
			enum Failure {
				Error,
				Panic,
			}

			#[derive(Debug)]
			struct FailOnce {
				phase: Phase,
				failure: Failure,
				failed: bool,
			}

			impl FailOnce {
				fn visit(&mut self, phase: Phase) -> io::Result<()> {
					if self.phase != phase || self.failed {
						return Ok(());
					}

					self.failed = true;
					match self.failure {
						Failure::Error => Err(io::Error::other("fail once")),
						Failure::Panic => panic!("fail once"),
					}
				}
			}

			impl CommandWrapper for FailOnce {
				fn pre_spawn(
					&mut self,
					_command: &mut Command,
					_core: &CommandWrap,
				) -> io::Result<()> {
					self.visit(Phase::Pre)
				}

				fn post_spawn(
					&mut self,
					_command: &mut Command,
					_child: &mut Child,
					_core: &CommandWrap,
				) -> io::Result<()> {
					panic!("boxed-child spawning must not run post_spawn")
				}

				fn wrap_child(
					&mut self,
					child: Box<dyn ChildWrapper>,
					_core: &CommandWrap,
				) -> io::Result<Box<dyn ChildWrapper>> {
					self.visit(Phase::Wrap)?;
					Ok(child)
				}
			}

			fn runtime() -> Option<tokio::runtime::Runtime> {
				$runtime
			}

			fn command() -> CommandWrap {
				#[cfg(unix)]
				return CommandWrap::with_new("sh", |command| {
					command.args(["-c", "exit 0"]);
				});

				#[cfg(windows)]
				return CommandWrap::with_new("cmd.exe", |command| {
					command.args(["/D", "/S", "/C", "exit /b 0"]);
				});
			}

			fn wait_for_exit(mut child: Box<dyn ChildWrapper>) {
				let deadline = Instant::now() + EXIT_TIMEOUT;
				loop {
					if child.try_wait().unwrap().is_some() {
						return;
					}
					assert!(
						Instant::now() < deadline,
						"child did not exit before timeout"
					);
					sleep(Duration::from_millis(10));
				}
			}

			fn recover_hook(failure: Failure, phase: Phase) {
				let runtime = runtime();
				let _runtime_guard = runtime.as_ref().map(tokio::runtime::Runtime::enter);
				let mut command = command();
				command.wrap(FailOnce {
					phase,
					failure,
					failed: false,
				});

				let mut spawn = || {
					command.spawn_with_child(|_| Ok(Box::new(CustomLeaf) as Box<dyn ChildWrapper>))
				};
				match failure {
					Failure::Error => assert_eq!(
						spawn().expect_err("the first hook must fail").to_string(),
						"fail once"
					),
					Failure::Panic => assert!(catch_unwind(AssertUnwindSafe(spawn)).is_err()),
				}

				assert!(command.get_wrap::<FailOnce>().unwrap().failed);
				let child = command
					.spawn_with_child(|command| {
						command
							.spawn()
							.map(|child| Box::new(child) as Box<dyn ChildWrapper>)
					})
					.expect("the restored command and wrapper must be reusable");
				wait_for_exit(child);
			}

			fn recover_spawner(failure: Failure) {
				let runtime = runtime();
				let _runtime_guard = runtime.as_ref().map(tokio::runtime::Runtime::enter);
				let mut command = command();

				match failure {
					Failure::Error => {
						let error = command
							.spawn_with_child(|_| Err(io::Error::other("spawner failed")))
							.expect_err("the spawner must fail");
						assert_eq!(error.to_string(), "spawner failed");
					}
					Failure::Panic => {
						let panic = catch_unwind(AssertUnwindSafe(|| {
							let _ = command.spawn_with_child(
								|_| -> io::Result<Box<dyn ChildWrapper>> {
									panic!("spawner failed")
								},
							);
						}));
						assert!(panic.is_err());
					}
				}

				let child = command
					.spawn_with_child(|command| {
						command
							.spawn()
							.map(|child| Box::new(child) as Box<dyn ChildWrapper>)
					})
					.expect("the restored command must be reusable");
				wait_for_exit(child);
			}

			#[test]
			fn boxed_child_runs_pre_spawn_and_wrap_child_without_post_spawn() {
				let runtime = runtime();
				let _runtime_guard = runtime.as_ref().map(tokio::runtime::Runtime::enter);
				let events = Arc::new(Mutex::new(Vec::new()));
				let mut command = command();
				command
					.wrap(First(Arc::clone(&events)))
					.wrap(Second(Arc::clone(&events)));

				let child = command
					.spawn_with_child(|_| {
						events.lock().unwrap().push(Event::Spawn);
						Ok(Box::new(CustomLeaf) as Box<dyn ChildWrapper>)
					})
					.expect("spawn custom child");

				assert_eq!(child.as_ref().type_id(), TypeId::of::<SecondChild>());
				assert_eq!(child.inner().type_id(), TypeId::of::<FirstChild>());
				assert_eq!(child.inner().inner().type_id(), TypeId::of::<CustomLeaf>());
				assert_eq!(
					*events.lock().unwrap(),
					vec![
						Event::Pre("first"),
						Event::Pre("second"),
						Event::Spawn,
						Event::Wrap("first"),
						Event::Wrap("second"),
					]
				);
			}

			#[test]
			fn native_spawn_with_still_runs_post_spawn() {
				let runtime = runtime();
				let _runtime_guard = runtime.as_ref().map(tokio::runtime::Runtime::enter);
				let events = Arc::new(Mutex::new(Vec::new()));
				let mut command = command();
				command
					.wrap(First(Arc::clone(&events)))
					.wrap(Second(Arc::clone(&events)));

				let child = command
					.spawn_with(|command| {
						events.lock().unwrap().push(Event::Spawn);
						command.spawn()
					})
					.expect("spawn native child");
				wait_for_exit(child);

				assert_eq!(
					*events.lock().unwrap(),
					vec![
						Event::Pre("first"),
						Event::Pre("second"),
						Event::Spawn,
						Event::Post("first"),
						Event::Post("second"),
						Event::Wrap("first"),
						Event::Wrap("second"),
					]
				);
			}

			#[test]
			fn boxed_child_restores_hooks_after_errors() {
				for phase in [Phase::Pre, Phase::Wrap] {
					recover_hook(Failure::Error, phase);
				}
			}

			#[test]
			fn boxed_child_restores_hooks_after_panics() {
				for phase in [Phase::Pre, Phase::Wrap] {
					recover_hook(Failure::Panic, phase);
				}
			}

			#[test]
			fn boxed_child_restores_command_after_spawner_error() {
				recover_spawner(Failure::Error);
			}

			#[test]
			fn boxed_child_restores_command_after_spawner_panic() {
				recover_spawner(Failure::Panic);
			}
		}
	};
}

#[cfg(feature = "std")]
spawn_with_child_tests!(
	std_frontend,
	std::process::Command,
	std::process::Child,
	process_wrap::std::CommandWrap,
	process_wrap::std::CommandWrapper,
	process_wrap::std::ChildWrapper,
	None
);

#[cfg(feature = "tokio1")]
spawn_with_child_tests!(
	tokio_frontend,
	tokio::process::Command,
	tokio::process::Child,
	process_wrap::tokio::CommandWrap,
	process_wrap::tokio::CommandWrapper,
	process_wrap::tokio::ChildWrapper,
	Some(
		tokio::runtime::Builder::new_current_thread()
			.enable_all()
			.build()
			.unwrap()
	)
);