Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make EventLoopPromise conform to Equatable #2714

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/NIOCore/EventLoopFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ public struct EventLoopPromise<Value> {
}
}

extension EventLoopPromise: Equatable {}

/// Holder for a result that will be provided later.
///
/// Functions that promise to do work asynchronously can return an `EventLoopFuture<Value>`.
Expand Down
43 changes: 43 additions & 0 deletions Tests/NIOPosixTests/EventLoopFutureTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1601,4 +1601,47 @@ class EventLoopFutureTest: XCTestCase {
XCTAssertNotNil(promise)
promise?.succeed()
}

func testPromiseEquatable() {
let eventLoop = EmbeddedEventLoop()

let promise1 = eventLoop.makePromise(of: Void.self)
let promise2 = eventLoop.makePromise(of: Void.self)
let promise3 = promise1
XCTAssertEqual(promise1, promise3)
XCTAssertNotEqual(promise1, promise2)
XCTAssertNotEqual(promise3, promise2)

promise1.succeed()
promise2.succeed()
}

func testPromiseEquatable_WhenSucceeded() {
let eventLoop = EmbeddedEventLoop()

let promise1 = eventLoop.makePromise(of: Void.self)
let promise2 = eventLoop.makePromise(of: Void.self)
let promise3 = promise1

promise1.succeed()
promise2.succeed()
XCTAssertEqual(promise1, promise3)
XCTAssertNotEqual(promise1, promise2)
XCTAssertNotEqual(promise3, promise2)
}

func testPromiseEquatable_WhenFailed() {
struct E: Error {}
let eventLoop = EmbeddedEventLoop()

let promise1 = eventLoop.makePromise(of: Void.self)
let promise2 = eventLoop.makePromise(of: Void.self)
let promise3 = promise1

promise1.fail(E())
promise2.fail(E())
XCTAssertEqual(promise1, promise3)
XCTAssertNotEqual(promise1, promise2)
XCTAssertNotEqual(promise3, promise2)
}
}
Loading