diff --git a/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockScheduledExecutor.java b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockScheduledExecutor.java new file mode 100644 index 000000000..fdc5076b7 --- /dev/null +++ b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockScheduledExecutor.java @@ -0,0 +1,110 @@ +package io.nop.autotest.core.mock; + +import io.nop.api.core.exceptions.NopException; +import io.nop.commons.concurrent.executor.IScheduledExecutor; +import io.nop.commons.concurrent.executor.ThreadPoolConfig; +import io.nop.commons.concurrent.executor.ThreadPoolStats; +import io.nop.commons.functional.Functionals; + +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.DelayQueue; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +public class MockScheduledExecutor implements IScheduledExecutor { + private String name; + private volatile boolean started = true; + private final DelayQueue tasks = new DelayQueue<>(); + + public void setName(String name) { + this.name = name; + } + + @Override + public synchronized CompletableFuture schedule(Callable callable, long delay, TimeUnit unit) { + CompletableFuture promise = new CompletableFuture<>(); + MockTask task = new MockTask(callable, delay, 0, MockTaskKind.ONCE, promise); + tasks.add(task); + return promise.exceptionally(err -> { + tasks.remove(task); + throw NopException.adapt(err); + }); + } + + @Override + public synchronized Future scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { + CompletableFuture promise = new CompletableFuture<>(); + MockTask task = new MockTask(Functionals.asCallable(command, null), initialDelay, + period, MockTaskKind.SCHEDULED_AT_FIXED_RATE, promise); + tasks.add(task); + return promise.exceptionally(err -> { + tasks.remove(task); + throw NopException.adapt(err); + }); + } + + @Override + public Future scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { + CompletableFuture promise = new CompletableFuture<>(); + MockTask task = new MockTask(Functionals.asCallable(command, null), initialDelay, + delay, MockTaskKind.SCHEDULED_WITH_FIXED_DELAY, promise); + tasks.add(task); + return promise.exceptionally(err -> { + tasks.remove(task); + throw NopException.adapt(err); + }); + } + + public Object triggerNext() { + MockTask task = pollTask(); + if (task == null) + return null; + return task.call(); + } + + protected synchronized MockTask pollTask() { + MockTask task = tasks.poll(); + return task; + } + + @Override + public String getName() { + return name; + } + + @Override + public ThreadPoolConfig getConfig() { + return null; + } + + @Override + public ThreadPoolStats stats() { + return null; + } + + @Override + public CompletableFuture submit(Callable callable) { + return schedule(callable, 0, TimeUnit.MILLISECONDS); + } + + @Override + public CompletableFuture submit(Runnable task, V result) { + return submit(Functionals.asCallable(task, result)); + } + + @Override + public void refreshConfig() { + + } + + @Override + public void destroy() { + started = false; + } + + @Override + public void execute(Runnable command) { + submit(command, null); + } +} diff --git a/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTask.java b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTask.java new file mode 100644 index 000000000..37cd41439 --- /dev/null +++ b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTask.java @@ -0,0 +1,63 @@ +package io.nop.autotest.core.mock; + +import io.nop.api.core.exceptions.NopException; + +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +public class MockTask implements Delayed { + private final Callable action; + private final long initialDelay; + private final long period; + private final MockTaskKind taskKind; + private final CompletableFuture promise; + + public MockTask(Callable action, long initialDelay, long period, MockTaskKind taskKind, + CompletableFuture promise) { + this.action = action; + this.initialDelay = initialDelay; + this.period = period; + this.taskKind = taskKind; + this.promise = promise; + } + + @Override + public long getDelay(TimeUnit unit) { + return unit.convert(initialDelay, TimeUnit.MILLISECONDS); + } + + @Override + public int compareTo(Delayed o) { + return Long.compare(initialDelay, o.getDelay(TimeUnit.MILLISECONDS)); + } + + public Callable getAction() { + return action; + } + + public Object call() { + try { + Object ret = action.call(); + if (taskKind == MockTaskKind.ONCE) + ((CompletableFuture) promise).complete(ret); + return ret; + } catch (Exception e) { + promise.completeExceptionally(e); + throw NopException.adapt(e); + } + } + + public long getInitialDelay() { + return initialDelay; + } + + public long getPeriod() { + return period; + } + + public MockTaskKind getTaskKind() { + return taskKind; + } +} diff --git a/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTaskKind.java b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTaskKind.java new file mode 100644 index 000000000..ba3ea02dd --- /dev/null +++ b/nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTaskKind.java @@ -0,0 +1,5 @@ +package io.nop.autotest.core.mock; + +public enum MockTaskKind { + ONCE, SCHEDULED_AT_FIXED_RATE, SCHEDULED_WITH_FIXED_DELAY +} diff --git a/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/DummyGraphQLArgsNormalizer.java b/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/DummyGraphQLArgsNormalizer.java new file mode 100644 index 000000000..1ab2309cf --- /dev/null +++ b/nop-graphql/nop-graphql-core/src/main/java/io/nop/graphql/core/reflection/DummyGraphQLArgsNormalizer.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2017-2024 Nop Platform. All rights reserved. + * Author: canonical_entropy@163.com + * Blog: https://www.zhihu.com/people/canonical-entropy + * Gitee: https://gitee.com/canonical-entropy/nop-entropy + * Github: https://github.com/entropy-cloud/nop-entropy + */ +package io.nop.graphql.core.reflection; + +import io.nop.graphql.core.IGraphQLExecutionContext; + +import java.util.Map; + +/** + * 对前台传入的参数进行规范化之后再传给服务函数 + */ +public class DummyGraphQLArgsNormalizer implements IGraphQLArgsNormalizer { + @Override + public Map normalize(Map args, IGraphQLExecutionContext context) { + return args; + } +}