Skip to content

Commit

Permalink
Correctly fail EventLoopFuture for unsupported operations on ServerSo…
Browse files Browse the repository at this point in the history
…cketChannel (#226)

Motivation:

ServerSocketChannel should just fail the EventLoopFuture for unsupported operations and not crash.

Modifications:

- Correctly fail the EventLoopFuture for unsupported operations
- Add unit test.

Result:

No more crashes when users invoke unsupported operations.
  • Loading branch information
normanmaurer authored and Lukasa committed Mar 23, 2018
1 parent eb60e89 commit 1bcf85d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/NIO/SocketChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,18 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket> {
ch.close(promise: nil)
}
}

override func bufferPendingWrite(data: NIOAny, promise: EventLoopPromise<Void>?) {
promise?.fail(error: ChannelError.operationUnsupported)
}

override func markFlushPoint(promise: EventLoopPromise<Void>?) {
promise?.fail(error: ChannelError.operationUnsupported)
}

override func flushNow() -> IONotificationState {
return IONotificationState.unregister
}
}

/// A channel used with datagram sockets.
Expand Down
3 changes: 3 additions & 0 deletions Tests/NIOTests/SocketChannelTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extension SocketChannelTest {
("testAcceptFailsWithEFAULT", testAcceptFailsWithEFAULT),
("testSetGetOptionClosedServerSocketChannel", testSetGetOptionClosedServerSocketChannel),
("testConnect", testConnect),
("testWriteServerSocketChannel", testWriteServerSocketChannel),
("testWriteAndFlushServerSocketChannel", testWriteAndFlushServerSocketChannel),
("testConnectServerSocketChannel", testConnectServerSocketChannel),
]
}
}
Expand Down
42 changes: 42 additions & 0 deletions Tests/NIOTests/SocketChannelTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,46 @@ public class SocketChannelTest : XCTestCase {
try channel.closeFuture.wait()
try promise.futureResult.wait()
}

public func testWriteServerSocketChannel() throws {
let group = MultiThreadedEventLoopGroup(numThreads: 1)
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }

let serverChannel = try ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait()
do {
try serverChannel.write("test").wait()
} catch let err as ChannelError where err == .operationUnsupported {
// expected
}
try serverChannel.close().wait()
}


public func testWriteAndFlushServerSocketChannel() throws {
let group = MultiThreadedEventLoopGroup(numThreads: 1)
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }

let serverChannel = try ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait()
do {
try serverChannel.writeAndFlush("test").wait()
} catch let err as ChannelError where err == .operationUnsupported {
// expected
}
try serverChannel.close().wait()
}


public func testConnectServerSocketChannel() throws {
let group = MultiThreadedEventLoopGroup(numThreads: 1)
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }

let serverChannel = try ServerBootstrap(group: group).bind(host: "127.0.0.1", port: 0).wait()
do {
try serverChannel.connect(to: serverChannel.localAddress!).wait()
} catch let err as ChannelError where err == .operationUnsupported {
// expected
}
try serverChannel.close().wait()
}

}

0 comments on commit 1bcf85d

Please sign in to comment.