TEST_FIXTURE(Test_case_wrapper, socket_processor_udp) { Socket_processor::Ptr sp = ugcs::vsm::Socket_processor::Get_instance(); Request_worker::Ptr worker = Request_worker::Create("UT socket processor worker"); CHECK(sp->Is_enabled()); CHECK(!worker->Is_enabled()); worker->Enable(); CHECK(worker->Is_enabled()); Socket_processor::Socket_listener::Ref server_stream; Socket_processor::Stream::Ref client_stream; Io_result server_result; Io_result client_result; Io_buffer::Ptr client_buf; Io_buffer::Ptr server_buf; // forward declaration std::function<void(Io_buffer::Ptr, Io_result, Socket_address::Ptr)> server_read_complete; Socket_address::Ptr bound_client_point = nullptr; std::function<void(Io_result)> server_write_complete = [&](Io_result result) { if (result != Io_result::OK) { std::cout<<"WRITE NOK"<<std::endl; } else { // initiate next read after write; server_stream->Read_from(1000, Make_callback(server_read_complete, server_buf, server_result, bound_client_point), worker); } }; // write back into socket everything received. server_read_complete = [&](Io_buffer::Ptr buffer, Io_result result, Socket_address::Ptr peer) { if (result != Io_result::OK) { server_stream->Close(); } else { // echo back server_stream->Write_to(buffer, peer, Make_callback(server_write_complete, server_result), worker); } }; auto server_point = Socket_address::Create("127.0.0.1", "32769"); sp->Connect(server_point, Make_socket_listen_callback([&](Socket_processor::Stream::Ref l, Io_result){ client_stream = l; }), Request_temp_completion_context::Create(), Io_stream::Type::UDP); // Create serverside socket. sp->Bind_udp(server_point, Make_socket_listen_callback([&](Socket_processor::Stream::Ref l, Io_result){ server_stream = l; server_stream->Read_from(1000, Make_callback(server_read_complete, server_buf, server_result, bound_client_point), worker); })); // Send data to server client_stream->Write(Io_buffer::Create("0123456789"), Make_setter(client_result)); client_stream->Write(Io_buffer::Create("abcdefghij"), Make_setter(client_result)); // Read back from server client_stream->Read(10, 10, Make_setter(client_buf, client_result)); // Check the data CHECK_EQUAL("0123456789", client_buf->Get_string().c_str()); // Read back from server client_stream->Read(100, 10, Make_setter(client_buf, client_result)).Timeout(std::chrono::milliseconds(1000)); CHECK(client_result == Io_result::OK); // Check the data CHECK_EQUAL("abcdefghij", client_buf->Get_string().c_str()); client_stream->Close(); server_stream->Close(); worker->Disable(); }
TEST_FIXTURE(Test_case_wrapper, ssdp_processor) { auto sp = ugcs::vsm::Socket_processor::Get_instance(); auto discoverer1 = ugcs::vsm::Service_discovery_processor::Create( Socket_address::Create("239.198.46.46","11111")); auto discoverer2 = ugcs::vsm::Service_discovery_processor::Create( Socket_address::Create("239.198.46.46","11111")); auto tp = ugcs::vsm::Timer_processor::Get_instance(); tp->Enable(); auto proc_context = ugcs::vsm::Request_processor::Create("callback processor"); proc_context->Enable(); Request_worker::Ptr worker = Request_worker::Create( "UT processor worker", std::initializer_list<Request_container::Ptr>({proc_context})); std::string service_type1("Service1"); std::string service_type2("Service2"); worker->Enable(); sp->Enable(); discoverer1->Enable(); discoverer2->Enable(); std::promise<void> detected1; std::promise<void> detected2; bool got_result1 = false; bool got_result2 = false; auto detector = [&](std::string type, std::string, std::string loc, std::string id, bool) { if (type == service_type1) { LOG("Discovered service %s, loc=%s id=%s", type.c_str(), loc.c_str(), id.c_str()); if (!got_result1) { detected1.set_value(); got_result1 = true; } } if (type == service_type2) { LOG("Discovered service %s, loc=%s id=%s", type.c_str(), loc.c_str(), id.c_str()); if (!got_result2) { detected2.set_value(); got_result2 = true; } } }; // Test Deactivate functionality. discoverer1->Advertise_service(service_type1, "n", "l"); std::this_thread::sleep_for(std::chrono::milliseconds(100)); discoverer1->Unadvertise_service(service_type1, "n", "l"); discoverer1->Subscribe_for_service(service_type1, Service_discovery_processor::Make_detection_handler(detector), proc_context); // Make sure subscriber1 get response from noify, not from m-search. std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Test subscribe and advertise. discoverer1->Advertise_service(service_type1, "name1", "tcp://{local_address}/path"); discoverer2->Advertise_service(service_type2, "name2", "tcp://location1"); // Make sure subscriber2 get response from m-search, not from notify. std::this_thread::sleep_for(std::chrono::milliseconds(100)); discoverer2->Subscribe_for_service(service_type2, Service_discovery_processor::Make_detection_handler(detector), proc_context); // Wait for 5 seconds to discover the services. // Normally it happnens much faster, but on our buildserver under valgrind it takes ~3 seconds. auto ret = detected1.get_future().wait_for(std::chrono::seconds(5)); CHECK (ret == std::future_status::ready); if (ret == std::future_status::ready) { ret = detected2.get_future().wait_for(std::chrono::seconds(5)); CHECK (ret == std::future_status::ready); } discoverer2->Disable(); discoverer1->Disable(); proc_context->Disable(); worker->Disable(); sp->Disable(); tp->Disable(); }
TEST_FIXTURE(Test_case_wrapper, socket_processor_listen_accept) { Socket_processor::Ptr sp = ugcs::vsm::Socket_processor::Get_instance(); Request_worker::Ptr worker = Request_worker::Create("UT socket processor worker"); CHECK(sp->Is_enabled()); CHECK(!worker->Is_enabled()); worker->Enable(); CHECK(worker->Is_enabled()); Socket_processor::Socket_listener::Ref listener; Socket_processor::Stream::Ref client_stream; Socket_processor::Stream::Ref server_stream; Io_result server_result; Io_result client_result; Io_buffer::Ptr client_buf; Io_buffer::Ptr server_buf; // forward declaration std::function<void(Io_buffer::Ptr, Io_result, Io_stream::Ref)> server_read_complete; std::function<void(Io_result, Io_stream::Ref)> server_write_complete = [&](Io_result result, Io_stream::Ref s) { if (result != Io_result::OK) { std::cout<<"WRITE NOK"<<std::endl; } else { // initiate next read after write; s->Read(1000, 1, Make_callback(server_read_complete, server_buf, server_result, s), worker); } }; // write back into socket everything received. server_read_complete = [&](Io_buffer::Ptr buffer, Io_result result, Io_stream::Ref stream) { if (result != Io_result::OK) { // std::cout<<"READ NOK"<<std::endl; stream->Close(); } else { // echo back // std::cout<<"Read "<<buffer->Get_length()<<" bytes"<<std::endl; stream->Write(buffer, Io_stream::OFFSET_NONE, Make_callback(server_write_complete, server_result, stream), worker); } }; // initiate std::function<void(Socket_processor::Stream::Ref, Io_result)> accept_complete = [&](Socket_processor::Stream::Ref s, Io_result) { if (s) { server_stream = s; auto addr = s->Get_peer_address(); if (addr) std::cout << "Connection from: " << addr->Get_as_string() << std::endl; s->Read(1000, 1, Make_callback(server_read_complete, server_buf, server_result, s), worker); } }; // Create listener socket. Do not provide compl context, the call will block until listener ready. sp->Listen("127.0.0.1", "12345", Make_socket_listen_callback([&](Socket_processor::Stream::Ref l, Io_result){ listener = l; })); // Set up acceptor. Provide worker as completion context. The call will return immediately. sp->Accept(listener, Make_socket_accept_callback(accept_complete), worker); // Connect to the socket. // Do not provide completion context, the call will block until conection ready. sp->Connect("127.0.0.1", "12345", Make_socket_connect_callback([&](Socket_processor::Stream::Ref s, Io_result){ client_stream = s; })); // std::this_thread::sleep_for(std::chrono::milliseconds(500)); // Send data to server client_stream->Write(Io_buffer::Create("0123456789"), Make_setter(client_result)); // Try sending zerolen buffer client_stream->Write(Io_buffer::Create(), Make_setter(client_result)); CHECK(Io_result::OK == client_result); // Send data to server client_stream->Write(Io_buffer::Create("abcdefghij"), Make_setter(client_result)); // Read back from server client_stream->Read(10, 10, Make_setter(client_buf, client_result)); // Check the data CHECK_EQUAL("0123456789", client_buf->Get_string().c_str()); // Read zero bytes from server client_stream->Read(0, 0, Make_setter(client_buf, client_result)); CHECK(Io_result::OK == client_result); client_stream->Read(0, 0, Make_setter(client_buf, client_result)); CHECK(Io_result::OK == client_result); // Read back from server client_stream->Read(100, 10, Make_setter(client_buf, client_result)).Timeout(std::chrono::milliseconds(1000)); CHECK(client_result == Io_result::OK); // Check the data CHECK_EQUAL("abcdefghij", client_buf->Get_string().c_str()); client_stream->Close(); listener->Close(); server_stream->Close(); worker->Disable(); }