code: def test_heapset_pickle():
      """Test pickle roundtrip for a HeapSet.
  
      Note
      ----
      To make this test work with plain pickle and not need cloudpickle, we had to avoid
      lambdas and local classes in our test. Here we're testing that HeapSet doesn't add
      lambdas etc. of its own.
      """
      heap = HeapSet(key=operator.attrgetter("i"))
  
      # The heap contains broken weakrefs
      for i in range(200):
          c = C(f"y{i}", random.random())
          heap.add(c)
          if random.random() > 0.7:
              heap.remove(c)
  
      list(heap.sorted())  # trigger sort
      assert heap._sorted
      heap2 = pickle.loads(pickle.dumps(heap))
      assert len(heap) == len(heap2)
      assert (
          not heap2._sorted
      )  # re-heapification may have broken the sort
      # Test that the heap has been re-heapified upon unpickle
      assert len(heap2._heap) < len(heap._heap)
      while heap:
          assert heap.pop() == heap2.pop()
  
imports: from __future__ import annotations
  import heapq
  import operator
  import pickle
  import random
  from collections.abc import Mapping
  import pytest
  from distributed.collections import LRU, HeapSet, sum_mappings
label_desc: Use image node_id 7 for calling a global function with example usage: test_heapset_pickle() without return types