Skip to content

Commit 5a548f5

Browse files
committed
Fix tests [14] --filter=[core] --verbose
1 parent 14cb562 commit 5a548f5

File tree

10 files changed

+56
-128
lines changed

10 files changed

+56
-128
lines changed

core-tests/src/core/wheel_timer.cpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

core-tests/src/os/async.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,36 @@ TEST(async, schedule) {
109109
ASSERT_EQ(handle_count, N);
110110
ASSERT_EQ(callback_count, N);
111111
}
112+
113+
114+
TEST(async, misc) {
115+
int count = 1000;
116+
callback_count = 0;
117+
std::atomic<int> handle_count(0);
118+
AsyncEvent event = {};
119+
AsyncEvent *rv;
120+
event.object = &handle_count;
121+
event.callback = [](AsyncEvent *event) { callback_count++; };
122+
event.handler = [](AsyncEvent *event) { ++(*static_cast<std::atomic<int> *>(event->object)); };
123+
124+
swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);
125+
126+
auto ret = swoole::async::dispatch(&event);
127+
EXPECT_EQ(ret->object, event.object);
128+
129+
sw_async_threads()->notify_one();
130+
131+
AsyncEvent event2 = { };
132+
event2.callback = [](AsyncEvent *event) {
133+
ASSERT_EQ(event->retval, -1);
134+
ASSERT_EQ(event->error, SW_ERROR_AIO_BAD_REQUEST);
135+
callback_count++;
136+
};
137+
rv = swoole::async::dispatch(&event2);
138+
EXPECT_NE(rv, nullptr);
139+
140+
swoole_event_wait();
141+
142+
ASSERT_EQ(handle_count, 1);
143+
ASSERT_EQ(callback_count, 2);
144+
}

core-tests/src/os/os.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ TEST(os, thread_name) {
7777
});
7878
t.join();
7979
}
80+
81+
TEST(os, thread_id) {
82+
auto tid = swoole_thread_id_to_str(std::this_thread::get_id());
83+
DEBUG() << "current thread id: " << tid << "\n";
84+
ASSERT_FALSE(tid.empty());
85+
}

include/swoole_thread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
#include "swoole_lock.h"
2121

2222
#include <thread>
23+
#include <string>
2324

2425
long swoole_thread_get_native_id(void);
2526
bool swoole_thread_set_name(const char *name);
2627
bool swoole_thread_get_name(char *buf, size_t len);
28+
std::string swoole_thread_id_to_str(std::thread::id id);
2729

2830
namespace swoole {
2931
class Thread {

include/swoole_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <set>
2727
#include <vector>
2828
#include <stack>
29+
#include <thread>
2930
#include <type_traits>
3031

3132
#define __SCOPEGUARD_CONCATENATE_IMPL(s1, s2) s1##s2
@@ -243,5 +244,4 @@ static inline bool ends_with(const char *haystack, size_t l_haystack, const char
243244
}
244245
return memcmp(haystack + l_haystack - l_needle, needle, l_needle) == 0;
245246
}
246-
247247
} // namespace swoole

include/swoole_wheel_timer.h

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/core/misc.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,4 @@ uint64_t swoole_hash_austin(const char *key, size_t keylen) {
223223

224224
return h;
225225
}
226+

src/os/async_thread.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swoole_pipe.h"
2323
#include "swoole_async.h"
2424
#include "swoole_util.h"
25+
#include "swoole_thread.h"
2526

2627
#include <thread>
2728
#include <atomic>
@@ -30,7 +31,6 @@
3031
#include <condition_variable>
3132
#include <mutex>
3233
#include <queue>
33-
#include <sstream>
3434
#include <system_error>
3535

3636
static std::mutex async_thread_lock;
@@ -173,22 +173,16 @@ class ThreadPool {
173173
return _queue.count();
174174
}
175175

176-
static std::string get_thread_id(std::thread::id id) {
177-
std::stringstream ss;
178-
ss << id;
179-
return ss.str();
180-
}
181-
182176
void release_thread(std::thread::id tid) {
183177
auto i = threads.find(tid);
184178
if (i == threads.end()) {
185-
swoole_warning("AIO thread#%s is missing", get_thread_id(tid).c_str());
179+
swoole_warning("AIO thread#%s is missing", swoole_thread_id_to_str(tid).c_str());
186180
return;
187181
} else {
188182
std::thread *_thread = i->second;
189183
swoole_trace_log(SW_TRACE_AIO,
190184
"release idle thread#%s, we have %zu now",
191-
get_thread_id(tid).c_str(),
185+
swoole_thread_id_to_str(tid).c_str(),
192186
threads.size() - 1);
193187
if (_thread->joinable()) {
194188
_thread->join();

src/os/base.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "swoole_socket.h"
2121
#include "swoole_async.h"
2222

23+
#include <thread>
24+
#include <sstream>
25+
2326
#include <arpa/inet.h>
2427

2528
#if __APPLE__
@@ -143,6 +146,12 @@ bool swoole_thread_get_name(char *buf, size_t len) {
143146
return check_pthread_return_value(pthread_getname_np(pthread_self(), buf, len));
144147
}
145148

149+
std::string swoole_thread_id_to_str(std::thread::id id) {
150+
std::stringstream ss;
151+
ss << id;
152+
return ss.str();
153+
}
154+
146155
namespace swoole {
147156
GetaddrinfoRequest::GetaddrinfoRequest(
148157
std::string _hostname, int _family, int _socktype, int _protocol, std::string _service)

src/server/master.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ int Server::create() {
810810
minimum_connection += ports.back()->get_fd();
811811
}
812812
if (max_connection < minimum_connection) {
813-
auto real_max_connection = SW_MAX(minimum_connection + 1, (int) SwooleG.max_sockets);
813+
auto real_max_connection = SW_MAX(minimum_connection + 1, SwooleG.max_sockets);
814814
swoole_warning(
815815
"max_connection must be bigger than %u, it's reset to %u", minimum_connection, real_max_connection);
816816
max_connection = real_max_connection;

0 commit comments

Comments
 (0)