Skip to content
This repository
Browse code

more extensive tests for executing callbacks in order

  • Loading branch information...
commit 6d51d73d143b28a31163048d605170fcd8e3cb98 1 parent 8c6a03d
albertyfwu authored March 17, 2013

Showing 1 changed file with 112 additions and 30 deletions. Show diff stats Hide diff stats

  1. 142  test/server.js
142  test/server.js
@@ -843,26 +843,33 @@ describe('server', function () {
843 843
       it('should execute in order when message sent (client) (polling)', function (done) {
844 844
         var engine = listen({ allowUpgrades: false }, function (port) {
845 845
           var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
  846
+          var i = 0;
846 847
           var j = 0;
847 848
 
848  
-          function check(temp) {
849  
-            expect(temp).to.be(j);
850  
-            j++;
851  
-          }
  849
+          engine.on('connection', function(conn) {
  850
+            conn.on('message', function(msg) {
  851
+              conn.send(msg);
  852
+            });
  853
+          });
852 854
 
853 855
           socket.on('open', function () {
854  
-            function sendFn(iter) {
855  
-              socket.send('a', function() {
856  
-                check(iter);
857  
-                // send another packet until we've sent 3 total
858  
-                if (j < 3) {
859  
-                  sendFn(iter+1);
860  
-                } else {
861  
-                  done();
862  
-                }
863  
-              });
  856
+            socket.on('message', function(msg) {
  857
+              // send another packet until we've sent 3 total
  858
+              if (++i < 3) {
  859
+                expect(i).to.eql(j);
  860
+                sendFn();
  861
+              } else {
  862
+                done();
  863
+              }
  864
+            });
  865
+
  866
+            function sendFn() {
  867
+              socket.send(j, (function(value) {
  868
+                j++;
  869
+              })(j));
864 870
             }
865  
-            sendFn(0);
  871
+
  872
+            sendFn();
866 873
           });
867 874
         });
868 875
       });
@@ -870,26 +877,101 @@ describe('server', function () {
870 877
       it('should execute in order when message sent (client) (websocket)', function (done) {
871 878
         var engine = listen({ allowUpgrades: false }, function (port) {
872 879
           var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] });
  880
+          var i = 0;
873 881
           var j = 0;
874 882
 
875  
-          function check(temp) {
876  
-            expect(temp).to.be(j);
877  
-            j++;
878  
-          }
  883
+          engine.on('connection', function(conn) {
  884
+            conn.on('message', function(msg) {
  885
+              conn.send(msg);
  886
+            });
  887
+          });
879 888
 
880 889
           socket.on('open', function () {
881  
-            function sendFn(iter) {
882  
-              socket.send('a', function() {
883  
-                check(iter);
884  
-                // send another packet until we've sent 3 total
885  
-                if (j < 3) {
886  
-                  sendFn(iter+1);
887  
-                } else {
888  
-                  done();
889  
-                }
890  
-              });
  890
+            socket.on('message', function(msg) {
  891
+              // send another packet until we've sent 3 total
  892
+              if (++i < 3) {
  893
+                expect(i).to.eql(j);
  894
+                sendFn();
  895
+              } else {
  896
+                done();
  897
+              }
  898
+            });
  899
+
  900
+            function sendFn() {
  901
+              socket.send(j, (function(value) {
  902
+                j++;
  903
+              })(j));
  904
+            }
  905
+
  906
+            sendFn();
  907
+          });
  908
+        });
  909
+      });
  910
+
  911
+      it('should execute in order with payloads (client) (polling)', function (done) {
  912
+        var engine = listen({ allowUpgrades: false }, function (port) {
  913
+          var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
  914
+          var i = 0;
  915
+          var lastCbFired = 0;
  916
+
  917
+          engine.on('connection', function(conn) {
  918
+            conn.on('message', function(msg) {
  919
+              conn.send(msg);
  920
+            });
  921
+          });
  922
+
  923
+          socket.on('open', function () {
  924
+            socket.on('message', function(msg) {
  925
+              expect(msg).to.eql(i + 1);
  926
+              i++;
  927
+            });
  928
+
  929
+            function cb(value) {
  930
+              expect(value).to.eql(lastCbFired + 1);
  931
+              lastCbFired = value;
  932
+              if (value == 3) {
  933
+                done();
  934
+              }
891 935
             }
892  
-            sendFn(0);
  936
+
  937
+            socket.send(1, function() { cb(1); });
  938
+            // 2 and 3 will be in the same payload            
  939
+            socket.send(2, function() { cb(2); });
  940
+            socket.send(3, function() { cb(3); });
  941
+          });
  942
+        });
  943
+      });
  944
+
  945
+      it('should execute in order with payloads (client) (websocket)', function (done) {
  946
+        var engine = listen({ allowUpgrades: false }, function (port) {
  947
+          var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] });
  948
+          var i = 0;
  949
+          var lastCbFired = 0;
  950
+
  951
+          engine.on('connection', function(conn) {
  952
+            conn.on('message', function(msg) {
  953
+              conn.send(msg);
  954
+            });
  955
+          });
  956
+
  957
+          socket.on('open', function () {
  958
+            socket.on('message', function(msg) {
  959
+              expect(msg).to.eql(i + 1);
  960
+              i++;
  961
+            });
  962
+
  963
+            function cb(value) {
  964
+              expect(value).to.eql(lastCbFired + 1);
  965
+              lastCbFired = value;
  966
+              if (value == 3) {
  967
+                done();
  968
+              }
  969
+            }
  970
+
  971
+            socket.send(1, function() { cb(1); });
  972
+            // 2 and 3 will be in the same payload            
  973
+            socket.send(2, function() { cb(2); });
  974
+            socket.send(3, function() { cb(3); });
893 975
           });
894 976
         });
895 977
       });

0 notes on commit 6d51d73

Please sign in to comment.
Something went wrong with that request. Please try again.