(self)
| 1950 | |
| 1951 | @threading_helper.requires_working_threading() |
| 1952 | def test_lru_cache_threaded(self): |
| 1953 | n, m = 5, 11 |
| 1954 | def orig(x, y): |
| 1955 | return 3 * x + y |
| 1956 | f = self.module.lru_cache(maxsize=n*m)(orig) |
| 1957 | hits, misses, maxsize, currsize = f.cache_info() |
| 1958 | self.assertEqual(currsize, 0) |
| 1959 | |
| 1960 | start = threading.Event() |
| 1961 | def full(k): |
| 1962 | start.wait(10) |
| 1963 | for _ in range(m): |
| 1964 | self.assertEqual(f(k, 0), orig(k, 0)) |
| 1965 | |
| 1966 | def clear(): |
| 1967 | start.wait(10) |
| 1968 | for _ in range(2*m): |
| 1969 | f.cache_clear() |
| 1970 | |
| 1971 | orig_si = sys.getswitchinterval() |
| 1972 | support.setswitchinterval(1e-6) |
| 1973 | try: |
| 1974 | # create n threads in order to fill cache |
| 1975 | threads = [threading.Thread(target=full, args=[k]) |
| 1976 | for k in range(n)] |
| 1977 | with threading_helper.start_threads(threads): |
| 1978 | start.set() |
| 1979 | |
| 1980 | hits, misses, maxsize, currsize = f.cache_info() |
| 1981 | if self.module is py_functools: |
| 1982 | # XXX: Why can be not equal? |
| 1983 | self.assertLessEqual(misses, n) |
| 1984 | self.assertLessEqual(hits, m*n - misses) |
| 1985 | else: |
| 1986 | self.assertEqual(misses, n) |
| 1987 | self.assertEqual(hits, m*n - misses) |
| 1988 | self.assertEqual(currsize, n) |
| 1989 | |
| 1990 | # create n threads in order to fill cache and 1 to clear it |
| 1991 | threads = [threading.Thread(target=clear)] |
| 1992 | threads += [threading.Thread(target=full, args=[k]) |
| 1993 | for k in range(n)] |
| 1994 | start.clear() |
| 1995 | with threading_helper.start_threads(threads): |
| 1996 | start.set() |
| 1997 | finally: |
| 1998 | sys.setswitchinterval(orig_si) |
| 1999 | |
| 2000 | @threading_helper.requires_working_threading() |
| 2001 | def test_lru_cache_threaded2(self): |
nothing calls this directly
no test coverage detected