summaryrefslogtreecommitdiff
path: root/cpan/IPC-SysV/t/msg.t
blob: 32dd9ffa75230e66cc91c4c26e9e579d46632319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
################################################################################
#
#  $Revision: 11 $
#  $Author: mhx $
#  $Date: 2008/11/28 18:08:11 +0100 $
#
################################################################################
#
#  Version 2.x, Copyright (C) 2007, Marcus Holland-Moritz <mhx@cpan.org>.
#  Version 1.x, Copyright (C) 1999, Graham Barr <gbarr@pobox.com>.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the same terms as Perl itself.
#
################################################################################

BEGIN {
  if ($ENV{'PERL_CORE'}) {
    chdir 't' if -d 't';
    @INC = '../lib' if -d '../lib' && -d '../ext';
  }

  require Test::More; import Test::More;
  require Config; import Config;

  if ($ENV{'PERL_CORE'} && $Config{'extensions'} !~ m[\bIPC/SysV\b]) {
    plan(skip_all => 'IPC::SysV was not built');
  }
}

if ($Config{'d_sem'} ne 'define') {
  plan(skip_all => '$Config{d_sem} undefined');
} elsif ($Config{'d_msg'} ne 'define') {
  plan(skip_all => '$Config{d_msg} undefined');
}

use IPC::SysV qw(IPC_PRIVATE IPC_RMID IPC_NOWAIT IPC_STAT S_IRWXU S_IRWXG S_IRWXO);
use strict;

use IPC::Msg;
#Creating a message queue

my $msq = sub {
  my $code = shift;
  if (exists $SIG{SYS}) {
    local $SIG{SYS} = sub { plan(skip_all => "SIGSYS caught") };
    return $code->();
  }
  return $code->();
}->(sub { new IPC::Msg(IPC_PRIVATE, S_IRWXU | S_IRWXG | S_IRWXO) });

unless (defined $msq) {
  my $info = "IPC::Msg->new failed: $!";
  if ($! == &IPC::SysV::ENOSPC || $! == &IPC::SysV::ENOSYS ||
      $! == &IPC::SysV::ENOMEM || $! == &IPC::SysV::EACCES) {
    plan(skip_all => $info);
  }
  else {
    die $info;
  }
}

plan(tests => 9);

pass('create message queue');

#Putting a message on the queue
my $test_name = 'enqueue message';

my $msgtype = 1;
my $msg = "hello";
if ($msq->snd($msgtype,$msg,IPC_NOWAIT)) {
  pass($test_name);
}
else {
  print "# snd: $!\n";
  fail($test_name);
}

#Check if there are messages on the queue
my $ds = $msq->stat;
ok($ds, 'stat');

if ($ds) {
  is($ds->qnum, 1, 'qnum');
}
else {
  fail('qnum');
}

#Retrieving a message from the queue
my $rmsg;
my $rmsgtype = 0; # Give me any type
$rmsgtype = $msq->rcv($rmsg,256,$rmsgtype,IPC_NOWAIT);
is($rmsgtype, $msgtype, 'rmsgtype');
is($rmsg, $msg, 'rmsg');

$ds = $msq->stat;
ok($ds, 'stat');

if ($ds) {
  is($ds->qnum, 0, 'qnum');
}
else {
  fail('qnum');
}

END {
  ok($msq->remove, 'remove message') if defined $msq;
}