Skip to content

Commit

Permalink
forward #file to #filePath differently
Browse files Browse the repository at this point in the history
Motivation:

only works if done when _calling_ a function, not when defining one :|.

- swiftlang/swift#32445
- https://bugs.swift.org/browse/SR-12936
- https://bugs.swift.org/browse/SR-12934
- https://bugs.swift.org/browse/SR-13041

Modifications:

Silence #file to #filePath differently.

Result:

Hopefully at some point we get this working.
  • Loading branch information
weissi committed Jun 18, 2020
1 parent 7d2e632 commit 790bea0
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ func spawnAndJoinRacingThreads(count: Int, _ body: @escaping (Int) -> Void) {
group.wait()
}

func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testInterval: TimeAmount? = nil, _ message: String = "condition not satisfied in time", file: StaticString = (#file), line: UInt = #line) {
func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testInterval: TimeAmount? = nil, _ message: String = "condition not satisfied in time", file: StaticString = #file, line: UInt = #line) {
let testInterval = testInterval ?? TimeAmount.nanoseconds(time.nanoseconds / 5)
let endTime = NIODeadline.now() + time

Expand All @@ -1035,7 +1035,7 @@ func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testI
} while (NIODeadline.now() < endTime)

if !condition() {
XCTFail(message, file: file, line: line)
XCTFail(message, file: (file), line: line)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/NIOHTTP1Tests/HTTPServerUpgradeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ extension ChannelPipeline {
}

func assertDoesNotContain<Handler: ChannelHandler>(handlerType: Handler.Type,
file: StaticString = (#file),
file: StaticString = #file,
line: UInt = #line) throws {
do {
let context = try self.context(handlerType: handlerType).wait()
XCTFail("Found handler: \(context.handler)", file: file, line: line)
XCTFail("Found handler: \(context.handler)", file: (file), line: line)
} catch ChannelPipelineError.notFound {
// Nothing to see here
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/NIOTestUtilsTests/NIOHTTP1TestServerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,34 @@ private final class TestHTTPHandler: ChannelInboundHandler {
}

extension HTTPServerRequestPart {
func assertHead(expectedURI: String, file: StaticString = (#file), line: UInt = #line) {
func assertHead(expectedURI: String, file: StaticString = #file, line: UInt = #line) {
switch self {
case .head(let head):
XCTAssertEqual(.GET, head.method)
XCTAssertEqual(expectedURI, head.uri)
XCTAssertEqual("text/plain; charset=utf-8", head.headers["Content-Type"].first)
default:
XCTFail("Expected head, got \(self)", file: file, line: line)
XCTFail("Expected head, got \(self)", file: (file), line: line)
}
}

func assertBody(expectedMessage: String, file: StaticString = (#file), line: UInt = #line) {
func assertBody(expectedMessage: String, file: StaticString = #file, line: UInt = #line) {
switch self {
case .body(let buffer):
// Note that the test server coalesces the body parts for us.
XCTAssertEqual(expectedMessage,
String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
default:
XCTFail("Expected body, got \(self)", file: file, line: line)
XCTFail("Expected body, got \(self)", file: (file), line: line)
}
}

func assertEnd(file: StaticString = (#file), line: UInt = #line) {
func assertEnd(file: StaticString = #file, line: UInt = #line) {
switch self {
case .end(_):
()
default:
XCTFail("Expected end, got \(self)", file: file, line: line)
XCTFail("Expected end, got \(self)", file: (file), line: line)
}
}
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func assert(_ condition: @autoclosure () -> Bool,
within time: TimeAmount,
testInterval: TimeAmount? = nil,
_ message: String = "condition not satisfied in time",
file: StaticString = (#file), line: UInt = #line) {
file: StaticString = #file, line: UInt = #line) {
let testInterval = testInterval ?? TimeAmount.nanoseconds(time.nanoseconds / 5)
let endTime = NIODeadline.now() + time

Expand All @@ -349,6 +349,6 @@ func assert(_ condition: @autoclosure () -> Bool,
} while (NIODeadline.now() < endTime)

if !condition() {
XCTFail(message, file: file, line: line)
XCTFail(message, file: (file), line: line)
}
}
1 change: 1 addition & 0 deletions Tests/NIOTests/ByteBufferTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ extension ByteBufferTest {
("testAllocatorGivesStableZeroSizedBuffers", testAllocatorGivesStableZeroSizedBuffers),
("testClearOnZeroCapacityActuallyAllocates", testClearOnZeroCapacityActuallyAllocates),
("testCreateBufferFromSequence", testCreateBufferFromSequence),
("testWeDoNotResizeIfWeHaveExactlyTheRightCapacityAvailable", testWeDoNotResizeIfWeHaveExactlyTheRightCapacityAvailable),
("testCreateArrayFromBuffer", testCreateArrayFromBuffer),
("testCreateStringFromBuffer", testCreateStringFromBuffer),
("testCreateDispatchDataFromBuffer", testCreateDispatchDataFromBuffer),
Expand Down
36 changes: 24 additions & 12 deletions Tests/NIOTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1120,18 +1120,18 @@ class ByteBufferTest: XCTestCase {
buf.reserveCapacity(1024)
buf.writeStaticString("hello world, just some trap bytes here")

func testIndexAndLengthFunc<T>(_ body: (Int, Int) -> T?, file: StaticString = (#file), line: UInt = #line) {
XCTAssertNil(body(Int.max, 1), file: file, line: line)
XCTAssertNil(body(Int.max - 1, 2), file: file, line: line)
XCTAssertNil(body(1, Int.max), file: file, line: line)
XCTAssertNil(body(2, Int.max - 1), file: file, line: line)
XCTAssertNil(body(Int.max, Int.max), file: file, line: line)
XCTAssertNil(body(Int.min, Int.min), file: file, line: line)
XCTAssertNil(body(Int.max, Int.min), file: file, line: line)
XCTAssertNil(body(Int.min, Int.max), file: file, line: line)
}

func testIndexOrLengthFunc<T>(_ body: (Int) -> T?, file: StaticString = (#file), line: UInt = #line) {
func testIndexAndLengthFunc<T>(_ body: (Int, Int) -> T?, file: StaticString = #file, line: UInt = #line) {
XCTAssertNil(body(Int.max, 1), file: (file), line: line)
XCTAssertNil(body(Int.max - 1, 2), file: (file), line: line)
XCTAssertNil(body(1, Int.max), file: (file), line: line)
XCTAssertNil(body(2, Int.max - 1), file: (file), line: line)
XCTAssertNil(body(Int.max, Int.max), file: (file), line: line)
XCTAssertNil(body(Int.min, Int.min), file: (file), line: line)
XCTAssertNil(body(Int.max, Int.min), file: (file), line: line)
XCTAssertNil(body(Int.min, Int.max), file: (file), line: line)
}

func testIndexOrLengthFunc<T>(_ body: (Int) -> T?, file: StaticString = #file, line: UInt = #line) {
XCTAssertNil(body(Int.max))
XCTAssertNil(body(Int.max - 1))
XCTAssertNil(body(Int.min))
Expand Down Expand Up @@ -2787,6 +2787,18 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual(self.buf, self.allocator.buffer(bytes: aSequenceThatIsNotACollection))
XCTAssertEqual(self.buf, ByteBuffer(bytes: aSequenceThatIsNotACollection))
}

func testWeDoNotResizeIfWeHaveExactlyTheRightCapacityAvailable() {
let bufferSize = 32*1024
var buffer = self.allocator.buffer(capacity: bufferSize)
buffer.moveWriterIndex(forwardBy: bufferSize / 2)
XCTAssertEqual(bufferSize, buffer.capacity)
let oldBufferStorage = buffer.storagePointerIntegerValue()
buffer.writeWithUnsafeMutableBytes(minimumWritableBytes: bufferSize/2, { _ in return 0 })
XCTAssertEqual(bufferSize, buffer.capacity)
let newBufferStorage = buffer.storagePointerIntegerValue()
XCTAssertEqual(oldBufferStorage, newBufferStorage)
}
}


Expand Down
6 changes: 3 additions & 3 deletions Tests/NIOTests/ChannelNotificationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import XCTest

class ChannelNotificationTest: XCTestCase {

private static func assertFulfilled(promise: EventLoopPromise<Void>?, promiseName: String, trigger: String, setter: String, file: StaticString = (#file), line: UInt = #line) {
private static func assertFulfilled(promise: EventLoopPromise<Void>?, promiseName: String, trigger: String, setter: String, file: StaticString = #file, line: UInt = #line) {
if let promise = promise {
XCTAssertTrue(promise.futureResult.isFulfilled, "\(promiseName) not fulfilled before \(trigger) was called", file: file, line: line)
XCTAssertTrue(promise.futureResult.isFulfilled, "\(promiseName) not fulfilled before \(trigger) was called", file: (file), line: line)
} else {
XCTFail("\(setter) not called before \(trigger)", file: file, line: line)
XCTFail("\(setter) not called before \(trigger)", file: (file), line: line)
}
}

Expand Down
52 changes: 26 additions & 26 deletions Tests/NIOTests/ChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public final class ChannelTests: XCTestCase {
expectedFileWritabilities: [(Int, Int)]?,
returns: [IOResult<Int>],
promiseStates: [[Bool]],
file: StaticString = (#file),
file: StaticString = #file,
line: UInt = #line) throws -> OverallWriteResult {
var everythingState = 0
var singleState = 0
Expand All @@ -272,11 +272,11 @@ public final class ChannelTests: XCTestCase {
XCTAssertEqual(expected[singleState], buf.count, "in single write \(singleState) (overall \(everythingState)), \(expected[singleState]) bytes expected but \(buf.count) actual")
return returns[everythingState]
} else {
XCTFail("single write call \(singleState) but less than \(expected.count) expected", file: file, line: line)
XCTFail("single write call \(singleState) but less than \(expected.count) expected", file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}
} else {
XCTFail("single write called on \(buf) but no single writes expected", file: file, line: line)
XCTFail("single write called on \(buf) but no single writes expected", file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}
}, vectorBufferWriteOperation: { ptrs in
Expand All @@ -289,15 +289,15 @@ public final class ChannelTests: XCTestCase {
XCTAssertGreaterThan(returns.count, everythingState)
XCTAssertEqual(expected[multiState], ptrs.map { numericCast($0.iov_len) },
"in vector write \(multiState) (overall \(everythingState)), \(expected[multiState]) byte counts expected but \(ptrs.map { $0.iov_len }) actual",
file: file, line: line)
file: (file), line: line)
return returns[everythingState]
} else {
XCTFail("vector write call \(multiState) but less than \(expected.count) expected", file: file, line: line)
XCTFail("vector write call \(multiState) but less than \(expected.count) expected", file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}
} else {
XCTFail("vector write called on \(ptrs) but no vector writes expected",
file: file, line: line)
file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}
}, scalarFileWriteOperation: { _, start, end in
Expand All @@ -307,58 +307,58 @@ public final class ChannelTests: XCTestCase {
}
guard let expected = expectedFileWritabilities else {
XCTFail("file write (\(start), \(end)) but no file writes expected",
file: file, line: line)
file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}

if expected.count > fileState {
XCTAssertGreaterThan(returns.count, everythingState)
XCTAssertEqual(expected[fileState].0, start,
"in file write \(fileState) (overall \(everythingState)), \(expected[fileState].0) expected as start index but \(start) actual",
file: file, line: line)
file: (file), line: line)
XCTAssertEqual(expected[fileState].1, end,
"in file write \(fileState) (overall \(everythingState)), \(expected[fileState].1) expected as end index but \(end) actual",
file: file, line: line)
file: (file), line: line)
return returns[everythingState]
} else {
XCTFail("file write call \(fileState) but less than \(expected.count) expected", file: file, line: line)
XCTFail("file write call \(fileState) but less than \(expected.count) expected", file: (file), line: line)
return IOResult.wouldBlock(-1 * (everythingState + 1))
}
})
if everythingState > 0 {
XCTAssertEqual(promises.count, promiseStates[everythingState - 1].count,
"number of promises (\(promises.count)) != number of promise states (\(promiseStates[everythingState - 1].count))",
file: file, line: line)
file: (file), line: line)
_ = zip(promises, promiseStates[everythingState - 1]).map { p, pState in
XCTAssertEqual(p.futureResult.isFulfilled, pState, "promise states incorrect (\(everythingState) callbacks)", file: file, line: line)
XCTAssertEqual(p.futureResult.isFulfilled, pState, "promise states incorrect (\(everythingState) callbacks)", file: (file), line: line)
}

XCTAssertEqual(everythingState, singleState + multiState + fileState,
"odd, calls the single/vector/file writes: \(singleState)/\(multiState)/\(fileState) but overall \(everythingState+1)", file: file, line: line)
"odd, calls the single/vector/file writes: \(singleState)/\(multiState)/\(fileState) but overall \(everythingState+1)", file: (file), line: line)

if singleState == 0 {
XCTAssertNil(expectedSingleWritabilities, "no single writes have been done but we expected some")
} else {
XCTAssertEqual(singleState, (expectedSingleWritabilities?.count ?? Int.min), "different number of single writes than expected", file: file, line: line)
XCTAssertEqual(singleState, (expectedSingleWritabilities?.count ?? Int.min), "different number of single writes than expected", file: (file), line: line)
}
if multiState == 0 {
XCTAssertNil(expectedVectorWritabilities, "no vector writes have been done but we expected some")
} else {
XCTAssertEqual(multiState, (expectedVectorWritabilities?.count ?? Int.min), "different number of vector writes than expected", file: file, line: line)
XCTAssertEqual(multiState, (expectedVectorWritabilities?.count ?? Int.min), "different number of vector writes than expected", file: (file), line: line)
}
if fileState == 0 {
XCTAssertNil(expectedFileWritabilities, "no file writes have been done but we expected some")
} else {
XCTAssertEqual(fileState, (expectedFileWritabilities?.count ?? Int.min), "different number of file writes than expected", file: file, line: line)
XCTAssertEqual(fileState, (expectedFileWritabilities?.count ?? Int.min), "different number of file writes than expected", file: (file), line: line)
}
} else {
XCTAssertEqual(0, returns.count, "no callbacks called but apparently \(returns.count) expected", file: file, line: line)
XCTAssertNil(expectedSingleWritabilities, "no callbacks called but apparently some single writes expected", file: file, line: line)
XCTAssertNil(expectedVectorWritabilities, "no callbacks calles but apparently some vector writes expected", file: file, line: line)
XCTAssertNil(expectedFileWritabilities, "no callbacks calles but apparently some file writes expected", file: file, line: line)
XCTAssertEqual(0, returns.count, "no callbacks called but apparently \(returns.count) expected", file: (file), line: line)
XCTAssertNil(expectedSingleWritabilities, "no callbacks called but apparently some single writes expected", file: (file), line: line)
XCTAssertNil(expectedVectorWritabilities, "no callbacks calles but apparently some vector writes expected", file: (file), line: line)
XCTAssertNil(expectedFileWritabilities, "no callbacks calles but apparently some file writes expected", file: (file), line: line)

_ = zip(promises, promiseStates[0]).map { p, pState in
XCTAssertEqual(p.futureResult.isFulfilled, pState, "promise states incorrect (no callbacks)", file: file, line: line)
XCTAssertEqual(p.futureResult.isFulfilled, pState, "promise states incorrect (no callbacks)", file: (file), line: line)
}
}
return result
Expand Down Expand Up @@ -1959,8 +1959,8 @@ public final class ChannelTests: XCTestCase {
}

func testAppropriateAndInappropriateOperationsForUnregisteredSockets() throws {
func checkThatItThrowsInappropriateOperationForState(file: StaticString = (#file), line: UInt = #line, _ body: () throws -> Void) {
XCTAssertThrowsError(try body(), file: file, line: line) { error in
func checkThatItThrowsInappropriateOperationForState(file: StaticString = #file, line: UInt = #line, _ body: () throws -> Void) {
XCTAssertThrowsError(try body(), file: (file), line: line) { error in
XCTAssertEqual(.inappropriateOperationForState, error as? ChannelError)
}
}
Expand All @@ -1969,17 +1969,17 @@ public final class ChannelTests: XCTestCase {
XCTAssertNoThrow(try elg.syncShutdownGracefully())
}

func withChannel(skipDatagram: Bool = false, skipStream: Bool = false, skipServerSocket: Bool = false, file: StaticString = (#file), line: UInt = #line, _ body: (Channel) throws -> Void) {
func withChannel(skipDatagram: Bool = false, skipStream: Bool = false, skipServerSocket: Bool = false, file: StaticString = #file, line: UInt = #line, _ body: (Channel) throws -> Void) {
XCTAssertNoThrow(try {
let el = elg.next() as! SelectableEventLoop
let channels: [Channel] = (skipDatagram ? [] : [try DatagramChannel(eventLoop: el, protocolFamily: .inet)]) +
/* Xcode need help */ (skipStream ? []: [try SocketChannel(eventLoop: el, protocolFamily: .inet)]) +
/* Xcode need help */ (skipServerSocket ? []: [try ServerSocketChannel(eventLoop: el, group: elg, protocolFamily: .inet)])
for channel in channels {
try body(channel)
XCTAssertNoThrow(try channel.close().wait(), file: file, line: line)
XCTAssertNoThrow(try channel.close().wait(), file: (file), line: line)
}
}(), file: file, line: line)
}(), file: (file), line: line)
}
withChannel { channel in
checkThatItThrowsInappropriateOperationForState {
Expand Down
Loading

0 comments on commit 790bea0

Please sign in to comment.