Merge pull request #117 from andrerod/dev

Improve RoleEnvironment eventEmitter
This commit is contained in:
André Rodrigues 2012-03-16 13:10:48 -07:00
Родитель e4ea57288f 5618502cc8
Коммит 373b563120
3 изменённых файлов: 52 добавлений и 16 удалений

Просмотреть файл

@ -40,7 +40,9 @@ var currentGoalState = null;
var currentEnvironmentData = null;
var lastState = null;
var maxDateTime = new Date('9999-12-31T23:59:59.9999999');
var eventEmitter = new events.EventEmitter();
// Merge event emitter properties into the RoleEnvironment (export) object.
RoleEnvironment = azureutil.merge(RoleEnvironment, new events.EventEmitter());
/**
* Returns a RoleInstance object that represents the role instance
@ -246,11 +248,6 @@ RoleEnvironment.clearStatus = function (callback) {
});
};
// TODO: find a better way to hook eventing up
RoleEnvironment.on = function (event, callback) {
eventEmitter.on(event, callback);
};
RoleEnvironment._initialize = function (callback) {
var getCurrentGoalState = function (finalCallback) {
RoleEnvironment.runtimeClient.getCurrentGoalState(function (error, goalState) {
@ -289,9 +286,9 @@ RoleEnvironment._initialize = function (callback) {
} else {
RoleEnvironment.runtimeClient = rtClient;
getCurrentGoalState(function (error) {
if (error) {
callback(error);
getCurrentGoalState(function (errorGetCurrentGoalState) {
if (errorGetCurrentGoalState) {
callback(errorGetCurrentGoalState);
} else {
if (RoleEnvironment.runtimeClient.listeners(ServiceRuntimeConstants.CHANGED).length === 0) {
RoleEnvironment.runtimeClient.on(ServiceRuntimeConstants.CHANGED, function (newGoalState) {
@ -336,7 +333,7 @@ RoleEnvironment._processGoalStateChange = function (newGoalState, callback) {
if (changes.length === 0) {
RoleEnvironment._acceptLatestIncarnation(newGoalState, last);
} else {
eventEmitter.emit(ServiceRuntimeConstants.CHANGING, changes);
RoleEnvironment.emit(ServiceRuntimeConstants.CHANGING, changes);
RoleEnvironment._acceptLatestIncarnation(newGoalState, last);
@ -346,7 +343,7 @@ RoleEnvironment._processGoalStateChange = function (newGoalState, callback) {
} else {
currentEnvironmentData = environmentData;
eventEmitter.emit(ServiceRuntimeConstants.CHANGED, changes);
RoleEnvironment.emit(ServiceRuntimeConstants.CHANGED, changes);
callback();
}
});
@ -506,5 +503,5 @@ RoleEnvironment._calculateChanges = function (callback) {
};
RoleEnvironment._raiseStoppingEvent = function () {
eventEmitter.emit(ServiceRuntimeConstants.STOPPING);
RoleEnvironment.emit(ServiceRuntimeConstants.STOPPING);
};

Просмотреть файл

@ -195,4 +195,28 @@ exports.stringIsDate = function(date) {
}
return !isNaN(date.getTime());
};
};
/**
* Merges multiple objects.
*
* @param {object} object The objects to be merged
* @return {object} The merged object.
*/
exports.merge = function () {
var source, sourceValue;
var target = arguments[0] || {};
var length = arguments.length;
for (var i = 1; i < length; i++) {
if ((source = arguments[i]) != null) {
for (var property in source) {
sourceValue = source[property];
if (sourceValue !== undefined) {
target[property] = sourceValue;
}
}
}
}
return target;
}

Просмотреть файл

@ -62,6 +62,22 @@ suite('roleenvironment-tests', function () {
done();
});
test('eventEmitter', function (done) {
// No event listeners registered yet
assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 0);
// Register one event listener
var listener = function () { };
azure.RoleEnvironment.on(ServiceRuntimeConstants.CHANGED, listener);
assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 1);
// Remove the event listener
azure.RoleEnvironment.removeListener(ServiceRuntimeConstants.CHANGED, listener);
assert.equal(azure.RoleEnvironment.listeners(ServiceRuntimeConstants.CHANGED).length, 0);
done();
});
test('IsAvailable', function (done) {
azure.RoleEnvironment.isAvailable(function (error1, isAvailable1) {
assert.notEqual(error1, null);
@ -163,7 +179,6 @@ suite('roleenvironment-tests', function () {
}
};
var originalFileInputChannelReadData = runtimeKernel.fileInputChannel._readData;
runtimeKernel.fileInputChannel._readData = function (name, callback) {
if (name === 'C:\\file.xml') {
callback(undefined,
@ -609,7 +624,7 @@ suite('roleenvironment-tests', function () {
});
// Make sure incarnation 1 is read
azure.RoleEnvironment.getConfigurationSettings(function (error, configurationSettings) {
azure.RoleEnvironment.getConfigurationSettings(function (error) {
// Update to incarnation 2
goalStateXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<GoalState xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
@ -735,7 +750,7 @@ suite('roleenvironment-tests', function () {
});
// Make sure incarnation 1 is read
azure.RoleEnvironment.getConfigurationSettings(function (error, configurationSettings) {
azure.RoleEnvironment.getConfigurationSettings(function (error) {
// Update to incarnation 2
goalStateXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<GoalState xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +