Skip to content

Commit b3b7564

Browse files
committed
Add module.destroy() to all tests to prevent native resource leaks
This commit was authored with the help of Claude.
1 parent 8fe6bec commit b3b7564

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/ModuleInstrumentationTest.kt

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,45 @@ class ModuleInstrumentationTest {
4343
@Throws(IOException::class, URISyntaxException::class)
4444
fun testModuleLoadAndForward() {
4545
val module = Module.load(getTestFilePath(TEST_FILE_NAME))
46-
47-
val results = module.forward(EValue.from(dummyInput()))
48-
Assert.assertTrue(results[0].isTensor)
46+
try {
47+
val results = module.forward(EValue.from(dummyInput()))
48+
Assert.assertTrue(results[0].isTensor)
49+
} finally {
50+
module.destroy()
51+
}
4952
}
5053

5154
@Test
5255
@Throws(IOException::class, URISyntaxException::class)
5356
fun testMethodMetadata() {
5457
val module = Module.load(getTestFilePath(TEST_FILE_NAME))
58+
module.destroy()
5559
}
5660

5761
@Test
5862
@Throws(IOException::class)
5963
fun testModuleLoadMethodAndForward() {
6064
val module = Module.load(getTestFilePath(TEST_FILE_NAME))
65+
try {
66+
module.loadMethod(FORWARD_METHOD)
6167

62-
module.loadMethod(FORWARD_METHOD)
63-
64-
val results = module.forward(EValue.from(dummyInput()))
65-
Assert.assertTrue(results[0].isTensor)
68+
val results = module.forward(EValue.from(dummyInput()))
69+
Assert.assertTrue(results[0].isTensor)
70+
} finally {
71+
module.destroy()
72+
}
6673
}
6774

6875
@Test
6976
@Throws(IOException::class)
7077
fun testModuleLoadForwardExplicit() {
7178
val module = Module.load(getTestFilePath(TEST_FILE_NAME))
72-
73-
val results = module.execute(FORWARD_METHOD, EValue.from(dummyInput()))
74-
Assert.assertTrue(results[0].isTensor)
79+
try {
80+
val results = module.execute(FORWARD_METHOD, EValue.from(dummyInput()))
81+
Assert.assertTrue(results[0].isTensor)
82+
} finally {
83+
module.destroy()
84+
}
7585
}
7686

7787
@Test(expected = RuntimeException::class)
@@ -84,15 +94,18 @@ class ModuleInstrumentationTest {
8494
@Throws(IOException::class)
8595
fun testModuleLoadMethodNonExistantMethod() {
8696
val module = Module.load(getTestFilePath(TEST_FILE_NAME))
87-
88-
val exception =
89-
Assert.assertThrows(ExecutorchRuntimeException::class.java) {
90-
module.loadMethod(NONE_METHOD)
91-
}
92-
Assert.assertEquals(
93-
ExecutorchRuntimeException.INVALID_ARGUMENT,
94-
exception.getErrorCode(),
95-
)
97+
try {
98+
val exception =
99+
Assert.assertThrows(ExecutorchRuntimeException::class.java) {
100+
module.loadMethod(NONE_METHOD)
101+
}
102+
Assert.assertEquals(
103+
ExecutorchRuntimeException.INVALID_ARGUMENT,
104+
exception.getErrorCode(),
105+
)
106+
} finally {
107+
module.destroy()
108+
}
96109
}
97110

98111
@Test(expected = RuntimeException::class)
@@ -155,6 +168,7 @@ class ModuleInstrumentationTest {
155168
}
156169

157170
Assert.assertEquals(numThreads.toLong(), completed.get().toLong())
171+
module.destroy()
158172
}
159173

160174
companion object {

0 commit comments

Comments
 (0)