Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions streamr-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var TIMESTAMP_KEY = "_T"
var BYE_KEY = "_bye"
var SUB_KEY = "_sub"

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

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

StreamrClient.prototype._requestResend = function(sub, options) {
options = options || sub.options
StreamrClient.prototype._requestResend = function(sub, resendOptions) {
// If overriding resendOptions are given, need to remove resend options in sub.options
var options = extend({}, sub.options)
if (resendOptions) {
Object.keys(options).forEach(function (key) {
if (key.match(/resend_.*/)) {
delete options[key]
}
})
}

sub.resending = true

var request = {}
Object.keys(options).forEach(function(key) {
request[key] = options[key]
})
request.channel = sub.streamId
request.sub = sub.id

var request = extend({}, options, resendOptions, {channel: sub.streamId, sub: sub.id})
debug("_requestResend: %o", request)
this.socket.emit('resend', request)
}
Expand Down
79 changes: 78 additions & 1 deletion test/test.streamr-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ describe('StreamrClient', function() {
})
})

it('should add any subscription options to subscription request', function(done) {
client.connect()
client.socket.once('connect', function() {
client.socket.once('subscribe', function(request) {
if (request.foo === 'bar')
done()
})
client.subscribe("stream1", function(message) {}, {foo: 'bar'})
})
})

it('should ignore any subscription options that conflict with required ones', function(done) {
client.connect()
client.socket.once('connect', function() {
client.socket.once('subscribe', function(request) {
if (request.channel === 'stream1')
done()
})
client.subscribe("stream1", function(message) {}, {channel: 'wrong'})
})
})

it('should mark Subscriptions as subscribed when the server responds with subscribed', function(done) {
var subscription = client.subscribe("stream1", function(message) {})
client.connect()
Expand Down Expand Up @@ -410,6 +432,17 @@ describe('StreamrClient', function() {
})
})

it('should emit a resend request with given other options', function(done) {
client.subscribe("stream1", function(message) {}, {resend_all:true, foo: 'bar'})
client.connect()

client.socket.once('resend', function(request) {
if (request.resend_all && request.foo === 'bar')
done()
else throw "Unexpected resend request: "+JSON.stringify(request)
})
})

it('should throw an error if multiple resend options are given', function() {
assert.throws(function() {
client.subscribe("stream1", function(message) {}, {resend_all:true, resend_last:5})
Expand Down Expand Up @@ -1026,7 +1059,9 @@ describe('StreamrClient', function() {
var el = validResendRequests[0]
// all fields in the model request must be equal in actual request
Object.keys(el).forEach(function(field) {
assert.equal(request[field], el[field])
if (request[field] !== el[field]) {
throw "Resend request field "+field+" does not match expected value! Was: "+JSON.stringify(request)+", expected: "+JSON.stringify(el)
}
})
validResendRequests.shift()
}
Expand Down Expand Up @@ -1168,6 +1203,48 @@ describe('StreamrClient', function() {
done()
})
})

it('should include any subscription options in resend request', function(done) {
client.subscribe("stream1", function(message) {}, {auth:'foo'})
client.connect()

validResendRequests.push({channel:"stream1", resend_from:1, resend_to:9})

client.socket.once('subscribed', function() {
client.socket.emit('ui', msg("stream1",0))
client.socket.emit('ui', msg("stream1",10))
})

client.socket.once('resend', function(request) {
assert.equal(request.auth, 'foo')
})

client.socket.once('resent', function() {
done()
})
})

it('should not include stronger resend requests in gap resend request', function(done) {
client.subscribe("stream1", function(message) {}, {auth:'foo', resend_all: true})
client.connect()

validResendRequests.push({channel:"stream1", resend_all:true})
validResendRequests.push({channel:"stream1", resend_from:1, resend_to:1})

client.socket.once('subscribed', function() {
client.socket.emit('ui', msg("stream1",0))
client.socket.emit('ui', msg("stream1",2))
})

client.socket.on('resend', function(request) {
if (request.resend_from)
assert.equal(request.resend_all, undefined)
})

client.socket.once('resent', function() {
done()
})
})

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