From 71f18f5ff3a418d29cdc1ea4b081ecf7b96bc8de Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Fri, 30 Oct 2026 12:57:29 -0500 Subject: [PATCH] test(appconfig): reuse named stores in race fixtures (#7026) Apply the separate follow-up requested in upstream #6791. Register named backing stores through InMemoryStorageFactory and preserve existing AppConfig update, delete and tagging race assertions and synchronization. Source projection from fork #271; original source and metadata preserved. Signed-off-by: Andre Kurait --- .../AppConfigServiceTaggingTest.java | 15 +---- .../AppConfigServiceUpdateRaceTest.java | 16 +---- .../floci/testing/InMemoryStorageFactory.java | 7 +++ .../testing/InMemoryStorageFactoryTest.java | 59 +++++++++++++++++++ 4 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactoryTest.java diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java index cdb2756124..482a448cb3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceTaggingTest.java @@ -1,10 +1,6 @@ package io.github.hectorvent.floci.services.appconfig; -import com.fasterxml.jackson.core.type.TypeReference; -import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; -import io.github.hectorvent.floci.core.storage.StorageFactory; -import io.github.hectorvent.floci.core.storage.WriteProfile; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.testing.InMemoryStorageFactory; import io.github.hectorvent.floci.testing.ParkingStore; @@ -94,15 +90,6 @@ class AppConfigServiceTaggingTest { /** An AppConfig service whose application store is {@code store} and every other store in memory. */ private static AppConfigService serviceWithApplicationStore(StorageBackend store) { - return new AppConfigService(new StorageFactory(null, null) { - @Override - @SuppressWarnings("unchecked") - public AccountAwareStorageBackend create(String serviceName, String fileName, - TypeReference> typeReference, WriteProfile profile) { - return fileName.equals(APPLICATIONS) - ? new AccountAwareStorageBackend<>((StorageBackend) (Object) store, null, ACCOUNT) - : AccountAwareStorageBackend.inMemory(ACCOUNT); - } - }, null); + return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(APPLICATIONS, store), null); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java index 4195b78ee2..02cdebc877 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigServiceUpdateRaceTest.java @@ -1,14 +1,11 @@ package io.github.hectorvent.floci.services.appconfig; -import com.fasterxml.jackson.core.type.TypeReference; -import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; -import io.github.hectorvent.floci.core.storage.StorageFactory; -import io.github.hectorvent.floci.core.storage.WriteProfile; import io.github.hectorvent.floci.services.appconfig.model.Application; import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile; import io.github.hectorvent.floci.services.appconfig.model.DeploymentStrategy; import io.github.hectorvent.floci.services.appconfig.model.Environment; +import io.github.hectorvent.floci.testing.InMemoryStorageFactory; import io.github.hectorvent.floci.testing.ParkingStore; import org.junit.jupiter.api.Test; @@ -132,15 +129,6 @@ class AppConfigServiceUpdateRaceTest { /** An AppConfig service whose {@code fileName} store is {@code store} and every other store in memory. */ private static AppConfigService serviceWithStore(String fileName, StorageBackend store) { - return new AppConfigService(new StorageFactory(null, null) { - @Override - @SuppressWarnings("unchecked") - public AccountAwareStorageBackend create(String serviceName, String file, - TypeReference> typeReference, WriteProfile profile) { - return file.equals(fileName) - ? new AccountAwareStorageBackend<>((StorageBackend) (Object) store, null, ACCOUNT) - : AccountAwareStorageBackend.inMemory(ACCOUNT); - } - }, null); + return new AppConfigService(new InMemoryStorageFactory(ACCOUNT).withStore(fileName, store), null); } } diff --git a/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java index c22e5b6e8f..83726cea4e 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java +++ b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactory.java @@ -31,6 +31,13 @@ public final class InMemoryStorageFactory extends StorageFactory { this.accountId = accountId; } + /** Registers a backing store before constructing the services that use this factory. */ + public InMemoryStorageFactory withStore(String fileName, StorageBackend store) { + stores.put(fileName, store instanceof AccountAwareStorageBackend scoped + ? scoped : new AccountAwareStorageBackend<>(store, null, accountId)); + return this; + } + @Override public AccountAwareStorageBackend create(String serviceName, String fileName, TypeReference> typeReference) { diff --git a/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactoryTest.java b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactoryTest.java new file mode 100644 index 0000000000..3d7e5d0948 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/InMemoryStorageFactoryTest.java @@ -0,0 +1,59 @@ +package io.github.hectorvent.floci.testing; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.WriteProfile; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; + +class InMemoryStorageFactoryTest { + + private static final String ACCOUNT = "123456789012"; + private static final TypeReference> TYPE = new TypeReference<>() {}; + + @Test + void suppliedBackingStoreUsesTheFactoryAccountAndBothCreateOverloads() { + InMemoryStorage backing = new InMemoryStorage<>(); + InMemoryStorageFactory factory = new InMemoryStorageFactory(ACCOUNT).withStore("items.json", backing); + AccountAwareStorageBackend store = factory.create("service", "items.json", TYPE); + + store.put("item", "value"); + + assertEquals("value", new AccountAwareStorageBackend<>(backing, null, ACCOUNT).get("item").orElseThrow()); + assertSame(store, factory.create("service", "items.json", TYPE, WriteProfile.DEFAULT)); + } + + @Test + void accountAwareOverridesRetainIdentityAndDoNotWrapTwice() { + AccountAwareStorageBackend first = AccountAwareStorageBackend.inMemory(ACCOUNT); + AccountAwareStorageBackend second = AccountAwareStorageBackend.inMemory(ACCOUNT); + InMemoryStorageFactory factory = new InMemoryStorageFactory(ACCOUNT) + .withStore("first.json", first).withStore("second.json", second); + + factory.create("service", "first.json", TYPE).put("item", "first"); + factory.create("service", "second.json", TYPE, WriteProfile.DEFAULT).put("item", "second"); + + assertSame(first, factory.create("service", "first.json", TYPE)); + assertSame(second, factory.create("service", "second.json", TYPE)); + assertEquals("first", first.get("item").orElseThrow()); + assertEquals("second", second.get("item").orElseThrow()); + } + + @Test + void unrelatedStoresRemainSharedByFileName() { + InMemoryStorageFactory factory = new InMemoryStorageFactory(ACCOUNT) + .withStore("overridden.json", new InMemoryStorage()); + AccountAwareStorageBackend store = factory.create("one", "other.json", TYPE); + store.put("item", "value"); + + AccountAwareStorageBackend reused = factory.create("two", "other.json", TYPE, WriteProfile.DEFAULT); + + assertSame(store, reused); + assertEquals("value", reused.get("item").orElseThrow()); + } +}