Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8ded0d

Browse files
committed
Merge pull request socketio#1903 from rase-/add/volatile-tests
Added tests for volatile
2 parents 086ccd2 + d9996f0 commit b8ded0d

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

test/socket.io.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,208 @@ describe('socket.io', function(){
815815
});
816816
});
817817

818+
it('should not emit volatile event after regular event (polling)', function(done) {
819+
var srv = http();
820+
var sio = io(srv, { transports: ['polling'] });
821+
822+
var counter = 0;
823+
srv.listen(function(){
824+
sio.on('connection', function(s){
825+
s.emit('ev', 'data');
826+
s.volatile.emit('ev', 'data');
827+
});
828+
829+
var socket = client(srv, { transports: ['polling'] });
830+
socket.on('ev', function() {
831+
counter++;
832+
});
833+
});
834+
835+
setTimeout(function() {
836+
expect(counter).to.be(1);
837+
done();
838+
}, 200);
839+
});
840+
841+
it('should not emit volatile event after regular event (ws)', function(done) {
842+
var srv = http();
843+
var sio = io(srv, { transports: ['websocket'] });
844+
845+
var counter = 0;
846+
srv.listen(function(){
847+
sio.on('connection', function(s){
848+
s.emit('ev', 'data');
849+
s.volatile.emit('ev', 'data');
850+
});
851+
852+
var socket = client(srv, { transports: ['websocket'] });
853+
socket.on('ev', function() {
854+
counter++;
855+
});
856+
});
857+
858+
setTimeout(function() {
859+
expect(counter).to.be(1);
860+
done();
861+
}, 200);
862+
});
863+
864+
it('should emit volatile event (polling)', function(done) {
865+
var srv = http();
866+
var sio = io(srv, { transports: ['polling'] });
867+
868+
var counter = 0;
869+
srv.listen(function(){
870+
sio.on('connection', function(s){
871+
// Wait to make sure there are no packets being sent for opening the connection
872+
setTimeout(function() {
873+
s.volatile.emit('ev', 'data');
874+
}, 20);
875+
});
876+
877+
var socket = client(srv, { transports: ['polling'] });
878+
socket.on('ev', function() {
879+
counter++;
880+
});
881+
});
882+
883+
setTimeout(function() {
884+
expect(counter).to.be(1);
885+
done();
886+
}, 200);
887+
});
888+
889+
it('should emit volatile event (ws)', function(done) {
890+
var srv = http();
891+
var sio = io(srv, { transports: ['websocket'] });
892+
893+
var counter = 0;
894+
srv.listen(function(){
895+
sio.on('connection', function(s){
896+
// Wait to make sure there are no packets being sent for opening the connection
897+
setTimeout(function() {
898+
s.volatile.emit('ev', 'data');
899+
}, 20);
900+
});
901+
902+
var socket = client(srv, { transports: ['websocket'] });
903+
socket.on('ev', function() {
904+
counter++;
905+
});
906+
});
907+
908+
setTimeout(function() {
909+
expect(counter).to.be(1);
910+
done();
911+
}, 200);
912+
});
913+
914+
it('should emit only one consecutive volatile event (polling)', function(done) {
915+
var srv = http();
916+
var sio = io(srv, { transports: ['polling'] });
917+
918+
var counter = 0;
919+
srv.listen(function(){
920+
sio.on('connection', function(s){
921+
// Wait to make sure there are no packets being sent for opening the connection
922+
setTimeout(function() {
923+
s.volatile.emit('ev', 'data');
924+
s.volatile.emit('ev', 'data');
925+
}, 20);
926+
});
927+
928+
var socket = client(srv, { transports: ['polling'] });
929+
socket.on('ev', function() {
930+
counter++;
931+
});
932+
});
933+
934+
setTimeout(function() {
935+
expect(counter).to.be(1);
936+
done();
937+
}, 200);
938+
});
939+
940+
it('should emit only one consecutive volatile event (ws)', function(done) {
941+
var srv = http();
942+
var sio = io(srv, { transports: ['websocket'] });
943+
944+
var counter = 0;
945+
srv.listen(function(){
946+
sio.on('connection', function(s){
947+
// Wait to make sure there are no packets being sent for opening the connection
948+
setTimeout(function() {
949+
s.volatile.emit('ev', 'data');
950+
s.volatile.emit('ev', 'data');
951+
}, 20);
952+
});
953+
954+
var socket = client(srv, { transports: ['websocket'] });
955+
socket.on('ev', function() {
956+
counter++;
957+
});
958+
});
959+
960+
setTimeout(function() {
961+
expect(counter).to.be(1);
962+
done();
963+
}, 200);
964+
});
965+
966+
it('should emit regular events after trying a failed volatile event (polling)', function(done) {
967+
var srv = http();
968+
var sio = io(srv, { transports: ['polling'] });
969+
970+
var counter = 0;
971+
srv.listen(function(){
972+
sio.on('connection', function(s){
973+
// Wait to make sure there are no packets being sent for opening the connection
974+
setTimeout(function() {
975+
s.emit('ev', 'data');
976+
s.volatile.emit('ev', 'data');
977+
s.emit('ev', 'data');
978+
}, 20);
979+
});
980+
981+
var socket = client(srv, { transports: ['polling'] });
982+
socket.on('ev', function() {
983+
counter++;
984+
});
985+
});
986+
987+
setTimeout(function() {
988+
expect(counter).to.be(2);
989+
done();
990+
}, 200);
991+
});
992+
993+
it('should emit regular events after trying a failed volatile event (ws)', function(done) {
994+
var srv = http();
995+
var sio = io(srv, { transports: ['websocket'] });
996+
997+
var counter = 0;
998+
srv.listen(function(){
999+
sio.on('connection', function(s){
1000+
// Wait to make sure there are no packets being sent for opening the connection
1001+
setTimeout(function() {
1002+
s.emit('ev', 'data');
1003+
s.volatile.emit('ev', 'data');
1004+
s.emit('ev', 'data');
1005+
}, 20);
1006+
});
1007+
1008+
var socket = client(srv, { transports: ['websocket'] });
1009+
socket.on('ev', function() {
1010+
counter++;
1011+
});
1012+
});
1013+
1014+
setTimeout(function() {
1015+
expect(counter).to.be(2);
1016+
done();
1017+
}, 200);
1018+
});
1019+
8181020
it('should emit message events through `send`', function(done){
8191021
var srv = http();
8201022
var sio = io(srv);

0 commit comments

Comments
 (0)