1use strict; 2use warnings; 3 4use Test::More; 5use Test::Warnings; 6 7use ZMQ::FFI qw(ZMQ_PUB ZMQ_SUB ZMQ_DONTWAIT); 8 9use Time::HiRes q(usleep); 10 11subtest 'pubsub', 12sub { 13 my $endpoint = "ipc:///tmp/test-zmq-ffi-$$"; 14 15 my $ctx = ZMQ::FFI->new(); 16 17 my $s = $ctx->socket(ZMQ_SUB); 18 my $p = $ctx->socket(ZMQ_PUB); 19 20 $s->connect($endpoint); 21 $p->bind($endpoint); 22 23 { 24 $s->subscribe(''); 25 26 until ($s->has_pollin) { 27 # sleep for a 100ms to compensate for slow subscriber problem 28 usleep 100_000; 29 $p->send('ohhai'); 30 } 31 32 my $msg = $s->recv(); 33 is $msg, 'ohhai', 'got msg sent to all topics'; 34 35 $s->unsubscribe(''); 36 } 37 38 { 39 $s->subscribe('mytopic'); 40 41 until ($s->has_pollin) { 42 usleep 100_000; 43 $p->send('mytopic ohhai'); 44 } 45 46 my $msg = $s->recv(); 47 is $msg, 'mytopic ohhai', 'got msg sent to mytopic'; 48 } 49}; 50 51done_testing; 52 53