Skip to content

Commit

Permalink
Remove deprecated ExtendedServer.setClock() migrate to DatabaseBuilde…
Browse files Browse the repository at this point in the history
…r.clock() (#3566)

So we can no longer dynamically change the Clock being used and instead for testing purposes need to create the Database with a test clock instance as needed.

Refer to the ExtendedServerTest for an example
  • Loading branch information
rbygrave authored Feb 26, 2025
1 parent cc48dce commit e18b59a
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 148 deletions.
12 changes: 0 additions & 12 deletions ebean-api/src/main/java/io/ebean/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,6 @@ static DatabaseBuilder builder() {
*/
<T> T reference(Class<T> beanType, Object id);

/**
* Return the extended API for Database.
* <p>
* The extended API has the options for executing queries that take an explicit
* transaction as an argument.
* <p>
* Typically, we only need to use the extended API when we do NOT want to use the
* usual ThreadLocal based mechanism to obtain the current transaction but instead
* supply the transaction explicitly.
*/
ExtendedServer extended();

/**
* Either Insert or Update the bean depending on its state.
* <p>
Expand Down
21 changes: 0 additions & 21 deletions ebean-api/src/main/java/io/ebean/ExtendedServer.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* Service Provider extension to EbeanServer.
*/
public interface SpiEbeanServer extends SpiServer, ExtendedServer, BeanCollectionLoader {
public interface SpiEbeanServer extends SpiServer, BeanCollectionLoader {

/**
* Return the NOW time from the Clock.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
private final QueryPlanManager queryPlanManager;
private final ExtraMetrics extraMetrics;
private final DataTimeZone dataTimeZone;
private final ClockService clockService;
private final Clock clock;
private final CallOriginFactory callStackFactory;
private final Persister persister;
private final OrmQueryEngine queryEngine;
Expand Down Expand Up @@ -151,7 +151,7 @@ public DefaultServer(InternalConfiguration config, ServerCacheManager cache) {
this.beanLoader = new DefaultBeanLoader(this);
this.jsonContext = config.createJsonContext(this);
this.dataTimeZone = config.getDataTimeZone();
this.clockService = config.getClockService();
this.clock = config.clock();

DocStoreIntegration docStoreComponents = config.createDocStoreIntegration(this);
this.transactionManager = config.createTransactionManager(this, docStoreComponents.updateProcessor());
Expand Down Expand Up @@ -413,19 +413,9 @@ public String name() {
return serverName;
}

@Override
public ExtendedServer extended() {
return this;
}

@Override
public long clockNow() {
return clockService.nowMillis();
}

@Override
public void setClock(Clock clock) {
this.clockService.setClock(clock);
return clock.millis();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.ebeanservice.docstore.api.DocStoreUpdateProcessor;
import io.ebeanservice.docstore.none.NoneDocStoreFactory;

import java.time.Clock;
import java.util.*;

import static java.lang.System.Logger.Level.*;
Expand All @@ -75,7 +76,7 @@ public final class InternalConfiguration {
private final DeployInherit deployInherit;
private final TypeManager typeManager;
private final DtoBeanManager dtoBeanManager;
private final ClockService clockService;
private final Clock clock;
private final DataTimeZone dataTimeZone;
private final Binder binder;
private final DeployCreateProperties deployCreateProperties;
Expand Down Expand Up @@ -103,7 +104,7 @@ public final class InternalConfiguration {
this.online = online;
this.config = config;
this.jacksonCorePresent = config.getClassLoadConfig().isJacksonCorePresent();
this.clockService = new ClockService(config.settings().getClock());
this.clock = config.settings().getClock();
this.tableModState = new TableModState();
this.logManager = initLogManager();
this.docStoreFactory = initDocStoreFactory(service(DocStoreFactory.class));
Expand Down Expand Up @@ -189,8 +190,8 @@ public DocStoreFactory getDocStoreFactory() {
return docStoreFactory;
}

ClockService getClockService() {
return clockService;
Clock clock() {
return clock;
}

public ExtraMetrics getExtraMetrics() {
Expand Down Expand Up @@ -391,7 +392,7 @@ TransactionManager createTransactionManager(SpiServer server, DocStoreUpdateProc
TransactionManagerOptions options =
new TransactionManagerOptions(server, notifyL2CacheInForeground, config, scopeManager, clusterManager, backgroundExecutor,
indexUpdateProcessor, beanDescriptorManager, dataSource(), profileHandler(), logManager,
tableModState, cacheNotify, clockService);
tableModState, cacheNotify);

if (config.isDocStoreOnly()) {
return new DocStoreTransactionManager(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.ebeaninternal.api.SpiLogManager;
import io.ebeaninternal.api.SpiProfileHandler;
import io.ebeaninternal.server.cluster.ClusterManager;
import io.ebeaninternal.server.core.ClockService;
import io.ebeaninternal.server.deploy.BeanDescriptorManager;
import io.ebeanservice.docstore.api.DocStoreUpdateProcessor;

Expand All @@ -30,13 +29,11 @@ public final class TransactionManagerOptions {
final SpiLogManager logManager;
final TableModState tableModState;
final ServerCacheNotify cacheNotify;
final ClockService clockService;


public TransactionManagerOptions(SpiServer server, boolean notifyL2CacheInForeground, DatabaseBuilder.Settings config, TransactionScopeManager scopeManager,
ClusterManager clusterManager, BackgroundExecutor backgroundExecutor, DocStoreUpdateProcessor docStoreUpdateProcessor,
BeanDescriptorManager descMgr, DataSourceSupplier dataSourceSupplier, SpiProfileHandler profileHandler,
SpiLogManager logManager, TableModState tableModState, ServerCacheNotify cacheNotify, ClockService clockService) {
SpiLogManager logManager, TableModState tableModState, ServerCacheNotify cacheNotify) {
this.server = server;
this.notifyL2CacheInForeground = notifyL2CacheInForeground;
this.config = config;
Expand All @@ -50,7 +47,6 @@ public TransactionManagerOptions(SpiServer server, boolean notifyL2CacheInForegr
this.logManager = logManager;
this.tableModState = tableModState;
this.cacheNotify = cacheNotify;
this.clockService = clockService;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.ebean.xtest.base;

import io.ebean.*;
import io.ebean.annotation.Platform;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.AfterEach;
import io.ebean.xtest.ForPlatform;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.EBasicVer;
import org.tests.model.basic.ResetBasicData;

import java.time.Clock;
Expand All @@ -14,18 +16,10 @@

import static org.assertj.core.api.Assertions.assertThat;

public class ExtendedServerTest extends BaseTestCase {

@AfterEach
public void cleanup() {
DB.getDefault()
.extended()
.setClock(Clock.systemUTC());
}
class ExtendedServerTest extends BaseTestCase {

@Test
public void findList() {

void findList() {
ResetBasicData.reset();

Database server = DB.getDefault();
Expand All @@ -51,47 +45,57 @@ public void findList() {
}
}

@ForPlatform(Platform.H2)
@Test
public void mockClock() {

Database database = DB.getDefault();
final Instant snapshot = Instant.now();
Instant backedSnapshot = snapshot.minus(1, ChronoUnit.DAYS);
void fixedClock() {
final Instant now = Instant.now();
Instant backedSnapshot = now.minus(1, ChronoUnit.DAYS);
Clock snapshotClock = Clock.fixed(backedSnapshot, Clock.systemUTC().getZone());

database.extended().setClock(snapshotClock);

ResetBasicData.reset();

int count = database
.find(Customer.class)
Database db = Database.builder()
.name("db")
.loadFromProperties()
.clock(snapshotClock)
.addClass(EBasicVer.class)
.ddlExtra(false)
.name("fixed-clock-db")
.register(false)
.defaultDatabase(false)
.build();

EBasicVer e0 = new EBasicVer("CheckClock");
db.save(e0);

EBasicVer found = db.find(EBasicVer.class, e0.getId());
assertThat(found).isNotNull();

int count = db
.find(EBasicVer.class)
.where()
.gt("cretime", snapshot)
.gt("lastUpdate", now)
.findCount();
assertThat(count).isEqualTo(0);

int count2 = database
.find(Customer.class)
int count2 = db
.find(EBasicVer.class)
.where()
.ge("cretime", backedSnapshot)
.ge("lastUpdate", snapshotClock.instant().minusSeconds(60))
.findCount();
assertThat(count2).isGreaterThan(0);
assertThat(count2).isEqualTo(1);

int count3 = database
.find(Customer.class)
int count3 = db
.find(EBasicVer.class)
.where()
.gt("updtime", snapshot)
.gt("lastUpdate", now)
.findCount();
assertThat(count3).isEqualTo(0);

int count4 = database
.find(Customer.class)
int count4 = db
.find(EBasicVer.class)
.where()
.ge("updtime", backedSnapshot)
.ge("lastUpdate", backedSnapshot.minus(2, ChronoUnit.DAYS))
.findCount();
assertThat(count4).isGreaterThan(0);


assertThat(count4).isEqualTo(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,11 @@ public TDSpiEbeanServer(String name) {
this.name = name;
}

@Override
public ExtendedServer extended() {
return this;
}

@Override
public long clockNow() {
return System.currentTimeMillis();
}

@Override
public void setClock(Clock clock) {
}

@Override
public boolean isDisableL2Cache() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,6 @@ public <T> T reference(Class<T> beanType, Object id) {
return null;
}

@Override
public ExtendedServer extended() {
return null;
}

@Override
public void save(Object bean) throws OptimisticLockException {

Expand Down
6 changes: 3 additions & 3 deletions ebean-test/src/test/java/org/tests/basic/TestIUDVanilla.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.tests.model.basic.UTMaster;

import jakarta.persistence.OptimisticLockException;
import java.sql.Timestamp;
import java.time.Instant;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -24,12 +24,12 @@ public void test() {
assertNotNull(e0.getId());
assertNotNull(e0.getLastUpdate());

Timestamp lastUpdate0 = e0.getLastUpdate();
Instant lastUpdate0 = e0.getLastUpdate();

e0.setName("modified");
e0.save();

Timestamp lastUpdate1 = e0.getLastUpdate();
Instant lastUpdate1 = e0.getLastUpdate();
assertNotNull(lastUpdate1);
assertNotSame(lastUpdate0, lastUpdate1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.tests.model.basic.*;
import org.tests.query.cache.Acl;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -324,7 +324,7 @@ public void testFlushOnGetProperty() {
server.save(b2, txn);

// flush here
Timestamp lastUpdate = b1.getLastUpdate();
Instant lastUpdate = b1.getLastUpdate();
assertNotNull(lastUpdate);

EBasicVer b3 = new EBasicVer("b3");
Expand Down
Loading

0 comments on commit e18b59a

Please sign in to comment.