File: CrashTests%2BEventLoop.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (212 lines) | stat: -rw-r--r-- 8,021 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if !canImport(Darwin) || os(macOS)
import Dispatch
import NIOCore
import NIOPosix

fileprivate let group = MultiThreadedEventLoopGroup(numberOfThreads: 2)

struct EventLoopCrashTests {
    let testMultiThreadedELGCrashesOnZeroThreads = CrashTest(
        regex: "Precondition failed: numberOfThreads must be positive"
    ) {
        try? MultiThreadedEventLoopGroup(numberOfThreads: 0).syncShutdownGracefully()
    }

    let testWaitCrashesWhenOnEL = CrashTest(
        regex: #"Precondition failed: BUG DETECTED: wait\(\) must not be called when on an EventLoop"#
    ) {
        let promise = group.next().makePromise(of: Void.self)
        try? group.next().submit {
            try? promise.futureResult.wait()
        }.wait()
    }

    let testAssertInEventLoop = CrashTest(
        regex: "Precondition failed"
    ) {
        group.next().assertInEventLoop(file: "DUMMY", line: 42)
    }

    let testPreconditionInEventLoop = CrashTest(
        regex: "Precondition failed"
    ) {
        group.next().preconditionInEventLoop(file: "DUMMY", line: 42)
    }

    let testAssertNotInEventLoop = CrashTest(
        regex: "Precondition failed"
    ) {
        let el = group.next()
        try? el.submit {
            el.assertNotInEventLoop(file: "DUMMY", line: 42)
        }.wait()
    }

    let testPreconditionNotInEventLoop = CrashTest(
        regex: "Precondition failed"
    ) {
        let el = group.next()
        try? el.submit {
            el.preconditionNotInEventLoop(file: "DUMMY", line: 42)
        }.wait()
    }

    let testSchedulingEndlesslyInELShutdown = CrashTest(
        regex: #"Precondition failed: EventLoop SelectableEventLoop \{ .* \} didn't quiesce after 1000 ticks."#
    ) {
        let group = MultiThreadedEventLoopGroup.init(numberOfThreads: 1)
        defer {
            try? group.syncShutdownGracefully()
            exit(4)
        }
        let el = group.next()
        el.scheduleTask(in: .hours(7)) {
            // Will never happen.
            exit(1)
        }.futureResult.whenFailure { error in
            guard case .some(.shutdown) = error as? EventLoopError else {
                exit(2)
            }
            func f() {
                el.scheduleTask(in: .nanoseconds(0)) { [f /* to make 5.1 compiler not crash */] in
                    f()
                }.futureResult.whenFailure { [f /* to make 5.1 compiler not crash */] error in
                    guard case .some(.shutdown) = error as? EventLoopError else {
                        exit(3)
                    }
                    f()
                }
            }
            f()
        }
    }

    let testLeakingAPromiseCrashes = CrashTest(
        regex: #"Fatal error: leaking promise created at"#
    ) {
        @inline(never)
        func leaker() {
            _ = group.next().makePromise(of: Void.self)
        }
        leaker()
        for el in group.makeIterator() {
            try! el.submit {}.wait()
        }
    }

    let testUsingTheSingletonGroupWhenDisabled = CrashTest(
        regex: #"Fatal error: Cannot create global singleton MultiThreadedEventLoopGroup because the global singletons"#
    ) {
        NIOSingletons.singletonsEnabledSuggestion = false
        try? NIOSingletons.posixEventLoopGroup.next().submit {}.wait()
    }

    let testUsingTheSingletonBlockingPoolWhenDisabled = CrashTest(
        regex: #"Fatal error: Cannot create global singleton NIOThreadPool because the global singletons have been"#
    ) {
        let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
        defer {
            try? group.syncShutdownGracefully()
        }
        NIOSingletons.singletonsEnabledSuggestion = false
        try? NIOSingletons.posixBlockingThreadPool.runIfActive(eventLoop: group.next(), {}).wait()
    }

    let testDisablingSingletonsEnabledValueTwice = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
    ) {
        NIOSingletons.singletonsEnabledSuggestion = false
        NIOSingletons.singletonsEnabledSuggestion = false
    }

    let testEnablingSingletonsEnabledValueTwice = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
    ) {
        NIOSingletons.singletonsEnabledSuggestion = true
        NIOSingletons.singletonsEnabledSuggestion = true
    }

    let testEnablingThenDisablingSingletonsEnabledValue = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
    ) {
        NIOSingletons.singletonsEnabledSuggestion = true
        NIOSingletons.singletonsEnabledSuggestion = false
    }

    let testSettingTheSingletonEnabledValueAfterUse = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
    ) {
        try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
        NIOSingletons.singletonsEnabledSuggestion = true
    }

    let testSettingTheSuggestedSingletonGroupCountTwice = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
    ) {
        NIOSingletons.groupLoopCountSuggestion = 17
        NIOSingletons.groupLoopCountSuggestion = 17
    }

    let testSettingTheSuggestedSingletonGroupChangeAfterUse = CrashTest(
        regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
    ) {
        try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
        NIOSingletons.groupLoopCountSuggestion = 17
    }

    let testSettingTheSuggestedSingletonGroupLoopCountToZero = CrashTest(
        regex: #"Precondition failed: illegal value: needs to be strictly positive"#
    ) {
        NIOSingletons.groupLoopCountSuggestion = 0
    }

    let testSettingTheSuggestedSingletonGroupLoopCountToANegativeValue = CrashTest(
        regex: #"Precondition failed: illegal value: needs to be strictly positive"#
    ) {
        NIOSingletons.groupLoopCountSuggestion = -1
    }

    #if compiler(>=5.9) && swift(<5.11) // We only support Concurrency executor take-over on 5.9-5.10, as versions greater than 5.10 have not been properly tested.
    let testInstallingSingletonMTELGAsConcurrencyExecutorWorksButOnlyOnce = CrashTest(
        regex: #"Fatal error: Must be called only once"#
    ) {
        guard NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor() else {
            print("Installation failed, that's unexpected -> let's not crash")
            return
        }

        // Yes, this pattern is bad abuse but this is a crash test, we don't mind.
        let semaphoreAbuse = DispatchSemaphore(value: 0)
        if #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) {
            Task {
                precondition(MultiThreadedEventLoopGroup.currentEventLoop != nil)
                try await Task.sleep(nanoseconds: 123)
                precondition(MultiThreadedEventLoopGroup.currentEventLoop != nil)
                semaphoreAbuse.signal()
            }
        } else {
            semaphoreAbuse.signal()
        }
        semaphoreAbuse.wait()
        print("Okay, worked")

        // This should crash
        _ = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor()
    }
    #endif // compiler(>=5.9) && swift(<5.11)
}
#endif // !canImport(Darwin) || os(macOS)