I am new to Perlmonks, which has provided me a great support during the last years since I have detected it. In fact, until now I did not have the necessity to post a question on my own, as everything I needed was already answered. :-)
I am writing a small service which state machine (FSM) uses two named pipes as control input and one other as outut. I want to test the FSM Perl module (which blesses itself from one state to one other) indipendently from the later application. A distinct test application shall be very close to the later service implementation and read the data from the named pipes: I can then use it as functional test as well as a reference implementation.
Talking in terms of C++, I am trying to write some kind of CPPUNIT test for my FSM using the final interfaces the service will use. Of course, it is still an option to use different applications to test the FSM on its own interface and the service interface separately. I know this would probably be a more reasonable way of testing, but as for now, I want to understand what limitation I am running into.
By manually feeding the named pipes on the shell, I know the test application reads and processes the data from both pipes correctly. But all my attempts to make the test application itself feed its own pipes failed: the piped data is apparently queued and processed only after a manual feed on the shell. As soon as I feed the single pipe from the shell, all data fed by the test application itself is read, too.
I do not really understand what is going on and am a little bit confused. Maybe someone can explain me how why the "auto-feed" of the named pipes does not work as expected?
Thank You in advance!
Posting a working example code would be very long and difficult to understand, so I limit the example code to the pipe opening and feeding. Here is a short sketch of the general setup:
#!/usr/bin/perl use diagnostics; use Data::Dumper; use statemachine; use Event qw (loop unloop); use Config::General; use Thread; use IO::Handle; # read config file to use from commandline my $cfg = Config::General->new ("inet.cfg"); my %tmp = $cfg->getall(); &initAppControl ($tmp{'Inet'}); # TODO # how to set sequences to test inside of the loop? Timer Event which +fires at deviceControl and appControl? # apparently, Event loop and "bidirectional named pipes" are in confl +ict with each other (pipes are blocked in reading), so testing is cur +rently moved into a shell script print "Errors found in initialization: $!\n\n"; #### TESTING STATE MACHINE ### FSM only has direct access to the internet control device ### all other things must be handled by the handling script (PPP/UDEV +events) my $sm = new statemachine( {config => $tmp{'Inet'} }); $sm->start(); my $cfd = $sm->{config}->{fifo}->{appControl}->{control}; print Dumper $sm; # set Event watchers on APP CONTROL # mapping commands -> FSM my $ctrlWatch = Event->io ( fd => ${$sm->{config}->{fifo}->{appControl}->{control}}, cb => sub { my $str = readline ($sm->{config}->{fifo}->{appControl}->{contro +l}); my $fd = $sm->{config}->{fifo}->{appControl}->{status}; # check command if ($str =~ m/connect to device/i) { print "CONNECT TO DEVICE CMD\n"; $sm->connectDevice (); } elsif ($str =~ m/disconnect device/i) { print "DISCONNECT DEVICE CMD\n"; $sm->disconnectDevice (); } elsif ($str =~ m/connect to internet/i) { print "CONNECT TO INTERNET CMD\n"; $sm->connectInternetStart (); } elsif ($str =~ m/internet connected/i) { print "INTERNET CONNECTED CMD\n"; $sm->connectInternetComplete (); } elsif ($str =~ m/disconnect internet/i) { print "DISCONNECT INTERNET CMD\n"; $sm->connectInternetStop (); } print "EEEEEEE $str\n"; print $fd "EEEEEEE $str\n"; }); my $testsequenceThread = Thread->new (\&testSequence); $testsequenceThread->join (); loop; exit; sub testSequence () { # shorthands for pipe handles my $appControlFifo = $sm->{config}->{fifo}->{appControl}->{control} +; my $deviceControlFifo = $sm->{config}->{fifo}->{deviceControl}; # autoflush pipes $| = 1; select ($appControlFifo); $| = 1; select ($deviceControlFifo); $| = 1; # test sequence print $appControlFifo "connect to device\n"; # state transition: 01 + -> 02, currently automatically set print $deviceControlFifo "^MODE:1,1\n"; # state transition: 02 + -> 03 # execution stops HERE unless pipe is not fed from Bash print "Instance>>> " . $sm->Instance . "\n"; if ($sm->Instance() =~ "Statemachine::03") # check if transition +succeeded (kind of ASSERT) { print "transition succeeded\n"; } print $appControlFifo "connect to internet\n"; # state transition: +03 -> 04 print $appControlFifo "internet connected\n"; # state transition: 0 +3 -> 04 } sub initAppControl () { my $config = shift; $config->{deviceControlCallback} = \&addDeviceControlCB; # create and open control FIFO if ('' eq (-p "$config->{fifoControl}->{status}" )) { POSIX::mkfifo ("$config->{fifoControl}->{status}", 0700); } if ('' eq (-p "$config->{fifoControl}->{control}" )) { POSIX::mkfifo ("$config->{fifoControl}->{control}", 0700); } # open pipes, set status pipe to "autoflush" open ($config->{fifo}->{appControl}->{control}, "+<", $config->{fif +oControl}->{control}) || die "Could not open status pipe ($!)\n"; open ($config->{fifo}->{appControl}->{status}, "+>", $config->{fifo +Control}->{status}) || die "Could not open control pipe ($!)\n"; $config->{fifo}->{appControl}->{status}->autoflush(1); } # callback for FSM when device is made available # mapping device events from deviceControl -> FSM sub addDeviceControlCB() { my $state = shift; # TODO Event shall be created only if deviceControl is available # -> move it into FSM? Possibly as a callback to FSM? # set Event watches of DEVICE CONTROL my $eventWatch = Event-> io ( fd => ${$sm->{config}->{fifo}->{deviceControl}}, cb => sub { my $str = readline ($sm->{config}->{fifo}->{deviceControl}); my $fd = $sm->{config}->{fifo}->{appControl}->{status}; # check status if ($str =~ m/\^MODE:(.*),(.*)/i) { if (($1 ne 0) && ($2 ne 0)) { print "devCtrl: LINK AVAILABLE ...\n"; # notify FSM $sm->setLink(); } else { print "devCtrl: LINK DROPPED\n"; # notify FSM $sm->clearLink(); } } elsif ($str =~ m/\^RSSI:(.*)/i) { if ($1 ne 0) { print "devCtrl: SIGNAL AVAILABLE\n"; # notify FSM $sm->setSignalStrength(); } else { print "devCtrl: SIGNAL DROPPED\n"; # notify FSM $sm->clearSignalStrength(); } } print "FFFFF $str\n"; print $fd "FFFFF $str\n"; }); }
In reply to testing of named pipes by lalu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |