From 6feca0d6408104dd827365e445785b3813309f4a Mon Sep 17 00:00:00 2001 From: janw Date: Wed, 4 Mar 2009 08:52:19 +0000 Subject: [PATCH] Added database tests to verify malforming of sqlite databases. git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6-lhm@13488 594d385d-05f5-0310-b6e9-bd551577e9d8 --- .../tests/run_sqlite-check-concurrency.sh | 8 ++ .../gosa-si/tests/sqlite-check-concurrency.pl | 93 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100755 trunk/gosa-si/tests/run_sqlite-check-concurrency.sh create mode 100755 trunk/gosa-si/tests/sqlite-check-concurrency.pl diff --git a/trunk/gosa-si/tests/run_sqlite-check-concurrency.sh b/trunk/gosa-si/tests/run_sqlite-check-concurrency.sh new file mode 100755 index 000000000..f098555ec --- /dev/null +++ b/trunk/gosa-si/tests/run_sqlite-check-concurrency.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +rm test.sqlite* + +for (( i=1; $i <= 10; i++ )) +do + ./sqlite-check-concurrency.pl & +done diff --git a/trunk/gosa-si/tests/sqlite-check-concurrency.pl b/trunk/gosa-si/tests/sqlite-check-concurrency.pl new file mode 100755 index 000000000..9f03c66c7 --- /dev/null +++ b/trunk/gosa-si/tests/sqlite-check-concurrency.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl -W + +package GOSA::DBsqlite; +use strict; +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= 10; +my $db_name= "./test.sqlite"; +my $lock = $db_name.".si.lock"; + +#if(stat($lock)) { +# unlink($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()) { + $thread->join(); +} + +sub check_database { + $threads{threads->self->tid()}= GOSA::DBsqlite->new($db_name); + threads->yield(); + $threads{threads->self->tid()}->run_test("test"); + + return; +} + +sub new { + my $class = shift; + my $db_name = shift; + + 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); +} + + +sub unlock { + my $self = shift; + flock($self->{db_lock_handle},LOCK_UN); +} + +sub run_test { + my $self= shift; + my $table_name= shift; + my $sql= "CREATE TABLE IF NOT EXISTS $table_name (id INTEGER PRIMARY KEY, status VARCHAR(255) DEFAULT 'none')"; + $self->lock(); + eval { + $self->{dbh}->do($sql); + }; + if($@) { + print STDERR Dumper($@); + } + $self->unlock(); + + for(my $i=0;$i<100;$i++) { + $sql= "INSERT INTO $table_name (id, status) VALUES (null, 'test $i')"; + $self->lock(); + eval { + $self->{dbh}->do($sql); + }; + if($@) { + print STDERR Dumper($@); + } + $self->unlock(); + } +} -- 2.30.2