Skip to content

Commit 9d637cc

Browse files
authored
replace mocked awaitable with std future (#2258)
1 parent bb540e6 commit 9d637cc

File tree

6 files changed

+43
-37
lines changed

6 files changed

+43
-37
lines changed

src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,25 @@ namespace
127127

128128
namespace CalculatorApp::ViewModel::DataLoaders
129129
{
130-
bool CurrencyHttpClient::ForceWebFailure = false;
131130
void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage)
132131
{
133132
m_sourceCurrencyCode = sourceCurrencyCode;
134133
m_responseLanguage = responseLanguage;
135134
}
136135

137-
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadataAsync() const
136+
std::future<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadataAsync() const
138137
{
139138
(void)m_responseLanguage; // to be used in production.
140-
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
139+
std::promise<Platform::String ^> mockedTask;
140+
mockedTask.set_value(ref new Platform::String(MockCurrencyStaticData));
141+
return mockedTask.get_future();
141142
}
142143

143-
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatiosAsync() const
144+
std::future<Platform::String ^> CurrencyHttpClient::GetCurrencyRatiosAsync() const
144145
{
145146
(void)m_sourceCurrencyCode; // to be used in production.
146-
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
147+
std::promise<Platform::String ^> mockedTask;
148+
mockedTask.set_value(ref new Platform::String(MockCurrencyConverterData));
149+
return mockedTask.get_future();
147150
}
148151
} // namespace CalculatorApp::ViewModel::DataLoaders

src/CalcViewModel/DataLoaders/CurrencyHttpClient.h

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,20 @@
33

44
#pragma once
55
#include <cassert>
6+
#include <future>
67

78
namespace CalculatorApp::ViewModel::DataLoaders
89
{
9-
template <class T>
10-
struct MockAwaitable
11-
{
12-
T Value;
13-
14-
bool await_ready() const noexcept
15-
{
16-
return true;
17-
}
18-
19-
void await_suspend(std::experimental::coroutine_handle<>) const noexcept
20-
{
21-
assert(false && "not implemented.");
22-
}
23-
24-
T await_resume() noexcept
25-
{
26-
return std::move(Value);
27-
}
28-
};
29-
3010
class CurrencyHttpClient
3111
{
3212
public:
13+
#ifdef VIEWMODEL_FOR_UT
3314
static bool ForceWebFailure;
15+
#endif
3416
void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage);
3517

36-
MockAwaitable<Platform::String ^> GetCurrencyMetadataAsync() const;
37-
MockAwaitable<Platform::String ^> GetCurrencyRatiosAsync() const;
18+
std::future<Platform::String ^> GetCurrencyMetadataAsync() const;
19+
std::future<Platform::String ^> GetCurrencyRatiosAsync() const;
3820

3921
private:
4022
Platform::String ^ m_sourceCurrencyCode;

src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
137137
<WarningLevel>Level4</WarningLevel>
138138
<TreatWarningAsError>true</TreatWarningAsError>
139+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
139140
</ClCompile>
140141
<Link>
141142
<SubSystem>Console</SubSystem>
@@ -156,6 +157,7 @@
156157
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
157158
<WarningLevel>Level4</WarningLevel>
158159
<TreatWarningAsError>true</TreatWarningAsError>
160+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
159161
</ClCompile>
160162
<Link>
161163
<SubSystem>Console</SubSystem>
@@ -176,6 +178,7 @@
176178
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
177179
<WarningLevel>Level4</WarningLevel>
178180
<TreatWarningAsError>true</TreatWarningAsError>
181+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
179182
</ClCompile>
180183
<Link>
181184
<SubSystem>Console</SubSystem>
@@ -196,6 +199,7 @@
196199
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
197200
<WarningLevel>Level4</WarningLevel>
198201
<TreatWarningAsError>true</TreatWarningAsError>
202+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
199203
</ClCompile>
200204
<Link>
201205
<SubSystem>Console</SubSystem>
@@ -216,6 +220,7 @@
216220
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
217221
<WarningLevel>Level4</WarningLevel>
218222
<TreatWarningAsError>true</TreatWarningAsError>
223+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
219224
</ClCompile>
220225
<Link>
221226
<SubSystem>Console</SubSystem>
@@ -236,6 +241,7 @@
236241
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
237242
<WarningLevel>Level4</WarningLevel>
238243
<TreatWarningAsError>true</TreatWarningAsError>
244+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
239245
</ClCompile>
240246
<Link>
241247
<SubSystem>Console</SubSystem>
@@ -256,6 +262,7 @@
256262
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
257263
<WarningLevel>Level4</WarningLevel>
258264
<TreatWarningAsError>true</TreatWarningAsError>
265+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
259266
</ClCompile>
260267
<Link>
261268
<SubSystem>Console</SubSystem>
@@ -276,6 +283,7 @@
276283
<AdditionalOptions>/bigobj /await /std:c++17 /utf-8 %(AdditionalOptions)</AdditionalOptions>
277284
<WarningLevel>Level4</WarningLevel>
278285
<TreatWarningAsError>true</TreatWarningAsError>
286+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
279287
</ClCompile>
280288
<Link>
281289
<SubSystem>Console</SubSystem>

src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,28 @@ namespace CalculatorApp::ViewModel::DataLoaders
2121
m_responseLanguage = responseLanguage;
2222
}
2323

24-
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadataAsync() const
24+
std::future<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadataAsync() const
2525
{
2626
if (ForceWebFailure)
2727
{
2828
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
2929
}
3030
(void)m_responseLanguage; // to be used in production.
31-
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
31+
std::promise<Platform::String ^> mockedTask;
32+
mockedTask.set_value(ref new Platform::String(MockCurrencyStaticData));
33+
return mockedTask.get_future();
3234
}
3335

34-
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatiosAsync() const
36+
std::future<Platform::String ^> CurrencyHttpClient::GetCurrencyRatiosAsync() const
3537
{
3638
if (ForceWebFailure)
3739
{
3840
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
3941
}
4042
(void)m_sourceCurrencyCode; // to be used in production.
41-
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
43+
44+
std::promise<Platform::String ^> mockedTask;
45+
mockedTask.set_value(ref new Platform::String(MockCurrencyConverterData));
46+
return mockedTask.get_future();
4247
}
4348
} // namespace CalculatorApp::ViewModel::DataLoaders

src/CalculatorUnitTests/CalculatorUnitTests.vcxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
136136
<WarningLevel>Level4</WarningLevel>
137137
<TreatWarningAsError>true</TreatWarningAsError>
138+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
138139
</ClCompile>
139140
</ItemDefinitionGroup>
140141
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
@@ -145,6 +146,7 @@
145146
<WarningLevel>Level4</WarningLevel>
146147
<TreatWarningAsError>true</TreatWarningAsError>
147148
<ControlFlowGuard>Guard</ControlFlowGuard>
149+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
148150
</ClCompile>
149151
</ItemDefinitionGroup>
150152
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
@@ -154,6 +156,7 @@
154156
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
155157
<WarningLevel>Level4</WarningLevel>
156158
<TreatWarningAsError>true</TreatWarningAsError>
159+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
157160
</ClCompile>
158161
</ItemDefinitionGroup>
159162
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
@@ -164,6 +167,7 @@
164167
<WarningLevel>Level4</WarningLevel>
165168
<TreatWarningAsError>true</TreatWarningAsError>
166169
<ControlFlowGuard>Guard</ControlFlowGuard>
170+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
167171
</ClCompile>
168172
</ItemDefinitionGroup>
169173
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -173,6 +177,7 @@
173177
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
174178
<WarningLevel>Level4</WarningLevel>
175179
<TreatWarningAsError>true</TreatWarningAsError>
180+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
176181
</ClCompile>
177182
</ItemDefinitionGroup>
178183
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -183,6 +188,7 @@
183188
<WarningLevel>Level4</WarningLevel>
184189
<TreatWarningAsError>true</TreatWarningAsError>
185190
<ControlFlowGuard>Guard</ControlFlowGuard>
191+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
186192
</ClCompile>
187193
</ItemDefinitionGroup>
188194
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -192,6 +198,7 @@
192198
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
193199
<WarningLevel>Level4</WarningLevel>
194200
<TreatWarningAsError>true</TreatWarningAsError>
201+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
195202
</ClCompile>
196203
</ItemDefinitionGroup>
197204
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -202,6 +209,7 @@
202209
<WarningLevel>Level4</WarningLevel>
203210
<TreatWarningAsError>true</TreatWarningAsError>
204211
<ControlFlowGuard>Guard</ControlFlowGuard>
212+
<PreprocessorDefinitions>VIEWMODEL_FOR_UT;%(PreprocessorDefinitions)</PreprocessorDefinitions>
205213
</ClCompile>
206214
</ItemDefinitionGroup>
207215
<ItemGroup>

src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ namespace CalculatorUnitTests
160160

161161
VERIFY_IS_TRUE(DeleteCurrencyCacheFiles());
162162

163-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value));
164-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().Value));
163+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get()));
164+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().get()));
165165
}
166166

167167
TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles();
@@ -207,7 +207,7 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist)
207207
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
208208

209209
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename));
210-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().Value));
210+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().get()));
211211

212212
CurrencyDataLoader loader{ L"en-US" };
213213

@@ -225,7 +225,7 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist)
225225
DateTime now = Utils::GetUniversalSystemTime();
226226
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
227227

228-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value));
228+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get()));
229229
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
230230

231231
CurrencyDataLoader loader{ L"en-US" };
@@ -245,7 +245,7 @@ TEST_METHOD(LoadFromCache_Fail_ResponseLanguageChanged)
245245
// Tests always use en-US as response language. Insert a different lang-code to fail the test.
246246
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheLangcodeKey, L"ar-SA");
247247

248-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value));
248+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get()));
249249
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
250250

251251
CurrencyDataLoader loader{ L"en-US" };

0 commit comments

Comments
 (0)