Skip to content

Commit acbd697

Browse files
authored
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 #2016, to keep track of our open task to move the other tests over. ### Result: `AddressedEnvelope` is conditionally `Equatable` and `Hashable`
1 parent e123c21 commit acbd697

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ var targets: [PackageDescription.Target] = [
7777
dependencies: ["NIOPosix", "NIOCore", "NIOEmbedded", "NIOHTTP1", "NIOWebSocket", "NIOFoundationCompat"]),
7878
.target(name: "NIOAsyncAwaitDemo",
7979
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
80+
.testTarget(name: "NIOCoreTests",
81+
dependencies: ["NIOCore"]),
8082
.testTarget(name: "NIOEmbeddedTests",
8183
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
8284
.testTarget(name: "NIOPosixTests",

Sources/NIOCore/AddressedEnvelope.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ extension AddressedEnvelope: CustomStringConvertible {
5858
}
5959
}
6060

61+
extension AddressedEnvelope: Equatable where DataType: Equatable {}
62+
63+
extension AddressedEnvelope: Hashable where DataType: Hashable {}
64+
6165
/// Possible Explicit Congestion Notification States
6266
public enum NIOExplicitCongestionNotificationState: Hashable {
6367
/// Non-ECN Capable Transport.

Tests/LinuxMain.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import XCTest
2424

2525
#if os(Linux) || os(FreeBSD) || os(Android)
2626
@testable import NIOConcurrencyHelpersTests
27+
@testable import NIOCoreTests
2728
@testable import NIODataStructuresTests
2829
@testable import NIOEmbeddedTests
2930
@testable import NIOFoundationCompatTests
@@ -45,6 +46,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
4546
XCTMain([
4647
testCase(AcceptBackoffHandlerTest.allTests),
4748
testCase(AdaptiveRecvByteBufferAllocatorTest.allTests),
49+
testCase(AddressedEnvelopeTests.allTests),
4850
testCase(ApplicationProtocolNegotiationHandlerTests.allTests),
4951
testCase(Base64Test.allTests),
5052
testCase(BaseObjectTest.allTests),
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
//
15+
// AddressedEnvelopeTests+XCTest.swift
16+
//
17+
import XCTest
18+
19+
///
20+
/// NOTE: This file was generated by generate_linux_tests.rb
21+
///
22+
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
23+
///
24+
25+
extension AddressedEnvelopeTests {
26+
27+
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
28+
static var allTests : [(String, (AddressedEnvelopeTests) -> () throws -> Void)] {
29+
return [
30+
("testHashable_whenEqual", testHashable_whenEqual),
31+
("testHashable_whenDifferentData", testHashable_whenDifferentData),
32+
("testHashable_whenDifferentAddress", testHashable_whenDifferentAddress),
33+
("testHashable_whenDifferentMetadata", testHashable_whenDifferentMetadata),
34+
("testHashable_whenDifferentData_andDifferentAddress", testHashable_whenDifferentData_andDifferentAddress),
35+
("testHashable_whenDifferentData_andDifferentMetadata", testHashable_whenDifferentData_andDifferentMetadata),
36+
("testHashable_whenDifferentAddress_andDifferentMetadata", testHashable_whenDifferentAddress_andDifferentMetadata),
37+
]
38+
}
39+
}
40+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
import NIOCore
15+
import XCTest
16+
17+
final class AddressedEnvelopeTests: XCTestCase {
18+
func testHashable_whenEqual() throws {
19+
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
20+
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo")
21+
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo")
22+
23+
XCTAssertEqual(envelope1, envelope2)
24+
XCTAssertEqual(envelope1.hashValue, envelope2.hashValue)
25+
}
26+
27+
func testHashable_whenDifferentData() throws {
28+
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
29+
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo")
30+
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar")
31+
32+
XCTAssertNotEqual(envelope1, envelope2)
33+
}
34+
35+
func testHashable_whenDifferentAddress() throws {
36+
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
37+
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
38+
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo")
39+
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "foo")
40+
41+
XCTAssertNotEqual(envelope1, envelope2)
42+
}
43+
44+
func testHashable_whenDifferentMetadata() throws {
45+
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
46+
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced))
47+
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .transportCapableFlag0))
48+
49+
XCTAssertNotEqual(envelope1, envelope2)
50+
}
51+
52+
func testHashable_whenDifferentData_andDifferentAddress() throws {
53+
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
54+
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
55+
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo")
56+
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar")
57+
58+
XCTAssertNotEqual(envelope1, envelope2)
59+
}
60+
61+
func testHashable_whenDifferentData_andDifferentMetadata() throws {
62+
let address = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
63+
let envelope1 = AddressedEnvelope(remoteAddress: address, data: "foo", metadata: .init(ecnState: .congestionExperienced))
64+
let envelope2 = AddressedEnvelope(remoteAddress: address, data: "bar", metadata: .init(ecnState: .transportCapableFlag0))
65+
66+
XCTAssertNotEqual(envelope1, envelope2)
67+
}
68+
69+
func testHashable_whenDifferentAddress_andDifferentMetadata() throws {
70+
let address1 = try SocketAddress(ipAddress: "127.0.0.0", port: 443)
71+
let address2 = try SocketAddress(ipAddress: "127.0.0.0", port: 444)
72+
let envelope1 = AddressedEnvelope(remoteAddress: address1, data: "foo", metadata: .init(ecnState: .congestionExperienced))
73+
let envelope2 = AddressedEnvelope(remoteAddress: address2, data: "bar", metadata: .init(ecnState: .transportCapableFlag0))
74+
75+
XCTAssertNotEqual(envelope1, envelope2)
76+
}
77+
}

0 commit comments

Comments
 (0)