forked from corda/corda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORDA-2705 - Prevent duplicates in cache and fix the mappings persist…
…ed for confidential identities
- Loading branch information
Showing
3 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...st/kotlin/net/corda/node/services/persistence/AppendOnlyPersistentMapNonConcurrentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package net.corda.node.services.persistence | ||
|
||
import net.corda.core.schemas.MappedSchema | ||
import net.corda.node.services.schema.NodeSchemaService | ||
import net.corda.node.utilities.AppendOnlyPersistentMap | ||
import net.corda.nodeapi.internal.persistence.DatabaseConfig | ||
import net.corda.testing.internal.TestingNamedCacheFactory | ||
import net.corda.testing.internal.configureDatabase | ||
import net.corda.testing.node.MockServices | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.After | ||
import org.junit.Test | ||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.Id | ||
|
||
class AppendOnlyPersistentMapNonConcurrentTest { | ||
|
||
private val database = configureDatabase(MockServices.makeTestDataSourceProperties(), | ||
DatabaseConfig(), | ||
{ null }, { null }, | ||
NodeSchemaService(setOf(MappedSchema(AppendOnlyPersistentMapTest::class.java, 1, listOf(AppendOnlyPersistentMapNonConcurrentTest.PersistentMapEntry::class.java))))) | ||
|
||
@Entity | ||
@javax.persistence.Table(name = "persist_map_test") | ||
class PersistentMapEntry( | ||
@Id | ||
@Column(name = "key") | ||
var key: Long = -1, | ||
|
||
@Column(name = "value", length = 16) | ||
var value: String = "" | ||
) | ||
|
||
class TestMap(cacheSize: Long) : AppendOnlyPersistentMap<Long, String, PersistentMapEntry, Long>( | ||
cacheFactory = TestingNamedCacheFactory(cacheSize), | ||
name = "ApoendOnlyPersistentMap_test", | ||
toPersistentEntityKey = { it }, | ||
fromPersistentEntity = { Pair(it.key, it.value) }, | ||
toPersistentEntity = { key: Long, value: String -> | ||
PersistentMapEntry().apply { | ||
this.key = key | ||
this.value = value | ||
} | ||
}, | ||
persistentEntityClass = PersistentMapEntry::class.java | ||
) | ||
|
||
private fun createMap(cacheSize: Long) = TestMap(cacheSize) | ||
|
||
@After | ||
fun closeDatabase() { | ||
database.close() | ||
} | ||
|
||
@Test | ||
fun `map prevents duplicates, when key has been evicted from cache, but present in database`() { | ||
val map = database.transaction { | ||
createMap(1) | ||
} | ||
|
||
|
||
database.transaction { | ||
map.addWithDuplicatesAllowed(1, "1") | ||
map.addWithDuplicatesAllowed(3, "3") | ||
} | ||
|
||
database.transaction { | ||
map.addWithDuplicatesAllowed(1, "2") | ||
} | ||
|
||
val result = database.transaction { | ||
map[1] | ||
} | ||
|
||
assertThat(result).isEqualTo("1") | ||
} | ||
|
||
} |