fix some test_weakref tests to not rely on ref-counting (closes #22267)
allow test to work on implementations not using ref-counting (closes #22265)