Skip to content

Commit 9ebdad7

Browse files
committed
Rewrite pubsub test to accomodate ROS 2 nondeterministic topic order
1 parent 98a51da commit 9ebdad7

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

test/examples/pubsub.example.ts

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,73 @@
1-
import { describe, it, expect, afterAll } from "vitest";
1+
import { describe, it, expect, afterAll, vi } from "vitest";
22
import * as ROSLIB from "../../src/RosLib.js";
33

44
describe("Topics Example", function () {
55
const ros = new ROSLIB.Ros({
66
url: "ws://localhost:9090",
77
});
88

9-
const example = ros.Topic({
9+
const example = ros.Topic<{ data: string }>({
1010
name: "/example_topic",
1111
messageType: "std_msgs/String",
1212
});
1313

14-
function format(msg) {
14+
function format(msg: string) {
1515
return { data: msg };
1616
}
1717
const messages1 = ["Hello Example2!", "Whats good?"].map(format);
1818
const messages2 = ["Hi there", "this example working"].map(format);
1919

20-
const example2 = ros.Topic({
20+
const example2 = ros.Topic<{ data: string }>({
2121
name: "/example_topic",
2222
messageType: "std_msgs/String",
2323
});
2424

25-
it("Listening and publishing to a topic", () =>
26-
new Promise((done) => {
27-
// Kind of harry...
28-
let topic1msg = messages1[0],
29-
topic2msg: { data?: string } = {};
30-
example.subscribe(function (message) {
31-
if (message.data === topic1msg.data) {
32-
return;
33-
}
34-
topic1msg = messages1[0];
35-
expect(message).to.be.eql(messages2.shift());
36-
if (messages1.length) {
37-
example.publish(topic1msg);
38-
} else {
39-
done(message);
40-
}
41-
});
42-
example2.subscribe(function (message) {
43-
if (message.data === topic2msg.data) {
44-
return;
25+
it("Client-side subscribers receive messages from client-side publishers", async () => {
26+
// Create copies of the message arrays so we can shift from them
27+
const messages1Copy = [...messages1];
28+
const messages2Copy = [...messages2];
29+
30+
const example1Callback = vi.fn((message: { data: string }) => {
31+
if (messages1.some((m) => m.data === message.data)) {
32+
return; // Skip our own published message
33+
}
34+
35+
if (messages1Copy.length) {
36+
example.publish(messages1Copy.shift()!);
37+
}
38+
});
39+
40+
const example2Callback = vi.fn((message: { data: string }) => {
41+
if (messages2.some((m) => m.data === message.data)) {
42+
return; // Skip our own published message
43+
}
44+
45+
if (messages2Copy.length) {
46+
example2.publish(messages2Copy.shift()!);
47+
}
48+
});
49+
50+
example.subscribe(example1Callback);
51+
example2.subscribe(example2Callback);
52+
53+
// Start the conversation
54+
example.publish(messages1Copy.shift()!);
55+
56+
// Wait for all expected calls to complete
57+
await vi.waitFor(
58+
() => {
59+
for (const message of messages1) {
60+
expect(example1Callback).toHaveBeenCalledWith(message);
61+
expect(example2Callback).toHaveBeenCalledWith(message);
4562
}
46-
topic2msg = messages2[0];
47-
expect(message).to.be.eql(messages1.shift());
48-
if (messages2.length) {
49-
example2.publish(topic2msg);
50-
} else {
51-
done(message);
63+
for (const message of messages2) {
64+
expect(example1Callback).toHaveBeenCalledWith(message);
65+
expect(example2Callback).toHaveBeenCalledWith(message);
5266
}
53-
});
54-
example.publish(topic1msg);
55-
}));
67+
},
68+
{ timeout: 5000 },
69+
);
70+
}, 10000);
5671

5772
it("unsubscribe doesn't affect other topics", () =>
5873
new Promise((done) => {

0 commit comments

Comments
 (0)