Skip to content

Commit

Permalink
Added unit tests for Resource Scope in Java (apache#12955)
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushghai authored and lanking520 committed Oct 24, 2018
1 parent b78faf6 commit a46aeee
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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()));

}
}
4 changes: 2 additions & 2 deletions scala-package/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit a46aeee

Please sign in to comment.