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

Use Burst more in our test suite #1538

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 12 additions & 14 deletions okio/src/commonTest/kotlin/okio/BufferedSinkFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions okio/src/commonTest/kotlin/okio/BufferedSourceFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package okio

enum class BufferedSourceFactory {
BUFFER {
NewBuffer {
override val isOneByteAtATime: Boolean
get() = false

Expand All @@ -30,7 +30,7 @@ enum class BufferedSourceFactory {
}
},

REAL_BUFFERED_SOURCE {
SourceBuffer {
override val isOneByteAtATime: Boolean
get() = false

Expand All @@ -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

Expand All @@ -69,7 +69,7 @@ enum class BufferedSourceFactory {
}
},

ONE_BYTE_AT_A_TIME_BUFFER {
OneByteAtATimeSink {
override val isOneByteAtATime: Boolean
get() = true

Expand All @@ -92,7 +92,7 @@ enum class BufferedSourceFactory {
}
},

PEEK_BUFFER {
PeekBuffer {
override val isOneByteAtATime: Boolean
get() = false

Expand All @@ -105,7 +105,7 @@ enum class BufferedSourceFactory {
}
},

PEEK_BUFFERED_SOURCE {
PeekBufferedSource {
override val isOneByteAtATime: Boolean
get() = false

Expand Down
58 changes: 28 additions & 30 deletions okio/src/commonTest/kotlin/okio/ByteStringFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 4 additions & 7 deletions okio/src/commonTest/kotlin/okio/ByteStringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package okio

import app.cash.burst.Burst
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -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() {
Expand Down Expand Up @@ -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())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package okio

import app.cash.burst.Burst
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion okio/src/jvmTest/kotlin/okio/BufferCursorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
10 changes: 5 additions & 5 deletions okio/src/jvmTest/kotlin/okio/BufferFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -50,7 +50,7 @@ enum class BufferFactory {
}
},

LARGE_BUFFER_WITH_RANDOM_LAYOUT {
LargeBufferWithRandomLayout {
@Throws(Exception::class)
override fun newBuffer(): Buffer {
val dice = Random(0)
Expand Down
6 changes: 2 additions & 4 deletions okio/src/jvmTest/kotlin/okio/BufferedSinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
;

Expand Down
Loading