Search in sources :

Example 6 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class GridMarshallerAbstractTest method testExternalClassesMarshalling.

/**
     * Tests marshal classes
     *
     * @throws Exception If test failed.
     */
public void testExternalClassesMarshalling() throws Exception {
    ClassLoader tstClsLdr = new GridTestClassLoader(Collections.singletonMap("org/apache/ignite/p2p/p2p.properties", "resource=loaded"), getClass().getClassLoader(), GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
    ComputeTask<?, ?> inTask = (ComputeTask<?, ?>) tstClsLdr.loadClass(GridP2PTestTask.class.getName()).newInstance();
    byte[] buf = marsh.marshal(inTask);
    ComputeTask<?, ?> outTask = marsh.unmarshal(buf, tstClsLdr);
    assert inTask != outTask;
    assert inTask.getClass().equals(outTask.getClass());
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridP2PTestTask(org.apache.ignite.p2p.GridP2PTestTask) GridP2PTestJob(org.apache.ignite.p2p.GridP2PTestJob)

Example 7 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execImplicitDeployLocally.

/**
     * @param byCls If {@code true} than executes task by Class.
     * @param byTask If {@code true} than executes task instance.
     * @param byName If {@code true} than executes task by class name.
     * @throws Exception If test failed.
     */
@SuppressWarnings("unchecked")
private void execImplicitDeployLocally(boolean byCls, boolean byTask, boolean byName) throws Exception {
    Ignite ignite = null;
    try {
        ignite = startGrid();
        // First task class loader.
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        // Second task class loader
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        // The same name but different classes/ class loaders.
        Class<? extends ComputeTask<String, Integer>> taskCls1 = (Class<? extends ComputeTask<String, Integer>>) ldr1.loadClass(GridDeploymentResourceTestTask.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls2 = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        if (byCls) {
            Integer res1 = ignite.compute().execute(taskCls1, null);
            Integer res2 = ignite.compute().execute(taskCls2, null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
        if (byTask) {
            Integer res1 = ignite.compute().execute(taskCls1.newInstance(), null);
            Integer res2 = ignite.compute().execute(taskCls2.newInstance(), null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
        if (byName) {
            ignite.compute().localDeployTask(taskCls1, ldr1);
            Integer res1 = (Integer) ignite.compute().execute(taskCls1.getName(), null);
            ignite.compute().localDeployTask(taskCls2, ldr2);
            Integer res2 = (Integer) ignite.compute().execute(taskCls2.getName(), null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
    } finally {
        stopGrid(ignite);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite)

Example 8 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execExplicitDeployLocally.

/**
     * @param byCls If {@code true} than executes task by Class.
     * @param byTask If {@code true} than executes task instance.
     * @param byName If {@code true} than executes task by class name.
     * @throws Exception If test failed.
     */
@SuppressWarnings("unchecked")
private void execExplicitDeployLocally(boolean byCls, boolean byTask, boolean byName) throws Exception {
    Ignite ignite = null;
    try {
        ignite = startGrid();
        // Explicit Deployment. Task execution should return 0.
        // Say resource class loader - different to task one.
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader());
        // Assume that users task and job were loaded with this class loader
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        info("Loader1: " + ldr1);
        info("Loader2: " + ldr2);
        Class<? extends ComputeTask<String, Integer>> taskCls = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        // Check auto-deploy. It should pick up resource class loader.
        if (byCls) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = ignite.compute().execute(taskCls, null);
            assert res != null;
            assert res == 2 : "Invalid response: " + res;
        }
        if (byTask) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = ignite.compute().execute(taskCls.newInstance(), null);
            assert res != null;
            assert res == 2 : "Invalid response: " + res;
        }
        if (byName) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = (Integer) ignite.compute().execute(taskCls.getName(), null);
            assert res != null;
            assert res == 1 : "Invalid response: " + res;
        }
    } finally {
        stopGrid(ignite);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite)

Example 9 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execImplicitDeployP2P.

/**
     * @param byCls If {@code true} than executes task by Class.
     * @param byTask If {@code true} than executes task instance.
     * @param byName If {@code true} than executes task by class name.
     * @throws Exception If test failed.
     */
@SuppressWarnings("unchecked")
private void execImplicitDeployP2P(boolean byCls, boolean byTask, boolean byName) throws Exception {
    Ignite ignite1 = null;
    Ignite ignite2 = null;
    try {
        ignite1 = startGrid(1);
        ignite2 = startGrid(2);
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls1 = (Class<? extends ComputeTask<String, Integer>>) ldr1.loadClass(GridDeploymentResourceTestTask.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls2 = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        if (byCls) {
            Integer res1 = ignite1.compute().execute(taskCls1, null);
            Integer res2 = ignite1.compute().execute(taskCls2, null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
        if (byTask) {
            Integer res1 = ignite1.compute().execute(taskCls1.newInstance(), null);
            Integer res2 = ignite1.compute().execute(taskCls2.newInstance(), null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
        if (byName) {
            ignite1.compute().localDeployTask(taskCls1, ldr1);
            Integer res1 = (Integer) ignite1.compute().execute(taskCls1.getName(), null);
            ignite1.compute().localDeployTask(taskCls2, ldr2);
            Integer res2 = (Integer) ignite1.compute().execute(taskCls2.getName(), null);
            assert res1 != null;
            assert res2 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
    } finally {
        stopGrid(ignite1);
        stopGrid(ignite2);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite)

Example 10 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class P2PGridifySelfTest method executeGridifyResource.

/**
     * Note that this method sends instance of test class to remote node.
     * Be sure that this instance does not have none-serializable fields or references
     * to the objects that could not be instantiated like class loaders and so on.
     *
     * @param res Result.
     * @return The same value as parameter has.
     */
@Gridify(igniteInstanceName = "org.apache.ignite.p2p.GridP2PGridifySelfTest1")
public Integer executeGridifyResource(int res) {
    String path = "org/apache/ignite/p2p/p2p.properties";
    GridTestClassLoader tstClsLdr = new GridTestClassLoader(GridP2PTestTask.class.getName(), GridP2PTestJob.class.getName());
    // Test property file load.
    byte[] bytes = new byte[20];
    try (InputStream in = tstClsLdr.getResourceAsStream(path)) {
        if (in == null) {
            System.out.println("Resource could not be loaded: " + path);
            return -2;
        }
        in.read(bytes);
    } catch (IOException e) {
        System.out.println("Failed to read from resource stream: " + e.getMessage());
        return -3;
    }
    String rsrcVal = new String(bytes).trim();
    System.out.println("Remote resource content is : " + rsrcVal);
    if (!rsrcVal.equals("resource=loaded")) {
        System.out.println("Invalid loaded resource value: " + rsrcVal);
        return -4;
    }
    return res;
}
Also used : InputStream(java.io.InputStream) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) IOException(java.io.IOException) Gridify(org.apache.ignite.compute.gridify.Gridify)

Aggregations

GridTestClassLoader (org.apache.ignite.testframework.GridTestClassLoader)15 ComputeTask (org.apache.ignite.compute.ComputeTask)13 Ignite (org.apache.ignite.Ignite)11 CountDownLatch (java.util.concurrent.CountDownLatch)2 ComputeTaskFuture (org.apache.ignite.compute.ComputeTaskFuture)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URLClassLoader (java.net.URLClassLoader)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 Gridify (org.apache.ignite.compute.gridify.Gridify)1 Event (org.apache.ignite.events.Event)1 GridPlainRunnable (org.apache.ignite.internal.util.lang.GridPlainRunnable)1 GridP2PTestJob (org.apache.ignite.p2p.GridP2PTestJob)1 GridP2PTestTask (org.apache.ignite.p2p.GridP2PTestTask)1 LocalDeploymentSpi (org.apache.ignite.spi.deployment.local.LocalDeploymentSpi)1