Repository: incubator-ignite Updated Branches: refs/heads/ignite-961 040bfde03 -> 4fa7cd8df
#ignite-961: add remove method to node js cache. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/4fa7cd8d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/4fa7cd8d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/4fa7cd8d Branch: refs/heads/ignite-961 Commit: 4fa7cd8dfec346e67214836739d23a13808f6c23 Parents: 040bfde Author: ivasilinets <[email protected]> Authored: Mon Jun 8 19:19:13 2015 +0300 Committer: ivasilinets <[email protected]> Committed: Mon Jun 8 19:19:13 2015 +0300 ---------------------------------------------------------------------- modules/nodejs/src/main/js/cache.js | 32 +++--- .../ignite/internal/NodeJsCacheApiSelfTest.java | 73 +++++++++++++ .../ignite/internal/NodeJsPutGetSelfTest.java | 52 ---------- .../testsuites/IgniteNodeJsTestSuite.java | 2 +- modules/nodejs/src/test/js/test-cache-api.js | 102 +++++++++++++++++++ modules/nodejs/src/test/js/test-put-get.js | 78 -------------- 6 files changed, 195 insertions(+), 144 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/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 83a93c5..b3f698a 100644 --- a/modules/nodejs/src/main/js/cache.js +++ b/modules/nodejs/src/main/js/cache.js @@ -15,6 +15,8 @@ * limitations under the License. */ +var Server = require("./server").Server; + /** * Creates an instance of Cache * @@ -24,14 +26,19 @@ * @param {string} cacheName Cache name */ function Cache(server, cacheName) { - var Server = require("./server").Server; - this._server = server; this._cacheName = cacheName; this._cacheNameParam = Server.pair("cacheName", this._cacheName); } /** + * Callback for cache get + * @callback Cache~onGet + * @param {string} error Error + * @param {string} result Result value + */ + +/** * Get cache value * * @this {Cache} @@ -39,16 +46,13 @@ function Cache(server, cacheName) { * @param {Cache~onGet} callback Called on finish */ Cache.prototype.get = function(key, callback) { - var Server = require("./server").Server; - this._server.runCommand("get", [this._cacheNameParam, Server.pair("key", key)], callback); }; /** - * Callback for cache get - * @callback Cache~onGet + * Callback for cache put + * @callback Cache~noValue * @param {string} error Error - * @param {string} result Result value */ /** @@ -57,19 +61,21 @@ Cache.prototype.get = function(key, callback) { * @this {Cache} * @param {string} key Key * @param {string} value Value - * @param {Cache~onPut} callback Called on finish + * @param {Cache~noValue} callback Called on finish */ Cache.prototype.put = function(key, value, callback) { - var Server = require("./server").Server; - this._server.runCommand("put", [this._cacheNameParam, Server.pair("key", key), Server.pair("val", value)], callback); } /** - * Callback for cache put - * @callback Cache~onPut - * @param {string} error Error + * Remove cache key + * + * @param {string} key Key + * @param {noValue} callback Called on finish */ +Cache.prototype.remove = function(key, callback) { + this._server.runCommand("rmv", [this._cacheNameParam, Server.pair("key", key)], callback); +} exports.Cache = Cache \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java new file mode 100644 index 0000000..13746cb --- /dev/null +++ b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsCacheApiSelfTest.java @@ -0,0 +1,73 @@ +/* + * 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 NodeJsCacheApiSelfTest extends NodeJsAbstractTest { + /** Constructor. */ + public NodeJsCacheApiSelfTest() { + super("test-cache-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(); + + assertNull(grid(0).cache(NodeJsAbstractTest.CACHE_NAME).get("key")); + } + + /** + * @throws Exception If failed. + */ + public void testPutGet() throws Exception { + runJsScript("testPutGet"); + } + + /** + * @throws Exception If failed. + */ + public void testIncorrectCache() throws Exception { + runJsScript("testIncorrectCacheName"); + } + + /** + * @throws Exception If failed. + */ + public void testRemove() throws Exception { + runJsScript("testRemove"); + } + + /** + * @throws Exception If failed. + */ + public void testRemoveNoKey() throws Exception { + runJsScript("testRemoveNoKey"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsPutGetSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsPutGetSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsPutGetSelfTest.java deleted file mode 100644 index bc0acd1..0000000 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/NodeJsPutGetSelfTest.java +++ /dev/null @@ -1,52 +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 NodeJsPutGetSelfTest extends NodeJsAbstractTest { - /** Constructor. */ - public NodeJsPutGetSelfTest() { - super("test-put-get.js"); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - startGrid(0); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * @throws Exception If failed. - */ - public void testPutGet() throws Exception { - runJsScript("testPutGet"); - } - - /** - * @throws Exception If failed. - */ - public void testIncorrectCache() throws Exception { - runJsScript("testIncorrectCacheName"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/modules/nodejs/src/test/java/org/apache/ignite/testsuites/IgniteNodeJsTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/testsuites/IgniteNodeJsTestSuite.java b/modules/nodejs/src/test/java/org/apache/ignite/testsuites/IgniteNodeJsTestSuite.java index 9c65d85..2f853f6 100644 --- a/modules/nodejs/src/test/java/org/apache/ignite/testsuites/IgniteNodeJsTestSuite.java +++ b/modules/nodejs/src/test/java/org/apache/ignite/testsuites/IgniteNodeJsTestSuite.java @@ -32,7 +32,7 @@ public class IgniteNodeJsTestSuite extends TestSuite { TestSuite suite = new TestSuite("Ignite Node JS Test Suite"); suite.addTest(new TestSuite(NodeJsIgnitionSelfTest.class)); - suite.addTest(new TestSuite(NodeJsPutGetSelfTest.class)); + suite.addTest(new TestSuite(NodeJsCacheApiSelfTest.class)); return suite; } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/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 new file mode 100644 index 0000000..6a00a62 --- /dev/null +++ b/modules/nodejs/src/test/js/test-cache-api.js @@ -0,0 +1,102 @@ +/* + * 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 TestUtils = require("./test-utils").TestUtils; + +var Apache = require(TestUtils.scriptPath()); +var Cache = Apache.Cache; +var Server = Apache.Server; + +var assert = require("assert"); + +testPutGet = function() { + TestUtils.startIgniteNode(onStart.bind(null, onPut, "mycache")); +} + +testIncorrectCacheName = function() { + TestUtils.startIgniteNode(onStart.bind(null, onIncorrectPut, "mycache1")); +} + +testRemove = function() { + TestUtils.startIgniteNode(onStart.bind(null, onPutRemove, "mycache")); +} + +testRemoveNoKey = function() { + TestUtils.startIgniteNode(onStartRemove.bind(null, onRemove, "mycache")); +} + +function onStart(onPut1, cacheName, error, ignite) { + var cache = ignite.cache(cacheName); + + cache.put("key", "6", onPut1.bind(null, cache)); +} + +function onStartRemove(onPut1, cacheName, error, ignite) { + var cache = ignite.cache(cacheName); + + cache.remove("key", onRemove.bind(null, cache)); +} + +function onPutRemove(cache, error) { + assert(error == null); + + cache.get("key", onGetRemove.bind(null, cache)); +} + +function onGetRemove(cache, error, value) { + assert(error == null); + + assert(value == 6); + + cache.remove("key", onRemove.bind(null, cache)); +} + +function onRemove(cache, error) { + assert(error == null); + + cache.get("key", onGet.bind(null, null)); +} + +function onPut(cache, error) { + assert(error == null); + + cache.get("key", onGet.bind(null, 6)); +} + +function onGet(expected, error, value) { + console.log("onGet [error=" + error + ", val=" + value + "]."); + + assert(error == null); + + assert.equal(value, expected, "Get return incorrect value. [expected=" + expected + ", val=" + value + "]."); + + TestUtils.testDone(); +} + +function onIncorrectPut(cache, error) { + if (error) { + console.error("Failed to put " + error); + + assert(error.indexOf("Failed to find cache for given cache name") !== -1); + + TestUtils.testDone(); + + return; + } + + TestUtils.testFails("Exception should be thrown."); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4fa7cd8d/modules/nodejs/src/test/js/test-put-get.js ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/js/test-put-get.js b/modules/nodejs/src/test/js/test-put-get.js deleted file mode 100644 index 12a9530..0000000 --- a/modules/nodejs/src/test/js/test-put-get.js +++ /dev/null @@ -1,78 +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 TestUtils = require("./test-utils").TestUtils; - -var Apache = require(TestUtils.scriptPath()); -var Cache = Apache.Cache; -var Server = Apache.Server; - -testPutGet = function() { - TestUtils.startIgniteNode(onStart.bind(null, onPut, "mycache")); -} - -testIncorrectCacheName = function() { - TestUtils.startIgniteNode(onStart.bind(null, onIncorrectPut, "mycache1")); -} - -function onStart(onPut1, cacheName, error, ignite) { - var cache = ignite.cache(cacheName); - - cache.put("key", "6", onPut1.bind(null, cache)); -} - -function onPut(cache, error) { - if (error) { - TestUtils.testFails("Incorrect error message: " + error); - - return; - } - - cache.get("key", onGet); -} - -function onGet(error, value) { - if (error) { - console.error("Failed to get " + error); - - TestUtils.testFails("Incorrect error message: " + error); - - return; - } - - var assert = require("assert"); - - assert.equal(value, 6, "Get return incorrect value. + [expected=" + 6 + ", val=" + value + "]."); - - TestUtils.testDone(); -} - -function onIncorrectPut(cache, error) { - if (error) { - console.error("Failed to get " + error); - - var assert = require("assert"); - - assert(error.indexOf("Failed to find cache for given cache name") !== -1); - - TestUtils.testDone(); - - return; - } - - TestUtils.testFails("Exception should be thrown."); -} \ No newline at end of file
