| 752 | |
| 753 | |
| 754 | async def test_circular_reference(send: Send) -> None: |
| 755 | self_dict: dict[str, object] = {} |
| 756 | self_dict["self"] = self_dict |
| 757 | with pytest.raises(ValueError, match="Circular reference detected"): |
| 758 | await send({"metadata": self_dict}) |
| 759 | |
| 760 | self_list: list[object] = [] |
| 761 | self_list.append(self_list) |
| 762 | with pytest.raises(ValueError, match="Circular reference detected"): |
| 763 | await send({"items": self_list}) |
| 764 | |
| 765 | inner: dict[str, object] = {} |
| 766 | indirect = {"a": [{"b": (inner,)}]} |
| 767 | inner["back"] = indirect |
| 768 | with pytest.raises(ValueError, match="Circular reference detected"): |
| 769 | await send(indirect) |
| 770 | |
| 771 | shared = {"a": 1, "l": [{"b": 2}]} |
| 772 | assert await send({"x": shared, "y": shared, "both": (shared, shared)}) == { |
| 773 | "x": shared, |
| 774 | "y": shared, |
| 775 | "both": [shared, shared], |
| 776 | } |
| 777 | |
| 778 | # freed generator items can have their `id()` reused by later ones; that must not read as a cycle |
| 779 | def rows() -> Iterator[dict[str, object]]: |
| 780 | for i in range(50): |
| 781 | yield {"k": [i]} |
| 782 | |
| 783 | def pages() -> Iterator[dict[str, object]]: |
| 784 | for _ in range(3): |
| 785 | yield {"x": rows()} |
| 786 | |
| 787 | assert await send({"items": pages()}) == {"items": [{"x": [{"k": [i]} for i in range(50)]} for _ in range(3)]} |
| 788 | |
| 789 | |
| 790 | class TestClient: |