Skip to content

Commit

Permalink
Merge branch 'GPII-4480'
Browse files Browse the repository at this point in the history
* GPII-4480:
  GPII-4480: Converted watcher tests to use IoC test framework.
  GPII-4480: Updated to infusion with resource loader fix.
  GPII-4480:  Updated to FLUID-6145 derivative to demonstrate resource loading issue.
  GPII-4480: Updated to newer FLUID-6148 variant of Infusion to demonstrate further problems.
  GPII-4480: Update to work with newer 'mainline' infusion.
  • Loading branch information
amb26 committed May 26, 2020
2 parents dd16a5d + 418608b commit bbf8699
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 89 deletions.
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@
"posttest": "node node_modules/nyc/bin/nyc.js report --reporter text-summary --reporter html"
},
"dependencies": {
"chokidar": "3.3.0",
"gpii-binder": "1.0.6",
"chokidar": "3.4.0",
"gpii-binder": "1.1.0",
"gpii-express": "1.0.15",
"handlebars": "4.5.3",
"infusion": "3.0.0-dev.20191123T235705Z.4cd1b9d90.FLUID-6426",
"json5": "2.1.1",
"handlebars": "4.7.6",
"infusion": "3.0.0-dev.20200525T143422Z.f7f6aab.FLUID-6148",
"json5": "2.1.3",
"markdown-it": "10.0.0",
"md5": "2.2.1",
"underscore-node": "0.1.2"
},
"devDependencies": {
"cheerio": "1.0.0-rc.3",
"eslint": "6.7.1",
"eslint": "7.0.0",
"eslint-config-fluid": "1.4.0",
"gpii-grunt-lint-all": "1.0.5",
"gpii-grunt-lint-all": "1.0.7",
"gpii-testem": "2.1.11",
"graceful-fs": "4.2.3",
"grunt": "1.0.4",
"kettle": "1.11.0",
"graceful-fs": "4.2.4",
"grunt": "1.1.0",
"kettle": "1.12.0",
"mkdirp": "0.5.1",
"node-jqunit": "1.1.8",
"nyc": "14.1.1",
"recursive-copy": "2.0.10",
"request": "2.88.0",
"rimraf": "3.0.0",
"testem": "3.0.1",
"tough-cookie": "3.0.1"
"request": "2.88.2",
"rimraf": "3.0.2",
"testem": "3.1.0",
"tough-cookie": "4.0.0"
}
}
11 changes: 11 additions & 0 deletions src/js/client/serverResourceAware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
events: {
onRendererAvailable: null
},
// TODO: Remove
listeners: {
"onResourcesLoaded.logTemplates": {
funcName: "console.log",
args: ["TEMPLATES:", "@expand:JSON.stringify({that}.model.templates, null, 2)"]
},
"onResourcesLoaded.logMessages": {
funcName: "console.log",
args: ["MESSAGES:", "@expand:JSON.stringify({that}.model.messages, null, 2)"]
}
},
resources: {
messages: {
url: "/messages",
Expand Down
2 changes: 1 addition & 1 deletion tests/js/server/all-server-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ require("./inlineMessageBundlingMiddleware-tests");
require("./live-reload-tests");
require("./resolver-tests");
require("./singleTemplateMiddleware-tests");
require("./watcher-tests");

// Non-IoC tests
require("./first-matching-path-tests");
require("./i18n-unit-tests");
require("./inline-tests");
require("./watcher-tests");
236 changes: 162 additions & 74 deletions tests/js/server/watcher-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,94 +110,182 @@ fluid.defaults("gpii.tests.handlebars.watcher", {
}
});

jqUnit.module("File change 'watcher' tests.");

jqUnit.asyncTest("We should be able to detect a file that has been added...", function () {
var watcherComponent = gpii.tests.handlebars.watcher({});

var newFilePath = path.resolve(watcherComponent.options.watchDirs[0], "newfile.txt");

watcherComponent.events.onFsChange.addListener(function (eventName, path) {
jqUnit.start();
jqUnit.assertEquals("A file 'add' event should have been fired...", "add", eventName);
jqUnit.assertEquals("The path should be correct...", newFilePath, path);
watcherComponent.destroy();
});

watcherComponent.events.onReady.addListener(function () {
fluid.log("Adding new file '", newFilePath, "'...");
fs.writeFileSync(newFilePath, "This is new file content.");
});
fluid.defaults("gpii.tests.handlebars.watcher.startSequenceElement", {
gradeNames: "fluid.test.sequenceElement",
sequence: [
{
func: "{gpii.tests.handlebars.watcher.environment}.events.createWatcher.fire"
},
{
event: "{gpii.tests.handlebars.watcher.environment}.events.watcherReady",
listener: "fluid.identity"
}
]
});

jqUnit.asyncTest("We should be able to detect a file that has been changed...", function () {
// We need to set the directory ourself so that we can create the file before we start "watching"
var tmpPath = path.resolve(os.tmpdir(), "watcher-change-tests-" + Math.random() * 99999 );
mkdirp.sync(tmpPath);
var toBeChanged = path.resolve(tmpPath, "to-be-changed.txt");

// Create the file.
fs.writeFileSync(toBeChanged, "Original.");
fluid.defaults("gpii.tests.handlebars.watcher.sequenceGrade", {
gradeNames: "fluid.test.sequence",
sequenceElements: {
startWatcher: {
gradeNames: "gpii.tests.handlebars.watcher.startSequenceElement",
priority: "before:sequence"
}
}
});

var watcherComponent = gpii.tests.handlebars.watcher({ watchDirs: [tmpPath] });
fluid.defaults("gpii.tests.handlebars.watcher.caseHolder", {
gradeNames: ["fluid.test.testCaseHolder"],
modules: [{
name: "File change 'watcher' tests.",
tests: [
{
name: "We should be able to detect a file that has been added.",
sequenceGrade: "gpii.tests.handlebars.watcher.sequenceGrade",
sequence: [
{
func: "gpii.tests.handlebars.watcher.caseHolder.addFile",
args: ["{testEnvironment}.watcher", "newFile.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.checkFsChangeResults",
args: ["{testEnvironment}.watcher", "newFile.txt", "add", "{arguments}.0", "{arguments}.1"] // watcherComponent, filename, expectedEventName, eventName, eventFilePath
}
]
},
{
name: "We should be able to detect a file that has been changed.",
sequenceGrade: "gpii.tests.handlebars.watcher.sequenceGrade",
sequence: [
{
func: "gpii.tests.handlebars.watcher.caseHolder.addFile",
args: ["{testEnvironment}.watcher", "changingFile.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.changeFile",
args: ["{testEnvironment}.watcher", "changingFile.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.checkFsChangeResults",
args: ["{testEnvironment}.watcher", "changingFile.txt", "change", "{arguments}.0", "{arguments}.1"] // watcherComponent, filename, expectedEventName, eventName, eventFilePath
}
]
},
{
name: "We should be able to detect a file that has been deleted.",
sequenceGrade: "gpii.tests.handlebars.watcher.sequenceGrade",
sequence: [
{
func: "gpii.tests.handlebars.watcher.caseHolder.addFile",
args: ["{testEnvironment}.watcher", "bornToDie.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.removeFile",
args: ["{testEnvironment}.watcher", "bornToDie.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.checkFsChangeResults",
args: ["{testEnvironment}.watcher", "bornToDie.txt", "unlink", "{arguments}.0", "{arguments}.1"] // watcherComponent, filename, expectedEventName, eventName, eventFilePath
}
]
},
{
name: "Large file writes should be handled correctly.",
sequenceGrade: "gpii.tests.handlebars.watcher.sequenceGrade",
sequence: [
{
func: "gpii.tests.handlebars.watcher.caseHolder.addLargeFile",
args: ["{testEnvironment}.watcher", "largeFile.txt"] // watcherComponent, filename
},
{
event: "{testEnvironment}.events.onFsChange",
listener: "gpii.tests.handlebars.watcher.caseHolder.checkLargeFileChangeResults",
args: ["{testEnvironment}.watcher", "largeFile.txt", "add", "{arguments}.0", "{arguments}.1"] // watcherComponent, filename, expectedEventName, eventName, eventFilePath
}
]
}
]
}]
});

watcherComponent.events.onReady.addListener(function () {
watcherComponent.events.onFsChange.addListener(function (eventName, path) {
jqUnit.start();
jqUnit.assertEquals("A file 'change' event should have been fired...", "change", eventName);
jqUnit.assertEquals("The path should be correct...", toBeChanged, path);
gpii.tests.handlebars.watcher.caseHolder.checkFsChangeResults = function (watcherComponent, filename, expectedEventName, eventName, eventFilePath) {
var expectedFilePath = path.resolve(watcherComponent.options.watchDirs[0], filename);

watcherComponent.destroy();
});
jqUnit.assertEquals("The event name should be as expected.", expectedEventName, eventName);
jqUnit.assertEquals("The path should be correct.", expectedFilePath, eventFilePath);
};

// Update the file
fs.writeFileSync(toBeChanged, "Updated.");
});
});
gpii.tests.handlebars.watcher.caseHolder.addFile = function (watcherComponent, filename) {
var newFilePath = path.resolve(watcherComponent.options.watchDirs[0], filename);

jqUnit.asyncTest("We should be able to detect a file that has been deleted...", function () {
var watcherComponent = gpii.tests.handlebars.watcher({});
fluid.log("Adding new file '", newFilePath, "'.");
fs.writeFileSync(newFilePath, "This is new file content.");
};

var toBeDeleted = path.resolve(watcherComponent.options.watchDirs[0], "to-be-deleted.txt");
gpii.tests.handlebars.watcher.caseHolder.changeFile = function (watcherComponent, filename) {
var toBeChanged = path.resolve(watcherComponent.options.watchDirs[0], filename);

watcherComponent.events.onReady.addListener(function () {
// Create the file before listening to changes.
fs.writeFileSync(toBeDeleted, "Don't get comfortable.");
// Update the file
fs.writeFileSync(toBeChanged, "Updated.");
};

// Because awaitWriteFinish is set, we need to give the above add ~2 seconds to complete before listening for and then making a change.
setTimeout(function () {
watcherComponent.events.onFsChange.addListener(function (eventName, path) {
jqUnit.start();
jqUnit.assert("A file 'unlink' event should have been fired...", "unlink", eventName);
jqUnit.assertEquals("The path should be correct...", toBeDeleted, path);
watcherComponent.destroy();
});
gpii.tests.handlebars.watcher.caseHolder.removeFile = function (watcherComponent, filename) {
var toBeDeleted = path.resolve(watcherComponent.options.watchDirs[0], filename);
fs.unlinkSync(toBeDeleted);
};

fluid.log("Removing file '", toBeDeleted, "'...");
fs.unlinkSync(toBeDeleted);
}, 2500);
});
});
gpii.tests.handlebars.watcher.caseHolder.addLargeFile = function (watcherComponent, filename) {
var largeFilePath = path.resolve(watcherComponent.options.watchDirs[0], filename);

jqUnit.asyncTest("Large writes should be complete before we are notified of a file change...", function () {
var watcherComponent = gpii.tests.handlebars.watcher({});
fluid.log("Adding large file '", largeFilePath, "'.");
fs.writeFileSync(largeFilePath, gpii.tests.handlebars.watcher.getLargeFilePayload());
};

var templateContent = (fluid.generate(1024 * 1024 * 25, "X")).join(""); // ~25Mb of "X" characters.
gpii.tests.handlebars.watcher.getLargeFilePayload = function () {
return (fluid.generate(1024 * 1024 * 25, "X")).join(""); // ~25Mb of "X" characters.
};

var templatePath = path.resolve(watcherComponent.options.watchDirs[0], "large-file.txt");
gpii.tests.handlebars.watcher.caseHolder.checkLargeFileChangeResults = function (watcherComponent, filename, expectedEventName, eventName, eventFilePath) {
// Run the basic checks.
gpii.tests.handlebars.watcher.caseHolder.checkFsChangeResults(watcherComponent, filename, expectedEventName, eventName, eventFilePath);

watcherComponent.events.onFsChange.addListener(function (eventName, path) {
jqUnit.start();
jqUnit.assertEquals("A file 'add' event should have been fired...", "add", eventName);
jqUnit.assertEquals("The path should be correct...", templatePath, path);
var fileContent = fs.readFileSync(templatePath, "utf8");
jqUnit.assertEquals("The file's contents should be correct...", templateContent, fileContent);
fs.unlinkSync(templatePath);
watcherComponent.destroy();
});
// Additionally, check that the content was completely written.
var largeFilePath = path.resolve(watcherComponent.options.watchDirs[0], filename);
var fileContent = fs.readFileSync(largeFilePath, "utf8");
var expectedFileContents = gpii.tests.handlebars.watcher.getLargeFilePayload();
jqUnit.assertEquals("The file's contents should be correct...", expectedFileContents, fileContent);
};

watcherComponent.events.onReady.addListener(function () {
fluid.log("Writing large template file to '", templatePath, "'...");
fs.writeFileSync(templatePath, templateContent);
});
fluid.defaults("gpii.tests.handlebars.watcher.environment", {
gradeNames: ["fluid.test.testEnvironment"],
events: {
createWatcher: null,
watcherReady: null,
onFsChange: null
},
components: {
caseHolder: {
type: "gpii.tests.handlebars.watcher.caseHolder"
},
watcher: {
type: "gpii.tests.handlebars.watcher",
createOnEvent: "createWatcher",
options: {
listeners: {
"onReady.notifyParent": {
func: "{gpii.tests.handlebars.watcher.environment}.events.watcherReady.fire"
},
"onFsChange.notifyParent": {
func: "{gpii.tests.handlebars.watcher.environment}.events.onFsChange.fire"
}
}
}
}
}
});

fluid.test.runTests("gpii.tests.handlebars.watcher.environment");

0 comments on commit bbf8699

Please sign in to comment.