Minor: add logging to the RecordingMap unit test utility

This commit is contained in:
Mike Hearn 2016-02-17 15:37:13 +01:00
parent b2b51183b8
commit 55989a8e92
2 changed files with 10 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import core.testutils.RecordingMap
import core.testutils.TEST_KEYS_TO_CORP_MAP
import core.testutils.TEST_PROGRAM_MAP
import core.testutils.TEST_TX_TIME
import org.slf4j.LoggerFactory
import java.security.KeyPair
import java.security.PrivateKey
import java.security.PublicKey
@ -67,7 +68,7 @@ class MockWalletService(val states: List<StateAndRef<OwnableState>>) : WalletSer
}
@ThreadSafe
class MockStorageService(val isRecording: Boolean = false) : StorageService {
class MockStorageService(val recordingAs: Map<String, String>? = null) : StorageService {
override val myLegalIdentityKey: KeyPair = generateKeyPair()
override val myLegalIdentity: Party = Party("Unit test party", myLegalIdentityKey.public)
@ -83,8 +84,8 @@ class MockStorageService(val isRecording: Boolean = false) : StorageService {
synchronized(tables) {
return tables.getOrPut(tableName) {
val map = Collections.synchronizedMap(HashMap<Any, Any>())
if (isRecording)
RecordingMap(map)
if (recordingAs != null && recordingAs[tableName] != null)
RecordingMap(map, LoggerFactory.getLogger("recordingmap.${recordingAs[tableName]}"))
else
map
} as MutableMap<K, V>

View File

@ -16,6 +16,8 @@
package core.testutils
import core.utilities.loggerFor
import org.slf4j.Logger
import java.util.*
import javax.annotation.concurrent.ThreadSafe
@ -26,7 +28,8 @@ import javax.annotation.concurrent.ThreadSafe
* Note: although this class itself thread safe, if the underlying map is not, then this class loses its thread safety.
*/
@ThreadSafe
class RecordingMap<K, V>(private val wrappedMap: MutableMap<K, V>) : MutableMap<K, V> by wrappedMap {
class RecordingMap<K, V>(private val wrappedMap: MutableMap<K, V>,
private val logger: Logger = loggerFor<RecordingMap<K, V>>()) : MutableMap<K, V> by wrappedMap {
// If/when Kotlin supports data classes inside sealed classes, that would be preferable to this.
interface Record
data class Get<K>(val key: K) : Record
@ -41,11 +44,13 @@ class RecordingMap<K, V>(private val wrappedMap: MutableMap<K, V>) : MutableMap<
override fun get(key: K): V? {
_records.add(Get(key))
logger.trace("GET ${logger.name} : $key = ${wrappedMap[key]}")
return wrappedMap[key]
}
override fun put(key: K, value: V): V? {
_records.add(Put(key, value))
logger.trace("PUT ${logger.name} : $key = $value")
return wrappedMap.put(key, value)
}