Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 10a4f5b

Browse files
committed
Merge pull request #5 from streamr-dev/621-metrics
streamr-client passes all subscription options to server
2 parents 81e0f3d + d925b36 commit 10a4f5b

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed

streamr-client.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var TIMESTAMP_KEY = "_T"
2222
var BYE_KEY = "_bye"
2323
var SUB_KEY = "_sub"
2424

25-
function extend(){
25+
function extend() {
2626
for(var i=1; i<arguments.length; i++)
2727
for(var key in arguments[i])
2828
if(arguments[i].hasOwnProperty(key))
@@ -601,7 +601,7 @@ StreamrClient.prototype._requestSubscribe = function(sub, from) {
601601

602602
// If this is the first subscription for this stream, send a subscription request to the server
603603
if (!subs._subscribing && subscribedSubs.length === 0) {
604-
var req = {channel: sub.streamId, from: from}
604+
var req = extend({}, sub.options, {channel: sub.streamId, from: from})
605605
debug("_requestSubscribe: subscribing client: %o", req)
606606
subs._subscribing = true
607607
_this.socket.emit('subscribe', req)
@@ -629,18 +629,20 @@ StreamrClient.prototype._requestUnsubscribe = function(streamId) {
629629
this.socket.emit('unsubscribe', {channel: streamId})
630630
}
631631

632-
StreamrClient.prototype._requestResend = function(sub, options) {
633-
options = options || sub.options
632+
StreamrClient.prototype._requestResend = function(sub, resendOptions) {
633+
// If overriding resendOptions are given, need to remove resend options in sub.options
634+
var options = extend({}, sub.options)
635+
if (resendOptions) {
636+
Object.keys(options).forEach(function (key) {
637+
if (key.match(/resend_.*/)) {
638+
delete options[key]
639+
}
640+
})
641+
}
634642

635643
sub.resending = true
636644

637-
var request = {}
638-
Object.keys(options).forEach(function(key) {
639-
request[key] = options[key]
640-
})
641-
request.channel = sub.streamId
642-
request.sub = sub.id
643-
645+
var request = extend({}, options, resendOptions, {channel: sub.streamId, sub: sub.id})
644646
debug("_requestResend: %o", request)
645647
this.socket.emit('resend', request)
646648
}

test/test.streamr-client.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ describe('StreamrClient', function() {
289289
})
290290
})
291291

292+
it('should add any subscription options to subscription request', function(done) {
293+
client.connect()
294+
client.socket.once('connect', function() {
295+
client.socket.once('subscribe', function(request) {
296+
if (request.foo === 'bar')
297+
done()
298+
})
299+
client.subscribe("stream1", function(message) {}, {foo: 'bar'})
300+
})
301+
})
302+
303+
it('should ignore any subscription options that conflict with required ones', function(done) {
304+
client.connect()
305+
client.socket.once('connect', function() {
306+
client.socket.once('subscribe', function(request) {
307+
if (request.channel === 'stream1')
308+
done()
309+
})
310+
client.subscribe("stream1", function(message) {}, {channel: 'wrong'})
311+
})
312+
})
313+
292314
it('should mark Subscriptions as subscribed when the server responds with subscribed', function(done) {
293315
var subscription = client.subscribe("stream1", function(message) {})
294316
client.connect()
@@ -410,6 +432,17 @@ describe('StreamrClient', function() {
410432
})
411433
})
412434

435+
it('should emit a resend request with given other options', function(done) {
436+
client.subscribe("stream1", function(message) {}, {resend_all:true, foo: 'bar'})
437+
client.connect()
438+
439+
client.socket.once('resend', function(request) {
440+
if (request.resend_all && request.foo === 'bar')
441+
done()
442+
else throw "Unexpected resend request: "+JSON.stringify(request)
443+
})
444+
})
445+
413446
it('should throw an error if multiple resend options are given', function() {
414447
assert.throws(function() {
415448
client.subscribe("stream1", function(message) {}, {resend_all:true, resend_last:5})
@@ -1026,7 +1059,9 @@ describe('StreamrClient', function() {
10261059
var el = validResendRequests[0]
10271060
// all fields in the model request must be equal in actual request
10281061
Object.keys(el).forEach(function(field) {
1029-
assert.equal(request[field], el[field])
1062+
if (request[field] !== el[field]) {
1063+
throw "Resend request field "+field+" does not match expected value! Was: "+JSON.stringify(request)+", expected: "+JSON.stringify(el)
1064+
}
10301065
})
10311066
validResendRequests.shift()
10321067
}
@@ -1168,6 +1203,48 @@ describe('StreamrClient', function() {
11681203
done()
11691204
})
11701205
})
1206+
1207+
it('should include any subscription options in resend request', function(done) {
1208+
client.subscribe("stream1", function(message) {}, {auth:'foo'})
1209+
client.connect()
1210+
1211+
validResendRequests.push({channel:"stream1", resend_from:1, resend_to:9})
1212+
1213+
client.socket.once('subscribed', function() {
1214+
client.socket.emit('ui', msg("stream1",0))
1215+
client.socket.emit('ui', msg("stream1",10))
1216+
})
1217+
1218+
client.socket.once('resend', function(request) {
1219+
assert.equal(request.auth, 'foo')
1220+
})
1221+
1222+
client.socket.once('resent', function() {
1223+
done()
1224+
})
1225+
})
1226+
1227+
it('should not include stronger resend requests in gap resend request', function(done) {
1228+
client.subscribe("stream1", function(message) {}, {auth:'foo', resend_all: true})
1229+
client.connect()
1230+
1231+
validResendRequests.push({channel:"stream1", resend_all:true})
1232+
validResendRequests.push({channel:"stream1", resend_from:1, resend_to:1})
1233+
1234+
client.socket.once('subscribed', function() {
1235+
client.socket.emit('ui', msg("stream1",0))
1236+
client.socket.emit('ui', msg("stream1",2))
1237+
})
1238+
1239+
client.socket.on('resend', function(request) {
1240+
if (request.resend_from)
1241+
assert.equal(request.resend_all, undefined)
1242+
})
1243+
1244+
client.socket.once('resent', function() {
1245+
done()
1246+
})
1247+
})
11711248

11721249
it('should not emit another resend request while waiting for resend', function(done) {
11731250
client.subscribe("stream1", function(message) {})

0 commit comments

Comments
 (0)