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

Enable command pipelining #552

Merged
merged 11 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion Sources/NIOIMAP/Coders/IMAPClientHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ public final class IMAPClientHandler: ChannelDuplexHandler {
}

public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
precondition(self.bufferedWrites.isEmpty, "Sorry, we only allow one command at a time right now. We're working on it. Issue #528")
let command = self.unwrapOutboundIn(data)
var encoder = CommandEncodeBuffer(buffer: context.channel.allocator.buffer(capacity: 1024), options: self.encodingOptions)
encoder.writeCommandStream(command)

guard self.bufferedWrites.isEmpty else {
self.bufferedWrites.append((encoder._buffer, promise))
Davidde94 marked this conversation as resolved.
Show resolved Hide resolved
return
}

switch command {
case .command(let command):
Expand Down
53 changes: 26 additions & 27 deletions Tests/NIOIMAPTests/Coders/IMAPClientHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,32 @@ class IMAPClientHandlerTests: XCTestCase {
state: .ok(.init(code: nil, text: "ok")))))
}

// TODO: Make a new state machine that can handle pipelined commands and uncomment this test, issue #528
// func testTwoContReqCommandsEnqueued() {
// let f1 = self.writeOutbound(CommandStream.command(TaggedCommand(tag: "x",
// command: .rename(from: .init("\\"),
// to: .init("to"),
// params: [:]))),
// wait: false)
// let f2 = self.writeOutbound(CommandStream.command(TaggedCommand(tag: "y",
// command: .rename(from: .init("from"),
// to: .init("\\"),
// params: [:]))),
// wait: false)
// self.assertOutboundString("x RENAME {1}\r\n")
// self.writeInbound("+ OK\r\n")
// XCTAssertNoThrow(try f1.wait())
// self.assertOutboundString("\\ \"to\"\r\n")
// self.assertOutboundString("y RENAME \"from\" {1}\r\n")
// self.writeInbound("+ OK\r\n")
// XCTAssertNoThrow(try f2.wait())
// self.assertOutboundString("\\\r\n")
// self.writeInbound("x OK ok\r\n")
// self.assertInbound(.taggedResponse(.init(tag: "x",
// state: .ok(.init(code: nil, text: "ok")))))
// self.writeInbound("y OK ok\r\n")
// self.assertInbound(.taggedResponse(.init(tag: "y",
// state: .ok(.init(code: nil, text: "ok")))))
// }
func testTwoContReqCommandsEnqueued() {
let f1 = self.writeOutbound(CommandStream.command(TaggedCommand(tag: "x",
command: .rename(from: .init("\\"),
to: .init("to"),
params: [:]))),
wait: false)
let f2 = self.writeOutbound(CommandStream.command(TaggedCommand(tag: "y",
command: .rename(from: .init("from"),
to: .init("\\"),
params: [:]))),
wait: false)
self.assertOutboundString("x RENAME {1}\r\n")
self.writeInbound("+ OK\r\n")
XCTAssertNoThrow(try f1.wait())
self.assertOutboundString("\\ \"to\"\r\n")
self.assertOutboundString("y RENAME \"from\" {1}\r\n")
self.writeInbound("+ OK\r\n")
XCTAssertNoThrow(try f2.wait())
self.assertOutboundString("\\\r\n")
self.writeInbound("x OK ok\r\n")
self.assertInbound(.taggedResponse(.init(tag: "x",
state: .ok(.init(code: nil, text: "ok")))))
self.writeInbound("y OK ok\r\n")
self.assertInbound(.taggedResponse(.init(tag: "y",
state: .ok(.init(code: nil, text: "ok")))))
}

func testUnexpectedContinuationRequest() {
let f = self.writeOutbound(CommandStream.command(TaggedCommand(tag: "x",
Expand Down