2
2
// SPDX-License-Identifier: LGPL-3.0-only
3
3
4
4
using System ;
5
+ using Nethermind . Logging ;
6
+ using NSubstitute ;
5
7
using NUnit . Framework ;
6
8
7
9
namespace Nethermind . Core . Test ;
8
10
9
- public class MeasuredProgressTests
11
+ public class ProgressLoggerTests
10
12
{
11
13
[ Test ]
12
14
public void Current_per_second_uninitialized ( )
13
15
{
14
- MeasuredProgress measuredProgress = new ( ) ;
15
- Assert . That ( measuredProgress . CurrentPerSecond , Is . EqualTo ( decimal . Zero ) ) ;
16
+ ProgressLogger progressLogger = CreateProgress ( ) ;
17
+ Assert . That ( progressLogger . CurrentPerSecond , Is . EqualTo ( decimal . Zero ) ) ;
16
18
}
17
19
18
20
[ Test ]
19
21
public void Total_per_second_uninitialized ( )
20
22
{
21
- MeasuredProgress measuredProgress = new ( ) ;
22
- Assert . That ( measuredProgress . TotalPerSecond , Is . EqualTo ( decimal . Zero ) ) ;
23
+ ProgressLogger progressLogger = CreateProgress ( ) ;
24
+ Assert . That ( progressLogger . TotalPerSecond , Is . EqualTo ( decimal . Zero ) ) ;
23
25
}
24
26
25
27
[ Test ]
26
28
public void Current_value_uninitialized ( )
27
29
{
28
- MeasuredProgress measuredProgress = new ( ) ;
29
- Assert . That ( measuredProgress . CurrentValue , Is . EqualTo ( 0L ) ) ;
30
+ ProgressLogger progressLogger = CreateProgress ( ) ;
31
+ Assert . That ( progressLogger . CurrentValue , Is . EqualTo ( 0L ) ) ;
30
32
}
31
33
32
34
[ Test ]
33
35
public void Update_0L ( )
34
36
{
35
- MeasuredProgress measuredProgress = new ( ) ;
36
- measuredProgress . Update ( 0L ) ;
37
- Assert . That ( measuredProgress . CurrentValue , Is . EqualTo ( 0L ) ) ;
37
+ ProgressLogger progressLogger = CreateProgress ( ) ;
38
+ progressLogger . Update ( 0L ) ;
39
+ Assert . That ( progressLogger . CurrentValue , Is . EqualTo ( 0L ) ) ;
38
40
}
39
41
40
42
[ Test ]
41
43
public void Update_0L_total_per_second ( )
42
44
{
43
- MeasuredProgress measuredProgress = new ( ) ;
44
- measuredProgress . Update ( 0L ) ;
45
- Assert . That ( measuredProgress . TotalPerSecond , Is . EqualTo ( 0L ) ) ;
45
+ ProgressLogger progressLogger = CreateProgress ( ) ;
46
+ progressLogger . Update ( 0L ) ;
47
+ Assert . That ( progressLogger . TotalPerSecond , Is . EqualTo ( 0L ) ) ;
46
48
}
47
49
48
50
[ Test ]
49
51
public void Update_0L_current_per_second ( )
50
52
{
51
- MeasuredProgress measuredProgress = new ( ) ;
52
- measuredProgress . Update ( 0L ) ;
53
- Assert . That ( measuredProgress . CurrentPerSecond , Is . EqualTo ( 0L ) ) ;
53
+ ProgressLogger progressLogger = CreateProgress ( ) ;
54
+ progressLogger . Update ( 0L ) ;
55
+ Assert . That ( progressLogger . CurrentPerSecond , Is . EqualTo ( 0L ) ) ;
54
56
}
55
57
56
58
[ Test ]
57
59
[ Retry ( 3 ) ]
58
60
public void Update_twice_total_per_second ( )
59
61
{
60
- ManualTimestamper manualTimestamper = new ( ) ;
61
- MeasuredProgress measuredProgress = new ( manualTimestamper ) ;
62
+ ( ProgressLogger measuredProgress , ManualTimestamper manualTimestamper ) = CreateProgressWithManualTimestamper ( ) ;
62
63
measuredProgress . Update ( 0L ) ;
63
64
measuredProgress . SetMeasuringPoint ( ) ;
64
65
manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
@@ -71,8 +72,7 @@ public void Update_twice_total_per_second()
71
72
[ Retry ( 3 ) ]
72
73
public void Update_twice_current_per_second ( )
73
74
{
74
- ManualTimestamper manualTimestamper = new ( ) ;
75
- MeasuredProgress measuredProgress = new ( manualTimestamper ) ;
75
+ ( ProgressLogger measuredProgress , ManualTimestamper manualTimestamper ) = CreateProgressWithManualTimestamper ( ) ;
76
76
measuredProgress . Update ( 0L ) ;
77
77
measuredProgress . SetMeasuringPoint ( ) ;
78
78
manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
@@ -84,8 +84,7 @@ public void Update_twice_current_per_second()
84
84
[ Test ]
85
85
public void Current_starting_from_non_zero ( )
86
86
{
87
- ManualTimestamper manualTimestamper = new ( ) ;
88
- MeasuredProgress measuredProgress = new ( manualTimestamper ) ;
87
+ ( ProgressLogger measuredProgress , ManualTimestamper manualTimestamper ) = CreateProgressWithManualTimestamper ( ) ;
89
88
measuredProgress . Update ( 10L ) ;
90
89
measuredProgress . SetMeasuringPoint ( ) ;
91
90
manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
@@ -96,8 +95,7 @@ public void Current_starting_from_non_zero()
96
95
[ Test ]
97
96
public void Update_thrice_result_per_second ( )
98
97
{
99
- ManualTimestamper manualTimestamper = new ( ) ;
100
- MeasuredProgress measuredProgress = new ( manualTimestamper ) ;
98
+ ( ProgressLogger measuredProgress , ManualTimestamper manualTimestamper ) = CreateProgressWithManualTimestamper ( ) ;
101
99
measuredProgress . Update ( 0L ) ;
102
100
measuredProgress . SetMeasuringPoint ( ) ;
103
101
manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
@@ -114,8 +112,7 @@ public void Update_thrice_result_per_second()
114
112
[ Test ]
115
113
public void After_ending_does_not_update_total_or_current ( )
116
114
{
117
- ManualTimestamper manualTimestamper = new ( ) ;
118
- MeasuredProgress measuredProgress = new ( manualTimestamper ) ;
115
+ ( ProgressLogger measuredProgress , ManualTimestamper manualTimestamper ) = CreateProgressWithManualTimestamper ( ) ;
119
116
measuredProgress . Update ( 0L ) ;
120
117
measuredProgress . SetMeasuringPoint ( ) ;
121
118
manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
@@ -138,15 +135,52 @@ public void After_ending_does_not_update_total_or_current()
138
135
[ Test ]
139
136
public void Has_ended_returns_true_when_ended ( )
140
137
{
141
- MeasuredProgress measuredProgress = new ( ) ;
142
- measuredProgress . MarkEnd ( ) ;
143
- Assert . That ( measuredProgress . HasEnded , Is . True ) ;
138
+ ProgressLogger progressLogger = CreateProgress ( ) ;
139
+ progressLogger . MarkEnd ( ) ;
140
+ Assert . That ( progressLogger . HasEnded , Is . True ) ;
144
141
}
145
142
146
143
[ Test ]
147
144
public void Has_ended_returns_false_when_ended ( )
148
145
{
149
- MeasuredProgress measuredProgress = new ( ) ;
150
- Assert . That ( measuredProgress . HasEnded , Is . False ) ;
146
+ ProgressLogger progressLogger = CreateProgress ( ) ;
147
+ Assert . That ( progressLogger . HasEnded , Is . False ) ;
151
148
}
149
+
150
+ [ Test ]
151
+ public void Print_Progress ( )
152
+ {
153
+ ManualTimestamper manualTimestamper = new ( ) ;
154
+ ILogManager logManager = Substitute . For < ILogManager > ( ) ;
155
+ InterfaceLogger iLogger = Substitute . For < InterfaceLogger > ( ) ;
156
+ iLogger . IsInfo . Returns ( true ) ;
157
+ ILogger logger = new ( iLogger ) ;
158
+ logManager . GetClassLogger ( Arg . Any < string > ( ) ) . Returns ( logger ) ;
159
+
160
+ ProgressLogger measuredProgress = new ( "Progress" , logManager , manualTimestamper ) ;
161
+
162
+ measuredProgress . Reset ( 0L , 100 ) ;
163
+ measuredProgress . SetMeasuringPoint ( ) ;
164
+ measuredProgress . IncrementSkipped ( 10 ) ;
165
+ measuredProgress . CurrentQueued = 99 ;
166
+ manualTimestamper . Add ( TimeSpan . FromMilliseconds ( 100 ) ) ;
167
+ measuredProgress . Update ( 1L ) ;
168
+
169
+ measuredProgress . LogProgress ( ) ;
170
+
171
+ iLogger . Received ( 1 ) . Info ( "Progress 1 / 100 ( 0.99 %) [⡆ ] queue 99 | skipped 90 Blk/s | current 10 Blk/s" ) ;
172
+ }
173
+
174
+ private ProgressLogger CreateProgress ( )
175
+ {
176
+ return new ( "" , LimboLogs . Instance ) ;
177
+ }
178
+
179
+ private ( ProgressLogger , ManualTimestamper ) CreateProgressWithManualTimestamper ( )
180
+ {
181
+ ManualTimestamper manualTimestamper = new ( ) ;
182
+ ProgressLogger progressLogger = new ( "" , LimboLogs . Instance , manualTimestamper ) ;
183
+ return ( progressLogger , manualTimestamper ) ;
184
+ }
185
+
152
186
}
0 commit comments