shlist

share and manage lists between multiple people
Log | Files | Refs

commit 5f586c2e549f74d663d3d0e6e585452a2e26ca79
parent 403f9e4034f93a36d9e202cc2a767c6d6a874ddb
Author: kyle <kyle@getaddrinfo.net>
Date:   Mon, 19 Oct 2015 21:28:15 -0600

sl: a few tweaks to make it easier to test

- don't bring up the network socket until really late
- add a -d option to specify a different database to use

Both of these play a role in the test suite running it's own server such that we
never accidentally run the test script against a production instance.

The test script clears the database :)

Diffstat:
Msl | 51++++++++++++++++++++++++++++-----------------------
1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/sl b/sl @@ -8,6 +8,7 @@ use DBI; use Digest::SHA qw(sha256_base64); use Getopt::Std; use IO::Socket qw(getnameinfo NI_NUMERICHOST NI_NUMERICSERV); +use MsgTypes; use Scalar::Util qw(looks_like_number); use Socket; @@ -17,36 +18,33 @@ my $LOG_LEVEL_INFO = 2; my $LOG_LEVEL_DEBUG = 3; my $LOG_LEVEL = $LOG_LEVEL_INFO; -do 'msg_types.pl'; - -# print ">>> " . $MSG_NEW_LIST . "\n"; +print $msgs{0}; my %args; -getopts("p:", \%args); +# -p is port, -d is database file +getopts("p:d:", \%args); + +my $db_file = "db"; +if ($args{d}) { + $db_file = $args{d}; + # random hack, when -d is given also disable output buffering + $| = 1; +} +print "info: creating new database '$db_file'\n" unless (-e $db_file); my $dbh = DBI->connect( - "dbi:SQLite:dbname=db", + "dbi:SQLite:dbname=$db_file", "", "", { RaiseError => 1 } ) or die $DBI::errstr; -# enable transactions, if possible +# our transaction scheme needs for this to be on $dbh->{AutoCommit} = 1; +# create any new tables, if needed create_tables($dbh); -my $sock = new IO::Socket::INET ( - LocalHost => '0.0.0.0', - LocalPort => $args{p} || '5437', - Proto => 'tcp', - Listen => 1, - Reuse => 1, -); - -die "Could not create socket: $!\n" unless $sock; -my $local_addr_port = inet_ntoa($sock->sockaddr) . ":" .$sock->sockport(); - # list table queries my $sql = qq{insert into lists (list_id, name, first_created, last_updated) values (?, ?, ?, ?)}; @@ -130,6 +128,17 @@ my @msg_handlers = ( # make sure children get reaped :) $SIG{CHLD} = 'IGNORE'; +my $sock = new IO::Socket::INET ( + LocalHost => '0.0.0.0', + LocalPort => $args{p} || '5437', + Proto => 'tcp', + Listen => 1, + Reuse => 1, +); + +die "Could not create socket: $!\n" unless $sock; +my $local_addr_port = inet_ntoa($sock->sockaddr) . ":" .$sock->sockport(); + print "info: ready for connections on $local_addr_port\n"; while (my ($new_sock, $bin_addr) = $sock->accept()) { @@ -139,14 +148,10 @@ while (my ($new_sock, $bin_addr) = $sock->accept()) { } my $pid = fork(); - - if (! defined $pid ) { - die "error: can't fork: $!\n"; - } + die "error: can't fork: $!\n" if (!defined $pid); if ($pid) { # parent goes back to listening for more connections - info("forked child $pid\n"); close $new_sock; next; } @@ -206,7 +211,7 @@ while (my ($new_sock, $bin_addr) = $sock->accept()) { } # we read more bytes than we were expecting, keep going } - print "info: $addr: received msg type $msg_type, $msg_size bytes\n"; + print "info: $addr: received msg type $msgs{$msg_type}, $msg_size bytes\n"; $child_dbh->begin_work; # call the appropriate handler