File tree Expand file tree Collapse file tree 4 files changed +70
-2
lines changed
Sources/signal-handling-tests-helper
Tests/SignalHandlingTests Expand file tree Collapse file tree 4 files changed +70
-2
lines changed Original file line number Diff line number Diff line change 1+ import Foundation
2+
3+ import ArgumentParser
4+ import CLTLogger
5+ import Logging
6+ import SystemPackage
7+
8+ import SignalHandling
9+
10+
11+
12+ struct DelaySignalBlock : ParsableCommand {
13+
14+ @Option
15+ var signalNumber : CInt
16+
17+ func run( ) throws {
18+ try SigactionDelayer_Block . bootstrap ( for: [ Signal ( rawValue: signalNumber) ] )
19+
20+ LoggingSystem . bootstrap { _ in CLTLogger ( ) }
21+ SignalHandlingConfig . logger? . logLevel = . trace
22+
23+ let signal = Signal ( rawValue: signalNumber)
24+
25+ try Sigaction ( handler: . ansiC( { _ in writeToStdout ( " in sigaction handler " ) } ) ) . install ( on: signal)
26+
27+ _ = try SigactionDelayer_Block . registerDelayedSigaction ( signal, handler: { _, doneHandler in
28+ DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + . milliseconds( 500 ) , execute: {
29+ writeToStdout ( " allowing signal to be resent " )
30+ doneHandler ( true )
31+ } )
32+ } )
33+
34+ Thread . sleep ( until: . distantFuture)
35+ }
36+
37+ }
38+
39+ /* Using print does not work in Terminal probably due to buffering. */
40+ private func writeToStdout( _ str: String ) {
41+ try ! FileDescriptor . standardOutput. writeAll ( Data ( ( str + " \n " ) . utf8) )
42+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ struct DelaySignalUnsigaction : ParsableCommand {
1515 var signalNumber : CInt
1616
1717 func run( ) throws {
18- // try SigactionDelayer_Block.bootstrap(for: [Signal(rawValue: signalNumber)])
1918 LoggingSystem . bootstrap { _ in CLTLogger ( ) }
2019 SignalHandlingConfig . logger? . logLevel = . trace
2120
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ struct SignalHandlingTestsHelper : ParsableCommand {
1111 subcommands: [
1212 ManualTest . self,
1313 ManualDispatchMemTest . self,
14+ DelaySignalBlock . self,
1415 DelaySignalUnsigaction . self
1516 ]
1617 )
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ final class SignalHandlingTests : XCTestCase {
2222 SignalHandlingConfig . logger? . logLevel = . trace
2323 }
2424
25- func testBasicUnsigaction ( ) throws {
25+ func testBasicSignalDelayByUnsigaction ( ) throws {
2626 let pipe = Pipe ( )
2727
2828 let p = Process ( )
@@ -48,6 +48,32 @@ final class SignalHandlingTests : XCTestCase {
4848 """ . utf8) )
4949 }
5050
51+ func testBasicSignalDelayByBlock( ) throws {
52+ let pipe = Pipe ( )
53+
54+ let p = Process ( )
55+ p. standardOutput = pipe
56+ p. executableURL = Self . helperURL
57+ p. arguments = [ " delay-signal-block " , " --signal-number " , " \( Signal . terminated. rawValue) " ]
58+
59+ try p. run ( )
60+
61+ Thread . sleep ( forTimeInterval: 0.125 ) /* If we go too soon, the handler are not installed yet */
62+ kill ( p. processIdentifier, Signal . terminated. rawValue)
63+
64+ Thread . sleep ( forTimeInterval: 0.750 )
65+ kill ( p. processIdentifier, Signal . interrupt. rawValue)
66+
67+ let data = try pipe. fileHandleForReading. readToEnd ( )
68+ p. waitUntilExit ( )
69+
70+ XCTAssertEqual ( data, Data ( """
71+ allowing signal to be resent
72+ in sigaction handler
73+
74+ """ . utf8) )
75+ }
76+
5177 /** Returns the path to the built products directory. */
5278 private static var productsDirectory : URL {
5379 #if os(macOS)
You can’t perform that action at this time.
0 commit comments