-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSocketPool+PollTests.swift
250 lines (216 loc) · 7.23 KB
/
SocketPool+PollTests.swift
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// SocketPool+PollTests.swift
// FlyingFox
//
// Created by Simon Whitty on 25/09/2022.
// Copyright © 2022 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
@testable import FlyingSocks
import XCTest
final class PollTests: XCTestCase {
func testInterval() {
XCTAssertEqual(
Poll.Interval.immediate.milliseconds,
0
)
XCTAssertEqual(
Poll.Interval.seconds(1).milliseconds,
1000
)
}
func testAddingAndRemovingEvents() throws {
var queue = Poll.make()
XCTAssertNoThrow(try queue.addEvents(.connection, for: .validMock))
XCTAssertEqual(queue.entries, [.init(file: .validMock, events: .connection)])
XCTAssertNoThrow(try queue.removeEvents(.connection, for: .validMock))
XCTAssertEqual(queue.entries, [])
}
func testAddingEventWhenNotOpen_ThrowsError() {
var queue = Poll.make()
queue.stop()
XCTAssertThrowsError(
try queue.addEvents(.read, for: .validMock)
)
}
func testRemovingEventWhenNotOpen_ThrowsError() {
var queue = Poll.make()
queue.stop()
XCTAssertThrowsError(
try queue.removeEvents(.read, for: .validMock)
)
}
func testReadEntry_CreatesPollFD() {
let entry = Poll.Entry(file: .init(rawValue: 10), events: .read)
XCTAssertEqual(entry.pollfd.fd, 10)
XCTAssertEqual(entry.pollfd.events, Int16(POLLIN))
XCTAssertEqual(entry.pollfd.revents, 0)
}
func testWriteEntry_CreatesPollFD() {
let entry = Poll.Entry(file: .init(rawValue: 20), events: .write)
XCTAssertEqual(entry.pollfd.fd, 20)
XCTAssertEqual(entry.pollfd.events, Int16(POLLOUT))
XCTAssertEqual(entry.pollfd.revents, 0)
}
func testConnectionEntry_CreatesPollFD() {
let entry = Poll.Entry(file: .init(rawValue: 30), events: .connection)
XCTAssertEqual(entry.pollfd.fd, 30)
XCTAssertEqual(entry.pollfd.events, Int16(POLLOUT | POLLIN))
XCTAssertEqual(entry.pollfd.revents, 0)
}
func testReadResult_CreatesNotification() {
XCTAssertEqual(
EventNotification.make(from: pollfd.make(
fd: 10,
events: POLLIN,
revents: POLLIN
)),
EventNotification(
file: .init(rawValue: 10),
events: .read,
errors: []
)
)
}
func testErrorsIgnored_WhenReadWithDataAvailable() {
XCTAssertEqual(
EventNotification.make(from: pollfd.make(
fd: 10,
events: POLLIN,
revents: POLLIN | POLLHUP
)),
EventNotification(
file: .init(rawValue: 10),
events: .read,
errors: []
)
)
}
func testWriteResult_CreatesNotification() {
XCTAssertEqual(
EventNotification.make(from: pollfd.make(
fd: 10,
events: POLLOUT,
revents: POLLOUT
)),
EventNotification(
file: .init(rawValue: 10),
events: .write,
errors: []
)
)
}
func testWriteHUP_CreatesNotification() {
XCTAssertEqual(
EventNotification.make(from: pollfd.make(
fd: 10,
events: POLLIN | POLLOUT,
revents: POLLHUP
)),
EventNotification(
file: .init(rawValue: 10),
events: .connection,
errors: [.endOfFile]
)
)
}
func testWriteErrors_CreatesNotification() {
XCTAssertEqual(
EventNotification.make(from: pollfd.make(
fd: 10,
events: POLLOUT,
revents: POLLERR
)),
EventNotification(
file: .init(rawValue: 10),
events: .write,
errors: [.error]
)
)
}
func testUnmatchedRevents_DoesNotCreateNotification() {
XCTAssertNil(
EventNotification.make(from: pollfd.make(
events: POLLIN,
revents: 0
))
)
XCTAssertNil(
EventNotification.make(from: pollfd.make(
events: POLLIN,
revents: POLLOUT
))
)
XCTAssertNil(
EventNotification.make(from: pollfd.make(
events: POLLIN | POLLOUT,
revents: 0
))
)
}
func testGetEvents_ReturnsEvents() async throws {
var queue = Poll.make()
let (s1, s2) = try Socket.makeNonBlockingPair()
try queue.addEvents([.read], for: s2.file)
let data = Data([10, 20])
_ = try s1.write(data, from: data.startIndex)
await AsyncAssertEqual(
try await queue.getEvents(),
[.init(file: s2.file, events: [.read], errors: [])]
)
}
func testGetEventsWhenNotReady_ThrowsError() async {
var queue = Poll.make()
queue.stop()
await AsyncAssertThrowsError(
try await queue.getEvents()
)
}
}
private extension Poll {
static func make() -> Self {
var queue = Poll(interval: .immediate)
queue.open()
return queue
}
func getEvents() async throws -> [EventNotification] {
let queue = UncheckedSendable(wrappedValue: self)
return try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global().async {
let result = Result {
try queue.wrappedValue.getNotifications()
}
continuation.resume(with: result)
}
}
}
}
private extension pollfd {
static func make(fd: Int32 = 0,
events: Int32 = POLLIN,
revents: Int32 = POLLIN) -> Self {
.init(fd: fd, events: Int16(events), revents: Int16(revents))
}
}