Skip to content

Commit

Permalink
Export methods to set/get the ambient scheduler in cpprest dll (#670)
Browse files Browse the repository at this point in the history
* Export methods to set/get the ambient scheduler in cpprest dll

* move the definiation of get/set_cpprestsdk_ambient_scheduler to pplxwin.cpp

* Add test for http request with ambient scheduler
  • Loading branch information
chakrab-msft authored and BillyONeal committed Oct 27, 2018
1 parent b3a7141 commit e5c6d84
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Release/include/pplx/pplxtasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@
#ifndef _PPLXTASKS_H
#define _PPLXTASKS_H

#include "cpprest/details/cpprest_compat.h"

#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
#include <ppltasks.h>
namespace pplx = Concurrency;

namespace Concurrency
{

/// <summary>
/// Sets the ambient scheduler to be used by the PPL constructs.
/// </summary>
_ASYNCRTIMP void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler);

/// <summary>
/// Gets the ambient scheduler to be used by the PPL constructs
/// </summary>
_ASYNCRTIMP const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler();

} // namespace Concurrency

#if (_MSC_VER >= 1900)
#include <concrt.h>
namespace Concurrency {
Expand Down
1 change: 1 addition & 0 deletions Release/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ elseif(CPPREST_PPLX_IMPL STREQUAL "linux")
install(FILES ../include/pplx/threadpool.h DESTINATION include/pplx)
endif()
elseif(CPPREST_PPLX_IMPL STREQUAL "win")
target_sources(cpprest PRIVATE pplx/pplxwin.cpp)
if(CPPREST_WEBSOCKETS_IMPL STREQUAL "wspp")
target_sources(cpprest PRIVATE pplx/threadpool.cpp ../include/pplx/threadpool.h)
if(CPPREST_INSTALL_HEADERS)
Expand Down
15 changes: 15 additions & 0 deletions Release/src/pplx/pplxwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,19 @@ namespace details

} // namespace pplx

#else
namespace Concurrency
{

void __cdecl set_cpprestsdk_ambient_scheduler(const std::shared_ptr<scheduler_interface>& _Scheduler)
{
pplx::set_ambient_scheduler(_Scheduler);
}

const std::shared_ptr<scheduler_interface>& __cdecl get_cpprestsdk_ambient_scheduler()
{
return pplx::get_ambient_scheduler();
}

} // namespace pplx
#endif
37 changes: 37 additions & 0 deletions Release/tests/functional/http/client/outside_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ TEST_FIXTURE(uri_address, multiple_https_requests)
});
}

#if (defined(_MSC_VER) && (_MSC_VER >= 1800)) && !CPPREST_FORCE_PPLX
TEST_FIXTURE(uri_address, multiple_https_requests_sync_scheduler)
{
struct sync_scheduler : public scheduler_interface
{
public:
virtual void schedule(TaskProc_t function, PVOID context) override
{
function(context);
}
};

// Save the current ambient scheduler
const auto scheduler = get_cpprestsdk_ambient_scheduler();

// Change the ambient scheduler to one that schedules synchronously
static std::shared_ptr<scheduler_interface> syncScheduler = std::make_shared<sync_scheduler>();
set_cpprestsdk_ambient_scheduler(syncScheduler);

handle_timeout([&] {
// Use code.google.com instead of www.google.com, which redirects
http_client client(U("https://code.google.com"));

http_response response;
for (int i = 0; i < 5; ++i)
{
response = client.request(methods::GET).get();
VERIFY_ARE_EQUAL(status_codes::OK, response.status_code());
response.content_ready().wait();
}
});

// Revert to the original scheduler
set_cpprestsdk_ambient_scheduler(scheduler);
}
#endif

TEST_FIXTURE(uri_address, reading_google_stream)
{
handle_timeout([&]
Expand Down

0 comments on commit e5c6d84

Please sign in to comment.