{"id":56531,"date":"2012-12-10T07:55:24","date_gmt":"2012-12-10T15:55:24","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/devops\/?p=56531"},"modified":"2019-05-06T07:56:15","modified_gmt":"2019-05-06T15:56:15","slug":"asynchronous-calls-in-c-windows-store-unit-tests","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/asynchronous-calls-in-c-windows-store-unit-tests\/","title":{"rendered":"Asynchronous calls in C++ Windows Store Unit Tests"},"content":{"rendered":"<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">In Visual Studio 2012, we added support for the Async Test Methods in C#. You can call <span style=\"color:\"><font color=\"#0070c0\">await <\/font><\/span>inside the test method body and the unit test framework will appropriately wait for its completion.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">Here is a sample test method.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">[<\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#2b91af\">TestMethod<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">]<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">public<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">async<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#2b91af\">Task<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\"> TestMethod1()<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">{<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Arrange<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#2b91af\">AtomPubClient<\/font><\/span><span style=\"font-family:;color:\"> client = <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">new<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#2b91af\">AtomPubClient<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">();<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#2b91af\">Uri<\/font><\/span><span style=\"font-family:;color:\"> uri = <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">new<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#2b91af\">Uri<\/font><\/span><span style=\"font-family:;color:\">(<\/span><span style=\"font-family:;color:\"><font color=\"#a31515\">&quot;http:\/\/blogs.msdn.com\/b\/mathew_aniyan\/atom.aspx&quot;<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Act<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">var<\/font><\/span><span style=\"font-family:;color:\"> result = <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">await<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\"> client.RetrieveFeedAsync(uri);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Assert<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#2b91af\">Assert<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">.IsTrue(result.Items.Count &gt; 0);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">}<\/font><\/font><\/span><span style=\"font-family:;color:;line-height: 10pt\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">Notice how the method signature includes <font face=\"Consolas\"><span style=\"font-family:;color:;line-height: 10pt\"><font color=\"#0000ff\"><font style=\"font-size: 9.5pt\">async<\/font><\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:;line-height: 10pt\"> <\/span><span style=\"font-family:;color:;line-height: 10pt\"><font color=\"#2b91af\">Task<\/font><\/span><\/font><span style=\"font-family:;color:;line-height: 10pt\"><font style=\"font-size: 9.5pt\" color=\"#2b91af\"> <\/font><\/span><\/font>and the method body contains a call to<font face=\"Consolas\"><span style=\"font-family:;color:;line-height: 10pt\"><font color=\"#2b91af\"><font style=\"font-size: 9.5pt\"> <\/font><\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font color=\"#0000ff\">await<\/font><\/span><span style=\"font-family:;color:;line-height: 10pt\"> client.RetrieveFeedAsync<\/span><\/font><span style=\"font-family:;color:;line-height: 10pt\"><font style=\"font-size: 9.5pt\">.<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">If you are using C++ Windows Store Unit Tests, you cannot leverage this model since the language does not have equivalent constructs as the async-await pattern in C#. Asynchronous methods ,though, are ubiquitous in Windows Store applications. This blog will describe a model by which you can test asynchronous functions in a C++ Windows Store Unit Test.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">&#160;<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">To get started, open \u201cTools -&gt; Extensions and Updates\u201d menu.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/4670.image_5469D567.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/2570.image_thumb_65215A48.png\" width=\"528\" height=\"297\" \/><\/a><\/p>\n<p>Search for and install C++ WinRT Async UnitTestLibrary.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/6470.image_6D557FDF.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/2654.image_thumb_4FB37F13.png\" width=\"545\" height=\"377\" \/><\/a><\/p>\n<p>This will install a new Project Template <b>Unit Test Library with Async Helper (Windows Store apps)<\/b> under the Visual C++ -&gt; Windows Store node in New Project dialog.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/5873.image_4AA11B97.png\"><img decoding=\"async\" title=\"image\" style=\"border-top: 0px;border-right: 0px;border-bottom: 0px;padding-top: 0px;padding-left: 0px;border-left: 0px;padding-right: 0px\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/45\/92\/metablogapi\/8176.image_thumb_00C293E7.png\" width=\"537\" height=\"368\" \/><\/a><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">Go ahead and create a new Project with this template and then add the following code to the Test Method.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">TEST_METHOD(TestMethod1)<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">{<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Arrange<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">Uri^ uri = <\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">ref<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">new<\/font><\/span><span style=\"font-family:;color:\"> Uri(<\/span><span style=\"font-family:;color:\"><font color=\"#a31515\">&quot;http:\/\/blogs.msdn.com\/b\/mathew_aniyan\/atom.aspx&quot;<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">AtomPubClient^ client = <\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">ref<\/font><\/span><span style=\"font-family:;color:\"> <\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">new<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\"> AtomPubClient();<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">auto<\/font><\/span><span style=\"font-family:;color:\"> count = std::make_shared&lt;<\/span><span style=\"font-family:;color:\"><font color=\"#0000ff\">int<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">&gt;(0);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Act<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">AsyncHelper::RunSynced(task&lt;SyndicationFeed^&gt;(client-&gt;RetrieveFeedAsync(uri)).then([count] (SyndicationFeed^ feed)<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">{<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">*count = feed-&gt;Items-&gt;Size;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">}));<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/Assert<\/font><\/span><\/font><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">Assert::IsTrue(*count &gt; 0);<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">}<\/font><\/font><\/span><span style=\"font-family:;color:;line-height: 10pt\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">Notice how<font color=\"#0070c0\"><span style=\"color:\"> AsyncHelper::RunSynced<\/span><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\"> <\/font><\/font><\/span><\/font>method is used. This will ensure that the task argument is completed before proceeding with the test method.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">If you open asynchelper.h file in the solution, you can see how RunSynced is implemented. The key code snippet is<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\" color=\"#008000\">\/\/ Spin wait and exercise message pump<\/font><\/font><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">DWORD waitResult = STATUS_PENDING;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">while<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">(waitResult != WAIT_OBJECT_0)<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">{<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><span style=\"font-family:;color:\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">dispatcher-&gt;ProcessEvents(Windows::UI::Core::CoreProcessEventsOption::ProcessAllIfPresent);<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 0pt;line-height: normal\"><font face=\"Consolas\"><span style=\"font-family:;color:\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">waitResult = WaitForSingleObjectEx(hEvent, 0, <\/font><\/span><font style=\"font-size: 9.5pt\"><span style=\"font-family:;color:\"><font color=\"#0000ff\">true<\/font><\/span><\/font><span style=\"font-family:;color:\"><font style=\"font-size: 9.5pt\">);<\/font><\/span><\/font><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><span><font style=\"font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><\/span><font style=\"font-size: 9.5pt\">}<\/font><\/font><\/span><span style=\"font-family:;color:;line-height: 10pt\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">You can now go ahead and run this test. You will see that the test passes.<\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\"><span style=\"font-family:;color:;line-height: 10pt\"><font face=\"Consolas\"><font style=\"font-size: 9.5pt\">&#160;<\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">This solution approach was identified by the Patterns<span>&#160; <\/span>&amp; Practices team and used in their <a href=\"http:\/\/hilo.codeplex.com\"><font color=\"#0563c1\">Hilo project.<\/font><\/a><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">Direct link to the project template in Visual Studio Gallery &#8211; <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/744fb771-756e-4801-811d-8994d82466dd\"><font color=\"#0563c1\">http:\/\/visualstudiogallery.msdn.microsoft.com\/744fb771-756e-4801-811d-8994d82466dd<\/font><\/a><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 8pt;line-height: 12pt\">The project template is published to codeplex at <a href=\"http:\/\/asynccppunittestlib.codeplex.com\/\"><font color=\"#0563c1\">http:\/\/asynccppunittestlib.codeplex.com\/<\/font><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Visual Studio 2012, we added support for the Async Test Methods in C#. You can call await inside the test method body and the unit test framework will appropriately wait for its completion. Here is a sample test method. &#160;&#160;&#160;&#160;&#160;&#160;&#160; [TestMethod] &#160;&#160;&#160;&#160;&#160;&#160;&#160; public async Task TestMethod1() &#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Arrange &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AtomPubClient client = [&hellip;]<\/p>\n","protected":false},"author":241,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-56531","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"acf":[],"blog_post_summary":"<p>In Visual Studio 2012, we added support for the Async Test Methods in C#. You can call await inside the test method body and the unit test framework will appropriately wait for its completion. Here is a sample test method. &#160;&#160;&#160;&#160;&#160;&#160;&#160; [TestMethod] &#160;&#160;&#160;&#160;&#160;&#160;&#160; public async Task TestMethod1() &#160;&#160;&#160;&#160;&#160;&#160;&#160; { &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/Arrange &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AtomPubClient client = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/56531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/241"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=56531"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/56531\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=56531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=56531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=56531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}