2018-08-20 03:34:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Common/Exception.h>
|
2018-12-27 13:09:11 +00:00
|
|
|
#include <Parsers/Lexer.h>
|
2018-08-20 03:34:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-01-12 13:47:56 +00:00
|
|
|
extern const int UNEXPECTED_ERROR_CODE;
|
2018-08-20 03:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks expected server and client error codes in testmode.
|
|
|
|
/// To enable it add special comment after the query: "-- { serverError 60 }" or "-- { clientError 20 }".
|
|
|
|
class TestHint
|
|
|
|
{
|
|
|
|
public:
|
2020-07-21 14:05:30 +00:00
|
|
|
TestHint(bool enabled_, const String & query_)
|
|
|
|
: enabled(enabled_)
|
|
|
|
, query(query_)
|
2018-08-20 03:34:10 +00:00
|
|
|
{
|
|
|
|
if (!enabled_)
|
|
|
|
return;
|
|
|
|
|
2018-12-27 13:09:11 +00:00
|
|
|
Lexer lexer(query.data(), query.data() + query.size());
|
2018-08-20 03:34:10 +00:00
|
|
|
|
2018-12-27 13:09:11 +00:00
|
|
|
for (Token token = lexer.nextToken(); !token.isEnd(); token = lexer.nextToken())
|
2018-08-20 03:34:10 +00:00
|
|
|
{
|
2018-12-27 13:09:11 +00:00
|
|
|
if (token.type == TokenType::Comment)
|
2018-08-20 03:34:10 +00:00
|
|
|
{
|
2018-12-28 16:45:45 +00:00
|
|
|
String comment(token.begin, token.begin + token.size());
|
|
|
|
|
|
|
|
if (!comment.empty())
|
2018-12-27 13:09:11 +00:00
|
|
|
{
|
2018-12-28 16:45:45 +00:00
|
|
|
size_t pos_start = comment.find('{', 0);
|
|
|
|
if (pos_start != String::npos)
|
|
|
|
{
|
|
|
|
size_t pos_end = comment.find('}', pos_start);
|
|
|
|
if (pos_end != String::npos)
|
|
|
|
{
|
|
|
|
String hint(comment.begin() + pos_start + 1, comment.begin() + pos_end);
|
|
|
|
parse(hint);
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 13:09:11 +00:00
|
|
|
}
|
2018-08-20 03:34:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @returns true if it's possible to continue without reconnect
|
|
|
|
bool checkActual(int & actual_server_error, int & actual_client_error,
|
|
|
|
bool & got_exception, std::unique_ptr<Exception> & last_exception) const
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (allErrorsExpected(actual_server_error, actual_client_error))
|
|
|
|
{
|
|
|
|
got_exception = false;
|
|
|
|
last_exception.reset();
|
|
|
|
actual_server_error = 0;
|
|
|
|
actual_client_error = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lostExpectedError(actual_server_error, actual_client_error))
|
|
|
|
{
|
2020-07-21 14:05:30 +00:00
|
|
|
std::cerr << "Success when error expected in query: " << query << "It expects server error "
|
2018-08-20 03:34:10 +00:00
|
|
|
<< server_error << ", client error " << client_error << "." << std::endl;
|
|
|
|
got_exception = true;
|
2020-01-12 13:47:56 +00:00
|
|
|
last_exception = std::make_unique<Exception>("Success when error expected", ErrorCodes::UNEXPECTED_ERROR_CODE); /// return error to OS
|
2018-08-20 03:34:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int serverError() const { return server_error; }
|
|
|
|
int clientError() const { return client_error; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool enabled = false;
|
2020-07-21 14:05:30 +00:00
|
|
|
const String & query;
|
2018-08-20 03:34:10 +00:00
|
|
|
int server_error = 0;
|
|
|
|
int client_error = 0;
|
|
|
|
|
|
|
|
void parse(const String & hint)
|
|
|
|
{
|
2020-11-10 18:22:26 +00:00
|
|
|
std::stringstream ss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
|
2019-01-21 16:14:26 +00:00
|
|
|
ss << hint;
|
|
|
|
String item;
|
|
|
|
|
2018-08-20 03:34:10 +00:00
|
|
|
while (!ss.eof())
|
|
|
|
{
|
|
|
|
ss >> item;
|
2019-01-21 16:14:26 +00:00
|
|
|
if (ss.eof())
|
|
|
|
break;
|
2018-08-20 03:34:10 +00:00
|
|
|
|
|
|
|
if (item == "serverError")
|
|
|
|
ss >> server_error;
|
|
|
|
else if (item == "clientError")
|
|
|
|
ss >> client_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allErrorsExpected(int actual_server_error, int actual_client_error) const
|
|
|
|
{
|
|
|
|
return (server_error || client_error) && (server_error == actual_server_error) && (client_error == actual_client_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lostExpectedError(int actual_server_error, int actual_client_error) const
|
|
|
|
{
|
|
|
|
return (server_error && !actual_server_error) || (client_error && !actual_client_error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|