use
if
$Config
{usethreads},
'threads'
;
BEGIN { plan
skip_all
=>
'requires threads'
unless
$Config
{usethreads} }
use
MongoDBTest
qw/skip_unless_mongod build_client get_test_db/
;
skip_unless_mongod();
my
$conn
= build_client();
my
$testdb
= get_test_db(
$conn
);
my
$col
=
$testdb
->get_collection(
'kooh'
);
$col
->drop;
{
my
$ret
=
try
{
threads->create(
sub
{
$conn
->reconnect;
$col
->insert_one({
foo
=> 42 })->inserted_id;
})->
join
->value;
}
catch
{
diag
$_
;
};
ok
$ret
,
'we survived destruction of a cloned connection'
;
my
$o
=
$col
->find_one({
foo
=> 42 });
is
$ret
,
$o
->{_id},
'we inserted and joined the OID back'
;
}
{
my
(
$n_threads
,
$n_inserts
) =
$ENV
{AUTOMATED_TESTING} ? (10,1000) : (5, 100);
note
"inserting $n_inserts items each in $n_threads threads"
;
my
@threads
=
map
{
threads->create(
sub
{
$conn
->reconnect;
my
$col
=
$conn
->get_database(
$testdb
->name)->get_collection(
'kooh'
);
map
{
$col
->insert_one({
foo
=> threads->self->tid })->inserted_id } 1 ..
$n_inserts
;
})
} 1 ..
$n_threads
;
my
@vals
=
map
{ (
$_
->tid ) x
$n_inserts
}
@threads
;
my
@ids
=
map
{
$_
->
join
}
@threads
;
my
$expected
=
scalar
@ids
;
is
scalar
keys
%{ {
map
{ (
$_
=>
undef
) }
@ids
} },
$expected
,
"we got $expected unique OIDs"
;
is_deeply(
[
map
{
$col
->find_one({
_id
=>
$_
})->{foo} }
@ids
],
[
@vals
],
'right values inserted from threads'
,
);
}
done_testing();