File: 14_progress_handler.t

package info (click to toggle)
libdbd-sqlite3-perl 1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,004 kB
  • sloc: ansic: 167,715; perl: 1,788; pascal: 277; makefile: 9
file content (50 lines) | stat: -rw-r--r-- 1,429 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use strict;
use warnings;
use lib "t/lib";
use SQLiteTest qw/connect_ok @CALL_FUNCS/;
use Test::More;
use if -d ".git", "Test::FailWarnings";

my $N_OPCODES = 50; # how many opcodes before calling the progress handler

# our progress_handler just remembers how many times it was called
my $n_callback = 0;
sub progress_handler {
  $n_callback += 1;
  return 0;
}

foreach my $call_func (@CALL_FUNCS) {
	$n_callback = 0;  # reinitialize

	# connect and register the progress handler
	my $dbh = connect_ok( RaiseError => 1 );
	ok($dbh->$call_func( $N_OPCODES, \&progress_handler, "progress_handler" ));

	# populate a temporary table with random numbers
	$dbh->do( 'CREATE TEMP TABLE progress_test ( foo )' );
	$dbh->begin_work;
	for my $count (1 .. 1000) {
	  my $rand = rand;
	  $dbh->do( "INSERT INTO progress_test(foo) VALUES ( $rand )" );
	}
	$dbh->commit;

	# let the DB do some work (sorting the random numbers)
	my $result = $dbh->do( "SELECT * from progress_test ORDER BY foo  " );

	# now the progress handler should have been called a number of times
	ok($n_callback);

	# unregister the progress handler, set counter back to zero, do more work
	ok($dbh->$call_func( $N_OPCODES, undef, "progress_handler" ));
	$n_callback = 0;
	$result = $dbh->do( "SELECT * from progress_test ORDER BY foo DESC " );

	# now the progress handler should have been called zero times
	ok(!$n_callback);

	$dbh->disconnect;
}

done_testing;