rezataroosheh commented on code in PR #13639: URL: https://github.com/apache/ignite/pull/13639#discussion_r4352793122
########## modules/core/src/test/java/org/apache/ignite/internal/util/IgniteClockTimerTest.java: ########## @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.util; + +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; + +/** + * Tests that {@link U#currentTimeMillis()} keeps being updated when the test clock is used. + */ +public class IgniteClockTimerTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** + * Checks that the internal clock timer is not started while the test clock is used + * and time is updated when no nodes are running. + * + * @throws Exception If failed. + */ + @Test + public void testInternalClockNotStartedWithTestClock() throws Exception { + assertFalse(internalClockRunning()); + + startGrid(0); + + assertFalse(internalClockRunning()); + + stopAllGrids(); + + assertClockAdvances(); + } + + /** + * Emulates a node left running by a test that does not extend {@link GridCommonAbstractTest} + * before the test clock was started (see IGNITE-29084): such a node must neither override the mocked time + * while it is alive nor leave {@link U#currentTimeMillis()} frozen after it is stopped. + * + * @throws Exception If failed. + */ + @Test + public void testLeakedNodeDoesNotFreezeClock() throws Exception { + assertTrue(G.allGrids().isEmpty()); + + synchronized (IgniteUtils.mux) { + // As if the test clock has not been started yet (no nodes are running, so only the test clock is counted). + IgniteUtils.gridCnt = 0; + } + + boolean testClockStarted = false; + + try { + // Node "leaked" by a previous test: it starts the internal clock timer. + startGrid(0); + + assertTrue(internalClockRunning()); + + // GridAbstractTest creates the test clock on class initialization regardless of running nodes. + new GridTestClockTimer(); + + testClockStarted = true; + + assertFalse(internalClockRunning()); + + // The mocked time must not be overridden by the internal clock timer of the leaked node. + long mockedTime = U.currentTimeMillis(); + + GridTestClockTimer.timeSupplier(() -> mockedTime); + + try { + // Several periods of the internal clock timer. + doSleep(100); + + assertEquals(mockedTime, U.currentTimeMillis()); + } + finally { + GridTestClockTimer.timeSupplier(GridTestClockTimer.DFLT_TIME_SUPPLIER); + } + + // Stopping the leaked node must not affect the time updates. + stopAllGrids(); + + assertClockAdvances(); + + // Internal clock timer must not be started again. + startGrid(0); + + assertFalse(internalClockRunning()); + } + finally { + // Restores the test clock counter and stops the internal clock timer if the test failed in between. Review Comment: @zstan Thanks for the repro! I ran IgniteCalciteTestSuite2 with time_repro.patch: - master: PlannerTimeoutTest fails in init(): expected:<1> but was:<2>. - this PR: PlannerTimeoutTest passes. On master, the failure comes from the gridCnt check, not from the clock. When PlannerTimeoutTest starts, the 2 leaked ScriptTestSuite nodes are still running, so gridCnt is 2, and their ignite-clock keeps time moving. The freeze comes one step later: PlannerTimeoutTest's @AfterAll calls stopAllGrids(), which stops all nodes in the JVM, including the leaked ones. On master, gridCnt then drops to 0 and ignite-clock stops. The test clock was never started, so subsequent tests see a frozen U.currentTimeMillis(). That's the case IgniteClockTimerTest.testLeakedNodeDoesNotFreezeClock covers. You're right that you should check gridCnt. The old check assertEquals(1, gridCnt) assumes no other nodes are running, so a forgotten node breaks it even when the counter is correct. I restored it in a form that always holds: G.allGrids().size() + 1 (each running node's slot plus the test clock's slot). With your repro: - this PR: gridCnt = 3, nodes + 1 = 3, passes. - master: gridCnt = 2, nodes + 1 = 3, fails with expected:<3> but was:<2>, because the test clock's slot is missing. So the check now catches the real problem instead of the leaked node itself. Pushed in 9cccac94c7. The other failures in the suite (TpchQueryPlannerTest plan diffs and the LIMIT 1.5 script in ScriptTestSuite) are the same on master and on this branch, so they're unrelated. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
