| 1 | # ***************************************************************************** |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | # See NOTICE file for details. |
| 16 | # |
| 17 | # ***************************************************************************** |
| 18 | import sys |
| 19 | import _jpype |
| 20 | import jpype |
| 21 | from jpype import JImplements, JOverride |
| 22 | from jpype.types import * |
| 23 | import common |
| 24 | |
| 25 | |
| 26 | class ReferenceQueueTestCase(common.JPypeTestCase): |
| 27 | def setUp(self): |
| 28 | common.JPypeTestCase.setUp(self) |
| 29 | self.refqueue = jpype.JClass( |
| 30 | 'org.jpype.ref.JPypeReferenceQueue').getInstance() |
| 31 | |
| 32 | def testAccess(self): |
| 33 | # Make sure we can get the instance |
| 34 | self.assertTrue(self.refqueue != None) |
| 35 | |
| 36 | def testRunning(self): |
| 37 | # Get the queue instance |
| 38 | self.assertTrue(self.refqueue.isRunning()) |
| 39 | |
| 40 | def testRefs(self): |
| 41 | # This routine will exercise each of the clean up paths once |
| 42 | fixture = JClass("jpype.common.Fixture")() |
| 43 | |
| 44 | def f(): |
| 45 | # Create a proxy to test the proxy path |
| 46 | @JImplements("java.util.function.Supplier") |
| 47 | class MySupplier(object): |
| 48 | @JOverride |
| 49 | def get(self): |
| 50 | # Send a Python exc to trigger Python ref path |
| 51 | raise RuntimeError("foo") |
| 52 | try: |
| 53 | u = MySupplier() |
| 54 | fixture.callSupplier(u) |
| 55 | except RuntimeError as ex: |
| 56 | pass |
| 57 | f() |
| 58 | |
| 59 | # Force a direct byffer and then trash it |
| 60 | b = bytearray([1, 2, 3]) |
| 61 | _jpype.convertToDirectBuffer(b, False) |
| 62 | |
| 63 | # Then force a GC to clean it up |
| 64 | jpype.java.lang.System.gc() |
| 65 | |
| 66 | # We can't check the results here as the GC may chose not |
| 67 | # to run which would trigger a failure |