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:
Franz Busch 2021-12-20 19:22:13 +01:00 committed by GitHub
parent e123c216cc
commit acbd697113
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 0 deletions

View File

@ -77,6 +77,8 @@ var targets: [PackageDescription.Target] = [
dependencies: ["NIOPosix", "NIOCore", "NIOEmbedded", "NIOHTTP1", "NIOWebSocket", "NIOFoundationCompat"]),
.target(name: "NIOAsyncAwaitDemo",
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
.testTarget(name: "NIOCoreTests",
dependencies: ["NIOCore"]),
.testTarget(name: "NIOEmbeddedTests",
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
.testTarget(name: "NIOPosixTests",

View File

@ -58,6 +58,10 @@ extension AddressedEnvelope: CustomStringConvertible {
}
}
extension AddressedEnvelope: Equatable where DataType: Equatable {}
extension AddressedEnvelope: Hashable where DataType: Hashable {}
/// Possible Explicit Congestion Notification States
public enum NIOExplicitCongestionNotificationState: Hashable {
/// Non-ECN Capable Transport.

View File

@ -24,6 +24,7 @@ import XCTest
#if os(Linux) || os(FreeBSD) || os(Android)
@testable import NIOConcurrencyHelpersTests
@testable import NIOCoreTests
@testable import NIODataStructuresTests
@testable import NIOEmbeddedTests
@testable import NIOFoundationCompatTests
@ -45,6 +46,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
XCTMain([
testCase(AcceptBackoffHandlerTest.allTests),
testCase(AdaptiveRecvByteBufferAllocatorTest.allTests),
testCase(AddressedEnvelopeTests.allTests),
testCase(ApplicationProtocolNegotiationHandlerTests.allTests),
testCase(Base64Test.allTests),
testCase(BaseObjectTest.allTests),

View File

@ -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),
]
}
}

View File

@ -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)
}
}