From 56153e310203461ad20c51232216e8dc11c86076 Mon Sep 17 00:00:00 2001 From: Vittorio Date: Thu, 12 Oct 2017 16:12:46 +0200 Subject: [PATCH] HRCPP-411 Fixed args passing for remote exec --- .../impl/operations/ExecuteCmdOperation.cpp | 15 +- test/Simple.cpp | 178 +++++++++--------- 2 files changed, 95 insertions(+), 98 deletions(-) diff --git a/src/hotrod/impl/operations/ExecuteCmdOperation.cpp b/src/hotrod/impl/operations/ExecuteCmdOperation.cpp index 2239b575..2ce42356 100644 --- a/src/hotrod/impl/operations/ExecuteCmdOperation.cpp +++ b/src/hotrod/impl/operations/ExecuteCmdOperation.cpp @@ -16,8 +16,8 @@ ExecuteCmdOperation::ExecuteCmdOperation( Topology& topologyId_, uint32_t flags_, const std::vector& cmdName_, - const std::map,std::vector>& cmdArgs_) - : RetryOnFailureOperation>( + const std::map,std::vector>& cmdArgs_) + : RetryOnFailureOperation>( codec_, transportFactory_, cacheName_, topologyId_, flags_), cmdName(cmdName_), cmdArgs(cmdArgs_) {} @@ -38,14 +38,11 @@ std::vector ExecuteCmdOperation::sendExecuteOperation( transport.writeArray(this->cmdName); transport.writeVInt((int)this->cmdArgs.size()); - for (auto i=(int)this->cmdArgs.size()-1; i>=0; i--) + for (auto it = cmdArgs.begin(); it!=cmdArgs.end(); it++) { - for (auto it = cmdArgs.begin(); it!=cmdArgs.end(); it++) - { - std::vector nameBuf, valBuf; - transport.writeArray(it->first); - transport.writeArray(it->second); - } + std::vector nameBuf, valBuf; + transport.writeArray(it->first); + transport.writeArray(it->second); } transport.flush(); diff --git a/test/Simple.cpp b/test/Simple.cpp index 0adb7e29..79a6e9dd 100644 --- a/test/Simple.cpp +++ b/test/Simple.cpp @@ -22,9 +22,9 @@ namespace infinispan { class MyRoundRobinBalancingStrategy: public FailOverRequestBalancingStrategy { public: - MyRoundRobinBalancingStrategy() : - index(0) { - } + MyRoundRobinBalancingStrategy() : + index(0) { + } static FailOverRequestBalancingStrategy *newInstance() { return new MyRoundRobinBalancingStrategy(); @@ -38,8 +38,8 @@ class MyRoundRobinBalancingStrategy: public FailOverRequestBalancingStrategy { } } - ~MyRoundRobinBalancingStrategy() { - } + ~MyRoundRobinBalancingStrategy() { + } const transport::InetSocketAddress &getServerByIndex(size_t pos) { const transport::InetSocketAddress &server = servers[pos]; @@ -48,22 +48,22 @@ class MyRoundRobinBalancingStrategy: public FailOverRequestBalancingStrategy { private: std::vector servers; size_t index; - const transport::InetSocketAddress &nextServer( - const std::set& failedServers) { - for (unsigned int i = 0; i <= servers.size(); i++) { - const transport::InetSocketAddress &server = getServerByIndex( - index++); - if (failedServers.empty() || failedServers.find(server)!=failedServers.end() || i>failedServers.size()) { + const transport::InetSocketAddress &nextServer( + const std::set& failedServers) { + for (unsigned int i = 0; i <= servers.size(); i++) { + const transport::InetSocketAddress &server = getServerByIndex( + index++); + if (failedServers.empty() || failedServers.find(server)!=failedServers.end() || i>failedServers.size()) { if (index >= servers.size()) { index = 0; - } + } } return server; } - throw Exception("Bad news, no server available."); - } + throw Exception("Bad news, no server available."); + } }; } @@ -347,25 +347,25 @@ int basicTest(RemoteCacheManager &cacheManager, RemoteCache &cache) { // Now user pass a lambda func that will be executed in the async thread after the put completion std::future future_put1= cache.putAsync(ak2,av2,0,0,[&] (std::string *v){flag=true; return v;}); { - // If the async put is not completed flag must be false - if (future_put1.wait_for(std::chrono::seconds(0))!=std::future_status::ready) - { - if (flag) - { - std::cerr << "fail: expected false got true" << std::endl; - return 1; - } - } + // If the async put is not completed flag must be false + if (future_put1.wait_for(std::chrono::seconds(0))!=std::future_status::ready) + { + if (flag) + { + std::cerr << "fail: expected false got true" << std::endl; + return 1; + } + } } // Now wait for put completion future_put1.wait(); { - // The user lambda must be executed so flag must be true - if (!flag) - { - std::cerr << "fail: expected true got false" << std::endl; - return 1; - } + // The user lambda must be executed so flag must be true + if (!flag) + { + std::cerr << "fail: expected true got false" << std::endl; + return 1; + } } // Same test for get flag=false; @@ -373,26 +373,26 @@ int basicTest(RemoteCacheManager &cacheManager, RemoteCache &cache) { if (future_get1.wait_for(std::chrono::seconds(0))!=std::future_status::ready) { - if (flag) - { - std::cerr << "fail: expected false got true" << std::endl; - return 1; - } + if (flag) + { + std::cerr << "fail: expected false got true" << std::endl; + return 1; + } } future_get1.wait(); { - if (!flag) - { - std::cerr << "fail: expected true got false" << std::endl; - return 1; - } + if (!flag) + { + std::cerr << "fail: expected true got false" << std::endl; + return 1; + } } std::string* arv3= future_get1.get(); if (!arv3 || arv3->compare(av2)) { - std::cerr << "fail: expected " << av2 << " got " << (arv3 ? *arv3 : "null") << std::endl; - return 1; + std::cerr << "fail: expected " << av2 << " got " << (arv3 ? *arv3 : "null") << std::endl; + return 1; } delete arv3; std::cout << "PASS: Async test" << std::endl; @@ -402,7 +402,7 @@ int basicTest(RemoteCacheManager &cacheManager, RemoteCache &cache) { int main(int argc, char** argv) { int result=0; - std::cout << "Tests for CacheManager" << std::endl; + std::cout << "Tests for CacheManager" << std::endl; { ConfigurationBuilder builder; builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222); @@ -442,7 +442,7 @@ int main(int argc, char** argv) { } if (result!=0) - return result; + return result; std::cout << "PASS: Tests for CacheManager" << std::endl; // Call basic test for every marshaller and every codec std::cout << "Basic Test with BasicMarshaller" << std::endl; @@ -486,49 +486,49 @@ int main(int argc, char** argv) { } } - try { - result = basicTest(cacheManager, cache); - std::map s; - std::string argName = std::string("a"); - std::string argValue = std::string("b"); - // execute() operation needs explicit JBossMarshalling format for argument values - s.insert( - std::pair(argName, - JBasicMarshaller::addPreamble( - argValue))); - std::string script("// mode=local,language=javascript\n " - "var cache = cacheManager.getCache(\"namedCache\");\n" - "cache.put(\"a\", \"abc\");\n" - "cache.put(\"b\", \"b\");\n" - "cache.get(\"a\");\n"); - std::string script_name("script.js"); - std::string p_script_name = - JBasicMarshaller::addPreamble(script_name); - std::string p_script = JBasicMarshaller::addPreamble( - script); - RemoteCache scriptCache = - cacheManager.getCache( - "___script_cache", false); - scriptCache.put(p_script_name, p_script); - std::vector execResult = cache.execute(script_name, - s); - - // We know the remote script returns a string and - // we use the helper to unmarshall - std::string res( - JBasicMarshallerHelper::unmarshall( - (char*) execResult.data())); - if (res.compare("abc") != 0) { - std::cerr << "fail: cache.exec() returned unexpected result" - << std::endl; - return 1; - } - } catch (const Exception& e) { - cacheManager.stop(); - std::cout << "is: " << typeid(e).name() << '\n'; - std::cerr << "fail unexpected exception: " << e.what() << std::endl; - return 1; - } + try { + result = basicTest(cacheManager, cache); + std::map s; + std::string argName = std::string("a"); + std::string argValue = std::string("abc"); + // execute() operation needs explicit JBossMarshalling format for argument values + s.insert( + std::pair(argName, + JBasicMarshaller::addPreamble( + argValue))); + std::string script("// mode=local,language=javascript, parameters=[a]\n" + "var cache = cacheManager.getCache(\"namedCache\");\n" + "cache.put(\"a\", a);\n" + "cache.put(\"b\", \"b\");\n" + "cache.get(\"a\");\n"); + std::string script_name("script.js"); + std::string p_script_name = + JBasicMarshaller::addPreamble(script_name); + std::string p_script = JBasicMarshaller::addPreamble( + script); + RemoteCache scriptCache = + cacheManager.getCache( + "___script_cache", false); + scriptCache.put(p_script_name, p_script); + std::vector execResult = cache.execute(script_name, + s); + + // We know the remote script returns a string and + // we use the helper to unmarshall + std::string res( + JBasicMarshallerHelper::unmarshall( + (char*) execResult.data())); + if (res.compare("abc") != 0) { + std::cerr << "fail: cache.exec() returned unexpected result" + << std::endl; + return 1; + } + } catch (const Exception& e) { + cacheManager.stop(); + std::cout << "is: " << typeid(e).name() << '\n'; + std::cerr << "fail unexpected exception: " << e.what() << std::endl; + return 1; + } std::cout << "PASS: script execution on server" << std::endl; cacheManager.stop(); @@ -556,14 +556,14 @@ int main(int argc, char** argv) { result = basicTest(cacheManager, cache); std::map s; std::string argName = std::string("a"); - std::string argValue = std::string("b"); + std::string argValue = std::string("abc"); // execute() operation needs explicit JBossMarshalling format for argument values JBasicMarshaller *km1 = new JBasicMarshaller(); JBasicMarshaller *vm1 = new JBasicMarshaller(); s.insert(std::pair(argName,JBasicMarshaller::addPreamble(argValue))); - std::string script ("// mode=local,language=javascript\n " + std::string script ("// mode=local,language=javascript, parameters=[a]\n " "var cache = cacheManager.getCache(\"namedCache\");\n" - "cache.put(\"a\", \"abc\");\n" + "cache.put(\"a\", a);\n" "cache.put(\"b\", \"b\");\n" "cache.get(\"a\");\n"); std::string script_name("script.js");