Skip to content

Commit d2c2650

Browse files
committed
Add a test
1 parent 1fd84e6 commit d2c2650

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
3+
import ArgumentParser
4+
import CLTLogger
5+
import Logging
6+
7+
import SignalHandling
8+
9+
10+
11+
struct DelaySignalUnsigaction : ParsableCommand {
12+
13+
@Option
14+
var signalNumber: CInt
15+
16+
func run() throws {
17+
LoggingSystem.bootstrap{ _ in CLTLogger() }
18+
SignalHandlingConfig.logger?.logLevel = .trace
19+
20+
let signal = Signal(rawValue: signalNumber)
21+
22+
try Sigaction(handler: .ansiC({ _ in print("in sigaction handler") })).install(on: signal)
23+
24+
_ = try SigactionDelayer_Unsig.registerDelayedSigaction(signal, handler: { _, doneHandler in
25+
DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(500), execute: {
26+
print("allowing signal to be resent")
27+
doneHandler(true)
28+
})
29+
})
30+
31+
Thread.sleep(until: .distantFuture)
32+
}
33+
34+
}

Sources/signal-handling-tests-helper/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ struct SignalHandlingTestsHelper : ParsableCommand {
88

99
static var configuration = CommandConfiguration(
1010
subcommands: [
11-
ManualTest.self
11+
ManualTest.self,
12+
DelaySignalUnsigaction.self
1213
]
1314
)
1415

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
import Foundation
22
import XCTest
33

4+
import CLTLogger
5+
import Logging
6+
47
@testable import SignalHandling
58

69

710

11+
@available(OSX 10.15.4, *)
812
final class SignalHandlingTests : XCTestCase {
13+
14+
static let helperURL = productsDirectory.appendingPathComponent("signal-handling-tests-helper")
15+
16+
override class func setUp() {
17+
super.setUp()
18+
19+
/* Setup the logger – Not needed for most tests as we launch an external
20+
 * executable to test. */
21+
LoggingSystem.bootstrap{ _ in CLTLogger() }
22+
SignalHandlingConfig.logger?.logLevel = .trace
23+
}
24+
25+
func testBasicUnsigaction() throws {
26+
let pipe = Pipe()
27+
28+
let p = Process()
29+
p.standardOutput = pipe
30+
p.standardError = FileHandle.standardError
31+
p.executableURL = Self.helperURL
32+
p.arguments = ["delay-signal-unsigaction", "--signal-number", "\(Signal.terminated.rawValue)"]
33+
34+
try p.run()
35+
36+
Thread.sleep(forTimeInterval: 0.125) /* If we go too soon, the handler are not installed yet */
37+
kill(p.processIdentifier, Signal.terminated.rawValue)
38+
39+
Thread.sleep(forTimeInterval: 0.750)
40+
kill(p.processIdentifier, Signal.interrupt.rawValue)
41+
42+
let data = try pipe.fileHandleForReading.readToEnd()
43+
p.waitUntilExit()
44+
45+
XCTAssertEqual(data, Data("""
46+
allowing signal to be resent
47+
in sigaction handler
48+
49+
""".utf8))
50+
}
51+
52+
/** Returns the path to the built products directory. */
53+
private static var productsDirectory: URL {
54+
#if os(macOS)
55+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
56+
return bundle.bundleURL.deletingLastPathComponent()
57+
}
58+
fatalError("couldn't find the products directory")
59+
#else
60+
return Bundle.main.bundleURL
61+
#endif
62+
}
63+
964
}

0 commit comments

Comments
 (0)