示例#1
0
TEST_F(PutGetTest, OperationOnCacheManagerNotStarted) {
    BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
    BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
    RemoteCache<std::string, std::string> cache = PutGetTest::remoteCacheManager->getCache<std::string, std::string>(km,
            &Marshaller<std::string>::destroy,
            vm,
            &Marshaller<std::string>::destroy, true);
    EXPECT_ANY_THROW(cache.get("k1"));
}
示例#2
0
TEST_F(PutGetTest, ATestThatFails) {
	PutGetTest::remoteCacheManager->start();
    BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
    BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
    RemoteCache<std::string, std::string> cache = PutGetTest::remoteCacheManager->getCache<std::string, std::string>(km,
            &Marshaller<std::string>::destroy,
            vm,
            &Marshaller<std::string>::destroy, true);
    cache.clear();
    EXPECT_LT(0,cache.size());
}
示例#3
0
TEST_F(PutGetTest, SimplePutForcedReturnReturnsPrevVal) {
	PutGetTest::remoteCacheManager->start();
    BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
    BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
    RemoteCache<std::string, std::string> cache = PutGetTest::remoteCacheManager->getCache<std::string, std::string>(km,
            &Marshaller<std::string>::destroy,
            vm,
            &Marshaller<std::string>::destroy, true);
    std::unique_ptr<std::string> prevVal(cache.get("k1"));
    std::unique_ptr<std::string> ret(cache.put("k1","v1"));
    EXPECT_TRUE(prevVal==ret || *prevVal==*ret);
}
示例#4
0
int main(int argc, char** argv) {
    int result=0;
    std::cout << "Events and Listeners" << std::endl;
    {
        ConfigurationBuilder builder;
        builder.balancingStrategyProducer(nullptr);
        builder.protocolVersion(argc > 1 ? argv[1] : Configuration::PROTOCOL_VERSION_24);
        builder.addServer().host(argc > 2 ? argv[2] : "127.0.0.1").port(argc > 3 ? atoi(argv[3]) : 11222);
        RemoteCacheManager cacheManager(builder.build(), false);
        cacheManager.start();
        JBasicMarshaller<int> *km = new JBasicMarshaller<int>();
        JBasicMarshaller<std::string> *vm = new JBasicMarshaller<std::string>();
        RemoteCache<int, std::string> cache = cacheManager.getCache<int, std::string>(km,
                &Marshaller<int>::destroy,
                vm,
                &Marshaller<std::string>::destroy);
        cache.clear();
        std::vector<std::vector<char> > filterFactoryParams;
        std::vector<std::vector<char> > converterFactoryParams;
        CacheClientListener<int, std::string> cl(cache);
        int createdCount=0, modifiedCount=0, removedCount=0, expiredCount=0;
        // We're using future and promise to have a basic listeners/main thread synch
        int setFutureEventKey=0;
        std::promise<void> promise;
        // Listeners definitions
        std::function<void(ClientCacheEntryCreatedEvent<int>)> listenerCreated
            = [&createdCount, &setFutureEventKey, &promise](ClientCacheEntryCreatedEvent<int> e) {
                createdCount++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                };
            };
        std::function<void(ClientCacheEntryModifiedEvent<int>)> listenerModified
            = [&modifiedCount, &setFutureEventKey, &promise](ClientCacheEntryModifiedEvent <int> e) {
                modifiedCount++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                }
            };
        std::function<void(ClientCacheEntryRemovedEvent<int>)> listenerRemoved
            = [&removedCount, &setFutureEventKey, &promise](ClientCacheEntryRemovedEvent <int> e) {
                removedCount++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                }
            };
        std::function<void(ClientCacheEntryExpiredEvent<int>)> listenerExpired
            = [&expiredCount, &setFutureEventKey, &promise](ClientCacheEntryExpiredEvent <int> e) {
                expiredCount++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                }
            };

        cl.add_listener(listenerCreated);
        cl.add_listener(listenerModified);
        cl.add_listener(listenerRemoved);
        cl.add_listener(listenerExpired);


        cache.put(1,"v1");
        cache.put(2,"v2");
        cache.put(3,"v3");

        cache.addClientListener(cl, filterFactoryParams, converterFactoryParams);
        // This tells the listener to fill the promise on event with key = 6
        setFutureEventKey=6;
        cache.put(4,"v4");
        cache.put(5,"v5");
        cache.put(6,"v6");
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners on Create (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(createdCount, createdCount == 3, result, "created count mismatch");

        setFutureEventKey=2;
        promise= std::promise<void>();
        cache.put(1,"v1a");
        cache.put(2,"v2a");
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners on Modify (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(modifiedCount, modifiedCount == 2, result, "modified count mismatch");

        setFutureEventKey=3;
        promise= std::promise<void>();
        cache.remove(3);
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners on Remove (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(removedCount, removedCount == 1, result, "removed count mismatch");

        // Now test expired events
        setFutureEventKey=10;
        promise= std::promise<void>();
        cache.put(11,"nexp");
        cache.put(10,"exp1",3);

        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        {
              std::cout << "FAIL: Events and Listeners (Timeout)" << std::endl;
              return -1;
        }

        promise= std::promise<void>();
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        {
              std::cout << "FAIL: Events and Listeners (Timeout)" << std::endl;
              return -1;
        }
        ASSERT(expiredCount, expiredCount == 1, result, "removed count mismatch");



        cache.removeClientListener(cl);

        // Now add a second listener to check includeCurrentState CS feature
        CacheClientListener<int, std::string> clCS(cache);
        clCS.includeCurrentState=true;
        int createdCountCS=0, modifiedCountCS=0, removedCountCS=0;

        std::function<void(ClientCacheEntryCreatedEvent<int>)> listenerCreatedCS
            = [&createdCountCS, &setFutureEventKey, &promise](ClientCacheEntryCreatedEvent<int> e) {
                createdCountCS++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                }
            };
        std::function<void(ClientCacheEntryModifiedEvent<int>)> listenerModifiedCS
            = [&modifiedCountCS, &setFutureEventKey, &promise](ClientCacheEntryModifiedEvent <int> e) {
                 modifiedCountCS++;
                 if (setFutureEventKey==e.getKey())
                 {
                     promise.set_value();
                 }
            };
        std::function<void(ClientCacheEntryRemovedEvent<int>)> listenerRemovedCS
            = [&removedCountCS, &setFutureEventKey, &promise](ClientCacheEntryRemovedEvent <int> e) {
                removedCountCS++;
                if (setFutureEventKey==e.getKey())
                {
                    promise.set_value();
                }
            };

        clCS.add_listener(listenerCreatedCS);
        clCS.add_listener(listenerModifiedCS);
        clCS.add_listener(listenerRemovedCS);

        setFutureEventKey=8;
        promise=std::promise<void>();
        cache.addClientListener(clCS, filterFactoryParams, converterFactoryParams);
        cache.put(8,"v8");
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(createdCountCS, createdCountCS == 7, result, "created with current state");
        std::cout << "9 Events and Listeners" << std::endl;

        setFutureEventKey=2;
        promise=std::promise<void>();
        cache.put(1,"v1a");
        cache.put(2,"v2a");
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(modifiedCountCS, modifiedCountCS == 2, result, "modified count mismatch");

        setFutureEventKey=8;
        promise=std::promise<void>();
        cache.remove(8);
        if (std::future_status::timeout==promise.get_future().wait_for(std::chrono::seconds(30)))
        { 
          std::cout << "FAIL: Events and Listeners (Timeout)" << std::endl;
          return -1;
        }
        ASSERT(removedCountCS, removedCountCS == 1, result, "removed count mismatch");

        cache.removeClientListener(clCS);

        cacheManager.stop();
    }
    if (result==0)
    {
      std::cout << "PASS: Events and Listeners" << std::endl;
    }
    else
    {
      std::cout << "FAIL: Events and Listeners" << std::endl;
    }
    return result;
}
示例#5
0
int main(int argc, char** argv) {
    ConfigurationBuilder nearCacheBuilder;
    nearCacheBuilder.protocolVersion(argc > 1 ? argv[1] : Configuration::PROTOCOL_VERSION_24);
    nearCacheBuilder.addServer().host(argc > 2 ? argv[2] : "127.0.0.1").port(argc > 3 ? atoi(argv[3]) : 11222);
    nearCacheBuilder.balancingStrategyProducer(nullptr);
    nearCacheBuilder.nearCache().mode(NearCacheMode::INVALIDATED).maxEntries(10);
    RemoteCacheManager nearCacheManager(nearCacheBuilder.build(), false);
    JBasicMarshaller<std::string> *km = new JBasicMarshaller<std::string>();
    JBasicMarshaller<std::string> *vm = new JBasicMarshaller<std::string>();
    try
    {
    nearCacheManager.start();
    RemoteCache<std::string, std::string> nearCache = nearCacheManager.getCache<std::string, std::string>(km,
            &Marshaller<std::string>::destroy,
            vm,
            &Marshaller<std::string>::destroy);
    nearCache.clear();
    // Read stats to do some checks on hits and miss counter
    std::map<std::string,std::string> statsBegin= nearCache.stats();
    auto hitsBegin = std::stoi(statsBegin["hits"]);
    auto missesBegin = std::stoi(statsBegin["misses"]);
    // Only the first get goes to the remote cache and miss the value
    // then all the gets are resolved nearly
    nearCache.get("key1");
    nearCache.put("key1", "value1");
    std::string *rest = nearCache.get("key1");
    std::cout << "Got result from near cache:" << ((rest) ? *rest : "null") << std::endl;
    delete rest;
    rest = nearCache.get("key1");
    delete rest;
    std::map<std::string,std::string> stats1= nearCache.stats();
    auto hits1 = std::stoi(stats1["hits"]);
    auto misses1 = std::stoi(stats1["misses"]);
    std::cout << "Remote misses is: " << misses1-missesBegin << "" << std::endl;
    std::cout << "Remote hits is: " << hits1-hitsBegin << "" << std::endl;
    for(int i=2; i <= 11; i++)
    {   // fill cache with 10 more entries (11 in total)
        nearCache.put("key"+std::to_string(i),std::to_string(i));
    }
    // now key1 one should not be near
    rest = nearCache.get("key1");  // remote get. Stored near (this delete key2 nearly)
    delete rest;
    rest = nearCache.get("key2");  // remote get. Stored near (this delete key3 nearly)
    delete rest;
    rest = nearCache.get("key1");  // near
    delete rest;
    std::map<std::string,std::string> statsEnd= nearCache.stats();
    auto hitsEnd = std::stoi(statsEnd["hits"]);
    auto missesEnd = std::stoi(statsEnd["misses"]);
    std::cout << "Remote misses is: " << missesEnd-missesBegin << "" << std::endl;
    std::cout << "Remote hits is: " << hitsEnd-hitsBegin << "" << std::endl;
    }
    catch (Exception &e) {
    }
    try
    {
        nearCacheManager.stop();
    }
    catch (Exception &e) {
    }
}
示例#6
0
int basicTest(RemoteCacheManager &cacheManager, RemoteCache<K,V> &cache) {

    std::cout << "HotRod C++ Library version " << cache.getVersion() << std::endl;
    std::cout << "Protocol " << cache.getProtocolVersion() << std::endl;

    std::string k1("key13");
    std::string k2("key14");
    std::string v1("boron");
    std::string v2("chlorine");

    cache.clear();

    // put
    cache.put(k1, v1);
    std::unique_ptr<std::string> rv(cache.get(k1));
    assert_not_null("get returned null!", __LINE__, rv);
    if (rv->compare(v1)) {
        std::cerr << "get/put fail for " << k1 << " got " << *rv << " expected " << v1 << std::endl;
        return 1;
    }

    cache.put(k2, v2);
    std::unique_ptr<std::string> rv2(cache.get(k2));
    assert_not_null("get returned null!", __LINE__, rv2);
    if (rv2->compare(v2)) {
        std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected " << v2 << std::endl;
        return 1;
    }
 
    std::unique_ptr<std::string> badValue(cache.get(std::string("no such key in the cache")));
    if (badValue.get()) {
        std::cout << "non-existent key failure, got " << *badValue << std::endl;
        return 1;
    }

    std::cout << "PASS: simple get/put" << std::endl;

    {
      std::string k3("rolling");
      std::string v3("stones");
      std::string v4("beatles");

      // putIfAbsent
      cache.putIfAbsent(k3, v3);
      std::unique_ptr<std::string> rv3(cache.get(k3));
      assert_not_null("get returned null!", __LINE__, rv3);
      if (rv3->compare(v3)) {
          std::cerr << "putIfAbsent fail for " << k3 << " got " << *rv3 << " expected " << v3 << std::endl;
          return 1;
      }

      cache.putIfAbsent(k3, v4);
      std::unique_ptr<std::string> rv4(cache.get(k3));
      assert_not_null("get returned null!", __LINE__, rv4);
      if (rv4->compare(v3)) {
          std::cerr << "putIfAbsent fail for " << k3 << " got " << *rv4 << " expected " << v3 << std::endl;
          return 1;
      }

      std::cout << "PASS: simple putIfAbsent"  << std::endl;
    }
    {
      std::string k3("rolling");
      std::string v3("stones");
      std::string v4("beatles");

      // putIfAbsent
      cache.putIfAbsent(k3, v3);
      std::unique_ptr<std::string> rv3(cache.get(k3));
      assert_not_null("get returned null!", __LINE__, rv3);
      if (rv3->compare(v3)) {
          std::cerr << "putIfAbsent fail for " << k3 << " got " << *rv3 << " expected " << v3 << std::endl;
          return 1;
      }

      std::unique_ptr<std::string> rv4(cache.withFlags(FORCE_RETURN_VALUE).putIfAbsent(k3, v4));
      assert_not_null("get returned null!", __LINE__, rv4);
      if (rv4->compare(v3)) {
          std::cerr << "putIfAbsent fail for " << k3 << " got " << *rv4 << " expected " << v3 << std::endl;
          return 1;
      }

      std::cout << "PASS: simple putIfAbsent with force return value"  << std::endl;
    }

    std::string k3("rolling");
    std::string v3("stones");
    std::string v4("beatles");

    cache.put(k3, v3, 10, SECONDS);
    // getWithMetadata
    std::pair<std::shared_ptr<std::string>, MetadataValue> rv5 = cache.getWithMetadata(k3);
    if (!rv5.first.get() || rv5.second.lifespan != 10) {
        std::cerr << "getWithMetadata with mortal entry fail for " << k3 << " not found" << std::endl;
        return 1;
    }
    std::cout << "PASS: simple getWithMetadata with mortal entry"  << std::endl;

    cache.put(k3, v3);
    // getWithMetadata
    rv5 = cache.getWithMetadata(k3);
    if (!rv5.first.get()
        || rv5.second.lifespan >= 0 || rv5.second.created >= 0
        || rv5.second.maxIdle >= 0 || rv5.second.lastUsed >= 0) {
        std::cerr << "getWithMetadata with immortal entry fail for " << k3 << std::endl;
        return 1;
    }
    std::cout << "PASS: simple getWithMetadata with immortal entry"  << std::endl;

    // getWithVersion
    std::pair<std::shared_ptr<std::string>, VersionedValue> rv5a = cache.getWithVersion(k3);
    if (!rv5a.first.get()) {
        std::cerr << "getWithVersion fail for " << k3 << " not found" << std::endl;
        return 1;
    }

    std::cout << "PASS: simple getWithVersion"  << std::endl;

    // replaceWithVersion
    cache.replaceWithVersion(k3, v4, rv5.second.version);
    std::unique_ptr<std::string> rv6(cache.get(k3));
    assert_not_null("get returned null!", __LINE__, rv6);
    if (rv6->compare(v4)) {
        std::cerr << "replaceWithVersion fail for " << k3 << " got " << *rv6 << " expected " << v4 << std::endl;
        return 1;
    }

    cache.replaceWithVersion(k3, v3, rv5.second.version);
    std::unique_ptr<std::string> rv7(cache.get(k3));
    assert_not_null("get returned null!", __LINE__, rv7);
    if (rv7->compare(v4)) {
        std::cerr << "replaceWithVersion fail for " << k3 << " got " << *rv7 << " expected " << v4 << std::endl;
        return 1;
    }

    std::cout << "PASS: simple replaceWithVersion" << std::endl;

    // size
    uint64_t size = cache.size();
    if (size != 3) {
        std::cerr << "size fail got " << size << " expected 3 " << std::endl;
        return 1;
    }

    std::cout << "PASS: simple size" << std::endl;

    // stats
    const std::map<std::string,std::string>& stats = cache.stats();
    if (stats.empty()) {
        std::cerr << "stats fail with empty map" << std::endl;
        return 1;
    }

    std::cout << "  stats result is:" << std::endl;
    for(std::map<std::string,std::string>::const_iterator i=stats.begin(); i!=stats.end(); i++) {
        std::cout << "    key: " << i->first << ", value: " << i->second << std::endl;
    }

    std::cout << "PASS: simple stats" << std::endl;

    // clear
    cache.clear();
    uint64_t size2 = cache.size();
    if (size2 != 0) {
        std::cerr << "clear fail cache has size " << size2 << " expected 0 " << std::endl;
        return 1;
    }

    std::cout << "PASS: simple clear" << std::endl;

    std::string k4("real");
    std::string v5("madrid");
    std::string v6("barcelona");

    // put with FORCE_RETURN_VALUE flag
    std::unique_ptr<std::string> rv8(cache.withFlags(FORCE_RETURN_VALUE).put(k4,v5));
    if (rv8.get()) {
        std::cerr << "put with FORCE_RETURN_FLAG fail for " << k4 << " got " << *rv8 << " expected null pointer" << std::endl;
        return 1;
    }

    std::unique_ptr<std::string> rv9(cache.withFlags(FORCE_RETURN_VALUE).put(k4,v6));
    assert_not_null("get returned null!", __LINE__, rv9);
    if (rv9->compare(v5)) {
        std::cerr << "put with FORCE_RETURN_FLAG fail for " << k4 << " got " << *rv9 << " expected " << v5 << std::endl;
        return 1;
    }

    std::cout << "PASS: simple put with FORCE_RETURN_FLAG" << std::endl;

    // keySet
    std::set<std::shared_ptr<std::string> > keySet = cache.keySet();
    if (keySet.size()!=1) {
        std::cerr << "keySet fail got " << keySet.size() << " entries expected 1" << std::endl;
        return 1;
    }

    std::cout << "  keySet result is:" << std::endl;
    for(std::set<std::shared_ptr<std::string> >::const_iterator i=keySet.begin(); i!=keySet.end(); i++) {
        std::cout << "    key: " << *i->get() << std::endl;
    }

    std::cout << "PASS: simple keySet" << std::endl;

    // getBulk
    std::map<std::shared_ptr<std::string>,std::shared_ptr<std::string> > map = cache.getBulk();
    if (map.size()!=1) {
        std::cerr << "getBulk fail got" << map.size() << " entries expected 1" << std::endl;
        return 1;
    }

    std::cout << "  getBulk result is:" << std::endl;
    for(std::map<std::shared_ptr<std::string>,std::shared_ptr<std::string> >::const_iterator i=map.begin(); i!=map.end(); i++) {
        std::cout << "    key: " << *i->first.get() << ", value: " << *i->second.get() << std::endl;
    }

    std::cout << "PASS: simple getBulk" << std::endl;

    // replace
    cache.replace(k4,v5);
    std::unique_ptr<std::string> rv10(cache.get(k4));
    assert_not_null("get returned null!", __LINE__, rv10);
    if (rv10->compare(v5)) {
        std::cerr << "replace fail for " << k4 << " got " << *rv10 << " expected " << v5 << std::endl;
        return 1;
    }

    std::cout << "PASS: simple replace" << std::endl;
    try {
        RemoteCache<std::string, std::string> namedCache =
                cacheManager.getCache<std::string, std::string>("namedCache", false);
        std::unique_ptr<std::string> namedCacheV1(namedCache.get("k1"));
    }
    catch (...)
    {
        return 1;
    }
    std::cout << "PASS: get for namedCache" << std::endl;

    // get non-existing cache
        try {
            RemoteCache<std::string, std::string> non_existing =
                    cacheManager.getCache<std::string, std::string>("non-existing", false);
            std::cerr << "fail getCache for non-existing cache didn't throw exception" << std::endl;
            return 1;
        } catch (const HotRodClientException &ex) {
            std::cout << "PASS: get non-existing cache" << std::endl;
        } catch (const Exception &e) {
            std::cout << "is: " << typeid(e).name() << '\n';
            std::cerr << "fail unexpected exception: " << e.what() << std::endl;
            return 1;
        }

    // Async test
    std::string ak1("asyncK1");
    std::string av1("sun");

    std::string ak2("asyncK2");
    std::string av2("mercury");

    cache.clear();

/*    // Put ak1,av1 in async thread A
    std::future<std::string*> future_put= cache.putAsync(ak1,av1);
    // Get the value in this thread
    std::string* arv1= cache.get(ak1);
    if (future_put.wait_for(std::chrono::seconds(0))!=std::future_status::ready)
    {
    	// get happens before put completes, arv1 must be null
    	if (arv1)
    	{
    		std::cerr << "fail: expected null got value" << std::endl;
    		return 1;
    	}
    }
    // Now wait for put completion
    future_put.wait();

    // All is synch now
    std::string* arv11= cache.get(ak1);
    if (!arv11 || arv11->compare(av1))
    {
    	std::cout << "fail: expected " << av1 << "got " << (arv11 ? *arv11 : "null") << std::endl;
    	return 1;
    }

    // Read ak1 again, but in async way and test that the result is the same
    std::future<std::string*> future_ga= cache.getAsync(ak1);
    std::string* arv2= future_ga.get();
    if (!arv2 || arv2->compare(av1))
    {
    	std::cerr << "fail: expected " << av1 << " got " << (arv2 ? *arv2 : "null") << std::endl;
    	return 1;
    }
*/
    bool flag=false;
    // Now user pass a lambda func that will be executed in the async thread after the put completion
    std::future<std::string*> 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;
    		}
    	}
    }
    // 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;
    	}
    }
    // Same test for get
    flag=false;
    std::future<std::string*> future_get1= cache.getAsync(ak2,[&] (std::string *v){flag=true; return v;});

    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;
    	}
    }
    future_get1.wait();
    {
    	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::cout << "PASS: Async test" << std::endl;
    return 0;
}
示例#7
0
int main(int argc, char** argv) {

    int result=0;
	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);
        builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
//        builder.balancingStrategyProducer(transport::MyRoundRobinBalancingStrategy::newInstance);
        builder.balancingStrategyProducer(nullptr);
        RemoteCacheManager cacheManager(builder.build(), false);
        RemoteCache<std::string, std::string> &cachef1 = cacheManager.getCache<std::string, std::string>(false);
        RemoteCache<std::string, std::string> &cachet1 = cacheManager.getCache<std::string, std::string>(true);
        RemoteCache<std::string, std::string> &cachenf1 = cacheManager.getCache<std::string, std::string>("namedCache",false);
        RemoteCache<std::string, std::string> &cachent1 = cacheManager.getCache<std::string, std::string>("namedCache",true);

        RemoteCache<std::string, std::string> &cachet2 = cacheManager.getCache<std::string, std::string>(true);
        RemoteCache<std::string, std::string> &cachef2 = cacheManager.getCache<std::string, std::string>(false);
        RemoteCache<std::string, std::string> &cachenf2 = cacheManager.getCache<std::string, std::string>("namedCache",false);
        RemoteCache<std::string, std::string> &cachent2 = cacheManager.getCache<std::string, std::string>("namedCache",true);
        if (&cachef1 != &cachef2)
        {
            std::cerr << "Got two different cache instances(false): " << &cachef1 << "   " << &cachef2 << std::endl;
            result=1;
        }
        if (&cachet1 != &cachet2)
        {
            std::cerr << "Got two different cache instances(false): " << &cachet1 << "   " << &cachet2 << std::endl;
            result=1;
        }
        if (&cachef1 != &cachef2)
        {
            std::cerr << "Got two different cache instances(false): " << &cachenf1 << "   " << &cachenf2 << std::endl;
            result=1;
        }
        if (&cachef1 != &cachef2)
        {
            std::cerr << "Got two different cache instances(false): " << &cachent1 << "   " << &cachent2 << std::endl;
            result=1;
        }
    }
    if (result!=0)
    	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;
    {
        ConfigurationBuilder builder;
        builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222);
        builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
//        builder.balancingStrategyProducer(transport::MyRoundRobinBalancingStrategy::newInstance);
        builder.balancingStrategyProducer(nullptr);
        RemoteCacheManager cacheManager(builder.build(), false);
        BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
        BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
        RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
                &Marshaller<std::string>::destroy,
                vm,
                &Marshaller<std::string>::destroy);
        cacheManager.start();
        result = basicTest<std::string, std::string>(cacheManager, cache);

        try
        {
            std::map<std::string,std::string> s;
            std::string argName = std::string("a");
            std::string argValue = std::string("b");
            // execute() operation needs explicit JBossMarshalling<string> format for argument values
            s.insert(std::pair<std::string, std::string>(argName,JBasicMarshaller<std::string>::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<std::string>::addPreamble(script_name);
            std::string p_script=JBasicMarshaller<std::string>::addPreamble(script);
            RemoteCache<std::string, std::string> scriptCache=cacheManager.getCache<std::string,std::string>("___script_cache",false);
            scriptCache.put(p_script_name, p_script);
            std::vector<unsigned char> 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<std::string>((char*)execResult.data()));
            if (res.compare("abc")!=0)
            {
                std::cerr << "fail: cache.exec() returned unexpected result"<< std::endl;
                return 1;
            }
        } catch (const Exception& e) {
            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();
    }
    if (result!=0)
        return result;

    std::cout << "Basic Test with JBasicMarshaller" << std::endl;
    {
        ConfigurationBuilder builder;
        //        builder.balancingStrategyProducer(transport::MyRoundRobinBalancingStrategy::newInstance);
                builder.balancingStrategyProducer(nullptr);
        builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222);
        builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
        RemoteCacheManager cacheManager(builder.build(), false);
        JBasicMarshaller<std::string> *km = new JBasicMarshaller<std::string>();
        JBasicMarshaller<std::string> *vm = new JBasicMarshaller<std::string>();
        RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
                &Marshaller<std::string>::destroy,
                vm,
                &Marshaller<std::string>::destroy);
        cacheManager.start();
        result = basicTest<std::string, std::string>(cacheManager, cache);
        try
        {
            std::map<std::string,std::string> s;
            std::string argName = std::string("a");
            std::string argValue = std::string("b");
            // execute() operation needs explicit JBossMarshalling<string> format for argument values
            s.insert(std::pair<std::string, std::string>(argName,JBasicMarshaller<std::string>::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");
            cacheManager.getCache<std::string,std::string>(km,
                    &Marshaller<std::string>::destroy,
                    vm,
                    &Marshaller<std::string>::destroy,"___script_cache").put(script_name, script);
            std::vector<unsigned char> 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<std::string>((char *)execResult.data()));
            if (res.compare("abc")!=0)
            {
                std::cerr << "fail: cache.exec() returned unexpected result"<< std::endl;
                return 1;
            }
        } catch (const Exception& e) {
            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();
    }
    return result;
}
示例#8
0
int main(int argc, char** argv) {

    int result = 0;
    {
        ConfigurationBuilder builder;
        simple_data = "writer";
        secret_data = "somePassword";
        builder.protocolVersion(argc > 1 ? argv[1] : Configuration::PROTOCOL_VERSION_24);
        builder.addServer().host(argc > 2 ? argv[2] : "127.0.0.1").port(argc > 3 ? atoi(argv[3]) : 11222);
        builder.security().authentication().saslMechanism("PLAIN").serverFQDN(
                "node0").callbackHandler(callbackHandler).enable();
        builder.balancingStrategyProducer(nullptr);
        RemoteCacheManager cacheManager(builder.build(), false);
        BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
        BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
        RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
                &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
        cacheManager.start();
        cache.put("key", "value");
        try {
            std::shared_ptr<std::string> ret(cache.get("key"));
            std::cerr << "FAIL: 'writer' should not read" << std::endl;
            return -1;
        } catch (Exception& ex) {

        }
        std::cout << "PASS: '******' sasl authorization" << std::endl;
        cacheManager.stop();
    }

    {
        ConfigurationBuilder builder;
        simple_data = "reader";
        secret_data = "password";
        builder.protocolVersion(argc > 1 ? argv[1] : Configuration::PROTOCOL_VERSION_24);
        builder.addServer().host(argc > 2 ? argv[2] : "127.0.0.1").port(argc > 3 ? atoi(argv[3]) : 11222);
        builder.security().authentication().saslMechanism("DIGEST-MD5").serverFQDN(
                "node0").callbackHandler(callbackHandler).enable();
        builder.balancingStrategyProducer(nullptr);
        RemoteCacheManager cacheManager(builder.build(), false);
        BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
        BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
        RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
                &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
        cacheManager.start();
        std::shared_ptr<std::string> ret(cache.get("key"));
        try {
            cache.put("key", "value");
            std::cerr << "FAIL: 'reader' should not write" << std::endl;
            return -1;
        } catch (Exception& ex) {

        }
        std::cout << "PASS: '******' sasl authorization" << std::endl;
        cacheManager.stop();
    }

#if !defined _WIN32 && !defined _WIN64
    {
        kinit();
        ConfigurationBuilder builder;
        simple_data = "*****@*****.**";
        secret_data = "lessStrongPassword";
        builder.protocolVersion(argc > 1 ? argv[1] : Configuration::PROTOCOL_VERSION_24);
        builder.addServer().host(argc > 2 ? argv[2] : "127.0.0.1").port(argc > 3 ? atoi(argv[3]) : 11222);
        builder.security().authentication().saslMechanism("GSSAPI").serverFQDN(
                "node0").callbackHandler(callbackHandler).enable();
        builder.balancingStrategyProducer(nullptr);
        RemoteCacheManager cacheManager(builder.build(), false);
        BasicMarshaller<std::string> *km = new BasicMarshaller<std::string>();
        BasicMarshaller<std::string> *vm = new BasicMarshaller<std::string>();
        RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
                &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("authCache"));
        cacheManager.start();
        try {
            cache.put("key", "value");
            std::shared_ptr<std::string> ret(cache.get("key"));
            result = 0;
        } catch (Exception& ex) {
            std::cerr << "FAIL: 'supervisor' should read and write" << std::endl;
            result = -1;
        }
        cacheManager.stop();
        std::cout << "PASS: '******' sasl authorization" << std::endl;
        kdestroy();
    }
#endif
    return result;
}
示例#9
0
// Hotrod 2.8 with mediatype allows to store and retrieve entries in different data format
// this can be done specifying the mediatype for the communication
int main(int argc, char** argv) {
    ConfigurationBuilder builder;
    builder.protocolVersion(Configuration::PROTOCOL_VERSION_28);
    builder.addServer().host("127.0.0.1").port(11222);
    builder.balancingStrategyProducer(nullptr);
    RemoteCacheManager cacheManager(builder.build(), false);
    cacheManager.start();

    std::cout << "Tests for CacheManager" << std::endl;
    Marshaller<std::string> *km = new JBasicMarshaller<std::string>();
    Marshaller<std::string> *vm = new JBasicMarshaller<std::string>();
    RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km,
            &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("transcodingCache"));

    // Define a data format for json
    DataFormat<std::string, std::string> df;
    df.keyMediaType = MediaType(APPLICATION_UNKNOWN_TYPE);
    df.valueMediaType = MediaType(APPLICATION_JSON_TYPE);
    df.valueMarshaller.reset(new BasicMarshaller<std::string>());
    RemoteCache<std::string, std::string> cacheJson = cache.withDataFormat(&df);
    // Define a data forma for jboss marshaller
    DataFormat<std::string, std::string> df1;
    df1.keyMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE);
    df1.valueMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE);
    RemoteCache<std::string, std::string> cacheJBoss = cache.withDataFormat(&df1);

    std::string k1("key13");
    std::string k2("key14");
    std::string v1("boron");
    std::string v2("chlorine");

    cache.clear();
    // put
    cache.put(k1, v1);
    std::unique_ptr<std::string> rv(cache.get(k1));
    assert_not_null("get returned null!", __LINE__, rv);
    if (rv->compare(v1)) {
        std::cerr << "get/put fail for " << k1 << " got " << *rv << " expected " << v1 << std::endl;
        return 1;
    }

    cache.put(k2, v2);
    std::unique_ptr<std::string> rv2(cache.get(k2));
    assert_not_null("get returned null!", __LINE__, rv2);
    if (rv2->compare(v2)) {
        std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected " << v2 << std::endl;
        return 1;
    }
    std::unique_ptr<std::string> rv2Json(cacheJson.get(k2));
    assert_not_null("get returned null!", __LINE__, rv2Json);
    if (rv2Json->compare("\"chlorine\"")) {
        std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected \"boron\"" << std::endl;
        return 1;
    }

    std::unique_ptr<std::string> rv2JBoss(cacheJBoss.get(k2));
    assert_not_null("get returned null!", __LINE__, rv2JBoss);
    if (rv2JBoss->compare(v2)) {
        std::cerr << "get/put fail for " << k2 << " got " << *rv2JBoss << " expected " << v2 << std::endl;
        return 1;
    }
    }