Skip to content

Commit 01eaacc

Browse files
authored
Merge pull request #3 from NestorDP/action-unit-tests
🛠️ chore(CI): Add unit-tests task
2 parents 2e95903 + 0f1f44d commit 01eaacc

2 files changed

Lines changed: 54 additions & 12 deletions

File tree

.github/workflows/unit-tests.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: unit-tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Install system deps
17+
run: |
18+
sudo apt-get update
19+
sudo apt-get install -y build-essential cmake git python3 python3-pip
20+
git clone https://github.com/google/googletest.git -b v1.14.0 && cd googletest && cmake . && make && sudo make install
21+
- name: Create build directory
22+
run: |
23+
mkdir -p build
24+
cd build
25+
cmake -DBUILD_TESTING=ON ..
26+
27+
- name: Build
28+
run: |
29+
cd build
30+
cmake --build . -- -j$(nproc)
31+
32+
- name: Run unit tests
33+
run: |
34+
cd build
35+
ctest --output-on-failure -j2
36+
37+
- name: Upload ctest results (Testing directory)
38+
if: always()
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: ctest-results
42+
path: build/Testing

src/serial.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "libserial/serial.hpp"
1010
#include <string>
11+
#include <poll.h>
1112

1213
namespace libserial {
1314

@@ -94,22 +95,21 @@ size_t Serial::readUntil(std::shared_ptr<std::string> buffer, char terminator) {
9495
throw SerialException("Read timeout exceeded while waiting for terminator");
9596
}
9697

97-
// Use select() to check if data is available with remaining timeout
98-
fd_set read_fds;
99-
FD_ZERO(&read_fds);
100-
FD_SET(fd_serial_port_, &read_fds);
98+
// Use poll() to check if data is available with remaining timeout.
99+
// poll() does not have the FD_SETSIZE limitation that select() has
100+
// and is more robust for larger file descriptor values.
101+
struct pollfd pfd;
102+
pfd.fd = fd_serial_port_;
103+
pfd.events = POLLIN;
101104

102-
struct timeval timeout;
103105
int64_t remaining_timeout = read_timeout_ - elapsed;
104-
timeout.tv_sec = remaining_timeout / 1000;
105-
timeout.tv_usec = (remaining_timeout % 1000) * 1000;
106+
int timeout_ms = static_cast<int>(remaining_timeout);
106107

107-
int select_result = select(fd_serial_port_ + 1, &read_fds, nullptr, nullptr, &timeout);
108-
109-
if (select_result < 0) {
110-
throw SerialException("Error in select(): " + std::string(strerror(errno)));
108+
int poll_result = poll(&pfd, 1, timeout_ms);
109+
if (poll_result < 0) {
110+
throw SerialException("Error in poll(): " + std::string(strerror(errno)));
111111
}
112-
else if (select_result == 0) {
112+
else if (poll_result == 0) {
113113
throw SerialException("Read timeout exceeded while waiting for data");
114114
}
115115
}

0 commit comments

Comments
 (0)