Created
January 7, 2011 18:31
-
-
Save robinp/769876 to your computer and use it in GitHub Desktop.
jzmq bug reproduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package zmqbugtest; | |
import org.zeromq.ZMQ; | |
public class Main { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
new Main().run(); | |
} | |
Main() { | |
} | |
private void run() { | |
ctx = ZMQ.context(1); | |
String addr = "inproc://bind1"; | |
ZMQ.Socket sock1 = ctx.socket(ZMQ.PUB); | |
sock1.bind(addr); | |
ZMQ.Socket sock2 = ctx.socket(ZMQ.SUB); | |
ZMQ.Socket sock3 = ctx.socket(ZMQ.SUB); | |
ZMQ.Poller poller = ctx.poller(); | |
int poll2 = poller.register(sock2); | |
int poll3 = poller.register(sock3); | |
poller.unregister(sock2); | |
byte any_topic[] = {}; | |
sock2.connect(addr); | |
sock2.subscribe(any_topic); | |
sock3.connect(addr); | |
sock3.subscribe(any_topic); | |
sleep(100); | |
byte[] msg = {'h', 'e', 'l', 'l', 'o'}; | |
sock1.send(msg, 0); | |
sleep(100); | |
long ret = poller.poll(); | |
System.out.println("pollin 2: " + poller.pollin(poll2) ); | |
System.out.println("pollin 3: " + poller.pollin(poll3) ); | |
System.out.println( "2: " + new String(sock2.recv(0)) ); | |
System.out.println( "3: " + new String(sock3.recv(0)) ); | |
} | |
private void sleep(long m) { | |
try { | |
Thread.sleep(m); | |
} | |
catch (InterruptedException e) { | |
// pass | |
} | |
} | |
private ZMQ.Context ctx; | |
} | |
/* | |
Patched output: | |
pollin 2: false | |
pollin 3: true | |
2: hello | |
3: hello | |
Unpatched output: | |
pollin 2: true | |
pollin 3: false (sometimes true) | |
2: hello | |
3: hello | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment