Conform `AddressedEnvelope` conditionally to `Hashable` & `Equatable` (#2017)
### Motivation: Sometimes, it is nice to check `AddressedEnvelope`s for equality or hash them. Especially, in tests this comes in handy. ### Modifications: This PR, adds conditional conformances to `AddressedEnvelope` for `Equatable` and `Hashable` depending on its generic `DataType` . I also added a new module `NIOCoreTests` which was an open todoleft from the creation of the `NIOCore` module. To not add more tests that we have to migrate in the future, I started to create the new tests in the new module right away. I also created issue https://github.com/apple/swift-nio/issues/2016, to keep track of our open task to move the other tests over. ### Result: `AddressedEnvelope` is conditionally `Equatable` and `Hashable`
This commit is contained in:
parent
e123c216cc
commit
acbd697113
|
@ -77,6 +77,8 @@ var targets: [PackageDescription.Target] = [
|
||||||
dependencies: ["NIOPosix", "NIOCore", "NIOEmbedded", "NIOHTTP1", "NIOWebSocket", "NIOFoundationCompat"]),
|
dependencies: ["NIOPosix", "NIOCore", "NIOEmbedded", "NIOHTTP1", "NIOWebSocket", "NIOFoundationCompat"]),
|
||||||
.target(name: "NIOAsyncAwaitDemo",
|
.target(name: "NIOAsyncAwaitDemo",
|
||||||
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
|
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
|
||||||
|
.testTarget(name: "NIOCoreTests",
|
||||||
|
dependencies: ["NIOCore"]),
|
||||||
.testTarget(name: "NIOEmbeddedTests",
|
.testTarget(name: "NIOEmbeddedTests",
|
||||||
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
|
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
|
||||||
.testTarget(name: "NIOPosixTests",
|
.testTarget(name: "NIOPosixTests",
|
||||||
|
|
|
@ -58,6 +58,10 @@ extension AddressedEnvelope: CustomStringConvertible {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AddressedEnvelope: Equatable where DataType: Equatable {}
|
||||||
|
|
||||||
|
extension AddressedEnvelope: Hashable where DataType: Hashable {}
|
||||||
|
|
||||||
/// Possible Explicit Congestion Notification States
|
/// Possible Explicit Congestion Notification States
|
||||||
public enum NIOExplicitCongestionNotificationState: Hashable {
|
public enum NIOExplicitCongestionNotificationState: Hashable {
|
||||||
/// Non-ECN Capable Transport.
|
/// Non-ECN Capable Transport.
|
||||||
|
|
|
@ -24,6 +24,7 @@ import XCTest
|
||||||
|
|
||||||
#if os(Linux) || os(FreeBSD) || os(Android)
|
#if os(Linux) || os(FreeBSD) || os(Android)
|
||||||
@testable import NIOConcurrencyHelpersTests
|
@testable import NIOConcurrencyHelpersTests
|
||||||
|
@testable import NIOCoreTests
|
||||||
@testable import NIODataStructuresTests
|
@testable import NIODataStructuresTests
|
||||||
@testable import NIOEmbeddedTests
|
@testable import NIOEmbeddedTests
|
||||||
@testable import NIOFoundationCompatTests
|
@testable import NIOFoundationCompatTests
|
||||||
|
@ -45,6 +46,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
|
||||||
XCTMain([
|
XCTMain([
|
||||||
testCase(AcceptBackoffHandlerTest.allTests),
|
testCase(AcceptBackoffHandlerTest.allTests),
|
||||||
testCase(AdaptiveRecvByteBufferAllocatorTest.allTests),
|
testCase(AdaptiveRecvByteBufferAllocatorTest.allTests),
|
||||||
|
testCase(AddressedEnvelopeTests.allTests),
|
||||||
testCase(ApplicationProtocolNegotiationHandlerTests.allTests),
|
testCase(ApplicationProtocolNegotiationHandlerTests.allTests),
|
||||||
testCase(Base64Test.allTests),
|
testCase(Base64Test.allTests),
|
||||||
testCase(BaseObjectTest.allTests),
|
testCase(BaseObjectTest.allTests),
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the SwiftNIO open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// AddressedEnvelopeTests+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 AddressedEnvelopeTests {
|
||||||
|
|
||||||
|
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
|
||||||
|
static var allTests : [(String, (AddressedEnvelopeTests) -> () throws -> Void)] {
|
||||||
|
return [
|
||||||
|
("testHashable_whenEqual", testHashable_whenEqual),
|
||||||
|
("testHashable_whenDifferentData", testHashable_whenDifferentData),
|
||||||
|
("testHashable_whenDifferentAddress", testHashable_whenDifferentAddress),
|
||||||
|
("testHashable_whenDifferentMetadata", testHashable_whenDifferentMetadata),
|
||||||
|
("testHashable_whenDifferentData_andDifferentAddress", testHashable_whenDifferentData_andDifferentAddress),
|
||||||
|
("testHashable_whenDifferentData_andDifferentMetadata", testHashable_whenDifferentData_andDifferentMetadata),
|
||||||
|
("testHashable_whenDifferentAddress_andDifferentMetadata", testHashable_whenDifferentAddress_andDifferentMetadata),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,77 @@
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the SwiftNIO open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 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
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
import NIOCore
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
final class AddressedEnvelopeTests: XCTestCase {
|
||||||
|
func testHashable_whenEqual() throws {
|
||||||
|
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo")
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo")
|
||||||
|
|
||||||
|
XCTAssertEqual(envelope1, envelope2)
|
||||||
|
XCTAssertEqual(envelope1.hashValue, envelope2.hashValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentData() throws {
|
||||||
|
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo")
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar")
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentAddress() throws {
|
||||||
|
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo")
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "foo")
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentMetadata() throws {
|
||||||
|
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced))
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .transportCapableFlag0))
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentData_andDifferentAddress() throws {
|
||||||
|
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo")
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar")
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentData_andDifferentMetadata() throws {
|
||||||
|
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced))
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar", metadata: .init(ecnState: .transportCapableFlag0))
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testHashable_whenDifferentAddress_andDifferentMetadata() throws {
|
||||||
|
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
|
||||||
|
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
|
||||||
|
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo", metadata: .init(ecnState: .congestionExperienced))
|
||||||
|
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar", metadata: .init(ecnState: .transportCapableFlag0))
|
||||||
|
|
||||||
|
XCTAssertNotEqual(envelope1, envelope2)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue