Skip to content

Commit

Permalink
Test callback lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
arg0d committed Sep 13, 2024
1 parent 9eafe14 commit 3ea5ab3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions fixtures/callbacks/tests/bindings/test_callbacks.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,11 @@ listOf(1, 2).forEach { v ->
assert(expected == observed) { "callback is sent on construction: $expected != $observed" }
}
rustStringifier.destroy()

// `stringifier` must remain valid after `rustStringifier2` drops the reference
val stringifier = StoredKotlinStringifier()
val rustStringifier1 = RustStringifier(stringifier)
val rustStringifier2 = RustStringifier(stringifier)
assert("kotlin: 123" == rustStringifier2.fromSimpleType(123))
rustStringifier2.destroy()
assert("kotlin: 123" == rustStringifier1.fromSimpleType(123))
10 changes: 10 additions & 0 deletions fixtures/callbacks/tests/bindings/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,14 @@ def test_complex_errors(self):
rust_getters.get_option(callback, "unexpected-error", True)
self.assertEqual(cm.exception.reason, repr(ValueError("unexpected value")))

class TestCallbackLifetime(unittest.TestCase):
def test_callback_reference_does_not_invalidate_other_references(self):
# `stringifier` must remain valid after `rust_stringifier_2` drops the reference
stringifier = StoredPythonStringifier()
rust_stringifier_1 = RustStringifier(stringifier)
rust_stringifier_2 = RustStringifier(stringifier)
assert("python: 123" == rust_stringifier_2.from_simple_type(123))
del rust_stringifier_2
assert("python: 123" == rust_stringifier_1.from_simple_type(123))

unittest.main()
10 changes: 10 additions & 0 deletions fixtures/callbacks/tests/bindings/test_callbacks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ do {
assert(expected == observed, "callback is sent on construction: \(expected) != \(observed)")
}

do {
// `stringifier` must remain valid after `rustStringifier2` drops the reference
let stringifier = StoredSwiftStringifier()
let rustStringifier1 = RustStringifier(callback: stringifier)
do {
let rustStringifier2 = RustStringifier(callback: stringifier)
assert("swift: 123" == rustStringifier2.fromSimpleType(value: 123))
}
assert("swift: 123" == rustStringifier1.fromSimpleType(value: 123))
}

// 3. Error handling
do {
Expand Down

0 comments on commit 3ea5ab3

Please sign in to comment.