File: test_memoize_exception.py

package info (click to toggle)
python-parsl 2026.01.26%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 12,144 kB
  • sloc: python: 24,400; makefile: 352; sh: 252; ansic: 45
file content (55 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download | duplicates (2)
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
import pytest

import parsl
from parsl.app.app import python_app
from parsl.config import Config
from parsl.dataflow.memoization import BasicMemoizer
from parsl.executors.threads import ThreadPoolExecutor


def local_config():
    return Config(
        executors=[ThreadPoolExecutor()],
        memoizer=BasicMemoizer()
    )


@python_app(cache=True)
def raise_exception_cache(x, cache=True):
    raise RuntimeError("exception from raise_exception_cache")


@python_app(cache=False)
def raise_exception_nocache(x, cache=True):
    raise RuntimeError("exception from raise_exception_nocache")


@pytest.mark.local
def test_python_memoization(n=2):
    """Test BasicMemoizer memoization of exceptions, with cache=True"""
    x = raise_exception_cache(0)

    # wait for x to be done
    x.exception()

    for i in range(0, n):
        fut = raise_exception_cache(0)

        # check that we get back the same exception object, rather than
        # a new one from a second invocation of raise_exception().
        assert fut.exception() is x.exception(), "Memoized exception should have been memoized"


@pytest.mark.local
def test_python_no_memoization(n=2):
    """Test BasicMemoizer non-memoization of exceptions, with cache=False"""
    x = raise_exception_nocache(0)

    # wait for x to be done
    x.exception()

    for i in range(0, n):
        fut = raise_exception_nocache(0)

        # check that we get back a different exception object each time
        assert fut.exception() is not x.exception(), "Memoized exception should have been memoized"