Skip to content

Commit

Permalink
Fixed a bug in TaskAsyncHelper::Delay
Browse files Browse the repository at this point in the history
- known issue: event source stream reader tests fails if a delay is removed
  • Loading branch information
JunTaoLuo committed Jul 20, 2013
1 parent 2ed61b9 commit 7927402
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Microsoft.AspNet.SignalR.Client.Cpp/TaskAsyncHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace MicrosoftAspNetSignalRClientCpp
TaskCompleted
};

// TaskAsyncHelper::Delay is experimental
template <typename T>
class DelayedTaskHelper
{
Expand All @@ -39,11 +40,16 @@ namespace MicrosoftAspNetSignalRClientCpp

if (ct != pplx::cancellation_token::none())
{
ct.register_callback([helper]()
pplx::cancellation_token_registration registration = ct.register_callback([helper]()
{
helper->mTimer.stop(false);
delete helper;
});

helper->DeregisterCallback = [ct, registration]()
{
ct.deregister_callback(registration);
};
}

auto task = pplx::create_task(helper->mTce);
Expand All @@ -58,11 +64,16 @@ namespace MicrosoftAspNetSignalRClientCpp

if (ct != pplx::cancellation_token::none())
{
ct.register_callback([helper]()
pplx::cancellation_token_registration registration = ct.register_callback([helper]()
{
helper->mTimer.stop(false);
delete helper;
});

helper->DeregisterCallback = [ct, registration]()
{
ct.deregister_callback(registration);
};
}

auto task = pplx::create_task(helper->mTce);
Expand All @@ -73,24 +84,28 @@ namespace MicrosoftAspNetSignalRClientCpp
private:
DelayedTaskHelper()
{
DeregisterCallback = [](){};
}

static void TimerCallback(void* context)
{
auto helper = static_cast<DelayedTaskHelper<T>*>(context);
helper->DeregisterCallback();
helper->mTce.set(T());
delete helper;
}

static void TimerCallbackVoid(void* context)
{
auto helper = static_cast<DelayedTaskHelper<void>*>(context);
helper->DeregisterCallback();
helper->mTce.set();
delete helper;
}

pplx::task_completion_event<T> mTce;
pplx::details::timer_t mTimer;
function<void()> DeregisterCallback;
};

class TaskAsyncHelper
Expand All @@ -99,7 +114,7 @@ namespace MicrosoftAspNetSignalRClientCpp
TaskAsyncHelper();
~TaskAsyncHelper();


// TaskAsyncHelper::Delay is experimental
static pplx::task<void> Delay(seconds seconds, pplx::cancellation_token ct = pplx::cancellation_token::none());

template <typename T1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ SUITE(EventSourceStreamReaderTests)
{
tce.set(sseEvent->GetData());
});

pEventSource->Start();

TaskAsyncHelper::Delay(seconds(1)).wait(); // this test crashes in debug mode if this delay is removed

//Assert
CHECK(pEvent->wait(5000) == 0);

Expand All @@ -46,6 +49,8 @@ SUITE(EventSourceStreamReaderTests)
}

//Cleanup
pEventSource->Abort();

pEventSource->SetOpenedCallback([](){});
pEventSource->SetMessageCallback([](shared_ptr<SseEvent>){});
}
Expand Down

0 comments on commit 7927402

Please sign in to comment.