Skip to content

Commit b0a4f32

Browse files
committed
implemented freezegun and added tests for fix wolph#160
1 parent 37facc7 commit b0a4f32

8 files changed

Lines changed: 143 additions & 98 deletions

File tree

progressbar/widgets.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,11 @@ def __call__(self, progress, data, delta=False):
273273
sample_values.append(progress.value)
274274

275275
if isinstance(self.samples, datetime.timedelta):
276-
begin_time = progress.last_update_time - self.samples
277-
begin_value = sample_values[0]
278-
while (sample_times[2:]
279-
and begin_time > sample_times[0]
280-
and begin_value > sample_values[0]):
276+
minimum_time = progress.last_update_time - self.samples
277+
minimum_value = sample_values[-1]
278+
while (sample_times[2:] and
279+
minimum_time > sample_times[1] and
280+
minimum_value > sample_values[1]):
281281
sample_times.pop(0)
282282
sample_values.pop(0)
283283
else:

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
'pytest-cov>=2.5.1',
7676
'pytest-flakes>=2.0.0',
7777
'pytest-pep8>=1.0.6',
78-
'pytest-xdist>=1.22.2',
7978
'sphinx>=1.7.1',
8079
],
8180
},

tests/conftest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import time
22
import pytest
3-
import progressbar
43
import logging
4+
import freezegun
5+
import progressbar
56

67

78
LOG_LEVELS = {
@@ -21,11 +22,12 @@ def pytest_configure(config):
2122
def small_interval(monkeypatch):
2223
# Remove the update limit for tests by default
2324
monkeypatch.setattr(
24-
progressbar.ProgressBar, '_MINIMUM_UPDATE_INTERVAL', 0.000001)
25+
progressbar.ProgressBar, '_MINIMUM_UPDATE_INTERVAL', 1e-6)
2526

2627

2728
@pytest.fixture(autouse=True)
2829
def sleep_faster(monkeypatch):
29-
sleep = time.sleep
30-
monkeypatch.setattr('time.sleep', lambda t: sleep(t / 1e6))
31-
30+
with freezegun.freeze_time() as fake_time:
31+
monkeypatch.setattr('time.sleep', fake_time.tick)
32+
monkeypatch.setattr('timeit.default_timer', time.time)
33+
yield

tests/test_custom_widgets.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import progressbar
23

34

@@ -28,16 +29,22 @@ def test_crazy_file_transfer_speed_widget():
2829
p.start()
2930
for i in range(0, 200, 5):
3031
# do something
32+
time.sleep(0.1)
3133
p.update(i + 1)
3234
p.finish()
3335

3436

3537
def test_dynamic_message_widget():
36-
widgets = [' [', progressbar.Timer(), '] ', progressbar.Bar(), ' (',
37-
progressbar.ETA(), ') ', progressbar.DynamicMessage('loss')]
38+
widgets = [
39+
' [', progressbar.Timer(), '] ',
40+
progressbar.Bar(),
41+
' (', progressbar.ETA(), ') ',
42+
progressbar.DynamicMessage('loss'),
43+
]
3844

3945
p = progressbar.ProgressBar(widgets=widgets, max_value=1000)
4046
p.start()
4147
for i in range(0, 200, 5):
48+
time.sleep(0.1)
4249
p.update(i + 1, loss=.5)
4350
p.finish()

tests/test_failure.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
import pytest
23
import progressbar
34

@@ -20,13 +21,15 @@ def test_no_max_value():
2021
p = progressbar.ProgressBar()
2122
p.start()
2223
for i in range(5):
24+
time.sleep(1)
2325
p.update(i)
2426

2527

2628
def test_correct_max_value():
2729
'''Looping up to 5 when max_value is 10? No problem'''
2830
p = progressbar.ProgressBar(max_value=10)
2931
for i in range(5):
32+
time.sleep(1)
3033
p.update(i)
3134

3235

@@ -62,7 +65,7 @@ def test_changing_max_value():
6265
'''Changing max_value? No problem'''
6366
p = progressbar.ProgressBar(max_value=10)(range(20), max_value=20)
6467
for i in p:
65-
pass
68+
time.sleep(1)
6669

6770

6871
def test_backwards():
@@ -77,10 +80,12 @@ def test_incorrect_max_value():
7780
'''Looping up to 10 when max_value is 5? This is madness!'''
7881
p = progressbar.ProgressBar(max_value=5)
7982
for i in range(5):
83+
time.sleep(1)
8084
p.update(i)
8185

8286
with pytest.raises(ValueError):
8387
for i in range(5, 10):
88+
time.sleep(1)
8489
p.update(i)
8590

8691

@@ -98,6 +103,7 @@ def test_unexpected_update_keyword_arg():
98103
p = progressbar.ProgressBar(max_value=10)
99104
with pytest.raises(TypeError):
100105
for i in range(10):
106+
time.sleep(1)
101107
p.update(i, foo=10)
102108

103109

tests/test_monitor_progress.py

Lines changed: 76 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import six
2-
import time
31
import pprint
4-
import progressbar
2+
53
pytest_plugins = 'pytester'
64

75

@@ -11,29 +9,31 @@ def test_list_example(testdir):
119
best capture its stderr. We expect to see match_lines in order in the
1210
output. This test is just a sanity check to ensure that the progress
1311
bar progresses from 1 to 10, it does not make sure that the '''
12+
1413
v = testdir.makepyfile('''
15-
import time
16-
import progressbar
14+
import time
15+
import freezegun
16+
import progressbar
1717
18-
bar = progressbar.ProgressBar(term_width=60)
18+
with freezegun.freeze_time() as fake_time:
19+
bar = progressbar.ProgressBar(term_width=65)
20+
bar._MINIMUM_UPDATE_INTERVAL = 1e-9
1921
for i in bar(list(range(9))):
20-
time.sleep(0.1)
21-
22+
fake_time.tick(1)
2223
''')
2324
result = testdir.runpython(v)
2425
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
2526
pprint.pprint(result.stderr.lines)
2627
result.stderr.re_match_lines([
27-
r'N/A% \(0 of 9\) \|\s+\| Elapsed Time: 0:00:00 ETA: --:--:--',
28-
r' 11% \(1 of 9\) \|\s+\| Elapsed Time: 0:00:00 ETA: 0:00:0[01]',
29-
r' 22% \(2 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
30-
r' 33% \(3 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
31-
r' 44% \(4 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
32-
r' 55% \(5 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
33-
r' 66% \(6 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
34-
r' 77% \(7 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
35-
r' 88% \(8 of 9\) \|#+\s+\| Elapsed Time: 0:00:00 ETA: 0:00:00',
36-
r'100% \(9 of 9\) \|#+\| Elapsed Time: 0:00:0[01] Time: 0:00:0[01]',
28+
'N/A% (0 of 9) | | Elapsed Time: 2:00:00 ETA: --:--:--',
29+
' 11% (1 of 9) |# | Elapsed Time: 2:00:01 ETA: 0:00:08',
30+
' 22% (2 of 9) |## | Elapsed Time: 2:00:02 ETA: 0:00:07',
31+
' 33% (3 of 9) |#### | Elapsed Time: 2:00:03 ETA: 0:00:06',
32+
' 44% (4 of 9) |##### | Elapsed Time: 2:00:04 ETA: 0:00:05',
33+
' 55% (5 of 9) |###### | Elapsed Time: 2:00:05 ETA: 0:00:04',
34+
' 66% (6 of 9) |######## | Elapsed Time: 2:00:06 ETA: 0:00:03',
35+
' 77% (7 of 9) |######### | Elapsed Time: 2:00:07 ETA: 0:00:02',
36+
' 88% (8 of 9) |########## | Elapsed Time: 2:00:08 ETA: 0:00:01',
3737
])
3838

3939

@@ -43,91 +43,86 @@ def test_generator_example(testdir):
4343
best capture its stderr. We expect to see match_lines in order in the
4444
output. This test is just a sanity check to ensure that the progress
4545
bar progresses from 1 to 10, it does not make sure that the '''
46+
4647
v = testdir.makepyfile('''
47-
import time
48-
import progressbar
48+
import time
49+
import freezegun
50+
import progressbar
4951
52+
with freezegun.freeze_time() as fake_time:
5053
bar = progressbar.ProgressBar(term_width=60)
54+
bar._MINIMUM_UPDATE_INTERVAL = 1e-9
5155
for i in bar(iter(range(9))):
52-
time.sleep(0.1)
53-
56+
fake_time.tick(1)
5457
''')
5558
result = testdir.runpython(v)
5659
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
5760
pprint.pprint(result.stderr.lines)
58-
result.stderr.re_match_lines([
59-
'/ |# | 0 Elapsed Time: 0:00:00',
60-
'- | # | 1 Elapsed Time: 0:00:00',
61-
'\\ | # | 2 Elapsed Time: 0:00:00',
62-
'| | # | 3 Elapsed Time: 0:00:00',
63-
'/ | # | 4 Elapsed Time: 0:00:00',
64-
'- | # | 5 Elapsed Time: 0:00:00',
65-
'\\ | # | 6 Elapsed Time: 0:00:00',
66-
'| | # | 7 Elapsed Time: 0:00:00',
67-
'/ | # | 8 Elapsed Time: 0:00:00',
68-
'| | # | 8 Elapsed Time: 0:00:00',
69-
])
61+
62+
lines = []
63+
for i in range(9):
64+
lines.append(r'[/\\|-] \|\s*#\s*\| %(i)d Elapsed Time: 2:00:%(i)02d' %
65+
dict(i=i))
66+
67+
result.stderr.re_match_lines(lines)
7068

7169

7270
def test_rapid_updates(testdir):
7371
''' Run some example code that updates 10 times, then sleeps .1 seconds,
7472
this is meant to test that the progressbar progresses normally with
7573
this sample code, since there were issues with it in the past '''
74+
7675
v = testdir.makepyfile('''
77-
import time
78-
import progressbar
76+
import time
77+
import freezegun
78+
import progressbar
7979
80+
with freezegun.freeze_time() as fake_time:
8081
bar = progressbar.ProgressBar(term_width=60)
81-
for i in bar(range(100)):
82-
if i % 10 == 0:
83-
time.sleep(0.1)
84-
82+
bar._MINIMUM_UPDATE_INTERVAL = 1e-9
83+
for i in bar(range(10)):
84+
if i % 2 == 0:
85+
fake_time.tick(1)
8586
''')
8687
result = testdir.runpython(v)
88+
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
8789
pprint.pprint(result.stderr.lines)
8890
result.stderr.re_match_lines([
89-
r' 1% \(1 of 100\)',
90-
r' 11% \(11 of 100\)',
91-
r' 21% \(21 of 100\)',
92-
r' 31% \(31 of 100\)',
93-
r' 41% \(41 of 100\)',
94-
r' 51% \(51 of 100\)',
95-
r' 61% \(61 of 100\)',
96-
r' 71% \(71 of 100\)',
97-
r' 81% \(81 of 100\)',
98-
r' 91% \(91 of 100\)',
99-
r'100% \(100 of 100\)'
91+
'N/A% (0 of 10) | | Elapsed Time: 2:00:00 ETA: --:--:--',
92+
' 10% (1 of 10) | | Elapsed Time: 2:00:01 ETA: 0:00:09',
93+
' 20% (2 of 10) |# | Elapsed Time: 2:00:01 ETA: 0:00:08',
94+
' 30% (3 of 10) |# | Elapsed Time: 2:00:02 ETA: 0:00:04',
95+
' 40% (4 of 10) |## | Elapsed Time: 2:00:02 ETA: 0:00:04',
96+
' 50% (5 of 10) |### | Elapsed Time: 2:00:03 ETA: 0:00:03',
97+
' 60% (6 of 10) |### | Elapsed Time: 2:00:03 ETA: 0:00:02',
98+
' 70% (7 of 10) |#### | Elapsed Time: 2:00:04 ETA: 0:00:01',
99+
' 80% (8 of 10) |#### | Elapsed Time: 2:00:04 ETA: 0:00:01',
100+
' 90% (9 of 10) |##### | Elapsed Time: 2:00:05 ETA: 0:00:00',
101+
'100% (10 of 10) |#####| Elapsed Time: 2:00:05 Time: 2:00:05',
100102
])
101103

102104

103105
def test_context_wrapper(testdir):
104-
fd = six.StringIO()
105-
106-
with progressbar.ProgressBar(term_width=60, fd=fd) as bar:
107-
bar._MINIMUM_UPDATE_INTERVAL = 0.0001
108-
for _ in bar(list(range(5))):
109-
time.sleep(0.001)
110-
111-
expected = (
112-
'',
113-
' ',
114-
'',
115-
'N/A% (0 of 5) | | Elapsed Time: 0:00:00 ETA: --:--:--',
116-
' ',
117-
'',
118-
' 20% (1 of 5) |# | Elapsed Time: 0:00:00 ETA: 0:00:00',
119-
' ',
120-
'',
121-
' 40% (2 of 5) |### | Elapsed Time: 0:00:00 ETA: 0:00:00',
122-
' ',
123-
'',
124-
' 60% (3 of 5) |#### | Elapsed Time: 0:00:00 ETA: 0:00:00',
125-
' ',
126-
'',
127-
' 80% (4 of 5) |###### | Elapsed Time: 0:00:00 ETA: 0:00:00',
128-
' ',
129-
'',
130-
'100% (5 of 5) |########| Elapsed Time: 0:00:00 Time: 0:00:00',
131-
)
132-
for line, expected_line in zip(fd.getvalue().split('\r'), expected):
133-
assert line == expected_line
106+
v = testdir.makepyfile('''
107+
import time
108+
import freezegun
109+
import progressbar
110+
111+
with freezegun.freeze_time() as fake_time:
112+
with progressbar.ProgressBar(term_width=60) as bar:
113+
bar._MINIMUM_UPDATE_INTERVAL = 1e-9
114+
for _ in bar(list(range(5))):
115+
fake_time.tick(1)
116+
''')
117+
118+
result = testdir.runpython(v)
119+
result.stderr.lines = [l for l in result.stderr.lines if l.strip()]
120+
pprint.pprint(result.stderr.lines)
121+
result.stderr.re_match_lines([
122+
'N/A% (0 of 5) | | Elapsed Time: 2:00:00 ETA: --:--:--',
123+
' 20% (1 of 5) |# | Elapsed Time: 2:00:01 ETA: 0:00:04',
124+
' 40% (2 of 5) |## | Elapsed Time: 2:00:02 ETA: 0:00:03',
125+
' 60% (3 of 5) |#### | Elapsed Time: 2:00:03 ETA: 0:00:02',
126+
' 80% (4 of 5) |##### | Elapsed Time: 2:00:04 ETA: 0:00:01',
127+
'100% (5 of 5) |#######| Elapsed Time: 2:00:05 Time: 2:00:05',
128+
])

0 commit comments

Comments
 (0)