From abf01e6b0ea9559318ffbb43d4c23826fc5471a0 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 14 Oct 2025 19:46:08 -0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20chore(CI):=20Add=20?= =?UTF-8?q?unit-tests=20task?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 41 ++++++++++++++++++++++++++++++++++++++++ src/serial.cpp | 25 ++++++++++++------------ 2 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c41e36f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,41 @@ +name: unit-tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build-and-test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install system deps + run: | + sudo apt-get update + sudo apt-get install -y build-essential cmake git python3 python3-pip + - name: Create build directory + run: | + mkdir -p build + cd build + cmake -DBUILD_TESTING=ON .. + + - name: Build + run: | + cd build + cmake --build . -- -j$(nproc) + + - name: Run unit tests + run: | + cd build + ctest --output-on-failure -j2 + + - name: Upload ctest results (Testing directory) + if: always() + uses: actions/upload-artifact@v4 + with: + name: ctest-results + path: build/Testing diff --git a/src/serial.cpp b/src/serial.cpp index e54484f..e4d9e50 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -8,6 +8,7 @@ #include "libserial/serial.hpp" #include +#include namespace libserial { @@ -94,22 +95,20 @@ size_t Serial::readUntil(std::shared_ptr buffer, char terminator) { throw SerialException("Read timeout exceeded while waiting for terminator"); } - // Use select() to check if data is available with remaining timeout - fd_set read_fds; - FD_ZERO(&read_fds); - FD_SET(fd_serial_port_, &read_fds); + // Use poll() to check if data is available with remaining timeout. + // poll() does not have the FD_SETSIZE limitation that select() has + // and is more robust for larger file descriptor values. + struct pollfd pfd; + pfd.fd = fd_serial_port_; + pfd.events = POLLIN; - struct timeval timeout; int64_t remaining_timeout = read_timeout_ - elapsed; - timeout.tv_sec = remaining_timeout / 1000; - timeout.tv_usec = (remaining_timeout % 1000) * 1000; + int timeout_ms = static_cast(remaining_timeout); - int select_result = select(fd_serial_port_ + 1, &read_fds, nullptr, nullptr, &timeout); - - if (select_result < 0) { - throw SerialException("Error in select(): " + std::string(strerror(errno))); - } - else if (select_result == 0) { + int poll_result = poll(&pfd, 1, timeout_ms); + if (poll_result < 0) { + throw SerialException("Error in poll(): " + std::string(strerror(errno))); + } else if (poll_result == 0) { throw SerialException("Read timeout exceeded while waiting for data"); } } From 4569c54c4da429a3c3d53833061a5d02e5ddba29 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 14 Oct 2025 19:52:08 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20style(uncrustify):=20Fix=20l?= =?UTF-8?q?int=20erro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/serial.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/serial.cpp b/src/serial.cpp index e4d9e50..2d96552 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -108,7 +108,8 @@ size_t Serial::readUntil(std::shared_ptr buffer, char terminator) { int poll_result = poll(&pfd, 1, timeout_ms); if (poll_result < 0) { throw SerialException("Error in poll(): " + std::string(strerror(errno))); - } else if (poll_result == 0) { + } + else if (poll_result == 0) { throw SerialException("Read timeout exceeded while waiting for data"); } } From 0f1f44d9cba9abd110a1f3eccb3495cd5ee84a89 Mon Sep 17 00:00:00 2001 From: Nestor Neto Date: Tue, 14 Oct 2025 19:54:58 -0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20chore(CI):=20Instal?= =?UTF-8?q?=20gtest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/{ci.yml => unit-tests.yml} | 1 + 1 file changed, 1 insertion(+) rename .github/workflows/{ci.yml => unit-tests.yml} (87%) diff --git a/.github/workflows/ci.yml b/.github/workflows/unit-tests.yml similarity index 87% rename from .github/workflows/ci.yml rename to .github/workflows/unit-tests.yml index c41e36f..669811e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/unit-tests.yml @@ -17,6 +17,7 @@ jobs: run: | sudo apt-get update sudo apt-get install -y build-essential cmake git python3 python3-pip + git clone https://github.com/google/googletest.git -b v1.14.0 && cd googletest && cmake . && make && sudo make install - name: Create build directory run: | mkdir -p build