Permalink
Browse files

Refactoring some longer methods in order to simplify async debugging

  • Loading branch information...
1 parent 66abdd7 commit 5338d099184dc803e96f09d345ef9a948476521f @lukebayes committed Feb 22, 2011
Showing with 50 additions and 35 deletions.
  1. +13 −9 src/node_should/context.js
  2. +36 −25 src/node_should/runner.js
  3. +1 −1 test/common.js
View
@@ -63,15 +63,8 @@ Context.prototype.addAsyncHandler = function(callback, timeout) {
options.asyncHandlers++;
timeoutId = setTimeout(function() {
- console.log("async timeout exceeded with!: " + timeoutError.stack);
timeoutExecuted = true;
- options.asyncHandlers--;
-
- self._onError({
- error : timeoutError
- });
-
- self._executeNextSetupOrTestOrTeardown(options);
+ self._executeAsyncTimeout(options, timeoutError);
}, timeout);
return function() {
@@ -83,6 +76,17 @@ Context.prototype.addAsyncHandler = function(callback, timeout) {
}
}
+Context.prototype._executeAsyncTimeout = function(options, timeoutError) {
+ console.log("async timeout exceeded with!: " + timeoutError.stack);
+ options.asyncHandlers--;
+
+ this._onError({
+ error : timeoutError
+ });
+
+ this._executeNextSetupOrTestOrTeardown(options);
+}
+
/**
* Will modify the provided arguments hash that may have a single handler argument,
* or a label, followed by a handler function. Regardless of how the args come in,
@@ -213,7 +217,7 @@ Context.prototype._onFailure = function(testHandlerData) {
}
Context.prototype._onError = function(testHandlerData) {
- //console.log(testHandlerData.error.stack);
+ console.log(testHandlerData.error.message);
if (this.listeners('error').length == 0) {
throw testHandlerData.error;
}
View
@@ -45,27 +45,24 @@ Runner.prototype._runFileContent = function(file, content, printers, completeHan
vm.runInNewContext(content, scope, file);
}
+/**
+ * This method creates a new scope object that will be handed to
+ * vm.runInNewScope.
+ *
+ * The named entities that are available on the returned scope wiil be
+ * available to tests in lexical scope.
+ *
+ * The body of this method is necessarily long as each scope should
+ * have it's own execution context in order to avoid asynchronous
+ * interactions across shared state.
+ */
Runner.prototype._createScope = function(file, printers, completeHandler) {
var context = null;
-
- var addPrintersToContext = function(c) {
- printers.forEach(function(printer) {
- printer.addContext(c);
- });
- }
-
- var removeContextFromPrinters = function(c) {
- printers.forEach(function(printer) {
- printer.removeContext(c);
- if (completeHandler && printer.finished) {
- completeHandler.call(this);
- }
- });
- }
+ var self = this;
var createContext = function() {
var c = new Context();
- addPrintersToContext(c);
+ self._addContextToPrinters(printers, c);
if (context == null) {
context = c;
@@ -75,7 +72,7 @@ Runner.prototype._createScope = function(file, printers, completeHandler) {
}
c.addExecutionHandler.apply(c, arguments);
c.execute(function() {
- removeContextFromPrinters(c);
+ self._removeContextFromPrinters(printers, c, completeHandler);
});
if (c.parent) {
context = c.parent;
@@ -87,27 +84,26 @@ Runner.prototype._createScope = function(file, printers, completeHandler) {
scope[k] = global[k];
}
- scope.global = scope;
- scope.root = root;
-
scope.assert = assert;
- scope.require = require;
scope.console = console;
+ scope.global = scope;
+ scope.require = require;
+ scope.root = root;
scope.async = function() {
return context.addAsyncHandler.apply(context, arguments);
};
scope.context = function() {
- createContext.apply(this, arguments);
+ return createContext.apply(this, arguments);
};
scope.should = function() {
- context.addTestHandler.apply(context, arguments);
+ return context.addTestHandler.apply(context, arguments);
};
scope.setup = function() {
- context.addSetupHandler.apply(context, arguments);
+ return context.addSetupHandler.apply(context, arguments);
};
scope.teardown = function() {
- context.addTeardownHandler.apply(context, arguments);
+ return context.addTeardownHandler.apply(context, arguments);
};
scope.ignore = function() {
console.log('Ignore not yet supported...');
@@ -116,5 +112,20 @@ Runner.prototype._createScope = function(file, printers, completeHandler) {
return scope;
}
+Runner.prototype._addContextToPrinters = function(printers, context) {
+ printers.forEach(function(printer) {
+ printer.addContext(context);
+ });
+}
+
+Runner.prototype._removeContextFromPrinters = function(printers, context, completeHandler) {
+ printers.forEach(function(printer) {
+ printer.removeContext(context);
+ if (completeHandler && printer.finished) {
+ completeHandler.call(this);
+ }
+ });
+}
+
exports.Runner = Runner;
View
@@ -85,7 +85,7 @@ process.on('exit', function() {
if (!found) {
console.error('Unknown global: %s', x);
- assert.ok(false, 'Unknown global founded');
+ assert.ok(false, 'Unknown global found');
}
}
});

0 comments on commit 5338d09

Please sign in to comment.