Skip to content
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
Prev Previous commit
Next Next commit
Migrate to AirSpec
  • Loading branch information
xerial committed May 12, 2021
commit 2be647bdd7bf1d5110fe6fe1bf137a7d90391ba7
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.msgpack.core.MessagePackSpec.createMessagePackData
*/
class InvalidDataReadTest extends MessagePackSpec {

"Reading long EXT32" in {
test("Reading long EXT32") {
// Prepare an EXT32 data with 2GB (Int.MaxValue size) payload for testing the behavior of MessageUnpacker.skipValue()
// Actually preparing 2GB of data, however, is too much for CI, so we create only the header part.
val msgpack = createMessagePackData(p => p.packExtensionTypeHeader(MessagePack.Code.EXT32, Int.MaxValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import java.util.Arrays
import org.msgpack.value.ValueFactory._

class MessageBufferPackerTest extends MessagePackSpec {
"MessageBufferPacker" should {
"be equivalent to ByteArrayOutputStream" in {
test("MessageBufferPacker") {
test("be equivalent to ByteArrayOutputStream") {
val packer1 = MessagePack.newDefaultBufferPacker
packer1.packValue(newMap(newString("a"), newInteger(1), newString("b"), newString("s")))

Expand All @@ -34,7 +34,7 @@ class MessageBufferPackerTest extends MessagePackSpec {
packer1.toByteArray shouldBe stream.toByteArray
}

"clear unflushed" in {
test("clear unflushed") {
val packer = MessagePack.newDefaultBufferPacker
packer.packInt(1);
packer.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import scala.util.Random
* Created on 2014/05/07.
*/
class MessageFormatTest extends MessagePackSpec {
"MessageFormat" should {
"cover all byte codes" in {
test("MessageFormat") {
test("cover all byte codes") {
def checkV(b: Byte, tpe: ValueType) {
try MessageFormat.valueOf(b).getValueType shouldBe tpe
catch {
Expand Down Expand Up @@ -102,7 +102,7 @@ class MessageFormatTest extends MessagePackSpec {
}
}

"improve the valueOf performance" in {
test("improve the valueOf performance") {
val N = 1000000
val idx = (0 until N).map(x => Random.nextInt(256).toByte).toArray[Byte]

Expand Down
23 changes: 8 additions & 15 deletions msgpack-core/src/test/scala/org/msgpack/core/MessagePackSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
package org.msgpack.core

import java.io.ByteArrayOutputStream
import org.scalatest._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import xerial.core.log.{LogLevel, Logger}
import xerial.core.util.{TimeReport, Timer}
import wvlet.airspec.AirSpec
import wvlet.log.LogLevel
import wvlet.log.io.{TimeReport, Timer}

import scala.language.implicitConversions

Expand All @@ -35,27 +33,22 @@ object MessagePackSpec {
}
}

trait MessagePackSpec extends AnyWordSpec with Matchers with GivenWhenThen with OptionValues with BeforeAndAfter with Benchmark with Logger {

implicit def toTag(s: String): Tag = Tag(s)

}
trait MessagePackSpec extends AirSpec with Benchmark {}

trait Benchmark extends Timer {
private val numWarmUpRuns = 10

val numWarmUpRuns = 10

override protected def time[A](blockName: String, logLevel: LogLevel, repeat: Int)(f: => A): TimeReport = {
override protected def time[A](blockName: String, logLevel: LogLevel = LogLevel.INFO, repeat: Int = 1, blockRepeat: Int = 1)(f: => A): TimeReport = {
super.time(blockName, logLevel = LogLevel.INFO, repeat)(f)
}

override protected def block[A](name: String, repeat: Int)(f: => A): TimeReport = {
override protected def block[A](name: String)(f: => A): TimeReport = {
var i = 0
while (i < numWarmUpRuns) {
f
i += 1
}

super.block(name, repeat)(f)
super.block(name)(f)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.msgpack.core.MessagePack.{PackerConfig, UnpackerConfig}
import org.msgpack.core.MessagePackSpec.toHex
import org.msgpack.value.{Value, Variable}
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Prop.{forAll, propBoolean}
import org.scalacheck.Prop.propBoolean
import wvlet.airspec.AirSpec
import wvlet.airspec.spi.PropertyCheck
import wvlet.log.io.Timer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import scala.util.Random
*/
class MessagePackerTest extends MessagePackSpec {

def verifyIntSeq(answer: Array[Int], packed: Array[Byte]) {
private def verifyIntSeq(answer: Array[Int], packed: Array[Byte]) {
val unpacker = MessagePack.newDefaultUnpacker(packed)
val b = Array.newBuilder[Int]
while (unpacker.hasNext) {
Expand All @@ -40,27 +40,27 @@ class MessagePackerTest extends MessagePackSpec {
result shouldBe answer
}

def createTempFile = {
private def createTempFile = {
val f = File.createTempFile("msgpackTest", "msgpack")
f.deleteOnExit
f
}

def createTempFileWithOutputStream = {
private def createTempFileWithOutputStream = {
val f = createTempFile
val out = new FileOutputStream(f)
(f, out)
}

def createTempFileWithChannel = {
private def createTempFileWithChannel = {
val (f, out) = createTempFileWithOutputStream
val ch = out.getChannel
(f, ch)
}

"MessagePacker" should {
test("MessagePacker") {

"reset the internal states" in {
test("reset the internal states") {
val intSeq = (0 until 100).map(i => Random.nextInt).toArray

val b = new ByteArrayOutputStream
Expand All @@ -86,8 +86,7 @@ class MessagePackerTest extends MessagePackSpec {
verifyIntSeq(intSeq3, b3.toByteArray)
}

"improve the performance via reset method" taggedAs ("reset") in {

test("improve the performance via reset method") {
val N = 1000
val t = time("packer", repeat = 10) {
block("no-buffer-reset") {
Expand Down Expand Up @@ -119,10 +118,10 @@ class MessagePackerTest extends MessagePackSpec {
}
}

t("buffer-reset").averageWithoutMinMax should be <= t("no-buffer-reset").averageWithoutMinMax
t("buffer-reset").averageWithoutMinMax <= t("no-buffer-reset").averageWithoutMinMax shouldBe true
}

"pack larger string array than byte buf" taggedAs ("larger-string-array-than-byte-buf") in {
test("pack larger string array than byte buf") {
// Based on https://github.com/msgpack/msgpack-java/issues/154

def test(bufferSize: Int, stringSize: Int): Boolean = {
Expand All @@ -148,7 +147,7 @@ class MessagePackerTest extends MessagePackSpec {
}
}

"reset OutputStreamBufferOutput" in {
test("reset OutputStreamBufferOutput") {
val (f0, out0) = createTempFileWithOutputStream
val packer = MessagePack.newDefaultPacker(out0)
packer.packInt(99)
Expand Down Expand Up @@ -178,7 +177,7 @@ class MessagePackerTest extends MessagePackSpec {
up1.close
}

"reset ChannelBufferOutput" in {
test("reset ChannelBufferOutput") {
val (f0, out0) = createTempFileWithChannel
val packer = MessagePack.newDefaultPacker(out0)
packer.packInt(99)
Expand Down Expand Up @@ -208,7 +207,7 @@ class MessagePackerTest extends MessagePackSpec {
up1.close
}

"pack a lot of String within expected time" in {
test("pack a lot of String within expected time") {
val count = 20000

def measureDuration(outputStream: java.io.OutputStream) = {
Expand All @@ -231,11 +230,11 @@ class MessagePackerTest extends MessagePackSpec {
measureDuration(fileOutput)
}
}
t("file-output-stream").averageWithoutMinMax shouldBe <(t("byte-array-output-stream").averageWithoutMinMax * 5)
t("file-output-stream").averageWithoutMinMax < (t("byte-array-output-stream").averageWithoutMinMax * 5) shouldBe true
}
}

"compute totalWrittenBytes" in {
test("compute totalWrittenBytes") {
val out = new ByteArrayOutputStream
val packerTotalWrittenBytes =
IOUtil.withResource(MessagePack.newDefaultPacker(out)) { packer =>
Expand All @@ -254,7 +253,7 @@ class MessagePackerTest extends MessagePackSpec {
out.toByteArray.length shouldBe packerTotalWrittenBytes
}

"support read-only buffer" taggedAs ("read-only") in {
test("support read-only buffer") {
val payload = Array[Byte](1)
val out = new ByteArrayOutputStream()
val packer = MessagePack
Expand All @@ -264,7 +263,7 @@ class MessagePackerTest extends MessagePackSpec {
.close()
}

"pack small string with STR8" in {
test("pack small string with STR8") {
val packer = new PackerConfig().newBufferPacker()
packer.packString("Hello. This is a string longer than 32 characters!")
val b = packer.toByteArray
Expand All @@ -274,7 +273,7 @@ class MessagePackerTest extends MessagePackSpec {
f shouldBe MessageFormat.STR8
}

"be able to disable STR8 for backward compatibility" in {
test("be able to disable STR8 for backward compatibility") {
val config = new PackerConfig()
.withStr8FormatSupport(false)

Expand All @@ -285,7 +284,7 @@ class MessagePackerTest extends MessagePackSpec {
f shouldBe MessageFormat.STR16
}

"be able to disable STR8 when using CharsetEncoder" in {
test("be able to disable STR8 when using CharsetEncoder") {
val config = new PackerConfig()
.withStr8FormatSupport(false)
.withSmallStringOptimizationThreshold(0) // Disable small string optimization
Expand All @@ -294,19 +293,19 @@ class MessagePackerTest extends MessagePackSpec {
packer.packString("small string")
val unpacker = MessagePack.newDefaultUnpacker(packer.toByteArray)
val f = unpacker.getNextFormat
f shouldNot be(MessageFormat.STR8)
f shouldNotBe MessageFormat.STR8
val s = unpacker.unpackString()
s shouldBe "small string"
}

"write raw binary" taggedAs ("raw-binary") in {
test("write raw binary") {
val packer = new MessagePack.PackerConfig().newBufferPacker()
val msg =
Array[Byte](-127, -92, 116, 121, 112, 101, -92, 112, 105, 110, 103)
packer.writePayload(msg)
}

"append raw binary" taggedAs ("append-raw-binary") in {
test("append raw binary") {
val packer = new MessagePack.PackerConfig().newBufferPacker()
val msg =
Array[Byte](-127, -92, 116, 121, 112, 101, -92, 112, 105, 110, 103)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
package org.msgpack.core.buffer

import akka.util.ByteString
import org.msgpack.core.MessagePack
import org.msgpack.core.MessagePackSpec.createMessagePackData
import org.msgpack.core.{MessagePack, MessagePackSpec, MessageUnpacker}
import wvlet.airspec.AirSpec

class ByteStringTest extends MessagePackSpec {
class ByteStringTest extends AirSpec {

val unpackedString = "foo"
val byteString = ByteString(createMessagePackData(_.packString(unpackedString)))
private val unpackedString = "foo"
private val byteString = ByteString(createMessagePackData(_.packString(unpackedString)))

def unpackString(messageBuffer: MessageBuffer) = {
private def unpackString(messageBuffer: MessageBuffer) = {
val input = new MessageBufferInput {

private var isRead = false
Expand All @@ -42,12 +43,14 @@ class ByteStringTest extends MessagePackSpec {
MessagePack.newDefaultUnpacker(input).unpackString()
}

"Unpacking a ByteString's ByteBuffer" should {
"fail with a regular MessageBuffer" in {
test("Unpacking a ByteString's ByteBuffer") {
test("fail with a regular MessageBuffer") {

// can't demonstrate with new ByteBufferInput(byteString.asByteBuffer)
// as Travis tests run with JDK6 that picks up MessageBufferU
a[RuntimeException] shouldBe thrownBy(unpackString(new MessageBuffer(byteString.asByteBuffer)))
intercept[RuntimeException] {
unpackString(new MessageBuffer(byteString.asByteBuffer))
}
}
}
}
Loading