File: amqpexchange_publish_mandatory_multiple_channels_pitfall.phpt

package info (click to toggle)
php-amqp 2.1.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,536 kB
  • sloc: ansic: 7,295; xml: 1,162; php: 690; pascal: 49; makefile: 2
file content (86 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download
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
--TEST--
AMQPExchange::publish() - publish unroutable mandatory on multiple channels pitfall
--SKIPIF--
<?php
if (!extension_loaded("amqp")) print "skip AMQP extension is not loaded";
elseif (!getenv("PHP_AMQP_HOST")) print "skip PHP_AMQP_HOST environment variable is not set";
elseif (getenv("SKIP_SLOW_TESTS")) print "skip slow test and SKIP_SLOW_TESTS is set";
?>
--FILE--
<?php

function exception_error_handler($errno, $errstr, $errfile, $errline)
{
    echo $errstr, PHP_EOL;
}

set_error_handler('exception_error_handler');


$cnn = new AMQPConnection();
$cnn->setHost(getenv('PHP_AMQP_HOST'));
$cnn->connect();


$ch1 = new AMQPChannel($cnn);

try {
    $ch1->waitForBasicReturn(1);
} catch (Exception $e) {
    echo get_class($e), "({$e->getCode()}): ", $e->getMessage() . PHP_EOL;
}

$ch2 = new AMQPChannel($cnn);

try {
    $ch2->waitForBasicReturn(1);
} catch (Exception $e) {
    echo get_class($e), "({$e->getCode()}): ", $e->getMessage() . PHP_EOL;
}


$exchange_name = "exchange-" . bin2hex(random_bytes(32));

$ex1 = new AMQPExchange($ch1);
$ex1->setName($exchange_name);
$ex1->setType(AMQP_EX_TYPE_FANOUT);
$ex1->setFlags(AMQP_AUTODELETE);
$ex1->declareExchange();


$ex2 = new AMQPExchange($ch2);
$ex2->setName($exchange_name);

var_dump($ex2->publish('message 1-2', 'routing.key', AMQP_MANDATORY));
var_dump($ex2->publish('message 2-2', 'routing.key', AMQP_MANDATORY));

// Create a new queue
$q = new AMQPQueue($ch1);
$q->setName('queue-' . bin2hex(random_bytes(32)));
$q->setFlags(AMQP_AUTODELETE);
$q->declareQueue();

$msg = $q->get();
var_dump($msg);


try {
    $ch1->waitForBasicReturn();
} catch (Exception $e) {
    echo get_class($e), "({$e->getCode()}): ", $e->getMessage(), PHP_EOL;
}

// This error happens because on a channel 1 we are expecting only messages withing channel 1, but inside current
// connection we already have pending message on channel 2

echo 'Connection active: ', ($cnn->isConnected() ? 'yes' : 'no');

?>
--EXPECTF--
AMQPQueueException(0): Wait timeout exceed
AMQPQueueException(0): Wait timeout exceed
NULL
NULL
NULL
AMQPException(0): unexpected method received
Connection active: no