lalu has asked for the wisdom of the Perl Monks concerning the following question:

Hallo!

Description of intention

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.

Analogies to C++

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.

Problem description

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!

Code Example

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"; }); }

Replies are listed 'Best First'.
Re: testing of named pipes
by jcb (Parson) on Aug 26, 2019 at 22:55 UTC

    Are you trying to run this all in one process with purely event-driven I/O to communicate between the test driver and the FSM? If so, your problem seems to be that testSequence expects the FSM to have changed state after feeding it input but without having actually run the main event loop to allow the FSM to process its pending input.

    Another possibility is that a FIFO has a read end and a write end, and I am unsure if the same file descriptor can be used for both. Are you opening the named pipes twice, once for read and once for write?

      Hallo!

      Thank You for the quick reply. In fact, I suspected the Event loop to make some trouble, so I moved the &testSequence() execution into an own thread. And the data fed by &testSequence() into the pipe is read and processed by the FSM -- once the pipes are fed from the bash. I have also tried to put the &testSequence() asleep for 1s in its initialisation to make sure the Event loop is started when the pipes are fed: the outcome is negative.

      I open the pipes in "+<" mode, and already suspected this could be an issue. But since opening the pipes in &testSequence() did not seem to work and I did not get any error or warning when writing to the pipe, I was not alarmed.

      Based on Your input, I have now tried opening the pipes in "+>" mode in &testSequence again: it makes no difference. :-( It looks to me like if the Event::IO was not notified, when the pipe is fed from the same process. Could it be?

      Code Example

      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); } sub testSequence () { my $appControlFifo; my $deviceControlFifo; if ('' eq (-p "$sm->{config}->{deviceControl}" )) { POSIX::mkfifo ("$sm->{config}->{deviceControl}", 0700); } # shorthands for pipe handles open ($appControlFifo, "+>", $sm->{config}->{fifoControl}->{control +}) || die "Could not open status pipe for writing ($!)\n"; open ($deviceControlFifo, "+>", $sm->{config}->{deviceControl}) || +die "Could not open device pipe for writing ($!)\n"; $| = 1; $appControlFifo->autoflush(1); $deviceControlFifo->autoflush(1); # test sequence print $appControlFifo "connect to device\n"; # state transition: 01 + -> 02, currently automatically set print $appControlFifo "connect to device\n"; # keep current state print $appControlFifo "connect to device\n"; # keep current state print $appControlFifo "disconnect device\n"; print $deviceControlFifo "^MODE:1,1\n"; # state transition: 02 + -> 03 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 }

        Update: after explicitelly triggering the Event loop after each write to the pipes, things seem to work fine. I will make a more detailed check this evening.

        sub testSequence () { my $appControlFifo; my $deviceControlFifo; if ('' eq (-p "$sm->{config}->{deviceControl}" )) { POSIX::mkfifo ("$sm->{config}->{deviceControl}", 0700); } # shorthands for pipe handles open ($appControlFifo, "+>", $sm->{config}->{fifoControl}->{control +}) || die "Could not open status pipe for writing ($!)\n"; open ($deviceControlFifo, "+>", $sm->{config}->{deviceControl}) || +die "Could not open status pipe for writing ($!)\n"; $| = 1; $appControlFifo->autoflush(1); $deviceControlFifo->autoflush(1); # test sequence print $appControlFifo "connect to device\n"; # state transition: 01 + -> 02, currently automatically set Event::one_event(); print $deviceControlFifo "^MODE:1,1\n"; # state transition: 02 + -> 03 Event::one_event(); print "State 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 Event::one_event(); print $appControlFifo "internet connected\n"; # state transition: 0 +3 -> 04 Event::one_event(); }