-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00e90d4
commit ca6d8df
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...test/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockScheduledExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
nop-autotest/nop-autotest-core/src/main/java/io/nop/autotest/core/mock/MockTaskKind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
22 changes: 22 additions & 0 deletions
22
...graphql-core/src/main/java/io/nop/graphql/core/reflection/DummyGraphQLArgsNormalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |