Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io |
| 2 | |
Sergey Mashkov | 6d05993 | 2017-10-06 18:46:23 +0300 | [diff] [blame] | 3 | import kotlinx.io.pool.* |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 4 | import org.junit.rules.* |
| 5 | import org.junit.runner.* |
| 6 | import org.junit.runners.model.* |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 7 | import java.io.* |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 8 | import java.util.concurrent.* |
| 9 | import kotlin.test.* |
| 10 | |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 11 | class VerifyingObjectPool<T : Any> internal constructor(private val delegate: ObjectPool<T>, private val log: Boolean = false) : ObjectPool<T> by delegate, TestRule { |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 12 | private val allocated = ConcurrentHashMap<IdentityWrapper<T>, Boolean>() |
| 13 | |
Sergey Mashkov | ccf8dde | 2017-09-29 18:18:00 +0300 | [diff] [blame] | 14 | val used: Int |
| 15 | get() = allocated.size |
| 16 | |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 17 | override fun borrow(): T { |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 18 | if (log) { |
| 19 | Trace.Allocate().dump() |
| 20 | } |
| 21 | |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 22 | val instance = delegate.borrow() |
| 23 | if (allocated.put(IdentityWrapper(instance), true) != null) { |
| 24 | throw AssertionError("Instance $instance has been provided by the pool twice") |
| 25 | } |
| 26 | return instance |
| 27 | } |
| 28 | |
| 29 | override fun recycle(instance: T) { |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 30 | if (log) { |
| 31 | Trace.Recycle().dump() |
| 32 | } |
| 33 | |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 34 | if (allocated.remove(IdentityWrapper(instance)) == null) { |
| 35 | throw AssertionError("Instance $instance hasn't been borrowed but tried to recycle (possibly double recycle)") |
| 36 | } |
| 37 | delegate.recycle(instance) |
| 38 | } |
| 39 | |
| 40 | override fun apply(base: Statement, description: Description): Statement { |
| 41 | return object: Statement() { |
| 42 | override fun evaluate() { |
Sergey Mashkov | ccf8dde | 2017-09-29 18:18:00 +0300 | [diff] [blame] | 43 | var failed = false |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 44 | try { |
| 45 | base.evaluate() |
| 46 | } catch (t: Throwable) { |
Sergey Mashkov | ccf8dde | 2017-09-29 18:18:00 +0300 | [diff] [blame] | 47 | failed = true |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 48 | try { |
| 49 | assertEmpty() |
| 50 | } catch (emptyFailed: Throwable) { |
| 51 | throw MultipleFailureException(listOf(t, emptyFailed)) |
| 52 | } |
| 53 | throw t |
| 54 | } finally { |
Sergey Mashkov | ccf8dde | 2017-09-29 18:18:00 +0300 | [diff] [blame] | 55 | if (!failed) { |
| 56 | assertEmpty() |
| 57 | } |
Sergey Mashkov | 57906eb | 2017-09-19 14:08:06 +0300 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private fun assertEmpty() { |
| 64 | assertEquals(0, allocated.size) |
| 65 | } |
| 66 | |
| 67 | private class IdentityWrapper<T : Any>(private val instance: T) { |
| 68 | override fun equals(other: Any?): Boolean { |
| 69 | if (other !is IdentityWrapper<*>) return false |
| 70 | return other.instance === this.instance |
| 71 | } |
| 72 | |
| 73 | override fun hashCode() = System.identityHashCode(instance) |
| 74 | } |
Sergey Mashkov | 88951ac | 2017-10-27 22:36:08 +0300 | [diff] [blame] | 75 | |
| 76 | private sealed class Trace : RuntimeException() { |
| 77 | class Allocate : Trace() |
| 78 | class Recycle() : Trace() |
| 79 | |
| 80 | fun dump() { |
| 81 | val sb = StringWriter() |
| 82 | val pw = PrintWriter(sb, false) |
| 83 | printStackTrace(pw) |
| 84 | pw.close() |
| 85 | |
| 86 | println(sb.buffer) |
| 87 | } |
| 88 | } |
| 89 | } |