TestStaticFieldIsolation.java 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/*
 * @test
 * @summary Test isolation of various static fields
 * @library /lib/testlibrary
 * @run main/othervm -XX:+UseG1GC -XX:+MultiTenant -XX:+TenantDataIsolation TestStaticFieldIsolation
 */

import static jdk.testlibrary.Asserts.*;
import java.lang.reflect.Field;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicInteger;
import javax.management.MBeanServerFactory;
import com.alibaba.tenant.TenantConfiguration;
import com.alibaba.tenant.TenantContainer;
import com.alibaba.tenant.TenantException;


public class TestStaticFieldIsolation {

    public static void main(String[] args) throws TenantException {
        TestStaticFieldIsolation test = new TestStaticFieldIsolation();
        test.testIsolation_java_lang_ResourceBundle_cacheList();
        test.testIsolation_java_sql_DriverManager_registeredDrivers();
        test.testIsolation_javax_management_MBeanServerFactory_mBeanServerList();
        test.testIsolation_java_lang_Class_classValueMap();
    }

    private void testIsolation_java_lang_ResourceBundle_cacheList() throws TenantException {
        Runnable task = () -> {
            try {
                ResourceBundle.getBundle("NON_EXIST");
            } catch (Throwable t) { /* ignore */ }
        };
        testStaticFieldIsolation(ResourceBundle.class, task, "cacheList");
    }

    private void testIsolation_javax_management_MBeanServerFactory_mBeanServerList() throws TenantException {
        Runnable task = () -> {
            try {
                MBeanServerFactory.findMBeanServer(null);
            } catch (Throwable t) { /* ignore */ }
        };
        testStaticFieldIsolation(MBeanServerFactory.class, task, "mBeanServerList");
    }

    private void testIsolation_java_sql_DriverManager_registeredDrivers() throws TenantException {
        Runnable task = () -> {
            try {
                java.sql.DriverManager.getDrivers();
            } catch (Throwable t) { /* ignore */ }
        };
        testStaticFieldIsolation(java.sql.DriverManager.class, task, "registeredDrivers");
    }

    class TestClass {
        int val;
    }

    private void testIsolation_java_lang_Class_classValueMap() throws TenantException {
        final AtomicInteger atomicInteger = new AtomicInteger(0);

        ClassValue<TestClass> value = new ClassValue<TestClass>() {
            @Override protected TestClass computeValue(Class<?> type) {
                TestClass obj = new TestClass();
                obj.val = atomicInteger.getAndIncrement();
                return obj;
            }
        };

        TestClass tc = value.get(TestClass.class);
        assertEquals(tc.val, 0);
        TestClass tc2 = value.get(TestClass.class);
        assertTrue(tc == tc2);

        TestClass[] receivers = new TestClass[2];
        TenantContainer tenant = createSimpleTenant();
        tenant.run(() -> {
            receivers[0] = value.get(TestClass.class);
        });
        assertEquals(receivers[0].val, 1);

        tenant.run(() -> {
            receivers[1] = value.get(TestClass.class);
        });
        assertEquals(receivers[1].val, 1);
        assertTrue(receivers[1] == receivers[0]);
        assertTrue(receivers[0] != tc);


    }

    /**
     * test isolation of {@code classKay}'s static field {@code fieldName}
     * @param classKey          The class contains target static field
     * @param tenantTask        Task to trigger the static filed isolation per tenant,
     *                          which is stored in TenantContainer.tenantData
     * @param fieldName         Field name
     * @throws TenantException
     */
    private static void testStaticFieldIsolation(Class classKey, Runnable tenantTask, String fieldName)
            throws TenantException {
        TenantContainer tenant = createSimpleTenant();

        assertNull(tenant.getFieldValue(classKey, fieldName));

        tenant.run(tenantTask);

        Object isolatedFieldValue = tenant.getFieldValue(classKey, fieldName);
        assertNotNull(isolatedFieldValue);

        try {
            Field field = classKey.getDeclaredField(fieldName);
            field.setAccessible(true);
            Object rootValue = field.get(null);

            assertTrue(rootValue != isolatedFieldValue);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
            fail();
        }

        tenant.destroy();
    }

    // convenient static method to create a simple {@code TenantContainer} object
    private static TenantContainer createSimpleTenant() {
        TenantConfiguration config = new TenantConfiguration(1024, 64 * 1024 * 1024);
        return TenantContainer.create(config);
    }
}