mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
10.37.2014 3461c8f39052a2767ca57ac2f63e85d8d7573d17
OPENDJ-1594 SDK should use forgerock-util's TimeService instead of its own TimeSource interface

Code review: Nicolas Capponi

TimeSource.java: REMOVED
Replaced with org.forgerock.util.time.TimeService.
1 files deleted
10 files modified
188 ■■■■■ changed files
opendj-core/src/main/java/com/forgerock/opendj/util/TimeSource.java 52 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/CachedConnectionPool.java 10 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/HeartBeatConnectionFactory.java 12 ●●●● patch | view | raw | blame | history
opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java 6 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java 12 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/HeartBeatConnectionFactoryTestCase.java 30 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java 10 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtilsTestCase.java 20 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/PartialDateAndTimeMatchingRuleTestCase.java 4 ●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/RelativeTimeGreaterThanMatchingRuleTest.java 16 ●●●●● patch | view | raw | blame | history
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/RelativeTimeLessThanMatchingRuleTest.java 16 ●●●●● patch | view | raw | blame | history
opendj-core/src/main/java/com/forgerock/opendj/util/TimeSource.java
File was deleted
opendj-core/src/main/java/org/forgerock/opendj/ldap/CachedConnectionPool.java
@@ -65,7 +65,7 @@
import org.forgerock.util.promise.SuccessHandler;
import com.forgerock.opendj.util.ReferenceCountedObject;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.forgerock.opendj.ldap.LdapException.*;
import static org.forgerock.util.promise.Promises.*;
@@ -590,7 +590,7 @@
                 * since we don't want to hold the lock too long.
                 */
                idleConnections = new LinkedList<Connection>();
                final long timeoutMillis = timeSource.currentTimeMillis() - idleTimeoutMillis;
                final long timeoutMillis = timeService.now() - idleTimeoutMillis;
                int nonCoreConnectionCount = currentPoolSize() - corePoolSize;
                for (QueueElement holder = queue.peek(); nonCoreConnectionCount > 0
                        && isTimedOutQueuedConnection(holder, timeoutMillis); holder = queue.peek()) {
@@ -693,7 +693,7 @@
     * This is package private in order to allow unit tests to inject fake time
     * stamps.
     */
    TimeSource timeSource = TimeSource.DEFAULT;
    TimeService timeService = TimeService.SYSTEM;
    private final Semaphore availableConnections;
    private final SuccessHandler<Connection> connectionSuccessHandler = new ConnectionSuccessHandler();
@@ -802,7 +802,7 @@
                } else if (hasWaitingConnections()) {
                    holder = queue.removeFirst();
                } else {
                    holder = new QueueElement(timeSource.currentTimeMillis(), getStackTraceIfDebugEnabled());
                    holder = new QueueElement(timeService.now(), getStackTraceIfDebugEnabled());
                    queue.add(holder);
                }
            }
@@ -890,7 +890,7 @@
                connectionPoolIsClosing = true;
                holder = null;
            } else {
                holder = new QueueElement(connection, timeSource.currentTimeMillis());
                holder = new QueueElement(connection, timeService.now());
                queue.add(holder);
                return;
            }
opendj-core/src/main/java/org/forgerock/opendj/ldap/HeartBeatConnectionFactory.java
@@ -71,7 +71,7 @@
import org.forgerock.util.promise.SuccessHandler;
import com.forgerock.opendj.util.ReferenceCountedObject;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.forgerock.opendj.ldap.LdapException.*;
import static org.forgerock.opendj.ldap.spi.LdapPromiseImpl.*;
@@ -160,7 +160,7 @@
         * Timestamp of last response received (any response, not just heart
         * beats).
         */
        private volatile long lastResponseTimestamp = timeSource.currentTimeMillis(); // Assume valid at creation.
        private volatile long lastResponseTimestamp = timeService.now(); // Assume valid at creation.
        private ConnectionImpl(final Connection connection) {
            this.connection = connection;
@@ -392,7 +392,7 @@
                 * only flag the connection as failed if no activity has been
                 * seen on the connection since the heart beat was sent.
                 */
                final long currentTimeMillis = timeSource.currentTimeMillis();
                final long currentTimeMillis = timeService.now();
                if (lastResponseTimestamp < (currentTimeMillis - timeoutMS)) {
                    logger.warn(LocalizableMessage.raw("No heartbeat detected for connection '%s'", connection));
                    handleConnectionError(false, newHeartBeatTimeoutError());
@@ -470,7 +470,7 @@
             * Only send the heart beat if the connection has been idle for some
             * time.
             */
            final long currentTimeMillis = timeSource.currentTimeMillis();
            final long currentTimeMillis = timeService.now();
            if (currentTimeMillis < (lastResponseTimestamp + minDelayMS)) {
                return false;
            }
@@ -546,7 +546,7 @@
        private <R> R timestamp(final R response) {
            if (!(response instanceof ConnectionException)) {
                lastResponseTimestamp = timeSource.currentTimeMillis();
                lastResponseTimestamp = timeService.now();
            }
            return response;
        }
@@ -748,7 +748,7 @@
     * This is package private in order to allow unit tests to inject fake time
     * stamps.
     */
    TimeSource timeSource = TimeSource.DEFAULT;
    TimeService timeService = TimeService.SYSTEM;
    /**
     * Scheduled task which checks that all heart beats have been received
opendj-core/src/main/java/org/forgerock/opendj/ldap/schema/TimeBasedMatchingRulesImpl.java
@@ -47,7 +47,7 @@
import org.forgerock.opendj.ldap.spi.Indexer;
import org.forgerock.opendj.ldap.spi.IndexingOptions;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static com.forgerock.opendj.ldap.CoreMessages.*;
import static com.forgerock.opendj.util.StaticUtils.*;
@@ -110,7 +110,7 @@
    private static abstract class TimeBasedMatchingRuleImpl extends AbstractMatchingRuleImpl {
        /** Unit tests can inject fake timestamps if necessary. */
        final TimeSource timeSource = TimeSource.DEFAULT;
        final TimeService timeService = TimeService.SYSTEM;
        /** {@inheritDoc} */
        @Override
@@ -228,7 +228,7 @@
            }
            long delta = (second + minute * 60 + hour * 3600 + day * 24 * 3600 + week * 7 * 24 * 3600) * 1000;
            long now = timeSource.currentTimeMillis();
            long now = timeService.now();
            return ByteString.valueOf(signed ? now - delta : now + delta);
        }
opendj-core/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
@@ -444,7 +444,7 @@
        assertThat(scheduler.isScheduled()).isTrue();
        // First populate the pool with idle connections at time 0.
        pool.timeSource = mockTimeSource(0);
        pool.timeService = mockTimeService(0);
        assertThat(pool.currentPoolSize()).isEqualTo(0);
        Connection c1 = pool.getConnection();
@@ -459,12 +459,12 @@
        assertThat(pool.currentPoolSize()).isEqualTo(4);
        // First purge at time 50 is no-op because no connections have expired.
        when(pool.timeSource.currentTimeMillis()).thenReturn(50L);
        when(pool.timeService.now()).thenReturn(50L);
        scheduler.runFirstTask();
        assertThat(pool.currentPoolSize()).isEqualTo(4);
        // Second purge at time 150 should remove 2 non-core connections.
        when(pool.timeSource.currentTimeMillis()).thenReturn(150L);
        when(pool.timeService.now()).thenReturn(150L);
        scheduler.runFirstTask();
        assertThat(pool.currentPoolSize()).isEqualTo(2);
@@ -474,7 +474,7 @@
        verify(pooledConnection4, times(0)).close();
        // Regrow the pool at time 200.
        when(pool.timeSource.currentTimeMillis()).thenReturn(200L);
        when(pool.timeService.now()).thenReturn(200L);
        Connection c5 = pool.getConnection(); // pooledConnection3
        Connection c6 = pool.getConnection(); // pooledConnection4
        Connection c7 = pool.getConnection(); // pooledConnection5
@@ -487,12 +487,12 @@
        assertThat(pool.currentPoolSize()).isEqualTo(4);
        // Third purge at time 250 should not remove any connections.
        when(pool.timeSource.currentTimeMillis()).thenReturn(250L);
        when(pool.timeService.now()).thenReturn(250L);
        scheduler.runFirstTask();
        assertThat(pool.currentPoolSize()).isEqualTo(4);
        // Fourth purge at time 350 should remove 2 non-core connections.
        when(pool.timeSource.currentTimeMillis()).thenReturn(350L);
        when(pool.timeService.now()).thenReturn(350L);
        scheduler.runFirstTask();
        assertThat(pool.currentPoolSize()).isEqualTo(2);
opendj-core/src/test/java/org/forgerock/opendj/ldap/HeartBeatConnectionFactoryTestCase.java
@@ -145,7 +145,7 @@
         */
        when(connection.searchAsync(any(SearchRequest.class), any(SearchResultHandler.class))).thenReturn(
            newSuccessfulLdapPromise(newResult(SUCCESS)));
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11000L);
        when(hbcf.timeService.now()).thenReturn(11000L);
        scheduler.runAllTasks(); // Send the heartbeat.
        // Capture the heartbeat search result handler.
@@ -241,14 +241,14 @@
        assertThat(hbc.isValid()).isTrue();
        // Invoke heartbeat after the connection is considered idle.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(6000L);
        when(hbcf.timeService.now()).thenReturn(6000L);
        scheduler.runAllTasks();
        verifyHeartBeatSent(connection, 2); // Heartbeat sent.
        assertThat(hbc.isValid()).isTrue();
        // Now force the heartbeat to fail.
        mockHeartBeatResponse(connection, listeners, ResultCode.CLIENT_SIDE_SERVER_DOWN);
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11000L);
        when(hbcf.timeService.now()).thenReturn(11000L);
        scheduler.runAllTasks();
        verifyHeartBeatSent(connection, 3);
        assertThat(hbc.isValid()).isFalse();
@@ -278,11 +278,11 @@
        // Now force the heartbeat to fail due to timeout.
        mockHeartBeatResponse(connection, listeners, null /* no response */);
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11000L);
        when(hbcf.timeService.now()).thenReturn(11000L);
        scheduler.runAllTasks(); // Send the heartbeat.
        verifyHeartBeatSent(connection, 2);
        assertThat(hbc.isValid()).isTrue(); // Not checked yet.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(12000L);
        when(hbcf.timeService.now()).thenReturn(12000L);
        scheduler.runAllTasks(); // Check for heartbeat.
        assertThat(hbc.isValid()).isFalse(); // Now invalid.
        assertThat(hbc.isClosed()).isFalse();
@@ -299,23 +299,23 @@
         * Send a bind request, trapping the bind call-back so that we can send
         * the response once we have attempted a heartbeat.
         */
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11000L);
        when(hbcf.timeService.now()).thenReturn(11000L);
        hbc.bindAsync(newSimpleBindRequest());
        verify(connection, times(1)).bindAsync(any(BindRequest.class), any(IntermediateResponseHandler.class));
        // Verify no heartbeat is sent because there is a bind in progress.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11001L);
        when(hbcf.timeService.now()).thenReturn(11001L);
        scheduler.runAllTasks(); // Invokes HBCF.ConnectionImpl.sendHeartBeat()
        verify(connection, times(1)).searchAsync(same(HEARTBEAT), any(SearchResultHandler.class));
        // Send fake bind response, releasing the heartbeat.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11099L);
        when(hbcf.timeService.now()).thenReturn(11099L);
        ((PromiseImpl) promise.getWrappedPromise()).handleResult(newResult(SUCCESS));
        // Check that bind response acts as heartbeat.
        assertThat(hbc.isValid()).isTrue();
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11100L);
        when(hbcf.timeService.now()).thenReturn(11100L);
        scheduler.runAllTasks(); // Invokes HBCF.ConnectionImpl.checkForHeartBeat()
        assertThat(hbc.isValid()).isTrue();
    }
@@ -327,18 +327,18 @@
        hbc = hbcf.getConnection();
        // Send another bind request which will timeout.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(20000L);
        when(hbcf.timeService.now()).thenReturn(20000L);
        hbc.bindAsync(newSimpleBindRequest());
        verify(connection, times(1)).bindAsync(any(BindRequest.class), any(IntermediateResponseHandler.class));
        // Verify no heartbeat is sent because there is a bind in progress.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(20001L);
        when(hbcf.timeService.now()).thenReturn(20001L);
        scheduler.runAllTasks(); // Invokes HBCF.ConnectionImpl.sendHeartBeat()
        verify(connection, times(1)).searchAsync(same(HEARTBEAT), any(SearchResultHandler.class));
        // Check that lack of bind response acts as heartbeat timeout.
        assertThat(hbc.isValid()).isTrue();
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(20100L);
        when(hbcf.timeService.now()).thenReturn(20100L);
        scheduler.runAllTasks(); // Invokes HBCF.ConnectionImpl.checkForHeartBeat()
        assertThat(hbc.isValid()).isFalse();
    }
@@ -358,7 +358,7 @@
         * Now attempt the heartbeat which should not happen because there is a
         * bind in progress.
         */
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(11000L);
        when(hbcf.timeService.now()).thenReturn(11000L);
        // Attempt to send the heartbeat.
        scheduler.runAllTasks();
        verify(connection, times(1)).searchAsync(same(HEARTBEAT), any(SearchResultHandler.class));
@@ -367,7 +367,7 @@
        ((PromiseImpl) promise.getWrappedPromise()).handleResult(newResult(SUCCESS));
        // Attempt to send a heartbeat again.
        when(hbcf.timeSource.currentTimeMillis()).thenReturn(16000L);
        when(hbcf.timeService.now()).thenReturn(16000L);
        // Attempt to send the heartbeat.
        scheduler.runAllTasks();
        verify(connection, times(2)).searchAsync(same(HEARTBEAT), any(SearchResultHandler.class));
@@ -404,7 +404,7 @@
        hbcf = new HeartBeatConnectionFactory(factory, 10000, 100, TimeUnit.MILLISECONDS, HEARTBEAT, scheduler);
        // Set initial time stamp.
        hbcf.timeSource = mockTimeSource(0);
        hbcf.timeService = mockTimeService(0);
    }
    private BindResultLdapPromiseImpl mockBindAsyncResponse() {
opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -41,7 +41,7 @@
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.fest.assertions.Fail.*;
import static org.forgerock.opendj.ldap.spi.LdapPromises.*;
@@ -216,7 +216,7 @@
    }
    /**
     * Returns a mock {@link TimeSource} which can be used for injecting fake
     * Returns a mock {@link TimeService} which can be used for injecting fake
     * time stamps into components.
     *
     * @param times
@@ -224,9 +224,9 @@
     *            time source.
     * @return The mock time source.
     */
    public static TimeSource mockTimeSource(final long... times) {
        final TimeSource mock = mock(TimeSource.class);
        OngoingStubbing<Long> stubbing = when(mock.currentTimeMillis());
    public static TimeService mockTimeService(final long... times) {
        final TimeService mock = mock(TimeService.class);
        OngoingStubbing<Long> stubbing = when(mock.now());
        for (long t : times) {
            stubbing = stubbing.thenReturn(t);
        }
opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtilsTestCase.java
@@ -26,11 +26,11 @@
package org.forgerock.opendj.ldap;
import static org.fest.assertions.Assertions.assertThat;
import static org.forgerock.opendj.ldap.TestCaseUtils.mockTimeSource;
import static org.forgerock.opendj.ldap.TestCaseUtils.mockTimeService;
import org.testng.annotations.Test;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
@SuppressWarnings("javadoc")
public class TestCaseUtilsTestCase extends SdkTestCase {
@@ -40,14 +40,14 @@
     */
    @Test
    public void testMockTimeSource() {
        final TimeSource mock1 = mockTimeSource(10);
        assertThat(mock1.currentTimeMillis()).isEqualTo(10);
        assertThat(mock1.currentTimeMillis()).isEqualTo(10);
        final TimeService mock1 = mockTimeService(10);
        assertThat(mock1.now()).isEqualTo(10);
        assertThat(mock1.now()).isEqualTo(10);
        final TimeSource mock2 = mockTimeSource(10, 20, 30);
        assertThat(mock2.currentTimeMillis()).isEqualTo(10);
        assertThat(mock2.currentTimeMillis()).isEqualTo(20);
        assertThat(mock2.currentTimeMillis()).isEqualTo(30);
        assertThat(mock2.currentTimeMillis()).isEqualTo(30);
        final TimeService mock2 = mockTimeService(10, 20, 30);
        assertThat(mock2.now()).isEqualTo(10);
        assertThat(mock2.now()).isEqualTo(20);
        assertThat(mock2.now()).isEqualTo(30);
        assertThat(mock2.now()).isEqualTo(30);
    }
}
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/PartialDateAndTimeMatchingRuleTestCase.java
@@ -36,7 +36,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.fest.assertions.Assertions.*;
import static org.forgerock.opendj.ldap.schema.AbstractSubstringMatchingRuleImplTest.*;
@@ -112,7 +112,7 @@
    @DataProvider(name = "matchingrules")
    public Object[][] createMatchingRuleTest() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(TimeSource.DEFAULT.currentTimeMillis());
        calendar.setTimeInMillis(TimeService.SYSTEM.now());
        final Date nowDate = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        final Date oneMonthAheadDate = calendar.getTime();
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/RelativeTimeGreaterThanMatchingRuleTest.java
@@ -36,7 +36,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.fest.assertions.Assertions.*;
import static org.forgerock.opendj.ldap.schema.AbstractSubstringMatchingRuleImplTest.*;
@@ -45,9 +45,7 @@
@Test
public class RelativeTimeGreaterThanMatchingRuleTest extends MatchingRuleTest {
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    @DataProvider(name = "matchingRuleInvalidAttributeValues")
    public Object[][] createMatchingRuleInvalidAttributeValues() {
@@ -79,14 +77,12 @@
        };
    }
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    @DataProvider(name = "matchingrules")
    public Object[][] createMatchingRuleTest() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(TimeSource.DEFAULT.currentTimeMillis());
        calendar.setTimeInMillis(TimeService.SYSTEM.now());
        final Date nowDate = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        final Date oneMonthAheadDate = calendar.getTime();
@@ -114,9 +110,7 @@
        };
    }
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    protected MatchingRule getRule() {
        // Note that oid and names are not used by the test (ie, they could be any value, test should pass anyway)
opendj-core/src/test/java/org/forgerock/opendj/ldap/schema/RelativeTimeLessThanMatchingRuleTest.java
@@ -36,7 +36,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.forgerock.opendj.util.TimeSource;
import org.forgerock.util.time.TimeService;
import static org.fest.assertions.Assertions.*;
import static org.forgerock.opendj.ldap.schema.AbstractSubstringMatchingRuleImplTest.*;
@@ -45,9 +45,7 @@
@Test
public class RelativeTimeLessThanMatchingRuleTest extends MatchingRuleTest {
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    @DataProvider(name = "matchingRuleInvalidAttributeValues")
    public Object[][] createMatchingRuleInvalidAttributeValues() {
@@ -79,14 +77,12 @@
        };
    }
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    @DataProvider(name = "matchingrules")
    public Object[][] createMatchingRuleTest() {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(TimeSource.DEFAULT.currentTimeMillis());
        calendar.setTimeInMillis(TimeService.SYSTEM.now());
        final Date nowDate = calendar.getTime();
        calendar.add(Calendar.MONTH, 1);
        final Date oneMonthAheadDate = calendar.getTime();
@@ -115,9 +111,7 @@
        };
    }
    /**
     * {@inheritDoc}
     */
    /** {@inheritDoc} */
    @Override
    protected MatchingRule getRule() {
        // Note that oid and names are not used by the test (ie, they could be any value, test should pass anyway)