diff options
Diffstat (limited to 'utils-test/src/main/java/org/onap/policy/common/utils/time')
8 files changed, 94 insertions, 40 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/PeriodicItem.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/PeriodicItem.java index 79d2f226..04a9b3f6 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/PeriodicItem.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/PeriodicItem.java @@ -1,8 +1,8 @@ -/* +/*-- * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +20,6 @@ package org.onap.policy.common.utils.time; -import org.onap.policy.common.utils.time.TestTime; - /** * Work item that runs periodically. */ diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoExecutor.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoExecutor.java new file mode 100644 index 00000000..b29f7421 --- /dev/null +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoExecutor.java @@ -0,0 +1,70 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.time; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.concurrent.Executor; +import lombok.Getter; + +/** + * Executor that will run tasks until the queue is empty or a maximum number of tasks have + * been executed. Doesn't actually run anything until {@link #runAll()} is invoked. + */ +public class PseudoExecutor implements Executor { + + /** + * Tasks to be run. + */ + @Getter + private final Queue<Runnable> tasks = new LinkedList<>(); + + + /** + * Gets the queue length. + * + * @return the queue length + */ + public int getQueueLength() { + return tasks.size(); + } + + @Override + public void execute(Runnable command) { + tasks.add(command); + } + + /** + * Runs all tasks until the queue is empty or the maximum number of tasks have been + * reached. + * + * @param maxTasks maximum number of tasks to run + * @return {@code true} if the queue is empty, {@code false} if the maximum number of + * tasks have been reached before the queue was emptied + */ + public boolean runAll(int maxTasks) { + for (var count = 0; count < maxTasks && !tasks.isEmpty(); ++count) { + tasks.remove().run(); + } + + return tasks.isEmpty(); + } +} diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorService.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorService.java index 4f9b32c9..71a24528 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorService.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledExecutorService.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; +import lombok.Getter; /** * Scheduled executor service that uses {@link TestTimeMulti} to execute its tasks. Note: @@ -48,6 +49,7 @@ public class PseudoScheduledExecutorService implements ScheduledExecutorService * {@code True} if {@link #shutdown()} or {@link #shutdownNow()} has been called, * {@code false} otherwise. */ + @Getter private boolean shutdown = false; /** @@ -80,13 +82,8 @@ public class PseudoScheduledExecutorService implements ScheduledExecutorService } @Override - public boolean isShutdown() { - return shutdown; - } - - @Override public boolean isTerminated() { - return shutdown; + return isShutdown(); } @Override diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledFuture.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledFuture.java index 6ce7bc04..34c756bb 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledFuture.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/PseudoScheduledFuture.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ class PseudoScheduledFuture<T> extends FutureTask<T> implements RunnableSchedule /** * {@code True} if this task is periodic, {@code false} otherwise. */ + @Getter private final boolean periodic; /** @@ -82,11 +83,6 @@ class PseudoScheduledFuture<T> extends FutureTask<T> implements RunnableSchedule } @Override - public boolean isPeriodic() { - return periodic; - } - - @Override public void run() { if (isPeriodic()) { super.runAndReset(); diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/RunnableItem.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/RunnableItem.java index 54560316..67371bb9 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/RunnableItem.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/RunnableItem.java @@ -1,8 +1,8 @@ -/* +/*- * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ package org.onap.policy.common.utils.time; import java.util.concurrent.Future; import lombok.AccessLevel; import lombok.Getter; -import org.onap.policy.common.utils.time.TestTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,7 +79,7 @@ class RunnableItem extends WorkItem { try { action.run(); } catch (RuntimeException e) { - logger.warn("work item {} threw an exception {}", this, e); + logger.warn("work item {} threw an exception", this, e); } } diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTime.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTime.java index 420021f3..ace19160 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTime.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTime.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Common Utils-Test * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,14 @@ package org.onap.policy.common.utils.time; import java.util.Date; import java.util.concurrent.atomic.AtomicLong; +import lombok.NoArgsConstructor; /** * "Current" time, when running junit tests. This is intended to be injected into classes * under test, to replace their {@link CurrentTime} objects. When {@link #sleep(long)} is * invoked, it simply advances the notion of "current" time and returns immediately. */ +@NoArgsConstructor public class TestTime extends CurrentTime { /** @@ -35,14 +37,6 @@ public class TestTime extends CurrentTime { */ private AtomicLong tcur = new AtomicLong(System.currentTimeMillis()); - /** - * Constructor. - * - */ - public TestTime() { - super(); - } - @Override public long getMillis() { return tcur.get(); diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java index f52105ed..9e61eaa3 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/TestTimeMulti.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,6 @@ import java.util.PriorityQueue; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import lombok.Getter; -import org.onap.policy.common.utils.time.TestTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -180,7 +179,7 @@ public class TestTimeMulti extends TestTime { long realEnd = System.currentTimeMillis() + maxWaitMs; while (System.currentTimeMillis() < realEnd) { - if (condition.call()) { + if (Boolean.TRUE.equals(condition.call())) { return; } @@ -190,11 +189,13 @@ public class TestTimeMulti extends TestTime { } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("interrupted while waiting for condition", e); - fail("interrupted while waiting for condition: " + e.getMessage()); + // disabling sonar, as this is only used by junit tests + fail("interrupted while waiting for condition: " + e.getMessage()); // NOSONAR } catch (Exception e) { logger.error("condition evaluator threw an exception", e); - fail("condition evaluator threw an exception: " + e.getMessage()); + // disabling sonar, as this is only used by junit tests + fail("condition evaluator threw an exception: " + e.getMessage()); // NOSONAR } fail(NEVER_SATISFIED); @@ -281,7 +282,7 @@ public class TestTimeMulti extends TestTime { return; } - SleepItem item = new SleepItem(this, sleepMs, Thread.currentThread()); + var item = new SleepItem(this, sleepMs, Thread.currentThread()); enqueue(item); // wait for the item to fire @@ -299,7 +300,7 @@ public class TestTimeMulti extends TestTime { logger.info("enqueue work item {}", item); synchronized (updateLock) { queue.add(item); - updateLock.notify(); + updateLock.notifyAll(); } } diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/time/WorkItem.java b/utils-test/src/main/java/org/onap/policy/common/utils/time/WorkItem.java index af3d5d7e..cd690602 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/time/WorkItem.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/time/WorkItem.java @@ -1,8 +1,8 @@ -/* +/*-- * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ package org.onap.policy.common.utils.time; import lombok.AccessLevel; import lombok.Getter; -import org.onap.policy.common.utils.time.TestTime; /** * Work item to be executed at some time. |