|
1 | | -import { describe, it, expect, afterAll } from "vitest"; |
| 1 | +import { describe, it, expect, afterAll, vi } from "vitest"; |
2 | 2 | import * as ROSLIB from "../../src/RosLib.js"; |
3 | 3 |
|
4 | 4 | describe("Topics Example", function () { |
5 | 5 | const ros = new ROSLIB.Ros({ |
6 | 6 | url: "ws://localhost:9090", |
7 | 7 | }); |
8 | 8 |
|
9 | | - const example = ros.Topic({ |
| 9 | + const example = ros.Topic<{ data: string }>({ |
10 | 10 | name: "/example_topic", |
11 | 11 | messageType: "std_msgs/String", |
12 | 12 | }); |
13 | 13 |
|
14 | | - function format(msg) { |
| 14 | + function format(msg: string) { |
15 | 15 | return { data: msg }; |
16 | 16 | } |
17 | 17 | const messages1 = ["Hello Example2!", "Whats good?"].map(format); |
18 | 18 | const messages2 = ["Hi there", "this example working"].map(format); |
19 | 19 |
|
20 | | - const example2 = ros.Topic({ |
| 20 | + const example2 = ros.Topic<{ data: string }>({ |
21 | 21 | name: "/example_topic", |
22 | 22 | messageType: "std_msgs/String", |
23 | 23 | }); |
24 | 24 |
|
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); |
45 | 62 | } |
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); |
52 | 66 | } |
53 | | - }); |
54 | | - example.publish(topic1msg); |
55 | | - })); |
| 67 | + }, |
| 68 | + { timeout: 5000 }, |
| 69 | + ); |
| 70 | + }, 10000); |
56 | 71 |
|
57 | 72 | it("unsubscribe doesn't affect other topics", () => |
58 | 73 | new Promise((done) => { |
|
0 commit comments