Fix pesky on_connect_* race condition abraxxa++ ilmari++
[dbsrgits/DBIx-Class.git] / t / storage / on_connect_do.t
CommitLineData
579ca3f7 1use strict;
2use warnings;
3
f3d405dc 4# !!! do not replace this with done_testing - tests reside in the callbacks
5# !!! number of calls is important
6c925c72 6use Test::More tests => 13;
f3d405dc 7# !!!
6c925c72 8use Test::Warn;
f3d405dc 9use Test::Exception;
579ca3f7 10
11use lib qw(t/lib);
8d6b1478 12use DBICTest;
7c9aa29f 13require DBI;
579ca3f7 14
15
16my $schema = DBICTest->init_schema(
17 no_connect => 1,
18 no_deploy => 1,
19);
00f57441 20
21ok $schema->connection(
22 DBICTest->_database,
23 {
24 on_connect_do => 'CREATE TABLE TEST_empty (id INTEGER)',
25 },
26), 'connection()';
27
28is_deeply (
29 $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
30 [],
31 'string version on_connect_do() worked'
32);
33
34$schema->storage->disconnect;
35
579ca3f7 36ok $schema->connection(
6c925c72 37 sub { DBI->connect(DBICTest->_database, undef, undef, { AutoCommit => 0 }) },
579ca3f7 38 {
1eb87dd7 39 # DO NOT REMOVE - this seems like an unrelated piece of info,
40 # but is in fact a test for a bug where setting an accessor-via-option
41 # would trigger an early connect *bypassing* the on_connect_* pieces
42 cursor_class => 'DBIx::Class::Storage::Cursor',
43
1bd1640b 44 on_connect_do => [
45 'CREATE TABLE TEST_empty (id INTEGER)',
46 [ 'INSERT INTO TEST_empty VALUES (?)', {}, 2 ],
1dafdb2a 47 \&insert_from_subref,
1bd1640b 48 ],
579ca3f7 49 on_disconnect_do =>
50 [\&check_exists, 'DROP TABLE TEST_empty', \&check_dropped],
51 },
52), 'connection()';
53
6c925c72 54warnings_exist {
55 $schema->storage->ensure_connected
56} qr/The 'RaiseError' of the externally supplied DBI handle is set to false/,
57'Warning on clobbered AutoCommit => 0 fired';
58
00f57441 59is_deeply (
1bd1640b 60 $schema->storage->dbh->selectall_arrayref('SELECT * FROM TEST_empty'),
1dafdb2a 61 [ [ 2 ], [ 3 ], [ 7 ] ],
00f57441 62 'on_connect_do() worked'
63);
f3d405dc 64dies_ok {
65 $schema->storage->dbh->do('SELECT 1 FROM TEST_nonexistent');
66} 'Searching for nonexistent table dies';
579ca3f7 67
68$schema->storage->disconnect();
69
abe38018 70my($connected, $disconnected, @cb_args);
6d2e7a96 71ok $schema->connection(
72 DBICTest->_database,
73 {
abe38018 74 on_connect_do => sub { $connected = 1; @cb_args = @_; },
6d2e7a96 75 on_disconnect_do => sub { $disconnected = 1 },
76 },
77), 'second connection()';
78$schema->storage->dbh->do('SELECT 1');
79ok $connected, 'on_connect_do() called after connect()';
80ok ! $disconnected, 'on_disconnect_do() not called after connect()';
81$schema->storage->disconnect();
82ok $disconnected, 'on_disconnect_do() called after disconnect()';
83
abe38018 84isa_ok($cb_args[0], 'DBIx::Class::Storage', 'first arg to on_connect_do hook');
6892eb09 85@cb_args = ();
6d2e7a96 86
579ca3f7 87sub check_exists {
88 my $storage = shift;
89 ok $storage->dbh->do('SELECT 1 FROM TEST_empty'), 'Table still exists';
1dafdb2a 90 return;
579ca3f7 91}
92
93sub check_dropped {
94 my $storage = shift;
f3d405dc 95
96 dies_ok {
97 $storage->dbh->do('SELECT 1 FROM TEST_empty');
98 } 'Reading from dropped table fails';
1dafdb2a 99 return;
100}
101
102sub insert_from_subref {
103 my $storage = shift;
104 return [
105 [ 'INSERT INTO TEST_empty VALUES (?)', {}, 3 ],
106 'INSERT INTO TEST_empty VALUES (7)',
107 ];
579ca3f7 108}