This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update IPC-SysV to CPAN version 2.04
[perl5.git] / cpan / IPC-SysV / t / msg.t
CommitLineData
8f85282b
MHM
1################################################################################
2#
dd0df890 3# Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz <mhx@cpan.org>.
8f85282b
MHM
4# Version 1.x, Copyright (C) 1999, Graham Barr <gbarr@pobox.com>.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the same terms as Perl itself.
8#
9################################################################################
10
72802890 11BEGIN {
8f85282b 12 if ($ENV{'PERL_CORE'}) {
6edcbe38 13 chdir 't' if -d 't';
8f85282b
MHM
14 @INC = '../lib' if -d '../lib' && -d '../ext';
15 }
6edcbe38 16
8f85282b
MHM
17 require Test::More; import Test::More;
18 require Config; import Config;
6edcbe38 19
8f85282b
MHM
20 if ($ENV{'PERL_CORE'} && $Config{'extensions'} !~ m[\bIPC/SysV\b]) {
21 plan(skip_all => 'IPC::SysV was not built');
22 }
23}
6edcbe38 24
8f85282b
MHM
25if ($Config{'d_sem'} ne 'define') {
26 plan(skip_all => '$Config{d_sem} undefined');
27} elsif ($Config{'d_msg'} ne 'define') {
28 plan(skip_all => '$Config{d_msg} undefined');
72802890
JH
29}
30
0ade1984 31use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_NOWAIT IPC_STAT S_IRWXU S_IRWXG S_IRWXO);
8f85282b 32use strict;
0ade1984
JH
33
34use IPC::Msg;
35#Creating a message queue
36
8f85282b
MHM
37my $msq = sub {
38 my $code = shift;
39 if (exists $SIG{SYS}) {
40 local $SIG{SYS} = sub { plan(skip_all => "SIGSYS caught") };
41 return $code->();
42 }
43 return $code->();
44}->(sub { new IPC::Msg(IPC_PRIVATE, S_IRWXU | S_IRWXG | S_IRWXO) });
45
46unless (defined $msq) {
47 my $info = "IPC::Msg->new failed: $!";
503ba33a
MHM
48 if ($! == &IPC::SysV::ENOSPC || $! == &IPC::SysV::ENOSYS ||
49 $! == &IPC::SysV::ENOMEM || $! == &IPC::SysV::EACCES) {
8f85282b
MHM
50 plan(skip_all => $info);
51 }
52 else {
53 die $info;
54 }
55}
0ade1984 56
8f85282b
MHM
57plan(tests => 9);
58
59pass('create message queue');
0ade1984
JH
60
61#Putting a message on the queue
8f85282b 62my $test_name = 'enqueue message';
0ade1984 63
8f85282b
MHM
64my $msgtype = 1;
65my $msg = "hello";
66if ($msq->snd($msgtype,$msg,IPC_NOWAIT)) {
67 pass($test_name);
68}
69else {
70 print "# snd: $!\n";
71 fail($test_name);
72}
0ade1984 73
8f85282b
MHM
74#Check if there are messages on the queue
75my $ds = $msq->stat;
76ok($ds, 'stat');
0ade1984 77
8f85282b
MHM
78if ($ds) {
79 is($ds->qnum, 1, 'qnum');
80}
81else {
82 fail('qnum');
83}
0ade1984 84
8f85282b
MHM
85#Retrieving a message from the queue
86my $rmsg;
87my $rmsgtype = 0; # Give me any type
88$rmsgtype = $msq->rcv($rmsg,256,$rmsgtype,IPC_NOWAIT);
89is($rmsgtype, $msgtype, 'rmsgtype');
90is($rmsg, $msg, 'rmsg');
0ade1984 91
8f85282b
MHM
92$ds = $msq->stat;
93ok($ds, 'stat');
0ade1984 94
8f85282b
MHM
95if ($ds) {
96 is($ds->qnum, 0, 'qnum');
97}
98else {
99 fail('qnum');
100}
0ade1984 101
cab0dcf9 102END {
8f85282b 103 ok($msq->remove, 'remove message') if defined $msq;
cab0dcf9 104}