From bd5f4f27ded34407affb02214bd4c320c7bfe18d Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Thu, 17 Oct 2024 15:36:09 -0400 Subject: [PATCH] Use Burst more in our test suite --- .../kotlin/okio/BufferedSinkFactory.kt | 26 ++++----- .../kotlin/okio/BufferedSourceFactory.kt | 12 ++-- .../kotlin/okio/ByteStringFactory.kt | 58 +++++++++---------- .../commonTest/kotlin/okio/ByteStringTest.kt | 11 ++-- ...dSinkTest.kt => CommonBufferedSinkTest.kt} | 7 +-- ...rceTest.kt => CommonBufferedSourceTest.kt} | 15 ++--- .../jvmTest/kotlin/okio/BufferCursorTest.kt | 2 +- okio/src/jvmTest/kotlin/okio/BufferFactory.kt | 10 ++-- .../jvmTest/kotlin/okio/BufferedSinkTest.kt | 6 +- .../jvmTest/kotlin/okio/BufferedSourceTest.kt | 28 +++------ .../jvmTest/kotlin/okio/ByteStringJavaTest.kt | 6 +- .../src/jvmTest/kotlin/okio/TimeoutFactory.kt | 8 +-- .../jvmTest/kotlin/okio/internal/HmacTest.kt | 6 +- 13 files changed, 84 insertions(+), 111 deletions(-) rename okio/src/commonTest/kotlin/okio/{AbstractBufferedSinkTest.kt => CommonBufferedSinkTest.kt} (97%) rename okio/src/commonTest/kotlin/okio/{AbstractBufferedSourceTest.kt => CommonBufferedSourceTest.kt} (97%) diff --git a/okio/src/commonTest/kotlin/okio/BufferedSinkFactory.kt b/okio/src/commonTest/kotlin/okio/BufferedSinkFactory.kt index 8f4f29ae74..6793ec0c1d 100644 --- a/okio/src/commonTest/kotlin/okio/BufferedSinkFactory.kt +++ b/okio/src/commonTest/kotlin/okio/BufferedSinkFactory.kt @@ -16,21 +16,19 @@ package okio -internal interface BufferedSinkFactory { - - fun create(data: Buffer): BufferedSink - - companion object { - val BUFFER: BufferedSinkFactory = object : BufferedSinkFactory { - override fun create(data: Buffer): BufferedSink { - return data - } +enum class BufferedSinkFactory { + BasicBuffer { + override fun create(data: Buffer): BufferedSink { + return data } + }, - val REAL_BUFFERED_SINK: BufferedSinkFactory = object : BufferedSinkFactory { - override fun create(data: Buffer): BufferedSink { - return (data as Sink).buffer() - } + SinkBuffer { + override fun create(data: Buffer): BufferedSink { + return (data as Sink).buffer() } - } + }, + ; + + abstract fun create(data: Buffer): BufferedSink } diff --git a/okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt b/okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt index 887a472250..a6425ffb03 100644 --- a/okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt +++ b/okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt @@ -17,7 +17,7 @@ package okio enum class BufferedSourceFactory { - BUFFER { + NewBuffer { override val isOneByteAtATime: Boolean get() = false @@ -30,7 +30,7 @@ enum class BufferedSourceFactory { } }, - REAL_BUFFERED_SOURCE { + SourceBuffer { override val isOneByteAtATime: Boolean get() = false @@ -47,7 +47,7 @@ enum class BufferedSourceFactory { * A factory deliberately written to create buffers whose internal segments are always 1 byte * long. We like testing with these segments because are likely to trigger bugs! */ - ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE { + OneByteAtATimeSource { override val isOneByteAtATime: Boolean get() = true @@ -69,7 +69,7 @@ enum class BufferedSourceFactory { } }, - ONE_BYTE_AT_A_TIME_BUFFER { + OneByteAtATimeSink { override val isOneByteAtATime: Boolean get() = true @@ -92,7 +92,7 @@ enum class BufferedSourceFactory { } }, - PEEK_BUFFER { + PeekBuffer { override val isOneByteAtATime: Boolean get() = false @@ -105,7 +105,7 @@ enum class BufferedSourceFactory { } }, - PEEK_BUFFERED_SOURCE { + PeekBufferedSource { override val isOneByteAtATime: Boolean get() = false diff --git a/okio/src/commonTest/kotlin/okio/ByteStringFactory.kt b/okio/src/commonTest/kotlin/okio/ByteStringFactory.kt index bbf6cc6da7..92ee5c44fe 100644 --- a/okio/src/commonTest/kotlin/okio/ByteStringFactory.kt +++ b/okio/src/commonTest/kotlin/okio/ByteStringFactory.kt @@ -20,34 +20,32 @@ import okio.ByteString.Companion.decodeHex import okio.ByteString.Companion.encodeUtf8 import okio.internal.commonAsUtf8ToByteArray -internal interface ByteStringFactory { - fun decodeHex(hex: String): ByteString - - fun encodeUtf8(s: String): ByteString - - companion object { - val BYTE_STRING: ByteStringFactory = object : ByteStringFactory { - override fun decodeHex(hex: String) = hex.decodeHex() - override fun encodeUtf8(s: String) = s.encodeUtf8() - } - - val SEGMENTED_BYTE_STRING: ByteStringFactory = object : ByteStringFactory { - override fun decodeHex(hex: String) = Buffer().apply { write(hex.decodeHex()) }.snapshot() - override fun encodeUtf8(s: String) = Buffer().apply { writeUtf8(s) }.snapshot() - } - - val ONE_BYTE_PER_SEGMENT: ByteStringFactory = object : ByteStringFactory { - override fun decodeHex(hex: String) = makeSegments(hex.decodeHex()) - override fun encodeUtf8(s: String) = makeSegments(s.encodeUtf8()) - } - - // For Kotlin/JVM, the native Java UTF-8 encoder is used. This forces - // testing of the Okio encoder used for Kotlin/JS and Kotlin/Native to be - // tested on JVM as well. - val OKIO_ENCODER: ByteStringFactory = object : ByteStringFactory { - override fun decodeHex(hex: String) = hex.decodeHex() - override fun encodeUtf8(s: String) = - ByteString.of(*s.commonAsUtf8ToByteArray()) - } - } +enum class ByteStringFactory { + BasicByteString { + override fun decodeHex(hex: String) = hex.decodeHex() + override fun encodeUtf8(s: String) = s.encodeUtf8() + }, + + SegmentedByteString { + override fun decodeHex(hex: String) = Buffer().apply { write(hex.decodeHex()) }.snapshot() + override fun encodeUtf8(s: String) = Buffer().apply { writeUtf8(s) }.snapshot() + }, + + OneBytePerSegment { + override fun decodeHex(hex: String) = makeSegments(hex.decodeHex()) + override fun encodeUtf8(s: String) = makeSegments(s.encodeUtf8()) + }, + + // For Kotlin/JVM, the native Java UTF-8 encoder is used. This forces + // testing of the Okio encoder used for Kotlin/JS and Kotlin/Native to be + // tested on JVM as well. + OkioUtf8Encoder { + override fun decodeHex(hex: String) = hex.decodeHex() + override fun encodeUtf8(s: String) = ByteString.of(*s.commonAsUtf8ToByteArray()) + }, + ; + + abstract fun decodeHex(hex: String): ByteString + + abstract fun encodeUtf8(s: String): ByteString } diff --git a/okio/src/commonTest/kotlin/okio/ByteStringTest.kt b/okio/src/commonTest/kotlin/okio/ByteStringTest.kt index 9866e14bc1..da1a89888b 100644 --- a/okio/src/commonTest/kotlin/okio/ByteStringTest.kt +++ b/okio/src/commonTest/kotlin/okio/ByteStringTest.kt @@ -16,6 +16,7 @@ package okio +import app.cash.burst.Burst import kotlin.random.Random import kotlin.test.Test import kotlin.test.assertEquals @@ -31,12 +32,8 @@ import okio.ByteString.Companion.encodeUtf8 import okio.ByteString.Companion.toByteString import okio.internal.commonAsUtf8ToByteArray -class ByteStringTest : AbstractByteStringTest(ByteStringFactory.BYTE_STRING) -class SegmentedByteStringTest : AbstractByteStringTest(ByteStringFactory.SEGMENTED_BYTE_STRING) -class ByteStringOneBytePerSegmentTest : AbstractByteStringTest(ByteStringFactory.ONE_BYTE_PER_SEGMENT) -class OkioEncoderTest : AbstractByteStringTest(ByteStringFactory.OKIO_ENCODER) - -abstract class AbstractByteStringTest internal constructor( +@Burst +class ByteStringTest( private val factory: ByteStringFactory, ) { @Test fun get() { @@ -229,7 +226,7 @@ abstract class AbstractByteStringTest internal constructor( @Test fun toAsciiLowerCaseNoUppercase() { val s = factory.encodeUtf8("a1_+") assertEquals(s, s.toAsciiLowercase()) - if (factory === ByteStringFactory.BYTE_STRING) { + if (factory === ByteStringFactory.BasicByteString) { assertSame(s, s.toAsciiLowercase()) } } diff --git a/okio/src/commonTest/kotlin/okio/AbstractBufferedSinkTest.kt b/okio/src/commonTest/kotlin/okio/CommonBufferedSinkTest.kt similarity index 97% rename from okio/src/commonTest/kotlin/okio/AbstractBufferedSinkTest.kt rename to okio/src/commonTest/kotlin/okio/CommonBufferedSinkTest.kt index e922d5f298..08c093a871 100644 --- a/okio/src/commonTest/kotlin/okio/AbstractBufferedSinkTest.kt +++ b/okio/src/commonTest/kotlin/okio/CommonBufferedSinkTest.kt @@ -16,16 +16,15 @@ package okio +import app.cash.burst.Burst import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import okio.ByteString.Companion.decodeHex import okio.ByteString.Companion.encodeUtf8 -class BufferSinkTest : AbstractBufferedSinkTest(BufferedSinkFactory.BUFFER) -class RealBufferedSinkTest : AbstractBufferedSinkTest(BufferedSinkFactory.REAL_BUFFERED_SINK) - -abstract class AbstractBufferedSinkTest internal constructor( +@Burst +class CommonBufferedSinkTest( factory: BufferedSinkFactory, ) { private val data: Buffer = Buffer() diff --git a/okio/src/commonTest/kotlin/okio/AbstractBufferedSourceTest.kt b/okio/src/commonTest/kotlin/okio/CommonBufferedSourceTest.kt similarity index 97% rename from okio/src/commonTest/kotlin/okio/AbstractBufferedSourceTest.kt rename to okio/src/commonTest/kotlin/okio/CommonBufferedSourceTest.kt index adf959349d..fb8bdf243f 100644 --- a/okio/src/commonTest/kotlin/okio/AbstractBufferedSourceTest.kt +++ b/okio/src/commonTest/kotlin/okio/CommonBufferedSourceTest.kt @@ -16,6 +16,7 @@ package okio +import app.cash.burst.Burst import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -24,14 +25,8 @@ import kotlin.test.assertTrue import okio.ByteString.Companion.decodeHex import okio.ByteString.Companion.encodeUtf8 -class BufferSourceTest : AbstractBufferedSourceTest(BufferedSourceFactory.BUFFER) -class RealBufferedSourceTest : AbstractBufferedSourceTest(BufferedSourceFactory.REAL_BUFFERED_SOURCE) -class OneByteAtATimeBufferedSourceTest : AbstractBufferedSourceTest(BufferedSourceFactory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE) -class OneByteAtATimeBufferTest : AbstractBufferedSourceTest(BufferedSourceFactory.ONE_BYTE_AT_A_TIME_BUFFER) -class PeekBufferTest : AbstractBufferedSourceTest(BufferedSourceFactory.PEEK_BUFFER) -class PeekBufferedSourceTest : AbstractBufferedSourceTest(BufferedSourceFactory.PEEK_BUFFERED_SOURCE) - -abstract class AbstractBufferedSourceTest internal constructor( +@Burst +class CommonBufferedSourceTest( private val factory: BufferedSourceFactory, ) { private val sink: BufferedSink @@ -730,7 +725,7 @@ abstract class AbstractBufferedSourceTest internal constructor( } /** - * With [BufferedSourceFactory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE], this code was extremely slow. + * With [BufferedSourceFactory.OneByteAtATimeSource], this code was extremely slow. * https://github.com/square/okio/issues/171 */ @Test fun indexOfByteStringAcrossSegmentBoundaries() { @@ -1247,7 +1242,7 @@ abstract class AbstractBufferedSourceTest internal constructor( } @Test fun rangeEqualsOnlyReadsUntilMismatch() { - if (factory !== BufferedSourceFactory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE) return // Other sources read in chunks anyway. + if (factory !== BufferedSourceFactory.OneByteAtATimeSource) return // Other sources read in chunks anyway. sink.writeUtf8("A man, a plan, a canal. Panama.") sink.emit() diff --git a/okio/src/jvmTest/kotlin/okio/BufferCursorTest.kt b/okio/src/jvmTest/kotlin/okio/BufferCursorTest.kt index 11f122ec99..466552c9f7 100644 --- a/okio/src/jvmTest/kotlin/okio/BufferCursorTest.kt +++ b/okio/src/jvmTest/kotlin/okio/BufferCursorTest.kt @@ -139,7 +139,7 @@ class BufferCursorTest( @Test fun seekWithinSegment() { - assumeTrue(bufferFactory === BufferFactory.SMALL_SEGMENTED_BUFFER) + assumeTrue(bufferFactory === BufferFactory.SmallSegmentedBuffer) val buffer = bufferFactory.newBuffer() assertEquals("abcdefghijkl", buffer.clone().readUtf8()) buffer.readUnsafe().use { cursor -> diff --git a/okio/src/jvmTest/kotlin/okio/BufferFactory.kt b/okio/src/jvmTest/kotlin/okio/BufferFactory.kt index 0e6ce90627..4c82a0b48f 100644 --- a/okio/src/jvmTest/kotlin/okio/BufferFactory.kt +++ b/okio/src/jvmTest/kotlin/okio/BufferFactory.kt @@ -20,26 +20,26 @@ import okio.TestUtil.bufferWithRandomSegmentLayout import okio.TestUtil.bufferWithSegments enum class BufferFactory { - EMPTY { + Empty { override fun newBuffer(): Buffer { return Buffer() } }, - SMALL_BUFFER { + SmallBuffer { override fun newBuffer(): Buffer { return Buffer().writeUtf8("abcde") } }, - SMALL_SEGMENTED_BUFFER { + SmallSegmentedBuffer { @Throws(Exception::class) override fun newBuffer(): Buffer { return bufferWithSegments("abc", "defg", "hijkl") } }, - LARGE_BUFFER { + LargeBuffer { @Throws(Exception::class) override fun newBuffer(): Buffer { val dice = Random(0) @@ -50,7 +50,7 @@ enum class BufferFactory { } }, - LARGE_BUFFER_WITH_RANDOM_LAYOUT { + LargeBufferWithRandomLayout { @Throws(Exception::class) override fun newBuffer(): Buffer { val dice = Random(0) diff --git a/okio/src/jvmTest/kotlin/okio/BufferedSinkTest.kt b/okio/src/jvmTest/kotlin/okio/BufferedSinkTest.kt index 53d96786ad..c3e731dceb 100644 --- a/okio/src/jvmTest/kotlin/okio/BufferedSinkTest.kt +++ b/okio/src/jvmTest/kotlin/okio/BufferedSinkTest.kt @@ -34,13 +34,11 @@ class BufferedSinkTest( factory: Factory, ) { enum class Factory { - BUFFER { + NewBuffer { override fun create(data: Buffer) = data - override fun toString() = "Buffer" }, - REAL_BUFFERED_SINK { + SinkBuffer { override fun create(data: Buffer) = (data as Sink).buffer() - override fun toString() = "RealBufferedSink" }, ; diff --git a/okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt b/okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt index fe8783de7c..9b42fe1fdd 100644 --- a/okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt +++ b/okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt @@ -47,18 +47,16 @@ class BufferedSourceTest( private val factory: Factory, ) { enum class Factory { - BUFFER { + NewBuffer { override fun pipe(): Pipe { val buffer = Buffer() return Pipe(buffer, buffer) } override val isOneByteAtATime: Boolean get() = false - - override fun toString() = "Buffer" }, - REAL_BUFFERED_SOURCE { + SourceBuffer { override fun pipe(): Pipe { val buffer = Buffer() return Pipe( @@ -68,15 +66,13 @@ class BufferedSourceTest( } override val isOneByteAtATime: Boolean get() = false - - override fun toString() = "RealBufferedSource" }, /** * A factory deliberately written to create buffers whose internal segments are always 1 byte * long. We like testing with these segments because are likely to trigger bugs! */ - ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE { + OneByteAtATimeSource { override fun pipe(): Pipe { val buffer = Buffer() return Pipe( @@ -95,11 +91,9 @@ class BufferedSourceTest( } override val isOneByteAtATime: Boolean get() = true - - override fun toString() = "OneByteAtATimeBufferedSource" }, - ONE_BYTE_AT_A_TIME_BUFFER { + OneByteAtATimeSink { override fun pipe(): Pipe { val buffer = Buffer() val sink = object : ForwardingSink(buffer) { @@ -120,11 +114,9 @@ class BufferedSourceTest( } override val isOneByteAtATime: Boolean get() = true - - override fun toString() = "OneByteAtATimeBuffer" }, - PEEK_BUFFER { + PeekSource { override fun pipe(): Pipe { val buffer = Buffer() return Pipe( @@ -134,11 +126,9 @@ class BufferedSourceTest( } override val isOneByteAtATime: Boolean get() = false - - override fun toString() = "PeekBuffer" }, - PEEK_BUFFERED_SOURCE { + PeekBufferedSource { override fun pipe(): Pipe { val buffer = Buffer() return Pipe( @@ -148,8 +138,6 @@ class BufferedSourceTest( } override val isOneByteAtATime: Boolean get() = false - - override fun toString() = "PeekBufferedSource" }, ; @@ -826,7 +814,7 @@ class BufferedSourceTest( } /** - * With [Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE], this code was extremely slow. + * With [Factory.OneByteAtATimeSource], this code was extremely slow. * https://github.com/square/okio/issues/171 */ @Test @@ -1438,7 +1426,7 @@ class BufferedSourceTest( @Test fun rangeEqualsOnlyReadsUntilMismatch() { - Assume.assumeTrue(factory === Factory.ONE_BYTE_AT_A_TIME_BUFFERED_SOURCE) // Other sources read in chunks anyway. + Assume.assumeTrue(factory === Factory.OneByteAtATimeSource) // Other sources read in chunks anyway. sink.writeUtf8("A man, a plan, a canal. Panama.") sink.emit() assertFalse(source.rangeEquals(0, "A man.".encodeUtf8())) diff --git a/okio/src/jvmTest/kotlin/okio/ByteStringJavaTest.kt b/okio/src/jvmTest/kotlin/okio/ByteStringJavaTest.kt index 71882e67c1..b5f31fef8f 100644 --- a/okio/src/jvmTest/kotlin/okio/ByteStringJavaTest.kt +++ b/okio/src/jvmTest/kotlin/okio/ByteStringJavaTest.kt @@ -41,7 +41,7 @@ class ByteStringJavaTest( private val factory: Factory, ) { enum class Factory { - BYTE_STRING { + BaseByteString { override fun decodeHex(hex: String): ByteString { return hex.decodeHex() } @@ -50,7 +50,7 @@ class ByteStringJavaTest( return s.encodeUtf8() } }, - SEGMENTED_BYTE_STRING { + SegmentedByteString { override fun decodeHex(hex: String): ByteString { val buffer = Buffer() buffer.write(hex.decodeHex()) @@ -63,7 +63,7 @@ class ByteStringJavaTest( return buffer.snapshot() } }, - ONE_BYTE_PER_SEGMENT { + OneBytePerSegment { override fun decodeHex(hex: String): ByteString { return makeSegments(hex.decodeHex()) } diff --git a/okio/src/jvmTest/kotlin/okio/TimeoutFactory.kt b/okio/src/jvmTest/kotlin/okio/TimeoutFactory.kt index 3a9cac7caf..7ab0479f87 100644 --- a/okio/src/jvmTest/kotlin/okio/TimeoutFactory.kt +++ b/okio/src/jvmTest/kotlin/okio/TimeoutFactory.kt @@ -16,15 +16,15 @@ package okio enum class TimeoutFactory { - BASE { + Base { override fun newTimeout() = Timeout() }, - FORWARDING { - override fun newTimeout() = ForwardingTimeout(BASE.newTimeout()) + Forwarding { + override fun newTimeout() = ForwardingTimeout(Base.newTimeout()) }, - ASYNC { + Async { override fun newTimeout() = AsyncTimeout() }, ; diff --git a/okio/src/jvmTest/kotlin/okio/internal/HmacTest.kt b/okio/src/jvmTest/kotlin/okio/internal/HmacTest.kt index 80024866e2..8c31517d82 100644 --- a/okio/src/jvmTest/kotlin/okio/internal/HmacTest.kt +++ b/okio/src/jvmTest/kotlin/okio/internal/HmacTest.kt @@ -69,9 +69,9 @@ enum class Algorithm( val algorithmName: String, internal val HmacFactory: (key: ByteString) -> Hmac, ) { - SHA_1("HmacSha1", Hmac.Companion::sha1), - SHA_256("HmacSha256", Hmac.Companion::sha256), - SHA_512("HmacSha512", Hmac.Companion::sha512), + Sha1("HmacSha1", Hmac.Companion::sha1), + Sha256("HmacSha256", Hmac.Companion::sha256), + Sha512("HmacSha512", Hmac.Companion::sha512), } private fun hmac(algorithm: String, key: ByteArray, bytes: ByteArray) =