Files
@ f2a6ba12fc29
Branch filter:
Location: libtransport.git/3rdparty/cpprestsdk/tests/common/UnitTestpp/src/TestRunner.cpp
f2a6ba12fc29
6.3 KiB
text/x-c++hdr
Slack frontend stub
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /***
* This file is based on or incorporates material from the UnitTest++ r30 open source project.
* Microsoft is not the original author of this code but has modified it and is licensing the code under
* the MIT License. Microsoft reserves all other rights not expressly granted under the MIT License,
* whether by implication, estoppel or otherwise.
*
* UnitTest++ r30
*
* Copyright (c) 2006 Noel Llopis and Charles Nicholson
* Portions Copyright (c) Microsoft Corporation
*
* All Rights Reserved.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***/
#include "stdafx.h"
#include "TestMacros.h"
#include "TestRunner.h"
#if _MSC_VER == 1600
#include <agents.h>
#include <functional>
#else
#include <future>
#endif
#if (defined(ANDROID) || defined(__ANDROID__))
#include <jni.h>
#include <boost/scope_exit.hpp>
#endif
#include <cstdlib>
#if (defined(ANDROID) || defined(__ANDROID__))
namespace crossplat {
extern std::atomic<JavaVM*> JVM;
}
#endif
namespace UnitTest {
TestRunner::TestRunner(TestReporter& reporter, bool breakOnError)
: m_reporter(&reporter)
, m_result(new TestResults(&reporter, breakOnError))
, m_timer(new Timer)
{
m_timer->Start();
}
TestRunner::~TestRunner()
{
delete m_result;
delete m_timer;
}
TestResults* TestRunner::GetTestResults()
{
return m_result;
}
int TestRunner::Finish() const
{
float const secondsElapsed = static_cast<float>(m_timer->GetTimeInMs() / 1000.0);
m_reporter->ReportSummary(m_result->GetTotalTestCount(),
m_result->GetFailedTestCount(),
m_result->GetFailureCount(),
secondsElapsed);
return m_result->GetFailureCount();
}
bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const
{
using namespace std;
return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName);
}
#if _MSC_VER == 1600
// std::future and std::thread doesn't exist on Visual Studio 2010 so fall back
// to use agent.
class TestRunnerAgent : public Concurrency::agent
{
public:
TestRunnerAgent(std::tr1::function<void()> func) : m_func(func) {}
protected:
void run()
{
Concurrency::Context::Oversubscribe(true);
m_func();
Concurrency::Context::Oversubscribe(false);
done();
}
private:
std::tr1::function<void()> m_func;
};
#endif
// Logic to decide the timeout for individual test
// 1. If /testtimeout is specified with testrunner arguments, use that timeout.
// 2. Else, if the test has a Timeout property set, use that timeout.
// 3. If both the above properties are not specified, use the default timeout value.
int TestRunner::GetTestTimeout(Test* const curTest, int const defaultTestTimeInMs) const
{
std::stringstream timeoutstream;
int timeout = defaultTestTimeInMs;
if(UnitTest::GlobalSettings::Has("testtimeout"))
{
timeoutstream << UnitTest::GlobalSettings::Get("testtimeout");
timeoutstream >> timeout;
}
else if(curTest->m_properties.Has("Timeout"))
{
timeoutstream << curTest->m_properties.Get("Timeout");
timeoutstream >> timeout;
}
return timeout;
}
void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const defaultTestTimeInMs) const
{
if (curTest->m_isMockTest == false)
CurrentTest::Results() = result;
int maxTestTimeInMs = GetTestTimeout(curTest, defaultTestTimeInMs);
Timer testTimer;
testTimer.Start();
result->OnTestStart(curTest->m_details);
if(maxTestTimeInMs > 0)
{
bool timedOut = false;
#if _MSC_VER == 1600
TestRunnerAgent testRunnerAgent([&]()
{
curTest->Run();
});
testRunnerAgent.start();
try
{
Concurrency::agent::wait(&testRunnerAgent, maxTestTimeInMs);
}
catch(const Concurrency::operation_timed_out &)
{
timedOut = true;
}
#else
// Timed wait requires async execution.
auto testRunnerFuture = std::async(std::launch::async, [&]()
{
#if (defined(ANDROID) || defined(__ANDROID__))
JNIEnv* env = nullptr;
auto result = crossplat::JVM.load()->AttachCurrentThread(&env, nullptr);
if (result != JNI_OK)
{
throw std::runtime_error("Could not attach to JVM");
}
BOOST_SCOPE_EXIT(void)
{
crossplat::JVM.load()->DetachCurrentThread();
} BOOST_SCOPE_EXIT_END
#endif
curTest->Run();
});
std::chrono::system_clock::time_point totalTime = std::chrono::system_clock::now() + std::chrono::milliseconds(maxTestTimeInMs);
if(testRunnerFuture.wait_until(totalTime) == std::future_status::timeout)
{
timedOut = true;
}
#endif
if(timedOut)
{
MemoryOutStream stream;
stream << "Test case timed out and is hung. Aborting all remaining test cases. ";
stream << "Expected under " << maxTestTimeInMs << "ms.";
result->OnTestFailure(curTest->m_details, stream.GetText());
abort();
}
}
else
{
curTest->Run();
}
double const testTimeInMs = testTimer.GetTimeInMs();
result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0));
}
}
|