| 1 |
import datetime |
| 2 |
import time |
| 3 |
|
| 4 |
import progressbar |
| 5 |
|
| 6 |
|
| 7 |
def test_timer() -> None: |
| 8 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 9 |
widgets = [ |
| 10 |
progressbar.Timer(), |
| 11 |
] |
| 12 |
p = progressbar.ProgressBar( |
| 13 |
max_value=2, |
| 14 |
widgets=widgets, |
| 15 |
poll_interval=0.0001, |
| 16 |
) |
| 17 |
|
| 18 |
p.start() |
| 19 |
p.update() |
| 20 |
p.update(1) |
| 21 |
p._needs_update() |
| 22 |
time.sleep(0.001) |
| 23 |
p.update(1) |
| 24 |
p.finish() |
| 25 |
|
| 26 |
|
| 27 |
def test_eta() -> None: |
| 28 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 29 |
widgets = [ |
| 30 |
progressbar.ETA(), |
| 31 |
] |
| 32 |
p = progressbar.ProgressBar( |
| 33 |
min_value=0, |
| 34 |
max_value=2, |
| 35 |
widgets=widgets, |
| 36 |
poll_interval=0.0001, |
| 37 |
) |
| 38 |
|
| 39 |
p.start() |
| 40 |
time.sleep(0.001) |
| 41 |
p.update(0) |
| 42 |
time.sleep(0.001) |
| 43 |
p.update(1) |
| 44 |
time.sleep(0.001) |
| 45 |
p.update(1) |
| 46 |
time.sleep(0.001) |
| 47 |
p.update(2) |
| 48 |
time.sleep(0.001) |
| 49 |
p.finish() |
| 50 |
time.sleep(0.001) |
| 51 |
p.update(2) |
| 52 |
|
| 53 |
|
| 54 |
def test_adaptive_eta() -> None: |
| 55 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 56 |
widgets = [ |
| 57 |
progressbar.AdaptiveETA(), |
| 58 |
] |
| 59 |
widgets[0].INTERVAL = datetime.timedelta(microseconds=1) |
| 60 |
p = progressbar.ProgressBar( |
| 61 |
max_value=2, |
| 62 |
samples=2, |
| 63 |
widgets=widgets, |
| 64 |
poll_interval=0.0001, |
| 65 |
) |
| 66 |
|
| 67 |
p.start() |
| 68 |
for _i in range(20): |
| 69 |
p.update(1) |
| 70 |
time.sleep(0.001) |
| 71 |
p.finish() |
| 72 |
|
| 73 |
|
| 74 |
def test_adaptive_transfer_speed() -> None: |
| 75 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 76 |
widgets = [ |
| 77 |
progressbar.AdaptiveTransferSpeed(), |
| 78 |
] |
| 79 |
p = progressbar.ProgressBar( |
| 80 |
max_value=2, |
| 81 |
widgets=widgets, |
| 82 |
poll_interval=0.0001, |
| 83 |
) |
| 84 |
|
| 85 |
p.start() |
| 86 |
p.update(1) |
| 87 |
time.sleep(0.001) |
| 88 |
p.update(1) |
| 89 |
p.finish() |
| 90 |
|
| 91 |
|
| 92 |
def test_etas(monkeypatch) -> None: |
| 93 |
"""Compare file transfer speed to adaptive transfer speed""" |
| 94 |
n = 10 |
| 95 |
interval = datetime.timedelta(seconds=1) |
| 96 |
widgets = [ |
| 97 |
progressbar.FileTransferSpeed(), |
| 98 |
progressbar.AdaptiveTransferSpeed(samples=n / 2), |
| 99 |
] |
| 100 |
|
| 101 |
datas = [] |
| 102 |
|
| 103 |
# Capture the output sent towards the `_speed` method |
| 104 |
def calculate_eta(self, value, elapsed): |
| 105 |
"""Capture the widget output""" |
| 106 |
data = dict( |
| 107 |
value=value, |
| 108 |
elapsed=int(elapsed), |
| 109 |
) |
| 110 |
datas.append(data) |
| 111 |
return 0, 0 |
| 112 |
|
| 113 |
monkeypatch.setattr(progressbar.FileTransferSpeed, '_speed', calculate_eta) |
| 114 |
monkeypatch.setattr( |
| 115 |
progressbar.AdaptiveTransferSpeed, |
| 116 |
'_speed', |
| 117 |
calculate_eta, |
| 118 |
) |
| 119 |
|
| 120 |
for widget in widgets: |
| 121 |
widget.INTERVAL = interval |
| 122 |
|
| 123 |
p = progressbar.ProgressBar( |
| 124 |
max_value=n, |
| 125 |
widgets=widgets, |
| 126 |
poll_interval=interval, |
| 127 |
) |
| 128 |
|
| 129 |
# Run the first few samples at a low speed and speed up later so we can |
| 130 |
# compare the results from both widgets |
| 131 |
for i in range(n): |
| 132 |
p.update(i) |
| 133 |
if i > n / 2: |
| 134 |
time.sleep(1) |
| 135 |
else: |
| 136 |
time.sleep(10) |
| 137 |
p.finish() |
| 138 |
|
| 139 |
# Due to weird travis issues, the actual testing is disabled for now |
| 140 |
# import pprint |
| 141 |
# pprint.pprint(datas[::2]) |
| 142 |
# pprint.pprint(datas[1::2]) |
| 143 |
|
| 144 |
# for i, (a, b) in enumerate(zip(datas[::2], datas[1::2])): |
| 145 |
# # Because the speed is identical initially, the results should be the |
| 146 |
# # same for adaptive and regular transfer speed. Only when the speed |
| 147 |
# # changes we should start see a lot of differences between the two |
| 148 |
# if i < (n / 2 - 1): |
| 149 |
# assert a['elapsed'] == b['elapsed'] |
| 150 |
# if i > (n / 2 + 1): |
| 151 |
# assert a['elapsed'] > b['elapsed'] |
| 152 |
|
| 153 |
|
| 154 |
def test_non_changing_eta() -> None: |
| 155 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 156 |
widgets = [ |
| 157 |
progressbar.AdaptiveETA(), |
| 158 |
progressbar.ETA(), |
| 159 |
progressbar.AdaptiveTransferSpeed(), |
| 160 |
] |
| 161 |
p = progressbar.ProgressBar( |
| 162 |
max_value=2, |
| 163 |
widgets=widgets, |
| 164 |
poll_interval=0.0001, |
| 165 |
) |
| 166 |
|
| 167 |
p.start() |
| 168 |
p.update(1) |
| 169 |
time.sleep(0.001) |
| 170 |
p.update(1) |
| 171 |
p.finish() |
| 172 |
|
| 173 |
|
| 174 |
def test_eta_not_available(): |
| 175 |
""" |
| 176 |
When ETA is not available (data coming from a generator), |
| 177 |
ETAs should not raise exceptions. |
| 178 |
""" |
| 179 |
|
| 180 |
def gen(): |
| 181 |
yield from range(200) |
| 182 |
|
| 183 |
widgets = [progressbar.AdaptiveETA(), progressbar.ETA()] |
| 184 |
|
| 185 |
bar = progressbar.ProgressBar(widgets=widgets) |
| 186 |
for _i in bar(gen()): |
| 187 |
pass |