Merge pull request #22 from jonas-schievink/droptest

Add a test ensuring that userdata is dropped
This commit is contained in:
kyren 2017-07-23 10:09:16 -04:00 committed by GitHub
commit d5ec09614c
1 changed files with 27 additions and 5 deletions

View File

@ -833,14 +833,36 @@ fn test_expired_userdata() {
hatch = self.userdata hatch = self.userdata
end }) end })
print("userdata = ", userdata)
print("hatch = ", hatch)
print "collecting..."
tbl = nil tbl = nil
userdata = nil -- make table and userdata collectable userdata = nil -- make table and userdata collectable
collectgarbage("collect") collectgarbage("collect")
print("userdata = ", userdata)
print("hatch = ", hatch)
hatch:access() hatch:access()
"#, None).unwrap(); "#, None).unwrap();
} }
#[test]
fn detroys_userdata() {
use std::sync::atomic::{Ordering, AtomicBool, ATOMIC_BOOL_INIT};
static DROPPED: AtomicBool = ATOMIC_BOOL_INIT;
struct Userdata;
impl LuaUserDataType for Userdata {}
impl Drop for Userdata {
fn drop(&mut self) {
DROPPED.store(true, Ordering::SeqCst);
}
}
let lua = Lua::new();
{
let globals = lua.globals();
globals.set("userdata", Userdata).unwrap();
}
assert_eq!(DROPPED.load(Ordering::SeqCst), false);
drop(lua); // should destroy all objects
assert_eq!(DROPPED.load(Ordering::SeqCst), true);
}