Skip to content

Commit eaa5bc8

Browse files
committed
stop the application between tests to make sure that there are no active transactions when we truncate the tables
1 parent 42d1db5 commit eaa5bc8

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

commafeed-server/src/main/java/com/commafeed/CommaFeedApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class CommaFeedApplication {
2222
private final CommaFeedConfiguration config;
2323

2424
public void start(@Observes StartupEvent ev) {
25+
log.info("starting up...");
26+
2527
PasswordConstraintValidator.setMinimumPasswordLength(config.users().minimumPasswordLength());
2628

2729
feedRefreshEngine.start();

commafeed-server/src/main/java/com/commafeed/backend/feed/FeedRefreshEngine.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public class FeedRefreshEngine {
4242

4343
private final BlockingDeque<Feed> queue;
4444

45-
private final ExecutorService feedProcessingLoopExecutor;
46-
private final ExecutorService refillLoopExecutor;
47-
private final ExecutorService refillExecutor;
48-
private final ThreadPoolExecutor workerExecutor;
49-
private final ThreadPoolExecutor databaseUpdaterExecutor;
50-
private final ThreadPoolExecutor notifierExecutor;
45+
private ExecutorService feedProcessingLoopExecutor;
46+
private ExecutorService refillLoopExecutor;
47+
private ThreadPoolExecutor refillExecutor;
48+
private ThreadPoolExecutor workerExecutor;
49+
private ThreadPoolExecutor databaseUpdaterExecutor;
50+
private ThreadPoolExecutor notifierExecutor;
5151

5252
public FeedRefreshEngine(UnitOfWork unitOfWork, FeedDAO feedDAO, FeedRefreshWorker worker, FeedRefreshUpdater updater,
5353
FeedUpdateNotifier notifier, CommaFeedConfiguration config, MetricRegistry metrics) {
@@ -61,22 +61,26 @@ public FeedRefreshEngine(UnitOfWork unitOfWork, FeedDAO feedDAO, FeedRefreshWork
6161

6262
this.queue = new LinkedBlockingDeque<>();
6363

64+
metrics.register(MetricRegistry.name(getClass(), "queue", "size"), (Gauge<Integer>) queue::size);
65+
metrics.register(MetricRegistry.name(getClass(), "worker", "active"), (Gauge<Integer>) () -> workerExecutor.getActiveCount());
66+
metrics.register(MetricRegistry.name(getClass(), "updater", "active"),
67+
(Gauge<Integer>) () -> databaseUpdaterExecutor.getActiveCount());
68+
metrics.register(MetricRegistry.name(getClass(), "notifier", "active"), (Gauge<Integer>) () -> notifierExecutor.getActiveCount());
69+
metrics.register(MetricRegistry.name(getClass(), "notifier", "queue"), (Gauge<Integer>) () -> notifierExecutor.getQueue().size());
70+
}
71+
72+
private void createExecutors() {
6473
this.feedProcessingLoopExecutor = Executors.newSingleThreadExecutor();
6574
this.refillLoopExecutor = Executors.newSingleThreadExecutor();
6675
this.refillExecutor = newDiscardingSingleThreadExecutorService();
6776
this.workerExecutor = newBlockingExecutorService(config.feedRefresh().httpThreads());
6877
this.databaseUpdaterExecutor = newBlockingExecutorService(config.feedRefresh().databaseThreads());
6978
this.notifierExecutor = newDiscardingExecutorService(config.pushNotifications().threads(),
7079
config.pushNotifications().queueCapacity());
71-
72-
metrics.register(MetricRegistry.name(getClass(), "queue", "size"), (Gauge<Integer>) queue::size);
73-
metrics.register(MetricRegistry.name(getClass(), "worker", "active"), (Gauge<Integer>) workerExecutor::getActiveCount);
74-
metrics.register(MetricRegistry.name(getClass(), "updater", "active"), (Gauge<Integer>) databaseUpdaterExecutor::getActiveCount);
75-
metrics.register(MetricRegistry.name(getClass(), "notifier", "active"), (Gauge<Integer>) notifierExecutor::getActiveCount);
76-
metrics.register(MetricRegistry.name(getClass(), "notifier", "queue"), (Gauge<Integer>) () -> notifierExecutor.getQueue().size());
7780
}
7881

7982
public void start() {
83+
createExecutors();
8084
startFeedProcessingLoop();
8185
startRefillLoop();
8286
}
@@ -204,6 +208,8 @@ public void stop() {
204208
MoreExecutors.shutdownAndAwaitTermination(this.workerExecutor, config.shutdownTimeout());
205209
MoreExecutors.shutdownAndAwaitTermination(this.databaseUpdaterExecutor, config.shutdownTimeout());
206210
MoreExecutors.shutdownAndAwaitTermination(this.notifierExecutor, config.shutdownTimeout());
211+
212+
queue.clear();
207213
}
208214

209215
/**

commafeed-server/src/main/java/com/commafeed/backend/task/ScheduledTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public void register(ScheduledExecutorService executor) {
2323
log.error(e.getMessage(), e);
2424
}
2525
};
26-
log.info("registering task {} for execution every {} {}, starting in {} {}", getClass().getSimpleName(), getPeriod(), getTimeUnit(),
27-
getInitialDelay(), getTimeUnit());
26+
log.debug("registering task {} for execution every {} {}, starting in {} {}", getClass().getSimpleName(), getPeriod(),
27+
getTimeUnit(), getInitialDelay(), getTimeUnit());
2828
executor.scheduleWithFixedDelay(runnable, getInitialDelay(), getPeriod(), getTimeUnit());
2929
}
3030
}

commafeed-server/src/main/java/com/commafeed/backend/task/TaskScheduler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ public class TaskScheduler {
1818

1919
private final List<ScheduledTask> tasks;
2020
private final CommaFeedConfiguration config;
21-
private final ScheduledExecutorService executor;
21+
22+
private ScheduledExecutorService executor;
2223

2324
public TaskScheduler(@All List<ScheduledTask> tasks, CommaFeedConfiguration config) {
2425
this.tasks = tasks;
2526
this.config = config;
26-
this.executor = Executors.newScheduledThreadPool(tasks.size());
2727
}
2828

2929
public void start() {
30-
tasks.forEach(task -> task.register(executor));
30+
this.executor = Executors.newScheduledThreadPool(tasks.size());
31+
this.tasks.forEach(task -> task.register(executor));
3132
}
3233

3334
public void stop() {

commafeed-server/src/test/java/com/commafeed/DatabaseReset.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.hibernate.Session;
77
import org.kohsuke.MetaInfServices;
88

9+
import io.quarkus.runtime.ShutdownEvent;
10+
import io.quarkus.runtime.StartupEvent;
911
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
1012
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
1113

@@ -17,12 +19,17 @@ public class DatabaseReset implements QuarkusTestBeforeEachCallback {
1719

1820
@Override
1921
public void beforeEach(QuarkusTestMethodContext context) {
20-
CDI.current()
21-
.select(EntityManager.class)
22-
.get()
23-
.unwrap(Session.class)
24-
.getSessionFactory()
25-
.getSchemaManager()
26-
.truncateMappedObjects();
22+
// stop the application to make sure that there are no active transactions when we truncate the tables
23+
getBean(CommaFeedApplication.class).stop(new ShutdownEvent());
24+
25+
// truncate all tables so that we have a clean slate for the next test
26+
getBean(EntityManager.class).unwrap(Session.class).getSessionFactory().getSchemaManager().truncateMappedObjects();
27+
28+
// restart the application
29+
getBean(CommaFeedApplication.class).start(new StartupEvent());
30+
}
31+
32+
private static <T> T getBean(Class<T> clazz) {
33+
return CDI.current().select(clazz).get();
2734
}
2835
}

0 commit comments

Comments
 (0)