| 350 | |
| 351 | |
| 352 | def test_tokenize_object(): |
| 353 | with dask.config.set({"tokenize.ensure-deterministic": False}): |
| 354 | # object() tokenization is idempotent... |
| 355 | o = object() |
| 356 | assert tokenize(o) == tokenize(o) |
| 357 | # ...but not deterministic |
| 358 | assert tokenize(object()) != tokenize(object()) |
| 359 | |
| 360 | # Two objects don't tokenize to the same token even if their pickle output is |
| 361 | # identical. Stress id reuse by creating and dereferencing many objects in quick |
| 362 | # succession. |
| 363 | assert len({tokenize(object()) for _ in range(100)}) == 100 |
| 364 | |
| 365 | # You can call normalize_token even if the _ensure_deterministic context |
| 366 | # variable hasn't been set |
| 367 | assert normalize_token(o) == normalize_token(o) |
| 368 | |
| 369 | with dask.config.set({"tokenize.ensure-deterministic": True}): |
| 370 | with pytest.raises(TokenizationError, match="deterministic"): |
| 371 | tokenize(o) |
| 372 | with pytest.raises(TokenizationError, match="deterministic"): |
| 373 | normalize_token(o) |
| 374 | |
| 375 | # Test env override |
| 376 | assert tokenize(o, ensure_deterministic=False) == tokenize( |
| 377 | o, ensure_deterministic=False |
| 378 | ) |
| 379 | with pytest.raises(TokenizationError, match="deterministic"): |
| 380 | tokenize(o, ensure_deterministic=True) |
| 381 | |
| 382 | |
| 383 | def nested_tokenize_ensure_deterministic(): |