Skip to content

Commit

Permalink
增加MockScheduledExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 5, 2025
1 parent 00e90d4 commit ca6d8df
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<MockTask> tasks = new DelayQueue<>();

public void setName(String name) {
this.name = name;
}

@Override
public synchronized <V> CompletableFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
CompletableFuture<V> 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<Void> 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<Void> 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 <V> CompletableFuture<V> submit(Callable<V> callable) {
return schedule(callable, 0, TimeUnit.MILLISECONDS);
}

@Override
public <V> CompletableFuture<V> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Object>) 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.nop.autotest.core.mock;

public enum MockTaskKind {
ONCE, SCHEDULED_AT_FIXED_RATE, SCHEDULED_WITH_FIXED_DELAY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2017-2024 Nop Platform. All rights reserved.
* Author: [email protected]
* 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<String, Object> normalize(Map<String, Object> args, IGraphQLExecutionContext context) {
return args;
}
}

0 comments on commit ca6d8df

Please sign in to comment.