From 6093e52ea7cc6da9123ca8d4be5bd446e97f5eac Mon Sep 17 00:00:00 2001
From: Lukas Stadler <lukas.stadler@oracle.com>
Date: Sun, 13 Aug 2017 14:02:11 +0200
Subject: [PATCH] do not create new engines for message resolution tests

---
 .../r/test/engine/interop/AbstractMRTest.java | 17 ++++++++++++++
 .../engine/interop/ActiveBindingMRTest.java   |  1 -
 .../r/test/engine/interop/ListMRTest.java     | 20 ++++++----------
 .../interop/RArgsValuesAndNamesMRTest.java    |  2 --
 .../engine/interop/REnvironmentMRTest.java    | 16 +++++--------
 .../test/engine/interop/RFunctionMRTest.java  | 10 ++++----
 .../test/engine/interop/RLanguageMRTest.java  | 13 +++++------
 .../test/engine/interop/RS4ObjectMRTest.java  | 15 +++++-------
 .../r/test/engine/interop/VectorMRTest.java   | 23 ++++++-------------
 9 files changed, 54 insertions(+), 63 deletions(-)

diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractMRTest.java
index 7ed0712e37..0b83817029 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/AbstractMRTest.java
@@ -27,16 +27,33 @@ import com.oracle.truffle.api.interop.InteropException;
 import com.oracle.truffle.api.interop.Message;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
+import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.ffi.impl.interop.NativePointer;
 import java.util.HashSet;
 import java.util.Set;
+
+import org.junit.AfterClass;
 import org.junit.Assert;
+import org.junit.BeforeClass;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import org.junit.Test;
 
 public abstract class AbstractMRTest {
 
+    protected static PolyglotEngine engine;
+
+    @BeforeClass
+    public static void before() {
+        engine = PolyglotEngine.newBuilder().build();
+    }
+
+    @AfterClass
+    public static void after() {
+        engine.dispose();
+    }
+
     /**
      * Create TruffleObject-s to be rudimentary tested for IS_NULL, IS_BOXED/UNBOX, IS_EXECUTABLE,
      * IS_POINTER, HAS_SIZE/GET_SIZE/KEYS behavior.
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ActiveBindingMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ActiveBindingMRTest.java
index 2d750d368e..fbeaa5c52e 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ActiveBindingMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ActiveBindingMRTest.java
@@ -43,7 +43,6 @@ public class ActiveBindingMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject[] createTruffleObjects() throws Exception {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         Source src = Source.newBuilder("f=function() {}").mimeType("text/x-r").name("test.R").build();
         PolyglotEngine.Value result = engine.eval(src);
         RFunction fn = result.as(RFunction.class);
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ListMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ListMRTest.java
index 984ed43dec..d9c4aaa65e 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ListMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/ListMRTest.java
@@ -22,6 +22,12 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.KeyInfo;
 import com.oracle.truffle.api.interop.Message;
@@ -30,9 +36,6 @@ import com.oracle.truffle.api.interop.UnknownIdentifierException;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
 import com.oracle.truffle.api.interop.UnsupportedTypeException;
 import com.oracle.truffle.api.interop.java.JavaInterop;
-
-import org.junit.Test;
-
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.ffi.impl.interop.NativePointer;
@@ -40,18 +43,10 @@ import com.oracle.truffle.r.runtime.data.RList;
 import com.oracle.truffle.r.runtime.data.RNull;
 import com.oracle.truffle.r.runtime.data.RPairList;
 import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 
 public class ListMRTest extends AbstractMRTest {
 
     private String testValues = "i=1L, d=2.1, b=TRUE, fn=function() {}, n=NULL, 4";
-    private final PolyglotEngine engine;
-
-    public ListMRTest() {
-        engine = PolyglotEngine.newBuilder().build();
-    }
 
     @Override
     @Test
@@ -157,7 +152,7 @@ public class ListMRTest extends AbstractMRTest {
         assertFalse(KeyInfo.isInternal(info));
     }
 
-    private RAbstractContainer create(String createFun, String values) {
+    private static RAbstractContainer create(String createFun, String values) {
         Source src = Source.newBuilder(createFun + "(" + values + ")").mimeType("text/x-r").name("test.R").build();
         PolyglotEngine.Value result = engine.eval(src);
         return result.as(RAbstractContainer.class);
@@ -188,5 +183,4 @@ public class ListMRTest extends AbstractMRTest {
     protected int getSize(TruffleObject obj) {
         return obj instanceof RList ? ((RList) obj).getLength() : ((RPairList) obj).getLength();
     }
-
 }
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RArgsValuesAndNamesMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RArgsValuesAndNamesMRTest.java
index a7e3eb8481..25e3ce0587 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RArgsValuesAndNamesMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RArgsValuesAndNamesMRTest.java
@@ -132,7 +132,6 @@ public class RArgsValuesAndNamesMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject[] createTruffleObjects() throws Exception {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         Source src = Source.newBuilder("f=function() {}").mimeType("text/x-r").name("test.R").build();
         PolyglotEngine.Value result = engine.eval(src);
         RFunction fn = result.as(RFunction.class);
@@ -159,5 +158,4 @@ public class RArgsValuesAndNamesMRTest extends AbstractMRTest {
     protected int getSize(TruffleObject obj) {
         return ((RArgsValuesAndNames) obj).getLength();
     }
-
 }
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/REnvironmentMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/REnvironmentMRTest.java
index cc85fd4757..60ef430454 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/REnvironmentMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/REnvironmentMRTest.java
@@ -22,6 +22,12 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.KeyInfo;
 import com.oracle.truffle.api.interop.Message;
@@ -29,15 +35,8 @@ import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnknownIdentifierException;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
 import com.oracle.truffle.api.interop.java.JavaInterop;
-
-import org.junit.Test;
-
 import com.oracle.truffle.api.source.Source;
-import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.env.REnvironment;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 
 public class REnvironmentMRTest extends AbstractMRTest {
 
@@ -133,7 +132,6 @@ public class REnvironmentMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject[] createTruffleObjects() throws Exception {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         Source src = Source.newBuilder("e <- new.env(); e$s <- 'aaa'; e$i <- 123L; e$d <- 123.1; e$b <- TRUE; e$fn <- function() {}; e$n <- NULL; e$l <- 666; lockBinding('l', e); e").mimeType(
                         "text/x-r").name("test.R").build();
         return new TruffleObject[]{engine.eval(src).as(REnvironment.class)};
@@ -146,9 +144,7 @@ public class REnvironmentMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject createEmptyTruffleObject() throws Exception {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         Source src = Source.newBuilder("new.env()").mimeType("text/x-r").name("test.R").build();
         return engine.eval(src).as(REnvironment.class);
     }
-
 }
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RFunctionMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RFunctionMRTest.java
index 52e912a153..1397ae9de2 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RFunctionMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RFunctionMRTest.java
@@ -22,19 +22,20 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ArityException;
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.Message;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
 import com.oracle.truffle.api.interop.UnsupportedTypeException;
-
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.data.RFunction;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import org.junit.Test;
 
 public class RFunctionMRTest extends AbstractMRTest {
 
@@ -72,7 +73,6 @@ public class RFunctionMRTest extends AbstractMRTest {
     }
 
     private static RFunction create(String fun) {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         Source src = Source.newBuilder(fun).mimeType("text/x-r").name("test.R").build();
         PolyglotEngine.Value result = engine.eval(src);
         return result.as(RFunction.class);
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RLanguageMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RLanguageMRTest.java
index 66a7eac259..df5d8d6aca 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RLanguageMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RLanguageMRTest.java
@@ -22,20 +22,21 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Assert;
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.KeyInfo;
 import com.oracle.truffle.api.interop.Message;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnknownIdentifierException;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
-
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.data.RLanguage;
-import org.junit.Assert;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import org.junit.Test;
 
 public class RLanguageMRTest extends AbstractMRTest {
 
@@ -92,7 +93,6 @@ public class RLanguageMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject[] createTruffleObjects() {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         // TODO any simpler way to create a RLanguage ?
         String srcTxt = "ne <- new.env(); delayedAssign('x', 1 + 2, assign.env = ne); substitute(x, ne)";
         Source src = Source.newBuilder(srcTxt).mimeType("text/x-r").name("test.R").build();
@@ -114,5 +114,4 @@ public class RLanguageMRTest extends AbstractMRTest {
     protected int getSize(TruffleObject obj) {
         return ((RLanguage) obj).getLength();
     }
-
 }
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RS4ObjectMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RS4ObjectMRTest.java
index 233ec91a21..70b314fae9 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RS4ObjectMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/RS4ObjectMRTest.java
@@ -22,26 +22,24 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.KeyInfo;
 import com.oracle.truffle.api.interop.Message;
 import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnknownIdentifierException;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
-
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.data.RS4Object;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import org.junit.Test;
 
 public class RS4ObjectMRTest extends AbstractMRTest {
 
-    public RS4ObjectMRTest() {
-    }
-
     @Test
     public void testKeysInfo() throws Exception {
         TruffleObject s4 = createTruffleObjects()[0];
@@ -118,7 +116,6 @@ public class RS4ObjectMRTest extends AbstractMRTest {
 
     @Override
     protected TruffleObject[] createTruffleObjects() {
-        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
         String srcTxt = "setClass('test', representation(s = 'character', d = 'numeric', i = 'integer', b = 'logical', fn = 'function'));" +
                         "new('test', s = 'aaa', d = 1.1, i=123L, b = TRUE, fn = function() {})";
         Source src = Source.newBuilder(srcTxt).mimeType("text/x-r").name("test.R").build();
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/VectorMRTest.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/VectorMRTest.java
index 8547a34982..dd2e5d8039 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/VectorMRTest.java
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/engine/interop/VectorMRTest.java
@@ -22,6 +22,12 @@
  */
 package com.oracle.truffle.r.test.engine.interop;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
 import com.oracle.truffle.api.interop.ForeignAccess;
 import com.oracle.truffle.api.interop.KeyInfo;
 import com.oracle.truffle.api.interop.Message;
@@ -29,26 +35,13 @@ import com.oracle.truffle.api.interop.TruffleObject;
 import com.oracle.truffle.api.interop.UnknownIdentifierException;
 import com.oracle.truffle.api.interop.UnsupportedMessageException;
 import com.oracle.truffle.api.interop.java.JavaInterop;
-
-import org.junit.Test;
-
 import com.oracle.truffle.api.source.Source;
-import com.oracle.truffle.api.vm.PolyglotEngine;
 import com.oracle.truffle.r.runtime.data.model.RAbstractIntVector;
 import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
 import com.oracle.truffle.r.runtime.data.model.RAbstractVector;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 
 public class VectorMRTest extends AbstractMRTest {
 
-    private final PolyglotEngine engine;
-
-    public VectorMRTest() {
-        engine = PolyglotEngine.newBuilder().build();
-    }
-
     @Test
     public void testReadWrite() throws Exception {
         final TruffleObject vi = create("1L:10L");
@@ -79,7 +72,6 @@ public class VectorMRTest extends AbstractMRTest {
         RAbstractVector vec = JavaInterop.asJavaObject(RAbstractVector.class, nvi);
         assertTrue(vec instanceof RAbstractStringVector);
         assertEquals("abc", ForeignAccess.sendRead(Message.READ.createNode(), nvi, 0));
-
     }
 
     @Test
@@ -123,9 +115,8 @@ public class VectorMRTest extends AbstractMRTest {
         return ((RAbstractVector) obj).getLength();
     }
 
-    private TruffleObject create(String createTxt) throws Exception {
+    private static TruffleObject create(String createTxt) throws Exception {
         Source src = Source.newBuilder(createTxt).mimeType("text/x-r").name("test.R").build();
         return engine.eval(src).as(RAbstractVector.class);
     }
-
 }
-- 
GitLab