TEST(QiAutoService, AutoReConnect) { TestSessionPair pair; boost::shared_ptr<Pong> pong(new Pong()); qi::AnyObject pongAsObject = qi::AnyValue::from(pong).to<qi::AnyObject>(); qi::Future<unsigned int> fut = pair.server()->registerService("Ping pong", pongAsObject); fut.wait(); qi::AutoService<PongProxy> as("Ping pong", pair.client()); as.waitForReady().wait(); EXPECT_EQ(42, as->incr()); pair.server()->unregisterService(fut.value()); qi::Future<unsigned int> fut2 = pair.server()->registerService("Ping pong", pongAsObject); fut2.wait(); while(true) { try { EXPECT_EQ(43, as->incr().value()); break; } catch (std::runtime_error e) { std::cout << e.what() << std::endl; } } }
TEST(Proxy, Property) { boost::shared_ptr<Bar> bar(new Bar); qi::AnyObject gbar = qi::AnyReference::from(bar).toObject(); ASSERT_TRUE(!!gbar); // The session must die before bar. TestSessionPair p; p.server()->registerService("bar", gbar); // we need that to force two clients p.server()->registerService("bar2", gbar); qi::AnyObject client = p.client()->service("bar"); ASSERT_EQ(0, client.call<int>("sum")); qi::ProxyProperty<int> pp(client, "prop"); bar->set(1); ASSERT_EQ(1, pp.get()); pp.set(2); ASSERT_EQ(2, bar->get()); // althoug PropertyProxy::set is itself synchronous, notify on remote end // may be asynchronous, so subscribe below may come too soon and catch // the pp.set above qi::os::msleep(100); qiLogDebug() << "subscribe"; bar->subscribe(); qi::os::msleep(100); qiLogDebug() << "set 3"; pp.set(3); // this is an event, all notify are asychronous PERSIST_ASSERT(, bar->sum() == 3, 500); Bar bar2; qi::SignalLink l = pp.connect(boost::bind(&Bar::onProp, &bar2, _1)); bar->set(4); // this one is async (remote notify of local property set) PERSIST_ASSERT(, bar2.sum() == 4, 500); pp.disconnect(l); bar->set(5); // we expect an async op *not* to happen, no choice but wait. qi::os::msleep(200); ASSERT_EQ(4, bar2.sum()); // reconnect to see if disconnect did not break anything l = pp.connect(boost::bind(&Bar::onProp, &bar2, _1)); bar->set(4); PERSIST_ASSERT(, bar2.sum() == 8, 500); // proxy-proxy qi::AnyObject client2 = p.client()->service("bar2"); qi::ProxyProperty<int> pp2(client2, "prop"); Bar bar3; pp2.connect(boost::bind(&Bar::onProp, &bar3, _1)); qiLogDebug() << "set 2"; pp.set(2); PERSIST(, bar3.sum() == 2, 1000); ASSERT_EQ(2, bar3.sum()); PERSIST(, bar2.sum() == 10, 500); ASSERT_EQ(10, bar2.sum()); qiLogDebug() << "set 3"; pp2.set(3); PERSIST_ASSERT(, bar2.sum() == 13, 500); PERSIST_ASSERT(, bar3.sum() == 5, 500); }
TEST(QiSession, RegisterUnregisterSameSession) { int a = 1000; TestSessionPair p; EXPECT_TRUE(p.client()->isConnected()); boost::thread worker(boost::bind(&alternateModule, p.server())); while (a) { a--; try { qi::Future<qi::AnyObject> fut = p.server()->service("TestToto"); if (fut.hasError()) { std::cout << "Call error:" << fut.error() << std::endl; continue; } fut.value().call<std::string>("reply", "plif"); } catch(const std::exception& e) { std::cout << "Call exception: " << e.what() << std::endl; } } worker.interrupt(); worker.join(); }
TEST(QiSession, multiClose) { qi::Atomic<int> counter; TestSessionPair p; boost::thread(session_close, p.client(), &counter); boost::thread(session_close, p.client(), &counter); boost::thread(session_close, p.server(), &counter); boost::thread(session_close, p.server(), &counter); boost::thread(session_close, p.server(), &counter); while (*counter != 5) qi::os::msleep(50); }
TEST(QiSession, getSimpleService) { TestSessionPair pair; qi::DynamicObjectBuilder ob; ob.advertiseMethod("reply", &reply); qi::AnyObject obj(ob.object()); pair.server()->registerService("serviceTest", obj); qi::AnyObject object = pair.server()->service("serviceTest"); EXPECT_TRUE(object); }
TEST(QiSession, CallOnCloseSession) { qi::Session sd; qi::Future<void> f = sd.listenStandalone("tcp://0.0.0.0:0"); int timeToWait = 1; for(int j = 0; j < 10 ; j ++) { for (int i = 0; i < 20; i++) { TestSessionPair p; std::cout << "time to wait is:" << timeToWait << std::endl; qi::SessionPtr s1 = p.server(); qi::SessionPtr s2 = p.client(); qi::DynamicObjectBuilder ob; ob.advertiseMethod("reply", &reply); qi::AnyObject obj(ob.object()); s1->registerService("service1", obj); qi::AnyObject myService; myService = s2->service("service1"); boost::thread myThread(boost::bind(&myCall, myService)); qi::os::msleep(timeToWait); s1->close(); qi::os::msleep(3); } timeToWait = timeToWait +1; } }
TEST(Proxy, Signal) { boost::shared_ptr<Foo> foo(new Foo); qi::AnyObject gfoo = qi::AnyReference::from(foo).toObject(); ASSERT_TRUE(!!gfoo); qi::detail::printMetaObject(std::cerr, gfoo.metaObject()); // The session must die before foo. TestSessionPair p; p.server()->registerService("foo", gfoo); qi::AnyObject client = p.client()->service("foo"); ASSERT_EQ(0, client.call<int>("count1")); qi::ProxySignal<void(int, int)> proxy1(client, "sig1"); foo->subscribe1(); proxy1(1, 2); PERSIST_ASSERT(, foo->count1() == 3, 500); // small hack, reuse foo function to test callback on proxy signal Foo foo2; qi::SignalLink l = proxy1.connect(boost::bind(&Foo::on1, &foo2, _1, _2)); proxy1(3, 4); PERSIST_ASSERT(, foo->count1() == 10, 500); PERSIST_ASSERT(, foo2.count1() == 7, 500); proxy1.disconnect(l); proxy1(1, 1); PERSIST_ASSERT(, foo->count1() == 12, 500); PERSIST_ASSERT(, foo2.count1() == 7, 500); }
TEST(QiAutoService, Signals) { int n = 0; TestSessionPair pair; boost::shared_ptr<Pong> pong(new Pong()); qi::AnyObject pongAsObject = qi::AnyValue::from(pong).to<qi::AnyObject>(); qi::Future<unsigned int> fut = pair.server()->registerService("Ping pong", pongAsObject); fut.wait(); qi::AutoService<PongProxy> as("Ping pong", pair.client()); as.serviceRemoved.connect(boost::bind<void>(&setTo42, &n)); pair.server()->unregisterService(fut.value()).wait(); qi::os::msleep(100); EXPECT_EQ(42, n); }
TEST(QiSession, RegisterUnregisterSameSessionStruct) { int a = 1000; if (TestMode::getTestMode() == TestMode::Mode_SSL) { a /= 100; } TestSessionPair p; EXPECT_TRUE(p.client()->isConnected()); boost::thread worker(boost::bind(&alternateModule, p.server())); while (a) { a--; try { qi::Future<qi::AnyObject> fut = p.server()->service("TestToto"); if (fut.hasError()) { std::cout << "Call error:" << fut.error() << std::endl; continue; } MyStruct ms; ms.i = 32; ms.j = 42; ms.titi = "tutu"; qi::Future<MyStruct> ret = fut.value().async<MyStruct>("reply2", ms); ret.wait(); if (ret.hasError()) { std::cout << "returned an error:" << fut.error() << std::endl; continue; } ASSERT_EQ(ms.i, ret.value().i); ASSERT_EQ(ms.j, ret.value().j); ASSERT_EQ(ms.titi, ret.value().titi); } catch(const std::exception& e) { std::cout << "Call exception: " << e.what() << std::endl; } } worker.interrupt(); worker.join(); }
TEST(QiSession, getSimpleServiceTwiceUnexisting) { TestSessionPair pair; qi::Future<qi::AnyObject> f1 = pair.server()->service("xxxLOL"); qi::Future<qi::AnyObject> f2 = pair.server()->service("xxxLOL"); f1.wait(); f2.wait(); EXPECT_TRUE(f1.hasError()); EXPECT_TRUE(f2.hasError()); }
TEST(QiAutoService, SimpleUsage) { TestSessionPair pair; boost::shared_ptr<Pong> pong(new Pong()); qi::AnyObject pongAsObject = qi::AnyValue::from(pong).to<qi::AnyObject>(); qi::Future<unsigned int> fut = pair.server()->registerService("Ping pong", pongAsObject); fut.wait(); qi::AutoService<PongProxy> as("Ping pong", pair.client()); as.waitForReady().wait(); EXPECT_EQ(42, as->incr().value()); }
TEST(QiSession, getSimpleServiceTwice) { TestSessionPair pair; qi::DynamicObjectBuilder ob; ob.advertiseMethod("reply", &reply); qi::AnyObject obj(ob.object()); pair.server()->registerService("serviceTest", obj); qi::Future<qi::AnyObject> f1 = pair.client()->service("serviceTest"); qi::Future<qi::AnyObject> f2 = pair.client()->service("serviceTest"); f1.wait(); f2.wait(); EXPECT_TRUE(f1.value().asGenericObject() == f2.value().asGenericObject()); }
TEST(QiAutoService, IsReadyMethod) { int n = 0; TestSessionPair pair; boost::shared_ptr<Pong> pong(new Pong()); qi::AnyObject pongAsObject = qi::AnyValue::from(pong).to<qi::AnyObject>(); qi::Future<unsigned int> fut = pair.server()->registerService("Ping pong", pongAsObject); fut.wait(); qi::AutoService<PongProxy> as("Ping pong", pair.client()); qi::Future<void> fut2 = as.waitForReady(); fut2.connect(&setTo42, &n); fut2.wait(); qi::os::msleep(100); EXPECT_EQ(42, n); }
TEST(TestObjectDyn, PropertyConnectOnDynamicObject) { qi::Property<int> prop; qi::DynamicObjectBuilder builder; builder.advertiseProperty("prop", &prop); TestSessionPair p; p.server()->registerService("Serv", builder.object()); qi::Promise<int> prom; qi::AnyObject obj = p.client()->service("Serv"); obj.connect("prop", boost::function<void(int)>([&prom](int i){ prom.setValue(i); })); prop.set(42); ASSERT_EQ(42, prom.future().value()); }