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-04-24 08:04:06 Functions: 9 9 100.0 %

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

Generated by: LCOV version 1.14