Skip to content

Commit 1969a34

Browse files
Merge pull request #118 from SpineEventEngine/bump-versions
Migrate to new logging API
2 parents 2c27cba + 6c8279f commit 1969a34

File tree

15 files changed

+124
-77
lines changed

15 files changed

+124
-77
lines changed

datastore/src/main/java/io/spine/server/storage/datastore/DatastoreWrapper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public Entity read(Key key) {
270270
* @see DatastoreReader#run(Query)
271271
*/
272272
public DsQueryIterator read(StructuredQuery<Entity> query) {
273-
Namespace namespace = getNamespace();
273+
Namespace namespace = currentNamespace();
274274
StructuredQuery<Entity> queryWithNamespace =
275275
query.toBuilder()
276276
.setNamespace(namespace.getValue())
@@ -377,7 +377,7 @@ public void delete(Key... keys) {
377377
* kind (a.k.a. type, table, etc.) of the records to delete
378378
*/
379379
void dropTable(String table) {
380-
Namespace namespace = getNamespace();
380+
Namespace namespace = currentNamespace();
381381
StructuredQuery<Entity> query =
382382
Query.newEntityQueryBuilder()
383383
.setNamespace(namespace.getValue())
@@ -497,7 +497,7 @@ public KeyFactory keyFactory(Kind kind) {
497497
if (keyFactory == null) {
498498
keyFactory = initKeyFactory(kind);
499499
}
500-
Namespace namespace = getNamespace();
500+
Namespace namespace = currentNamespace();
501501
keyFactory.setNamespace(namespace.getValue());
502502

503503
return keyFactory;
@@ -536,8 +536,8 @@ private KeyFactory initKeyFactory(Kind kind) {
536536
*/
537537
private Iterator<Entity> readBulk(List<Key> keys) {
538538
int pageCount = keys.size() / MAX_KEYS_PER_READ_REQUEST + 1;
539-
log().debug("Reading a big bulk of entities synchronously. The data is read as {} pages.",
540-
pageCount);
539+
_debug().log("Reading a big bulk of entities synchronously." +
540+
" The data is read as %d pages.", pageCount);
541541
int lowerBound = 0;
542542
int higherBound = MAX_KEYS_PER_READ_REQUEST;
543543
int keysLeft = keys.size();
@@ -567,9 +567,9 @@ private void writeBulk(Entity[] entities) {
567567
}
568568
}
569569

570-
private Namespace getNamespace() {
570+
private Namespace currentNamespace() {
571571
Namespace namespace = namespaceSupplier.get();
572-
log().debug("Using namespace \"{}\".", namespace.getValue());
572+
_debug().log("Using namespace `%s`.", namespace);
573573
return namespace;
574574
}
575575

datastore/src/main/java/io/spine/server/storage/datastore/tenant/Namespace.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,6 @@ enum ConverterType {
295295
*/
296296
SINGLE_CUSTOM(NamespaceConverters.forCustomNamespace());
297297

298-
// This enum isn't intended to be serialized.
299-
@SuppressWarnings("NonSerializableFieldInSerializableClass")
300298
private final NamespaceConverter namespaceConverter;
301299

302300
static ConverterType forTenantId(TenantId tenantId) {

datastore/src/main/java/io/spine/server/storage/datastore/tenant/NamespaceIndex.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ final class NamespaceIndex implements TenantIndex {
5050
private static final Kind NAMESPACE_KIND = Kind.ofNamespace();
5151

5252
private final Set<Namespace> cache = new HashSet<>();
53-
5453
private final Object lock = new Object();
5554
private final NamespaceQuery namespaceQuery;
56-
5755
private final NsConverterFactory converterFactory;
58-
5956
private final boolean multitenant;
6057

58+
private boolean registered;
59+
6160
NamespaceIndex(Datastore datastore, boolean multitenant, NsConverterFactory converterFactory) {
6261
this(new DefaultNamespaceQuery(datastore),
6362
multitenant,
@@ -66,7 +65,8 @@ final class NamespaceIndex implements TenantIndex {
6665
}
6766

6867
NamespaceIndex(NamespaceQuery namespaceQuery,
69-
boolean multitenant, NsConverterFactory converterFactory) {
68+
boolean multitenant,
69+
NsConverterFactory converterFactory) {
7070
this.namespaceQuery = checkNotNull(namespaceQuery);
7171
this.converterFactory = converterFactory;
7272
this.multitenant = multitenant;
@@ -75,9 +75,13 @@ final class NamespaceIndex implements TenantIndex {
7575
@Override
7676
public void registerWith(BoundedContext context) {
7777
checkNotNull(context);
78+
registered = true;
79+
// Do nothing more, as this implementation does not rely on any `BoundedContext` properties.
80+
}
7881

79-
// Do nothing more, as this implementation does not rely on any {@code BoundedContext}
80-
// properties.
82+
@Override
83+
public boolean isRegistered() {
84+
return registered;
8185
}
8286

8387
/**

datastore/src/test/java/io/spine/server/storage/datastore/BigDataTester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void testBigDataOperations(RecordStorage<I> storage) {
109109
writeTime));
110110
}
111111

112-
log().debug("Writing took {} millis.", writeTime);
112+
_debug().log("Writing took %d millis.", writeTime);
113113

114114
try {
115115
Thread.sleep(1000);
@@ -130,7 +130,7 @@ public void testBigDataOperations(RecordStorage<I> storage) {
130130
readMillisLimit,
131131
readTime));
132132
}
133-
log().debug("Reading took {} millis.", readTime);
133+
_debug().log("Reading took %d millis.", readTime);
134134

135135
assertEquals(records.size(), size(readRecords), "Unexpected records count read.");
136136
}

datastore/src/test/java/io/spine/server/storage/datastore/DsAggregateStorageTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ void testLoadHistoryAfterSnapshotTriggerChange() {
254254
for (int i = 0; i < tasksCount; i++) {
255255
AggAddTask command = addTask(id);
256256
CommandEnvelope envelope = CommandEnvelope.of(factory.createCommand(command));
257-
ProjectId target = repository.dispatch(envelope);
258-
assertEquals(id, target);
257+
repository.dispatch(envelope);
259258
}
260259

261260
int minimalSnapshotTrigger = 1;

datastore/src/test/java/io/spine/server/storage/datastore/TestDatastoreStorageFactory.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@
2222

2323
import com.google.cloud.datastore.Datastore;
2424
import com.google.cloud.datastore.DatastoreOptions;
25+
import com.google.common.flogger.FluentLogger;
2526
import io.spine.annotation.Internal;
26-
import io.spine.logging.Logging;
2727
import io.spine.server.storage.datastore.given.TestDatastores;
2828
import io.spine.server.storage.datastore.type.DatastoreTypeRegistryFactory;
2929
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
30-
import org.slf4j.Logger;
31-
32-
import static java.lang.String.format;
3330

3431
/**
3532
* Creates storages based on the local Google {@link Datastore}.
3633
*/
3734
public class TestDatastoreStorageFactory extends DatastoreStorageFactory {
3835

36+
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
3937
private static @MonotonicNonNull TestDatastoreStorageFactory instance = null;
4038

4139
/**
@@ -53,7 +51,9 @@ public static synchronized TestDatastoreStorageFactory defaultInstance() {
5351
}
5452
return instance;
5553
} catch (Throwable e) {
56-
log().error("Failed to initialize local datastore factory", e);
54+
logger.atSevere()
55+
.withCause(e)
56+
.log("Failed to initialize local datastore factory.");
5757
throw new IllegalStateException(e);
5858
}
5959
}
@@ -111,13 +111,11 @@ public void clear() {
111111
try {
112112
datastore.dropAllTables();
113113
} catch (Throwable e) {
114-
log().error(format("Unable to drop tables in Datastore %s", datastore), e);
114+
logger.atSevere()
115+
.withCause(e)
116+
.log("Unable to drop tables in Datastore `%s`.", datastore);
115117
throw new IllegalStateException(e);
116118
}
117119
}
118120
}
119-
120-
private static Logger log() {
121-
return Logging.get(TestDatastoreStorageFactory.class);
122-
}
123121
}

datastore/src/test/java/io/spine/server/storage/datastore/TestDatastoreWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ private void dropTableConsistently(String table) {
141141
@SuppressWarnings("BusyWait") // allow Datastore to become consistent before reading.
142142
private void waitForConsistency() {
143143
if (!waitForConsistency) {
144-
log().debug("Wait for consistency is not required.");
144+
_debug().log("Wait for consistency is not required.");
145145
return;
146146
}
147-
log().debug("Waiting for data consistency to establish.");
147+
_debug().log("Waiting for data consistency to establish.");
148148

149149
for (int awaitCycle = 0; awaitCycle < CONSISTENCY_AWAIT_ITERATIONS; awaitCycle++) {
150150
try {
@@ -159,7 +159,7 @@ private void waitForConsistency() {
159159
* Deletes all records from the datastore.
160160
*/
161161
public void dropAllTables() {
162-
log().debug("Dropping all tables");
162+
_debug().log("Dropping all tables...");
163163
for (String kind : kindsCache) {
164164
dropTable(kind);
165165
}

datastore/src/test/java/io/spine/server/storage/datastore/given/TestDatastores.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private Ci() {
118118
ServiceAccountCredentials credentials = fromStream(bufferedStream);
119119
builder().setCredentials(credentials);
120120
} catch (@SuppressWarnings("OverlyBroadCatchBlock") IOException e) {
121-
log().warn("Cannot find the credentials file {}.", CREDENTIALS_FILE_PATH);
121+
_warn().log("Cannot find the credentials file `%s`.", CREDENTIALS_FILE_PATH);
122122
}
123123
}
124124
}

datastore/src/test/java/io/spine/server/storage/datastore/tenant/NamespaceIndexTest.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import io.spine.server.BoundedContext;
3333
import io.spine.server.BoundedContextBuilder;
3434
import io.spine.testing.TestValues;
35+
import org.junit.jupiter.api.AfterEach;
36+
import org.junit.jupiter.api.BeforeEach;
3537
import org.junit.jupiter.api.DisplayName;
3638
import org.junit.jupiter.api.Test;
3739

@@ -61,13 +63,27 @@ class NamespaceIndexTest {
6163

6264
private static final NsConverterFactory converterFactory = NsConverterFactory.defaults();
6365

66+
private NamespaceIndex namespaceIndex;
67+
private BoundedContext context;
68+
6469
private static TenantId newTenantId() {
6570
return TenantId
6671
.newBuilder()
6772
.setValue(TestValues.randomString())
6873
.vBuild();
6974
}
7075

76+
@BeforeEach
77+
void createIndex() {
78+
namespaceIndex = nsIndex();
79+
context = BoundedContextBuilder.assumingTests().build();
80+
}
81+
82+
@AfterEach
83+
void closeContext() throws Exception {
84+
context.close();
85+
}
86+
7187
@Test
7288
@DisplayName(NOT_ACCEPT_NULLS)
7389
void testNulls() {
@@ -77,17 +93,13 @@ void testNulls() {
7793
new NullPointerTester()
7894
.setDefault(Namespace.class, defaultNamespace)
7995
.setDefault(TenantId.class, tenantId)
80-
.setDefault(BoundedContext.class, BoundedContextBuilder.assumingTests()
81-
.build())
82-
.testInstanceMethods(nsIndex(),
83-
NullPointerTester.Visibility.PACKAGE);
96+
.setDefault(BoundedContext.class, context)
97+
.testInstanceMethods(namespaceIndex, NullPointerTester.Visibility.PACKAGE);
8498
}
8599

86100
@Test
87101
@DisplayName("store tenant IDs")
88102
void testStore() {
89-
NamespaceIndex namespaceIndex = nsIndex();
90-
91103
Set<TenantId> initialEmptySet = namespaceIndex.all();
92104
assertTrue(initialEmptySet.isEmpty());
93105

@@ -107,8 +119,6 @@ void testStore() {
107119
@Test
108120
@DisplayName("do nothing on close")
109121
void testClose() {
110-
NamespaceIndex namespaceIndex = nsIndex();
111-
112122
namespaceIndex.close();
113123
namespaceIndex.close();
114124
// No exception is thrown on the second call to #close() => no operation is performed
@@ -117,8 +127,6 @@ void testClose() {
117127
@Test
118128
@DisplayName("find existing namespaces")
119129
void testFindExisting() {
120-
NamespaceIndex namespaceIndex = nsIndex();
121-
122130
// Ensure no namespace has been kept
123131
Set<TenantId> initialEmptySet = namespaceIndex.all();
124132
assertTrue(initialEmptySet.isEmpty());
@@ -133,8 +141,6 @@ void testFindExisting() {
133141
@Test
134142
@DisplayName("not find non-existing namespaces")
135143
void testNotFindNonExisting() {
136-
NamespaceIndex namespaceIndex = nsIndex();
137-
138144
// Ensure no namespace has been kept
139145
Set<TenantId> initialEmptySet = namespaceIndex.all();
140146
assertTrue(initialEmptySet.isEmpty());
@@ -152,6 +158,14 @@ void testAsync() {
152158
NamespaceIndexTest::testSynchronizeAccessMethods);
153159
}
154160

161+
@Test
162+
@DisplayName("confirm registration")
163+
void registered() {
164+
namespaceIndex.registerWith(context);
165+
assertThat(namespaceIndex.isRegistered())
166+
.isTrue();
167+
}
168+
155169
@SuppressWarnings("OverlyLongMethod")
156170
private static void testSynchronizeAccessMethods() throws InterruptedException {
157171
// Initial data

0 commit comments

Comments
 (0)