| 1 |
import datetime |
| 2 |
import time |
| 3 |
|
| •4 |
import progressbar2 |
| 5 |
|
| 6 |
|
| 7 |
def test_timer() -> None: |
| 8 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 9 |
widgets = [ |
| •10 |
progressbar2.Timer(), |
| 11 |
] |
| •12 |
p = progressbar2.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 |
progressbar2.ETA(), |
| 31 |
] |
| •32 |
p = progressbar2.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 |
progressbar2.AdaptiveETA(), |
| 58 |
] |
| 59 |
widgets[0].INTERVAL = datetime.timedelta(microseconds=1) |
| •60 |
p = progressbar2.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 |
progressbar2.AdaptiveTransferSpeed(), |
| 78 |
] |
| •79 |
p = progressbar2.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 |
progressbar2.FileTransferSpeed(), |
| 98 |
progressbar2.AdaptiveTransferSpeed(samples=n / 2), |
| 99 |
] |
| 100 |
|
| 101 |
datas = [] |
| 102 |
|
| 103 |
|
| 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(progressbar2.FileTransferSpeed, '_speed', calculate_eta) |
| 114 |
monkeypatch.setattr( |
| •115 |
progressbar2.AdaptiveTransferSpeed, |
| 116 |
'_speed', |
| 117 |
calculate_eta, |
| 118 |
) |
| 119 |
|
| 120 |
for widget in widgets: |
| 121 |
widget.INTERVAL = interval |
| 122 |
|
| •123 |
p = progressbar2.ProgressBar( |
| 124 |
max_value=n, |
| 125 |
widgets=widgets, |
| 126 |
poll_interval=interval, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
def test_non_changing_eta() -> None: |
| 155 |
"""Testing (Adaptive)ETA when the value doesn't actually change""" |
| 156 |
widgets = [ |
| •157 |
progressbar2.AdaptiveETA(), |
| 158 |
progressbar2.ETA(), |
| 159 |
progressbar2.AdaptiveTransferSpeed(), |
| 160 |
] |
| •161 |
p = progressbar2.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 = [progressbar2.AdaptiveETA(), progressbar2.ETA()] |
| 184 |
|
| •185 |
bar = progressbar2.ProgressBar(widgets=widgets) |
| 186 |
for _i in bar(gen()): |
| 187 |
pass |