Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,44 @@ jobs:
- name: Prepare for testing
run: |
brew services restart postgresql@14
for _ in {1..30}; do
if pg_isready -h 127.0.0.1 -p 5432 -U postgres; then
break
fi
sleep 1
done
pg_isready -h 127.0.0.1 -p 5432 -U postgres

brew services start mariadb
for _ in {1..30}; do
if mariadb-admin ping --silent; then
break
fi
sleep 1
done
mariadb-admin ping --silent

brew services start redis
sleep 4
for _ in {1..30}; do
if redis-cli ping | grep -q PONG; then
break
fi
sleep 1
done
redis-cli ping | grep -q PONG

mariadb -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('')"
mariadb -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'"
mariadb -e "FLUSH PRIVILEGES"
brew services restart mariadb
sleep 4
for _ in {1..30}; do
if mariadb-admin ping --silent; then
break
fi
sleep 1
done
mariadb-admin ping --silent

psql -c 'create user postgres superuser;' postgres

- name: Test
Expand Down Expand Up @@ -228,7 +258,13 @@ jobs:
- name: Prepare for testing
run: |
sudo systemctl start postgresql
sleep 1
for _ in {1..30}; do
if pg_isready -h 127.0.0.1 -p 5432 -U postgres; then
break
fi
sleep 1
done
pg_isready -h 127.0.0.1 -p 5432 -U postgres
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '12345'" postgres

- name: Test
Expand Down
52 changes: 38 additions & 14 deletions lib/tests/integration_test/client/RequestStreamTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>
#include <iostream>
#include <fstream>
#include <filesystem>

using namespace drogon;

Expand Down Expand Up @@ -100,23 +101,46 @@ DROGON_TEST(RequestStreamTest)

LOG_INFO << "Test request stream";

std::string filePath = "./中文.txt";
std::ifstream file(filePath);
std::stringstream content;
REQUIRE(file.is_open());
content << file.rdbuf();
const auto uniqueSuffix = std::to_string(
std::chrono::steady_clock::now().time_since_epoch().count());
auto tempDir = std::make_shared<std::filesystem::path>(
std::filesystem::temp_directory_path() /
("request_stream_upload_test_" + uniqueSuffix));
std::filesystem::create_directories(*tempDir);
auto tempPath = std::make_shared<std::filesystem::path>(
*tempDir / std::filesystem::path(u8"中文.txt"));
tempPath->make_preferred();
{
std::ofstream out(*tempPath, std::ios::binary | std::ios::trunc);
REQUIRE(out.is_open());
out << "request-stream-upload-content\nline2\n";
}

std::ifstream in(*tempPath, std::ios::binary);
REQUIRE(in.is_open());
std::stringstream ss;
ss << in.rdbuf();
const auto uploadContent = std::make_shared<std::string>(ss.str());

req = HttpRequest::newFileUploadRequest({UploadFile{filePath}});
const auto uploadPathUtf8 = std::make_shared<std::string>([&tempPath]() {
auto u8Path = tempPath->u8string();
return std::string(reinterpret_cast<const char *>(u8Path.data()),
u8Path.size());
}());
req = HttpRequest::newFileUploadRequest({UploadFile{*uploadPathUtf8}});
req->setPath("/stream_upload_echo");
req->setMethod(Post);
client->sendRequest(req,
[TEST_CTX,
content = content.str()](ReqResult r,
const HttpResponsePtr &resp) {
CHECK(r == ReqResult::Ok);
CHECK(resp->statusCode() == k200OK);
CHECK(resp->body() == content);
});
client->sendRequest(
req,
[TEST_CTX, tempPath, tempDir, uploadPathUtf8, content = uploadContent](
ReqResult r, const HttpResponsePtr &resp) {
CHECK(r == ReqResult::Ok);
CHECK(resp->statusCode() == k200OK);
CHECK(resp->body() == *content);
std::error_code ec;
std::filesystem::remove(*tempPath, ec);
std::filesystem::remove(*tempDir, ec);
});

checkStreamRequest(TEST_CTX,
client->getLoop(),
Expand Down
48 changes: 32 additions & 16 deletions orm_lib/src/TransactionImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ void TransactionImpl::execNewTask()
{
loop_->assertInLoopThread();
thisPtr_.reset();
if (!isWorking_)
return;
assert(isWorking_);
if (!isCommitedOrRolledback_)
{
Expand Down Expand Up @@ -246,23 +248,31 @@ void TransactionImpl::execNewTask()
else
{
isWorking_ = false;
if (!sqlCmdBuffer_.empty())
{
auto exceptPtr = std::make_exception_ptr(
TransactionRollback("The transaction has been rolled back"));
for (auto const &cmd : sqlCmdBuffer_)
{
if (cmd->exceptionCallback_)
{
cmd->exceptionCallback_(exceptPtr);
}
}
sqlCmdBuffer_.clear();
}
if (usedUpCallback_)
failBufferedCommands(std::make_exception_ptr(
TransactionRollback("The transaction has been rolled back")));
releaseConnection();
}
}

void TransactionImpl::releaseConnection()
{
if (usedUpCallback_)
{
usedUpCallback_();
usedUpCallback_ = std::function<void()>();
}
}

void TransactionImpl::failBufferedCommands(const std::exception_ptr &ePtr)
{
std::list<SqlCmdPtr> pendingCmds;
pendingCmds.swap(sqlCmdBuffer_);
for (auto &cmd : pendingCmds)
{
cmd->thisPtr_.reset();
if (cmd->exceptionCallback_)
{
usedUpCallback_();
usedUpCallback_ = std::function<void()>();
cmd->exceptionCallback_(ePtr);
}
}
}
Expand Down Expand Up @@ -307,6 +317,12 @@ void TransactionImpl::doBegin()
[thisPtr](const std::exception_ptr &) {
LOG_ERROR << "Error occurred in transaction begin";
thisPtr->isCommitedOrRolledback_ = true;
thisPtr->isWorking_ = false;
thisPtr->thisPtr_.reset();
thisPtr->failBufferedCommands(std::make_exception_ptr(
TransactionRollback("Transaction begin failed, cannot "
"execute queued SQL")));
thisPtr->releaseConnection();
});
});
}
Expand Down
2 changes: 2 additions & 0 deletions orm_lib/src/TransactionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class TransactionImpl : public Transaction,
bool isCommitedOrRolledback_{false};
bool isWorking_{false};
void execNewTask();
void releaseConnection();
void failBufferedCommands(const std::exception_ptr &ePtr);

struct SqlCmd
{
Expand Down
Loading
Loading