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

Commit 0063e57

Browse files
author
Eric Andrews
committed
Fix unsubscribe tests
1 parent 8e17ce5 commit 0063e57

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

streamr-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@
958958

959959
StreamrClient.prototype._checkAutoDisconnect = function() {
960960
// Disconnect if no longer subscribed to any channels
961-
if (Object.keys(this.subsByStream).length===0 && this.options.autoDisconnect) {
961+
if (this.options.autoDisconnect && Object.keys(this.subsByStream).length === 0) {
962962
debug("Disconnecting due to no longer being subscribed to any channels")
963963
this.disconnect()
964964
}

test/test.streamr-client.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ describe('StreamrClient', function() {
124124
s.unsubscribeHandler(parsed)
125125
} else if (parsed.type === 'resend') {
126126
s.resendHandler(parsed)
127+
} else {
128+
throw "Unexpected message of type " + parsed.type
127129
}
128130
}
129131

@@ -676,11 +678,11 @@ describe('StreamrClient', function() {
676678

677679
client.connection.once('unsubscribed', function() {
678680
setTimeout(function() {
679-
client.unsubscribe(sub)
680-
done()
681681
client.connection.once('unsubscribed', function() {
682682
throw "Unsubscribed event sent more than once for same Subscription!"
683683
})
684+
client.unsubscribe(sub)
685+
done()
684686
})
685687
})
686688
})
@@ -690,24 +692,31 @@ describe('StreamrClient', function() {
690692
var sub2 = client.subscribe("stream1", function(message) {})
691693
client.connect()
692694

693-
sub2.on('subscribed', function() {
694-
client.unsubscribe(sub2)
695-
})
696-
697-
sub2.on('unsubscribed', function() {
698-
done()
699-
})
700-
701695
client.connection.on('unsubscribed', function() {
702696
throw "Socket should not have unsubscribed"
703697
})
704698

705699
sub1.on('unsubscribed', function() {
706700
throw "sub1 should not have unsubscribed"
707701
})
702+
703+
sub2.on('unsubscribed', function() {
704+
done()
705+
})
706+
707+
sub2.on('subscribed', function() {
708+
client.unsubscribe(sub2)
709+
})
708710
})
709711

710712
it('should not send an unsubscribe request again if unsubscribe is called multiple times', function(done) {
713+
var count = 0
714+
var defaultUnusubscribeHandler = socket.unsubscribeHandler
715+
socket.unsubscribeHandler = function (request) {
716+
++count
717+
defaultUnusubscribeHandler(request)
718+
}
719+
711720
var sub = client.subscribe("stream1", function(message) {})
712721
client.connect()
713722

@@ -717,11 +726,6 @@ describe('StreamrClient', function() {
717726
client.unsubscribe(sub)
718727
})
719728

720-
var count = 0
721-
client.connection.on('unsubscribe', function() {
722-
count++
723-
})
724-
725729
client.connection.on('unsubscribed', function() {
726730
assert.equal(count, 1)
727731
assert(!sub.unsubscribing)
@@ -763,10 +767,12 @@ describe('StreamrClient', function() {
763767

764768
sub.on('unsubscribed', function() {
765769
var newSub = client.subscribe("stream1", function(message) {
770+
assert.deepEqual(message, { count: 0})
766771
done()
767772
})
768773
newSub.on('subscribed', function() {
769-
client.connection.emit('b', msg("stream1", 0, {}))
774+
const broadcastCode = 0
775+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0, { count: 0 })])
770776
})
771777
})
772778
})
@@ -785,7 +791,7 @@ describe('StreamrClient', function() {
785791
}
786792
})
787793

788-
client.connection.on('disconnect', function() {
794+
client.connection.on('disconnected', function() {
789795
assert(!sub1.isSubscribed())
790796
assert(!sub2.isSubscribed())
791797
done()
@@ -795,21 +801,24 @@ describe('StreamrClient', function() {
795801
it('should disconnect if all subscriptions are done during resend', function(done) {
796802
client.options.autoDisconnect = true
797803

804+
socket.resendHandler = function(request) {
805+
const resendingCode = 4
806+
socket.fakeReceive([0, resendingCode, null, { channel: request.channel, partition: 0, sub: request.sub }])
807+
}
808+
798809
var sub1 = client.subscribe("stream1", function(message) {}, {resend_all: true})
799810
client.connect()
800811

801-
client.connection.on('resend', function(request) {
812+
client.connection.on('resending', function(request) {
802813
async(function() {
803-
client.connection.emit('resending', {
804-
channel: request.channel,
805-
sub: request.sub
806-
})
807-
client.connection.emit('b', byeMsg(request.channel,0))
808-
client.connection.emit('resent', {channel: request.channel, sub: request.sub})
814+
const broadcastCode = 0
815+
const resentCode = 5
816+
socket.fakeReceive([0, broadcastCode, null, byeMsg(request.channel, 0)])
817+
socket.fakeReceive([0, resentCode, null, {channel: request.channel, partition: 0, sub: request.sub }])
809818
})
810819
})
811820

812-
client.connection.on('disconnect', function() {
821+
client.connection.on('disconnected', function() {
813822
done()
814823
})
815824
})

0 commit comments

Comments
 (0)