From aa2e7fd917fcaf7f79019a95903e7184eb24876a Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Fri, 20 Nov 2015 19:30:51 +0100
Subject: [PATCH] Added jsconfig.json, fixed tests for large numbers

---
 .vscode/launch.json    | 30 +++++++++++++++++++++
 .vscode/settings.json  |  3 +++
 jsconfig.json          |  6 +++++
 src/Connector.js       | 36 ++++++++++++-------------
 src/Connectors/Test.js | 61 +++++++++++++++++++++++++-----------------
 src/Database.js        | 13 ++++-----
 src/Database.spec.js   |  2 +-
 src/SpecHelper.js      | 23 +++++++++-------
 src/Types/Map.spec.js  |  4 +--
 9 files changed, 118 insertions(+), 60 deletions(-)
 create mode 100644 .vscode/launch.json
 create mode 100644 .vscode/settings.json
 create mode 100644 jsconfig.json

diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 00000000..833c43e9
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,30 @@
+{
+	"version": "0.2.0",
+	"configurations": [
+		{
+			"name": "Launch",
+			"type": "node",
+			"request": "launch",
+			"program": "node_modules/gulp/bin/gulp.js",
+			"stopOnEntry": false,
+			"args": ["test"],
+			"cwd": ".",
+			"runtimeExecutable": null,
+			"runtimeArgs": [
+				"--nolazy"
+			],
+			"env": {
+				"NODE_ENV": "development"
+			},
+			"externalConsole": false,
+			"sourceMaps": false,
+			"outDir": null
+		},
+		{
+			"name": "Attach",
+			"type": "node",
+			"request": "attach",
+			"port": 5858
+		}
+	]
+}
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 00000000..20af2f68
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+// Place your settings in this file to overwrite default and user settings.
+{
+}
\ No newline at end of file
diff --git a/jsconfig.json b/jsconfig.json
new file mode 100644
index 00000000..56705ead
--- /dev/null
+++ b/jsconfig.json
@@ -0,0 +1,6 @@
+{
+	"compilerOptions": {
+		"target": "ES6",
+		"module": "commonjs"
+	}
+}
\ No newline at end of file
diff --git a/src/Connector.js b/src/Connector.js
index 3fa4ad2a..9ff9db4f 100644
--- a/src/Connector.js
+++ b/src/Connector.js
@@ -189,26 +189,26 @@ module.exports = function (Y) {
         var broadcastHB = !this.broadcastedHB
         this.broadcastedHB = true
         var db = this.y.db
-        this.syncStep2 = new Promise(function (resolve) {
+        var defer = Promise.defer()
+        this.syncStep2 = defer.promise 
+        db.requestTransaction(function * () {
+          yield* this.applyDeleteSet(m.deleteSet)
+          this.store.apply(m.os)
           db.requestTransaction(function * () {
-            yield* this.applyDeleteSet(m.deleteSet)
-            this.store.apply(m.os)
-            db.requestTransaction(function * () {
-              var ops = yield* this.getOperations(m.stateSet)
-              if (ops.length > 0) {
-                m = {
-                  type: 'update',
-                  ops: ops
-                }
-                if (!broadcastHB) { // TODO: consider to broadcast here..
-                  conn.send(sender, m)
-                } else {
-                  // broadcast only once!
-                  conn.broadcast(m)
-                }
+            var ops = yield* this.getOperations(m.stateSet)
+            if (ops.length > 0) {
+              m = {
+                type: 'update',
+                ops: ops
               }
-              resolve()
-            })
+              if (!broadcastHB) { // TODO: consider to broadcast here..
+                conn.send(sender, m)
+              } else {
+                // broadcast only once!
+                conn.broadcast(m)
+              }
+            }
+            defer.resolve()
           })
         })
       } else if (m.type === 'sync done') {
diff --git a/src/Connectors/Test.js b/src/Connectors/Test.js
index 9e798a54..97d7329a 100644
--- a/src/Connectors/Test.js
+++ b/src/Connectors/Test.js
@@ -24,11 +24,16 @@ module.exports = function (Y) {
       }
     },
     whenTransactionsFinished: function () {
-      var ps = []
-      for (var name in this.users) {
-        ps.push(this.users[name].y.db.whenTransactionsFinished())
-      }
-      return Promise.all(ps)
+      var self = this
+      return new Promise (function (resolve) {
+        wait().then(function () {
+          var ps = []
+          for (var name in self.users) {
+            ps.push(self.users[name].y.db.whenTransactionsFinished())
+          }
+          Promise.all(ps).then(resolve)
+        })
+      })
     },
     flushOne: function flushOne () {
       var bufs = []
@@ -46,6 +51,32 @@ module.exports = function (Y) {
       } else {
         return false
       }
+    },
+    flushAll: function () {
+      return new Promise(function (resolve) {
+        // flushes may result in more created operations,
+        // flush until there is nothing more to flush
+        function nextFlush () {
+          var c = globalRoom.flushOne()
+          if (c) {
+            while (c = globalRoom.flushOne()) {
+            }
+            globalRoom.whenTransactionsFinished().then(nextFlush)              
+          } else {
+            setTimeout(function () {
+              var c = globalRoom.flushOne()
+              if (c) {
+                c.then(function () {
+                  globalRoom.whenTransactionsFinished().then(nextFlush)                  
+                })
+              } else {
+                resolve()
+              }
+            }, 10)
+          }
+        }
+        globalRoom.whenTransactionsFinished().then(nextFlush)
+      })
     }
   }
   Y.utils.globalRoom = globalRoom
@@ -88,7 +119,7 @@ module.exports = function (Y) {
         globalRoom.addUser(this)
         super.reconnect()
       }
-      return this.flushAll()
+      return Y.utils.globalRoom.flushAll()
     }
     disconnect () {
       if (!this.isDisconnected()) {
@@ -107,24 +138,6 @@ module.exports = function (Y) {
         yield self.whenTransactionsFinished()
       })
     }
-    flushAll () {
-      return new Promise(function (resolve) {
-        // flushes may result in more created operations,
-        // flush until there is nothing more to flush
-        function nextFlush () {
-          var c = globalRoom.flushOne()
-          if (c) {
-            while (globalRoom.flushOne()) {
-              // nop
-            }
-            globalRoom.whenTransactionsFinished().then(nextFlush)
-          } else {
-            resolve()
-          }
-        }
-        globalRoom.whenTransactionsFinished().then(nextFlush)
-      })
-    }
   }
 
   Y.Test = Test
diff --git a/src/Database.js b/src/Database.js
index e6977888..d733e18a 100644
--- a/src/Database.js
+++ b/src/Database.js
@@ -343,14 +343,15 @@ module.exports = function (Y) {
     requestTransaction (makeGen, callImmediately) {
       if (callImmediately) {
         this.transact(makeGen)
-      } else if (!this.transactionInProgress) {
-        this.transactionInProgress = true
-        var self = this
-        setTimeout(function () {
-          self.transact(makeGen)
-        }, 0)
       } else {
         this.waitingTransactions.push(makeGen)
+        if (!this.transactionInProgress) {
+          this.transactionInProgress = true
+          var self = this
+          setTimeout(function () {
+            self.transact(self.getNextRequest())
+          }, 0)
+        }
       }
     }
   }
diff --git a/src/Database.spec.js b/src/Database.spec.js
index d8ee4922..96b07a96 100644
--- a/src/Database.spec.js
+++ b/src/Database.spec.js
@@ -1,4 +1,4 @@
-/* global async, databases */
+/* global async, databases, describe, beforeEach, afterEach */
 /* eslint-env browser,jasmine,console */
 'use strict'
 
diff --git a/src/SpecHelper.js b/src/SpecHelper.js
index 41f2363d..5a80de99 100644
--- a/src/SpecHelper.js
+++ b/src/SpecHelper.js
@@ -24,7 +24,7 @@ g.g = g
 
 g.YConcurrency_TestingMode = true
 
-jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
+jasmine.DEFAULT_TIMEOUT_INTERVAL = 8000
 
 g.describeManyTimes = function describeManyTimes (times, name, f) {
   for (var i = 0; i < times; i++) {
@@ -36,8 +36,6 @@ g.describeManyTimes = function describeManyTimes (times, name, f) {
   Wait for a specified amount of time (in ms). defaults to 5ms
 */
 function wait (t) {
-  throw new Error("waiting..")
-  console.log("waiting..", t)
   if (t == null) {
     t = 5
   }
@@ -106,24 +104,24 @@ function * applyTransactions (relAmount, numberOfTransactions, objects, users, t
 
 g.applyRandomTransactionsAllRejoinNoGC = async(function * applyRandomTransactions (users, objects, transactions, numberOfTransactions) {
   yield* applyTransactions(1, numberOfTransactions, objects, users, transactions)
-  yield users[0].connector.flushAll()
+  yield Y.utils.globalRoom.flushAll()
   for (var u in users) {
     yield users[u].reconnect()
   }
-  yield users[0].connector.flushAll()
+  yield Y.utils.globalRoom.flushAll()
   yield g.garbageCollectAllUsers(users)
 })
 
 g.applyRandomTransactionsWithGC = async(function * applyRandomTransactions (users, objects, transactions, numberOfTransactions) {
   yield* applyTransactions(1, numberOfTransactions, objects, users.slice(1), transactions)
-  yield users[0].connector.flushAll()
+  yield Y.utils.globalRoom.flushAll()
   yield g.garbageCollectAllUsers(users)
   for (var u in users) {
     // TODO: here, we enforce that two users never sync at the same time with u[0]
     //       enforce that in the connector itself!
     yield users[u].reconnect()
   }
-  yield users[0].connector.flushAll()
+  yield Y.utils.globalRoom.flushAll()
   yield g.garbageCollectAllUsers(users)
 })
 
@@ -158,8 +156,15 @@ g.compareAllUsers = async(function * compareAllUsers (users) {
       allDels2.push(d)
     })
   }
-  yield users[0].connector.flushAll()
+  yield Y.utils.globalRoom.flushAll()
   yield g.garbageCollectAllUsers(users)
+  yield Y.utils.globalRoom.flushAll()
+  var buffer = Y.utils.globalRoom.buffers
+  for (var name in buffer) {
+    if (buffer[name].length > 0) {
+      debugger // not all ops were transmitted..
+    }
+  }
 
   for (var uid = 0; uid < users.length; uid++) {
     var u = users[uid]
@@ -219,7 +224,7 @@ g.compareAllUsers = async(function * compareAllUsers (users) {
 
 g.createUsers = async(function * createUsers (self, numberOfUsers, database) {
   if (Y.utils.globalRoom.users[0] != null) {
-    yield Y.utils.globalRoom.users[0].flushAll()
+    yield Y.utils.globalRoom.flushAll()
   }
   // destroy old users
   for (var u in Y.utils.globalRoom.users) {
diff --git a/src/Types/Map.spec.js b/src/Types/Map.spec.js
index 7cd44d04..2aa9e241 100644
--- a/src/Types/Map.spec.js
+++ b/src/Types/Map.spec.js
@@ -3,7 +3,7 @@
 'use strict'
 
 var Y = require('../SpecHelper.js')
-var numberOfYMapTests = 10
+var numberOfYMapTests = 100
 var repeatMapTeasts = 1
 
 for (let database of databases) {
@@ -16,7 +16,7 @@ for (let database of databases) {
       y2 = this.users[1].root
       y3 = this.users[2].root
       y4 = this.users[3].root
-      flushAll = this.users[0].connector.flushAll
+      flushAll = Y.utils.globalRoom.flushAll
       done()
     }))
     afterEach(async(function * (done) {
-- 
GitLab