1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
# Tests the spec based on:
# https://github.com/promises-aplus/promises-tests
from promise import Promise
from .utils import assert_exception
from threading import Event
class Counter:
"""
A helper class with some side effects
we can test.
"""
def __init__(self):
self.count = 0
def tick(self):
self.count += 1
def value(self):
return self.count
def test_3_2_1():
"""
Test that the arguments to 'then' are optional.
"""
p1 = Promise()
p2 = p1.then()
p3 = Promise()
p4 = p3.then()
p1.do_resolve(5)
p3.do_reject(Exception("How dare you!"))
def test_3_2_1_1():
"""
That that the first argument to 'then' is ignored if it
is not a function.
"""
results = {}
nonFunctions = [None, False, 5, {}, []]
def testNonFunction(nonFunction):
def foo(k, r):
results[k] = r
p1 = Promise.reject(Exception("Error: " + str(nonFunction)))
p2 = p1.then(nonFunction, lambda r: foo(str(nonFunction), r))
p2._wait()
for v in nonFunctions:
testNonFunction(v)
for v in nonFunctions:
assert_exception(results[str(v)], Exception, "Error: " + str(v))
def test_3_2_1_2():
"""
That that the second argument to 'then' is ignored if it
is not a function.
"""
results = {}
nonFunctions = [None, False, 5, {}, []]
def testNonFunction(nonFunction):
def foo(k, r):
results[k] = r
p1 = Promise.resolve("Error: " + str(nonFunction))
p2 = p1.then(lambda r: foo(str(nonFunction), r), nonFunction)
p2._wait()
for v in nonFunctions:
testNonFunction(v)
for v in nonFunctions:
assert "Error: " + str(v) == results[str(v)]
def test_3_2_2_1():
"""
The first argument to 'then' must be called when a promise is
fulfilled.
"""
c = Counter()
def check(v, c):
assert v == 5
c.tick()
p1 = Promise.resolve(5)
p2 = p1.then(lambda v: check(v, c))
p2._wait()
assert 1 == c.value()
def test_3_2_2_2():
"""
Make sure callbacks are never called more than once.
"""
c = Counter()
p1 = Promise.resolve(5)
p2 = p1.then(lambda v: c.tick())
p2._wait()
try:
# I throw an exception
p1.do_resolve(5)
assert False # Should not get here!
except AssertionError:
# This is expected
pass
assert 1 == c.value()
def test_3_2_2_3():
"""
Make sure fulfilled callback never called if promise is rejected
"""
cf = Counter()
cr = Counter()
p1 = Promise.reject(Exception("Error"))
p2 = p1.then(lambda v: cf.tick(), lambda r: cr.tick())
p2._wait()
assert 0 == cf.value()
assert 1 == cr.value()
def test_3_2_3_1():
"""
The second argument to 'then' must be called when a promise is
rejected.
"""
c = Counter()
def check(r, c):
assert_exception(r, Exception, "Error")
c.tick()
p1 = Promise.reject(Exception("Error"))
p2 = p1.then(None, lambda r: check(r, c))
p2._wait()
assert 1 == c.value()
def test_3_2_3_2():
"""
Make sure callbacks are never called more than once.
"""
c = Counter()
p1 = Promise.reject(Exception("Error"))
p2 = p1.then(None, lambda v: c.tick())
p2._wait()
try:
# I throw an exception
p1.do_reject(Exception("Error"))
assert False # Should not get here!
except AssertionError:
# This is expected
pass
assert 1 == c.value()
def test_3_2_3_3():
"""
Make sure rejected callback never called if promise is fulfilled
"""
cf = Counter()
cr = Counter()
p1 = Promise.resolve(5)
p2 = p1.then(lambda v: cf.tick(), lambda r: cr.tick())
p2._wait()
assert 0 == cr.value()
assert 1 == cf.value()
def test_3_2_5_1_when():
"""
Then can be called multiple times on the same promise
and callbacks must be called in the order of the
then calls.
"""
def add(l, v):
l.append(v)
p1 = Promise.resolve(2)
order = []
p2 = p1.then(lambda v: add(order, "p2"))
p3 = p1.then(lambda v: add(order, "p3"))
p2._wait()
p3._wait()
assert 2 == len(order)
assert "p2" == order[0]
assert "p3" == order[1]
def test_3_2_5_1_if():
"""
Then can be called multiple times on the same promise
and callbacks must be called in the order of the
then calls.
"""
def add(l, v):
l.append(v)
p1 = Promise.resolve(2)
order = []
p2 = p1.then(lambda v: add(order, "p2"))
p3 = p1.then(lambda v: add(order, "p3"))
p2._wait()
p3._wait()
assert 2 == len(order)
assert "p2" == order[0]
assert "p3" == order[1]
def test_3_2_5_2_when():
"""
Then can be called multiple times on the same promise
and callbacks must be called in the order of the
then calls.
"""
def add(l, v):
l.append(v)
p1 = Promise.reject(Exception("Error"))
order = []
p2 = p1.then(None, lambda v: add(order, "p2"))
p3 = p1.then(None, lambda v: add(order, "p3"))
p2._wait()
p3._wait()
assert 2 == len(order)
assert "p2" == order[0]
assert "p3" == order[1]
def test_3_2_5_2_if():
"""
Then can be called multiple times on the same promise
and callbacks must be called in the order of the
then calls.
"""
def add(l, v):
l.append(v)
p1 = Promise.reject(Exception("Error"))
order = []
p2 = p1.then(None, lambda v: add(order, "p2"))
p3 = p1.then(None, lambda v: add(order, "p3"))
p2._wait()
p3._wait()
assert 2 == len(order)
assert "p2" == order[0]
assert "p3" == order[1]
def test_3_2_6_1():
"""
Promises returned by then must be fulfilled when the promise
they are chained from is fulfilled IF the fulfillment value
is not a promise.
"""
p1 = Promise.resolve(5)
pf = p1.then(lambda v: v * v)
assert pf.get() == 25
p2 = Promise.reject(Exception("Error"))
pr = p2.then(None, lambda r: 5)
assert 5 == pr.get()
def test_3_2_6_2_when():
"""
Promises returned by then must be rejected when any of their
callbacks throw an exception.
"""
def fail(v):
raise AssertionError("Exception Message")
p1 = Promise.resolve(5)
pf = p1.then(fail)
pf._wait()
assert pf.is_rejected
assert_exception(pf.reason, AssertionError, "Exception Message")
p2 = Promise.reject(Exception("Error"))
pr = p2.then(None, fail)
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, AssertionError, "Exception Message")
def test_3_2_6_2_if():
"""
Promises returned by then must be rejected when any of their
callbacks throw an exception.
"""
def fail(v):
raise AssertionError("Exception Message")
p1 = Promise.resolve(5)
pf = p1.then(fail)
pf._wait()
assert pf.is_rejected
assert_exception(pf.reason, AssertionError, "Exception Message")
p2 = Promise.reject(Exception("Error"))
pr = p2.then(None, fail)
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, AssertionError, "Exception Message")
def test_3_2_6_3_when_fulfilled():
"""
Testing return of pending promises to make
sure they are properly chained.
This covers the case where the root promise
is fulfilled after the chaining is defined.
"""
p1 = Promise()
pending = Promise()
def p1_resolved(v):
return pending
pf = p1.then(p1_resolved)
assert pending.is_pending
assert pf.is_pending
p1.do_resolve(10)
pending.do_resolve(5)
pending._wait()
assert pending.is_fulfilled
assert 5 == pending.get()
pf._wait()
assert pf.is_fulfilled
assert 5 == pf.get()
p2 = Promise()
bad = Promise()
pr = p2.then(lambda r: bad)
assert bad.is_pending
assert pr.is_pending
p2.do_resolve(10)
bad._reject_callback(Exception("Error"))
bad._wait()
assert bad.is_rejected
assert_exception(bad.reason, Exception, "Error")
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, Exception, "Error")
def test_3_2_6_3_if_fulfilled():
"""
Testing return of pending promises to make
sure they are properly chained.
This covers the case where the root promise
is fulfilled before the chaining is defined.
"""
p1 = Promise()
p1.do_resolve(10)
pending = Promise()
pending.do_resolve(5)
pf = p1.then(lambda r: pending)
pending._wait()
assert pending.is_fulfilled
assert 5 == pending.get()
pf._wait()
assert pf.is_fulfilled
assert 5 == pf.get()
p2 = Promise()
p2.do_resolve(10)
bad = Promise()
bad.do_reject(Exception("Error"))
pr = p2.then(lambda r: bad)
bad._wait()
assert_exception(bad.reason, Exception, "Error")
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, Exception, "Error")
def test_3_2_6_3_when_rejected():
"""
Testing return of pending promises to make
sure they are properly chained.
This covers the case where the root promise
is rejected after the chaining is defined.
"""
p1 = Promise()
pending = Promise()
pr = p1.then(None, lambda r: pending)
assert pending.is_pending
assert pr.is_pending
p1.do_reject(Exception("Error"))
pending.do_resolve(10)
pending._wait()
assert pending.is_fulfilled
assert 10 == pending.get()
assert 10 == pr.get()
p2 = Promise()
bad = Promise()
pr = p2.then(None, lambda r: bad)
assert bad.is_pending
assert pr.is_pending
p2.do_reject(Exception("Error"))
bad.do_reject(Exception("Assertion"))
bad._wait()
assert bad.is_rejected
assert_exception(bad.reason, Exception, "Assertion")
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, Exception, "Assertion")
def test_3_2_6_3_if_rejected():
"""
Testing return of pending promises to make
sure they are properly chained.
This covers the case where the root promise
is rejected before the chaining is defined.
"""
p1 = Promise()
p1.do_reject(Exception("Error"))
pending = Promise()
pending.do_resolve(10)
pr = p1.then(None, lambda r: pending)
pending._wait()
assert pending.is_fulfilled
assert 10 == pending.get()
pr._wait()
assert pr.is_fulfilled
assert 10 == pr.get()
p2 = Promise()
p2.do_reject(Exception("Error"))
bad = Promise()
bad.do_reject(Exception("Assertion"))
pr = p2.then(None, lambda r: bad)
bad._wait()
assert bad.is_rejected
assert_exception(bad.reason, Exception, "Assertion")
pr._wait()
assert pr.is_rejected
assert_exception(pr.reason, Exception, "Assertion")
def test_3_2_6_4_pending():
"""
Handles the case where the arguments to then
are not functions or promises.
"""
p1 = Promise()
p2 = p1.then(5)
p1.do_resolve(10)
assert 10 == p1.get()
p2._wait()
assert p2.is_fulfilled
assert 10 == p2.get()
def test_3_2_6_4_fulfilled():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p1.do_resolve(10)
p2 = p1.then(5)
assert 10 == p1.get()
p2._wait()
assert p2.is_fulfilled
assert 10 == p2.get()
def test_3_2_6_5_pending():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p2 = p1.then(None, 5)
p1.do_reject(Exception("Error"))
assert_exception(p1.reason, Exception, "Error")
p2._wait()
assert p2.is_rejected
assert_exception(p2.reason, Exception, "Error")
def test_3_2_6_5_rejected():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise()
p1.do_reject(Exception("Error"))
p2 = p1.then(None, 5)
assert_exception(p1.reason, Exception, "Error")
p2._wait()
assert p2.is_rejected
assert_exception(p2.reason, Exception, "Error")
def test_chained_promises():
"""
Handles the case where the arguments to then
are values, not functions or promises.
"""
p1 = Promise(lambda resolve, reject: resolve(Promise.resolve(True)))
assert p1.get() == True
def test_promise_resolved_after():
"""
The first argument to 'then' must be called when a promise is
fulfilled.
"""
c = Counter()
def check(v, c):
assert v == 5
c.tick()
p1 = Promise()
p2 = p1.then(lambda v: check(v, c))
p1.do_resolve(5)
Promise.wait(p2)
assert 1 == c.value()
def test_promise_follows_indifentely():
a = Promise.resolve(None)
b = a.then(lambda x: Promise.resolve("X"))
e = Event()
def b_then(v):
c = Promise.resolve(None)
d = c.then(lambda v: Promise.resolve("B"))
return d
promise = b.then(b_then)
assert promise.get() == "B"
def test_promise_all_follows_indifentely():
promises = Promise.all(
[
Promise.resolve("A"),
Promise.resolve(None)
.then(Promise.resolve)
.then(lambda v: Promise.resolve(None).then(lambda v: Promise.resolve("B"))),
]
)
assert promises.get() == ["A", "B"]
|