Is there a way to force all finalizers to run synchronously?

I’m trying to unit test some logic around finalizer code, but in order
to do so, I need to know when all finalizers that can run have run.
That way, I can tell if the effects of the finalizers that should have
been realized, are in fact realized.

Thus far I’ve had no luck (on MRI 2.2), and the finalizers always seem
to run “later”

To be clear, I have no interest in forcing GC and finalizers to run
anywhere else except in the unit tests, but the finalization logic is
nuanced, and so I’m really not comfortable leaving it untested.

V8 provides a method just for this purpose, and it is really handy, but I haven’t
been able to find anything equally reliable in Ruby.

So my question is, how can I force a complete and synchronous run of all
GC, and all finalizers? Could this be done with a C extension even if
there’s no public API do it with?

Here is a link to a script demonstrating the problem

thanks in advance!