DBIx-Custom / t / mysql-async-opt.t /
Newer Older
96 lines | 1.933kb
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
1
use Test::More;
2
use strict;
3
use warnings;
4
use utf8;
5

            
6
use FindBin;
7
use DBIx::Custom;
8

            
9
my $dbi;
10
my $dsn;
11
my $args;
12
my $user = 'dbix_custom';
13
my $password = 'dbix_custom';
14
my $database = 'dbix_custom';
15

            
16
$dsn = "dbi:mysql:database=$database";
17
$args = {dsn => $dsn, user => $user, password => $password,};
18

            
19
plan skip_all => 'mysql private test' unless -f "$FindBin::Bin/run/mysql-async-opt.run"
20
  && eval { $dbi = DBIx::Custom->connect($args); 1 };
21
plan 'no_plan';
22

            
23
$SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /DEPRECATED/};
24

            
25
# Function for test name
26
sub test { print "# $_[0]\n" }
27

            
28
# Varialbes for tests
29
my $dbname;
30
my $row;
31
my $rows;
32
my $result;
33
my $result2;
34
my $model;
35
my $dbi1;
36
my $dbi2;
37
my $dbi3;
38
my @dbis;
39
my @results;
40

            
41
test 'connect';
42
eval {
43
  $dbi = DBIx::Custom->connect(
44
    dsn => "dbi:mysql:database=$database;",
45
    user => $user,
46
    password => $password
47
  );
48
};
49
ok(!$@);
50

            
51
eval { $dbi->do('drop table table1') };
52
$dbi->do('create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB');
53
$dbi->insert({key1 => 1, key2 => 2}, table => 'table1');
54

            
55
test 'async test';
56

            
57
require AnyEvent;
58

            
59
my $cond = AnyEvent->condvar;
60

            
61
my $timer = AnyEvent->timer(
62
  interval => 1,
63
  cb => sub {
64
    1;
65
  }
66
);
67

            
68
my $count = 0;
69

            
70
$dbi->execute('SELECT SLEEP(1), 3', undef,
71
  prepare_attr => {async => 1}, select => 1,
72
  async => sub {
73
    my ($dbi, $result) = @_;
74
    my $row = $result->fetch_one;
75
    is($row->[1], 3, 'before');
76
    $cond->send if ++$count == 2;
77
  }
78
);
79

            
80
$dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
81
  async => sub {
82
    my ($dbi, $result) = @_;
83
    my $row = $result->fetch_one;
84
    is($row->[0], 1, 'after1');
85
    $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
86
      async => sub {
87
        my ($dbi, $result) = @_;
88
        my $row = $result->fetch_one;
89
        is($row->[0], 1, 'after2');
90
        $cond->send if ++$count == 2;
91
      }
92
    )
93
  }
94
);
95

            
96
$cond->recv;