Files
swift-nio/Tests/NIOTests/ChannelOptionStorageTest.swift
T
Cory Benfield 30265d69a8 Adjust names of BSDSocket option values. (#1510)
Motivation:

The names of most of the BSD socket option values were brought over into
their NIO helper properties, with their namespace removed. This vastly
increases the risk of collision, particularly for things like TCP_INFO,
which just became .info.

Modifications:

- Added the prefixes back, e.g. `.info` is now `.tcp_info`.
- Renamed socket type `.dgram` to `.datagram`, as this is the nice clean
  API and we should use nice clean names.

Result:

Better APIs
2020-05-11 15:13:58 +01:00

108 lines
4.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 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
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
class ChannelOptionStorageTest: XCTestCase {
func testWeStartWithNoOptions() throws {
let cos = ChannelOptions.Storage()
let optionsCollector = OptionsCollectingChannel()
XCTAssertNoThrow(try cos.applyAllChannelOptions(to: optionsCollector).wait())
XCTAssertEqual(0, optionsCollector.allOptions.count)
}
func testSetTwoOptionsOfDifferentType() throws {
var cos = ChannelOptions.Storage()
let optionsCollector = OptionsCollectingChannel()
cos.append(key: ChannelOptions.socketOption(.so_reuseaddr), value: 1)
cos.append(key: ChannelOptions.backlog, value: 2)
XCTAssertNoThrow(try cos.applyAllChannelOptions(to: optionsCollector).wait())
XCTAssertEqual(2, optionsCollector.allOptions.count)
}
func testSetTwoOptionsOfSameType() throws {
let options: [(ChannelOptions.Types.SocketOption, SocketOptionValue)] = [(ChannelOptions.socketOption(.so_reuseaddr), 1),
(ChannelOptions.socketOption(.so_rcvtimeo), 2)]
var cos = ChannelOptions.Storage()
let optionsCollector = OptionsCollectingChannel()
for kv in options {
cos.append(key: kv.0, value: kv.1)
}
XCTAssertNoThrow(try cos.applyAllChannelOptions(to: optionsCollector).wait())
XCTAssertEqual(2, optionsCollector.allOptions.count)
XCTAssertEqual(options.map { $0.0 },
optionsCollector.allOptions.map { option in
return option.0 as! ChannelOptions.Types.SocketOption
})
XCTAssertEqual(options.map { $0.1 },
optionsCollector.allOptions.map { option in
return option.1 as! SocketOptionValue
})
}
func testSetOneOptionTwice() throws {
var cos = ChannelOptions.Storage()
let optionsCollector = OptionsCollectingChannel()
cos.append(key: ChannelOptions.socketOption(.so_reuseaddr), value: 1)
cos.append(key: ChannelOptions.socketOption(.so_reuseaddr), value: 2)
XCTAssertNoThrow(try cos.applyAllChannelOptions(to: optionsCollector).wait())
XCTAssertEqual(1, optionsCollector.allOptions.count)
XCTAssertEqual([ChannelOptions.socketOption(.so_reuseaddr)],
optionsCollector.allOptions.map { option in
return option.0 as! ChannelOptions.Types.SocketOption
})
XCTAssertEqual([SocketOptionValue(2)],
optionsCollector.allOptions.map { option in
return option.1 as! SocketOptionValue
})
}
}
class OptionsCollectingChannel: Channel {
var allOptions: [(Any, Any)] = []
var allocator: ByteBufferAllocator { fatalError() }
var closeFuture: EventLoopFuture<Void> { fatalError() }
var pipeline: ChannelPipeline { fatalError() }
var localAddress: SocketAddress? { fatalError() }
var remoteAddress: SocketAddress? { fatalError() }
var parent: Channel? { fatalError() }
func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
self.allOptions.append((option, value))
return self.eventLoop.makeSucceededFuture(())
}
func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
fatalError()
}
var isWritable: Bool { fatalError() }
var isActive: Bool { fatalError() }
var _channelCore: ChannelCore { fatalError() }
var eventLoop: EventLoop {
return EmbeddedEventLoop()
}
}