Skip to content

Commit 25f1d7c

Browse files
committed
fix unit test
1 parent 38d68c9 commit 25f1d7c

File tree

14 files changed

+229
-204
lines changed

14 files changed

+229
-204
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"platform": "iOS",
2+
"platform": "PC",
33
"build_type": "Debug",
44
"disable_http": false,
55
"disable_test": false
Binary file not shown.

src/base/RingBuffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ template<typename T>
2222
class RingBufferImpl: public NonCopyable {
2323
public:
2424
[[nodiscard]] uint32_t capacity() const noexcept {
25-
return capacity;
25+
return capacity_;
2626
}
2727

2828
protected:

src/base/ThreadPool.hpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66
#include <condition_variable>
77
#include <vector>
88
#include <thread>
9-
#include <type_traits>
109
#include <queue>
1110
#include <functional>
1211
#include <mutex>
13-
#include <algorithm>
14-
#include <memory>
15-
#include <future>
1612
#include <expected>
13+
#include <future>
1714
#include "Log.hpp"
1815
#include "NonCopyable.h"
1916

2017
namespace slark {
2118

2219
struct ThreadPoolConfig {
2320
uint32_t threadCount = 4;
24-
uint32_t maxQueueSize = 10000;
25-
std::chrono::milliseconds timeout{100};
2621
};
2722

2823
class ThreadPool: public NonCopyable {
@@ -45,16 +40,21 @@ class ThreadPool: public NonCopyable {
4540
template<class F, typename... Args>
4641
auto submit(F&& f, Args&&... args) -> std::expected<std::future<std::invoke_result_t<F, Args...>>, bool>;
4742

43+
size_t getTaskCount() const noexcept {
44+
std::lock_guard<std::mutex> lock(mutex_);
45+
return tasks_.size();
46+
}
47+
4848
void waitForAll() noexcept {
4949
std::unique_lock<std::mutex> lock(mutex_);
50-
notFull_.wait(lock, [this] {
50+
allTasksDone_.wait(lock, [this] {
5151
return tasks_.empty() && activeThreads_ == 0;
5252
});
5353
}
54-
55-
size_t getTaskCount() const noexcept {
54+
55+
void clear() noexcept {
5656
std::lock_guard<std::mutex> lock(mutex_);
57-
return tasks_.size();
57+
tasks_.clear();
5858
}
5959

6060
size_t getThreadCount() const noexcept {
@@ -70,11 +70,11 @@ class ThreadPool: public NonCopyable {
7070
void workThread();
7171

7272
std::vector<std::thread> workers_;
73-
std::queue<std::function<void()>> tasks_;
73+
std::deque<std::function<void()>> tasks_;
7474

7575
mutable std::mutex mutex_;
7676
std::condition_variable notEmpty_;
77-
std::condition_variable notFull_;
77+
std::condition_variable allTasksDone_;
7878

7979
std::atomic<bool> shutdown_{false};
8080
std::atomic<size_t> activeThreads_{0};
@@ -106,8 +106,7 @@ inline void ThreadPool::workThread() {
106106
}
107107

108108
task = std::move(tasks_.front());
109-
tasks_.pop();
110-
notFull_.notify_one();
109+
tasks_.pop_front();
111110
}
112111

113112
++activeThreads_;
@@ -117,6 +116,7 @@ inline void ThreadPool::workThread() {
117116
LogE("error in executing task");
118117
}
119118
--activeThreads_;
119+
allTasksDone_.notify_all();
120120
}
121121
}
122122

@@ -133,23 +133,15 @@ auto ThreadPool::submit(F&& f, Args&&... args)
133133
std::future<ReturnType> future = task->get_future();
134134
{
135135
std::unique_lock<std::mutex> lock(mutex_);
136-
137-
if (!notFull_.wait_for(lock, config_.timeout, [this] {
138-
return tasks_.size() < config_.maxQueueSize || shutdown_;
139-
})) {
140-
LogE("The task queue is full");
141-
return std::unexpected(false);
142-
}
143-
144136
if (shutdown_) {
145137
LogE("The thread pool is exiting");
146138
return std::unexpected(false);
147139
}
148-
tasks_.emplace([task]() { (*task)(); });
140+
tasks_.emplace_back([task]() { (*task)(); });
149141
}
150142

151143
notEmpty_.notify_one();
152144
return future;
153145
}
154146

155-
}
147+
}

src/core/public/GLContextManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ IEGLContextRefPtr GLContextManager::createShareContextWithId(const std::string&
4646
}
4747

4848
#if !SLARK_IOS && !SLARK_ANDROID
49-
IEGLContextPtr createGLContext() {
49+
IEGLContextRefPtr createGLContext() noexcept {
5050
return nullptr;
5151
}
5252
#endif

test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
cmake_minimum_required(VERSION 3.20)
22

3-
add_subdirectory(base_test)
3+
add_subdirectory(base)
44
if(NOT DISABLE_HTTP)
5-
add_subdirectory(http_test)
5+
add_subdirectory(http)
66
endif ()
77

88
get_property(LIBRARY_TYPE GLOBAL PROPERTY "LIBRARY_TYPE" )

test/base/BufferTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Copyright (c) 2024 Nevermore All rights reserved.
55
//
66

7-
#include <algorithm>
87
#include <gtest/gtest.h>
98
#include <string_view>
109
#include "Buffer.hpp"

test/base/ClockTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TEST(ClockTest, InitialState) {
1818

1919
TEST(ClockTest, SetTime) {
2020
Clock clock;
21-
clock.setTime(milliseconds(5000));
21+
clock.setTime(Time::TimePoint::fromMilliSeconds(5000ms));
2222
clock.start();
2323
std::this_thread::sleep_for(milliseconds(2000));
2424
EXPECT_GE(clock.time().toMilliSeconds(), milliseconds(7000));
@@ -27,7 +27,7 @@ TEST(ClockTest, SetTime) {
2727

2828
TEST(ClockTest, StartPauseReset) {
2929
Clock clock;
30-
clock.setTime(milliseconds(5000));
30+
clock.setTime(Time::TimePoint::fromMilliSeconds(5000ms));
3131
clock.start();
3232
std::this_thread::sleep_for(milliseconds(2000));//7000
3333
EXPECT_GE(clock.time().toMilliSeconds(), milliseconds(6500));

test/base/DataTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ TEST(Data, append) {
7979
ASSERT_EQ(data.length, str.length());
8080

8181
Data data1("hello");
82-
data1 += {" world!"};
82+
data1.append(" world!");
8383
auto data2 = data1 + Data{"xxxxx"};
8484
ASSERT_EQ(data1.view().view(), std::string_view("hello world!"));
8585
ASSERT_EQ(data2.view().view(), std::string_view("hello world!xxxxx"));

test/base/FileTest.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <utility>
99
#include "IReader.h"
1010
#include "Writer.hpp"
11-
#include "FileUtil.h"
1211
#include "Reader.h"
1312

1413
using namespace slark;
@@ -32,7 +31,7 @@ TEST(Writer, getPath) {
3231

3332
TEST(Reader, getPath) {
3433
Reader reader;
35-
auto task = std::make_unique<ReaderTask>([](DataPacket, IOState) {
34+
auto task = std::make_unique<ReaderTask>([](IReader*, DataPacket, IOState) {
3635

3736
});
3837
task->path = "test1.txt";
@@ -57,7 +56,7 @@ TEST(Reader, open) {
5756
writer.reset();
5857

5958
Reader reader;
60-
auto task = std::make_unique<ReaderTask>([&str](DataPacket data, IOState state) {
59+
auto task = std::make_unique<ReaderTask>([&str](IReader*, DataPacket data, IOState state) {
6160
std::cout << data.data->view().view() << std::endl;
6261
ASSERT_EQ(data.offset, 0);
6362
ASSERT_EQ(state, IOState::EndOfFile);

0 commit comments

Comments
 (0)