Beispiel #1
0
void PutGetTest::SetUp() {
	if (PutGetTest::remoteCacheManager == nullptr){
		ConfigurationBuilder builder;
		builder.addServer().host("127.0.0.1").port(11222);
		builder.protocolVersion(Configuration::PROTOCOL_VERSION_24);
		builder.balancingStrategyProducer(nullptr);
		PutGetTest::remoteCacheManager.reset(new RemoteCacheManager(builder.build(), false));
	}
};
Beispiel #2
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;
}
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) {
    }
}
int main(int argc, char** argv) {
	std::cout << "Tests for Query" << std::endl;
    ConfigurationBuilder builder;
    builder.addServer().host(argc > 1 ? argv[1] : "127.0.0.1").port(argc > 2 ? atoi(argv[2]) : 11222).protocolVersion(Configuration::PROTOCOL_VERSION_24);
    builder.balancingStrategyProducer(nullptr);
    RemoteCacheManager cacheManager(builder.build(), false);
    cacheManager.start();
    //Create the Protobuf Metadata cache peer with a Protobuf marshaller
    auto *km = new BasicTypesProtoStreamMarshaller<std::string>();
    auto *vm = new BasicTypesProtoStreamMarshaller<std::string>();
    auto metadataCache = cacheManager.getCache<std::string, std::string>(
    		km, &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy,PROTOBUF_METADATA_CACHE_NAME, false);
    // Install the data model into the Protobuf metadata cache
    metadataCache.put("sample_bank_account/bank.proto", read("proto/bank.proto"));
    if (metadataCache.containsKey(ERRORS_KEY_SUFFIX))
	{
      std::cerr << "fail: error in registering .proto model" << std::endl;
      return -1;
	}
    //Create the application cache peer with a Protobuf marshaller
    auto *testkm = new BasicTypesProtoStreamMarshaller<int>();
    auto *testvm = new ProtoStreamMarshaller<sample_bank_account::User>();
    auto testCache = cacheManager.getCache<int, sample_bank_account::User>(
    		testkm, &Marshaller<int>::destroy, testvm, &Marshaller<sample_bank_account::User>::destroy, "namedCache", false);

    //Fill the cache with the application data: two users Tom and Jerry
    testCache.clear();
    sample_bank_account::User_Address a;
    sample_bank_account::User user1;
    user1.set_id(3);
    user1.set_name("Tom");
    user1.set_surname("Cat");
    user1.set_gender(sample_bank_account::User_Gender_MALE);
    sample_bank_account::User_Address * addr= user1.add_addresses();
    addr->set_street("Via Roma");
    addr->set_number(3);
    addr->set_postcode("202020");
    testCache.put(3, user1);
    user1.set_id(4);
    user1.set_name("Jerry");
    user1.set_surname("Mouse");
    addr->set_street("Via Milano");
    user1.set_gender(sample_bank_account::User_Gender_MALE);
    testCache.put(4, user1);
    // Simple query to get User objects
	{
		QueryRequest qr;
		std::cout << "Query: from sample_bank_account.User" << std::endl;
		qr.set_jpqlstring("from sample_bank_account.User");
		QueryResponse resp = testCache.query(qr);
		std::vector<sample_bank_account::User> res;
		unwrapResults(resp, res);
		for (auto i = 0; i < res.size(); i++) {
			std::cout << "User(id=" << res[i].id() << ",name=" << res[i].name()
					<< ",surname=" << res[i].surname() << ")" << std::endl;
		}
	}
    // Simple query to get User objects with where condition
	{
		QueryRequest qr;
		std::cout << "from sample_bank_account.User u where u.addresses.street=\"Via Milano\"" << std::endl;
		qr.set_jpqlstring("from sample_bank_account.User u where u.addresses.street=\"Via Milano\"");
		QueryResponse resp = testCache.query(qr);
		std::vector<sample_bank_account::User> res;
		unwrapResults(resp, res);
		for (auto i = 0; i < res.size(); i++) {
			std::cout << "User(id=" << res[i].id() << ",name=" << res[i].name()
					<< ",surname=" << res[i].surname() << ")" << std::endl;
		}
	}
	// Simple query to get projection (name, surname)
	{
		QueryRequest qr;
		std::cout << "Query: select u.name, u.surname from sample_bank_account.User u" << std::endl;
		qr.set_jpqlstring(
				"select u.name, u.surname from sample_bank_account.User u");
		QueryResponse resp = testCache.query(qr);
        //Typed resultset
		std::vector<std::tuple<std::string, std::string> > prjRes;

		unwrapProjection(resp, prjRes);
		for (auto i = 0; i < prjRes.size(); i++) {
			std::cout << "Name: " << std::get<0> (prjRes[i])
							  << " Surname: " << std::get<1> (prjRes[i]) << std::endl;
		}
	}
	{
		QueryRequest qr;
		qr.set_jpqlstring("select count(u.id) from sample_bank_account.User u");
		QueryResponse resp = testCache.query(qr);
		int i = unwrapSingleResult<int>(resp);
		std::cout << "Result is: " << i << std::endl;
	}
	cacheManager.stop();
	return 0;
}
Beispiel #5
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;
}
Beispiel #6
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;
}
Beispiel #7
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;
    }
    }