Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-648 48db19a26 -> 5b9e082be


# ignite-648: improve framework (callable)


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/5b9e082b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/5b9e082b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/5b9e082b

Branch: refs/heads/ignite-648
Commit: 5b9e082be82ba108c5c2f1d38cd2d047efc51ce4
Parents: 48db19a
Author: ashutak <ashu...@gridgain.com>
Authored: Fri Jul 3 17:09:23 2015 +0300
Committer: ashutak <ashu...@gridgain.com>
Committed: Fri Jul 3 17:09:23 2015 +0300

----------------------------------------------------------------------
 .../cache/GridCacheAbstractFullApiSelfTest.java |  54 +++----
 .../testframework/junits/GridAbstractTest.java  | 139 +++++++++++--------
 2 files changed, 99 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5b9e082b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
index 8630ea7..7db882c 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractFullApiSelfTest.java
@@ -4026,44 +4026,26 @@ public abstract class GridCacheAbstractFullApiSelfTest 
extends GridCacheAbstract
      * @return Collection of keys for which given cache is primary.
      */
     protected List<String> primaryKeysForCache(final IgniteCache<String, 
Integer> cache, final int cnt, final int startFrom) {
-        Ignite grid = cache.unwrap(Ignite.class);
+        return executeOnLocalOrRemoteJvm(cache, new TestCacheCallable<String, 
Integer, List<String>>() {
+            @Override public List<String> call(Ignite ignite, 
IgniteCache<String, Integer> cache) throws Exception {
+                List<String> found = new ArrayList<>();
 
-        if (!(cache instanceof IgniteCacheProcessProxy))
-            return primaryKeysForCache0(grid.name(), cnt, startFrom);
-        else {
-            final IgniteProcessProxy proxy = (IgniteProcessProxy)grid;
-
-            final String name = proxy.name();
-
-            return proxy.remoteCompute().call(new 
IgniteCallable<List<String>>() {
-                @Override public List<String> call() throws Exception {
-                    return primaryKeysForCache0(name, cnt, startFrom);
-                }
-            });
-        }
-    }
-
-    private List<String> primaryKeysForCache0(String name, int cnt, int 
startFrom) {
-        List<String> found = new ArrayList<>();
+                Affinity<Object> affinity = ignite.affinity(cache.getName());
 
-        IgniteEx ignite = grid(name);
+                for (int i = startFrom; i < startFrom + 100_000; i++) {
+                    String key = "key" + i;
 
-        final IgniteCache<String, Integer> cache = ignite.cache(null);
+                    if (affinity.isPrimary(ignite.cluster().localNode(), key)) 
{
+                        found.add(key);
 
-        Affinity<Object> affinity = ignite.affinity(cache.getName());
-
-        for (int i = startFrom; i < startFrom + 100_000; i++) {
-            String key = "key" + i;
-
-            if (affinity.isPrimary(ignite.cluster().localNode(), key)) {
-                found.add(key);
+                        if (found.size() == cnt)
+                            return found;
+                    }
+                }
 
-                if (found.size() == cnt)
-                    return found;
+                throw new IgniteException("Unable to find " + cnt + " keys as 
primary for cache.");
             }
-        }
-
-        throw new IgniteException("Unable to find " + cnt + " keys as primary 
for cache.");
+        });
     }
 
     /**
@@ -4255,14 +4237,14 @@ public abstract class GridCacheAbstractFullApiSelfTest 
extends GridCacheAbstract
      */
     private void checkIteratorsCleared() {
         for (int j = 0; j < gridCount(); j++) {
-            runOnLocalOrRemoteJvm(j, new IndexSerializableJob() {
-                @Override public void run(int i) throws Exception {
-                    GridCacheQueryManager queries = context(i).queries();
+            executeOnLocalOrRemoteJvm(j, new TestIgniteIdxRunnable() {
+                @Override public void run(int idx) throws Exception {
+                    GridCacheQueryManager queries = context(idx).queries();
 
                     Map map = GridTestUtils.getFieldValue(queries, 
GridCacheQueryManager.class, "qryIters");
 
                     for (Object obj : map.values())
-                        assertEquals("Iterators not removed for grid " + i, 0, 
((Map)obj).size());
+                        assertEquals("Iterators not removed for grid " + idx, 
0, ((Map)obj).size());
                 }
             });
         }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5b9e082b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
 
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
index 94fa248..6fb0b2b 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testframework/junits/GridAbstractTest.java
@@ -1368,12 +1368,9 @@ public abstract class GridAbstractTest extends TestCase {
      * @return <code>True</code> to run nodes in separate jvms.
      * @see IgniteNodeRunner
      * @see IgniteProcessProxy
-     * @see #runRemotely(int, IndexSerializableJob)
-     * @see #runRemotely(IgniteProcessProxy, IgniteSerializableJob)
-     * @see #runRemotely(IgniteCacheProcessProxy, CacheSerializiableJob)
-     * @see #runOnLocalOrRemoteJvm(int, IndexSerializableJob)
-     * @see #runOnLocalOrRemoteJvm(Ignite, IgniteSerializableJob)
-     * @see #runOnLocalOrRemoteJvm(IgniteCache, CacheSerializiableJob)
+     * @see #executeOnLocalOrRemoteJvm(int, TestIgniteIdxCallable)
+     * @see #executeOnLocalOrRemoteJvm(Ignite, TestIgniteCallable)
+     * @see #executeOnLocalOrRemoteJvm(IgniteCache, TestCacheCallable)
      */
     protected boolean isMultiJvm() {
         return false;
@@ -1424,18 +1421,18 @@ public abstract class GridAbstractTest extends TestCase 
{
      * @param idx Grid index.
      * @param job Job.
      */
-    public void runOnLocalOrRemoteJvm(final int idx, final 
IndexSerializableJob job) {
+    public <R> R executeOnLocalOrRemoteJvm(final int idx, final 
TestIgniteIdxCallable<R> job) {
         IgniteEx ignite = grid(idx);
 
         if (!isMultiJvmObject(ignite))
             try {
-                job.run(idx);
+                return job.call(idx);
             }
             catch (Exception e) {
                 throw new IgniteException(e);
             }
         else
-            runRemotely(idx, job);
+            return executeRemotely(idx, job);
     }
 
     /**
@@ -1444,36 +1441,36 @@ public abstract class GridAbstractTest extends TestCase 
{
      * @param ignite Ignite.
      * @param job Job.
      */
-    public void runOnLocalOrRemoteJvm(Ignite ignite, final 
IgniteSerializableJob job) {
+    public <R> R executeOnLocalOrRemoteJvm(Ignite ignite, final 
TestIgniteCallable<R> job) {
         if (!isMultiJvmObject(ignite))
             try {
-                job.run(ignite);
+                return job.call(ignite);
             }
             catch (Exception e) {
                 throw new IgniteException(e);
             }
         else
-            runRemotely((IgniteProcessProxy)ignite, job);
+            return executeRemotely((IgniteProcessProxy)ignite, job);
     }
 
     /**
-     * Runs job on local jvm or on remote jvm in multi jvm case.
+     * Calls job on local jvm or on remote jvm in multi jvm case.
      *
      * @param cache Cache.
      * @param job Job.
      */
-    public <K,V> void runOnLocalOrRemoteJvm(IgniteCache<K,V> cache, 
CacheSerializiableJob<K,V> job) {
+    public <K,V,R> R executeOnLocalOrRemoteJvm(IgniteCache<K,V> cache, 
TestCacheCallable<K,V,R> job) {
         Ignite ignite = cache.unwrap(Ignite.class);
 
         if (!isMultiJvmObject(ignite))
             try {
-                job.run(ignite, cache);
+                return job.call(ignite, cache);
             }
             catch (Exception e) {
                 throw new IgniteException(e);
             }
         else
-            runRemotely((IgniteCacheProcessProxy<K, V>)cache, job);
+            return executeRemotely((IgniteCacheProcessProxy<K, V>)cache, job);
     }
 
     /**
@@ -1482,7 +1479,7 @@ public abstract class GridAbstractTest extends TestCase {
      * @param idx Grid index.
      * @param job Job.
      */
-    public void runRemotely(final int idx, final IndexSerializableJob job) {
+    public <R> R executeRemotely(final int idx, final TestIgniteIdxCallable<R> 
job) {
         IgniteEx ignite = grid(idx);
 
         if (!isMultiJvmObject(ignite))
@@ -1490,14 +1487,9 @@ public abstract class GridAbstractTest extends TestCase {
 
         IgniteProcessProxy proxy = (IgniteProcessProxy)ignite;
 
-        proxy.remoteCompute().run(new CAX() {
-            @Override public void applyx() throws IgniteCheckedException {
-                try {
-                    job.run(idx);
-                }
-                catch (Exception e) {
-                    throw new IgniteCheckedException(e);
-                }
+        return proxy.remoteCompute().call(new IgniteCallable<R>() {
+            @Override public R call() throws Exception {
+                return job.call(idx);
             }
         });
     }
@@ -1508,19 +1500,14 @@ public abstract class GridAbstractTest extends TestCase 
{
      * @param proxy Ignite.
      * @param job Job.
      */
-    public static void runRemotely(IgniteProcessProxy proxy, final 
IgniteSerializableJob job) {
+    public static <R> R executeRemotely(IgniteProcessProxy proxy, final 
TestIgniteCallable<R> job) {
         final UUID id = proxy.getId();
 
-        proxy.remoteCompute().run(new CAX() {
-            @Override public void applyx() throws IgniteCheckedException {
+        return proxy.remoteCompute().call(new IgniteCallable<R>() {
+            @Override public R call() throws Exception {
                 Ignite ignite = Ignition.ignite(id);
 
-                try {
-                    job.run(ignite);
-                }
-                catch (Exception e) {
-                    throw new IgniteCheckedException(e);
-                }
+                return job.call(ignite);
             }
         });
     }
@@ -1531,23 +1518,19 @@ public abstract class GridAbstractTest extends TestCase 
{
      * @param cache Cache.
      * @param job Job.
      */
-    public static <K, V> void runRemotely(IgniteCacheProcessProxy<K, V> cache, 
final CacheSerializiableJob<K,V> job) {
+    public static <K, V, R> R executeRemotely(IgniteCacheProcessProxy<K, V> 
cache,
+        final TestCacheCallable<K, V, R> job) {
         IgniteProcessProxy proxy = 
(IgniteProcessProxy)cache.unwrap(Ignite.class);
 
         final UUID id = proxy.getId();
         final String cacheName = cache.getName();
 
-        proxy.remoteCompute().run(new CAX() {
-            @Override public void applyx() throws IgniteCheckedException {
+        return proxy.remoteCompute().call(new IgniteCallable<R>() {
+            @Override public R call() throws Exception {
                 Ignite ignite = Ignition.ignite(id);
                 IgniteCache<K,V> cache = ignite.cache(cacheName);
 
-                try {
-                    job.run(ignite, cache);
-                }
-                catch (Exception e) {
-                    throw new IgniteCheckedException(e);
-                }
+                return job.call(ignite, cache);
             }
         });
     }
@@ -1866,34 +1849,74 @@ public abstract class GridAbstractTest extends TestCase 
{
         }
     }
 
-    /**
-     * Serializable runnable.
-     */
-    public static interface IgniteSerializableJob extends Serializable {
+    /** */
+    public static interface TestIgniteCallable<R> extends Serializable {
         /**
          * @param ignite Ignite.
          */
-        void run(Ignite ignite) throws Exception;
+        R call(Ignite ignite) throws Exception;
     }
 
-    /**
-     * Serializiable runnable.
-     */
-    public static interface IndexSerializableJob extends Serializable {
+    /** */
+    public abstract static class TestIgniteRunnable implements 
TestIgniteCallable<Object> {
+        /** {@inheritDoc} */
+        @Override public Object call(Ignite ignite) throws Exception {
+            run(ignite);
+
+            return null;
+        }
+
+        /**
+         * @param ignite Ignite.
+         */
+        public abstract void run(Ignite ignite) throws Exception;
+    }
+
+    /** */
+    public static interface TestIgniteIdxCallable<R> extends Serializable {
         /**
          * @param idx Grid index.
          */
-        void run(int idx) throws Exception;
+        R call(int idx) throws Exception;
     }
 
-    /**
-     * Serializiable runnable.
-     */
-    public static interface CacheSerializiableJob<K, V> extends Serializable {
+    /** */
+    public abstract static class TestIgniteIdxRunnable implements 
TestIgniteIdxCallable<Object> {
+        /** {@inheritDoc} */
+        @Override public Object call(int idx) throws Exception {
+            run(idx);
+
+            return null;
+        }
+
+        /**
+         * @param idx Index.
+         */
+        public abstract void run(int idx) throws Exception;
+    }
+
+    /** */
+    public static interface TestCacheCallable<K, V, R> extends Serializable {
+        /**
+         * @param ignite Ignite.
+         * @param cache Cache.
+         */
+        R call(Ignite ignite, IgniteCache<K, V> cache) throws Exception;
+    }
+
+    /** */
+    public abstract static class TestTestCacheRunnable<K, V> implements 
TestCacheCallable<K, V, Object> {
+        /** {@inheritDoc} */
+        @Override public Object call(Ignite ignite, IgniteCache cache) throws 
Exception {
+            run(ignite, cache);
+
+            return null;
+        }
+
         /**
          * @param ignite Ignite.
          * @param cache Cache.
          */
-        void run(Ignite ignite, IgniteCache<K, V> cache) throws Exception;
+        public abstract void run(Ignite ignite, IgniteCache<K, V> cache) 
throws Exception;
     }
 }

Reply via email to