#ignite-961: rewrite 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/0479f5b4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/0479f5b4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/0479f5b4 Branch: refs/heads/ignite-961-promise Commit: 0479f5b42c899a836f2830c0f6ddb8336708a115 Parents: dace2ec Author: ivasilinets <[email protected]> Authored: Tue Jul 14 13:08:09 2015 +0300 Committer: ivasilinets <[email protected]> Committed: Tue Jul 14 13:08:09 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/apache-ignite.js | 1 - modules/nodejs/src/main/js/cache-promise.js | 355 -------- modules/nodejs/src/main/js/cache.js | 263 +++--- modules/nodejs/src/main/js/ignite.js | 141 ++-- modules/nodejs/src/main/js/ignition-promise.js | 111 --- modules/nodejs/src/main/js/ignition.js | 106 ++- modules/nodejs/src/main/js/package.json | 5 +- modules/nodejs/src/main/js/server-promises.js | 253 ------ .../ignite/internal/NodeJsIgnitionSelfTest.java | 12 +- .../ScriptingJsCachePromisesApiSelfTest.java | 227 ------ modules/nodejs/src/test/js/test-cache-api.js | 799 +++++++++++-------- .../src/test/js/test-cache-promise-api.js | 562 ------------- modules/nodejs/src/test/js/test-ignite.js | 104 +-- modules/nodejs/src/test/js/test-ignition.js | 78 +- modules/nodejs/src/test/js/test-key.js | 56 +- modules/nodejs/src/test/js/test-utils.js | 21 +- 16 files changed, 815 insertions(+), 2279 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/apache-ignite.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/apache-ignite.js b/modules/nodejs/src/main/js/apache-ignite.js index d670e32..053b88a 100644 --- a/modules/nodejs/src/main/js/apache-ignite.js +++ b/modules/nodejs/src/main/js/apache-ignite.js @@ -16,7 +16,6 @@ */ module.exports = { - IgnitionPromise : require('./ignition-promise.js').IgnitionPromise, Cache : require('./cache.js').Cache, CacheEntry : require('./cache.js').CacheEntry, Ignition : require('./ignition.js').Ignition, http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/cache-promise.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/cache-promise.js b/modules/nodejs/src/main/js/cache-promise.js deleted file mode 100644 index 1e34233..0000000 --- a/modules/nodejs/src/main/js/cache-promise.js +++ /dev/null @@ -1,355 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -var Promise = require("bluebird"); -var Server = require("./server").Server; -var Command = require("./server").Command; -var SqlFieldsQuery = require("./sql-fields-query").SqlFieldsQuery -var SqlQuery = require("./sql-query").SqlQuery - -/** - * Creates an instance of CachePromise - * - * @constructor - * @this {CachePromise} - * @param {Server} server Server class - * @param {string} cacheName Cache name - */ -function CachePromise(server, cacheName) { - this._server = server; - this._cacheName = cacheName; -} - -/** - * Get cache name. - * - * @this{Cache} - * @returns {string} Cache name. - */ -CachePromise.prototype.name = function() { - return this._cacheName; -} - -/** - * Get cache value - * - * @this {Cache} - * @param {string} key Key - */ -CachePromise.prototype.get = function(key) { - return this.__createPromise(this._createCommand("get"). - setPostData(JSON.stringify({"key": key}))); -}; - -/** - * Put cache value - * - * @this {Cache} - * @param {string} key Key - * @param {string} value Value - */ -CachePromise.prototype.put = function(key, value) { - return this.__createPromise(this._createCommand("put"). - setPostData(JSON.stringify({"key": key, "val" : value}))); -} - -/** - * Put if absent - * - * @this {Cache} - * @param {string} key Key - * @param {string} value Value - */ -CachePromise.prototype.putIfAbsent = function(key, value) { - return this.__createPromise(this._createCommand("putifabsent"). - setPostData(JSON.stringify({"key": key, "val" : value}))); -} - -/** - * Remove cache key - * - * @this {Cache} - * @param key Key - */ -CachePromise.prototype.remove = function(key, callback) { - return this.__createPromise(this._createCommand("rmv"). - setPostData(JSON.stringify({"key": key}))); -} - -/** - * Remove cache key - * - * @this {Cache} - * @param key Key - * @param value Value - */ -CachePromise.prototype.removeValue = function(key, value, callback) { - return this.__createPromise(this._createCommand("rmvvalue"). - setPostData(JSON.stringify({"key": key, "val" : value}))); -} - -/** - * Get and remove cache key - * - * @this {Cache} - * @param {string} key Key - */ -CachePromise.prototype.getAndRemove = function(key, callback) { - return this.__createPromise(this._createCommand("getandrmv"). - setPostData(JSON.stringify({"key": key}))); -} - -/** - * Remove cache keys - * - * @this {Cache} - * @param {string[]} keys Keys to remove - */ -CachePromise.prototype.removeAll = function(keys, callback) { - return this.__createPromise(this._createCommand("rmvall"). - setPostData(JSON.stringify({"keys" : keys}))); -} - -/** - * Remove all cache keys - * - * @this {Cache} - */ -CachePromise.prototype.removeAllFromCache = function(callback) { - return this.__createPromise(this._createCommand("rmvall")); -} - -/** - * Put keys to cache - * - * @this {Cache} - * @param {CacheEntry[]} List of entries to put in the cache - */ -CachePromise.prototype.putAll = function(entries) { - return this.__createPromise(this._createCommand("putall").setPostData( - JSON.stringify({"entries" : entries}))); -} - -/** - * Get keys from the cache - * - * @this {Cache} - * @param {Object[]} keys Keys - */ -CachePromise.prototype.getAll = function(keys, callback) { - var cmd = this._createCommand("getall").setPostData(JSON.stringify({"keys" : keys})); - - return new Promise(function(resolve, reject) { - server.runCommand(cmd, function(err, res) { - if(err != null) { - reject(err); - } - else { - var result = []; - - for (var key of res) { - result.push(new CacheEntry(key["key"], key["value"])); - } - - resolve(result); - } - }); - }); -} - -/** - * Determines if the cache contains an entry for the specified key. - * - * @this {Cache} - * @param {Object} key Key - */ -CachePromise.prototype.containsKey = function(key) { - return this.__createPromise(this._createCommand("containskey"). - setPostData(JSON.stringify({"key" : key}))); -} - -/** - * Determines if the cache contains all keys. - * - * @this {Cache} - * @param {Object[]} keys Keys - */ -CachePromise.prototype.containsKeys = function(keys, callback) { - return this.__createPromise(this._createCommand("containskeys"). - setPostData(JSON.stringify({"keys" : keys}))); -} - -/** - * Get and put cache value - * - * @this {Cache} - * @param {string} key Key - * @param {string} value Value - */ -CachePromise.prototype.getAndPut = function(key, val) { - return this.__createPromise(this._createCommand("getandput"). - setPostData(JSON.stringify({"key" : key, "val" : val}))); -} - -/** - * replace cache value - * - * @this {Cache} - * @param key Key - * @param value Value - */ -CachePromise.prototype.replace = function(key, val, callback) { - return this.__createPromise(this._createCommand("rep"). - setPostData(JSON.stringify({"key" : key, "val" : val}))); -} - -/** - * replace cache value - * - * @this {Cache} - * @param key Key - * @param value Value - * @param oldVal Old value - */ -CachePromise.prototype.replaceValue = function(key, val, oldVal) { - return this.__createPromise(this._createCommand("repVal"). - setPostData(JSON.stringify({"key" : key, "val" : val, "oldVal" : oldVal}))); -} - -/** - * Get and put cache value - * - * @this {Cache} - * @param {string} key Key - * @param {string} value Value - */ -CachePromise.prototype.getAndReplace = function(key, val) { - return this.__createPromise(this._createCommand("getandreplace"). - setPostData(JSON.stringify({"key" : key, "val" : val}))); -} - -/** - * Stores given key-value pair in cache only if cache had no previous mapping for it. - * - * @this {Cache} - * @param {string} key Key - * @param {string} value Value - */ -CachePromise.prototype.getAndPutIfAbsent = function(key, val) { - return this.__createPromise(this._createCommand("getandputifabsent"). - setPostData(JSON.stringify({"key" : key, "val" : val}))); -} - -/** - * @this {Cache} - */ -CachePromise.prototype.size = function(callback) { - return this.__createPromise(this._createCommand("cachesize")); -} - -/** - * Execute sql query - * - * @param {SqlQuery|SqlFieldsQuery} qry Query - */ -CachePromise.prototype.query = function(qry) { - if (qry.returnType() == null) { - qry.end("No type for sql query."); - - return; - } - - var command = this._createQueryCommand("qryexecute", qry).addParam("type", qry.returnType()). - setPostData(JSON.stringify({"arg" : qry.arguments()})); - - var server = this._server; - - return new Promise(function(resolve, reject) { - server.runCommand(command, function(err, res){ - if(err != null) { - reject(err); - } - else { - resolve(new Cursor(qry, res)); - } - }); - }); -} - -CachePromise.prototype.__createPromise = function(cmd) { - return new Promise(function(resolve, reject) { - server.runCommand(cmd, function(err, res) { - if(err != null) { - reject(err); - } - else { - resolve(res); - } - }); - }); -} - -CachePromise.prototype._createCommand = function(name) { - var command = new Command(name); - - return command.addParam("cacheName", this._cacheName); -} - -CachePromise.prototype._createQueryCommand = function(name, qry) { - var command = this._createCommand(name); - - command.addParam("qry", qry.query()); - - return command.addParam("psz", qry.pageSize()); -} - -Cursor = function(qry, res) { - this._qry = qry; - this._res = res; -} - -Cursor.prototype.next = function() { - if (this._res["last"]) { - throw "All pages are returned."; - } - - var cmd = this._createCommand("qryfetch").addParam("qryId", _res.queryId). - addParam("psz", _qry.pageSize()); - - var server = this._server; - - return new Promise(function(resolve, reject) { - server.runCommand(cmd, function(err, res) { - if(err != null) { - reject(err); - } - else { - resolve(new Cursor(qry, res)); - } - }); - }); -} - -Cursor.prototype.page = function() { - return this._res["items"]; -} - -Cursor.prototype.isFinished = function() { - return this._res["last"]; -} - -exports.CachePromise = CachePromise \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/cache.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/cache.js b/modules/nodejs/src/main/js/cache.js index 893a945..a84b993 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -15,6 +15,7 @@ * limitations under the License. */ +var Promise = require("bluebird"); var Server = require("./server").Server; var Command = require("./server").Command; var SqlFieldsQuery = require("./sql-fields-query").SqlFieldsQuery @@ -48,12 +49,10 @@ Cache.prototype.name = function() { * * @this {Cache} * @param {string} key Key - * @param {onGet} callback Called on finish */ -Cache.prototype.get = function(key, callback) { - this._server.runCommand(this._createCommand("get"). - setPostData(JSON.stringify({"key": key})), - callback); +Cache.prototype.get = function(key) { + return this.__createPromise(this._createCommand("get"). + setPostData(JSON.stringify({"key": key}))); }; /** @@ -62,12 +61,10 @@ Cache.prototype.get = function(key, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {noValue} callback Called on finish */ -Cache.prototype.put = function(key, value, callback) { - this._server.runCommand(this._createCommand("put"). - setPostData(JSON.stringify({"key": key, "val" : value})), - callback); +Cache.prototype.put = function(key, value) { + return this.__createPromise(this._createCommand("put"). + setPostData(JSON.stringify({"key": key, "val" : value}))); } /** @@ -76,12 +73,10 @@ Cache.prototype.put = function(key, value, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {onGet} callback Called on finish */ -Cache.prototype.putIfAbsent = function(key, value, callback) { - this._server.runCommand(this._createCommand("putifabsent"). - setPostData(JSON.stringify({"key": key, "val" : value})), - callback); +Cache.prototype.putIfAbsent = function(key, value) { + return this.__createPromise(this._createCommand("putifabsent"). + setPostData(JSON.stringify({"key": key, "val" : value}))); } /** @@ -89,12 +84,10 @@ Cache.prototype.putIfAbsent = function(key, value, callback) { * * @this {Cache} * @param key Key - * @param {noValue} callback Called on finish */ Cache.prototype.remove = function(key, callback) { - this._server.runCommand(this._createCommand("rmv"). - setPostData(JSON.stringify({"key": key})), - callback); + return this.__createPromise(this._createCommand("rmv"). + setPostData(JSON.stringify({"key": key}))); } /** @@ -103,12 +96,10 @@ Cache.prototype.remove = function(key, callback) { * @this {Cache} * @param key Key * @param value Value - * @param {noValue} callback Called on finish */ Cache.prototype.removeValue = function(key, value, callback) { - this._server.runCommand(this._createCommand("rmvvalue"). - setPostData(JSON.stringify({"key": key, "val" : value})), - callback); + return this.__createPromise(this._createCommand("rmvvalue"). + setPostData(JSON.stringify({"key": key, "val" : value}))); } /** @@ -116,12 +107,10 @@ Cache.prototype.removeValue = function(key, value, callback) { * * @this {Cache} * @param {string} key Key - * @param {onGet} callback Called on finish with previous value */ Cache.prototype.getAndRemove = function(key, callback) { - this._server.runCommand(this._createCommand("getandrmv"). - setPostData(JSON.stringify({"key": key})), - callback); + return this.__createPromise(this._createCommand("getandrmv"). + setPostData(JSON.stringify({"key": key}))); } /** @@ -129,23 +118,19 @@ Cache.prototype.getAndRemove = function(key, callback) { * * @this {Cache} * @param {string[]} keys Keys to remove - * @param {noValue} callback Called on finish */ Cache.prototype.removeAll = function(keys, callback) { - this._server.runCommand(this._createCommand("rmvall"). - setPostData(JSON.stringify({"keys" : keys})), - callback); + return this.__createPromise(this._createCommand("rmvall"). + setPostData(JSON.stringify({"keys" : keys}))); } /** * Remove all cache keys * * @this {Cache} - * @param {noValue} callback Called on finish */ Cache.prototype.removeAllFromCache = function(callback) { - this._server.runCommand(this._createCommand("rmvall"), - callback); + return this.__createPromise(this._createCommand("rmvall")); } /** @@ -153,11 +138,10 @@ Cache.prototype.removeAllFromCache = function(callback) { * * @this {Cache} * @param {CacheEntry[]} List of entries to put in the cache - * @param {noValue} callback Called on finish */ -Cache.prototype.putAll = function(entries, callback) { - this._server.runCommand(this._createCommand("putall").setPostData( - JSON.stringify({"entries" : entries})), callback); +Cache.prototype.putAll = function(entries) { + return this.__createPromise(this._createCommand("putall").setPostData( + JSON.stringify({"entries" : entries}))); } /** @@ -165,28 +149,27 @@ Cache.prototype.putAll = function(entries, callback) { * * @this {Cache} * @param {Object[]} keys Keys - * @param {Cache~onGetAll} callback Called on finish */ Cache.prototype.getAll = function(keys, callback) { - function onGetAll(callback, err, res) { - if (err) { - callback.call(null, err, null); - - return; - } - - var result = []; - - for (var key of res) { - result.push(new CacheEntry(key["key"], key["value"])); - } - - callback.call(null, null, result); - } - - this._server.runCommand(this._createCommand("getall").setPostData( - JSON.stringify({"keys" : keys})), - onGetAll.bind(null, callback)); + var cmd = this._createCommand("getall").setPostData(JSON.stringify({"keys" : keys})); + + var server = this._server; + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if(err != null) { + reject(err); + } + else { + var result = []; + + for (var key of res) { + result.push(new CacheEntry(key["key"], key["value"])); + } + + resolve(result); + } + }); + }); } /** @@ -194,11 +177,10 @@ Cache.prototype.getAll = function(keys, callback) { * * @this {Cache} * @param {Object} key Key - * @param {Cache~onGetAll} callback Called on finish with boolean result */ -Cache.prototype.containsKey = function(key, callback) { - this._server.runCommand(this._createCommand("containskey"). - setPostData(JSON.stringify({"key" : key})), callback); +Cache.prototype.containsKey = function(key) { + return this.__createPromise(this._createCommand("containskey"). + setPostData(JSON.stringify({"key" : key}))); } /** @@ -206,11 +188,10 @@ Cache.prototype.containsKey = function(key, callback) { * * @this {Cache} * @param {Object[]} keys Keys - * @param {Cache~onGetAll} callback Called on finish with boolean result */ Cache.prototype.containsKeys = function(keys, callback) { - this._server.runCommand(this._createCommand("containskeys"). - setPostData(JSON.stringify({"keys" : keys})), callback); + return this.__createPromise(this._createCommand("containskeys"). + setPostData(JSON.stringify({"keys" : keys}))); } /** @@ -219,11 +200,10 @@ Cache.prototype.containsKeys = function(keys, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {onGet} callback Called on finish */ -Cache.prototype.getAndPut = function(key, val, callback) { - this._server.runCommand(this._createCommand("getandput"). - setPostData(JSON.stringify({"key" : key, "val" : val})), callback); +Cache.prototype.getAndPut = function(key, val) { + return this.__createPromise(this._createCommand("getandput"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); } /** @@ -232,11 +212,10 @@ Cache.prototype.getAndPut = function(key, val, callback) { * @this {Cache} * @param key Key * @param value Value - * @param {onGet} callback Called on finish */ Cache.prototype.replace = function(key, val, callback) { - this._server.runCommand(this._createCommand("rep"). - setPostData(JSON.stringify({"key" : key, "val" : val})), callback); + return this.__createPromise(this._createCommand("rep"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); } /** @@ -246,11 +225,10 @@ Cache.prototype.replace = function(key, val, callback) { * @param key Key * @param value Value * @param oldVal Old value - * @param {onGet} callback Called on finish */ -Cache.prototype.replaceValue = function(key, val, oldVal, callback) { - this._server.runCommand(this._createCommand("repVal"). - setPostData(JSON.stringify({"key" : key, "val" : val, "oldVal" : oldVal})), callback); +Cache.prototype.replaceValue = function(key, val, oldVal) { + return this.__createPromise(this._createCommand("repVal"). + setPostData(JSON.stringify({"key" : key, "val" : val, "oldVal" : oldVal}))); } /** @@ -259,11 +237,10 @@ Cache.prototype.replaceValue = function(key, val, oldVal, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {onGet} callback Called on finish */ -Cache.prototype.getAndReplace = function(key, val, callback) { - this._server.runCommand(this._createCommand("getandreplace"). - setPostData(JSON.stringify({"key" : key, "val" : val})), callback); +Cache.prototype.getAndReplace = function(key, val) { + return this.__createPromise(this._createCommand("getandreplace"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); } /** @@ -272,19 +249,17 @@ Cache.prototype.getAndReplace = function(key, val, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {onGet} callback Called on finish */ -Cache.prototype.getAndPutIfAbsent = function(key, val, callback) { - this._server.runCommand(this._createCommand("getandputifabsent"). - setPostData(JSON.stringify({"key" : key, "val" : val})), callback); +Cache.prototype.getAndPutIfAbsent = function(key, val) { + return this.__createPromise(this._createCommand("getandputifabsent"). + setPostData(JSON.stringify({"key" : key, "val" : val}))); } /** * @this {Cache} - * @param {onGet} callback Called on finish */ Cache.prototype.size = function(callback) { - this._server.runCommand(this._createCommand("cachesize"), callback); + return this.__createPromise(this._createCommand("cachesize")); } /** @@ -293,57 +268,42 @@ Cache.prototype.size = function(callback) { * @param {SqlQuery|SqlFieldsQuery} qry Query */ Cache.prototype.query = function(qry) { - function onQueryExecute(qry, error, res) { - if (error !== null) { - qry.end(error); - - return; - } - - qry.page(res["items"]); - - if (res["last"]) { - qry.end(null); - } - else { - var command = this._createCommand("qryfetch"); - - command.addParam("qryId", res.queryId).addParam("psz", qry.pageSize()); - - this._server.runCommand(command, onQueryExecute.bind(this, qry)); - } - } - - if (qry.type() === "Sql") { - this._sqlQuery(qry, onQueryExecute); - } - else { - this._sqlFieldsQuery(qry, onQueryExecute); - } -} - -Cache.prototype._sqlFieldsQuery = function(qry, onQueryExecute) { - var command = this._createQueryCommand("qryfieldsexecute", qry); - - command.setPostData(JSON.stringify({"arg" : qry.arguments()})); - - this._server.runCommand(command, onQueryExecute.bind(this, qry)); -} - -Cache.prototype._sqlQuery = function(qry, onQueryExecute) { if (qry.returnType() == null) { qry.end("No type for sql query."); return; } - var command = this._createQueryCommand("qryexecute", qry); - - command.addParam("type", qry.returnType()); - - command.setPostData(JSON.stringify({"arg" : qry.arguments()})); + var command = this._createQueryCommand("qryexecute", qry).addParam("type", qry.returnType()). + setPostData(JSON.stringify({"arg" : qry.arguments()})); + + var server = this._server; + + return new Promise(function(resolve, reject) { + server.runCommand(command, function(err, res){ + if(err != null) { + reject(err); + } + else { + resolve(new Cursor(qry, res)); + } + }); + }); +} - this._server.runCommand(command, onQueryExecute.bind(this, qry)); +Cache.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); + } + }); + }); } Cache.prototype._createCommand = function(name) { @@ -360,6 +320,41 @@ Cache.prototype._createQueryCommand = function(name, qry) { return command.addParam("psz", qry.pageSize()); } +Cursor = function(qry, res) { + this._qry = qry; + this._res = res; +} + +Cursor.prototype.next = function() { + if (this._res["last"]) { + throw "All pages are returned."; + } + + var cmd = this._createCommand("qryfetch").addParam("qryId", _res.queryId). + addParam("psz", _qry.pageSize()); + + var server = this._server; + + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if(err != null) { + reject(err); + } + else { + resolve(new Cursor(qry, res)); + } + }); + }); +} + +Cursor.prototype.page = function() { + return this._res["items"]; +} + +Cursor.prototype.isFinished = function() { + return this._res["last"]; +} + /** * @this{CacheEntry} * @param key Key @@ -370,13 +365,5 @@ function CacheEntry(key0, val0) { this.value = val0; } -/** - * Callback for cache get - * - * @callback Cache~onGetAll - * @param {string} error Error - * @param {string[]} results Result values - */ - exports.Cache = Cache exports.CacheEntry = CacheEntry \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/ignite.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/ignite.js b/modules/nodejs/src/main/js/ignite.js index 71d6e3e..c3d90ac 100644 --- a/modules/nodejs/src/main/js/ignite.js +++ b/modules/nodejs/src/main/js/ignite.js @@ -15,11 +15,10 @@ * limitations under the License. */ +var Promise = require("bluebird"); var Cache = require("./cache").Cache; -var CachePromise = require("./cache-promise").CachePromise; var Compute = require("./compute").Compute; var ClusterNode = require("./cluster-node").ClusterNode; -var Server = require("./server").Server; var Command = require("./server").Command; /** @@ -53,58 +52,24 @@ Ignite.prototype.cache = function(cacheName) { } /** - * Get an instance of cache - * - * @this {Ignite} - * @param {string} Cache name - * @returns {Cache} Cache - */ -Ignite.prototype.cachePromise = function(cacheName) { - return new CachePromise(this._server, cacheName); -} - -/** - * Get or create an instance of cache - * - * @this {Ignite} - * @param {string} Cache name - * @param callback Callback with cache. - */ -Ignite.prototype.getOrCreateCache = function(cacheName, callback) { - var onCreateCallback = function(callback, err, res) { - if (err !== null) { - callback.call(null, err, null); - - return; - } - - callback.call(null, null, new Cache(this._server, cacheName)) - } - - this._server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName), - onCreateCallback.bind(this, callback)); -} - -/** * Get or create an instance of cache * * @this {Ignite} * @param {string} Cache name - * @param callback Callback with cache. */ -Ignite.prototype.getOrCreatePromiseCache = function(cacheName, callback) { - var onCreateCallback = function(callback, err, res) { - if (err !== null) { - callback.call(null, err, null); - - return; - } - - callback.call(null, null, new CachePromise(this._server, cacheName)) - } - - this._server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName), - onCreateCallback.bind(this, callback)); +Ignite.prototype.getOrCreateCache = function(cacheName) { + var server = this._server; + return new Promise(function(resolve, reject) { + server.runCommand(new Command("getorcreatecache").addParam("cacheName", cacheName), + function(err, res) { + if (err != null) { + reject(err); + } + else { + resolve(new Cache(server, cacheName)); + } + }); + }); } /** @@ -112,10 +77,9 @@ Ignite.prototype.getOrCreatePromiseCache = function(cacheName, callback) { * * @this {Ignite} * @param {string} cacheName Cache name to stop - * @param {noValue} callback Callback contains only error */ -Ignite.prototype.destroyCache = function(cacheName, callback) { - this._server.runCommand(new Command("destroycache").addParam("cacheName", cacheName), callback); +Ignite.prototype.destroyCache = function(cacheName) { + return this._createPromise(new Command("destroycache").addParam("cacheName", cacheName)); } /** @@ -132,51 +96,62 @@ Ignite.prototype.compute = function() { * Ignite version * * @this {Ignite} - * @param {onGet} callback Result in callback contains string with Ignite version. */ -Ignite.prototype.version = function(callback) { - this._server.runCommand(new Command("version"), callback); +Ignite.prototype.version = function() { + return this._createPromise(new Command("version")); } /** * Connected ignite name * * @this {Ignite} - * @param {onGet} callback Result in callback contains string with Ignite name. */ -Ignite.prototype.name = function(callback) { - this._server.runCommand(new Command("name"), callback); +Ignite.prototype.name = function() { + return this._createPromise(new Command("name")); } /** * @this {Ignite} - * @param {onGet} callback Result in callback contains list of ClusterNodes */ -Ignite.prototype.cluster = function(callback) { - function onTop(callback, err, res) { - if (err) { - callback.call(null, err, null); - - return; - } - - if (!res || res.length == 0) { - callback.call(null, "Empty topology cluster.", null); - - return; - } - - var nodes = []; - - for (var node of res) { - nodes.push(new ClusterNode(node.nodeId, node.attributes)); - } - - callback.call(null, null, nodes); - } +Ignite.prototype.cluster = function() { + var cmd = new Command("top").addParam("attr", "true").addParam("mtr", "false"); + + var server = this._server; + return new Promise(function(resolve, reject) { + server.runCommand(cmd, function(err, res) { + if (err != null) { + reject(err); + } + else { + if (!res || res.length == 0) { + reject("Empty topology cluster."); + } + else { + var nodes = []; + + for (var node of res) { + nodes.push(new ClusterNode(node.nodeId, node.attributes)); + } + + resolve(nodes); + } + } + }); + }); +} - this._server.runCommand(new Command("top").addParam("attr", "true").addParam("mtr", "false"), - onTop.bind(null, callback)); +Ignite.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); + } + }); + }); } exports.Ignite = Ignite; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/ignition-promise.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/ignition-promise.js b/modules/nodejs/src/main/js/ignition-promise.js deleted file mode 100644 index 4aa6035..0000000 --- a/modules/nodejs/src/main/js/ignition-promise.js +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Creates an instance of Ignition - * - * @constructor - */ -function IgnitionPromise() { -} - -/** - * Callback for Ignition start - * - * @callback Ignition~onStart - * @param {string} error Error - * @param {Ignite} ignite Connected ignite - */ - -/** - * Open connection with ignite node - * - * @param {string[]} address List of nodes hosts with ports - * @param {string} secretKey Secret key. - */ -IgnitionPromise.start = function(address, secretKey) { - return new Promise(function(resolve, reject) { - var Server = require("./server").Server; - var Ignite = require("./ignite").Ignite - - var numConn = 0; - - for (var addr of address) { - var params = addr.split(":"); - - var portsRange = params[1].split(".."); - - var start; - var end; - - if (portsRange.length === 1) { - start = parseInt(portsRange[0], 10); - end = start; - } - else if (portsRange.length === 2) { - start = parseInt(portsRange[0], 10); - end = parseInt(portsRange[1], 10); - } - - if (isNaN(start) || isNaN(end)) { - incorrectAddress(); - - return; - } - - for (var i = start; i <= end; i++) { - checkServer(params[0], i, secretKey); - } - } - - function checkServer(host, port, secretKey) { - numConn++; - - var server = new Server(host, port, secretKey); - - server.checkConnection(onConnect.bind(null, server)); - } - - var needVal = true; - - function incorrectAddress() { - reject("Incorrect address format."); - - needVal = false; - } - - function onConnect(server, error) { - if (!needVal) return; - - numConn--; - - if (!error) { - resolve(new Ignite(server)); - - needVal = false; - - return; - } - - if (!numConn) { - reject("Cannot connect to servers. " + error); - } - } - }); -} - -exports.IgnitionPromise = IgnitionPromise; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/ignition.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/ignition.js b/modules/nodejs/src/main/js/ignition.js index 049eb4b..a7d4518 100644 --- a/modules/nodejs/src/main/js/ignition.js +++ b/modules/nodejs/src/main/js/ignition.js @@ -14,7 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +var Server = require("./server").Server; +var Ignite = require("./ignite").Ignite /** * Creates an instance of Ignition * @@ -36,73 +37,70 @@ function Ignition() { * * @param {string[]} address List of nodes hosts with ports * @param {string} secretKey Secret key. - * @param {Ignition~onStart} callback Called on finish */ -Ignition.start = function(address, secretKey, callback) { - var Server = require("./server").Server; - var Ignite = require("./ignite").Ignite - - var numConn = 0; - - for (var addr of address) { - var params = addr.split(":"); - - var portsRange = params[1].split(".."); - - var start; - var end; - - if (portsRange.length === 1) { - start = parseInt(portsRange[0], 10); - end = start; - } - else if (portsRange.length === 2) { - start = parseInt(portsRange[0], 10); - end = parseInt(portsRange[1], 10); +Ignition.start = function(address, secretKey) { + return new Promise(function(resolve, reject) { + var numConn = 0; + + var needVal = true; + + for (var addr of address) { + var params = addr.split(":"); + + var portsRange = params[1].split(".."); + + var start; + var end; + + if (portsRange.length === 1) { + start = parseInt(portsRange[0], 10); + end = start; + } + else if (portsRange.length === 2) { + start = parseInt(portsRange[0], 10); + end = parseInt(portsRange[1], 10); + } + + if (isNaN(start) || isNaN(end)) { + needVal = false; + + reject("Incorrect address format."); + } + else { + for (var i = start; i <= end; i++) { + checkServer(params[0], i, secretKey); + } + } } - if (isNaN(start) || isNaN(end)) { - incorrectAddress(); + function checkServer(host, port, secretKey) { + numConn++; - return; - } + var server = new Server(host, port, secretKey); - for (var i = start; i <= end; i++) { - checkServer(params[0], i, secretKey); + server.checkConnection(onConnect.bind(this, server)); } - } - - function checkServer(host, port, secretKey) { - numConn++; - - var server = new Server(host, port, secretKey); - server.checkConnection(onConnect.bind(null, server)); - } + function onConnect(server, error) { + if (!needVal) return; - function incorrectAddress() { - callback.call(null, "Incorrect address format.", null); + numConn--; - callback = null; - } + if (!error) { + resolve(new Ignite(server)); - function onConnect(server, error) { - if (!callback) return; + needVal = false; - numConn--; + return; + } - if (!error) { - callback.call(null, null, new Ignite(server)); - - callback = null; - - return; - } + if (!numConn) { + reject("Cannot connect to servers. " + error); - if (!numConn) { - callback.call(null, "Cannot connect to servers. " + error, null); + return; + } } - } + }); } exports.Ignition = Ignition; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/package.json ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/package.json b/modules/nodejs/src/main/js/package.json index ae4b911..47c627e 100644 --- a/modules/nodejs/src/main/js/package.json +++ b/modules/nodejs/src/main/js/package.json @@ -10,5 +10,8 @@ "license" : "Apache-2.0", "keywords" : "grid", "homepage" : "https://ignite.incubator.apache.org/", - "engines" : { "node" : ">=0.12.4" } + "engines" : { "node" : ">=0.12.4" }, + "dependencies" : { + "bluebird" : ">=2.0.0" + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/main/js/server-promises.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/main/js/server-promises.js b/modules/nodejs/src/main/js/server-promises.js deleted file mode 100644 index 40a5dd3..0000000 --- a/modules/nodejs/src/main/js/server-promises.js +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Creates an instance of Server - * - * @constructor - * @this {Server} - * @param {string} host Host address - * @param {number} port Port - * @param {string} secretKey Secret key for connection - */ -function Server(host, port, secretKey) { - this._host = host; - this._port = port; - this._secretKey = secretKey; -} - -/** - * Host value - * - * @this {Server} - * @returns {string} Host value - */ -Server.prototype.host = function() { - return this._host; -} - -/** - * Callback for Server runCommand - * - * @callback onGet - * @param {string} error Error - * @param {string} result Result value - */ - -/** - * @callback noValue - * @param {string} error Error - */ - -/** - * Run http request - * - * @this {Server} - * @param {Command} cmd Command - */ -Server.prototype.runCommand = function(cmd) { - return new Promise(function(resolve, reject) { - var requestQry = "cmd=" + cmd.name() + cmd.paramsString(); - - var http = require('http'); - - var options = { - host: this._host, - port: this._port, - method : cmd._method(), - path: "/ignite?" + requestQry, - headers: this._signature() - }; - - if (cmd._isPost()) { - options.headers['Content-Length'] = cmd.postData().length; - options.headers['JSONObject'] = "true"; - } - - function streamCallback(response) { - var fullResponseString = ''; - - response.on('data', function (chunk) { - fullResponseString += chunk; - }); - - response.on('end', function () { - if (response.statusCode !== 200) { - if (response.statusCode === 401) { - reject("Authentication failed. Status code 401."); - } - else { - reject("Request failed. Status code " + response.statusCode); - } - - return; - } - - var igniteResponse; - - try { - igniteResponse = JSON.parse(fullResponseString); - } - catch (e) { - reject(e); - - return; - } - - if (igniteResponse.successStatus) { - reject(igniteResponse.error) - } - else { - resolve(igniteResponse.response); - } - }); - } - - var request = http.request(options, streamCallback); - - - request.setTimeout(20000, function() {reject("Request timeout: >5 sec")}); - - request.on('error', function(err) {reject(err)}); - - if (cmd._isPost()) { - request.write(cmd.postData()); - } - - request.end(); - }); -} - -/** - * Check the connection with server node. - * - * @this {Server} - * @param {onGet} callback Called on finish - */ -Server.prototype.checkConnection = function(callback) { - this.runCommand(new Command("version"), callback); -} - -/** - * Get signature for connection. - * - * @this {Server} - * @returns Signature - */ -Server.prototype._signature = function() { - if (!this._secretKey) { - return {}; - } - - var loadTimeInMS = Date.now(); - - var baseKey = '' + loadTimeInMS + ":" + this._secretKey; - - var crypto = require('crypto') - - var shasum = crypto.createHash('sha1'); - - shasum.update(baseKey, 'binary'); - - var hash = shasum.digest('base64'); - - var key = loadTimeInMS + ":" + hash; - - return {"X-Signature" : key}; -} - -/** - * @param {noValue} f Function - * @returns {string} Encoding function - */ -Server._escape = function(f) { - var qs = require('querystring'); - - return qs.escape(f.toString()); -} - -/** - * @constructor - * @this{Command} - * @param{string} name Command name. - */ -function Command(name) { - this._name = name; - this._params = []; -} - -/** - * @this {Command} - * @param {string} key Key - * @param {string} val Value - * @returns this - */ -Command.prototype.addParam = function(key, value) { - this._params.push({key: key, value: value}); - return this; -} - -/** - * @this {Command} - * @param{JSONObject} postData Post data. - * @returns this - */ -Command.prototype.setPostData = function(postData) { - this._postData = postData; - return this; -} - -/** - * @this {Command} - * @returns Post data. - */ -Command.prototype.postData = function() { - return this._postData; -} - -/** - * @this {Command} - * @returns Command name. - */ -Command.prototype.name = function() { - return this._name; -} - -/** - * @this {Command} - * @returns Http request string. - */ -Command.prototype.paramsString = function() { - var paramsString = ""; - - for (var p of this._params) { - paramsString += "&" + Server._escape(p.key) + "=" + Server._escape(p.value); - } - - return paramsString; -} - -Command.prototype._method = function() { - return this._isPost()? "POST" : "GET"; -} - -Command.prototype._isPost = function() { - return !!this._postData; -} - -exports.Server = Server; -exports.Command = Command; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsIgnitionSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsIgnitionSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsIgnitionSelfTest.java index 205e467..bdbebab 100644 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsIgnitionSelfTest.java +++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsIgnitionSelfTest.java @@ -41,28 +41,28 @@ public class NodeJsIgnitionSelfTest extends NodeJsAbstractTest { /** * @throws Exception If failed. */ - public void testIgnitionStart() throws Exception { - runJsScript("ignitionStartSuccess"); + public void testIgnitionStartSuccess() throws Exception { + runJsScript("testIgnitionStartSuccess"); } /** * @throws Exception If failed. */ - public void testIgnitionFailedStart() throws Exception { + public void testIgnitionFail() throws Exception { runJsScript("testIgnitionFail"); } /** * @throws Exception If failed. */ - public void testIgnitionStartWithSeveralPorts() throws Exception { - runJsScript("ignitionStartSuccessWithSeveralPorts"); + public void testIgnitionStartSuccessWithSeveralPorts() throws Exception { + runJsScript("testIgnitionStartSuccessWithSeveralPorts"); } /** * @throws Exception If failed. */ public void testIgnitionNotStartWithSeveralPorts() throws Exception { - runJsScript("ignitionNotStartWithSeveralPorts"); + runJsScript("testIgnitionNotStartWithSeveralPorts"); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/test/java/org/apache/ignite/internal/ScriptingJsCachePromisesApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/ScriptingJsCachePromisesApiSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/ScriptingJsCachePromisesApiSelfTest.java deleted file mode 100644 index 02cce7b..0000000 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/ScriptingJsCachePromisesApiSelfTest.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.internal; - -/** - * Test node js client put/get. - */ -public class ScriptingJsCachePromisesApiSelfTest extends NodeJsAbstractTest { - /** - * Constructor. - */ - public ScriptingJsCachePromisesApiSelfTest() { - super("test-cache-promise-api.js"); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - startGrid(0); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - grid(0).cache(NodeJsAbstractTest.CACHE_NAME).removeAll(); - } - - /** - * @throws Exception If failed. - */ - public void testPutGet() throws Exception { - runJsScript("testPutGet"); - } - - /** - * @throws Exception If failed. - */ - public void testPutGetObject() throws Exception { - runJsScript("testPutGetObject"); - } - - /** - * @throws Exception If failed. - */ - public void testIncorrectCache() throws Exception { - runJsScript("testIncorrectCacheName"); - } - - /** - * @throws Exception If failed. - */ - public void testGetOrCreateCacheName() throws Exception { - runJsScript("testGetOrCreateCacheName"); - } - - /** - * @throws Exception If failed. - */ - public void testRemove() throws Exception { - runJsScript("testRemove"); - } - - /** - * @throws Exception If failed. - */ - public void testRemoveNoKey() throws Exception { - runJsScript("testRemoveNoKey"); - } - - /** - * @throws Exception If failed. - */ - public void testRemoveAll() throws Exception { - runJsScript("testRemoveAll"); - } - - /** - * @throws Exception If failed. - */ - public void testPutAllGetAll() throws Exception { - runJsScript("testPutAllGetAll"); - } - - /** - * @throws Exception If failed. - */ - public void testPutAllObjectGetAll() throws Exception { - runJsScript("testPutAllObjectGetAll"); - } - - /** - * @throws Exception If failed. - */ - public void testRemoveAllObjectGetAll() throws Exception { - runJsScript("testRemoveAllObjectGetAll"); - } - - /** - * @throws Exception If failed. - */ - public void testContains() throws Exception { - runJsScript("testContains"); - } - - /** - * @throws Exception If failed. - */ - public void testPutContains() throws Exception { - runJsScript("testPutContains"); - } - - /** - * @throws Exception If failed. - */ - public void testPutContainsAll() throws Exception { - runJsScript("testPutContainsAll"); - } - - /** - * @throws Exception If failed. - */ - public void testNotContainsAll() throws Exception { - runJsScript("testNotContainsAll"); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndPut() throws Exception { - runJsScript("testGetAndPut"); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndPutIfAbsent() throws Exception { - runJsScript("testGetAndPutIfAbsent"); - } - - /** - * @throws Exception If failed. - */ - public void testPutIfAbsent() throws Exception { - runJsScript("testPutIfAbsent"); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndRemove() throws Exception { - runJsScript("testGetAndRemove"); - } - - /** - * @throws Exception If failed. - */ - public void testRemoveValue() throws Exception { - runJsScript("testRemoveValue"); - } - - /** - * @throws Exception If failed. - */ - public void testRemoveAllFromCache() throws Exception { - runJsScript("testRemoveAllFromCache"); - } - - /** - * @throws Exception If failed. - */ - public void testReplace() throws Exception { - runJsScript("testReplace"); - } - - /** - * @throws Exception If failed. - */ - public void testIncorrectReplaceObject() throws Exception { - runJsScript("testIncorrectReplaceObject"); - } - - /** - * @throws Exception If failed. - */ - public void testReplaceObject() throws Exception { - runJsScript("testReplaceObject"); - } - - /** - * @throws Exception If failed. - */ - public void testGetAndReplaceObject() throws Exception { - runJsScript("testGetAndReplaceObject"); - } - - /** - * @throws Exception If failed. - */ - public void testReplaceValueObject() throws Exception { - runJsScript("testReplaceValueObject"); - } - - /** - * @throws Exception If failed. - */ - public void testSize() throws Exception { - runJsScript("testSize"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0479f5b4/modules/nodejs/src/test/js/test-cache-api.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-cache-api.js b/modules/nodejs/src/test/js/test-cache-api.js index 9855fa3..ed05a90 100644 --- a/modules/nodejs/src/test/js/test-cache-api.js +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -23,264 +23,570 @@ var CacheEntry = Ignite.CacheEntry; var assert = require("assert"); testPutGet = function() { - startTest(false, "mycache", {trace: [put, getExist], entry: ["key" , "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + cache.put(key, val).then(function() { + return cache.get(key); + }).then(function(res) { + assert(TestUtils.compareObject(val, res), "Get incorrect value on get [exp=" + + JSON.stringify(val) + ", val=" + JSON.stringify(res) + "]"); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }).catch(function (err) { + assert(err === null, err); + }); } testPutGetObject = function() { var key = {"name" : "Paul"}; var val = {"age" : 12, "books" : ["1", "Book"]}; - startTest(false, "mycache", {trace: [put, getExist], entry: [key , val]}); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put(key, val).then(function() { + return cache.get(key); + }).then(function(res) { + assert(TestUtils.compareObject(val, res), "Get incorrect value on get [exp=" + + JSON.stringify(val) + ", val=" + JSON.stringify(res) + "]"); + TestUtils.testDone(); + }) + }); } testPutContains = function() { - startTest(false, "mycache", {trace: [put, containsKey], entry: ["key" , "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put(key, val).then(function() { + return cache.containsKey(key); + }).then(function(res) { + assert(res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testContains = function() { - startTest(false, "mycache", {trace: [notContainsKey], entry: ["key" , "6"]}); + var key = "key"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.containsKey(key).then(function(res) { + assert(res === false, "Incorrect result [expected=" + false + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testPutContainsAll = function() { - startTest(false, "mycache", {trace: [putAll, containsKeys], entry: objectEntries()}); + var entries = objectEntries(); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(entries).then(function(res) { + var keys = [] + + for (var entry of entries) { + keys.push(entry.key); + } + + return cache.containsKeys(keys); + }).then(function(res) { + assert(res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testNotContainsAll = function() { - startTest(false, "mycache", {trace: [notContainsKeys], entry: stringEntries()}); + var entries = stringEntries(); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + var keys = [] + + for (var entry of entries) { + keys.push(entry.key); + } + + cache.containsKeys(entries).then(function(res) { + assert(res === false, "Incorrect result [expected=" + false + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemove = function() { - startTest(false, "mycache", {trace: [put, getExist, remove, getNonExist], entry: ["key" , "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put(key, val).then(function(res) { + return cache.get(key); + }).then(function(res) { + assert (res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); + + return cache.remove(key); + }).then(function(res) { + assert (res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); + + return cache.get(key); + }).then(function(res) { + assert (res === null, "Incorrect result [expected=" + null + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemoveNoKey = function() { - startTest(false, "mycache", {trace: [remove, getNonExist], entry: ["key" , "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.remove(key).then(function(res) { + assert (res === false, "Incorrect result [expected=" + false + ", val=" + res + "]"); + + return cache.get(key); + }).then(function(res) { + assert (res === null, "Incorrect result [expected=" + null + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testPutAllGetAll = function() { - startTest(false, "mycache", {trace: [putAll, getAll], entry: stringEntries()}); + var entries = stringEntries(); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(entries).then(function(res) { + var keys = getKeys(entries); + + return cache.getAll(keys); + }).then(function(res) { + onGetAll(entries, res); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testPutAllObjectGetAll = function() { - startTest(false, "mycache", {trace: [putAll, getAll], entry: objectEntries()}); + var entries = objectEntries(); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(entries).then(function(res) { + var keys = getKeys(entries); + + return cache.getAll(keys); + }).then(function(res) { + onGetAll(entries, res); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemoveAllObjectGetAll = function() { - startTest(false, "mycache", {trace: [putAll, getAll, removeAll, getNone], entry: objectEntries()}); + var entries = objectEntries(); + var keys = getKeys(entries); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(entries).then(function(res) { + return cache.getAll(keys); + }).then(function(res) { + onGetAll(entries, res); + + return cache.removeAll(keys); + }).then(function(res) { + assert (res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); + + return cache.getAll(keys); + }).then(function(res) { + for (var i = 0; i < res.length; ++i) { + assert(res[i] === null, "Incorrect result [expected=" + null + ", val=" + res[i] + "]"); + } + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemoveAll = function() { - startTest(false, "mycache", {trace: [putAll, getAll, removeAll, getNone], entry: stringEntries()}); + var entries = stringEntries(); + var keys = getKeys(entries); + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.putAll(entries).then(function(res) { + return cache.getAll(keys); + }).then(function(res) { + onGetAll(entries, res); + + return cache.removeAll(keys); + }).then(function(res) { + assert (res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); + + return cache.getAll(keys); + }).then(function(res) { + for (var i = 0; i < res.length; ++i) { + assert(res[i] === null, "Incorrect result [expected=" + null + ", val=" + res[i] + "]"); + } + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testIncorrectCacheName = function() { - startTest(false, "mycache1", {trace: [incorrectPut], entry: ["key", "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache1"); + + cache.put(key, val).then(function(res) { + assert(false, "Do not get exception."); + }).catch(function (err) { + assert(err !== null, err); + assert(err.indexOf("Failed to find cache for given cache name") !== -1, + "Incorrect message on not exist cache. " + err); + + TestUtils.testDone(); + }) + }); } testGetOrCreateCacheName = function() { - startTest(true, "mycache2", {trace: [put, getExist], entry: ["key", "6"]}); + var key = "key"; + var val = "6"; + + TestUtils.startIgniteNode().then(function(ignite) { + return ignite.getOrCreateCache("mycache2"); + }).then(function(cache) { + return cache.put(key, val); + }).then(function(res) { + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }); } testGetAndPut = function() { - function onGetAndPut(err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === "6", "Incorrect result for getAndPut [expected=6, val" + res + "]"); + var key = "key"; + var val = "6"; + var val2 = "7"; - TestUtils.testDone(); - } + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - function getAndPut(cache, entry, next) { - cache.getAndPut("key", "7", onGetAndPut); - } + cache.put(key, val).then(function() { + return cache.getAndPut(key, val2); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, getAndPut], entry: ["key", "6"]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testGetAndPutIfAbsent = function() { - function getAndPutIfAbsent(cache, entry, next) { - cache.getAndPutIfAbsent("key", "7", onGetAndPutIfAbsent); + var key = "key"; + var val = "6"; + var val2 = "7"; - function onGetAndPutIfAbsent(err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === "6", "Incorrect result for getAndPutIfAbsent [expected=6, val" + res + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - next(); - } - } + cache.put(key, val).then(function() { + return cache.getAndPutIfAbsent(key, val2); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, getAndPutIfAbsent, getExist], entry: ["key", "6"]}); + return cache.get(key); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testPutIfAbsent = function() { - function putIfAbsent(cache, entry, next) { - cache.putIfAbsent("key", "7", onPutIfAbsent); + var key = "key"; + var val = "6"; + var val2 = "7"; - function onPutIfAbsent(err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === false, "Incorrect result for putIfAbsent [expected=false, val" + res + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - next(); - } - } + cache.put(key, val).then(function() { + return cache.putIfAbsent(key, val2); + }).then(function(res) { + assert(res === false, "Incorrect result [expected=" + false + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, putIfAbsent, getExist], entry: ["key", "6"]}); + return cache.get(key); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemoveValue = function() { - function removeValue(cache, entry, next) { - cache.removeValue("key", "7", onRemoveValue); + var key = "key"; + var val = "6"; + var val2 = "7"; - function onRemoveValue(err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === false, "Incorrect result for onRemoveValue [expected=false, val" + res + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - next(); - } - } + cache.put(key, val).then(function() { + return cache.removeValue(key, val2); + }).then(function(res) { + assert(res === false, "Incorrect result [expected=" + false + ", val=" + res + "]"); + + return cache.get(key); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, removeValue, getExist], entry: ["key", "6"]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testGetAndRemove = function() { - function getAndRemove(cache, entry, next) { - cache.getAndRemove("key", onGetAndRemove); + var key = "key"; + var val = "6"; + var val2 = "7"; - function onGetAndRemove(err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === "6", "Incorrect result for getAndPut [expected=6, val" + res + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - next(); - } - } + cache.put(key, val).then(function() { + return cache.getAndRemove(key, val2); + }).then(function(res) { + assert(res === val, "Incorrect result [expected=" + val + ", val=" + res + "]"); + + return cache.get(key); + }).then(function(res) { + assert(res === null, "Incorrect result [expected=" + null + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, getAndRemove, getNone], entry: ["key", "6"]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testRemoveAllFromCache = function() { - function removeAllFromCache(cache, entry, next) { - cache.removeAllFromCache(next); - } + var key = "key"; + var val = "6"; - startTest(false, "mycache", {trace: [put, removeAllFromCache, getNone], entry: ["key", "6"]}); -} + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); -testReplace = function() { - function replace(cache, entry, next) { - cache.replace(entry[0], "7", onReplace.bind(null, cache)); - - function onReplace(cache, err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === true, "Incorrect result for replace [expected=true, val=" + res + "]"); - - cache.get(entry[0], function(err, res) { - assert(!err); - assert("7" === res, "Get incorrect value on get [exp=7, val=" + res + "]"); - next(); - }); - } - } + cache.put(key, val).then(function() { + return cache.removeAllFromCache(); + }).then(function(res) { + return cache.get(key); + }).then(function(res) { + assert(res === null, "Incorrect result [expected=" + null + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, replace], entry: ["key", "6"]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } -testReplaceObject = function() { - function replace(cache, entry, next) { - var newKey = {"key" :"7"}; - cache.replace(entry[0], newKey, onReplace.bind(null, cache)); +testReplace = function() { + var key = "key"; + var val = "6"; + var val2 = "7"; - function onReplace(cache, err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === true, "Incorrect result for replace [expected=true, val" + res + "]"); + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - cache.get(entry[0], function(err, res) { - assert(!err); - assert(TestUtils.compareObject(newKey, res), "Get incorrect value on get."); + cache.put(key, val).then(function() { + return cache.replace(key, val2); + }).then(function(res) { + assert(res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); - next(); - }); - } - } + return cache.get(key); + }).then(function(res) { + assert(res === val2, "Incorrect result [expected=" + val2 + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); +} +testReplaceObject = function() { var key = {"name" : "Paul"}; var val = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"key" :"7"}; - startTest(false, "mycache", {trace: [put, replace], entry: [key, val]}); -} + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); -testGetAndReplaceObject = function() { - function getAndReplace(cache, entry, next) { - var newKey = {"key" :"7"}; - cache.getAndReplace(entry[0], newKey, onGetAndReplace.bind(null, cache)); + cache.put(key, val).then(function() { + return cache.replace(key, val2); + }).then(function(res) { + assert(res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); - function onGetAndReplace(cache, err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(TestUtils.compareObject(val, res), "Get incorrect value on get."); + return cache.get(key); + }).then(function(res) { + assert(TestUtils.compareObject(val2, res), "Incorrect result [expected=" + val2 + ", val=" + res + "]"); - next(); - } - } + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); +} +testGetAndReplaceObject = function() { var key = {"name" : "Paul"}; var val = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"key" :"7"}; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - startTest(false, "mycache", {trace: [put, getAndReplace], entry: [key, val]}); + cache.put(key, val).then(function() { + return cache.getAndReplace(key, val2); + }).then(function(res) { + assert(TestUtils.compareObject(val, res), "Incorrect result [expected=" + val + ", val=" + res + "]"); + + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testReplaceValueObject = function() { - function replaceValue(cache, entry, next) { - var newVal = {"key" :"7"}; - cache.replaceValue(entry[0], newVal, entry[1], onReplaceValue.bind(null, cache)); - - function onReplaceValue(cache, err, res) { - assert(err === null, "Get error on get and put [err=" + err + "]"); - assert(res === true, "Incorrect result for replace [expected=true, val" + res + "]"); - next(); - } - } - var key = {"name" : "Paul"}; var val = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = {"key" :"7"}; + + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put(key, val).then(function() { + return cache.replaceValue(key, val2, val); + }).then(function(res) { + assert(res === true, "Incorrect result [expected=" + true + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [put, replaceValue], entry: [key, val]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } testIncorrectReplaceObject = function() { - function replace(cache, entry, next) { - cache.replace(entry[0], "7", onReplace.bind(null, cache)); + var key = {"name" : "Paul"}; + var val = {"age" : 12, "books" : ["1", "Book"]}; + var val2 = "7"; - function onReplace(cache, err, res) { + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); + + cache.put(key, val).then(function() { + return cache.replace(key, val2); + }).then(function(res) { + assert(false, "Do not get exception."); + }).catch(function (err) { assert(err !== null, "Do not get error"); assert(err.indexOf("Failed to update keys") > -1, "Incorrect error message: " + err); - next(); - } - } - var key = {"name" : "Paul"}; - var val = {"age" : 12, "books" : ["1", "Book"]}; - - startTest(false, "mycache", {trace: [put, replace], entry: [key, val]}); + TestUtils.testDone(); + }) + }); } testSize = function() { - function onSize(exp, next, cache, err, res) { - assert(err === null, "Do not get error"); - assert(res === exp, "Incorrect size: " + res); - - next(); - } + var key = {"name" : "Paul"}; + var val = {"age" : 12, "books" : ["1", "Book"]}; - function size0(cache, entry, next) { - cache.size(onSize.bind(null, 0, next, cache)); - } + TestUtils.startIgniteNode().then(function(ignite) { + var cache = ignite.cache("mycache"); - function size1(cache, entry, next) { - cache.size(onSize.bind(null, 1, next, cache)); - } + cache.size().then(function(res) { + assert(res === 0, "Incorrect result [expected=" + 0 + ", val=" + res + "]"); - var key = {"name" : "Paul"}; - var val = {"age" : 12, "books" : ["1", "Book"]}; + return cache.put(key, val); + }).then(function() { + return cache.size(); + }).then(function(res) { + assert(res === 1, "Incorrect result [expected=" + 1 + ", val=" + res + "]"); - startTest(false, "mycache", {trace: [size0, put, size1], entry: [key, val]}); + TestUtils.testDone(); + }).catch(function (err) { + assert(err === null, err); + }) + }); } function objectEntries() { @@ -306,203 +612,46 @@ function stringEntries() { return entries; } -function startTest(createCache, cacheName, testDescription) { - TestUtils.startIgniteNode(onStart.bind(null, createCache, cacheName, testDescription)); -} - -function onStart(createCache, cacheName, testDescription, error, ignite) { - if (createCache) { - ignite.getOrCreateCache(cacheName, function(err, cache) { - assert(err === null, err); +function onGetAll(expected, values) { + var keys = getKeys(expected); - function callNext(error) { - assert(!error); - var next = testDescription.trace.shift(); - if (next) - next.call(null, cache, testDescription.entry, callNext); - else - TestUtils.testDone(); - } + assert(values.length === keys.length, "Values length is incorrect " + + "[expected=" + keys.length + ", real=" + values.length + "]"); - callNext(); - }); - } - else { - var cache = ignite.cache(cacheName); - - function callNext(error) { - assert(!error); - var next = testDescription.trace.shift(); - if (next) - next.call(null, cache, testDescription.entry, callNext); - else - TestUtils.testDone(); - } - - callNext(); - } - - -} - -function put(cache, entry, next) { - cache.put(entry[0], entry[1], next); -} - -function containsKey(cache, entry, next) { - cache.containsKey(entry[0], onContainsKey); - - function onContainsKey(err, val) { - assert(err === null, "Error on contains key [err=" + err + "]"); - assert(val === true, "Incorrect result [expected=" + true + ", val=" + val + "]"); - - TestUtils.testDone(); - } -} - -function notContainsKey(cache, entry, next) { - cache.containsKey(entry[0], onContainsKey); - - function onContainsKey(err, val) { - assert(err === null, "Error on contains key [err=" + err + "]"); - assert(val === false, "Incorrect result [expected=" + false + ", val=" + val + "]"); - - TestUtils.testDone(); - } -} - -function containsKeys(cache, entries, next) { - var keys = [] - - for (var entry of entries) { - keys.push(entry.key); - } + for (var i = 0; i < keys.length; ++i) { + var key = keys[i]; - cache.containsKeys(keys, onContainsKeys); + var foundVal = null; - function onContainsKeys(err, val) { - assert(err === null, "Error on contains key [err=" + err + "]"); - assert(val === true, "Incorrect result [expected=" + true + ", val=" + val + "]"); - - TestUtils.testDone(); - } -} - -function notContainsKeys(cache, entries, next) { - var keys = [] - - for (var entry of entries) { - keys.push(entry.key); - } - - cache.containsKeys(keys, onContainsKeys); - - function onContainsKeys(err, val) { - assert(err === null, "Error on contains key [err=" + err + "]"); - assert(val === false, "Incorrect result [expected=" + false + ", val=" + val + "]"); - - TestUtils.testDone(); - } -} + for (var j = 0; j < values.length; ++j) { + if (TestUtils.compareObject(key, values[j].key)) { + foundVal = values[j]; + } + } -function getExist(cache, entry, next) { - function onGet(error, value) { - assert(!error); - assert(TestUtils.compareObject(entry[1], value), "Get incorrect value on get [exp=" + - JSON.stringify(entry[1]) + ", val=" + JSON.stringify(value) + "]"); - next(); - } + var foundExp = null; - cache.get(entry[0], onGet); -} - -function remove(cache, entry, next) { - cache.remove(entry[0], next); -} + for (var j = 0; j < expected.length; ++j) { + if (TestUtils.compareObject(key, expected[j].key)) { + foundExp = expected[j]; + } + } -function getNonExist(cache, entry, next) { - cache.get(entry[0], onGet); + assert(foundVal !== null, "Cannot find key. [key=" + key + "]."); + assert(foundExp !== null, "Cannot find key. [key=" + key + "]."); - function onGet(error, value) { - assert(!error); - assert(!value); - next(); + assert(TestUtils.compareObject(foundExp, foundVal), "Incorrect value"); } -} -function putAll(cache, entries, next) { - cache.putAll(entries, next); + return true; } -function getAll(cache, entries, next) { +function getKeys(entries) { var keys = [] for (var entry of entries) { keys.push(entry.key); } - cache.getAll(keys, onGetAll.bind(null, keys)); - - var expected = entries; - - function onGetAll(keys, error, values) { - assert(!error, error); - - assert(values.length === keys.length, "Values length is incorrect " - + "[expected=" + keys.length + ", real=" + values.length + "]"); - - for (var i = 0; i < keys.length; ++i) { - var key = keys[i]; - - var foundVal = null; - - for (var j = 0; j < values.length; ++j) { - if (TestUtils.compareObject(key, values[j].key)) { - foundVal = values[j]; - } - } - - var foundExp = null; - - for (var j = 0; j < expected.length; ++j) { - if (TestUtils.compareObject(key, expected[j].key)) { - foundExp = expected[j]; - } - } - - assert(foundVal !== null, "Cannot find key. [key=" + key + "]."); - assert(foundExp !== null, "Cannot find key. [key=" + key + "]."); - - assert(TestUtils.compareObject(foundExp, foundVal), "Incorrect value"); - } - - next(); - } -} - -function removeAll(cache, entries, next) { - cache.removeAll(Object.keys(entries), next) -} - -function getNone(cache, entries, next) { - cache.getAll(Object.keys(entries), onGetAll); - - function onGetAll(error, values) { - assert(!error, error); - assert(!values || !Object.keys(values).length); - - next(); - } -} - -function incorrectPut(cache, entry, next) { - cache.put(entry[0], entry[1], callback); - - function callback(error) { - assert(!!error, "Do not get error for not exist cache"); - assert(error.indexOf("Failed to find cache for given cache name") !== -1, - "Incorrect message on not exist cache. " + error); - - next(); - } + return keys; } \ No newline at end of file
