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

Commit 8e17ce5

Browse files
author
Eric Andrews
committed
Fix 'message handling' tests
1 parent 1d2ef2e commit 8e17ce5

File tree

1 file changed

+75
-107
lines changed

1 file changed

+75
-107
lines changed

test/test.streamr-client.js

Lines changed: 75 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('StreamrClient', function() {
4747

4848
// unicast message to subscription
4949
if (subId != null) {
50-
var msg = [
50+
return JSON.stringify([
5151
28, // version
5252
streamId,
5353
0, // partition
@@ -56,12 +56,7 @@ describe('StreamrClient', function() {
5656
offset,
5757
forcePreviousOffset, // previousOffset
5858
27, // contentType (JSON)
59-
JSON.stringify(content)]
60-
61-
return JSON.stringify({
62-
m: msg,
63-
sub: subId
64-
})
59+
JSON.stringify(content)])
6560
}
6661
// broadcast message to all subscriptions
6762
else {
@@ -107,23 +102,13 @@ describe('StreamrClient', function() {
107102

108103
s.subscribeHandler = function(request) {
109104
async(function() {
110-
if (!s.done) {
111-
mockDebug("defaultSubscribeHandler: emitting subscribed")
112-
s.onmessage({
113-
data: JSON.stringify([0, 2, null, {channel: request.channel, partition: 0}])
114-
})
115-
}
105+
s.fakeReceive([0, 2, null, {channel: request.channel, partition: 0}])
116106
})
117107
}
118108

119109
s.unsubscribeHandler = function(request) {
120110
async(function() {
121-
if (!s.done) {
122-
mockDebug("defaultUnsubscribeHandler: emitting unsubscribed")
123-
s.onmessage({
124-
data: JSON.stringify([0, 3, null, {channel: request.channel, partition: 0}])
125-
})
126-
}
111+
s.fakeReceive([0, 3, null, {channel: request.channel, partition: 0}])
127112
})
128113
}
129114

@@ -141,7 +126,13 @@ describe('StreamrClient', function() {
141126
s.resendHandler(parsed)
142127
}
143128
}
144-
129+
130+
s.fakeReceive = function (msg) {
131+
if (!s.done) {
132+
s.onmessage({data: JSON.stringify(msg)})
133+
}
134+
}
135+
145136
s.close = function() {
146137
s.disconnect()
147138
}
@@ -372,11 +363,7 @@ describe('StreamrClient', function() {
372363

373364
it('should trigger an error event on the client if the subscribe fails', function(done) {
374365
socket.subscribeHandler = function(request) {
375-
if (!socket.done) {
376-
socket.onmessage({
377-
data: JSON.stringify([0, 2, null, {channel: request.channel, partition: 0, error: 'error message'}])
378-
})
379-
}
366+
socket.fakeReceive([0, 2, null, {channel: request.channel, partition: 0, error: 'error message'}])
380367
}
381368

382369
client.subscribe("stream1", function(message) {})
@@ -462,31 +449,17 @@ describe('StreamrClient', function() {
462449

463450
if (request.resend_all) {
464451
async(function() {
465-
socket.onmessage({
466-
data: JSON.stringify([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
467-
})
468-
socket.onmessage({
469-
data: JSON.stringify([0, unicastCode, request.sub, msg('stream1', 0, request.sub)])
470-
})
471-
socket.onmessage({
472-
data: JSON.stringify([0, unicastCode, request.sub, msg('stream1', 1, request.sub)])
473-
})
474-
socket.onmessage({
475-
data: JSON.stringify([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
476-
})
452+
socket.fakeReceive([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
453+
socket.fakeReceive([0, unicastCode, request.sub, msg('stream1', 0, request.sub)])
454+
socket.fakeReceive([0, unicastCode, request.sub, msg('stream1', 1, request.sub)])
455+
socket.fakeReceive([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
477456
})
478457
}
479458
else if (request.resend_last===1) {
480459
async(function() {
481-
socket.onmessage({
482-
data: JSON.stringify([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
483-
})
484-
socket.onmessage({
485-
data: JSON.stringify([0, unicastCode, request.sub, msg('stream1', 1, request.sub)])
486-
})
487-
socket.onmessage({
488-
data: JSON.stringify([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
489-
})
460+
socket.fakeReceive([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
461+
socket.fakeReceive([0, unicastCode, request.sub, msg('stream1', 1, request.sub)])
462+
socket.fakeReceive([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
490463
})
491464
}
492465
}
@@ -523,15 +496,9 @@ describe('StreamrClient', function() {
523496
const resentCode = 5
524497

525498
async(function() {
526-
socket.onmessage({
527-
data: JSON.stringify([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
528-
})
529-
socket.onmessage({
530-
data: JSON.stringify([0, broadcastCode, null, byeMsg('stream1', 0)])
531-
})
532-
socket.onmessage({
533-
data: JSON.stringify([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
534-
})
499+
socket.fakeReceive([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
500+
socket.fakeReceive([0, broadcastCode, null, byeMsg('stream1', 0)])
501+
socket.fakeReceive([0, resentCode, request.sub, {channel:'stream1', partition: 0}])
535502
done()
536503
})
537504
}
@@ -548,19 +515,10 @@ describe('StreamrClient', function() {
548515
const resendingCode = 4
549516
const resentCode = 5
550517

551-
socket.onmessage({
552-
data: JSON.stringify([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
553-
})
554-
555-
socket.onmessage({
556-
data: JSON.stringify([0, broadcastCode, request.sub, byeMsg('stream1', 0)])
557-
})
558-
socket.onmessage({
559-
data: JSON.stringify([0, unicastCode, request.sub, msg('stream1', 1, sub.id)])
560-
})
561-
socket.onmessage({
562-
data: JSON.stringify([0, resentCode, request.sub, {channel:'stream1', sub:sub.id}])
563-
})
518+
socket.fakeReceive([0, resendingCode, request.sub, {channel:'stream1', partition: 0}])
519+
socket.fakeReceive([0, broadcastCode, request.sub, byeMsg('stream1', 0)])
520+
socket.fakeReceive([0, unicastCode, request.sub, msg('stream1', 1, sub.id)])
521+
socket.fakeReceive([0, resentCode, request.sub, {channel:'stream1', sub:sub.id}])
564522
done()
565523
})
566524
}
@@ -573,101 +531,111 @@ describe('StreamrClient', function() {
573531
describe("message handling", function() {
574532

575533
it('should call the callback when a message is received', function(done) {
576-
var sub = client.subscribe("stream1", function(message) {
534+
client.subscribe("stream1", function() {
577535
done()
578536
})
579537
client.connect()
580538
client.connection.once('subscribed', function() {
581-
client.connection.emit('b', msg("stream1", 0))
539+
const broadcastCode = 0
540+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
582541
})
583542
})
584543

585544
it('should not call the callback nor throw an exception when a message is re-received', function(done) {
586545
var callbackCounter = 0
587546
client.subscribe("stream1", function(message) {
588-
callbackCounter++
589-
if (callbackCounter>1)
590-
throw "Callback called more than once!"
547+
++callbackCounter
548+
assert.equal(callbackCounter, 1)
549+
done()
591550
})
592551
client.connect()
593552

594553
client.connection.once('subscribed', function() {
595-
// Fake messages
596-
client.connection.emit('b', msg("stream1",0))
597-
client.connection.emit('b', msg("stream1",0))
598-
client.connection.emit('b', msg("stream1",0))
599-
done()
600-
})
554+
// Fake message
555+
const broadcastCode = 0
556+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
557+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
558+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
559+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
560+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
561+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
562+
})
601563
})
602564

603565
it('should call the callback once for each message in order', function(done) {
604-
var count = 0
566+
var receivedCounts = []
605567
client.subscribe("stream1", function(message) {
606-
console.log("Count: "+count+", message: "+message.count)
607-
608-
if (message.count !== count)
609-
throw "Message counter: "+message.count+", expected: "+count
610-
611-
if (++count === 3)
568+
receivedCounts.push(message.count)
569+
if (receivedCounts.length === 5) {
570+
assert.deepEqual(receivedCounts, [0, 1, 2, 3, 4])
612571
done()
572+
}
613573
})
614574
client.connect()
615575

616576
client.connection.once('subscribed', function() {
617-
client.connection.emit('b', msg("stream1", 0, {count:0}))
618-
client.connection.emit('b', msg("stream1", 1, {count:1}))
619-
client.connection.emit('b', msg("stream1", 2, {count:2}))
577+
const broadcastCode = 0
578+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0, { count: 0 })])
579+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 1, { count: 1 })])
580+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 2, { count: 2 })])
581+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 3, { count: 3 })])
582+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 4, { count: 4 })])
620583
})
621584
})
622585

623586
it('should emit unsubscribe after processing a message with the bye key', function(done) {
624587
var processed = false
625-
client.subscribe("stream1", function(message) {
626-
processed = true
627-
})
588+
client.subscribe("stream1", function(message) { processed = true })
628589
client.connect()
629590

630591
client.connection.once('subscribed', function() {
631-
client.connection.emit('b', byeMsg("stream1", 0))
592+
const broadcastCode = 0
593+
socket.fakeReceive([0, broadcastCode, null, byeMsg("stream1", 0)])
632594
})
633595

634596
client.connection.once('unsubscribed', function(response) {
635-
if (processed && response.channel==='stream1')
636-
done()
597+
assert.equal(processed, true)
598+
assert.equal(response.channel, 'stream1')
599+
done()
637600
})
638601
})
639602

640603
it('should direct messages to specific subscriptions if the messages contain the _sub key', function(done) {
641-
var sub1 = client.subscribe("stream1", function(message) {
642-
throw "sub1 should not have received a message!"
643-
})
644-
sub1.counter = 0
604+
var numReceived = 0
605+
var sub1 = client.subscribe("stream1", function(message) {
606+
++numReceived
607+
if (numReceived === 2) {
608+
done()
609+
}
610+
})
645611

646612
var sub2 = client.subscribe("stream1", function(message) {
647-
done()
613+
throw "sub1 should not have received a message!"
648614
})
649-
sub2.counter = 0
650615

651616
client.connect()
652617
sub2.on('subscribed', function() {
618+
const broadcastCode = 0
619+
const unicastCode = 1
620+
653621
assert.throws(function() {
654622
// Received by sub2
655-
client.connection.emit('b', msg('stream1', 0, {}))
623+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0)])
656624
})
657-
client.connection.emit('u', msg('stream1', 0, {}, sub2.id))
625+
socket.fakeReceive([0, unicastCode, sub1.id, msg("stream1", 1)])
658626
})
659627
})
660628

661629
it('should not call the handlers with any additional keys present in the message', function(done) {
662-
var sub = client.subscribe("stream1", function(message) {
663-
console.log(message)
664-
assert.equal(Object.keys(message).length, 1)
630+
client.subscribe("stream1", function(message) {
631+
assert.deepEqual(message, { count: 0 })
665632
done()
666633
})
667634
client.connect()
668635

669636
client.connection.once('subscribed', function() {
670-
client.connection.emit('b', msg("stream1", 0, {count:0}))
637+
const broadcastCode = 0
638+
socket.fakeReceive([0, broadcastCode, null, msg("stream1", 0, { count: 0 })])
671639
})
672640
})
673641

0 commit comments

Comments
 (0)