From 8bedf8fca1f4b0ea1f614505072329e373dcc4de Mon Sep 17 00:00:00 2001 From: Kostya Date: Tue, 24 Mar 2020 13:26:41 +0300 Subject: [PATCH] Fix thread local (#23) * Fix thread local Co-authored-by: k.struzhkin --- Jenkinsfile | 2 +- build_utils | 2 +- pom.xml | 6 +++- .../fraudo/visitor/TemplateVisitor.java | 4 ++- .../visitor/impl/FirstFindVisitorImpl.java | 31 ++++++++++++------- .../java/com/rbkmoney/fraudo/CustomTest.java | 2 ++ 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index bbafb8e..85c23c2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -8,6 +8,6 @@ build('fraudo', 'docker-host') { javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy") } - def buildImageTag = "4799280a02cb73761a3ba3641285aac8ec4ec482" + def buildImageTag = "fcf116dd775cc2e91bffb6a36835754e3f2d5321" javaLibPipeline(buildImageTag) } \ No newline at end of file diff --git a/build_utils b/build_utils index 9b66408..cf6578d 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit 9b664082ddc8ec8cdfbe7513d54e433d24198cc2 +Subproject commit cf6578dac794859b0495c812761ce5b94c11bbaf diff --git a/pom.xml b/pom.xml index fe53ba3..1b8314e 100644 --- a/pom.xml +++ b/pom.xml @@ -11,9 +11,13 @@ fraudo - 0.0.10-SNAPSHOT + 0.0.11-SNAPSHOT + UTF-8 + 8 + bc95d0d6dc13c693acd2b274531a7d604b877bf3 + ${env.REGISTRY} 2.0.0.0 ${project.basedir}/../target/jacoco.exec diff --git a/src/main/java/com/rbkmoney/fraudo/visitor/TemplateVisitor.java b/src/main/java/com/rbkmoney/fraudo/visitor/TemplateVisitor.java index bcc4384..d85af27 100644 --- a/src/main/java/com/rbkmoney/fraudo/visitor/TemplateVisitor.java +++ b/src/main/java/com/rbkmoney/fraudo/visitor/TemplateVisitor.java @@ -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 { +import java.io.Closeable; + +public interface TemplateVisitor extends Closeable { Object visit(ParseTree tree, T model); diff --git a/src/main/java/com/rbkmoney/fraudo/visitor/impl/FirstFindVisitorImpl.java b/src/main/java/com/rbkmoney/fraudo/visitor/impl/FirstFindVisitorImpl.java index aa3a17a..9945edd 100644 --- a/src/main/java/com/rbkmoney/fraudo/visitor/impl/FirstFindVisitorImpl.java +++ b/src/main/java/com/rbkmoney/fraudo/visitor/impl/FirstFindVisitorImpl.java @@ -23,10 +23,10 @@ import java.util.Map; @Slf4j @RequiredArgsConstructor -public class FirstFindVisitorImpl extends FraudoBaseVisitor implements TemplateVisitor { +public class FirstFindVisitorImpl extends FraudoBaseVisitor implements TemplateVisitor { - private ThreadLocal> localFuncCache; - private ThreadLocal threadLocalModel; + private ThreadLocal> localFuncCache = ThreadLocal.withInitial(HashMap::new); + private ThreadLocal threadLocalModel = new ThreadLocal<>(); private final CountVisitor countVisitor; private final SumVisitor sumVisitor; @@ -34,21 +34,28 @@ public class FirstFindVisitorImpl extends FraudoBaseVisi private final CustomFuncVisitor customFuncVisitor; private final FieldResolver 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 diff --git a/src/test/java/com/rbkmoney/fraudo/CustomTest.java b/src/test/java/com/rbkmoney/fraudo/CustomTest.java index f275716..2cca370 100644 --- a/src/test/java/com/rbkmoney/fraudo/CustomTest.java +++ b/src/test/java/com/rbkmoney/fraudo/CustomTest.java @@ -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()); }