Extend identity service tests

RBS report problems with multiple nodes being able to find each other in the identity service. This extends the name
lookup test case to try three nodes, to check all of the stored values, and to use random keys rather than static.

Add trace log of identity registrations to aid with tracking state.
This commit is contained in:
Ross Nicoll 2017-03-17 10:39:10 +00:00
parent 558a3207e9
commit c3cf2226ea
2 changed files with 21 additions and 8 deletions

View File

@ -6,6 +6,8 @@ import net.corda.core.crypto.CompositeKey
import net.corda.core.crypto.Party import net.corda.core.crypto.Party
import net.corda.core.node.services.IdentityService import net.corda.core.node.services.IdentityService
import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.serialization.SingletonSerializeAsToken
import net.corda.core.utilities.loggerFor
import net.corda.core.utilities.trace
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import javax.annotation.concurrent.ThreadSafe import javax.annotation.concurrent.ThreadSafe
@ -15,10 +17,15 @@ import javax.annotation.concurrent.ThreadSafe
*/ */
@ThreadSafe @ThreadSafe
class InMemoryIdentityService() : SingletonSerializeAsToken(), IdentityService { class InMemoryIdentityService() : SingletonSerializeAsToken(), IdentityService {
companion object {
private val log = loggerFor<InMemoryIdentityService>()
}
private val keyToParties = ConcurrentHashMap<CompositeKey, Party>() private val keyToParties = ConcurrentHashMap<CompositeKey, Party>()
private val nameToParties = ConcurrentHashMap<String, Party>() private val nameToParties = ConcurrentHashMap<String, Party>()
override fun registerIdentity(party: Party) { override fun registerIdentity(party: Party) {
log.trace { "Registering identity ${party}" }
keyToParties[party.owningKey] = party keyToParties[party.owningKey] = party
nameToParties[party.name] = party nameToParties[party.name] = party
} }

View File

@ -1,10 +1,10 @@
package net.corda.node.services package net.corda.node.services
import net.corda.core.crypto.Party
import net.corda.core.crypto.composite
import net.corda.core.crypto.generateKeyPair
import net.corda.node.services.identity.InMemoryIdentityService import net.corda.node.services.identity.InMemoryIdentityService
import net.corda.testing.ALICE import net.corda.testing.*
import net.corda.testing.ALICE_PUBKEY
import net.corda.testing.BOB
import net.corda.testing.BOB_PUBKEY
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertNull import kotlin.test.assertNull
@ -40,11 +40,17 @@ class InMemoryIdentityServiceTests {
} }
@Test @Test
fun `get identity by name`() { fun `get identity by name with no registered identities`() {
val service = InMemoryIdentityService() val service = InMemoryIdentityService()
assertNull(service.partyFromName(ALICE.name)) assertNull(service.partyFromName(ALICE.name))
service.registerIdentity(ALICE) }
assertEquals(ALICE, service.partyFromName(ALICE.name))
assertNull(service.partyFromName(BOB.name)) @Test
fun `get identity by name`() {
val service = InMemoryIdentityService()
val identities = listOf("Node A", "Node B", "Node C").map { Party(it, generateKeyPair().public.composite) }
assertNull(service.partyFromName(identities.first().name))
identities.forEach { service.registerIdentity(it) }
identities.forEach { assertEquals(it, service.partyFromName(it.name)) }
} }
} }