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:
Ryan Ernst 2016-07-08 08:54:23 -07:00
parent 58058792a7
commit 6b5aea138f
6 changed files with 14 additions and 51 deletions

View File

@ -55,7 +55,8 @@ import org.elasticsearch.xpack.rest.action.RestXPackInfoAction;
import org.elasticsearch.xpack.rest.action.RestXPackUsageAction;
import org.elasticsearch.xpack.security.Security;
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;
public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
@ -132,10 +133,15 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin {
return Collections.emptyList();
}
// overridable by tests
protected Clock getClock() {
return SystemClock.INSTANCE;
}
@Override
public Collection<Module> nodeModules() {
ArrayList<Module> modules = new ArrayList<>();
modules.add(new ClockModule());
modules.add(b -> b.bind(Clock.class).toInstance(getClock()));
modules.addAll(notification.nodeModules());
modules.addAll(licensing.nodeModules());
modules.addAll(security.nodeModules());

View File

@ -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);
}
}

View File

@ -5,43 +5,19 @@
*/
package org.elasticsearch.xpack;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.support.clock.ClockMock;
import org.elasticsearch.xpack.support.clock.ClockModule;
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 TimeWarpedXPackPlugin(Settings settings) {
super(settings);
watcher = new TimeWarpedWatcher(settings);
}
@Override
public Collection<Module> nodeModules() {
List<Module> modules = new ArrayList<>(super.nodeModules());
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);
}
protected Clock getClock() {
return new ClockMock();
}
}

View File

@ -12,7 +12,7 @@ import org.joda.time.DateTimeZone;
import java.util.concurrent.TimeUnit;
/**
*
* A clock that can be modified for testing.
*/
public class ClockMock implements Clock {

View File

@ -39,6 +39,7 @@ import org.elasticsearch.test.TestCluster;
import org.elasticsearch.test.store.MockFSIndexStore;
import org.elasticsearch.test.transport.AssertingLocalTransport;
import org.elasticsearch.test.transport.MockTransportService;
import org.elasticsearch.xpack.support.clock.Clock;
import org.elasticsearch.xpack.watcher.WatcherLifeCycleService;
import org.elasticsearch.xpack.watcher.WatcherService;
import org.elasticsearch.xpack.watcher.WatcherState;
@ -270,7 +271,7 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase
private void setupTimeWarp() throws Exception {
if (timeWarped()) {
timeWarp = new TimeWarp(getInstanceFromMaster(ScheduleTriggerEngineMock.class), getInstanceFromMaster(ClockMock.class));
timeWarp = new TimeWarp(getInstanceFromMaster(ScheduleTriggerEngineMock.class), (ClockMock)getInstanceFromMaster(Clock.class));
}
}

View File

@ -57,7 +57,7 @@ import static org.hamcrest.Matchers.notNullValue;
/**
*/
public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
public class BasicWatcherTests extends AbstractWatcherIntegrationTestCase {
@Override
protected boolean timeWarped() {