Merge pull request elastic/elasticsearch#2754 from rjernst/clock_init_without_guice

Initialize Clock directly instead of with guice

Original commit: elastic/x-pack-elasticsearch@17ab249b95
This commit is contained in:
Ryan Ernst 2016-07-11 11:49:19 -07:00 committed by GitHub
commit 5c7279773c
5 changed files with 13 additions and 50 deletions

View File

@ -54,7 +54,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 {
@ -131,10 +132,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));
}
}