tkalkirill commented on code in PR #800: URL: https://github.com/apache/ignite-3/pull/800#discussion_r870114155
########## modules/core/src/test/java/org/apache/ignite/internal/util/worker/IgniteWorkerTest.java: ########## @@ -0,0 +1,464 @@ +/* + * 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.worker; + +import static org.apache.ignite.internal.testframework.IgniteTestUtils.runAsync; +import static org.apache.ignite.internal.util.FastTimestamps.coarseCurrentTimeMillis; +import static org.apache.ignite.internal.util.worker.IgniteWorkerTest.TestWorkerListener.ON_IDLE; +import static org.apache.ignite.internal.util.worker.IgniteWorkerTest.TestWorkerListener.ON_STARTED; +import static org.apache.ignite.internal.util.worker.IgniteWorkerTest.TestWorkerListener.ON_STOPPED; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.ignite.lang.IgniteLogger; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.Test; + +/** + * For {@link IgniteWorker} testing. + */ +public class IgniteWorkerTest { + static final String CLEANUP = "cleanup"; + + private final IgniteLogger log = IgniteLogger.forClass(IgniteWorkerTest.class); + + @Test + void testNewIgniteWorker() { + IgniteWorker worker = new NoopWorker(log, null); + + assertEquals("testNode", worker.igniteInstanceName()); + assertEquals("testWorker", worker.name()); + + assertEquals(0, worker.heartbeat()); + + assertFalse(worker.isCancelled()); + assertFalse(worker.isDone()); + + assertNull(worker.runner()); + } + + @Test + void testBlockingSelection() { + IgniteWorker worker = new NoopWorker(log, null); + + long currentTimeMillis = coarseCurrentTimeMillis(); + + worker.blockingSectionBegin(); + + assertEquals(Long.MAX_VALUE, worker.heartbeat()); + + worker.blockingSectionEnd(); + + assertThat(worker.heartbeat(), greaterThanOrEqualTo(currentTimeMillis)); + + // Checks update heartbeat after blockingSectionBegin(). + + worker.blockingSectionBegin(); + + assertEquals(Long.MAX_VALUE, worker.heartbeat()); + + worker.updateHeartbeat(); + + assertThat(worker.heartbeat(), greaterThanOrEqualTo(currentTimeMillis)); + + worker.blockingSectionEnd(); + + assertThat(worker.heartbeat(), greaterThanOrEqualTo(currentTimeMillis)); + } + + @Test + void testUpdateHeartbeat() throws Exception { + IgniteWorker worker = new NoopWorker(log, null); + + long currentTimeMillis = coarseCurrentTimeMillis(); + + worker.updateHeartbeat(); + + long heartbeat = worker.heartbeat(); + + assertThat(heartbeat, greaterThanOrEqualTo(currentTimeMillis)); + + Thread.sleep(10); + + assertEquals(heartbeat, worker.heartbeat()); + + worker.updateHeartbeat(); + + assertThat(worker.heartbeat(), greaterThan(heartbeat)); + } + + @Test + void testIdle() { + List<String> events = new ArrayList<>(); + + TestWorkerListener listener = new TestWorkerListener(events); + + IgniteWorker worker = new NoopWorker(log, listener); + + worker.onIdle(); + + assertThat(events, equalTo(List.of(ON_IDLE))); + } + + @Test + void testRun() { + List<String> events = new ArrayList<>(); + + TestWorkerListener listener = new TestWorkerListener(events) { + /** {@inheritDoc} */ + @Override + public void onStarted(IgniteWorker worker) { + super.onStarted(worker); + + assertThat(worker.heartbeat(), lessThanOrEqualTo(coarseCurrentTimeMillis())); + assertSame(Thread.currentThread(), worker.runner()); + assertFalse(worker.isCancelled()); + assertFalse(worker.isDone()); + } + + /** {@inheritDoc} */ + @Override + public void onStopped(IgniteWorker worker) { + super.onStopped(worker); + + assertThat(worker.heartbeat(), lessThanOrEqualTo(coarseCurrentTimeMillis())); + assertSame(Thread.currentThread(), worker.runner()); + assertFalse(worker.isCancelled()); + assertTrue(worker.isDone()); + } + }; + + IgniteWorker worker = new NoopWorker(log, listener) { + /** {@inheritDoc} */ + @Override + protected void cleanup() { + events.add(CLEANUP); + } + }; + + worker.run(); + + assertThat(events, equalTo(List.of(ON_STARTED, CLEANUP, ON_STOPPED))); + + assertThat(worker.heartbeat(), lessThanOrEqualTo(coarseCurrentTimeMillis())); + assertNull(worker.runner()); + assertFalse(worker.isCancelled()); + assertTrue(worker.isDone()); + } + + @Test + void testInterruptFromBody() { + List<String> events = new ArrayList<>(); + + TestWorkerListener listener = new TestWorkerListener(events); + + IgniteWorker worker = new NoopWorker(log, listener) { + /** {@inheritDoc} */ + @Override + protected void body() throws InterruptedException { + throw new InterruptedException(); Review Comment: Added it. -- 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]
