From 1db53d966665142433de26e2c692f9a60b8a0b88 Mon Sep 17 00:00:00 2001 From: Huiba Li Date: Mon, 22 Apr 2024 17:30:10 +0800 Subject: [PATCH] fix warnings in CI, as well as bugs (#460) * fix warnings in CI, as well as bugs --- CMakeLists.txt | 2 +- common/PMF.h | 3 + common/checksum/test/test_checksum.cpp | 3 +- common/estring.h | 4 +- common/executor/test/test_async.cpp | 3 +- common/executor/test/test_easy.cpp | 3 +- common/executor/test/test_easyexport.cpp | 3 +- .../executor/test/test_export_as_executor.cpp | 3 +- common/executor/test/test_std.cpp | 3 +- common/generator.h | 4 +- common/iovector.h | 2 +- common/lockfree_queue.h | 2 +- common/memory-stream/test/test.cpp | 2 +- common/range-lock.h | 12 +- common/stream-messenger/test/test.cpp | 2 +- common/string-keyed.h | 27 ++-- common/test/test.cpp | 20 +-- common/test/test_alog.cpp | 6 +- common/test/test_constexprstr.cpp | 3 +- common/test/test_lockfree.cpp | 4 +- common/test/test_objcache.cpp | 7 +- common/test/test_scalepool.cpp | 2 +- common/test/test_throttle.cpp | 2 +- common/utility.h | 23 +-- ecosystem/simple_dom.h | 6 +- ecosystem/test/test.cpp | 3 +- ecosystem/test/test_simple_dom.cpp | 8 +- examples/perf/multi-conn-perf.cpp | 13 +- fs/extfs/test/test.cpp | 13 +- fs/test/test.cpp | 153 +++++++++--------- fs/test/test_exportfs.cpp | 2 +- fs/test/test_filecopy.cpp | 3 +- fs/test/test_throttledfile.cpp | 6 +- io/test/signalfdboom.cpp | 2 +- io/test/signalfdtest.cpp | 3 +- io/test/test-fork.cpp | 7 +- io/test/test-iouring.cpp | 8 +- io/test/test-syncio.cpp | 2 +- net/http/client.h | 6 + net/http/test/client_function_test.cpp | 36 +++-- net/http/test/client_perf.cpp | 14 +- net/http/test/client_tls_test.cpp | 6 +- net/http/test/cookie_jar_test.cpp | 2 +- net/http/test/headers_test.cpp | 10 +- net/http/test/server_function_test.cpp | 60 +++---- net/security-context/test/test-sasl.cpp | 2 +- net/security-context/test/test.cpp | 2 +- net/test/test-ipv6.cpp | 5 +- net/test/test-udp.cpp | 15 +- net/test/test.cpp | 16 +- net/test/test_base64.cpp | 2 +- net/test/test_curl.cpp | 2 +- net/test/test_sockpool.cpp | 2 +- rpc/test/test-ooo.cpp | 10 +- rpc/test/test-rpc-message.cpp | 14 +- rpc/test/test.cpp | 2 +- test/gtest.h | 21 +++ thread/test/perf_usleepdefer_semaphore.cpp | 2 +- thread/test/perf_workpool.cpp | 6 +- thread/test/test-lib-data.cpp | 2 +- thread/test/test-multi-vcpu-locking.cpp | 2 +- thread/test/test-pooled-stack-allocator.cpp | 4 +- thread/test/test-specific-key.cpp | 6 +- thread/test/test-std-compat.cpp | 2 +- thread/test/test-thread-local.cpp | 2 +- thread/test/test-tls-order-native.cpp | 6 +- thread/test/test-tls-order-photon.cpp | 2 +- thread/test/test.cpp | 16 +- 68 files changed, 344 insertions(+), 307 deletions(-) create mode 100644 test/gtest.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b3fc816..0cd372a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -344,7 +344,7 @@ endif () if (PHOTON_BUILD_TESTING) include(CTest) - add_compile_options(-Wno-error) + # add_compile_options(-Wno-error) add_library(ci-tools STATIC test/ci-tools.cpp) diff --git a/common/PMF.h b/common/PMF.h index b709917a..286ee282 100644 --- a/common/PMF.h +++ b/common/PMF.h @@ -24,6 +24,8 @@ struct pmf_map T* obj; // may be adjusted for virtual function call }; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Warray-bounds" template inline auto __get_mfa__(T* obj, MF f) -> pmf_map @@ -65,6 +67,7 @@ inline auto __get_mfa__(T* obj, MF f) auto addr = pmf.get_function_address((void*&)obj); return pmf_map{(PF)addr, obj}; } +#pragma GCC diagnostic pop template inline auto get_member_function_address(T* obj, R (T::*f)(ARGS...)) diff --git a/common/checksum/test/test_checksum.cpp b/common/checksum/test/test_checksum.cpp index e6549ff4..cb63d629 100644 --- a/common/checksum/test/test_checksum.cpp +++ b/common/checksum/test/test_checksum.cpp @@ -1,11 +1,10 @@ -#include #include - #include #include #include #include #include +#include "../../../test/gtest.h" #ifndef DATA_DIR #define DATA_DIR "" diff --git a/common/estring.h b/common/estring.h index cdd90af7..4a562fe9 100644 --- a/common/estring.h +++ b/common/estring.h @@ -308,9 +308,9 @@ class estring_view : public std::string_view bool to_double_check(double* v = nullptr) { char buf[32]; - auto len = std::max(this->size(), sizeof(buf) - 1 ); + auto len = std::min(this->size(), sizeof(buf) - 1 ); memcpy(buf, data(), len); - buf[len] = '0'; + buf[len] = '\0'; return sscanf(buf, "%lf", v) == 1; } double to_double(double default_val = NAN) diff --git a/common/executor/test/test_async.cpp b/common/executor/test/test_async.cpp index 6248113b..fa397214 100644 --- a/common/executor/test/test_async.cpp +++ b/common/executor/test/test_async.cpp @@ -15,7 +15,6 @@ limitations under the License. */ #include -#include #include #include #include @@ -25,9 +24,9 @@ limitations under the License. #include #include #include - #include #include +#include "../../../test/gtest.h" using namespace photon; diff --git a/common/executor/test/test_easy.cpp b/common/executor/test/test_easy.cpp index bd296124..432a9236 100644 --- a/common/executor/test/test_easy.cpp +++ b/common/executor/test/test_easy.cpp @@ -15,8 +15,6 @@ limitations under the License. */ #include -#include - #include #include #include @@ -24,6 +22,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" using namespace photon; diff --git a/common/executor/test/test_easyexport.cpp b/common/executor/test/test_easyexport.cpp index 3266f395..7c4ef805 100644 --- a/common/executor/test/test_easyexport.cpp +++ b/common/executor/test/test_easyexport.cpp @@ -15,8 +15,6 @@ limitations under the License. */ #include -#include - #include #include #include @@ -25,6 +23,7 @@ limitations under the License. #include #include #include +#include "../../../test/gtest.h" using namespace photon; diff --git a/common/executor/test/test_export_as_executor.cpp b/common/executor/test/test_export_as_executor.cpp index 33c1bd19..d330147a 100644 --- a/common/executor/test/test_export_as_executor.cpp +++ b/common/executor/test/test_export_as_executor.cpp @@ -1,11 +1,10 @@ -#include #include #include #include #include #include - #include +#include "../../test/gtest.h" TEST(enter_as_executor, test) { // set global default logger output to null diff --git a/common/executor/test/test_std.cpp b/common/executor/test/test_std.cpp index 26235661..4f3e62af 100644 --- a/common/executor/test/test_std.cpp +++ b/common/executor/test/test_std.cpp @@ -15,14 +15,13 @@ limitations under the License. */ #include -#include - #include #include #include #include #include #include +#include "../../../test/gtest.h" using namespace photon; diff --git a/common/generator.h b/common/generator.h index 408cd2b5..0a36bdf0 100644 --- a/common/generator.h +++ b/common/generator.h @@ -161,7 +161,7 @@ inline void ___example_of_generator____() }; { - int i = 0; + int i = 0; (void)i; for (auto x: example_generator1(10)) { assert(x == i); i++; @@ -198,7 +198,7 @@ inline void ___example_of_generator____() }; { - int i = 0; + int i = 0; (void)i; for (auto x: example_generator2(10)) { assert(x == i); i++; diff --git a/common/iovector.h b/common/iovector.h index 8b73e157..a01746c1 100644 --- a/common/iovector.h +++ b/common/iovector.h @@ -900,8 +900,8 @@ class iovector } void copy(IOAlloc* rhs, uint16_t nbases) { + *(IOAlloc*)this = *rhs; auto rhs_ = (IOVAllocation_*)rhs; - *this = *rhs_; memcpy(bases, rhs_->bases, sizeof(bases[0]) * nbases); } }; diff --git a/common/lockfree_queue.h b/common/lockfree_queue.h index c3c20b5f..9f255a44 100644 --- a/common/lockfree_queue.h +++ b/common/lockfree_queue.h @@ -472,7 +472,7 @@ class LockfreeSPSCRingQueue : public LockfreeRingQueueBase { n = std::min(n, tail.load(std::memory_order_acquire) - h); if (n == 0) return 0; auto first_idx = idx(h); - auto last_idx = idx(h + n - 1); + auto last_idx = idx(h + n - 1); (void)last_idx; auto part_length = Base::capacity - first_idx; if (likely(part_length >= n)) { memcpy(x, &slots[first_idx], sizeof(T) * n); diff --git a/common/memory-stream/test/test.cpp b/common/memory-stream/test/test.cpp index 330e6544..0af7505e 100644 --- a/common/memory-stream/test/test.cpp +++ b/common/memory-stream/test/test.cpp @@ -18,11 +18,11 @@ limitations under the License. #include #include #include -#include #include #include #include #include +#include "../../../test/gtest.h" using namespace std; using namespace photon; diff --git a/common/range-lock.h b/common/range-lock.h index 69077c78..bf8f3057 100644 --- a/common/range-lock.h +++ b/common/range-lock.h @@ -69,7 +69,7 @@ class RangeLock it = m_index.emplace_hint(it, r); assert(it != m_index.end()); static_assert(sizeof(it) == sizeof(LockHandle*), "..."); - return (LockHandle*&)it; + return __reinterpret_cast(it); } } @@ -87,7 +87,7 @@ class RangeLock if (!h) return -1; range_t r1(offset, length); SCOPED_LOCK(m_lock); - auto it = (iterator&)h; + auto it = __reinterpret_cast(h); auto r0 = (range_t*) &*it; if ((r1.offset < r0->offset && r1.offset < prev_end(it)) || (r1.end() > it->end() && r1.end() > next_offset(it))) @@ -100,7 +100,7 @@ class RangeLock void unlock(LockHandle* h) { SCOPED_LOCK(m_lock); - auto it = (iterator&)h; + auto it = __reinterpret_cast(h); m_index.erase(it); } @@ -147,6 +147,12 @@ class RangeLock { return (it == m_index.begin()) ? 0 : (--it)->end(); } + template + T& __reinterpret_cast(P& x) { + static_assert(sizeof(P) == sizeof(T), "..."); + auto y = (T*)&x; + return *y; + } }; class ScopedRangeLock diff --git a/common/stream-messenger/test/test.cpp b/common/stream-messenger/test/test.cpp index 48fc0d97..7196f97f 100644 --- a/common/stream-messenger/test/test.cpp +++ b/common/stream-messenger/test/test.cpp @@ -18,11 +18,11 @@ limitations under the License. #include #include #include -#include #include #include #include #include +#include "../../../test/gtest.h" using namespace std; using namespace photon; using namespace StreamMessenger; diff --git a/common/string-keyed.h b/common/string-keyed.h index 3415285f..9aea4c9e 100644 --- a/common/string-keyed.h +++ b/common/string-keyed.h @@ -264,6 +264,10 @@ class basic_map_string_kv : public M mutable bool _has_val = false; iterator(base_it b_it) : _b_it(b_it) { } + iterator(typename base::const_iterator b_it) { + auto x = (base_it*) &b_it; + _b_it = *x; + } mutable_value_type& _init_val() const { if (_has_val) return _val; @@ -321,11 +325,9 @@ class basic_map_string_kv : public M { return base::begin(); } - using B_IT = typename base::iterator; const_iterator end() const { - auto it = base::end(); - return (B_IT&)it; + return {base::end()}; } iterator end() { @@ -353,13 +355,11 @@ class basic_map_string_kv : public M } const_iterator find ( const key_type& k ) const { - auto it = base::find((const skvm&)k); - return (B_IT&)it; + return {base::find((const skvm&)k)}; } iterator find ( const key_type& k ) { - auto it = base::find((const skvm&)k); - return (B_IT&)it; + return {base::find((const skvm&)k)}; } size_type count ( const key_type& k ) const { @@ -367,8 +367,7 @@ class basic_map_string_kv : public M } std::pair equal_range ( const key_type& k ) const { - auto r = base::equal_range((const skvm&)k); - return {(B_IT&) r.first, (B_IT&) r.second}; + return {base::equal_range((const skvm&)k)}; } std::pair emplace (const key_type& k, const mapped_type& v ) { @@ -437,18 +436,12 @@ class map_string_kv : public basic_map_string_kv> { { return base::upper_bound((const skvm&)k); } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstrict-aliasing" - using typename base::B_IT; const_iterator lower_bound (const key_type& k) const { - auto it = base::lower_bound((const skvm&)k); - return (B_IT&)it; + return {base::lower_bound((const skvm&)k)}; } const_iterator upper_bound (const key_type& k) const { - auto it = base::upper_bound((const skvm&)k); - return (B_IT&)it; + return {base::upper_bound((const skvm&)k)}; } -#pragma GCC diagnostic pop }; diff --git a/common/test/test.cpp b/common/test/test.cpp index 45b4142a..828d3c72 100644 --- a/common/test/test.cpp +++ b/common/test/test.cpp @@ -45,12 +45,11 @@ limitations under the License. #include #include //#include -#include -#include //#include #ifndef __clang__ #include #endif +#include "../../test/gtest.h" using namespace std; @@ -872,8 +871,8 @@ TEST(estring, test) EXPECT_EQ(estring_view("234423").to_uint64(), 234423); EXPECT_EQ(estring_view("-234423").to_int64(), -234423); EXPECT_EQ(estring_view("asfdsf").to_uint64(32), 32); - EXPECT_EQ(estring_view("-3.14").to_double(), -3.14); - EXPECT_EQ(estring_view("1e10").to_double(), 1e10); + EXPECT_NEAR(estring_view("-3.14").to_double(), -3.14, 1e-5); + EXPECT_NEAR(estring_view("1e10").to_double(), 1e10, 1e-5); EXPECT_EQ(estring_view("1").hex_to_uint64(), 0x1); EXPECT_EQ(estring_view("1a2b3d4e5f").hex_to_uint64(), 0x1a2b3d4e5f); @@ -920,7 +919,8 @@ TEST(retval, basic) { EXPECT_EQ(rvs[2], -1234); EXPECT_EQ(rvs[3], -5234); - for (int i = 0; i < LEN(rvs); ++i) { + for (auto i: xrange(LEN(rvs))) { + static_assert(std::is_same::value, "..."); auto ret = foo(i); LOG_DEBUG("got ", ret); EXPECT_EQ(ret, rvs[i]); @@ -954,7 +954,7 @@ void basic_map_test(T &test_map) { char xname[1000]; auto p = test_map.begin(); for (int i = 200000; i < 300000; i++) if (i % 2 == 0) { - sprintf(xname, "%s%d", prefix.c_str(), i); + snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i); auto s = std::string_view(xname).substr(prefix.size()); test_map.insert(p, make_pair(xname, s)); ASSERT_EQ(test_map.size(), i/2+1); @@ -962,7 +962,7 @@ void basic_map_test(T &test_map) { // LOG_DEBUG("asdf"); for (int i = 300000; i < 400000; i++) if (i % 2 == 0) { - sprintf(xname, "%s%d", prefix.c_str(), i); + snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i); auto s = std::string_view(xname).substr(prefix.size()); test_map[xname] = s; EXPECT_EQ(test_map[xname], s); @@ -971,7 +971,7 @@ void basic_map_test(T &test_map) { // LOG_DEBUG("asdf"); for (int i = 400000; i < 500000; i++) if (i % 2 == 0) { - sprintf(xname, "%s%d", prefix.c_str(), i); + snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i); auto s = std::string_view(xname).substr(prefix.size()); test_map.insert(pair{xname, s}); EXPECT_EQ(test_map.size(), i/2+1); @@ -1087,7 +1087,7 @@ TEST(string_key, unordered_map_string_key) { std::string s = std::to_string(i); // string_view view(s); char chars[1000]; - sprintf(chars, "%d", i); + snprintf(chars, sizeof(chars), "%d", i); // std::pair pr = make_pair(string_view(s), i); // unordered_map_string_key::value_type x = make_pair(s, i); const std::pair x = make_pair(s, i);//{s, i}; @@ -1100,7 +1100,7 @@ TEST(string_key, unordered_map_string_key) { //std::string std::string s = std::to_string(i); char chars[1000]; - sprintf(chars, "%d", i); + snprintf(chars, sizeof(chars), "%d", i); // string_key k(s); // string_view view(s); string_view sv(chars); diff --git a/common/test/test_alog.cpp b/common/test/test_alog.cpp index e0fc8f0a..49d1129e 100644 --- a/common/test/test_alog.cpp +++ b/common/test/test_alog.cpp @@ -19,7 +19,6 @@ limitations under the License. #include "../alog-stdstring.h" #include "../alog-functionptr.h" #include "../alog-audit.h" -#include #include #include #include @@ -28,6 +27,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" class LogOutputTest : public ILogOutput { public: @@ -408,8 +408,8 @@ void test_defer() TEST(test, test) { - char asdf[20]; -// int qwer[LEN(asdf)]; +// char asdf[20]; +// int qwer[LEN(asdf)]; // vector uio; // should not compile! to avoid misuse // auto len = LEN(uio); diff --git a/common/test/test_constexprstr.cpp b/common/test/test_constexprstr.cpp index 11409e2c..00ae33a9 100644 --- a/common/test/test_constexprstr.cpp +++ b/common/test/test_constexprstr.cpp @@ -14,14 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include - #include #include #include "../alog-stdstring.h" #include "../alog.h" #include "../conststr.h" +#include "../../test/gtest.h" DEFINE_ENUM_STR(VERBS, verbs, UNKNOW, DELETE, GET, HEAD, POST, PUT, CONNECT, OPTIONS, TRACE, COPY, LOCK, MKCOL, MOV, PROPFIND, PROPPATCH, diff --git a/common/test/test_lockfree.cpp b/common/test/test_lockfree.cpp index 1e6bf083..a0d9085b 100644 --- a/common/test/test_lockfree.cpp +++ b/common/test/test_lockfree.cpp @@ -14,7 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include @@ -175,7 +177,7 @@ int test_queue_batch(const char *name, QType &queue) { } LRType::unlock(rlock); if (x) rspent += (std::chrono::high_resolution_clock::now() - tm) / size; - for (auto y = 0; y < size; y++) { + for (auto y: xrange(size)) { rc[buffer[y]]++; rcnt[i]++; } diff --git a/common/test/test_objcache.cpp b/common/test/test_objcache.cpp index 84c8c822..b8b22971 100644 --- a/common/test/test_objcache.cpp +++ b/common/test/test_objcache.cpp @@ -21,11 +21,10 @@ limitations under the License. #undef private #undef protected -#include #include #include - #include +#include "../../test/gtest.h" static int thread_local release_cnt = 0; struct ShowOnDtor { @@ -100,7 +99,7 @@ TEST(ObjectCache, timeout_refresh) { ObjectCache ocache(1000UL * 1000); // 1s auto ctor = [] { return new ShowOnDtor(0); }; - auto ret = ocache.acquire(0, ctor); + auto ret = ocache.acquire(0, ctor); (void)ret; photon::thread_usleep(1100UL * 1000); ocache.expire(); ocache.release(0); @@ -249,7 +248,7 @@ struct OCArg2 { void* objcache_borrow_once(void* arg) { auto args = (OCArg2*)arg; auto oc = args->oc; - auto id = args->id; + // auto id = args->id; auto& count = *args->count; auto ctor = [&]() { // failed after 1s; diff --git a/common/test/test_scalepool.cpp b/common/test/test_scalepool.cpp index 696cf66c..30fffc81 100644 --- a/common/test/test_scalepool.cpp +++ b/common/test/test_scalepool.cpp @@ -22,9 +22,9 @@ limitations under the License. #undef protected #include "../alog.h" -#include #include #include +#include "../../test/gtest.h" TEST(IdentityPoolGC, basic) { auto pool = new_identity_pool(128); diff --git a/common/test/test_throttle.cpp b/common/test/test_throttle.cpp index f44289b5..bd597e84 100644 --- a/common/test/test_throttle.cpp +++ b/common/test/test_throttle.cpp @@ -1,13 +1,13 @@ #include #include #include -#include #include #include #include #include #include #include +#include "../../test/gtest.h" #include "../../test/ci-tools.h" TEST(Throttle, basic) { diff --git a/common/utility.h b/common/utility.h index ddf57f0d..02196bac 100644 --- a/common/utility.h +++ b/common/utility.h @@ -18,6 +18,7 @@ limitations under the License. #include #include #include +#include #include #include "string_view.h" // #include @@ -151,7 +152,7 @@ struct xrange_t } bool operator == (const iterator& rhs) const { - return _xrange == rhs._xrange && (i == rhs.i || (i >= _xrange->_end && rhs.i >= _xrange->_end)); + return _xrange == rhs._xrange && i == rhs.i; } bool operator != (const iterator& rhs) const { @@ -173,19 +174,16 @@ struct xrange_t // xrange() function of Python // usage: for (auto i: xrange(2, 8)) { ... } -template::value)> -xrange_t xrange(T begin, T end, int64_t step = 1) -{ +template inline +xrange_t xrange(T begin, T end, int64_t step = 1) { static_assert(std::is_integral::value, "..."); - return xrange_t{begin, end, step}; -} - -template::value)> -xrange_t xrange(T end) -{ - return xrange(0, end); + assert(begin < end && (end - begin) % step == 0); + return xrange_t{begin, end, step}; } +template inline +xrange_t xrange(T end) { return xrange(0, end); } +/* template::value)> xrange_t xrange(T begin, T end, int64_t step = 1) { @@ -198,6 +196,9 @@ xrange_t xrange(T end) { return xrange(0, end); } +*/ + +#define FOR_LOOP(N) for (auto i = N; i; --i) inline uint64_t align_down(uint64_t x, uint64_t alignment) { diff --git a/ecosystem/simple_dom.h b/ecosystem/simple_dom.h index e47254a8..e3ce6552 100644 --- a/ecosystem/simple_dom.h +++ b/ecosystem/simple_dom.h @@ -91,12 +91,8 @@ class Node { Node get_attributes() const { return get("__attributes__"); } str to_string() const { return value(); } #undef IF_RET - template - Node operator[](const char (&key)[N]) const { - return get(key); - } int64_t to_integer(int64_t def_val = 0) const { - return value().to_uint64(def_val); + return value().to_int64(def_val); } double to_number(double def_val = NAN) const { return value().to_double(def_val); diff --git a/ecosystem/test/test.cpp b/ecosystem/test/test.cpp index 9dd1e176..22fdbc88 100644 --- a/ecosystem/test/test.cpp +++ b/ecosystem/test/test.cpp @@ -14,8 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include - +#include "../../test/gtest.h" int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/ecosystem/test/test_simple_dom.cpp b/ecosystem/test/test_simple_dom.cpp index 28a7e8a4..8216f396 100644 --- a/ecosystem/test/test_simple_dom.cpp +++ b/ecosystem/test/test_simple_dom.cpp @@ -22,9 +22,9 @@ limitations under the License. #include #include #include -#include #include #include +#include "../../test/gtest.h" using namespace std; using namespace photon::SimpleDOM; @@ -188,7 +188,7 @@ TEST(simple_dom, json) { "t": true , "f": false, "n": null, - "i": 123, + "i": -123, "pi": 3.1416, "a": [1, 2, 3, 4], })"; @@ -198,9 +198,11 @@ TEST(simple_dom, json) { {"hello", "world"}, {"t", "true"}, {"f", "false"}, - {"i", "123"}, + {"i", "-123"}, {"pi", "3.1416"}, }); + EXPECT_EQ(doc["i"].to_integer(), -123); + EXPECT_NEAR(doc["pi"].to_number(), 3.1416, 1e-5); expect_eq_vals(doc["a"], {"1", "2", "3", "4"}); } diff --git a/examples/perf/multi-conn-perf.cpp b/examples/perf/multi-conn-perf.cpp index d67f11ce..b37706f1 100644 --- a/examples/perf/multi-conn-perf.cpp +++ b/examples/perf/multi-conn-perf.cpp @@ -26,6 +26,7 @@ limitations under the License. #include #include #include +#include DEFINE_uint64(client_thread_num, 8, "client thread number"); DEFINE_uint64(server_thread_num, 8, "server thread number"); @@ -76,8 +77,8 @@ static int client(int client_index) { photon::init(photon::INIT_EVENT_DEFAULT, photon::INIT_IO_NONE); - for (int server_index = 0; server_index < FLAGS_server_thread_num; ++server_index) { - photon::thread_create11(do_write, server_index); + for (auto i: xrange(FLAGS_server_thread_num)) { + photon::thread_create11(do_write, i); } photon::thread_sleep(-1); return 0; @@ -117,7 +118,7 @@ static void server(int server_index) { for (int i = 0; i < num_events; ++i) { int fd = (int) (uintptr_t) data[i]; ssize_t n = photon::net::read_n(fd, buf, sizeof(buf)); - if (n != sizeof(buf)) + if (n != (ssize_t)sizeof(buf)) exit(1); qps++; } @@ -129,7 +130,7 @@ static void server(int server_index) { char buf[FLAGS_buf_size]; while (true) { ssize_t n = photon::net::read_n(_conn_fd, buf, sizeof(buf)); - if (n != sizeof(buf)) + if (n != (ssize_t)sizeof(buf)) exit(1); qps++; } @@ -161,14 +162,14 @@ int main(int argc, char** arg) { if (Mode(FLAGS_mode) == Mode::Standalone || Mode(FLAGS_mode) == Mode::Server) { photon::thread_create11(run_qps_loop); - for (int i = 0; i < FLAGS_server_thread_num; ++i) { + for (auto i: xrange(FLAGS_server_thread_num)) { std::thread(server, i).detach(); } } if (Mode(FLAGS_mode) == Mode::Standalone || Mode(FLAGS_mode) == Mode::Client) { photon::thread_sleep(1); - for (int i = 0; i < FLAGS_client_thread_num; ++i) { + for (auto i: xrange(FLAGS_client_thread_num)) { std::thread(client, i).detach(); } } diff --git a/fs/extfs/test/test.cpp b/fs/extfs/test/test.cpp index 901f80dc..cec15f22 100644 --- a/fs/extfs/test/test.cpp +++ b/fs/extfs/test/test.cpp @@ -27,11 +27,12 @@ limitations under the License. #include #include #include -#include +#include "../../../test/gtest.h" #define FILE_SIZE (2 * 1024 * 1024) void print_stat(const char *path, struct stat *st) { +/* printf("File: %s\n", path); printf("Size: %d, Blocks: %d, IO Blocks: %d, Type: %d\n", st->st_size, st->st_blocks, st->st_blksize, IFTODT(st->st_mode)); printf("Device: %u/%u, Inode: %d, Links: %d, Device type: %u,%u\n", @@ -40,6 +41,16 @@ void print_stat(const char *path, struct stat *st) { printf("Access: %s", asctime(localtime(&(st->st_atim.tv_sec)))); printf("Modify: %s", asctime(localtime(&(st->st_mtim.tv_sec)))); printf("Change: %s", asctime(localtime(&(st->st_ctim.tv_sec)))); +*/ +#define KV(k, v) make_named_value(#k, v) + LOG_INFO(VALUE(path)); + LOG_INFO(KV(Size, st->st_size), KV(Blocks, st->st_blocks), KV(BlkSize, st->st_blksize), KV(Type, IFTODT(st->st_mode))); + LOG_INFO(KV(Device, HEX(st->st_dev)), KV(Inode, st->st_ino), KV(nLinks, st->st_nlink)); + LOG_INFO(KV(Access, OCT(st->st_mode & 0xFFF)), KV(Uid, st->st_uid), KV(Gid, st->st_gid)); + LOG_INFO(KV(AccessTime, asctime(localtime(&(st->st_atim.tv_sec))))); + LOG_INFO(KV(ModifyTime, asctime(localtime(&(st->st_mtim.tv_sec))))); + LOG_INFO(KV(ChangeTime, asctime(localtime(&(st->st_ctim.tv_sec))))); +#undef KV } photon::fs::IFile *new_file(photon::fs::IFileSystem *fs, const char *path) { diff --git a/fs/test/test.cpp b/fs/test/test.cpp index 7a55438c..08aa2fc4 100644 --- a/fs/test/test.cpp +++ b/fs/test/test.cpp @@ -30,8 +30,6 @@ limitations under the License. #include #include -#include -#include #ifdef __linux__ #include #endif @@ -52,13 +50,15 @@ limitations under the License. #include #include #include - +#include "../../test/gtest.h" #include "mock.h" using namespace std; using namespace photon; using namespace photon::fs; +#pragma GCC diagnostic ignored "-Wsign-compare" + TEST(Path, split) { static const char* paths[]={ @@ -165,19 +165,19 @@ TEST(Tree, node) node.creat(k1234, (void*)1234); node.creat(k1234, (void*)2345); node.creat(k1234, v1234); - EXPECT_EQ(node.size(), 5); + EXPECT_EQ(node.size(), 5ul); for (auto x: subnodes) node.mkdir(x); - EXPECT_EQ(node.size(), 9); + EXPECT_EQ(node.size(), 9ul); f = F; void* v; for (auto x: items) { node.read(x, &v); - EXPECT_EQ(v, (void*)f++); + EXPECT_EQ(v, (void*)f); f++; node.unlink(x); EXPECT_FALSE(node.is_file(x)); } @@ -710,6 +710,8 @@ void *(*old_malloc)(size_t size, const void *caller); void *my_malloc(size_t size, const void *caller); void my_free(void *ptr, const void *caller); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void malloc_hook() { __malloc_hook = my_malloc; __free_hook = my_free; @@ -724,6 +726,7 @@ void init_hook() { old_malloc = __malloc_hook; old_free = __free_hook; } +#pragma GCC diagnostic pop void *my_malloc(size_t size, const void *caller) { return nullptr; @@ -743,8 +746,8 @@ TEST(range_split, sub_range) EXPECT_FALSE(sr); sr.assign(0, 233, 1024); EXPECT_TRUE(sr); - EXPECT_EQ(233, sr.begin()); - EXPECT_EQ(233+1024, sr.end()); + EXPECT_EQ(233ul, sr.begin()); + EXPECT_EQ(233ul+1024ul, sr.end()); sr.clear(); EXPECT_FALSE(sr); sr.assign(1, 233, 1024); @@ -757,25 +760,25 @@ TEST(range_split, range_split_simple_case) // with abegin, aend as 0, 11 // 11 parts in total EXPECT_FALSE(split.small_note); - EXPECT_EQ(42, split.begin); - EXPECT_EQ(363, split.end); - EXPECT_EQ(1, split.abegin); - EXPECT_EQ(12, split.aend); - EXPECT_EQ(2, split.apbegin); - EXPECT_EQ(split.apend,11); - EXPECT_EQ(32, split.aligned_begin_offset()); - EXPECT_EQ(384, split.aligned_end_offset()); + EXPECT_EQ(42ul, split.begin); + EXPECT_EQ(363ul, split.end); + EXPECT_EQ(1ul, split.abegin); + EXPECT_EQ(12ul, split.aend); + EXPECT_EQ(2ul, split.apbegin); + EXPECT_EQ(11ul, split.apend); + EXPECT_EQ(32ul, split.aligned_begin_offset()); + EXPECT_EQ(384ul, split.aligned_end_offset()); auto p = split.all_parts(); - EXPECT_EQ(1, p.begin()->i); - EXPECT_EQ(10, p.begin()->begin()); - EXPECT_EQ(32, p.begin()->end()); - EXPECT_EQ(12, p.end()->i); - int cnt = 1; + EXPECT_EQ(1ul, p.begin()->i); + EXPECT_EQ(10ul, p.begin()->begin()); + EXPECT_EQ(32ul, p.begin()->end()); + EXPECT_EQ(12ul, p.end()->i); + uint64_t cnt = 1; for (auto &rs: p) { EXPECT_EQ(cnt++, rs.i); if (rs != p.begin() && rs != p.end()) { - EXPECT_EQ(0, rs.begin()); - EXPECT_EQ(32, rs.end()); + EXPECT_EQ(0ul, rs.begin()); + EXPECT_EQ(32ul, rs.end()); } } split = fs::range_split(2, 12, 24); @@ -793,27 +796,27 @@ TEST(range_split, range_split_aligned_case) // it should be split into [begin, end) as [32, 64)+[64, 76) +... +[352,353) // with abegin, aend as 0, 11 // 11 parts in total - EXPECT_EQ(32, split.begin); - EXPECT_EQ(353, split.end); - EXPECT_EQ(1, split.abegin); - EXPECT_EQ(12, split.aend); - EXPECT_EQ(1, split.apbegin); - EXPECT_EQ(11, split.apend); + EXPECT_EQ(32ul, split.begin); + EXPECT_EQ(353ul, split.end); + EXPECT_EQ(1ul, split.abegin); + EXPECT_EQ(12ul, split.aend); + EXPECT_EQ(1ul, split.apbegin); + EXPECT_EQ(11ul, split.apend); auto p = split.all_parts(); EXPECT_FALSE(split.is_aligned()); EXPECT_TRUE(split.is_aligned(128)); EXPECT_TRUE(split.is_aligned_ptr((const void*)(uint64_t(65536)))); - EXPECT_EQ(1, p.begin()->i); - EXPECT_EQ(0, p.begin()->begin()); - EXPECT_EQ(32, p.begin()->end()); - EXPECT_EQ(12, p.end()->i); - EXPECT_EQ(352, split.aligned_length()); + EXPECT_EQ(1ul, p.begin()->i); + EXPECT_EQ(0ul, p.begin()->begin()); + EXPECT_EQ(32ul, p.begin()->end()); + EXPECT_EQ(12ul, p.end()->i); + EXPECT_EQ(352ul, split.aligned_length()); auto q = split.aligned_parts(); - int cnt = 1; + uint64_t cnt = 1; for (auto &rs: q) { EXPECT_EQ(cnt++, rs.i); - EXPECT_EQ(0, rs.begin()); - EXPECT_EQ(32, rs.end()); + EXPECT_EQ(0ul, rs.begin()); + EXPECT_EQ(32ul, rs.end()); } split = fs::range_split(0, 23, 24); EXPECT_TRUE(split.postface); @@ -842,25 +845,25 @@ TEST(range_split_power2, basic) { LOG_DEBUG(rs.i, ' ', rs.begin(), ' ', rs.end()); } EXPECT_FALSE(split.small_note); - EXPECT_EQ(42, split.begin); - EXPECT_EQ(363, split.end); - EXPECT_EQ(1, split.abegin); - EXPECT_EQ(12, split.aend); - EXPECT_EQ(2, split.apbegin); - EXPECT_EQ(11, split.apend); - EXPECT_EQ(32, split.aligned_begin_offset()); - EXPECT_EQ(384, split.aligned_end_offset()); + EXPECT_EQ(42ul, split.begin); + EXPECT_EQ(363ul, split.end); + EXPECT_EQ(1ul, split.abegin); + EXPECT_EQ(12ul, split.aend); + EXPECT_EQ(2ul, split.apbegin); + EXPECT_EQ(11ul, split.apend); + EXPECT_EQ(32ul, split.aligned_begin_offset()); + EXPECT_EQ(384ul, split.aligned_end_offset()); auto p = split.all_parts(); - EXPECT_EQ(p.begin()->i, 1); - EXPECT_EQ(p.begin()->begin(), 10); - EXPECT_EQ(p.begin()->end(), 32); - EXPECT_EQ(p.end()->i, 12); - int cnt = 1; + EXPECT_EQ(p.begin()->i, 1ul); + EXPECT_EQ(p.begin()->begin(), 10ul); + EXPECT_EQ(p.begin()->end(), 32ul); + EXPECT_EQ(p.end()->i, 12ul); + uint64_t cnt = 1; for (auto &rs: p) { EXPECT_EQ(rs.i, cnt++); if (rs != p.begin() && rs != p.end()) { - EXPECT_EQ(rs.begin(), 0); - EXPECT_EQ(rs.end(), 32); + EXPECT_EQ(rs.begin(), 0ul); + EXPECT_EQ(rs.end(), 32ul); } } } @@ -886,15 +889,15 @@ TEST(range_split_vi, basic) { uint64_t kp[] = {0, 32, 64, 128, 256, 512, UINT64_MAX}; fs::range_split_vi split(12, 321, kp, 7); uint64_t *it = kp; - EXPECT_EQ(12, split.begin); - EXPECT_EQ(333, split.end); - EXPECT_TRUE(split.is_aligned((uint64_t)0)); - EXPECT_FALSE(split.is_aligned(1)); - EXPECT_TRUE(split.is_aligned(128)); + EXPECT_EQ(12ul, split.begin); + EXPECT_EQ(333ul, split.end); + EXPECT_TRUE(split.is_aligned(0ul)); + EXPECT_FALSE(split.is_aligned(1ul)); + EXPECT_TRUE(split.is_aligned(128ul)); for (auto p : split.all_parts()) { LOG_DEBUG(p.i, ' ', p.begin(), ' ', p.end()); - EXPECT_EQ(*it == 0?12:0, p.begin()); - EXPECT_EQ(*it == 256? 321-256+12 :(*(it+1) - *it), p.end()); + EXPECT_EQ(*it == 0ul ? 12ul : 0ul, p.begin()); + EXPECT_EQ(*it == 256ul ? 321ul-256ul+12ul : (*(it+1) - *it), p.end()); it++; } uint64_t kpfail[] = {0, 32, 796, 128, 256, 512, UINT64_MAX}; @@ -911,14 +914,14 @@ TEST(range_split_vi, left_side_aligned) { uint64_t kp[] = {0, 32, 64, 128, 256, 512, UINT64_MAX}; fs::range_split_vi split(0, 256, kp, 7); uint64_t *it = kp; - EXPECT_EQ(0, split.begin); - EXPECT_EQ(256, split.end); - EXPECT_TRUE(split.is_aligned((uint64_t)0)); - EXPECT_FALSE(split.is_aligned(1)); - EXPECT_TRUE(split.is_aligned(128)); + EXPECT_EQ(0ul, split.begin); + EXPECT_EQ(256ul, split.end); + EXPECT_TRUE(split.is_aligned(0ul)); + EXPECT_FALSE(split.is_aligned(1ul)); + EXPECT_TRUE(split.is_aligned(128ul)); for (auto p : split.all_parts()) { LOG_DEBUG(p.i, ' ', p.begin(), ' ', p.end()); - EXPECT_EQ(0, p.begin()); + EXPECT_EQ(0ul, p.begin()); EXPECT_EQ((*(it+1) - *it), p.end()); it++; } @@ -938,14 +941,14 @@ std::unique_ptr random_block(uint64_t size) { while (size--) { *(p++) = rand() % UCHAR_MAX; } - return std::move(buff); + return buff; } void random_content_rw_test(uint64_t test_block_size, uint64_t test_block_num, fs::IFile* file) { vector> rand_data; file->lseek(0, SEEK_SET); - for (auto i = 0; i< test_block_num; i++) { - rand_data.emplace_back(std::move(random_block(test_block_size))); + for (uint64_t i = 0; i < test_block_num; i++) { + rand_data.emplace_back(random_block(test_block_size)); char * buff = rand_data.back().get(); file->write(buff, test_block_size); } @@ -961,14 +964,14 @@ void random_content_rw_test(uint64_t test_block_size, uint64_t test_block_num, f void sequence_content_rw_test (uint64_t test_block_size, uint64_t test_block_num, const char* test_seq, fs::IFile* file) { char data[test_block_size]; file->lseek(0, SEEK_SET); - for (auto i = 0; i< test_block_num; i++) { + for (auto i: xrange(test_block_num)) { memset(data, test_seq[i], test_block_size); file->write(data, test_block_size); } file->fdatasync(); file->lseek(0, SEEK_SET); char buff[test_block_size]; - for (auto i = 0; i< test_block_num; i++) { + for (uint64_t i = 0; i< test_block_num; i++) { file->read(buff, test_block_size); memset(data, *(test_seq++), test_block_size); EXPECT_EQ(0, memcmp(data, buff, test_block_size)); @@ -978,7 +981,7 @@ void sequence_content_rw_test (uint64_t test_block_size, uint64_t test_block_num void xfile_fstat_test(uint64_t fsize, fs::IFile* file) { struct stat st; file->fstat(&st); - EXPECT_EQ(fsize, st.st_size); + EXPECT_EQ(fsize, (uint64_t)st.st_size); } void xfile_not_impl_test(fs::IFile* file) { @@ -1002,7 +1005,7 @@ TEST(XFile, fixed_size_linear_file_basic) { const uint64_t test_block_size = 3986; std::unique_ptr fs(new_localfs_adaptor("/tmp/")); IFile* lf[test_file_num]; - for (int i=0;iopen(("test_fixed_size_linear_file_" + std::to_string(i)).c_str(), O_RDWR | O_CREAT, 0666)); } std::unique_ptr xf(new_fixed_size_linear_file(test_block_size, lf, test_file_num, true)); @@ -1037,7 +1040,7 @@ TEST(XFile, linear_file_basic) { for (auto x : test_file_size) file_max_limit += x; std::unique_ptr fs(new_localfs_adaptor("/tmp/")); IFile* lf[test_file_num]; - for (int i=0;iopen(("test_linear_file_" + std::to_string(i)).c_str(), O_RDWR | O_CREAT, 0666)); lf[i]->ftruncate(test_file_size[i]); } @@ -1073,7 +1076,7 @@ TEST(XFile, stripe_file_basic) { const uint64_t test_block_size = 128; std::unique_ptr fs(new_localfs_adaptor("/tmp/")); IFile* lf[test_file_num]; - for (int i=0;iopen(("test_stripe_file_" + std::to_string(i)).c_str(), O_RDWR | O_CREAT, 0666)); lf[i]->ftruncate(test_file_size); } @@ -1308,6 +1311,7 @@ TEST(range_split_vi, special_case) { &kp[0], kp.size() ); + (void)iovsplit; cnt++; ASSERT_LT(1, cnt); } @@ -1340,6 +1344,7 @@ TEST(Walker, basic) { DEFER(delete srcFs); for (auto file : enumerable(Walker(srcFs, ""))) { + (void)file; EXPECT_FALSE(true); } diff --git a/fs/test/test_exportfs.cpp b/fs/test/test_exportfs.cpp index d7ebee41..1d57fd38 100644 --- a/fs/test/test_exportfs.cpp +++ b/fs/test/test_exportfs.cpp @@ -20,7 +20,6 @@ limitations under the License. #include "../exportfs.cpp" #include -#include #include #include #include @@ -30,6 +29,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" using namespace photon; using namespace photon::fs; diff --git a/fs/test/test_filecopy.cpp b/fs/test/test_filecopy.cpp index e8b1d509..bff6a86b 100644 --- a/fs/test/test_filecopy.cpp +++ b/fs/test/test_filecopy.cpp @@ -14,18 +14,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include #include #include #include #include - #include #include #include #include #include #include +#include "../../test/gtest.h" using namespace photon; diff --git a/fs/test/test_throttledfile.cpp b/fs/test/test_throttledfile.cpp index cd870a2c..28141c95 100644 --- a/fs/test/test_throttledfile.cpp +++ b/fs/test/test_throttledfile.cpp @@ -26,16 +26,12 @@ limitations under the License. #include #include #include -#include -#include - - #include #include #include #include #include - +#include "../../test/gtest.h" #include "mock.h" using namespace photon; diff --git a/io/test/signalfdboom.cpp b/io/test/signalfdboom.cpp index 7a5a9ce1..59fa4b7a 100644 --- a/io/test/signalfdboom.cpp +++ b/io/test/signalfdboom.cpp @@ -21,8 +21,8 @@ limitations under the License. #undef protected #include -#include #include +#include "../../test/gtest.h" using namespace photon; using namespace std; diff --git a/io/test/signalfdtest.cpp b/io/test/signalfdtest.cpp index 709af8b6..95d907de 100644 --- a/io/test/signalfdtest.cpp +++ b/io/test/signalfdtest.cpp @@ -19,10 +19,9 @@ limitations under the License. #include #include #include - #include -#include #include +#include "../../test/gtest.h" using namespace photon; using namespace std; diff --git a/io/test/test-fork.cpp b/io/test/test-fork.cpp index 3bfca28c..cb945a27 100644 --- a/io/test/test-fork.cpp +++ b/io/test/test-fork.cpp @@ -16,10 +16,8 @@ limitations under the License. #include #include -#include #include #include - #include #include #include @@ -27,6 +25,11 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" + +#if __GNUC__ >= 11 +#pragma GCC diagnostic ignored "-Wmismatched-dealloc" +#endif bool exit_flag = false; bool exit_normal = false; diff --git a/io/test/test-iouring.cpp b/io/test/test-iouring.cpp index 464bb088..511e808f 100644 --- a/io/test/test-iouring.cpp +++ b/io/test/test-iouring.cpp @@ -18,10 +18,7 @@ limitations under the License. #include #include #include - -#include #include - #include #include #include @@ -33,6 +30,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" #include "../../test/ci-tools.h" using namespace photon; @@ -56,11 +54,11 @@ static void handle_signal(int sig) { LOG_INFO("try to stop test"); stop_test = true; } - +/* static void ignore_signal(int sig) { LOG_INFO("ignore signal `", sig); } - +*/ static void show_qps_loop() { while (!stop_test) { photon::thread_sleep(FLAGS_show_loop_interval); diff --git a/io/test/test-syncio.cpp b/io/test/test-syncio.cpp index 4e28319f..6b70eb3e 100644 --- a/io/test/test-syncio.cpp +++ b/io/test/test-syncio.cpp @@ -26,7 +26,7 @@ limitations under the License. #include #include #include -#include +#include "../../test/gtest.h" using namespace photon; diff --git a/net/http/client.h b/net/http/client.h index c1a80cb3..1421ea34 100644 --- a/net/http/client.h +++ b/net/http/client.h @@ -64,6 +64,10 @@ class Client : public Object { auto ptr = malloc(sizeof(Operation) + buf_size); return new (ptr) Operation(c, buf_size); } + void destroy() { + this->~Operation(); + free(this); + } void set_enable_proxy(bool enable) { enable_proxy = enable; } int call() { @@ -90,10 +94,12 @@ class Client : public Object { Operation(uint16_t buf_size) : req(_buf, buf_size) {} }; + // use delete_operation() or destroy(), instead of delete Operation* new_operation(Verb v, std::string_view url, uint16_t buf_size = UINT16_MAX) { return Operation::create(this, v, url, buf_size); } + // use delete_operation() or destroy(), instead of delete Operation* new_operation(uint16_t buf_size = UINT16_MAX) { return Operation::create(this, buf_size); } diff --git a/net/http/test/client_function_test.cpp b/net/http/test/client_function_test.cpp index 3ca4162d..d3fd6a57 100644 --- a/net/http/test/client_function_test.cpp +++ b/net/http/test/client_function_test.cpp @@ -16,7 +16,6 @@ limitations under the License. #include #include -#include #include #include @@ -39,6 +38,7 @@ limitations under the License. #include #include #include +#include "../../../test/gtest.h" #include "to_url.h" using namespace photon::net; @@ -98,7 +98,7 @@ TEST(http_client, get) { auto client = new_http_client(); DEFER(delete client); auto op2 = client->new_operation(Verb::GET, target); - DEFER(delete op2); + DEFER(op2->destroy()); op2->req.headers.content_length(0); int ret = client->call(op2); GTEST_ASSERT_EQ(0, ret); @@ -112,7 +112,7 @@ TEST(http_client, get) { EXPECT_EQ(0, strcmp(resp_body_buf, socket_buf)); auto op3 = client->new_operation(Verb::GET, target); - DEFER(delete op3); + DEFER(op3->destroy()); op3->req.headers.content_length(0); op3->req.headers.range(10, 19); client->call(op3); @@ -123,7 +123,7 @@ TEST(http_client, get) { LOG_DEBUG(resp_body_buf_range); auto op4 = client->new_operation(Verb::GET, target); - DEFER(delete op4); + DEFER(op4->destroy()); op4->req.headers.content_length(0); op4->call(); EXPECT_EQ(sizeof(socket_buf), op4->resp.resource_size()); @@ -140,7 +140,7 @@ TEST(http_client, get) { static const char target_tb[] = "http://www.taobao.com?x"; auto op5 = client->new_operation(Verb::GET, target_tb); - DEFER(delete op5); + DEFER(op5->destroy()); op5->req.headers.content_length(0); op5->call(); EXPECT_EQ(op5->resp.status_code(), 200); @@ -193,7 +193,7 @@ TEST(http_client, post) { // body stream test auto op1 = client->new_operation(Verb::POST, target); - DEFER(delete op1); + DEFER(op1->destroy()); struct stat st; EXPECT_EQ(0, file->fstat(&st)); op1->req.headers.content_length(st.st_size); @@ -207,7 +207,7 @@ TEST(http_client, post) { // body writer test auto op2 = client->new_operation(Verb::POST, target); - DEFER(delete op2); + DEFER(op2->destroy()); op2->req.headers.content_length(st.st_size); auto writer = [&](Request *req)-> ssize_t { file->lseek(0, SEEK_SET); @@ -284,6 +284,7 @@ int chunked_handler_complict(void*, ISocketStream* sock) { std::string std_data; const size_t std_data_size = 64 * 1024; +/* static int digtal_num(int n) { int ret = 0; do { @@ -292,6 +293,7 @@ static int digtal_num(int n) { } while (n); return ret; } +*/ void chunked_send(int offset, int size, ISocketStream* sock) { char s[10]; auto len = snprintf(s, sizeof(s), "%x\r\n", size); @@ -311,7 +313,7 @@ int chunked_handler_pt(void*, ISocketStream* sock) { EXPECT_GT(len, 0); auto ret = sock->write(header_data, sizeof(header_data) - 1); EXPECT_EQ(sizeof(header_data) - 1, ret); - auto offset = 0; + size_t offset = 0; rec.clear(); while (offset < std_data_size) { auto remain = std_data_size - offset; @@ -346,7 +348,7 @@ TEST(http_client, chunked) { DEFER(delete client); auto url = to_url(server, "/"); auto op = client->new_operation(Verb::GET, url); - DEFER(delete op); + DEFER(op->destroy()); std::string buf; op->call(); @@ -360,7 +362,7 @@ TEST(http_client, chunked) { server->set_handler({nullptr, &chunked_handler_complict}); auto opc = client->new_operation(Verb::GET, url); - DEFER(delete opc); + DEFER(opc->destroy()); opc->call(); EXPECT_EQ(200, opc->status_code); buf.resize(20000); @@ -380,7 +382,7 @@ TEST(http_client, chunked) { server->set_handler({nullptr, &chunked_handler_pt}); for (auto tmp = 0; tmp < 20; tmp++) { auto op_test = client->new_operation(Verb::GET, url); - DEFER(delete op_test); + DEFER(op_test->destroy()); op_test->call(); EXPECT_EQ(200, op_test->status_code); buf.resize(std_data_size); @@ -457,7 +459,7 @@ TEST(http_client, debug) { auto client = new_http_client(); DEFER(delete client); auto op_test = client->new_operation(Verb::GET, to_url(server, "/")); - DEFER(delete op_test); + DEFER(op_test->destroy()); op_test->call(); EXPECT_EQ(200, op_test->status_code); std::string buf; @@ -466,7 +468,7 @@ TEST(http_client, debug) { ret = op_test->resp.read((void*)buf.data(), std_data_size); EXPECT_EQ(std_data_size, ret); EXPECT_TRUE(buf == std_data); - for (int i = 0; i < buf.size(); i++) { + for (auto i: xrange(buf.size())) { if (buf[i] != std_data[i]) { LOG_ERROR("first occurrence of difference at: ", i); break; @@ -490,7 +492,7 @@ TEST(http_client, server_no_resp) { auto client = new_http_client(); DEFER(delete client); auto op = client->new_operation(Verb::GET, to_url(server, "/wtf")); - DEFER(delete op); + DEFER(op->destroy()); op->req.headers.content_length(0); client->call(op); EXPECT_EQ(-1, op->status_code); @@ -519,7 +521,7 @@ TEST(http_client, partial_body) { auto client = new_http_client(); DEFER(delete client); auto op = client->new_operation(Verb::GET, target_get); - DEFER(delete op); + DEFER(op->destroy()); op->req.headers.content_length(0); client->call(op); EXPECT_EQ(sizeof(socket_buf), op->resp.resource_size()); @@ -538,7 +540,7 @@ TEST(DISABLED_http_client, ipv6) { // make sure runing in a ipv6-ready environm DEFER(delete client); // here is an ipv6-only website auto op = client->new_operation(Verb::GET, "http://test6.ustc.edu.cn"); - DEFER(delete op); + DEFER(op->destroy()); op->call(); EXPECT_EQ(200, op->resp.status_code()); } @@ -616,7 +618,7 @@ TEST(url, path_fix) { // DEFER(delete client); // client->set_proxy("http://localhost:8899/"); // auto op = client->new_operation(Verb::delete_, "https://domain:1234/targetName"); -// DEFER(delete op); +// DEFER(op->destroy()); // LOG_DEBUG(VALUE(op->req.whole())); // op->req.redirect(Verb::GET, "baidu.com", true); // LOG_DEBUG(VALUE(op->req.whole())); diff --git a/net/http/test/client_perf.cpp b/net/http/test/client_perf.cpp index 7b8f7033..7e0ce3d6 100644 --- a/net/http/test/client_perf.cpp +++ b/net/http/test/client_perf.cpp @@ -32,10 +32,10 @@ limitations under the License. using namespace photon; DEFINE_string(ip, "127.0.0.1", "server ip"); -DEFINE_int32(port, 19876, "port"); +DEFINE_uint64(port, 19876, "port"); DEFINE_uint64(count, -1UL, "request count per thread, -1 for endless loop"); -DEFINE_int32(threads, 4, "num threads"); -DEFINE_int32(body_size, 4096, "http body size"); +DEFINE_uint64(threads, 4, "num threads"); +DEFINE_uint64(body_size, 4096, "http body size"); DEFINE_bool(curl, false, "use curl client rather than http client"); class StringStream { @@ -91,7 +91,7 @@ void curl_thread_entry(result* res) { buffer.clean(); client.GET(target.c_str(), &buffer); auto t_end = GetSteadyTimeUs(); - if (buffer.ptr != FLAGS_body_size) { + if (buffer.ptr != (int)FLAGS_body_size) { LOG_ERROR(VALUE(buffer.ptr), VALUE(errno), VALUE(i), VALUE(FLAGS_body_size)); res->failed = true; } @@ -103,7 +103,7 @@ void curl_thread_entry(result* res) { void test_curl(result &res) { res.t_begin = GetSteadyTimeUs(); std::vector jhs; - for (auto i = 0; i < FLAGS_threads; ++i) { + FOR_LOOP(FLAGS_threads) { jhs.emplace_back(photon::thread_enable_join( photon::thread_create11(&curl_thread_entry, &res))); } @@ -126,7 +126,7 @@ void client_thread_entry(result *res, net::http::Client *client, int idx) { res->failed = true; } auto ret = op->resp.read((void*)body_buf.data(), FLAGS_body_size); - if (ret != FLAGS_body_size) { + if (ret != (ssize_t)FLAGS_body_size) { LOG_ERROR(VALUE(ret), VALUE(errno), VALUE(i), VALUE(FLAGS_body_size)); res->failed = true; } @@ -141,7 +141,7 @@ void test_client(result &res) { auto client = net::http::new_http_client(); DEFER(delete client); std::vector jhs; - for (auto i = 0; i < FLAGS_threads; ++i) { + for (auto i: xrange(FLAGS_threads)) { jhs.emplace_back(photon::thread_enable_join( photon::thread_create11(&client_thread_entry, &res, client, i))); } diff --git a/net/http/test/client_tls_test.cpp b/net/http/test/client_tls_test.cpp index 97a1c18f..3d2929c4 100644 --- a/net/http/test/client_tls_test.cpp +++ b/net/http/test/client_tls_test.cpp @@ -15,7 +15,6 @@ limitations under the License. */ #include -#include #include #include @@ -26,6 +25,7 @@ limitations under the License. #include #include #include +#include "../../../test/gtest.h" #include "to_url.h" #include "../../test/cert-key.cpp" @@ -72,7 +72,7 @@ TEST(client_tls, basic) { auto client = net::http::new_http_client(nullptr, ctx); DEFER(delete client); auto op = client->new_operation(net::http::Verb::GET, to_surl(tcpserver, "/test")); - DEFER(delete op); + DEFER(op->destroy()); auto exp_len = 20; op->req.headers.range(0, exp_len - 1); op->call(); @@ -91,7 +91,7 @@ TEST(http_client, DISABLED_SNI) { auto client = photon::net::http::new_http_client(nullptr, tls); DEFER(delete client); auto op = client->new_operation(photon::net::http::Verb::GET, "https://debug.fly.dev"); - DEFER(delete op); + DEFER(op->destroy()); op->retry = 0; int res = op->call(); ASSERT_EQ(0, res); diff --git a/net/http/test/cookie_jar_test.cpp b/net/http/test/cookie_jar_test.cpp index d437d82b..5bbe3380 100644 --- a/net/http/test/cookie_jar_test.cpp +++ b/net/http/test/cookie_jar_test.cpp @@ -21,7 +21,6 @@ limitations under the License. #undef private #include -#include #include #include @@ -34,6 +33,7 @@ limitations under the License. #include #include #include +#include "../../../test/gtest.h" using namespace photon; using namespace photon::net; diff --git a/net/http/test/headers_test.cpp b/net/http/test/headers_test.cpp index f4b4da8a..2fc85953 100644 --- a/net/http/test/headers_test.cpp +++ b/net/http/test/headers_test.cpp @@ -22,7 +22,7 @@ limitations under the License. #undef private #include -#include +#include "../../../test/gtest.h" #include #include @@ -54,9 +54,9 @@ class RequestHeadersStored : public Request char _buffer[BUF_CAPACITY]; }; TEST(headers, req_header) { - char std_req_stream[] = "GET /targetName HTTP/1.1\r\n" - "Host: HostName\r\n" - "Content-Length: 0\r\n\r\n"; + // char std_req_stream[] = "GET /targetName HTTP/1.1\r\n" + // "Host: HostName\r\n" + // "Content-Length: 0\r\n\r\n"; RequestHeadersStored<> req(Verb::GET, "http://HostName:80/targetName"); req.headers.content_length(0); EXPECT_EQ(false, req.headers.empty()); @@ -134,7 +134,7 @@ class test_stream : public net::SocketStreamBase { }; TEST(headers, resp_header) { - char of_buf[128 * 1024 - 1]; + char of_buf[64 * 1024 - 1]; Response of_header(of_buf, sizeof(of_buf)); string of_stream = "HTTP/1.1 123 status_message\r\n"; for (auto i = 0; i < 10; i++) of_stream += "key" + to_string(i) + ": value" + to_string(i) + "\r\n"; diff --git a/net/http/test/server_function_test.cpp b/net/http/test/server_function_test.cpp index ca957735..d9397b3c 100644 --- a/net/http/test/server_function_test.cpp +++ b/net/http/test/server_function_test.cpp @@ -17,7 +17,6 @@ limitations under the License. #include #include -#include #include #include #include @@ -25,7 +24,7 @@ limitations under the License. #include #include #include - +#include "../../../test/gtest.h" #include "../server.h" #include "to_url.h" @@ -64,7 +63,7 @@ TEST(http_server, headers) { auto client = new_http_client(); DEFER(delete client); auto op = client->new_operation(Verb::GET, to_url(tcpserver, "/test")); - DEFER(delete op); + DEFER(op->destroy()); auto exp_len = 20; op->req.headers.range(0, exp_len - 1); op->call(); @@ -105,7 +104,7 @@ TEST(http_server, post) { auto client = new_http_client(); DEFER(delete client); auto op = client->new_operation(Verb::POST, to_url(tcpserver, "/test")); - DEFER(delete op); + DEFER(op->destroy()); op->req.headers.content_length(10); std::string body = "1234567890"; auto writer = [&](Request *req)-> ssize_t { @@ -126,30 +125,33 @@ std::string fs_handler_std_str = "01234567890123456789"; void test_case(Client* client, estring_view url, off_t st, size_t len, size_t exp_content_length, bool invalid = false) { LOG_INFO("test case start"); auto op = client->new_operation(Verb::GET, url); - DEFER(delete op); + DEFER(op->destroy()); op->req.headers.range(st, st + len - 1); auto ret = op->call(); LOG_INFO("call finished"); EXPECT_EQ(0, ret); - if (!invalid) { - if (exp_content_length != fs_handler_std_str.size()) - EXPECT_EQ(206, op->resp.status_code()); - else - EXPECT_EQ(200, op->resp.status_code()); - char buf[4096]; - ret = op->resp.read(buf, 4096); - EXPECT_EQ(exp_content_length, ret); - if (st >= fs_handler_std_str.size()) st = fs_handler_std_str.size() - 1; - if (st + len > fs_handler_std_str.size()) len = fs_handler_std_str.size() - st; - EXPECT_EQ(true, std::string(fs_handler_std_str.data() + st, exp_content_length) == - std::string(buf, exp_content_length)); + if (invalid) return; + + if (exp_content_length != fs_handler_std_str.size()) { + EXPECT_EQ(206, op->resp.status_code()); + } else { + EXPECT_EQ(200, op->resp.status_code()); } + char buf[4096]; + ret = op->resp.read(buf, 4096); + EXPECT_EQ(exp_content_length, ret); + if ((size_t)st >= fs_handler_std_str.size()) len = 0; + else if ((size_t)st + len > fs_handler_std_str.size()) + len = fs_handler_std_str.size() - st; + std::string_view x(fs_handler_std_str.data() + st, len); + std::string_view y(buf, exp_content_length); + EXPECT_EQ(x, y); } void test_head_case(Client* client, estring_view url, off_t st, size_t len, size_t exp_content_length) { LOG_INFO("test HEAD case start"); auto op = client->new_operation(Verb::HEAD, url); - DEFER(delete op); + DEFER(op->destroy()); op->req.headers.range(st, st + len - 1); op->req.headers.content_length(fs_handler_std_str.size()); auto ret = op->call(); @@ -160,8 +162,10 @@ void test_head_case(Client* client, estring_view url, off_t st, size_t len, size else EXPECT_EQ(200, op->resp.status_code()); char range[64]; - auto range_len = snprintf(range, 64, "bytes %lu-%lu/%lu", st, st + len - 1, fs_handler_std_str.size()); - std::string rangestr(op->resp.headers["Content-Range"]); + auto range_len = snprintf(range, sizeof(range), "bytes %lu-%lu/%lu", + (unsigned long)st, (unsigned long)(st + len - 1), + (unsigned long)fs_handler_std_str.size()); + auto rangestr = op->resp.headers["Content-Range"]; EXPECT_EQ(0, memcmp(range, rangestr.data(), range_len)); } @@ -218,7 +222,7 @@ int chunked_handler_pt(void*, net::ISocketStream* sock) { LOG_INFO("req:", std::string_view(recv, len)); auto ret = sock->write(header_data, sizeof(header_data) - 1); EXPECT_EQ(sizeof(header_data) - 1, ret); - auto offset = 0; + size_t offset = 0; rec.clear(); while (offset < std_data_size) { auto remain = std_data_size - offset; @@ -295,7 +299,7 @@ TEST(http_server, proxy_handler_get) { tcpserver->start_loop(); //---------------------------------------------------- auto op = client->new_operation(Verb::GET, to_url(tcpserver, "/filename")); - DEFER(delete op); + DEFER(op->destroy()); ret = op->call(); EXPECT_EQ(0, ret); std::string data_buf; @@ -337,7 +341,7 @@ TEST(http_server, proxy_handler_post) { tcpserver->start_loop(); //---------------------------------------------------- auto op = client->new_operation(Verb::POST, to_url(tcpserver, "/filename")); - DEFER(delete op); + DEFER(op->destroy()); std::string body = "1234567890"; op->req.headers.content_length(10); auto writer = [&](Request *req)-> ssize_t { @@ -398,7 +402,7 @@ TEST(http_server, proxy_handler_post_forward) { DEFER(delete client1); client1->set_proxy(to_url(tcpserver, "/")); auto op = client1->new_operation(Verb::POST, to_url(source_server, "/filename")); - DEFER(delete op); + DEFER(op->destroy()); std::string body = "1234567890"; op->req.headers.content_length(10); auto writer = [&](Request *req)-> ssize_t { @@ -437,7 +441,7 @@ TEST(http_server, proxy_handler_failure) { //---------------------------------------------------- auto url = to_url(tcpserver, "/filename"); auto op = client->new_operation(Verb::GET, url); - DEFER(delete op); + DEFER(op->destroy()); auto ret = op->call(); EXPECT_EQ(0, ret); EXPECT_EQ(502, op->resp.status_code()); @@ -488,7 +492,7 @@ TEST(http_server, mux_handler) { //---------------------------------------------------- //--------------test static service-------------------- auto op_static = client->new_operation(Verb::GET, to_url(tcpserver, "/static_service/fs_handler_test")); - DEFER(delete op_static); + DEFER(op_static->destroy()); ret = op_static->call(); EXPECT_EQ(0, ret); EXPECT_EQ(200, op_static->resp.status_code()); @@ -499,7 +503,7 @@ TEST(http_server, mux_handler) { EXPECT_EQ(true, data_buf == fs_handler_std_str); //--------------test proxy service--------------------- auto op_proxy = client->new_operation(Verb::GET, to_url(tcpserver, "/proxy/filename_not_important")); - DEFER(delete op_proxy); + DEFER(op_proxy->destroy()); ret = op_proxy->call(); EXPECT_EQ(0, ret); EXPECT_EQ(200, op_proxy->resp.status_code()); @@ -510,7 +514,7 @@ TEST(http_server, mux_handler) { EXPECT_EQ(true, data_buf == std_data); //-------------test mux default handler--------------- auto op_default = client->new_operation(Verb::GET, to_url(tcpserver, "/not_recorded/should_be_404")); - DEFER(delete op_default); + DEFER(op_default->destroy()); ret = op_default->call(); EXPECT_EQ(0, ret); EXPECT_EQ(404, op_default->resp.status_code()); diff --git a/net/security-context/test/test-sasl.cpp b/net/security-context/test/test-sasl.cpp index f483bbc5..13e47d6a 100644 --- a/net/security-context/test/test-sasl.cpp +++ b/net/security-context/test/test-sasl.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../../test/gtest.h" #include #include diff --git a/net/security-context/test/test.cpp b/net/security-context/test/test.cpp index 5d403af3..11de89e6 100644 --- a/net/security-context/test/test.cpp +++ b/net/security-context/test/test.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../../test/gtest.h" #include #include diff --git a/net/test/test-ipv6.cpp b/net/test/test-ipv6.cpp index 3bb3385d..b05d2f47 100644 --- a/net/test/test-ipv6.cpp +++ b/net/test/test-ipv6.cpp @@ -1,11 +1,10 @@ #include -#include - #include #include #include #include #include +#include "../../test/gtest.h" TEST(ipv6, endpoint) { auto c = photon::net::EndPoint("127.0.0.1"); @@ -88,7 +87,7 @@ TEST(ipv6, dns_lookup) { class DualStackTest : public ::testing::Test { public: void run() { - auto server = photon::net::new_tcp_socket_server_ipv6(); + auto server = photon::net::new_tcp_socket_server(); ASSERT_NE(nullptr, server); DEFER(delete server); diff --git a/net/test/test-udp.cpp b/net/test/test-udp.cpp index 82996219..bfc75de2 100644 --- a/net/test/test-udp.cpp +++ b/net/test/test-udp.cpp @@ -15,19 +15,19 @@ limitations under the License. */ #include -#include #include #include #include #include #include #include "cert-key.cpp" +#include "../../test/gtest.h" using namespace photon; using namespace net; constexpr char uds_path[] = "udsudptest.sock"; -constexpr size_t uds_len = sizeof(uds_path) - 1; +// constexpr size_t uds_len = sizeof(uds_path) - 1; TEST(UDP, basic) { auto s1 = new_udp_socket(); @@ -74,21 +74,21 @@ TEST(UDP, uds) { EXPECT_EQ(0, s1->bind(uds_path)); char path[1024] = {}; - socklen_t pathlen = s1->getsockname(path, 1024); + int ret = s1->getsockname(path, sizeof(path)); + EXPECT_EQ(ret, 0); LOG_INFO("Bind at ", path); EXPECT_EQ(0, s2->connect(path)); ASSERT_EQ(6, s2->send("Hello", 6)); char buf[4096]; - ASSERT_EQ(6, s1->recv(buf, 4096)); + ASSERT_EQ(6, s1->recv(buf, sizeof(buf))); EXPECT_STREQ("Hello", buf); auto s3 = new_uds_datagram_socket(); DEFER(delete s3); ASSERT_EQ(6, s3->sendto("Hello", 6, uds_path)); - pathlen = 1024; memset(path, 0, sizeof(path)); - ASSERT_EQ(6, s1->recvfrom(buf, 4096, path, sizeof(path))); + ASSERT_EQ(6, s1->recvfrom(buf, sizeof(buf), path, sizeof(path))); LOG_INFO(VALUE(path)); EXPECT_STREQ("Hello", buf); } @@ -111,7 +111,8 @@ TEST(UDP, uds_huge_datag) { EXPECT_EQ(0, s1->bind(uds_path)); char path[1024] = {}; - socklen_t pathlen = s1->getsockname(path, 1024); + int ret = s1->getsockname(path, 1024); + EXPECT_EQ(ret, 0); LOG_INFO("Bind at ", path); constexpr static size_t msgsize = 63 * 1024; // more data returned failure diff --git a/net/test/test.cpp b/net/test/test.cpp index 0068410c..e1a871ba 100644 --- a/net/test/test.cpp +++ b/net/test/test.cpp @@ -15,10 +15,9 @@ limitations under the License. */ #include -#include #include - #include +#include #include #include #include @@ -26,6 +25,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" #define protected public #define private public @@ -121,7 +121,7 @@ void tcp_client() { LOG_DEBUG(VALUE(sock), VALUE(errno)); EndPoint epget = sock->getpeername(); LOG_DEBUG("Connected `", epget); - EXPECT_TRUE(ep.port = epget.port); + EXPECT_EQ(ep.port, epget.port); char buff[] = "Hello"; char recv[256]; sock->send("Hello", 5); @@ -170,8 +170,8 @@ class LogOutputTest final : public ILogOutput { } int get_log_file_fd() override { return -1; } - uint64_t set_throttle(uint64_t) { return -1; } - uint64_t get_throttle() { return -1; } + uint64_t set_throttle(uint64_t) override { return -1; } + uint64_t get_throttle() override { return -1; } void destruct() override {} } log_output_test; @@ -576,7 +576,7 @@ bool server_down = false; photon::thread* server_thread = nullptr; void* serve_connection(void* arg) { - auto fd = (int&)arg; + auto fd = (int)(uint64_t)arg; while (true) { char buf[1024]; auto ret = net::read(fd, buf, sizeof(buf)); @@ -727,9 +727,9 @@ TEST(utils, resolver) { DEFER(delete resolver); net::IPAddr localhost("127.0.0.1"); net::IPAddr addr = resolver->resolve("localhost"); - if (addr.is_ipv4()) EXPECT_EQ(localhost.to_nl(), addr.to_nl()); + if (addr.is_ipv4()) { EXPECT_EQ(localhost.to_nl(), addr.to_nl()); } auto func = [&](net::IPAddr addr_){ - if (addr_.is_ipv4()) EXPECT_EQ(localhost.to_nl(), addr_.to_nl()); + if (addr_.is_ipv4()) { EXPECT_EQ(localhost.to_nl(), addr_.to_nl()); } }; resolver->resolve("localhost", func); resolver->discard_cache("non-exist-host.com"); diff --git a/net/test/test_base64.cpp b/net/test/test_base64.cpp index d61bd15b..5605e04d 100644 --- a/net/test/test_base64.cpp +++ b/net/test/test_base64.cpp @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ #include -#include #include #include +#include "../../test/gtest.h" struct Base64TestData { std::string str; diff --git a/net/test/test_curl.cpp b/net/test/test_curl.cpp index 74854d1d..2de12b7b 100644 --- a/net/test/test_curl.cpp +++ b/net/test/test_curl.cpp @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include #include #include #include @@ -23,6 +22,7 @@ limitations under the License. #include #include #include +#include "../../test/gtest.h" using namespace photon; diff --git a/net/test/test_sockpool.cpp b/net/test/test_sockpool.cpp index 08f72bdf..312ec7f0 100644 --- a/net/test/test_sockpool.cpp +++ b/net/test/test_sockpool.cpp @@ -1,10 +1,10 @@ -#include #include #include #include #include #include #include +#include "../../test/gtest.h" void task(photon::net::ISocketClient* client, photon::net::EndPoint ep) { photon::net::ISocketStream* st = nullptr; diff --git a/rpc/test/test-ooo.cpp b/rpc/test/test-ooo.cpp index 734d35ee..b6c2d38f 100644 --- a/rpc/test/test-ooo.cpp +++ b/rpc/test/test-ooo.cpp @@ -19,11 +19,9 @@ limitations under the License. #include #include #include - -#include +#include "../../test/gtest.h" #include #include - #define private public #define protected public #include "../out-of-order-execution.cpp" @@ -58,7 +56,7 @@ int do_issue(void*, OutOfOrderContext* args) auto tag = args->tag; LOG_DEBUG(VALUE(tag)); aop.push_back(args->tag); - random_shuffle(aop.begin(), aop.end()); + shuffle(aop.begin(), aop.end()); return 0; } @@ -121,7 +119,7 @@ int heavy_complete(void*, OutOfOrderContext* args) { int process_thread() { while (issue_list.size()) { thread_yield(); - random_shuffle(issue_list.begin(), issue_list.end()); + shuffle(issue_list.begin(), issue_list.end()); processing_queue.push(issue_list.back()); issue_list.pop_back(); } @@ -207,7 +205,7 @@ inline int error_complete(void*, OutOfOrderContext* args) { inline int error_process() { while (issue_list.size()) { thread_yield_to(nullptr); - random_shuffle(issue_list.begin(), issue_list.end()); + shuffle(issue_list.begin(), issue_list.end()); auto val = issue_list.back(); if (rand()%2) val = UINT64_MAX; diff --git a/rpc/test/test-rpc-message.cpp b/rpc/test/test-rpc-message.cpp index ef188830..1a632d04 100644 --- a/rpc/test/test-rpc-message.cpp +++ b/rpc/test/test-rpc-message.cpp @@ -15,13 +15,13 @@ limitations under the License. */ #include -#include #include #include #include #include #include #include +#include "../../test/gtest.h" struct map_value : photon::rpc::Message { @@ -110,17 +110,17 @@ class TestRPCServer { assert(req->code == 999); for (size_t i = 0; i < req->buf.size(); ++i) { char* c = (char*) req->buf.addr() + i; - assert(*c == 'x'); + EXPECT_EQ(*c, 'x'); } auto iter = req->map.find("2"); - assert(iter != req->map.end()); + EXPECT_NE(iter, req->map.end()); auto k = iter->first; - assert(k == "2"); + EXPECT_EQ(k, "2"); auto v = iter->second; - assert(v.a == 2); - assert(v.b == "val-2"); - assert(v.c == '2'); + EXPECT_EQ(v.a, 2); + EXPECT_EQ(v.b, "val-2"); + EXPECT_EQ(v.c, '2'); iter = req->map.find("4"); if (iter != req->map.end()) { diff --git a/rpc/test/test.cpp b/rpc/test/test.cpp index e7141350..a7e55514 100644 --- a/rpc/test/test.cpp +++ b/rpc/test/test.cpp @@ -17,13 +17,13 @@ limitations under the License. #include "../../rpc/rpc.cpp" #include #include -#include #include #include #include #include #include #include +#include "../../test/gtest.h" #include "../../test/ci-tools.h" using namespace std; diff --git a/test/gtest.h b/test/gtest.h new file mode 100644 index 00000000..a562f174 --- /dev/null +++ b/test/gtest.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +// #pragma GCC diagnostic push +// #ifdef __clang__ +#pragma GCC diagnostic ignored "-Wsign-compare" +#pragma GCC diagnostic ignored "-Wunused-result" + +// #endif +#include +#include +// #pragma GCC diagnostic pop + +template< class RandomIt> inline +void shuffle( RandomIt first, RandomIt last) { + std::random_device rd; + std::mt19937 g(rd()); + return std::shuffle(first, last, g); +} + diff --git a/thread/test/perf_usleepdefer_semaphore.cpp b/thread/test/perf_usleepdefer_semaphore.cpp index 8302345a..ef33b245 100644 --- a/thread/test/perf_usleepdefer_semaphore.cpp +++ b/thread/test/perf_usleepdefer_semaphore.cpp @@ -17,7 +17,7 @@ limitations under the License. #include "../thread.h" #include "../thread11.h" #include -#include +#include "../../test/gtest.h" #include #include #include diff --git a/thread/test/perf_workpool.cpp b/thread/test/perf_workpool.cpp index ba1c823a..0425a810 100644 --- a/thread/test/perf_workpool.cpp +++ b/thread/test/perf_workpool.cpp @@ -35,7 +35,7 @@ std::atomic sum; void* task(void* arg) { photon::semaphore sem(0); - for (auto i = 0; i < fires; i++) { + FOR_LOOP(fires) { auto start = std::chrono::steady_clock::now(); pool->async_call(new auto ([&, start]{ auto end = std::chrono::steady_clock::now(); @@ -49,7 +49,7 @@ void* task(void* arg) { } void* task_sync(void* arg) { - for (auto i = 0; i < fires; i++) { + FOR_LOOP(fires) { auto start = std::chrono::steady_clock::now(); pool->call([&, start]{ auto end = std::chrono::steady_clock::now(); @@ -66,7 +66,7 @@ int main() { DEFER(delete pool); std::vector jhs; auto start = photon::now; - for (uint64_t i = 0; i < concurrent; i++) { + for (auto i: xrange(concurrent)) { jhs.emplace_back( photon::thread_enable_join(photon::thread_create(task, (void*)i))); } diff --git a/thread/test/test-lib-data.cpp b/thread/test/test-lib-data.cpp index ffb85dbb..cf9f8e8c 100644 --- a/thread/test/test-lib-data.cpp +++ b/thread/test/test-lib-data.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include #include diff --git a/thread/test/test-multi-vcpu-locking.cpp b/thread/test/test-multi-vcpu-locking.cpp index aefafe0c..3f683bd0 100644 --- a/thread/test/test-multi-vcpu-locking.cpp +++ b/thread/test/test-multi-vcpu-locking.cpp @@ -17,7 +17,7 @@ limitations under the License. #include #include #include -#include +#include "../../test/gtest.h" #include #include #include diff --git a/thread/test/test-pooled-stack-allocator.cpp b/thread/test/test-pooled-stack-allocator.cpp index c99264e1..3324662e 100644 --- a/thread/test/test-pooled-stack-allocator.cpp +++ b/thread/test/test-pooled-stack-allocator.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include #include @@ -30,7 +30,7 @@ void do_test(int pool_mode, const PhotonOptions& options) { WorkPool pool(4, INIT_EVENT_DEFAULT, INIT_IO_NONE, pool_mode); semaphore sem(0); auto start = now; - for (int i = 0; i < N; i++) { + for (uint64_t i = 0; i < N; i++) { pool.async_call(new auto([&] { sem.signal(1); })); } sem.wait(N); diff --git a/thread/test/test-specific-key.cpp b/thread/test/test-specific-key.cpp index b67ba9ad..9348e0c9 100644 --- a/thread/test/test-specific-key.cpp +++ b/thread/test/test-specific-key.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include @@ -115,9 +115,9 @@ int main(int argc, char** arg) { static Value v; photon::thread_key_t key; int ret_key = photon::thread_key_create(&key, &Value::key_dtor); - assert(ret_key == 0); + EXPECT_EQ(ret_key, 0); ret_key = photon::thread_setspecific(key, &v); - assert(ret_key == 0); + EXPECT_EQ(ret_key, 0); return ret; } \ No newline at end of file diff --git a/thread/test/test-std-compat.cpp b/thread/test/test-std-compat.cpp index 17bf0ca9..2af95407 100644 --- a/thread/test/test-std-compat.cpp +++ b/thread/test/test-std-compat.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include diff --git a/thread/test/test-thread-local.cpp b/thread/test/test-thread-local.cpp index 9820df37..4c60466e 100644 --- a/thread/test/test-thread-local.cpp +++ b/thread/test/test-thread-local.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include diff --git a/thread/test/test-tls-order-native.cpp b/thread/test/test-tls-order-native.cpp index f40aed81..b5d8ed19 100644 --- a/thread/test/test-tls-order-native.cpp +++ b/thread/test/test-tls-order-native.cpp @@ -15,8 +15,8 @@ limitations under the License. */ #include -#include #include +#include "../../test/gtest.h" struct Value { explicit Value(int val) : m_val(val) { @@ -46,8 +46,8 @@ struct GlobalEnv { ~GlobalEnv() { printf("Destruct GlobalEnv\n"); - assert(get_v1().m_val == -1); - assert(get_v4().m_val == 4); + EXPECT_EQ(get_v1().m_val, -1); + EXPECT_EQ(get_v4().m_val, 4); } static thread_local Value v3; }; diff --git a/thread/test/test-tls-order-photon.cpp b/thread/test/test-tls-order-photon.cpp index 5d32c0ed..590f3c85 100644 --- a/thread/test/test-tls-order-photon.cpp +++ b/thread/test/test-tls-order-photon.cpp @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include +#include "../../test/gtest.h" #include #include diff --git a/thread/test/test.cpp b/thread/test/test.cpp index 6d92590f..fe40f110 100644 --- a/thread/test/test.cpp +++ b/thread/test/test.cpp @@ -23,7 +23,7 @@ limitations under the License. #include #include #include -#include +#include "../../test/gtest.h" #define private public @@ -116,7 +116,7 @@ void print_heap(SleepQueue& sleepq) LOG_INFO(" "); int i = 0, k = 1; for (auto it : sleepq.q) { - printf("%lu(%d)", it->ts_wakeup, it->idx); + printf("%lu(%d)", (unsigned long)it->ts_wakeup, it->idx); i++; if (i == k) { printf("\n"); @@ -143,7 +143,7 @@ void sleepq_perf(SleepQueue& sleepq, const vector& items) check(sleepq); auto pops = items; - random_shuffle(pops.begin(), pops.end()); + shuffle(pops.begin(), pops.end()); pops.resize(pops.size()/2); { update_now(); @@ -1116,7 +1116,7 @@ void* test_smp_cvar_recver(void* args_) thread_yield(); SCOPED_LOCK(args->mutex); int ret = args->cvar.wait(args->mutex); - assert(ret == 0); + EXPECT_EQ(ret, 0); args->recvd++; } if (args->senders == 0) break; @@ -1348,7 +1348,7 @@ TEST(workpool, async_work_lambda) { for (int i = 0; i < 4; i++) { CopyMoveRecord *r = new CopyMoveRecord(); pool->async_call( - new auto ([i, r]() { + new auto ([r]() { LOG_INFO("START ", VALUE(__cplusplus), VALUE(r->copy), VALUE(r->move)); EXPECT_EQ(0, r->copy); @@ -1374,7 +1374,7 @@ TEST(workpool, async_work_lambda_threadcreate) { for (int i = 0; i < 4; i++) { CopyMoveRecord *r = new CopyMoveRecord(); pool->async_call( - new auto ([&sem, i, r]() { + new auto ([&sem, r]() { LOG_INFO("START ", VALUE(__cplusplus), VALUE(r->copy), VALUE(r->move)); EXPECT_EQ(0, r->copy); @@ -1403,7 +1403,7 @@ TEST(workpool, async_work_lambda_threadpool) { for (int i = 0; i < 4; i++) { CopyMoveRecord *r = new CopyMoveRecord(); pool->async_call( - new auto ([&sem, i, r]() { + new auto ([&sem, r]() { LOG_INFO("START ", VALUE(__cplusplus), VALUE(r->copy), VALUE(r->move)); EXPECT_EQ(0, r->copy); @@ -1441,7 +1441,7 @@ TEST(workpool, async_work_lambda_threadpool_append) { for (int i = 0; i < 4; i++) { CopyMoveRecord *r = new CopyMoveRecord(); pool->async_call( - new auto ([&sem, i, r]() { + new auto ([&sem, r]() { LOG_INFO("START ", VALUE(__cplusplus), VALUE(r->copy), VALUE(r->move)); EXPECT_EQ(0, r->copy);