Skip to content

Commit e897614

Browse files
committed
Fix pubsub, and switch to a Supplier rather than a stream
1 parent 000a9bb commit e897614

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/main/java/io/ipfs/api/IPFS.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,14 @@ public Object peers(String topic) throws IOException {
234234
}
235235

236236
public Object pub(String topic, String data) throws IOException {
237-
return retrieveAndParse("pubsub/peers?arg="+topic + "&data=" + data);
237+
return retrieveAndParse("pubsub/peers?arg="+topic + "&arg=" + data);
238238
}
239239

240-
public Stream<Object> sub(String topic) throws IOException {
240+
public Supplier<Object> sub(String topic) throws IOException {
241241
return sub(topic, ForkJoinPool.commonPool());
242242
}
243243

244-
public Stream<Object> sub(String topic, ForkJoinPool threadSupplier) throws IOException {
244+
public Supplier<Object> sub(String topic, ForkJoinPool threadSupplier) throws IOException {
245245
return retrieveAndParseStream("pubsub/sub?arg="+topic, threadSupplier);
246246
}
247247
}
@@ -556,10 +556,16 @@ private Object retrieveAndParse(String path) throws IOException {
556556
return JSONParser.parse(new String(res));
557557
}
558558

559-
private Stream<Object> retrieveAndParseStream(String path, ForkJoinPool executor) throws IOException {
560-
Queue<byte[]> objects = new LinkedBlockingQueue<>();
559+
private Supplier<Object> retrieveAndParseStream(String path, ForkJoinPool executor) throws IOException {
560+
BlockingQueue<byte[]> objects = new LinkedBlockingQueue<>();
561561
executor.submit(() -> getObjectStream(path, objects::add));
562-
return Stream.generate(() -> JSONParser.parse(new String(objects.poll())));
562+
return () -> {
563+
try {
564+
return JSONParser.parse(new String(objects.take()));
565+
} catch (InterruptedException e) {
566+
throw new RuntimeException(e);
567+
}
568+
};
563569
}
564570

565571
private byte[] retrieve(String path) throws IOException {

src/test/java/io/ipfs/api/APITest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,12 @@ public void bulkBlockTest() {
401401
public void pubsub() throws IOException {
402402
Object ls = ipfs.pubsub.ls();
403403
Object peers = ipfs.pubsub.peers();
404-
Stream<Object> sub = ipfs.pubsub.sub("orbit" + System.nanoTime());
405-
String data = "Gday All!";
406-
Object pub = ipfs.pubsub.pub("orbit", data);
407-
Optional<Object> first = sub.findFirst();
408-
Assert.assertTrue(first.isPresent() && first.get().equals(data));
404+
String topic = "topic" + System.nanoTime();
405+
Supplier<Object> sub = ipfs.pubsub.sub(topic);
406+
Object first = sub.get();
407+
String data = "Hello!";
408+
Object pub = ipfs.pubsub.pub(topic, data);
409+
Assert.assertTrue(first.equals(Collections.emptyMap()));
409410
}
410411

411412
private static String toEscapedHex(byte[] in) throws IOException {

0 commit comments

Comments
 (0)