summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9827492)
raw | patch | inline | side by side (parent: 9827492)
author | janw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Thu, 26 Feb 2009 14:49:17 +0000 (14:49 +0000) | ||
committer | janw <janw@594d385d-05f5-0310-b6e9-bd551577e9d8> | |
Thu, 26 Feb 2009 14:49:17 +0000 (14:49 +0000) |
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@13467 594d385d-05f5-0310-b6e9-bd551577e9d8
gosa-si/tests/sqlite-check-concurrency.pl | patch | blob | history |
index d6c4ce9c1458223c4ca6091d755f10d7cc842e22..dfd258340ea17aaf73650517fc06ca64c071519d 100755 (executable)
use warnings;
use DBI;
+use Data::Dumper;
use Time::HiRes qw(usleep);
use Fcntl ':flock';
use threads;
my %threads;
# Count of threads, if > 1 it corrupts the db
-my $count= 1;
+my $count= 10;
my $db_name= "./test.sqlite";
+my $lock = $db_name.".si.lock";
+if(stat($lock)) {
+ unlink($lock);
+}
-unlink $db_name;
-unlink $db_name.".si.lock";
+if(stat($db_name)) {
+ unlink($db_name)
+}
for(my $i=0;$i<$count;$i++) {
$threads{$i}= threads->create(\&check_database);
}
foreach my $thread (threads->list()) {
- print $thread->tid()."\n";
+ $thread->join();
}
sub check_database {
return;
}
-
sub new {
- my $class = shift;
- my $db_name = shift;
+ my $class = shift;
+ my $db_name = shift;
- my $lock = $db_name.".si.lock";
- # delete existing lock - instance should be running only once
- if(stat($lock)) {
- print STDERR "DEBUG: Removed existing lock file $lock.";
- unlink($lock);
- }
- my $self = {dbh=>undef,db_name=>undef,db_lock=>undef,db_lock_handle=>undef};
- my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name", "", "", {RaiseError => 0, AutoCommit => 0});
- $self->{dbh} = $dbh;
- $self->{db_name} = $db_name;
- $self->{db_lock} = $lock;
- bless($self,$class);
- return($self);
+ my $self = {dbh=>undef,db_name=>undef,db_lock=>undef,db_lock_handle=>undef};
+ my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name", "", "", {RaiseError => 1, AutoCommit => 1});
+ $self->{dbh} = $dbh;
+ $self->{db_name} = $db_name;
+ $self->{db_lock} = $lock;
+ bless($self,$class);
+ return($self);
}
sub lock {
- my $self = shift;
- open($self->{db_lock_handle}, ">>".($self->{db_lock})) unless ref $self->{db_lock_handle};
- flock($self->{db_lock_handle},LOCK_EX);
- seek($self->{db_lock_handle}, 0, 2);
+ my $self = shift;
+ open($self->{db_lock_handle}, ">>".($self->{db_lock})) unless ref $self->{db_lock_handle};
+ flock($self->{db_lock_handle},LOCK_EX);
+ seek($self->{db_lock_handle}, 0, 2);
}
sub unlock {
- my $self = shift;
- flock($self->{db_lock_handle},LOCK_UN);
+ my $self = shift;
+ flock($self->{db_lock_handle},LOCK_UN);
}
sub run_test {
my $table_name= shift;
my $sql= "CREATE TABLE IF NOT EXISTS $table_name (id INTEGER, value VARCHAR(255))";
$self->lock();
- $self->{dbh}->do($sql);
+ eval {
+ $self->{dbh}->do($sql);
+ };
+ if($@) {
+ print STDERR Dumper($@);
+ }
$self->unlock();
-
- for(my $i=0;$i<10000;$i++) {
- $sql= "INSERT INTO $table_name VALUES ($i, 'test $i')";
- $self->lock();
- $self->{dbh}->do($sql);
- $self->unlock();
+
+ for(my $i=0;$i<100;$i++) {
+ $sql= "INSERT INTO $table_name VALUES ($i, 'test $i')";
+ $self->lock();
+ eval {
+ $self->{dbh}->do($sql);
+ };
+ if($@) {
+ print STDERR Dumper($@);
+ }
+ $self->unlock();
}
}