|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tomcat.jni; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tests for APR memory pool management. |
| 25 | + */ |
| 26 | +public class TestPool extends BaseTest { |
| 27 | + |
| 28 | + @Before |
| 29 | + public void checkLibrary() { |
| 30 | + requireLibrary(); // Fail all tests if library not loaded |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testCreateRootPool() { |
| 35 | + long pool = Pool.create(0); |
| 36 | + Assert.assertNotEquals("Root pool should be created", 0, pool); |
| 37 | + Pool.destroy(pool); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testCreateChildPool() { |
| 42 | + long parentPool = Pool.create(0); |
| 43 | + Assert.assertNotEquals("Parent pool should be created", 0, parentPool); |
| 44 | + |
| 45 | + long childPool = Pool.create(parentPool); |
| 46 | + Assert.assertNotEquals("Child pool should be created", 0, childPool); |
| 47 | + Assert.assertNotEquals("Child should differ from parent", parentPool, childPool); |
| 48 | + |
| 49 | + // Destroy child first, then parent |
| 50 | + Pool.destroy(childPool); |
| 51 | + Pool.destroy(parentPool); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testMultiplePoolCreation() { |
| 56 | + // Create multiple independent pools |
| 57 | + long pool1 = Pool.create(0); |
| 58 | + long pool2 = Pool.create(0); |
| 59 | + long pool3 = Pool.create(0); |
| 60 | + |
| 61 | + Assert.assertNotEquals("Pool 1 should be created", 0, pool1); |
| 62 | + Assert.assertNotEquals("Pool 2 should be created", 0, pool2); |
| 63 | + Assert.assertNotEquals("Pool 3 should be created", 0, pool3); |
| 64 | + |
| 65 | + // All pools should be different |
| 66 | + Assert.assertNotEquals("Pool 1 and 2 should differ", pool1, pool2); |
| 67 | + Assert.assertNotEquals("Pool 2 and 3 should differ", pool2, pool3); |
| 68 | + Assert.assertNotEquals("Pool 1 and 3 should differ", pool1, pool3); |
| 69 | + |
| 70 | + // Clean up in any order (they're independent) |
| 71 | + Pool.destroy(pool2); |
| 72 | + Pool.destroy(pool1); |
| 73 | + Pool.destroy(pool3); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testPoolHierarchy() { |
| 78 | + // Test a 3-level pool hierarchy |
| 79 | + long rootPool = Pool.create(0); |
| 80 | + long childPool = Pool.create(rootPool); |
| 81 | + long grandchildPool = Pool.create(childPool); |
| 82 | + |
| 83 | + Assert.assertNotEquals("Root pool should be created", 0, rootPool); |
| 84 | + Assert.assertNotEquals("Child pool should be created", 0, childPool); |
| 85 | + Assert.assertNotEquals("Grandchild pool should be created", 0, grandchildPool); |
| 86 | + |
| 87 | + // All should be different |
| 88 | + Assert.assertNotEquals("Root and child should differ", rootPool, childPool); |
| 89 | + Assert.assertNotEquals("Child and grandchild should differ", childPool, grandchildPool); |
| 90 | + |
| 91 | + // Destroy from bottom up |
| 92 | + Pool.destroy(grandchildPool); |
| 93 | + Pool.destroy(childPool); |
| 94 | + Pool.destroy(rootPool); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testChildPoolDestroyedBeforeParent() { |
| 99 | + // Test that child pools can be safely destroyed before parent |
| 100 | + long parentPool = Pool.create(0); |
| 101 | + long childPool = Pool.create(parentPool); |
| 102 | + |
| 103 | + Assert.assertNotEquals("Parent pool should be created", 0, parentPool); |
| 104 | + Assert.assertNotEquals("Child pool should be created", 0, childPool); |
| 105 | + |
| 106 | + // Destroy child first - this is safe and proper cleanup |
| 107 | + Pool.destroy(childPool); |
| 108 | + |
| 109 | + // Parent can still be destroyed after child |
| 110 | + Pool.destroy(parentPool); |
| 111 | + } |
| 112 | +} |
0 commit comments