Internal: Initialize Clock directly instead of with guice
The Clock interface, which basically allows testing in watcher to "time warp" is currently constructed using guice. This change constructs it using a protected method on XPackPlugin which can be overriden in tests. This allows removing the ClockModule. For now, the Clock still needs to be bound in guice, but this at least removes one guice construction, and shows how other things can be overriden for tests. Original commit: elastic/x-pack-elasticsearch@7addaea086
This commit is contained in:
parent
58058792a7
commit
6b5aea138f
|
@ -55,7 +55,8 @@ import org.elasticsearch.xpack.rest.action.RestXPackInfoAction;
|
||||||
import org.elasticsearch.xpack.rest.action.RestXPackUsageAction;
|
import org.elasticsearch.xpack.rest.action.RestXPackUsageAction;
|
||||||
import org.elasticsearch.xpack.security.Security;
|
import org.elasticsearch.xpack.security.Security;
|
||||||
import org.elasticsearch.xpack.security.authc.AuthenticationModule;
|
import org.elasticsearch.xpack.security.authc.AuthenticationModule;
|
||||||
import org.elasticsearch.xpack.support.clock.ClockModule;
|
import org.elasticsearch.xpack.support.clock.Clock;
|
||||||
|
import org.elasticsearch.xpack.support.clock.SystemClock;
|
||||||
import org.elasticsearch.xpack.watcher.Watcher;
|
import org.elasticsearch.xpack.watcher.Watcher;
|
||||||
|
|
||||||
public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
|
public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
|
||||||
|
@ -132,10 +133,15 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// overridable by tests
|
||||||
|
protected Clock getClock() {
|
||||||
|
return SystemClock.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Module> nodeModules() {
|
public Collection<Module> nodeModules() {
|
||||||
ArrayList<Module> modules = new ArrayList<>();
|
ArrayList<Module> modules = new ArrayList<>();
|
||||||
modules.add(new ClockModule());
|
modules.add(b -> b.bind(Clock.class).toInstance(getClock()));
|
||||||
modules.addAll(notification.nodeModules());
|
modules.addAll(notification.nodeModules());
|
||||||
modules.addAll(licensing.nodeModules());
|
modules.addAll(licensing.nodeModules());
|
||||||
modules.addAll(security.nodeModules());
|
modules.addAll(security.nodeModules());
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
||||||
* or more contributor license agreements. Licensed under the Elastic License;
|
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
|
||||||
*/
|
|
||||||
package org.elasticsearch.xpack.support.clock;
|
|
||||||
|
|
||||||
import org.elasticsearch.common.inject.AbstractModule;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ClockModule extends AbstractModule {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void configure() {
|
|
||||||
bind(Clock.class).toInstance(SystemClock.INSTANCE);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -5,43 +5,19 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack;
|
package org.elasticsearch.xpack;
|
||||||
|
|
||||||
import org.elasticsearch.common.inject.Module;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.xpack.support.clock.Clock;
|
import org.elasticsearch.xpack.support.clock.Clock;
|
||||||
import org.elasticsearch.xpack.support.clock.ClockMock;
|
import org.elasticsearch.xpack.support.clock.ClockMock;
|
||||||
import org.elasticsearch.xpack.support.clock.ClockModule;
|
|
||||||
import org.elasticsearch.xpack.watcher.test.TimeWarpedWatcher;
|
import org.elasticsearch.xpack.watcher.test.TimeWarpedWatcher;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class TimeWarpedXPackPlugin extends XPackPlugin {
|
public class TimeWarpedXPackPlugin extends XPackPlugin {
|
||||||
|
|
||||||
public TimeWarpedXPackPlugin(Settings settings) {
|
public TimeWarpedXPackPlugin(Settings settings) {
|
||||||
super(settings);
|
super(settings);
|
||||||
watcher = new TimeWarpedWatcher(settings);
|
watcher = new TimeWarpedWatcher(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Module> nodeModules() {
|
protected Clock getClock() {
|
||||||
List<Module> modules = new ArrayList<>(super.nodeModules());
|
return new ClockMock();
|
||||||
for (int i = 0; i < modules.size(); ++i) {
|
|
||||||
Module module = modules.get(i);
|
|
||||||
if (module instanceof ClockModule) {
|
|
||||||
// replacing the clock module so we'll be able
|
|
||||||
// to control time in tests
|
|
||||||
modules.set(i, new MockClockModule());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return modules;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class MockClockModule extends ClockModule {
|
|
||||||
@Override
|
|
||||||
protected void configure() {
|
|
||||||
bind(ClockMock.class).asEagerSingleton();
|
|
||||||
bind(Clock.class).to(ClockMock.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import org.joda.time.DateTimeZone;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* A clock that can be modified for testing.
|
||||||
*/
|
*/
|
||||||
public class ClockMock implements Clock {
|
public class ClockMock implements Clock {
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@ import org.elasticsearch.test.TestCluster;
|
||||||
import org.elasticsearch.test.store.MockFSIndexStore;
|
import org.elasticsearch.test.store.MockFSIndexStore;
|
||||||
import org.elasticsearch.test.transport.AssertingLocalTransport;
|
import org.elasticsearch.test.transport.AssertingLocalTransport;
|
||||||
import org.elasticsearch.test.transport.MockTransportService;
|
import org.elasticsearch.test.transport.MockTransportService;
|
||||||
|
import org.elasticsearch.xpack.support.clock.Clock;
|
||||||
import org.elasticsearch.xpack.watcher.WatcherLifeCycleService;
|
import org.elasticsearch.xpack.watcher.WatcherLifeCycleService;
|
||||||
import org.elasticsearch.xpack.watcher.WatcherService;
|
import org.elasticsearch.xpack.watcher.WatcherService;
|
||||||
import org.elasticsearch.xpack.watcher.WatcherState;
|
import org.elasticsearch.xpack.watcher.WatcherState;
|
||||||
|
@ -270,7 +271,7 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
|
||||||
|
|
||||||
private void setupTimeWarp() throws Exception {
|
private void setupTimeWarp() throws Exception {
|
||||||
if (timeWarped()) {
|
if (timeWarped()) {
|
||||||
timeWarp = new TimeWarp(getInstanceFromMaster(ScheduleTriggerEngineMock.class), getInstanceFromMaster(ClockMock.class));
|
timeWarp = new TimeWarp(getInstanceFromMaster(ScheduleTriggerEngineMock.class), (ClockMock)getInstanceFromMaster(Clock.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
|
public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean timeWarped() {
|
protected boolean timeWarped() {
|
||||||
|
|
Loading…
Reference in New Issue