Add missing override and remove redundant virtual in folly
[folly.git] / folly / wangle / concurrent / test / ThreadPoolExecutorTest.cpp
index d08d2e501affa816a9b8c13d87ba961074457b1e..59c28c01c42d547e318ba599e41871fdfcb68178 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -310,10 +310,10 @@ TEST(ThreadPoolExecutorTest, PriorityPreemptionTest) {
   };
   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();
@@ -322,16 +322,12 @@ TEST(ThreadPoolExecutorTest, PriorityPreemptionTest) {
 
 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() {
@@ -372,3 +368,24 @@ TEST(ThreadPoolExecutorTest, CPUObserver) {
 
   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);
+}