Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Theosakamg committed May 2, 2018
1 parent 0f1357b commit 730e3c3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@

import org.ros2.rcljava.RCLJava;
import org.ros2.rcljava.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseThreadedExecutor implements ThreadedExecutor {

private static final Logger logger = LoggerFactory.getLogger(BaseThreadedExecutor.class);

protected final Object mutex = new Object();
protected final NativeExecutor baseExecutor;
protected volatile ExecutorService executorService;
Expand All @@ -42,6 +46,7 @@ public void addNode(final Node node) {
@Override
public void addNode(final Node node, final boolean notify) {
if (!this.nodes.contains(node)) {
logger.debug("Add node : " + node.getName());
this.nodes.add(node);

// if (notify) {
Expand All @@ -58,6 +63,7 @@ public void removeNode(final Node node) {
@Override
public void removeNode(final Node node, final boolean notify) {
if (this.nodes.contains(node)) {
logger.debug("Remove node : " + node.getName());
this.nodes.remove(node);

// if (notify) {
Expand Down Expand Up @@ -108,6 +114,8 @@ public void spinNodeSome(final Node node) {

@Override
public void run() {
logger.debug("Starting Executor.");

while (RCLJava.ok()) {
this.spinOnce(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void clearHandles() {
this.clientHandles.clear();
}


public boolean collectEntities(final BlockingQueue<Node> nodes) {
for (final Node node : nodes) {
for (final Subscription<?> subscription : node.getSubscriptions()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Pool-multiple-threaded executor implementation
* This is the default executor created by {@link RCLJava::spin}.
*/
public final class MultiThreadedExecutor extends BaseThreadedExecutor {

private static final Logger logger = LoggerFactory.getLogger(MultiThreadedExecutor.class);

private int numberOfThreads;

/**
Expand All @@ -38,6 +43,9 @@ public MultiThreadedExecutor() {
*/
public MultiThreadedExecutor(final int numberOfThreads) {
super();

logger.debug("Initialized Executor.");

if (numberOfThreads == 0) {
this.numberOfThreads = 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@

import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Single-threaded executor implementation
* This is the default executor created by {@link RCLJava::spin}.
*/
public final class SingleThreadedExecutor extends BaseThreadedExecutor {

private static final Logger logger = LoggerFactory.getLogger(SingleThreadedExecutor.class);

/**
* Default constructor. See the default constructor for Executor.
*/
public SingleThreadedExecutor() {
super();

logger.debug("Initialized Executor.");

this.executorService = Executors.newSingleThreadExecutor();
}

Expand Down
5 changes: 5 additions & 0 deletions rcljava/src/test/java/org/ros2/rcljava/AbstractRosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public void setUp() {
@After
public void tearDown() {
this.releaseRCLjava();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

protected void initRCLjava() {
Expand Down
8 changes: 4 additions & 4 deletions rcljava/src/test/java/org/ros2/rcljava/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Map;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;

import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -121,19 +120,20 @@ public void testGetNodeName() {
}

@Test
public final void testPubSub() throws InterruptedException, ExecutionException, Exception {
public final void testPubSub() throws Exception {
logger.debug(new Object(){}.getClass().getEnclosingMethod().getName());

this.initRCLjava();
final NativeNode node = (NativeNode) RCLJava.createNode("test_node");
Assert.assertNotEquals(0, node.getNodeHandle());

Publisher<std_msgs.msg.String> publisher = node.<std_msgs.msg.String>createPublisher(std_msgs.msg.String.class,
final Publisher<std_msgs.msg.String> publisher = node.<std_msgs.msg.String>createPublisher(
std_msgs.msg.String.class,
"test_topic");

final RCLFuture<std_msgs.msg.String> future = new RCLFuture<std_msgs.msg.String>(new WeakReference<Node>(node));

Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
final Subscription<std_msgs.msg.String> subscription = node.<std_msgs.msg.String>createSubscription(
std_msgs.msg.String.class, "test_topic", new TestConsumer<std_msgs.msg.String>(future));

final std_msgs.msg.String msg = new std_msgs.msg.String();
Expand Down

0 comments on commit 730e3c3

Please sign in to comment.