KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_throttle.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <core/throttle.h>
24#include <thread>
25
26
27BOOST_AUTO_TEST_SUITE( Throttle )
28
29
30BOOST_AUTO_TEST_CASE( FirstCallAlwaysReady )
31{
32 THROTTLE t( std::chrono::milliseconds( 500 ) );
33 BOOST_CHECK( t.Ready() );
34}
35
36
37BOOST_AUTO_TEST_CASE( SecondCallRejectsWithinInterval )
38{
39 THROTTLE t( std::chrono::milliseconds( 200 ) );
40 BOOST_CHECK( t.Ready() );
41 BOOST_CHECK( !t.Ready() );
42}
43
44
45BOOST_AUTO_TEST_CASE( ReadyAgainAfterInterval )
46{
47 THROTTLE t( std::chrono::milliseconds( 50 ) );
48 BOOST_CHECK( t.Ready() );
49
50 std::this_thread::sleep_for( std::chrono::milliseconds( 60 ) );
51
52 BOOST_CHECK( t.Ready() );
53}
54
55
56BOOST_AUTO_TEST_CASE( ProgressReporterGatingThrottlesBulkCalls )
57{
58 THROTTLE throttle( std::chrono::milliseconds( 100 ) );
59
60 int refreshes = 0;
61 int maxProgress = 1000;
62
63 // Only the priming refresh should run inside the throttle window.
64 for( int progress = 0; progress < maxProgress; ++progress )
65 {
66 if( WX_PROGRESS_REPORTER::shouldRefresh( progress, maxProgress, 0, 1, throttle ) )
67 refreshes++;
68 }
69
70 BOOST_CHECK_EQUAL( refreshes, 1 );
71}
72
73
74BOOST_AUTO_TEST_CASE( ProgressReporterGatingAlwaysRefreshesFinalTick )
75{
76 THROTTLE throttle( std::chrono::milliseconds( 100 ) );
77
78 int maxProgress = 1000;
79
80 // Consume the priming refresh.
81 BOOST_CHECK( WX_PROGRESS_REPORTER::shouldRefresh( 0, maxProgress, 0, 1, throttle ) );
82 BOOST_CHECK( !WX_PROGRESS_REPORTER::shouldRefresh( 500, maxProgress, 0, 1, throttle ) );
83
84 // The final tick must refresh regardless of throttle state.
85 BOOST_CHECK( WX_PROGRESS_REPORTER::shouldRefresh( maxProgress, maxProgress, 0, 1, throttle ) );
86}
87
88
89BOOST_AUTO_TEST_CASE( ProgressReporterGatingThrottlesPhaseBoundaries )
90{
91 THROTTLE throttle( std::chrono::milliseconds( 100 ) );
92
93 int maxProgress = 1000;
94
95 // Consume the priming refresh.
96 BOOST_CHECK( WX_PROGRESS_REPORTER::shouldRefresh( 0, maxProgress, 0, 2, throttle ) );
97
98 // Intermediate phase completion must stay throttled.
99 BOOST_CHECK( !WX_PROGRESS_REPORTER::shouldRefresh( maxProgress, maxProgress, 0, 2, throttle ) );
100
101 // The last phase is the terminal tick.
102 BOOST_CHECK( WX_PROGRESS_REPORTER::shouldRefresh( maxProgress, maxProgress, 1, 2, throttle ) );
103}
104
105
Rate-limiter that fires at most once per interval.
Definition throttle.h:31
bool Ready()
Definition throttle.h:44
static bool shouldRefresh(int aProgress, int aMaxProgress, int aPhase, int aNumPhases, THROTTLE &aThrottle)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EQUAL(result, "25.4")
BOOST_AUTO_TEST_CASE(FirstCallAlwaysReady)