This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
mongo_ascoltatore_spec.js
152 lines (136 loc) · 4.49 KB
/
mongo_ascoltatore_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var MongoClient = require('mongodb').MongoClient;
var steed = require('steed')();
describeAscoltatore("mongo", function() {
afterEach(function(done) {
this.instance.close(done);
});
it("should publish a binary payload", function(done) {
var that = this;
that.instance.sub("hello/*", function(topic, value) {
expect(value).to.eql(new Buffer("42"));
done();
}, function() {
that.instance.pub("hello/123", new Buffer("42"));
});
});
it("should support the old connect uri + db", function(done) {
this.instance.close(function() {
this.instance = new ascoltatori.MongoAscoltatore({ uri: 'mongodb://127.0.0.1/', db: 'ascoltatoriTests2' });
this.instance.on('ready', function() {
expect(this.instance.db.databaseName).to.eql('ascoltatoriTests2');
done();
}.bind(this));
}.bind(this));
});
it("should reuse another mongo connection", function(done) {
this.instance.close(function() {
MongoClient.connect('mongodb://127.0.0.1/ascoltatoriTest3', {}, function(err, db) {
this.instance = new ascoltatori.MongoAscoltatore({ db: db });
this.instance.on('ready', done);
}.bind(this));
}.bind(this));
});
it.skip("should publish 2000 messages without skipping one", function(done) {
var that = this;
var count = 0;
var max = 2000;
function doPub(n, next) {
that.instance.pub("hello/123", "abcde " + n, {}, next);
}
that.instance.sub("hello/*", function(topic, value) {
count++;
if (count === max) {
done();
}
}, function() {
steed.times(max, doPub);
});
});
it("should publish a big payload", function(done) {
var that = this;
var payload = new Buffer(5 * 1024);
that.instance.sub("hello/*", function(topic, value) {
expect(value).to.eql(payload);
done();
}, function(err) {
that.instance.pub("hello/123", payload);
});
});
it("should publish with options", function(done) {
var that = this;
that.instance.subscribe("hello/*", function(topic, value, options) {
expect(value).to.equal("42");
expect(options.qos).to.equal(1);
expect(options.messageId).to.equal(5);
done();
}, function() {
that.instance.publish("hello/123", "42", { qos: 1, messageId: 5 });
});
});
// flaky test on CI
it.skip("should not suffer from mongo interruptions", function (done) {
this.instance.close(function () {
MongoClient.connect('mongodb://127.0.0.1/ascoltatoriTest4', {}, function (err, db) {
db.on('error', done);
this.instance = new ascoltatori.MongoAscoltatore({ db: db });
this.instance.on('error', done);
this.instance.on('ready', function () {
_test(this);
}.bind(this));
}.bind(this));
}.bind(this));
function _test(that) {
that.instance.subscribe("hello/*", function (topic, value, options) {
if (value === "42") {
done();
}
}, function () {
that.instance.publish("hello/123", "21");
that.instance.db.logout({}, function (err, res) {
if (err) {
throw(err);
}
that.instance.publish("hello/456", "42");
});
});
}
});
it("should not duplicate messages", function(done) {
this.timeout(5000);
var that = this;
var called = 0;
that.instance.sub("hello", function(topic, value) {
called++;
expect(called).to.be.lessThan(3);
expect(value).to.eql(new Buffer("42"));
}, function() {
that.instance.pub("hello", new Buffer("42"));
that.instance.pub("hello", new Buffer("42"));
setTimeout(done, 3000);
});
});
it("should not duplicate messages when sending messages sequentially", function(done) {
this.timeout(5000);
var that = this;
var called = 0;
that.instance.sub("hello", function(topic, value) {
called++;
expect(called).to.be.lessThan(7);
expect(value).to.eql(new Buffer("42"));
}, function() {
setTimeout(function(){
that.instance.pub("hello", new Buffer("42"));
that.instance.pub("hello", new Buffer("42"));
setTimeout(function(){
that.instance.pub("hello", new Buffer("42"));
that.instance.pub("hello", new Buffer("42"));
setTimeout(function(){
that.instance.pub("hello", new Buffer("42"));
that.instance.pub("hello", new Buffer("42"));
},100);
},100);
},100);
setTimeout(done, 3000);
});
});
});