~gary-wzl77/+junk/net-cpp_test_pause_and_resume

« back to all changes in this revision

Viewing changes to tests/http_streaming_client_test.cpp

  • Committer: Gary.Wang
  • Date: 2016-05-23 13:35:17 UTC
  • Revision ID: gary.wang@canonical.com-20160523133517-cwmtfd7avy4umq9o
add pause_and_resume test

Show diffs side-by-side

added added

removed removed

Lines of Context:
594
594
    EXPECT_EQ(url, root["url"].asString());
595
595
}
596
596
 
 
597
TEST(StreamingHttpClient, pause_and_resume)
 
598
{
 
599
    using namespace ::testing;
 
600
    // We obtain a default client instance, dispatching to the default implementation.
 
601
    auto client = http::make_streaming_client();
 
602
 
 
603
    // Execute the client
 
604
    std::thread worker{[client]() { client->run(); }};
 
605
 
 
606
    auto url = "https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tar.xz";
 
607
 
 
608
    // The client mostly acts as a factory for http requests.
 
609
    auto request = client->streaming_get(http::Request::Configuration::from_uri_as_string(url));
 
610
 
 
611
    // Our mocked data handler.
 
612
    auto dh = MockDataHandler::create(); EXPECT_CALL(*dh, on_new_data(_)).Times(AtLeast(1));
 
613
 
 
614
    std::promise<core::net::http::Response> promise;
 
615
    auto future = promise.get_future();
 
616
 
 
617
    // We finally execute the query asynchronously.
 
618
    request->async_execute(
 
619
                http::Request::Handler()
 
620
                    .on_progress(default_progress_reporter)
 
621
                    .on_response([&](const core::net::http::Response& response)
 
622
                    {
 
623
                        promise.set_value(response);
 
624
                    })
 
625
                    .on_error([&](const core::net::Error& e)
 
626
                    {
 
627
                        promise.set_exception(std::make_exception_ptr(e));
 
628
                    }),
 
629
                dh->to_data_handler());
 
630
 
 
631
    std::this_thread::sleep_for(std::chrono::milliseconds(5000));
 
632
    std::cout << "we pause" << std::endl;
 
633
    request->pause();
 
634
    
 
635
    //Please check if we can resume if we didn't set speed_limit/time option for this request
 
636
    std::this_thread::sleep_for(std::chrono::microseconds(5000));
 
637
 
 
638
    std::cout << "we resume." << std::endl;
 
639
    request->resume();
 
640
 
 
641
    auto response = future.get();
 
642
    
 
643
    // We expect the query to complete successfully
 
644
    EXPECT_EQ(core::net::http::Status::ok, response.status);
 
645
 
 
646
    client->stop();
 
647
 
 
648
    // We shut down our worker thread
 
649
    if (worker.joinable())
 
650
        worker.join();    
 
651
}
 
652