-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHarnessCoverageHelper.swift
More file actions
59 lines (49 loc) · 1.6 KB
/
HarnessCoverageHelper.swift
File metadata and controls
59 lines (49 loc) · 1.6 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
#if HARNESS_COVERAGE
import Foundation
import UIKit
@_silgen_name("__llvm_profile_write_file")
func __llvm_profile_write_file() -> Int32
@_silgen_name("__llvm_profile_set_filename")
func __llvm_profile_set_filename(_ filename: UnsafePointer<CChar>)
@objc public class HarnessCoverageHelper: NSObject {
private static var isSetUp = false
private static var flushThread: Thread?
@objc public static func setup() {
guard !isSetUp else { return }
isSetUp = true
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let profrawPath = docs.appendingPathComponent("harness-\(ProcessInfo.processInfo.processIdentifier).profraw").path
__llvm_profile_set_filename(profrawPath)
startFlushTimer()
NotificationCenter.default.addObserver(
forName: UIApplication.willTerminateNotification,
object: nil, queue: nil
) { _ in
_ = __llvm_profile_write_file()
}
NotificationCenter.default.addObserver(
forName: UIApplication.didEnterBackgroundNotification,
object: nil, queue: nil
) { _ in
_ = __llvm_profile_write_file()
}
signal(SIGTERM) { _ in
_ = __llvm_profile_write_file()
exit(0)
}
}
private static func startFlushTimer() {
let thread = Thread {
let timer = Timer(timeInterval: 1.0, repeats: true) { _ in
_ = __llvm_profile_write_file()
}
RunLoop.current.add(timer, forMode: .default)
RunLoop.current.run()
}
thread.name = "HarnessCoverageFlush"
thread.qualityOfService = .background
thread.start()
flushThread = thread
}
}
#endif