86 lines
2.7 KiB
Plaintext
86 lines
2.7 KiB
Plaintext
# Tests of the owners shared by every copy: `rc`, counted plainly, and `arc`,
|
|||
|
|
# counted atomically, with the weak references that do not keep their values.
|
||
|
|
# Each closes what it holds exactly once, where its last owner closes.
|
||
|
|
import std::containers;
|
||
|
|
import std::testing;
|
||
|
|
import std::thread;
|
||
|
|
|
||
|
|
|
||
|
|
# How many values the tests gave owners have been closed, from any thread.
|
||
|
|
var owned_closed: atomic<u64>;
|
||
|
|
|
||
|
|
struct owned : owning {
|
||
|
|
var mark: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
def owned::close(&mut this): void {
|
||
|
|
if this.mark != 0 {
|
||
|
|
_ = owned_closed.fetch_add(1);
|
||
|
|
this.mark = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def counted_owners_and_maybe_owners_are_one_word(t: &mut tester): void {
|
||
|
|
t.expects(size_of::<rc<owned>>() == 8 && size_of::<maybe<rc<owned>>>() == 8);
|
||
|
|
t.expects(size_of::<arc<owned>>() == 8 && size_of::<maybe<arc<owned>>>() == 8);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def rc_copies_share_one_value_the_last_closes(t: &mut tester): void {
|
||
|
|
val before := owned_closed.load();
|
||
|
|
var first := rc::<owned>::new(owned { mark = 1 });
|
||
|
|
var weak := first.weak();
|
||
|
|
{
|
||
|
|
var second := copy(&first);
|
||
|
|
second.get().mark = 5;
|
||
|
|
t.expects(first.count() == 2 && first.get().mark == 5);
|
||
|
|
}
|
||
|
|
t.expects(first.count() == 1 && owned_closed.load() == before);
|
||
|
|
if weak.lock() is some(&locked) {
|
||
|
|
t.expects(locked.count() == 2 && locked.get().mark == 5);
|
||
|
|
} else {
|
||
|
|
t.expects(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
var weaker := copy(&weak);
|
||
|
|
first.close();
|
||
|
|
t.expects(owned_closed.load() == before + 1);
|
||
|
|
t.expects(!(weak.lock() is some) && !(weaker.lock() is some));
|
||
|
|
|
||
|
|
# Moving an owner moves the count's claim with it.
|
||
|
|
var moved_from := rc::<owned>::new(owned { mark = 7 });
|
||
|
|
var moved_to := move(&mut moved_from);
|
||
|
|
moved_from.close();
|
||
|
|
t.expects(moved_to.count() == 1 && owned_closed.load() == before + 1);
|
||
|
|
moved_to.close();
|
||
|
|
t.expects(owned_closed.load() == before + 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
@test def arc_copies_are_counted_from_many_threads(t: &mut tester): void {
|
||
|
|
val before := owned_closed.load();
|
||
|
|
var shared := arc::<owned>::new(owned { mark = 9 });
|
||
|
|
val weak := shared.weak();
|
||
|
|
val from: &arc<owned> = &shared;
|
||
|
|
var workers := vector::<thread::handle<void>>::new();
|
||
|
|
for _ in 0..4u {
|
||
|
|
var started := thread::spawn(def() {
|
||
|
|
for _ in 0..2000u {
|
||
|
|
val held := copy(from);
|
||
|
|
if held.get().mark != 9 { return; }
|
||
|
|
}
|
||
|
|
}) else (e) { t.expects(false); return; };
|
||
|
|
workers.append(move(&mut started));
|
||
|
|
}
|
||
|
|
for i in 0..workers.length {
|
||
|
|
workers[i].join();
|
||
|
|
}
|
||
|
|
t.expects(shared.count() == 1 && owned_closed.load() == before);
|
||
|
|
|
||
|
|
var last := copy(&shared);
|
||
|
|
shared.close();
|
||
|
|
t.expects(last.count() == 1 && owned_closed.load() == before);
|
||
|
|
if weak.lock() is some(&locked) {
|
||
|
|
t.expects(locked.get().mark == 9);
|
||
|
|
}
|
||
|
|
last.close();
|
||
|
|
t.expects(owned_closed.load() == before + 1 && !(weak.lock() is some));
|
||
|
|
}
|