@@ -3,12 +3,12 @@ var assert = require('chai').assert
33var http = require ( 'http' )
44var parallel = require ( 'run-parallel' )
55var SolidWs = require ( '../' )
6+ var EventEmitter = require ( 'events' ) . EventEmitter
67
78describe ( 'Solid-ws' , function ( ) {
89 var server = http . createServer ( )
910 var port = 8000
1011 var pubsub = SolidWs ( server )
11- var client
1212
1313 function check ( msgs , uris , done ) {
1414 parallel ( msgs . map ( function ( msg , i ) {
@@ -28,13 +28,22 @@ describe('Solid-ws', function() {
2828
2929 before ( function ( done ) {
3030 server . listen ( port , function ( err ) {
31- if ( err ) done ( err )
32- client = new WebSocket ( 'http://localhost:' + port )
33- client . on ( 'open' , done )
31+ done ( err )
3432 } )
3533 } )
34+ after ( function ( ) {
35+ server . close ( )
36+ } )
3637
3738 describe ( 'sub' , function ( ) {
39+ beforeEach ( function ( done ) {
40+ client = new WebSocket ( 'http://localhost:' + port )
41+ client . on ( 'open' , done )
42+ } )
43+ afterEach ( function ( done ) {
44+ client . close ( )
45+ done ( )
46+ } )
3847 it ( 'should receive ack for any resource given' , function ( done ) {
3948
4049 var uris = [
@@ -78,54 +87,74 @@ describe('Solid-ws', function() {
7887 } )
7988 } )
8089 } )
81- // describe('pub', function() {
82- // describe('delete resource', function() {
83-
84- // it('should', function(done) {
85- // server.emit('pub '+ url)
86-
87- // client.onmessage = function(msg) {
88- // assert.ok(msg)
89- // assert.equal('pub ' + url)
90- // done()
91- // }
92- // })
93- // })
94- // describe('patch resource', function() {
95-
96- // it('should', function(done) {
97- // server.emit('pub '+ url)
98-
99- // client.onmessage = function(msg) {
100- // assert.ok(msg)
101- // assert.equal('pub ' + url)
102- // done()
103- // }
104- // })
105- // })
106- // describe('put resource', function() {
107-
108- // it('should', function(done) {
109- // server.emit('pub '+ url)
110-
111- // client.onmessage = function(msg) {
112- // assert.ok(msg)
113- // assert.equal('pub ' + url)
114- // done()
115- // }
116- // })
117- // })
118- // describe('add resource in folder', function() {
119-
120- // it('should', function(done) {
121- // server.emit('pub '+ url)
122-
123- // client.onmessage = function(msg) {
124- // assert.ok(msg)
125- // assert.equal('pub ' + url)
126- // done()
127- // }
128- // })
129- // })
130- // })
90+
91+ describe ( 'pub' , function ( ) {
92+ it ( 'should be received by all the clients subscribed to a resource' , function ( done ) {
93+
94+ var url = 'http://example.com/resource.ttl'
95+ var users = [
96+ 'http://nicola.io#me' ,
97+ 'http://timbl.com#me' ,
98+ 'http://deiu.io#me' ]
99+
100+ var clients = users . map ( function ( ) {
101+ return new WebSocket ( 'http://localhost:' + port )
102+ } )
103+
104+ var pubs = [ ]
105+ var workflow = new EventEmitter ( )
106+ workflow . on ( 'done' , function ( ) {
107+ assert . equal ( pubs . length , users . length )
108+ done ( )
109+ } )
110+
111+ connectAll ( clients , function ( ) {
112+ ackAll ( clients , function ( ) {
113+ pubAll ( clients , function ( ) {
114+ done ( )
115+ } )
116+ pubsub . publish ( url )
117+ } )
118+ } )
119+
120+ function connectAll ( clients , done ) {
121+ parallel ( clients . map ( function ( client ) {
122+ return function ( cb ) {
123+ client . on ( 'open' , function ( ) {
124+ client . send ( 'sub ' + url )
125+ cb ( )
126+ } )
127+ }
128+ } ) , done )
129+ }
130+
131+ function ackAll ( clients , done ) {
132+ parallel ( clients . map ( function ( client ) {
133+ return function ( cb ) {
134+ client . on ( 'message' , function ( msg ) {
135+ if ( msg . split ( ' ' ) [ 0 ] === 'ack' ) {
136+ cb ( )
137+ return ;
138+ }
139+ } )
140+ }
141+ } ) , done )
142+ }
143+
144+ function pubAll ( clients , done ) {
145+ parallel ( clients . map ( function ( client ) {
146+ return function ( cb ) {
147+ client . on ( 'message' , function ( msg ) {
148+ if ( msg . split ( ' ' ) [ 0 ] === 'pub' ) {
149+ pubs . push ( msg )
150+ cb ( )
151+ return ;
152+ }
153+ } )
154+ }
155+ } ) , done )
156+ }
157+
158+ } )
159+ } )
131160} )
0 commit comments