MCE

 view release on metacpan or  search on metacpan

t/04_channel_threads.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use open qw(:std :utf8);

use Test::More;

BEGIN {
   plan skip_all => "Not used on Cygwin" if ( $^O eq 'cygwin' );

   if ( $] lt '5.010001' && $^O ne 'MSWin32' ) {
      plan skip_all => "old Perl and threads not supported on Unix platforms";
   }
   eval 'use threads'; ## no critic
   plan skip_all => "threads not available" if $@;

   use_ok 'MCE::Channel';
   use_ok 'MCE::Channel::Threads';
}

## https://sacred-texts.com/cla/usappho/sph02.htm (V)

my $sappho_text =
  "κὤττι μοι μάλιστα θέλω γένεσθαι
   μαινόλᾳ θύμῳ, τίνα δηὖτε πείθω
   μαῖσ ἄγην ἐσ σὰν φιλότατα τίσ τ, ὦ
   Πσάπφ᾽, ἀδίκηει;";

my $translation =
  "What in my mad heart was my greatest desire,
   Who was it now that must feel my allurements,
   Who was the fair one that must be persuaded,
   Who wronged thee Sappho?";

my $come_then_i_pray = "さあ、私は祈る" . "Ǣ";


my $chnl = MCE::Channel->new( impl => 'Threads' );
is $chnl->impl(), 'Threads', 'implementation name';

# send recv
{
   $chnl->send('a string');
   is $chnl->recv, 'a string', 'send recv scalar';

   $chnl->send($sappho_text);
   is $chnl->recv, $sappho_text, 'send recv utf8';

   $chnl->send($come_then_i_pray);
   is $chnl->recv, $come_then_i_pray, 'send recv utf8_ja';

   $chnl->send(qw/ a list of arguments /);
   is scalar( my @args = $chnl->recv ), 4, 'send recv list';

   $chnl->send({ complex => 'structure' });
   is ref( $chnl->recv ), 'HASH', 'send recv complex';
}

# send recv_nb
if ($^O ne 'MSWin32')
{
   $chnl->send('a string');
   is $chnl->recv_nb, 'a string', 'send recv_nb scalar';

   $chnl->send($sappho_text);
   is $chnl->recv_nb, $sappho_text, 'send recv_nb utf8';

   $chnl->send($come_then_i_pray);
   is $chnl->recv_nb, $come_then_i_pray, 'send recv_nb utf8_ja';

   $chnl->send(qw/ a list of arguments /);



( run in 2.983 seconds using v1.01-cache-2.11-cpan-5e09290becf )