From ee69b1a676b59e0cf8ebfa86343561e0bf6655f7 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Tue, 14 Nov 2023 18:05:58 +0100 Subject: [PATCH] IdentityServiceTest: Flush data stores after each test --- .../java/bisq/identity/IdentityService.java | 8 +- .../bisq/identity/IdentityServiceTest.java | 156 ++++-------------- .../java/bisq/persistence/Persistence.java | 4 + .../java/bisq/security/KeyPairService.java | 4 +- 4 files changed, 45 insertions(+), 127 deletions(-) diff --git a/identity/src/main/java/bisq/identity/IdentityService.java b/identity/src/main/java/bisq/identity/IdentityService.java index 83b80462e9..4dd44a1766 100644 --- a/identity/src/main/java/bisq/identity/IdentityService.java +++ b/identity/src/main/java/bisq/identity/IdentityService.java @@ -117,7 +117,7 @@ public Identity getOrCreateDefaultIdentity() { synchronized (lock) { persistableStore.setDefaultIdentity(Optional.of(identity)); } - persist().join(); + persist(); return identity; }); } @@ -128,7 +128,7 @@ public Identity createAndInitializeNewActiveIdentity(String tag) { synchronized (lock) { getActiveIdentityByTag().put(tag, identity); } - persist().join(); + persist(); return identity; } @@ -143,7 +143,7 @@ public boolean retireActiveIdentity(String tag) { } } if (wasRemoved) { - persist().join(); + persist(); } return wasRemoved; } @@ -205,7 +205,7 @@ private Identity createAndInitializeNewActiveIdentity(String tag, String keyId, synchronized (lock) { getActiveIdentityByTag().put(tag, identity); } - persist().join(); + persist(); return identity; } diff --git a/identity/src/test/java/bisq/identity/IdentityServiceTest.java b/identity/src/test/java/bisq/identity/IdentityServiceTest.java index 14482dd240..bab46063d3 100644 --- a/identity/src/test/java/bisq/identity/IdentityServiceTest.java +++ b/identity/src/test/java/bisq/identity/IdentityServiceTest.java @@ -25,6 +25,8 @@ import bisq.persistence.PersistenceService; import bisq.security.KeyPairService; import bisq.security.PubKey; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -39,16 +41,29 @@ import static org.mockito.Mockito.when; public class IdentityServiceTest { - @Test - void getOrCreateIdentityTest(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - + @TempDir + private Path tempDir; + private KeyPairService keyPairService; + private IdentityService identityService; + + @BeforeEach + void setUp() { + PersistenceService persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); NetworkService networkService = mock(NetworkService.class); when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + keyPairService = new KeyPairService(persistenceService); + identityService = new IdentityService(persistenceService, keyPairService, networkService); + } + + @AfterEach + void tearDown() { + keyPairService.getPersistence().flush().join(); + identityService.getPersistence().flush().join(); + } + @Test + void getOrCreateIdentityTest() { String myTag = "myTag"; Identity activeIdentity = identityService.getOrCreateIdentity(myTag); Identity persistedActiveIdentity = identityService.getOrCreateIdentity(myTag); @@ -59,15 +74,7 @@ void getOrCreateIdentityTest(@TempDir Path tempDir) { } @Test - void getOrCreateIdentityWithAllArguments(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); - + void getOrCreateIdentityWithAllArguments() { String myTag = "myTag"; KeyPair keyPair = keyPairService.getOrCreateKeyPair(myTag); Identity activeIdentity = identityService.getOrCreateIdentity(myTag, myTag, keyPair); @@ -84,15 +91,7 @@ void getOrCreateIdentityWithAllArguments(@TempDir Path tempDir) { } @Test - void getOrCreateDefaultIdentityTest(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); - + void getOrCreateDefaultIdentityTest() { Identity firstDefaultIdentity = identityService.getOrCreateDefaultIdentity(); assertThat(firstDefaultIdentity.getTag()) .isEqualTo(IdentityService.DEFAULT_IDENTITY_TAG); @@ -102,15 +101,7 @@ void getOrCreateDefaultIdentityTest(@TempDir Path tempDir) { } @Test - void createNewIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); - + void createNewIdentity() { String myTag = "myTag"; Identity activeIdentity = identityService.createAndInitializeNewActiveIdentity(myTag); Identity anotherActiveIdentity = identityService.createAndInitializeNewActiveIdentity(myTag); @@ -119,25 +110,13 @@ void createNewIdentity(@TempDir Path tempDir) { } @Test - void retireInvalidIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - NetworkService networkService = mock(NetworkService.class); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void retireInvalidIdentity() { boolean isRemoved = identityService.retireActiveIdentity("tag"); assertThat(isRemoved).isFalse(); } @Test - void retireActiveIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void retireActiveIdentity() { String myTag = "myTag"; Identity identity = identityService.getOrCreateIdentity(myTag); @@ -156,26 +135,13 @@ void retireActiveIdentity(@TempDir Path tempDir) { } @Test - void findInvalidIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findInvalidIdentity() { Optional activeIdentity = identityService.findActiveIdentity("tag"); assertThat(activeIdentity).isEmpty(); } @Test - void findInvalidIdentityByNetworkId(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - + void findInvalidIdentityByNetworkId() { AddressByTransportTypeMap addressByTransportTypeMap = new AddressByTransportTypeMap( Map.of(TransportType.CLEAR, Address.localHost(1234))); @@ -183,20 +149,12 @@ void findInvalidIdentityByNetworkId(@TempDir Path tempDir) { var pubKey = new PubKey(keyPair.getPublic(), "keyId"); var networkId = new NetworkId(addressByTransportTypeMap, pubKey); - var identityService = new IdentityService(persistenceService, keyPairService, networkService); Optional activeIdentity = identityService.findActiveIdentityByNetworkId(networkId); assertThat(activeIdentity).isEmpty(); } @Test - void findActiveIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findActiveIdentity() { String myTag = "myTag"; Identity identity = identityService.getOrCreateIdentity(myTag); @@ -205,14 +163,7 @@ void findActiveIdentity(@TempDir Path tempDir) { } @Test - void findActiveIdentityByNetworkId(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findActiveIdentityByNetworkId() { String myTag = "myTag"; Identity identity = identityService.getOrCreateIdentity(myTag); @@ -221,15 +172,7 @@ void findActiveIdentityByNetworkId(@TempDir Path tempDir) { } @Test - void findInvalidRetiredIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); - + void findInvalidRetiredIdentity() { AddressByTransportTypeMap addressByTransportTypeMap = new AddressByTransportTypeMap( Map.of(TransportType.CLEAR, Address.localHost(1234))); @@ -242,14 +185,7 @@ void findInvalidRetiredIdentity(@TempDir Path tempDir) { } @Test - void findRetiredIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findRetiredIdentity() { Identity identity = identityService.getOrCreateIdentity("tag"); identityService.retireActiveIdentity("tag"); @@ -258,30 +194,15 @@ void findRetiredIdentity(@TempDir Path tempDir) { } @Test - void findDefaultIdentityInFindAnyIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findDefaultIdentityInFindAnyIdentity() { Identity defaultIdentity = identityService.getOrCreateDefaultIdentity(); - Optional identityByNetworkId = identityService .findAnyIdentityByNetworkId(defaultIdentity.getNetworkId()); assertThat(identityByNetworkId).hasValue(defaultIdentity); } @Test - void findActiveIdentityInFindAnyIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findActiveIdentityInFindAnyIdentity() { String myTag = "myTag"; Identity identity = identityService.getOrCreateIdentity(myTag); @@ -291,14 +212,7 @@ void findActiveIdentityInFindAnyIdentity(@TempDir Path tempDir) { } @Test - void findRetiredIdentityInFindAnyIdentity(@TempDir Path tempDir) { - var persistenceService = new PersistenceService(tempDir.toAbsolutePath().toString()); - var keyPairService = new KeyPairService(persistenceService); - - NetworkService networkService = mock(NetworkService.class); - when(networkService.getSupportedTransportTypes()).thenReturn(Set.of(TransportType.TOR)); - - var identityService = new IdentityService(persistenceService, keyPairService, networkService); + void findRetiredIdentityInFindAnyIdentity() { Identity identity = identityService.getOrCreateIdentity("tag"); identityService.retireActiveIdentity("tag"); diff --git a/persistence/src/main/java/bisq/persistence/Persistence.java b/persistence/src/main/java/bisq/persistence/Persistence.java index bd9d63184c..44b4b9e67b 100644 --- a/persistence/src/main/java/bisq/persistence/Persistence.java +++ b/persistence/src/main/java/bisq/persistence/Persistence.java @@ -61,6 +61,10 @@ public CompletableFuture persistAsync(T serializable) { }, executorService); } + public CompletableFuture flush() { + return CompletableFuture.runAsync(() -> Thread.currentThread().setName("Flush-Persistence.persist-" + storePath), executorService); + } + protected void persist(T persistableStore) { persistableStoreReaderWriter.write(persistableStore); } diff --git a/security/src/main/java/bisq/security/KeyPairService.java b/security/src/main/java/bisq/security/KeyPairService.java index b8f02249c8..9cb35be62e 100644 --- a/security/src/main/java/bisq/security/KeyPairService.java +++ b/security/src/main/java/bisq/security/KeyPairService.java @@ -84,7 +84,7 @@ public void persistKeyPair(String keyId, KeyPair keyPair) { synchronized (persistableStore) { persistableStore.put(keyId, keyPair); } - persist().join(); + persist(); } public CompletableFuture getOrCreateKeyPairAsync(String keyId) { @@ -95,7 +95,7 @@ public CompletableFuture getOrCreateKeyPairAsync(String keyId) { synchronized (persistableStore) { persistableStore.put(keyId, keyPair); } - persist().join(); + persist(); return keyPair; } catch (GeneralSecurityException e) { e.printStackTrace();