-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow two distinct ChannelOptions of one type (#597)
Motivation: Quite embarrasingly, we previously would only store one `ChannelOption` per `ChannelOption` type. Most channel option types are distinct and that's probably why it took so long to find this issue. Thanks @pushkarnk for reporting. Unfortunately though, the most important `ChannelOption` is `.socket` which crucially also holds a level and a name. That means if you set two `ChannelOptions.socket` options with distinct name/level, one would still override the other. Example: .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), value: 1) .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) would only actually set the latter. Modifications: - made all common `ChannelOption` types equatable (for 2.0 this will be a protocol requirement) - deprecated non-Equatable `ChannelOption` types - zero out buffer before calling getsockopt as Linux doesn't do that Result: you can now set two distinct `ChannelOptions` for one type Motivation: Explain here the context, and why you're making that change. What is the problem you're trying to solve. Modifications: Describe the modifications you've done. Result: After your change, what will change.
- Loading branch information
Showing
11 changed files
with
291 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// ChannelOptionStorageTest+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension ChannelOptionStorageTest { | ||
|
||
static var allTests : [(String, (ChannelOptionStorageTest) -> () throws -> Void)] { | ||
return [ | ||
("testWeStartWithNoOptions", testWeStartWithNoOptions), | ||
("testSetTwoOptionsOfDifferentType", testSetTwoOptionsOfDifferentType), | ||
("testSetTwoOptionsOfSameType", testSetTwoOptionsOfSameType), | ||
("testSetOneOptionTwice", testSetOneOptionTwice), | ||
] | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
@testable import NIO | ||
|
||
class ChannelOptionStorageTest: XCTestCase { | ||
func testWeStartWithNoOptions() throws { | ||
let cos = ChannelOptionStorage() | ||
let optionsCollector = OptionsCollectingChannel() | ||
XCTAssertNoThrow(try cos.applyAll(channel: optionsCollector).wait()) | ||
XCTAssertEqual(0, optionsCollector.allOptions.count) | ||
} | ||
|
||
func testSetTwoOptionsOfDifferentType() throws { | ||
var cos = ChannelOptionStorage() | ||
let optionsCollector = OptionsCollectingChannel() | ||
cos.put(key: ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) | ||
cos.put(key: ChannelOptions.backlog, value: 2) | ||
XCTAssertNoThrow(try cos.applyAll(channel: optionsCollector).wait()) | ||
XCTAssertEqual(2, optionsCollector.allOptions.count) | ||
} | ||
|
||
func testSetTwoOptionsOfSameType() throws { | ||
let options: [(SocketOption, SocketOptionValue)] = [(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), 1), | ||
(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEPORT), 2)] | ||
var cos = ChannelOptionStorage() | ||
let optionsCollector = OptionsCollectingChannel() | ||
for kv in options { | ||
cos.put(key: kv.0, value: kv.1) | ||
} | ||
XCTAssertNoThrow(try cos.applyAll(channel: optionsCollector).wait()) | ||
XCTAssertEqual(2, optionsCollector.allOptions.count) | ||
XCTAssertEqual(options.map { $0.0 }, | ||
(optionsCollector.allOptions as! [(SocketOption, SocketOptionValue)]).map { $0.0 }) | ||
XCTAssertEqual(options.map { $0.1 }, | ||
(optionsCollector.allOptions as! [(SocketOption, SocketOptionValue)]).map { $0.1 }) | ||
} | ||
|
||
func testSetOneOptionTwice() throws { | ||
var cos = ChannelOptionStorage() | ||
let optionsCollector = OptionsCollectingChannel() | ||
cos.put(key: ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) | ||
cos.put(key: ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 2) | ||
XCTAssertNoThrow(try cos.applyAll(channel: optionsCollector).wait()) | ||
XCTAssertEqual(1, optionsCollector.allOptions.count) | ||
XCTAssertEqual([ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR)], | ||
(optionsCollector.allOptions as! [(SocketOption, SocketOptionValue)]).map { $0.0 }) | ||
XCTAssertEqual([SocketOptionValue(2)], | ||
(optionsCollector.allOptions as! [(SocketOption, SocketOptionValue)]).map { $0.1 }) | ||
} | ||
} | ||
|
||
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<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T : ChannelOption { | ||
self.allOptions.append((option, value)) | ||
return self.eventLoop.newSucceededFuture(result: ()) | ||
} | ||
|
||
func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T : ChannelOption { | ||
fatalError() | ||
} | ||
|
||
var isWritable: Bool { fatalError() } | ||
|
||
var isActive: Bool { fatalError() } | ||
|
||
var _unsafe: ChannelCore { fatalError() } | ||
|
||
var eventLoop: EventLoop { | ||
return EmbeddedEventLoop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters