Fix thread local (#23)

* Fix thread local

Co-authored-by: k.struzhkin <k.struzhkin@MacBook-Pro-rbkadmin.local>
This commit is contained in:
Kostya 2020-03-24 13:26:41 +03:00 committed by GitHub
parent 021da93393
commit 8bedf8fca1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 16 deletions

2
Jenkinsfile vendored
View File

@ -8,6 +8,6 @@ build('fraudo', 'docker-host') {
javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy")
}
def buildImageTag = "4799280a02cb73761a3ba3641285aac8ec4ec482"
def buildImageTag = "fcf116dd775cc2e91bffb6a36835754e3f2d5321"
javaLibPipeline(buildImageTag)
}

@ -1 +1 @@
Subproject commit 9b664082ddc8ec8cdfbe7513d54e433d24198cc2
Subproject commit cf6578dac794859b0495c812761ce5b94c11bbaf

View File

@ -11,9 +11,13 @@
</parent>
<artifactId>fraudo</artifactId>
<version>0.0.10-SNAPSHOT</version>
<version>0.0.11-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<dockerfile.base.service.tag>bc95d0d6dc13c693acd2b274531a7d604b877bf3</dockerfile.base.service.tag>
<dockerfile.registry>${env.REGISTRY}</dockerfile.registry>
<hamcrest.junit.version>2.0.0.0</hamcrest.junit.version>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
</properties>

View File

@ -3,7 +3,9 @@ package com.rbkmoney.fraudo.visitor;
import com.rbkmoney.fraudo.model.BaseModel;
import org.antlr.v4.runtime.tree.ParseTree;
public interface TemplateVisitor<T extends BaseModel> {
import java.io.Closeable;
public interface TemplateVisitor<T extends BaseModel> extends Closeable {
Object visit(ParseTree tree, T model);

View File

@ -23,10 +23,10 @@ import java.util.Map;
@Slf4j
@RequiredArgsConstructor
public class FirstFindVisitorImpl<T extends BaseModel, U> extends FraudoBaseVisitor<Object> implements TemplateVisitor <T>{
public class FirstFindVisitorImpl<T extends BaseModel, U> extends FraudoBaseVisitor<Object> implements TemplateVisitor<T> {
private ThreadLocal<Map<String, Object>> localFuncCache;
private ThreadLocal<T> threadLocalModel;
private ThreadLocal<Map<String, Object>> localFuncCache = ThreadLocal.withInitial(HashMap::new);
private ThreadLocal<T> threadLocalModel = new ThreadLocal<>();
private final CountVisitor<T> countVisitor;
private final SumVisitor<T> sumVisitor;
@ -34,21 +34,28 @@ public class FirstFindVisitorImpl<T extends BaseModel, U> extends FraudoBaseVisi
private final CustomFuncVisitor<T> customFuncVisitor;
private final FieldResolver<T, U> fieldResolver;
@Override
public void close() {
localFuncCache.remove();
threadLocalModel.remove();
}
@Override
public Object visit(ParseTree tree, T model) {
try {
validateModel(model);
threadLocalModel.set(model);
return super.visit(tree);
} finally {
localFuncCache.get().clear();
}
}
private void validateModel(T model) {
if (model == null) {
log.error("Model is not init!");
throw new NotValidContextException();
}
localFuncCache = ThreadLocal.withInitial(HashMap::new);
threadLocalModel = ThreadLocal.withInitial(() -> model);
Object visit = super.visit(tree);
localFuncCache.remove();
threadLocalModel.remove();
return visit;
}
@Override

View File

@ -8,6 +8,7 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
@ -92,6 +93,7 @@ public class CustomTest extends AbstractPaymentTest {
com.rbkmoney.fraudo.FraudoParser.ParseContext parseContext = getParseContext(resourceAsStream);
PaymentModel model = new PaymentModel();
model.setAmount(500L);
ResultModel result = invoke(parseContext, model);
assertEquals(ResultStatus.ACCEPT, result.getResultStatus());
}