/*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <glog/logging.h>
#include <gtest/gtest.h>
+using namespace folly;
using namespace folly::wangle;
using namespace std::chrono;
};
CPUThreadPoolExecutor pool(0, 2);
for (int i = 0; i < 50; i++) {
- pool.add(lopri, 0);
+ pool.addWithPriority(lopri, Executor::LO_PRI);
}
for (int i = 0; i < 50; i++) {
- pool.add(hipri, 1);
+ pool.addWithPriority(hipri, Executor::HI_PRI);
}
pool.setNumThreads(1);
pool.join();
class TestObserver : public ThreadPoolExecutor::Observer {
public:
- void threadStarted(ThreadPoolExecutor::ThreadHandle*) {
+ void threadStarted(ThreadPoolExecutor::ThreadHandle*) override { threads_++; }
+ void threadStopped(ThreadPoolExecutor::ThreadHandle*) override { threads_--; }
+ void threadPreviouslyStarted(ThreadPoolExecutor::ThreadHandle*) override {
threads_++;
}
- void threadStopped(ThreadPoolExecutor::ThreadHandle*) {
- threads_--;
- }
- void threadPreviouslyStarted(ThreadPoolExecutor::ThreadHandle*) {
- threads_++;
- }
- void threadNotYetStopped(ThreadPoolExecutor::ThreadHandle*) {
+ void threadNotYetStopped(ThreadPoolExecutor::ThreadHandle*) override {
threads_--;
}
void checkCalls() {
ASSERT_EQ(threads_, 0);
}
private:
- int threads_{0};
+ std::atomic<int> threads_{0};
};
TEST(ThreadPoolExecutorTest, IOObserver) {
observer->checkCalls();
}
+
+TEST(ThreadPoolExecutorTest, AddWithPriority) {
+ std::atomic_int c{0};
+ auto f = [&]{ c++; };
+
+ // IO exe doesn't support priorities
+ IOThreadPoolExecutor ioExe(10);
+ EXPECT_THROW(ioExe.addWithPriority(f, 0), std::runtime_error);
+
+ CPUThreadPoolExecutor cpuExe(10, 3);
+ cpuExe.addWithPriority(f, -1);
+ cpuExe.addWithPriority(f, 0);
+ cpuExe.addWithPriority(f, 1);
+ cpuExe.addWithPriority(f, -2); // will add at the lowest priority
+ cpuExe.addWithPriority(f, 2); // will add at the highest priority
+ cpuExe.addWithPriority(f, Executor::LO_PRI);
+ cpuExe.addWithPriority(f, Executor::HI_PRI);
+ cpuExe.join();
+
+ EXPECT_EQ(7, c);
+}