Repository: incubator-ignite Updated Branches: refs/heads/ignite-961-promise 0479f5b42 -> 341f30310
#ignite-961: rewrite compute-tests. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/88bb97d6 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/88bb97d6 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/88bb97d6 Branch: refs/heads/ignite-961-promise Commit: 88bb97d69aef4adc7f327b5c9af25b3062efa1d9 Parents: 0479f5b Author: ivasilinets <ivasilin...@gridgain.com> Authored: Tue Jul 14 13:51:04 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Tue Jul 14 13:51:04 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/compute.js | 36 +- modules/nodejs/src/test/js/test-compute.js | 814 ++++++++++++------------ 2 files changed, 415 insertions(+), 435 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/88bb97d6/modules/nodejs/src/main/js/compute.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/compute.js b/modules/nodejs/src/main/js/compute.js index 5c28418..53cd11c 100644 --- a/modules/nodejs/src/main/js/compute.js +++ b/modules/nodejs/src/main/js/compute.js @@ -31,11 +31,10 @@ function Compute(server) { * @this {Compute} * @param job Function * @param args Function arguments - * @param {onGet} callback Callback */ -Compute.prototype.run = function(job, args, callback) { - this._server.runCommand(new Command("runscript").addParam("func", job). - setPostData(JSON.stringify({"arg" : args})), callback); +Compute.prototype.run = function(job, args) { + return this._createPromise(new Command("runscript").addParam("func", job). + setPostData(JSON.stringify({"arg" : args}))); } /** @@ -46,11 +45,10 @@ Compute.prototype.run = function(job, args, callback) { * @param {string|number|JSONObject} key Key. * @param job Function * @param args Function arguments - * @param {onGet} callback Callback */ -Compute.prototype.affinityRun = function(cacheName, key, job, args, callback) { - this._server.runCommand(new Command("affrun").addParam("func", job).addParam("cacheName", cacheName). - setPostData(JSON.stringify({"arg" : args, "key" : key})), callback); +Compute.prototype.affinityRun = function(cacheName, key, job, args) { + return this._createPromise(new Command("affrun").addParam("func", job).addParam("cacheName", cacheName). + setPostData(JSON.stringify({"arg" : args, "key" : key}))); } /** @@ -61,14 +59,26 @@ Compute.prototype.affinityRun = function(cacheName, key, job, args, callback) { * @param {onGet} callback Callback */ Compute.prototype.mapReduce = function(map, reduce, arg, callback) { - var command = new Command("excmapreduce"); - - command.addParam("map", map).addParam("reduce", reduce); - command.setPostData(JSON.stringify({"arg" : arg})); + var cmd = new Command("excmapreduce").addParam("map", map).addParam("reduce", reduce). + setPostData(JSON.stringify({"arg" : arg})); - this._server.runCommand(command, callback); + return this._createPromise(cmd); } + +Compute.prototype._createPromise = function(cmd) { + var server = this._server; + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if (err != null) { + reject(err); + } + else { + resolve(res); + } + }); + }); +} /** * @name EmitFunction * @function http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/88bb97d6/modules/nodejs/src/test/js/test-compute.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-compute.js b/modules/nodejs/src/test/js/test-compute.js index 5cdc3e0..600f11d 100644 --- a/modules/nodejs/src/test/js/test-compute.js +++ b/modules/nodejs/src/test/js/test-compute.js @@ -23,28 +23,222 @@ var CacheEntry = Ignite.CacheEntry; var assert = require("assert"); testComputeRunScript = function() { - TestUtils.startIgniteNode(onStart.bind(null, computeRunScript)); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(getEntries()).then(function(res) { + var comp = ignite.compute(); + + var f = function (args) { + print("!!!!" + args + " " + ignite.name()); + return args + " " + ignite.name(); + } + + return comp.run(f, "GridGain"); + }).then(function(res) { + assert(res.indexOf("NodeJsComputeSelfTest") !== -1, "Incorrect result message. [mes=" + res + "]."); + assert(res.indexOf("GridGain") !== -1, "Incorrect result message. [mes=" + res + "]."); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeExecute = function() { - TestUtils.startIgniteNode(computeExecute); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(getEntries()).then(function(res) { + var map = function(nodes, arg) { + var words = arg.split(" "); + + for (var i = 0; i < words.length; i++) { + var f = function (word) { + print(">>> Printing " + word); + + return word.length; + }; + + emit(f, words[i], nodes[i % nodes.length]); + } + }; + + var reduce = function(results) { + var sum = 0; + + for (var i = 0; i < results.length; ++i) { + sum += results[i]; + } + + return sum; + }; + + return ignite.compute().mapReduce(map, reduce, "Hi Alice"); + }).then(function(res) { + assert.equal(res, 7); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeAllNodeExecute = function() { - TestUtils.startIgniteNode(computeAllNodeExecute); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(getEntries()).then(function(res) { + var map = function(nodes, arg) { + for (var i = 0; i < nodes.length; i++) { + var f = function (node) { + print(">>> Printing " + node.id().toString()); + + return ""; + }; + + emit(f, nodes[i % nodes.length], nodes[i % nodes.length]); + } + }; + + var reduce = function(results) {}; + + return ignite.compute().mapReduce(map, reduce, ""); + }).then(function(res) { + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeCacheSizeExecute = function() { - TestUtils.startIgniteNode(computeCacheSizeExecute); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put("key", "val").then(function(res) { + var map = function(nodes, arg) { + for (var i = 0; i < nodes.length; i++) { + var f = function (args) { + print("!!!!!Node id " + ignite.localNode().id()); + + return ignite.cache("mycache").localSize(); + }; + + emit(f, [1, 2], nodes[i]); + } + }; + + var reduce = function(results) { + var sum = 0; + + for (var i = 0; i < results.length; i++) { + sum += results[i]; + } + + return sum; + }; + + return ignite.compute().mapReduce(map, reduce, ""); + }).then(function(res) { + console.log("Result=" + res); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeCacheExecute = function() { - TestUtils.startIgniteNode(computeCacheExecute); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + entries = []; + + var key1 = {"name" : "Ann"}; + var key2 = {"name" : "Paul"}; + var val1 = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"age" : 13, "books" : ["1", "Book"]}; + + entries.push(new CacheEntry(key1, val1)); + entries.push(new CacheEntry(key2, val2)); + + cache.putAll(entries).then(function(res) { + var map = function(nodes, args) { + for (var i = 0; i < 1; i++) { + var f = function (args1) { + ignite.cache("mycache").put({"1": "1"}, 2); + + var val = ignite.cache("mycache").get({"1": "1"}); + + if (val !== 2) { + throw "Incorrect return val [expected=2, val=" + val + "]"; + } + + var val1 = ignite.cache("mycache").get(args1.get(0)); + + if (val1["age"] !== 12) { + throw "Incorrect age [expected=12, val=" + val + "]"; + } + + print("BOOKS=" + val1.books); + + if (val1.books.length !== 2) { + throw "Incorrect books length [expected=2, val=" + + val1.books.length + "]"; + } + + if (val1.books[0] !== "1") { + throw "Incorrect books value [expected=1, val=" + + val1.books[0] + "]"; + } + if (val1.books[1] !== "Book") { + throw "Incorrect books value [expected=Book, val=" + + val1.books[1] + "]"; + } + + return val1; + }; + + emit(f, args, nodes[i]); + } + }; + + var reduce = function(results) { + return {"1" : 1}; + }; + + return ignite.compute().mapReduce(map, reduce, [key1, val1]); + }).then(function(res) { + assert(TestUtils.compareObject({"1": 1}, res), + "Incorrect result [exp= {1:1}, val=" + res); + + return cache.size(); + }).then(function(size){ + assert(size === 3, "Incorrect size [size=" + 3 + ", res=" + size + "]"); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeRunScriptContainsKey = function() { - function computeRunScriptContainsKey(error, ignite) { - assert(error == null, "Error on start:" + error); + TestUtils.startIgniteNode().then(function(ignite) { + var initKey = {"1" : ["1", "2"]}; var comp = ignite.compute(); @@ -58,28 +252,25 @@ testComputeRunScriptContainsKey = function() { return key; } - function onEnd(err, res) { - assert(err == null); - assert(TestUtils.compareObject(initKey, res), "Incorrect result after script.") - - ignite.cache("mycache").containsKey(initKey, function(err0, res0) { - assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); - assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); - TestUtils.testDone(); - }); - } - - var initKey = {"1" : ["1", "2"]}; + comp.run(f, initKey).then(function(res) { + assert(TestUtils.compareObject(initKey, res), "Incorrect result after script."); - comp.run(f, initKey, onEnd.bind(null)); - } + return ignite.cache("mycache").containsKey(initKey); + }).then(function(res) { + assert(res === true, "Incorrect value on js contains key [res=" + res + "]"); - TestUtils.startIgniteNode(computeRunScriptContainsKey); + TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeAffinityRunScriptContainsKey = function() { - function computeRunScriptContainsKey(error, ignite) { - assert(error == null, "Error on start:" + error); + TestUtils.startIgniteNode().then(function(ignite) { + var initKey = {"1" : ["1", "2"]}; var comp = ignite.compute(); @@ -93,28 +284,26 @@ testComputeAffinityRunScriptContainsKey = function() { return key; } - function onEnd(err, res) { - assert(err == null, err); - assert(TestUtils.compareObject(initKey, res), "Incorrect result after script.") - - ignite.cache("mycache").containsKey(initKey, function(err0, res0) { - assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); - assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); - TestUtils.testDone(); - }); - } - - var initKey = {"1" : ["1", "2"]}; + comp.affinityRun("mycache", initKey, f, initKey).then(function(res) { + assert(TestUtils.compareObject(initKey, res), "Incorrect result after script."); - comp.affinityRun("mycache", initKey, f, initKey, onEnd.bind(null)); - } + return ignite.cache("mycache").containsKey(initKey); + }).then(function(res) { + assert(res === true, "Incorrect value on js contains key [res=" + res + "]"); - TestUtils.startIgniteNode(computeRunScriptContainsKey); + TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeRunScriptContainsKeys = function() { - function computeRunScriptContainsKey(error, ignite) { - assert(error == null, "Error on start:" + error); + TestUtils.startIgniteNode().then(function(ignite) { + var initKey0 = {"1" : ["1", "2"]}; + var initKey1 = {"2" : "AAA"}; var comp = ignite.compute(); @@ -129,29 +318,29 @@ testComputeRunScriptContainsKeys = function() { return keys; } - function onEnd(err, res) { - assert(err == null); + comp.run(f, [initKey0, initKey1]).then(function(res) { assert(TestUtils.compareObject([initKey0, initKey1], res), "Incorrect result after script.") - ignite.cache("mycache").containsKey(initKey0, function(err0, res0) { - assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); - assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); - TestUtils.testDone(); - }); - } + return ignite.cache("mycache").containsKey(initKey0); + }).then(function(res) { + assert(res === true, "Incorrect value on js contains key [res=" + res + "]"); - var initKey0 = {"1" : ["1", "2"]}; - var initKey1 = {"2" : "AAA"}; - - comp.run(f, [initKey0, initKey1], onEnd.bind(null)); - } - - TestUtils.startIgniteNode(computeRunScriptContainsKey); + TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeRunScriptPutAllGetAll = function() { - function computeRunScriptPutAllGetAll(error, ignite) { - assert(error == null, "Error on start:" + error); + TestUtils.startIgniteNode().then(function(ignite) { + var initKey0 = {"1" : ["1", "2"]}; + var initKey1 = {"2" : "AAA"}; + var initVal0 = {"1" : ["1", "2"]}; + var initVal1 = {"2" : "AAA"}; + var initEntries = [new CacheEntry(initKey0, initVal0), new CacheEntry(initKey1, initVal1)]; var comp = ignite.compute(); @@ -163,281 +352,121 @@ testComputeRunScriptPutAllGetAll = function() { return cache.getAll(args[1]); } - function onEnd(err, res) { - assert(err == null); - + comp.run(f, [initEntries, [initKey0, initKey1]]).then(function(res) { assert(TestUtils.compareObject(initEntries[0].key, res[0].key), "Incorrect result after script " + "[InitEntries=" + JSON.stringify(initEntries[0].key) + ", val=" + JSON.stringify(res[0].key) + "]"); - ignite.cache("mycache").containsKey(initKey0, function(err0, res0) { - assert(err0 === null, "Get error on js contatins key [err=" + err0 + "]"); - assert(res0 === true, "Incorrect value on js contains key [res=" + res0 + "]"); - TestUtils.testDone(); - }); - } + return ignite.cache("mycache").containsKey(initKey0); + }).then(function(res) { + assert(res === true, "Incorrect value on js contains key [res=" + res + "]"); - var initKey0 = {"1" : ["1", "2"]}; - var initKey1 = {"2" : "AAA"}; - var initVal0 = {"1" : ["1", "2"]}; - var initVal1 = {"2" : "AAA"}; - var initEntries = [new CacheEntry(initKey0, initVal0), new CacheEntry(initKey1, initVal1)]; - - comp.run(f, [initEntries, [initKey0, initKey1]], - onEnd.bind(null)); - } - - TestUtils.startIgniteNode(computeRunScriptPutAllGetAll); + TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }) } testComputeRunScriptRemoveOperations = function() { - computeRunScriptRemoveOperations = function(error, ignite) { - assert(error === null, "Error on put:" + error); - - var comp = ignite.compute(); - - var f = function (args) { - var cache = ignite.cache("mycache"); - - if (cache.remove("key1") === true) { - throw "Incorrect remove from empty map"; - } - - var key0 = {"keyName" : "keyVal"}; - var key1 = {"keyName" : "keyVal1"}; - var val0 = {"valName" : 1}; - var val1 = {"valName" : 2}; - - var entries = [new CacheEntry(key0, val0), new CacheEntry(key1, val1)]; - var keys = [key0, key1]; - - cache.put(key0, val0); - - if (cache.removeValue(key0, val1) === true) { - throw "Incorrect removeValue from empty map [key=" + JSON.stringify(key0) + "]"; - } - - if (cache.remove(key0) === false) { - throw "Incorrect remove from empty map [key=" + JSON.stringify(key0) + "]"; - } - - cache.put(key0, val0); - - if (cache.replaceValue(key0, val0, val1) === true) { - throw "Incorrect replaceValue result [key=" + JSON.stringify(key0) + "]"; - } - - var prevVal = cache.getAndReplace(key0, val1); - - if (prevVal.valName !== val0.valName) { - throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + - ", prevVal=" + prevVal.valName + - ", expected=" + val0.valName + "]"; - } + var f = function(args) { + var cache = ignite.cache("mycache"); - prevVal = cache.get(key0); - - if (prevVal.valName !== val1.valName) { - throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + "]"; - } - - cache.removeAllFromCache(); - - if (cache.get(key0) !== null) { - throw "Incorrect removeAll result"; - } - - cache.putAll(entries); - - if (cache.replace(key1, val0) !== true) { - throw "Incorrect replace result"; - } + if (cache.remove("key1") === true) { + throw "Incorrect remove from empty map"; + } - prevVal = cache.get(key1); + var key0 = {"keyName" : "keyVal"}; + var key1 = {"keyName" : "keyVal1"}; + var val0 = {"valName" : 1}; + var val1 = {"valName" : 2}; - if (prevVal.valName !== val0.valName) { - throw "Incorrect replace [key=" + JSON.stringify(key1) + "]"; - } + var entries = [new CacheEntry(key0, val0), new CacheEntry(key1, val1)]; + var keys = [key0, key1]; - cache.removeAll(keys); + cache.put(key0, val0); - if (cache.size() !== 0) { - throw "Incorrect removeAll result."; - } + if (cache.removeValue(key0, val1) === true) { + throw "Incorrect removeValue from empty map [key=" + JSON.stringify(key0) + "]"; } - function onEnd(err, res) { - assert(err == null); - - TestUtils.testDone(); + if (cache.remove(key0) === false) { + throw "Incorrect remove from empty map [key=" + JSON.stringify(key0) + "]"; } - comp.run(f, [], onEnd.bind(null)); - } - - TestUtils.startIgniteNode(computeRunScriptRemoveOperations); -} - -testComputeMapReduceGetAndPut = function() { - function computeMapReduceGetAndPut(error, ignite) { - assert(error == null, "Error on start:" + error); - - var map = function(nodes, arg) { - for (var i = 0; i < nodes.length; i++) { - var f = function (val) { - var prev = ignite.cache("mycache").getAndPutIfAbsent(val, val); + cache.put(key0, val0); - if (prev !== null) { - throw "Get and put if absent does not work."; - } - - return val; - }; - - emit(f, i, nodes[i]); - } - }; - - var reduce = function(results) { - var sum = 0; - - for (var i = 0; i < results.length; ++i) { - if (results.indexOf(i) === -1) { - throw "Do not find " + i; - } - - var prev = ignite.cache("mycache").getAndPut(i, i + 1); - - if (prev !== i) { - throw "Incorrect previous value [key=" + i + ", val=" + prev + "]"; - } - - sum += prev; - } - - return sum; - }; - - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); - assert(res === 1); - - TestUtils.testDone(); + if (cache.replaceValue(key0, val0, val1) === true) { + throw "Incorrect replaceValue result [key=" + JSON.stringify(key0) + "]"; } - ignite.compute().mapReduce(map, reduce, [], callback); - } + var prevVal = cache.getAndReplace(key0, val1); - TestUtils.startIgniteNode(computeMapReduceGetAndPut); -} - -testComputeMapReduceGetAndRemoveObject = function() { - function computeMapReduceGetAndRemove(error, ignite) { - assert(error == null, "Error on start:" + error); - - var map = function(nodes, entries) { - for (var i = 0; i < entries.length; i++) { - var f = function (entry) { - var cache = ignite.cache("mycache"); - print("ENTRY =" + entry); - - print("ENTRY Key=" + entry.key); - - if (cache.putIfAbsent(entry.key, entry.value) !== true) { - throw "Incorrect put if absent result." - } - - if (cache.putIfAbsent(entry.key, "1") !== false) { - throw "Incorrect put if absent result." - } - - return cache.getAndRemove(entry.key); - }; - - emit(f, entries[i], nodes[i % nodes.length]); - } - }; - - var reduce = function(results) { - var sum = 0; - - for (var i = 0; i < results.length; ++i) { - sum += results[i].age; - } - - return sum; - }; - - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); - assert(res === 25, "Incorrect reduce result."); - - TestUtils.testDone(); + if (prevVal.valName !== val0.valName) { + throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + + ", prevVal=" + prevVal.valName + + ", expected=" + val0.valName + "]"; } + prevVal = cache.get(key0); - entries = []; - - var key1 = {"name" : "Ann"}; - var key2 = {"name" : "Paul"}; - var val1 = {"age" : 12, "books" : ["1", "Book"]}; - var val2 = {"age" : 13, "books" : ["1", "Book"]}; - - entries.push(new CacheEntry(key1, val1)); - entries.push(new CacheEntry(key2, val2)); - - ignite.compute().mapReduce(map, reduce, entries, callback); - } + if (prevVal.valName !== val1.valName) { + throw "Incorrect getAndReplace result [key=" + JSON.stringify(key0) + "]"; + } - TestUtils.startIgniteNode(computeMapReduceGetAndRemove); -} + cache.removeAllFromCache(); -function onStart(onPut, error, ignite) { - var cache = ignite.cache("mycache"); + if (cache.get(key0) !== null) { + throw "Incorrect removeAll result"; + } - var params = []; + cache.putAll(entries); - for (var i = 900; i < 1000; ++i) { - params.push(new CacheEntry("key" + i, "val" + i)); - } + if (cache.replace(key1, val0) !== true) { + throw "Incorrect replace result"; + } - cache.putAll(params, onPut.bind(null, ignite)) -} + prevVal = cache.get(key1); -function computeRunScript(ignite, error) { - assert(error == null, "Error on put:" + error); + if (prevVal.valName !== val0.valName) { + throw "Incorrect replace [key=" + JSON.stringify(key1) + "]"; + } - var comp = ignite.compute(); + cache.removeAll(keys); - var f = function (args) { - print("!!!!" + args + " " + ignite.name()); - return args + " " + ignite.name(); + if (cache.size() !== 0) { + throw "Incorrect removeAll result."; + } } - function onEnd(err, res) { - assert(err == null); - assert(res.indexOf("NodeJsComputeSelfTest") !== -1, "Incorrect result message. [mes=" + res + "]."); - assert(res.indexOf("GridGain") !== -1, "Incorrect result message. [mes=" + res + "]."); - - TestUtils.testDone(); - } + TestUtils.startIgniteNode().then(function(ignite) { + var comp = ignite.compute(); - comp.run(f, "GridGain", onEnd.bind(null)); + comp.run(f, []).then(function(res) { + TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); + }) + }).catch(function(err) { + assert(err === null, err); + }); } -function computeExecute(error, ignite) { - assert(error == null, "Error on put:" + error); - +testComputeMapReduceGetAndPut = function() { var map = function(nodes, arg) { - var words = arg.split(" "); + for (var i = 0; i < nodes.length; i++) { + var f = function (val) { + var prev = ignite.cache("mycache").getAndPutIfAbsent(val, val); - for (var i = 0; i < words.length; i++) { - var f = function (word) { - print(">>> Printing " + word); + if (prev !== null) { + throw "Get and put if absent does not work."; + } - return word.length; + return val; }; - emit(f, words[i], nodes[i % nodes.length]); + emit(f, i, nodes[i]); } }; @@ -445,159 +474,90 @@ function computeExecute(error, ignite) { var sum = 0; for (var i = 0; i < results.length; ++i) { - sum += results[i]; - } - - return sum; - }; - - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); - assert.equal(res, 7); - - TestUtils.testDone(); - } - - ignite.compute().mapReduce(map, reduce, "Hi Alice", callback); -} - -function computeAllNodeExecute(error, ignite) { - assert(error == null, "Error on put:" + error); - - var map = function(nodes, arg) { - for (var i = 0; i < nodes.length; i++) { - var f = function (node) { - print(">>> Printing " + node.id().toString()); - - return ""; - }; - - emit(f, nodes[i % nodes.length], nodes[i % nodes.length]); - } - }; - - var reduce = function(results) {}; - - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); - TestUtils.testDone(); - } - - ignite.compute().mapReduce(map, reduce, "", callback); -} - -function computeCacheExecute(error, ignite) { - assert(error == null, "Error on put:" + error); - - var map = function(nodes, args) { - for (var i = 0; i < 1; i++) { - var f = function (args1) { - ignite.cache("mycache").put({"1": "1"}, 2); - - var val = ignite.cache("mycache").get({"1": "1"}); - - if (val !== 2) { - throw "Incorrect return val [expected=2, val=" + val + "]"; - } - - var val1 = ignite.cache("mycache").get(args1.get(0)); - - if (val1["age"] !== 12) { - throw "Incorrect age [expected=12, val=" + val + "]"; - } - - print("BOOKS=" + val1.books); - - if (val1.books.length !== 2) { - throw "Incorrect books length [expected=2, val=" + - val1.books.length + "]"; - } + if (results.indexOf(i) === -1) { + throw "Do not find " + i; + } - if (val1.books[0] !== "1") { - throw "Incorrect books value [expected=1, val=" + - val1.books[0] + "]"; - } - if (val1.books[1] !== "Book") { - throw "Incorrect books value [expected=Book, val=" + - val1.books[1] + "]"; - } + var prev = ignite.cache("mycache").getAndPut(i, i + 1); - return val1; - }; + if (prev !== i) { + throw "Incorrect previous value [key=" + i + ", val=" + prev + "]"; + } - emit(f, args, nodes[i]); + sum += prev; } - }; - var reduce = function(results) { - return {"1" : 1}; + return sum; }; - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); - - assert(TestUtils.compareObject({"1": 1}, res), - "Incorrect result [exp= {1:1}, val=" + res); + TestUtils.startIgniteNode().then(function(ignite) { + ignite.compute().mapReduce(map, reduce, []).then(function(res) { + assert(res === 1); - ignite.cache("mycache").size(function(err, size){ - assert(size === 3, "Incorrect size [size=" + 3 + ", res=" + size + "]"); TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); }) - } - - entries = []; - - var key1 = {"name" : "Ann"}; - var key2 = {"name" : "Paul"}; - var val1 = {"age" : 12, "books" : ["1", "Book"]}; - var val2 = {"age" : 13, "books" : ["1", "Book"]}; - - entries.push(new CacheEntry(key1, val1)); - entries.push(new CacheEntry(key2, val2)); - - ignite.cache("mycache").putAll(entries, function(err) { - ignite.compute().mapReduce(map, reduce, [key1, val1], callback); + }).catch(function(err) { + assert(err === null, err); }); } -function computeCacheSizeExecute(error, ignite) { - assert(error == null, "Error on put:" + error); +testComputeMapReduceGetAndRemoveObject = function() { + var map = function(nodes, entries) { + for (var i = 0; i < entries.length; i++) { + var f = function (entry) { + var cache = ignite.cache("mycache"); + print("ENTRY =" + entry); + + print("ENTRY Key=" + entry.key); - var map = function(nodes, arg) { - for (var i = 0; i < nodes.length; i++) { - var f = function (args) { - print("!!!!!Node id " + ignite.localNode().id()); + if (cache.putIfAbsent(entry.key, entry.value) !== true) { + throw "Incorrect put if absent result." + } - return ignite.cache("mycache").localSize(); + if (cache.putIfAbsent(entry.key, "1") !== false) { + throw "Incorrect put if absent result." + } + + return cache.getAndRemove(entry.key); }; - emit(f, [1, 2], nodes[i]); + emit(f, entries[i], nodes[i % nodes.length]); } }; var reduce = function(results) { var sum = 0; - for (var i = 0; i < results.length; i++) { - sum += results[i]; + for (var i = 0; i < results.length; ++i) { + sum += results[i].age; } return sum; }; - var callback = function(err, res) { - assert(err == null, "Get error on compute task [err=" + err + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + entries = []; + + var key1 = {"name" : "Ann"}; + var key2 = {"name" : "Paul"}; + var val1 = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"age" : 13, "books" : ["1", "Book"]}; + + entries.push(new CacheEntry(key1, val1)); + entries.push(new CacheEntry(key2, val2)); + + ignite.compute().mapReduce(map, reduce, entries).then(function(res) { + assert(res === 25, "Incorrect reduce result."); - ignite.cache("mycache").size(function(err, size){ - assert(size === res, "Incorrect size [size=" + size + ", res=" + res + "]"); TestUtils.testDone(); + }).catch(function(err) { + assert(err === null, err); }) - } - - ignite.cache("mycache").put("key", "val", - function(err) { - ignite.compute().mapReduce(map, reduce, "", callback); - }); + }).catch(function(err) { + assert(err === null, err); + }); } testComputeFuncWithErrorExecute = function() { @@ -641,18 +601,28 @@ testComputeIncorrectMapExecute = function() { } function testComputeWithErrors(map) { - function computeErrorExecute(error, ignite) { - var callback = function(err, res) { - assert(err != null, "Do not get error on compute task."); - + TestUtils.startIgniteNode().then(function(ignite) { + ignite.compute().mapReduce(map, function (args) {}, "Hi Alice").then(function(res) { + assert(false, "Do not get an error."); + }).catch(function(err) { + assert(err !== null, err); assert(err.indexOf("Function evaluation failed") > -1, "Incorrect error "+ "[expected=function evaluation failed, value=" + err + "]"); TestUtils.testDone(); - } + }) + }).catch(function(err) { + assert(err === null, err); + }); +} - ignite.compute().mapReduce(map, function (args) {}, "Hi Alice", callback); + +function getEntries() { + var params = []; + + for (var i = 900; i < 1000; ++i) { + params.push(new CacheEntry("key" + i, "val" + i)); } - TestUtils.startIgniteNode(computeErrorExecute); -} + return params; +} \ No newline at end of file