FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix a bug ChannelBufferInput#next blocks until the buffer is filled by komamitsu · Pull Request #428 · msgpack/msgpack-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (1) .scala  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ public MessageBuffer next()
throws IOException
{
ByteBuffer b = buffer.sliceAsByteBuffer();
while (b.remaining() > 0) {
int ret = channel.read(b);
if (ret == -1) {
break;
}
int ret = channel.read(b);
if (ret == -1) {
return null;
}
b.flip();
return b.remaining() == 0 ? null : buffer.slice(0, b.limit());
return buffer.slice(0, b.limit());
}

@Override
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package org.msgpack.core.buffer

import java.io._
import java.net.{InetSocketAddress}
import java.nio.ByteBuffer
import java.nio.channels.{ServerSocketChannel, SocketChannel}
import java.util.concurrent.{Callable, Executors, TimeUnit}
import java.util.zip.{GZIPInputStream, GZIPOutputStream}

import org.msgpack.core.{MessagePack, MessagePackSpec, MessageUnpacker}
import org.msgpack.core.{MessagePack, MessagePackSpec}
import xerial.core.io.IOUtil._

import scala.util.Random
Expand Down Expand Up @@ -201,5 +204,44 @@ class MessageBufferInputTest
buf.reset(in1)
readInt(buf) shouldBe 42
}

"unpack without blocking" in {
val server = ServerSocketChannel.open.bind(new InetSocketAddress("localhost", 0))
val executorService = Executors.newCachedThreadPool

try {
executorService.execute(new Runnable {
override def run {
val server_ch = server.accept
val packer = MessagePack.newDefaultPacker(server_ch)
packer.packString("0123456789")
packer.flush
// Keep the connection open
while (!executorService.isShutdown) {
TimeUnit.SECONDS.sleep(1)
}
packer.close
}
})

val future = executorService.submit(new Callable[String] {
override def call: String = {
val conn_ch = SocketChannel.open(new InetSocketAddress("localhost", server.socket.getLocalPort))
val unpacker = MessagePack.newDefaultUnpacker(conn_ch)
val s = unpacker.unpackString
unpacker.close
s
}
})

future.get(5, TimeUnit.SECONDS) shouldBe "0123456789"
}
finally {
executorService.shutdown
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow
}
}
}
}
}

Back | FazBrowse Home | New Git URL