You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/10/24 13:41:10 UTC

[GitHub] nswamy closed pull request #12955: [MXNET-1159]Added unit tests for making Resource Scope work in Java

nswamy closed pull request #12955: [MXNET-1159]Added unit tests for making Resource Scope work in Java
URL: https://github.com/apache/incubator-mxnet/pull/12955
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/scala-package/core/src/test/java/org/apache/mxnet/javaapi/ResourceScopeTestSuite.java b/scala-package/core/src/test/java/org/apache/mxnet/javaapi/ResourceScopeTestSuite.java
new file mode 100644
index 00000000000..f570ba927fa
--- /dev/null
+++ b/scala-package/core/src/test/java/org/apache/mxnet/javaapi/ResourceScopeTestSuite.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.mxnet.javaapi;
+
+import org.apache.mxnet.NativeResourceRef;
+import org.apache.mxnet.ResourceScope;
+import org.junit.Test;
+
+import java.util.*;
+import java.util.concurrent.Callable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class ResourceScopeTestSuite {
+
+    /**
+     * This is a placeholder class to test out whether NDArray References get collected or not when using
+     * try-with-resources in Java.
+     *
+     */
+    class TestNDArray  {
+        NDArray selfArray;
+
+        public TestNDArray(Context context, int[] shape) {
+            this.selfArray = NDArray.ones(context, shape);
+        }
+
+        public boolean verifyIsDisposed() {
+            return this.selfArray.nd().isDisposed();
+        }
+
+        public NativeResourceRef getNDArrayReference() {
+            return this.selfArray.nd().ref();
+        }
+    }
+
+    @Test
+    public void testNDArrayAutoRelease() {
+        TestNDArray test = null;
+
+        try (ResourceScope scope = new ResourceScope()) {
+            test = new TestNDArray(Context.cpu(), new int[]{100, 100});
+        }
+
+        assertTrue(test.verifyIsDisposed());
+    }
+
+    @Test
+    public void testObjectReleaseFromList() {
+        List<TestNDArray> list = new ArrayList<>();
+
+        try (ResourceScope scope = new ResourceScope()) {
+            for (int i = 0;i < 10; i++) {
+                list.add(new TestNDArray(Context.cpu(), new int[] {100, 100}));
+            }
+        }
+
+        assertEquals(list.size() , 10);
+        list.forEach(n -> assertTrue(n.verifyIsDisposed()));
+    }
+
+    @Test
+    public void testObjectReleaseFromMap() {
+        Map<String, TestNDArray> stringToNDArrayMap = new HashMap<>();
+
+        try (ResourceScope scope = new ResourceScope()) {
+            for (int i = 0;i < 10; i++) {
+                stringToNDArrayMap.put(String.valueOf(i),new TestNDArray(Context.cpu(), new int[] {i, i}));
+            }
+        }
+
+        assertEquals(stringToNDArrayMap.size(), 10);
+        stringToNDArrayMap.forEach((key, value) ->  assertTrue(value.verifyIsDisposed()));
+
+        Map<TestNDArray, String> ndArrayToStringMap = new HashMap<>();
+
+        try (ResourceScope scope = new ResourceScope()) {
+            for (int i = 0;i < 10; i++) {
+                ndArrayToStringMap.put(new TestNDArray(Context.cpu(), new int[] {i, i}), String.valueOf(i));
+            }
+        }
+
+        assertEquals(ndArrayToStringMap.size(), 10);
+        ndArrayToStringMap.forEach((key, value) ->  assertTrue(key.verifyIsDisposed()));
+
+    }
+}
diff --git a/scala-package/pom.xml b/scala-package/pom.xml
index fe78a629ed2..eb3f6f0b335 100644
--- a/scala-package/pom.xml
+++ b/scala-package/pom.xml
@@ -190,8 +190,8 @@
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.3</version>
         <configuration>
-          <source>1.6</source>
-          <target>1.6</target>
+          <source>1.8</source>
+          <target>1.8</target>
           <encoding>UTF-8</encoding>
         </configuration>
       </plugin>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services