Skip to content

Commit

Permalink
feat(mybatis): fix test bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed May 15, 2024
1 parent bd7a9cd commit 20177f6
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import me.ahoo.cosid.accessor.registry.CosIdAccessorRegistry;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
Expand All @@ -35,35 +34,35 @@
*/
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class CosIdPlugin implements Interceptor {

public static final String DEFAULT_LIST_KEY = "list";
private final CosIdAccessorRegistry accessorRegistry;
private final String listKey;

public CosIdPlugin(CosIdAccessorRegistry accessorRegistry) {
this(accessorRegistry, DEFAULT_LIST_KEY);
}

public CosIdPlugin(CosIdAccessorRegistry accessorRegistry, String listKey) {
this.accessorRegistry = accessorRegistry;
this.listKey = listKey;
}

@SuppressWarnings("rawtypes")
@Override
public Object intercept(Invocation invocation) throws Throwable {

Object[] args = invocation.getArgs();
MappedStatement statement = (MappedStatement) args[0];
if (!SqlCommandType.INSERT.equals(statement.getSqlCommandType())) {
return invocation.proceed();
}

Object parameter = args[1];
if (Objects.isNull(parameter)) {
return invocation.proceed();
}

if (!(parameter instanceof Map)) {
accessorRegistry.ensureId(parameter);
return invocation.proceed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@
import lombok.SneakyThrows;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.transaction.Transaction;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -149,20 +161,92 @@ public void setId(long id) {
}
}

public static class InvocationTarget {
public static class InvocationTarget implements Executor {

public static final Method INVOKE_METHOD;

static {
try {
INVOKE_METHOD = InvocationTarget.class.getMethod("invoke", MappedStatement.class, Object.class);
INVOKE_METHOD = Executor.class.getMethod("update", MappedStatement.class, Object.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

public void invoke(MappedStatement statement, Object entity) {


@Override
public int update(MappedStatement ms, Object parameter) throws SQLException {
return 0;
}

@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
return List.of();
}

@Override
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
return List.of();
}

@Override
public <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException {
return null;
}

@Override
public List<BatchResult> flushStatements() throws SQLException {
return List.of();
}

@Override
public void commit(boolean required) throws SQLException {

}

@Override
public void rollback(boolean required) throws SQLException {

}

@Override
public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
return null;
}

@Override
public boolean isCached(MappedStatement ms, CacheKey key) {
return false;
}

@Override
public void clearLocalCache() {

}

@Override
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType) {

}

@Override
public Transaction getTransaction() {
return null;
}

@Override
public void close(boolean forceRollback) {

}

@Override
public boolean isClosed() {
return false;
}

@Override
public void setExecutorWrapper(Executor executor) {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,83 +30,84 @@ public class ConcurrentGenerateStingSpec implements TestSpec {
private final int concurrentThreads;
private final long idSize;
private final int singleGenerates;

public ConcurrentGenerateStingSpec(IdGenerator... idGenerators) {
this(10, 800000, idGenerators);
}

public ConcurrentGenerateStingSpec(int concurrentThreads, long idSize, IdGenerator... idGenerators) {
Preconditions.checkState(idGenerators.length > 0, "idGenerators can not be empty.");
this.idGenerators = idGenerators;
this.concurrentThreads = concurrentThreads;
this.idSize = idSize;
this.singleGenerates = (int) (idSize / concurrentThreads);
}

public int getConcurrentThreads() {
return concurrentThreads;
}

public long getIdSize() {
return idSize;
}

private IdGenerator getIdGenerator(int threadIdx) {
return idGenerators[threadIdx % (idGenerators.length)];
}

protected void assertSingleEach(String previousId, String id) {
Preconditions.checkState(id.compareTo(previousId) > 0, "id:[%s] must greater then previousId:[%s]", id, previousId);
}

protected void assertGlobalEach(String previousId, String id) {
Preconditions.checkState(id.compareTo(previousId) > 0, "id:[%s] must equals previousId:[%s]+1.", id, previousId);
}

@Override
@SuppressWarnings("unchecked")
public void verify() {

CompletableFuture<String[]>[] completableFutures = new CompletableFuture[concurrentThreads];

for (int i = 0; i < completableFutures.length; i++) {
final IdGenerator idGenerator = getIdGenerator(i);
completableFutures[i] = CompletableFuture
.supplyAsync(() -> {
String[] ids = new String[singleGenerates];
String previousId = "0";
for (int j = 0; j < ids.length; j++) {
String nextId = idGenerator.generateAsString();
ids[j] = nextId;
assertSingleEach(previousId, nextId);
previousId = nextId;
}
return ids;
});
.supplyAsync(() -> {
String[] ids = new String[singleGenerates];
String previousId = "0";
for (int j = 0; j < ids.length; j++) {
String nextId = idGenerator.generateAsString();
ids[j] = nextId;
assertSingleEach(previousId, nextId);
previousId = nextId;
}
return ids;
});
}

CompletableFuture
.allOf(completableFutures)
.thenAccept(nil -> {
final String[] totalIds = new String[(int) idSize];
int totalIdx = 0;
for (CompletableFuture<String[]> completableFuture : completableFutures) {
String[] ids = completableFuture.join();
for (String id : ids) {
totalIds[totalIdx++] = id;
.allOf(completableFutures)
.thenAccept(nil -> {
final String[] totalIds = new String[(int) idSize];
int totalIdx = 0;
for (CompletableFuture<String[]> completableFuture : completableFutures) {
String[] ids = completableFuture.join();
for (String id : ids) {
totalIds[totalIdx++] = id;
}
}
}

Arrays.sort(totalIds);

String previousId = "-1";
for (String id : totalIds) {
if ("-1".equals(previousId)) {

Arrays.sort(totalIds);

String previousId = "-1";
for (String id : totalIds) {
if ("-1".equals(previousId)) {
previousId = id;
continue;
}
assertGlobalEach(previousId, id);
previousId = id;
continue;
}
assertGlobalEach(previousId, id);
previousId = id;
}
}).join();
}).join();
}
}

0 comments on commit 20177f6

Please sign in to comment.