|
16 | 16 | package org.msgpack.core.buffer
|
17 | 17 |
|
18 | 18 | import java.io._
|
| 19 | +import java.net.{InetSocketAddress} |
19 | 20 | import java.nio.ByteBuffer
|
| 21 | +import java.nio.channels.{ServerSocketChannel, SocketChannel} |
| 22 | +import java.util.concurrent.{Callable, Executors, TimeUnit} |
20 | 23 | import java.util.zip.{GZIPInputStream, GZIPOutputStream}
|
21 | 24 |
|
22 |
| -import org.msgpack.core.{MessagePack, MessagePackSpec, MessageUnpacker} |
| 25 | +import org.msgpack.core.{MessagePack, MessagePackSpec} |
23 | 26 | import xerial.core.io.IOUtil._
|
24 | 27 |
|
25 | 28 | import scala.util.Random
|
@@ -201,5 +204,44 @@ class MessageBufferInputTest
|
201 | 204 | buf.reset(in1)
|
202 | 205 | readInt(buf) shouldBe 42
|
203 | 206 | }
|
| 207 | + |
| 208 | + "unpack without blocking" in { |
| 209 | + val server = ServerSocketChannel.open.bind(new InetSocketAddress("localhost", 0)) |
| 210 | + val executorService = Executors.newCachedThreadPool |
| 211 | + |
| 212 | + try { |
| 213 | + executorService.execute(new Runnable { |
| 214 | + override def run { |
| 215 | + val server_ch = server.accept |
| 216 | + val packer = MessagePack.newDefaultPacker(server_ch) |
| 217 | + packer.packString("0123456789") |
| 218 | + packer.flush |
| 219 | + // Keep the connection open |
| 220 | + while (!executorService.isShutdown) { |
| 221 | + TimeUnit.SECONDS.sleep(1) |
| 222 | + } |
| 223 | + packer.close |
| 224 | + } |
| 225 | + }) |
| 226 | + |
| 227 | + val future = executorService.submit(new Callable[String] { |
| 228 | + override def call: String = { |
| 229 | + val conn_ch = SocketChannel.open(new InetSocketAddress("localhost", server.socket.getLocalPort)) |
| 230 | + val unpacker = MessagePack.newDefaultUnpacker(conn_ch) |
| 231 | + val s = unpacker.unpackString |
| 232 | + unpacker.close |
| 233 | + s |
| 234 | + } |
| 235 | + }) |
| 236 | + |
| 237 | + future.get(5, TimeUnit.SECONDS) shouldBe "0123456789" |
| 238 | + } |
| 239 | + finally { |
| 240 | + executorService.shutdown |
| 241 | + if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { |
| 242 | + executorService.shutdownNow |
| 243 | + } |
| 244 | + } |
| 245 | + } |
204 | 246 | }
|
205 | 247 | }
|
0 commit comments