Skip to content

Commit 00192a4

Browse files
chris-smithIanTheEngineer
authored andcommitted
Add start of generic Ros Master Stub for testing. guard on the fly tests (#43)
1 parent 68d01fd commit 00192a4

File tree

3 files changed

+114
-38
lines changed

3 files changed

+114
-38
lines changed

test/Log.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ describe('Logging', () => {
3939
};
4040

4141
before(() => {
42-
rosnodejs.log.addStream({
43-
type: 'raw',
44-
level: 'info',
45-
stream: outputCapture
46-
});
47-
48-
rosnodejs.log.setLevel('trace');
42+
return rosnodejs.initNode('/testNode', {rosMasterUri: `http://localhost:${MASTER_PORT}`, logging: {skipRosLogging: true}})
43+
.then(() => {
44+
rosnodejs.log.addStream({
45+
type: 'raw',
46+
level: 'info',
47+
stream: outputCapture
48+
});
49+
50+
rosnodejs.log.setLevel('trace');
51+
});
4952
});
5053

5154
after(()=> {

test/onTheFly.js

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
1-
'use strict'
1+
'use strict';
22

33
const chai = require('chai');
44
const expect = chai.expect;
5-
let rosnodejs = require('../index.js');
6-
7-
rosnodejs.initNode('/my_node', { onTheFly: true})
8-
.then((rosNode) => {
9-
10-
const geometry_msgs = rosnodejs.require('geometry_msgs').msg;
11-
const msg = new geometry_msgs.PoseWithCovariance({
12-
pose: {
13-
position: {x:0, y:0, z:0},
14-
orientation: {w:1, x:0, y:0, z:0}
15-
},
16-
covariance: [
17-
0,0,0,0,0,0.123,
18-
0,2,0,0,0,0,
19-
0,0,4,0,0,0,
20-
0,0,0,6,0,0,
21-
0,0,0,0,8,0,
22-
0.123,0,0,0,0,0.654321654321
23-
]
24-
});
5+
const xmlrpc = require('xmlrpc');
6+
const rosnodejs = require('../index.js');
7+
const Master = require('./utils/MasterStub.js');
8+
9+
const MASTER_PORT = 11234;
2510

26-
describe('OnTheFly', () => {
11+
describe('OnTheFly', function () {
12+
let master;
2713

28-
it('serialize/deserialize', (done) => {
29-
const size = geometry_msgs.PoseWithCovariance.getMessageSize(msg);
30-
const buffer = new Buffer(size);
31-
geometry_msgs.PoseWithCovariance.serialize(msg, buffer, 0);
14+
before(function (done) {
15+
this.timeout(0);
3216

33-
const read = geometry_msgs.PoseWithCovariance.deserialize(buffer);
34-
expect(read.covariance.length == msg.covariance.length
35-
&& read.covariance.every((v,i)=> v === msg.covariance[i])).to.be.true;
17+
master = new Master('localhost', MASTER_PORT);
18+
master.provideAll();
3619

37-
done();
38-
});
20+
return rosnodejs.initNode('/testNode', {
21+
rosMasterUri: `http://localhost:${MASTER_PORT}`,
22+
onTheFly: true,
23+
logging: {skipRosLogging: true}})
24+
.then(() => {
25+
done();
3926
});
4027
});
28+
29+
after(() => {
30+
rosnodejs.reset();
31+
return master.shutdown();
32+
});
33+
34+
const geometry_msgs = rosnodejs.require('geometry_msgs').msg;
35+
const msg = new geometry_msgs.PoseWithCovariance({
36+
pose: {
37+
position: {x:0, y:0, z:0},
38+
orientation: {w:1, x:0, y:0, z:0}
39+
},
40+
covariance: [
41+
0,0,0,0,0,0.123,
42+
0,2,0,0,0,0,
43+
0,0,4,0,0,0,
44+
0,0,0,6,0,0,
45+
0,0,0,0,8,0,
46+
0.123,0,0,0,0,0.654321654321
47+
]
48+
});
49+
50+
it('serialize/deserialize', (done) => {
51+
const size = geometry_msgs.PoseWithCovariance.getMessageSize(msg);
52+
const buffer = new Buffer(size);
53+
geometry_msgs.PoseWithCovariance.serialize(msg, buffer, 0);
54+
55+
const read = geometry_msgs.PoseWithCovariance.deserialize(buffer);
56+
expect(read.covariance.length == msg.covariance.length
57+
&& read.covariance.every((v,i)=> v === msg.covariance[i])).to.be.true;
58+
59+
done();
60+
});
61+
});

test/utils/MasterStub.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const xmlrpc = require('xmlrpc');
2+
const EventEmitter = require('events').EventEmitter;
3+
4+
class RosMasterStub extends EventEmitter {
5+
constructor(host, port) {
6+
super();
7+
8+
this._host = host;
9+
this._port = port;
10+
11+
this._server = xmlrpc.createServer({host, port}, () => {
12+
this.emit('ready');
13+
});
14+
15+
this._server.on('NotFound', (method, params) => {
16+
console.error('Method %s does not exist', method);
17+
});
18+
19+
this._apiMap = {
20+
getUri: this._onGetUri.bind(this)
21+
};
22+
23+
this._providedApis = new Set();
24+
}
25+
26+
shutdown() {
27+
return new Promise((resolve, reject) => {
28+
this._server.close(resolve);
29+
});
30+
}
31+
32+
provide(api) {
33+
const method = this._apiMap[api];
34+
if (method && !this._providedApis.has(api)) {
35+
this._server.on(api, method);
36+
this._providedApis.add(api);
37+
}
38+
}
39+
40+
provideAll() {
41+
Object.keys(this._apiMap).forEach((api) => {
42+
this.provide(api);
43+
});
44+
}
45+
46+
_onGetUri(err, params, callback) {
47+
const resp = [ 1, '', `${this._host}:${this._port}`];
48+
callback(null, resp);
49+
}
50+
}
51+
52+
module.exports = RosMasterStub;

0 commit comments

Comments
 (0)