LCOV - code coverage report
Current view: top level - test/unitTest - scheduler.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 34 34 100.0 %
Date: 2024-12-21 08:56:24 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include <cppunit/TestAssert.h>
      19             : #include <cppunit/TestFixture.h>
      20             : #include <cppunit/extensions/HelperMacros.h>
      21             : 
      22             : #include "test_runner.h"
      23             : 
      24             : #include "scheduled_executor.h"
      25             : #include <opendht/rng.h>
      26             : 
      27             : namespace jami { namespace test {
      28             : 
      29             : class SchedulerTest : public CppUnit::TestFixture {
      30             : public:
      31           2 :     static std::string name() { return "scheduler"; }
      32             : 
      33             : private:
      34             :     void schedulerTest();
      35             : 
      36           2 :     CPPUNIT_TEST_SUITE(SchedulerTest);
      37           1 :     CPPUNIT_TEST(schedulerTest);
      38           4 :     CPPUNIT_TEST_SUITE_END();
      39             : };
      40             : 
      41             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SchedulerTest, SchedulerTest::name());
      42             : 
      43             : void
      44           1 : SchedulerTest::schedulerTest()
      45             : {
      46           2 :     jami::ScheduledExecutor executor("test");
      47             : 
      48           1 :     constexpr unsigned N = 1024;
      49           1 :     std::mutex mtx;
      50           1 :     std::condition_variable cv;
      51           1 :     std::unique_lock lk(mtx);
      52             : 
      53           1 :     std::atomic_uint64_t taskRun {0};
      54           1 :     std::atomic_uint64_t result {0};
      55             : 
      56        2049 :     auto task = [&]{
      57        2049 :         auto rng = dht::crypto::getSeededRandomEngine();
      58        2049 :         uint64_t sum {0};
      59   134285313 :         for (uint64_t i=0; i<64 * N; i++)
      60   134283264 :             sum += rng();
      61        2049 :         result += sum;
      62        2049 :         std::lock_guard l(mtx);
      63        2049 :         if (++taskRun == N)
      64           1 :             cv.notify_all();
      65        2049 :     };
      66           1 :     CPPUNIT_ASSERT(taskRun == 0);
      67             : 
      68        1025 :     for (unsigned i=0; i<N; i++)
      69        1024 :         executor.run(task);
      70             : 
      71           3 :     CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(30), [&]{
      72             :         return taskRun == N;
      73             :     }));
      74             : 
      75        1025 :     for (unsigned i=0; i<N; i++)
      76        1024 :         executor.scheduleIn(task, std::chrono::microseconds(1));
      77             : 
      78           3 :     CPPUNIT_ASSERT(cv.wait_for(lk, std::chrono::seconds(30), [&]{
      79             :         return taskRun == 2 * N;
      80             :     }));
      81             : 
      82        1025 :     for (unsigned i=0; i<N; i++)
      83        1024 :         executor.scheduleIn(task, std::chrono::microseconds(1));
      84           1 :     executor.stop();
      85           1 : }
      86             : 
      87             : }} // namespace jami::test
      88             : 
      89           1 : RING_TEST_RUNNER(jami::test::SchedulerTest::name());

Generated by: LCOV version 1.14