bool connect () { ResultCallbackHelper helper; mClient->connect(helper.onResultFunc()); bool suc = helper.waitReadyAndNoError(10000); if (!suc) { fprintf (stderr, "Could not connect %s", mClient->ownId().c_str()); } else { mClient->setPresence (IMClient::PS_UNKNOWN, ""); } return suc; }
int testAutoConnect () { shared_ptr<XMPPStream> stream (new XMPPStream); BoshXMPPConnection con; XMPPConnection::XmppConnectDetails details; tcheck1 (details.setTo ("xmpp://*****:*****@localhost/autoConnect")); con.setConnectionDetails(details); ResultCallbackHelper conResultHandler; Error e = con.connect(stream, 2500, conResultHandler.onResultFunc()); tcheck1 (!e && conResultHandler.waitReadyAndNoError(5000)); tcheck1 (stream->channelInfo().authenticated); tcheck1 (stream->ownFullJid() == "autotest1@localhost/autoConnect"); e = stream->close(); tcheck1 (!e); return 0; }
/// creates an UDT server and connects using a socket to it int testUDTServer () { UDTServer server; Error e = server.listen (0); tassert (!e, "Should find a port"); int port = server.port (); printf ("Listening on %d\n", port); UDTSocket socket; ResultCallbackHelper helper; socket.connectAsync ("localhost", port, helper.onResultFunc()); helper.wait(2000); tassert (helper.ready() && helper.result() == error::InvalidArgument, "Should not accept DNS names"); socket.connectAsync ("127.0.0.1", port, helper.onResultFunc()); // localhost won't work! tassert (helper.waitReadyAndNoError(2000), "Should connect"); test::millisleep_locked (100); tassert (server.pendingConnections() == 1, "Should have one pending connection"); UDTSocket * socket2 = server.nextConnection(); tassert (socket2->error() == NoError, "Incoming socket shall be ok"); tassert (socket.error() == NoError, "Outgoing socket shall be ok"); tassert (testSockets (&socket, socket2) == 0, "Sockets shall support this test"); socket2->close(); test::millisleep_locked (100); tassert1 (!socket.isConnected()); delete socket2; return 0; }
// Test reusing using UDT Sockets as a replacement of UDP sockets int testReusage () { // Init two UDP Sockets UDPSocket base1; UDPSocket base2; Error e = base1.bind(); tcheck (!e, "Shall bind"); e = base2.bind(); tcheck (!e, "Shall bind"); int aPort = base1.port (); int bPort = base2.port (); // Sending some data between them e = base1.sendTo ("127.0.0.1", bPort, sf::createByteArrayPtr("Hello World")); tcheck (!e, "Shall send"); e = base2.sendTo ("127.0.0.1", aPort, sf::createByteArrayPtr ("You too!")); tcheck (!e, "Shall send"); test::millisleep_locked (1000); tassert1 (base1.datagramsAvailable()); tassert1 (base2.datagramsAvailable()); // Checking data String from; int port = -1; ByteArrayPtr data; data = base2.recvFrom (&from, &port); tcheck (data && *data == ByteArray ("Hello World"), "Received wrong data"); data = base1.recvFrom (&from, &port); tcheck (data && *data == ByteArray ("You too!"), "Received wrong data"); // Converting to UDT Sockets. UDTSocket a; UDTSocket b; e = a.rebind(&base1); tcheck (!e, "Shall have no problem to rebind"); e = b.rebind (&base2); tcheck (!e, "Shall have no problem to rebind"); tassert1 (a.port() == aPort); tassert1 (b.port() == bPort); printf ("Ports now A: %d B: %d\n", a.port(), b.port()); // Let's connect them... ResultCallbackHelper helperA; ResultCallbackHelper helperB; b.connectRendezvousAsync("127.0.0.1", a.port(), helperB.onResultFunc()); a.connectRendezvousAsync("127.0.0.1", b.port(), helperA.onResultFunc()); tcheck1 (helperA.waitReadyAndNoError(1000)); tcheck1 (helperB.waitReadyAndNoError(1000)); test::millisleep_locked (100); // giving IOService thread enough time to connectRendezvous tassert (testSockets (&a, &b) == 0, "supports shall work"); return 0; }
/// Test async rendezvous int testAsyncRendezvous () { UDTSocket a; UDTSocket b; Error e = a.bind(); tassert1 (!e); e = b.bind(); tassert1 (!e); ResultCallbackHelper helperA; ResultCallbackHelper helperB; a.connectRendezvousAsync("127.0.0.1", b.port(), helperA.onResultFunc()); b.connectRendezvousAsync("127.0.0.1", a.port(), helperB.onResultFunc()); bool suc = helperA.waitUntilReady (2000); tassert (suc, "Must connect"); suc = helperB.waitUntilReady(2000); tassert (suc, "Must connect"); tassert1 (testConnectivity (&a, &b) == 0); return 0; }
int testManualConnect () { int timeoutMs = 1000000; // for debugging Url url = "http://*****:*****@" + host, host); Error e = stream.startInitAfterHandshake(transport); tcheck1(!e); stream.waitFeatures(helper.onResultFunc()); tcheck (helper.waitReadyAndNoError(timeoutMs), "Should receive features"); // No TLS/reconnecting, is already encrypted. // Try to login e = stream.authenticate(user,pass, helper.onResultFunc()); tcheck (!e, "Should start authenticate"); tcheck (helper.waitReadyAndNoError(timeoutMs), "Should authenticate"); // Reuse transport->restart(); e = stream.startInitAfterHandshake(transport); tcheck (!e, "Should reuse"); // Waiting for features e = stream.waitFeatures(helper.onResultFunc()); tcheck (!e, "Shall wait for features again"); tcheck (helper.waitReadyAndNoError(timeoutMs), "Shall wait for a new feature set"); // Bind resource name e = stream.bindResource("testcase", bind (helper.onResultFunc(), _1)); tcheck (!e, "Should start resource bind"); tcheck (helper.waitReadyAndNoError(timeoutMs), "Should bind resource"); // Start session e = stream.startSession(helper.onResultFunc()); tcheck (!e, "Should start session"); tcheck (helper.waitReadyAndNoError(timeoutMs), "Should start session"); // Ok thats it e = stream.close(); tcheck (!e, "Should close again"); return 0; }
int main (int argc, char * argv[]) { schnee::SchneeApp app (argc, argv); SF_SCHNEE_LOCK; AsyncExample example; { std::cout << "Time out calculation" << std::endl; { AsyncExample::Op1 op (regTimeOutMs (-1)); assert (op.lastingTimeMs() == -1); // infinite time } { AsyncExample::Op1 op (regTimeOutMs (10000)); test::sleep_locked (1); int ms = op.lastingTimeMs(); assert (ms > 8000 && ms < 9500); // around 9000 } { AsyncExample::Op1 op (regTimeOutMs (1)); test::sleep_locked (1); int ms = op.lastingTimeMs(); // exactly 0 time is out assert (ms == 0); } { AsyncExample::Op1 op (regTimeOutMs (10000)); test::sleep_locked (1); int ms = op.lastingTimeMs(0.5f); assert (ms > 4000 && ms < 5000); // around 4500 } { // opcb_locked function ResultCallbackHelper helper; example.asyncCall3(100, helper.onResultFunc()); bool suc = helper.waitReadyAndNoError(100); assert (suc); } { // opcb_locked function2 ResultCallbackHelper helper; example.asyncCall3Param0(100, helper.onResultFunc()); bool suc = helper.waitReadyAndNoError(100); assert (suc); } } { std::cout << "Callback against non-existing instances" << std::endl; { DisappearingHandler handler; example.asyncCall1 (100, dMemFun (&handler, &DisappearingHandler::handle)); } test::sleep_locked (1); } { std::cout << "Testing TimeOut (in backward order)" << std::endl; for (int i = 0; i < 1000; i++) { int timeOutInMs = 2000 - i; OpId id = example.asyncCall1 (timeOutInMs, mustTimeOutOrdered); std::cout << "Kicking id " << id << " timeOutInMs " << timeOutInMs << std::endl; } test::sleep_locked(3); //< All must time out assert (example.waitingOps() == 0); //< No call has to left } { std::cout << "cancel by hand" << std::endl; for (int i = 0; i < 1000; i++) { example.asyncCall1 (1000, mustBeOffline, 50); } example.cancelCall (50, error::TargetOffline); test::sleep_locked (1); assert (example.waitingOps() == 0); } { std::cout << "Testing finishing" << std::endl; // - finishing works OpId startId(0), endId(0); int iterations = 1000; for (int i = 0; i < iterations; i++) { OpId id = example.asyncCall2 (2000 - (i / 10), &mustFinish); if (i == 0) startId = id; if (i == iterations - 1) endId = id; } assert (endId - startId == iterations - 1); for (int i = 0; i < iterations; i++) { xcall(abind (dMemFun (&example, &AsyncExample::finishCall), startId + i)); // xcall (bind (&AsyncExample::finishCall, &example, startId + i)); } test::sleep_locked (3); assert (example.waitingOps() == 0); //< No call has to left } { std::cout << "Testing new taks during timeOut" << std::endl; // - starting new tasks during timeOut works example.asyncCall1 (100, abind (&startTask, 0, &example)); // example.asyncCall1 (100, bind (startTask, _1, _2, 0, &example)); test::sleep_locked (1); assert (example.waitingOps() == 0); //< No call has to left } { std::cout << "Testing new tasks during finish" << std::endl; // - starting new tasks during finishing // OpId id = example.asyncCall2 (1000, bind (startTask, _1, _2, _3, &example)); OpId id = example.asyncCall2 (1000, abind (&startTask, &example)); // xcall (bind (&AsyncExample::finishCall, &example, id)); xcall ( abind (dMemFun (&example, &AsyncExample::finishCall), id)); test::sleep_locked (1); } { std::cout << "Testing correct shut down" << std::endl; AsyncExample example2; for (int i = 0; i < 1000; i++) { example.asyncCall1 (2000 - (i / 10), mayNotHappen); } assert (example.waitingOps() == 1000); } return 0; }