Skip to content

Commit

Permalink
test(cmake): add gtest configuration with tests and minor code adjust…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
scyth3-c committed Sep 1, 2024
1 parent 73ee053 commit 561e49d
Show file tree
Hide file tree
Showing 15 changed files with 1,079 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .deepsource.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
version = 1

exclude_patterns = [
"**/examples/**"
"**/examples/**",
"**/tests/**"
]

[[analyzers]]
Expand Down
58 changes: 58 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,64 @@ file(GLOB_RECURSE HEADERS
)
add_library(vibe STATIC ${SOURCES} ${HEADERS})

option(TESTING "Enable testing" OFF)
if (TESTING)

enable_testing()
add_executable(main tests/main_test.cpp)
target_link_libraries(
main
vibe
)

include(FetchContent)


find_package(GTest QUIET)

if (NOT GTest_FOUND)

message(STATUS "extracting https://github.com/google/googletest.git")
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
message(STATUS "using -> build/ [gtest]")
else ()
message(STATUS "#### using local GTest: In case of error verify your installation")
endif ()

target_link_libraries(main GTest::gtest_main)
include(GoogleTest)
gtest_discover_tests(main)


find_package(CURL QUIET)
if ( NOT CURL_FOUND )

message( STATUS "extracting https://github.com/curl/curl.git")
FetchContent_Declare(
curl
GIT_REPOSITORY https://github.com/curl/curl.git
GIT_TAG 83bedbd
)
FetchContent_MakeAvailable(curl)
message(STATUS "using -> build/ [curl]")
else ()
message(STATUS "#### using local curl: In case of error with .so.4 verify your installation curl --version")
endif ()

target_link_libraries(main CURL::libcurl)

else ()

add_executable(vibex tests/debug.cpp)
target_link_libraries(vibex vibe)

endif ()

install(TARGETS vibe
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ In the `examples/` folder, you'll find examples of how to use Vibe for different

<img alt="Linux" src="https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black">

## Testing

cmake shell basic commands
```shell
cmake -DTESTING=ON -S. -B build
cmake --build build/
cd build/
ctest
```

with NPM

```shell
npm run build
npm run test
```

### Debug

for debug
```shell
npm run dev:run
```

and modify the file tests/debug.cpp


## Contribution

Contributions are welcome! If you want to contribute to Vibe, please follow these guidelines:
Expand Down
2 changes: 1 addition & 1 deletion examples/files/cpp.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
$
std::cout << "hello from c++" << std::endl;
std::cout << "[hello from c++]" << std::endl;
$
</body>
</html>
4 changes: 1 addition & 3 deletions examples/files/test.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"message": "hello"
}
{"message": "hello"}
75 changes: 51 additions & 24 deletions include/vibe/request/router_epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,60 @@
#include "../util/nterminal.h"
#include "io.h"

constexpr int BUFFER = enums::neo::eSize::BUFFER;
constexpr int SESSION = enums::neo::eSize::SESSION;
constexpr int INIT_MAX_EVENTS = 10;

namespace workers {
using RoutesMap = std::unordered_map<string, std::unique_ptr<listen_routes>>;
using enums::neo;

constexpr int BUFFER = neo::eSize::BUFFER;
constexpr int SESSION = neo::eSize::SESSION;
constexpr int INIT_MAX_EVENTS = 10;

template<class T>
class RouterEpoll {

int epoll_fd;
int epoll_fd, file_descriptor;
shared_ptr<std::vector<epoll_event>> events;
shared_ptr<T> connection;
shared_ptr<RequestIO> request_t;
neo::eStatus listen_status_;

public:

explicit RouterEpoll(const shared_ptr<T> &conn) :
epoll_fd(epoll_create1(0)),
epoll_fd(epoll_create1(0)), file_descriptor(-1),
events(make_shared<vector<epoll_event>>(INIT_MAX_EVENTS)),
connection(conn)
connection(conn),
listen_status_(enums::neo::eStatus::START)
{}

auto getMainProcess(const shared_ptr<RoutesMap> &_routes){

connection->on();
int file_descriptor = connection->getDescription();
auto InitListenProcess() {
connection->on();
file_descriptor = connection->getDescription();

if(Server::setNonblocking(file_descriptor) == MG_ERROR)
close(file_descriptor);
if(Server::setNonblocking(file_descriptor) == MG_ERROR)
close(file_descriptor);

if (epoll_fd == -1)
throw std::range_error(VB_EPOLL_RANGE);
if (epoll_fd == -1)
throw std::range_error(VB_EPOLL_RANGE);
}


auto ListenProcess(epoll_event &event) const {
try {
const int notice = epoll_wait(epoll_fd, events->data(), INIT_MAX_EVENTS, VB_NVALUE);
request_t->Dispatch(notice, event);
}
catch(const std::exception& e) {
terminal(e.what());
close(epoll_fd);
if(close(file_descriptor) < VB_OK)
throw std::range_error(VB_MAIN_THREAD);
}
}

auto getMainProcess(const shared_ptr<RoutesMap> &_routes, const neo::LISTEN_TYPE _listen_type = neo::WHILE) {
InitListenProcess();

epoll_event event{};
event.events = EPOLLIN;
Expand All @@ -61,22 +81,29 @@ using RoutesMap = std::unordered_map<string, std::unique_ptr<listen_routes>>;
close(epoll_fd);
throw std::range_error(VB_EPOLL_CTL);
}

request_t = make_shared<RequestIO>(events, _routes, file_descriptor, epoll_fd, connection);

while (static_cast<bool>(enums::neo::eStatus::START)){
try {
const int notice = epoll_wait(epoll_fd, events->data(), INIT_MAX_EVENTS, VB_NVALUE);
request_t->Dispatch(notice, event);
if (_listen_type == neo::WHILE) {
while (static_cast<bool>(listen_status_)) {
ListenProcess(event);
}
catch(const std::exception& e) {
terminal(e.what());
close(epoll_fd);
close(file_descriptor);
request_t.reset();
}
else {
// UNIQUE
ListenProcess(event);
ListenProcess(event);
close(epoll_fd);
if(close(file_descriptor) < VB_OK)
throw std::range_error(VB_MAIN_THREAD);
}
close(file_descriptor);
request_t.reset();
}
}

void setListenStatus(const neo::eStatus _status) {
this->listen_status_ = _status;
}
};
}

Expand Down
5 changes: 4 additions & 1 deletion include/vibe/util/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace enums {
OK = 0x0,
ERROR = -0x1,
NA = 0x2,
UKNOW = 0x3
};
enum eSize {
BUFFER = 0x800,
Expand All @@ -26,6 +25,10 @@ namespace enums {
START = 0x1,
STOP = 0x0
};
enum LISTEN_TYPE {
WHILE,
UNIQUE
};
};

} // enums
Expand Down
32 changes: 22 additions & 10 deletions include/vibe/vibe.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <memory>
#include <string>

using std::make_shared;
using std::make_unique;
using std::make_shared, std::make_unique;
using std::string;

using workers::RoutesMap;
using workers::RoutesMap, workers::BUFFER, workers::SESSION;
using enums::neo;

template <class T>
class Vibe {
Expand All @@ -21,7 +21,7 @@ class Vibe {
shared_ptr<RoutesMap> routes;
std::shared_ptr<T> tcpControl;

uint16_t PORT{enums::neo::eSize::DEF_PORT};
uint16_t PORT{neo::DEF_PORT};

void tcpInt();

Expand All @@ -47,12 +47,14 @@ class Vibe {
int setPort(uint16_t) noexcept;
[[nodiscard]] [[maybe_unused]] inline uint16_t getPort() const noexcept{return PORT;};
void listen();
void listenOne();
void setListenStatus(neo::eStatus);

};

template <class T>
[[maybe_unused]] Vibe<T>::Vibe(const uint16_t port) {
if (port >= enums::neo::eSize::MIN_PORT) { PORT = port; }
if (port >= neo::MIN_PORT) { PORT = port; }
tcpInt();
}

Expand All @@ -72,9 +74,9 @@ int Vibe<T>::http_response(const string &endpoint, MiddlewareList middlewareList
}
catch (const std::exception &e) {
std::cerr << e.what() << '\n';
return enums::neo::eReturn::ERROR;
return neo::ERROR;
}
return enums::neo::eReturn::OK;
return neo::OK;
}

template <class T>
Expand Down Expand Up @@ -128,20 +130,30 @@ template <class T>
void Vibe<T>::listen() {
router_epoll->getMainProcess(routes);
}
template <class T>
void Vibe<T>::listenOne() {
router_epoll->getMainProcess(routes, neo::UNIQUE);
}

template <class T>
void Vibe<T>::setListenStatus(neo::eStatus _status) {
router_epoll->setListenStatus(_status);
}


template <class T>
int Vibe<T>::setPort(const uint16_t _port) noexcept {
if (_port >= enums::neo::eSize::MIN_PORT) {
if (_port >= neo::MIN_PORT) {
PORT = _port;
if(tcpControl != nullptr) {
tcpControl->setPort(PORT);
return enums::neo::eReturn::OK;
return neo::OK;
}
}
return enums::neo::eReturn::ERROR;
return neo::ERROR;
}


template<class T>
void Vibe<T>::tcpInt() {

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "1.0.0",
"description": "User-friendly and compact C++ Web Framework",
"scripts": {
"build": "cmake . . && cmake --build .",
"install": "make install",
"test": "echo \"Error: no test specified\" && exit 1"
"build": "cmake -DTESTING=ON -S . -B build/ && cmake --build build",
"test": "ctest --test-dir build",
"dev:build": "cmake -S . -B build/ && cmake --build build",
"dev:run": "cmake -S . -B build/ && cmake --build build && ./build/vibex"
},
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion sources/request/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void RequestIO::Dispatch(const int id, epoll_event &event) const {
this->Process(id, event);
});
task.get();

}


Expand Down
2 changes: 1 addition & 1 deletion sources/threading/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ThreadPool::ThreadPool(const size_t threads) : size_(threads), stop_(false) {
ThreadPool::~ThreadPool() {

stop_.store(true);

condition_.notify_all();
for(thread &thread : threads_)
thread.join();
}
Expand Down
14 changes: 14 additions & 0 deletions tests/debug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../include/vibe/vibe.h"

int main() {

Router router;
router.setPort(8080);

router.get("/",{[&](Query &web) {
web.send("Hello World, Debug");
}});


router.listen();
}
Loading

0 comments on commit 561e49d

Please sign in to comment.