LeaksTests.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // LeaksTests.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Testing
  25. import XCTest
  26. // Only build when built through SPM, as tests run through Xcode don't like this.
  27. // Add LEAKS flag once we figure out a way to automate this.
  28. // Can run by invoking swift test -c debug -Xswiftc -DLEAKS in the Alamofire directory.
  29. // Sample code from the Swift forums: https://forums.swift.org/t/test-for-memory-leaks-in-ci/36526/19
  30. #if SWIFT_PACKAGE && LEAKS && os(macOS)
  31. final class LeaksTests: XCTestCase {
  32. @MainActor
  33. func testForLeaks() {
  34. // Sets up an atexit handler that invokes the leaks tool.
  35. atexit {
  36. @discardableResult
  37. func leaksTo(_ file: String) -> Process {
  38. let out = FileHandle(forWritingAtPath: file)!
  39. defer {
  40. try! out.close()
  41. }
  42. let process = Process()
  43. process.launchPath = "/usr/bin/leaks"
  44. process.arguments = ["\(getpid())"]
  45. process.standardOutput = out
  46. process.standardError = out
  47. process.launch()
  48. process.waitUntilExit()
  49. return process
  50. }
  51. let process = leaksTo("/dev/null")
  52. guard process.terminationReason == .exit && [0, 1].contains(process.terminationStatus) else {
  53. print("Process terminated: \(process.terminationReason): \(process.terminationStatus)")
  54. exit(255)
  55. }
  56. if process.terminationStatus == 1 {
  57. print("================")
  58. print("Leaks Detected!!!")
  59. leaksTo("/dev/tty")
  60. }
  61. exit(process.terminationStatus)
  62. }
  63. }
  64. }
  65. @Suite
  66. struct SwiftTestingLeaksTest {
  67. @Test
  68. func forLeaks() {
  69. // Sets up an atexit handler that invokes the leaks tool.
  70. atexit {
  71. @discardableResult
  72. func leaksTo(_ file: String) -> Process {
  73. let out = FileHandle(forWritingAtPath: file)!
  74. defer {
  75. try! out.close()
  76. }
  77. let process = Process()
  78. process.launchPath = "/usr/bin/leaks"
  79. process.arguments = ["\(getpid())"]
  80. process.standardOutput = out
  81. process.standardError = out
  82. process.launch()
  83. process.waitUntilExit()
  84. return process
  85. }
  86. let process = leaksTo("/dev/null")
  87. guard process.terminationReason == .exit && [0, 1].contains(process.terminationStatus) else {
  88. print("Process terminated: \(process.terminationReason): \(process.terminationStatus)")
  89. exit(255)
  90. }
  91. if process.terminationStatus == 1 {
  92. print("================")
  93. print("Leaks Detected!!!")
  94. leaksTo("/dev/tty")
  95. }
  96. exit(process.terminationStatus)
  97. }
  98. }
  99. }
  100. #endif