From a93d857d7d6180c68e1d351b1ade3836dde0373c Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Wed, 21 Mar 2018 15:11:26 +0400 Subject: [PATCH 1/2] Refactored tests to run using `meteortesting:mocha` --- .npm/package/.gitignore | 1 + .npm/package/README | 7 + .npm/package/npm-shrinkwrap.json | 40 ++++ README.md | 6 +- package.js | 8 +- test/job_collection_tests.coffee | 364 ++++++++++++++++--------------- 6 files changed, 243 insertions(+), 183 deletions(-) create mode 100644 .npm/package/.gitignore create mode 100644 .npm/package/README create mode 100644 .npm/package/npm-shrinkwrap.json diff --git a/.npm/package/.gitignore b/.npm/package/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.npm/package/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.npm/package/README b/.npm/package/README new file mode 100644 index 0000000..3d49255 --- /dev/null +++ b/.npm/package/README @@ -0,0 +1,7 @@ +This directory and the files immediately inside it are automatically generated +when you change this package's NPM dependencies. Commit the files in this +directory (npm-shrinkwrap.json, .gitignore, and this README) to source control +so that others run the same versions of sub-dependencies. + +You should NOT check in the node_modules directory that Meteor automatically +creates; if you are using git, the .gitignore file tells git to ignore it. diff --git a/.npm/package/npm-shrinkwrap.json b/.npm/package/npm-shrinkwrap.json new file mode 100644 index 0000000..0fa7a48 --- /dev/null +++ b/.npm/package/npm-shrinkwrap.json @@ -0,0 +1,40 @@ +{ + "lockfileVersion": 1, + "dependencies": { + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==" + }, + "chai": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.1.2.tgz", + "integrity": "sha1-D2RYS6ZC8PKs4oBiefTwbKI61zw=" + }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=" + }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==" + }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=" + }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=" + }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==" + } + } +} diff --git a/README.md b/README.md index a786327..437316f 100644 --- a/README.md +++ b/README.md @@ -245,11 +245,11 @@ To add to your project, run: The package exposes a global object `JobCollection` on both client and server. -To run tests (using Meteor tiny-test) run from within the `job-collection` subdirectory: +To run tests (using Meteor `meteortesting:mocha`) run from within the `job-collection` subdirectory: - meteor test-packages ./ + meteor test-packages ./ --driver-package meteortesting:mocha -Load `http://localhost:3000/` and the tests should run in your browser and on the server. +Load `http://localhost:3000/` to run the client-tests in the browser or follow the instructions on https://github.com/meteortesting/meteor-mocha#run-client-tests-headless. A basic sample application that implements a basic image gallery with upload/download support and automatic generation of thumbnail images is available. It also implements a basic job manager UI diff --git a/package.js b/package.js index 30c9975..c3bbc38 100644 --- a/package.js +++ b/package.js @@ -31,8 +31,12 @@ Package.onTest(function (api) { api.use('mrt:later@1.6.1', ['server','client']); api.use('coffeescript@1.12.6_1', ['server','client']); api.use('check@1.2.5', ['server','client']); - api.use('tinytest@1.0.12', ['server','client']); - api.use('test-helpers@1.0.11', ['server','client']); + api.use('meteortesting:mocha@0.4.1', ['server','client']); api.use('ddp@1.2.5', 'client'); + + Npm.depends({ + chai: '4.1.2', + }); + api.addFiles('test/job_collection_tests.coffee', ['server', 'client']); }); diff --git a/test/job_collection_tests.coffee b/test/job_collection_tests.coffee index e923d0e..996eb9b 100644 --- a/test/job_collection_tests.coffee +++ b/test/job_collection_tests.coffee @@ -1,3 +1,5 @@ +assert = require('chai').assert; + ############################################################################ # Copyright (C) 2014-2017 by Vaughn Iverson # job-collection is free software released under the MIT/X11 license. @@ -27,18 +29,18 @@ defaultColl = new JobCollection() validJobDoc = (d) -> Match.test(d, defaultColl.jobDocPattern) -Tinytest.add 'JobCollection default constructor', (test) -> - test.instanceOf defaultColl, JobCollection, "JobCollection constructor failed" - test.equal defaultColl.root, 'queue', "default root isn't 'queue'" +it 'JobCollection constructs the object correctly', () -> + assert.instanceOf defaultColl, JobCollection, "JobCollection constructor failed" + assert.equal defaultColl.root, 'queue', "default root isn't 'queue'" if Meteor.isServer - test.equal defaultColl.stopped, true, "isn't initially stopped" - test.equal defaultColl.logStream, null, "Doesn't have a logStream" - test.instanceOf defaultColl.allows, Object, "allows isn't an object" - test.equal Object.keys(defaultColl.allows).length, 22, "allows not properly initialized" - test.instanceOf defaultColl.denys, Object, "denys isn't an object" - test.equal Object.keys(defaultColl.denys).length, 22, "denys not properly initialized" + assert.equal defaultColl.stopped, true, "isn't initially stopped" + assert.equal defaultColl.logStream, null, "Doesn't have a logStream" + assert.instanceOf defaultColl.allows, Object, "allows isn't an object" + assert.equal Object.keys(defaultColl.allows).length, 22, "allows not properly initialized" + assert.instanceOf defaultColl.denys, Object, "denys isn't an object" + assert.equal Object.keys(defaultColl.denys).length, 22, "denys not properly initialized" else - test.equal defaultColl.logConsole, false, "Doesn't have a logConsole" + assert.equal defaultColl.logConsole, false, "Doesn't have a logConsole" clientTestColl = new JobCollection 'ClientTest', { idGeneration: 'MONGO' } serverTestColl = new JobCollection 'ServerTest', { idGeneration: 'STRING' } @@ -61,156 +63,156 @@ if Meteor.isServer clientTestColl.allow admin: () -> true - Tinytest.add 'Set permissions to allow admin on ClientTest', (test) -> - test.equal clientTestColl.allows.admin[0](), true + it 'Set permissions to allow admin on ClientTest', () -> + assert.equal clientTestColl.allows.admin[0](), true - Tinytest.add 'Set polling interval', (test) -> + it 'Set polling interval', () -> interval = clientTestColl.interval clientTestColl.promote 250 - test.notEqual interval, clientTestColl.interval, "clientTestColl interval not updated" + assert.notEqual interval, clientTestColl.interval, "clientTestColl interval not updated" interval = serverTestColl.interval serverTestColl.promote 250 - test.notEqual interval, serverTestColl.interval, "serverTestColl interval not updated" + assert.notEqual interval, serverTestColl.interval, "serverTestColl interval not updated" testColl = if Meteor.isClient then clientTestColl else serverTestColl -# Tinytest.addAsync 'Run startJobs on new job collection', (test, onComplete) -> +# it 'Run startJobs on new job collection', (onComplete) -> # testColl.startJobs (err, res) -> -# test.fail(err) if err -# test.equal res, true, "startJobs failed in callback result" +# assert.fail(err) if err +# assert.equal res, true, "startJobs failed in callback result" # if Meteor.isServer -# test.equal testColl.stopped, false, "startJobs didn't start job collection" +# assert.equal testColl.stopped, false, "startJobs didn't start job collection" # onComplete() -Tinytest.addAsync 'Run startJobServer on new job collection', (test, onComplete) -> +it 'Run startJobServer on new job collection', (onComplete) -> testColl.startJobServer (err, res) -> - test.fail(err) if err - test.equal res, true, "startJobServer failed in callback result" + assert.fail(err) if err + assert.equal res, true, "startJobServer failed in callback result" if Meteor.isServer - test.equal testColl.stopped, false, "startJobServer didn't start job collection" + assert.equal testColl.stopped, false, "startJobServer didn't start job collection" onComplete() if Meteor.isServer - Tinytest.addAsync 'Create a server-side job and see that it is added to the collection and runs', (test, onComplete) -> + it 'Create a server-side job and see that it is added to the collection and runs', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { some: 'data' } - test.ok validJobDoc(job.doc) + assert.ok validJobDoc(job.doc) res = job.save() - test.ok validId(res), "job.save() failed in sync result" + assert.ok validId(res), "job.save() failed in sync result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> - test.equal job._doc._id, res + assert.equal job._doc._id, res job.done() cb() ev = testColl.events.once 'jobDone', (msg) -> - test.equal msg.method, 'jobDone' + assert.equal msg.method, 'jobDone' if msg.params[0] is res q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'Create a job and see that it is added to the collection and runs', (test, onComplete) -> +it 'Create a job and see that it is added to the collection and runs', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { some: 'data' } - test.ok validJobDoc(job.doc) + assert.ok validJobDoc(job.doc) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> - test.equal job._doc._id, res + assert.equal job._doc._id, res job.done() cb() q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'Create an invalid job and see that errors correctly propagate', (test, onComplete) -> +it 'Create an invalid job and see that errors correctly propagate', (onComplete) -> console.warn "****************************************************************************************************" console.warn "***** The following exception dump is a Normal and Expected part of error handling unit tests: *****" console.warn "****************************************************************************************************" jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { some: 'data' } delete job.doc.status - test.equal validJobDoc(job.doc), false + assert.equal validJobDoc(job.doc), false if Meteor.isServer eventFlag = false err = null ev = testColl.events.once 'jobSave', (msg) -> eventFlag = true - test.fail(new Error "Server error event didn't dispatch") unless msg.error + assert.fail(new Error "Server error event didn't dispatch") unless msg.error try job.save() catch e err = e finally - test.ok eventFlag - test.fail(new Error "Server exception wasn't thrown") unless err + assert.ok eventFlag + assert.fail(new Error "Server exception wasn't thrown") unless err onComplete() else job.save (err, res) -> - test.fail(new Error "Error did not propagate to Client") unless err + assert.fail(new Error "Error did not propagate to Client") unless err onComplete() -Tinytest.addAsync 'Create a job and then make a new doc with its document', (test, onComplete) -> +it 'Create a job and then make a new doc with its document', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job2 = new Job testColl, jobType, { some: 'data' } if Meteor.isServer job = new Job 'ServerTest', job2.doc else job = new Job 'ClientTest', job2.doc - test.ok validJobDoc(job.doc) + assert.ok validJobDoc(job.doc) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> - test.equal job._doc._id, res + assert.equal job._doc._id, res job.done() cb() q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'A repeating job that returns the _id of the next job', (test, onComplete) -> +it 'A repeating job that returns the _id of the next job', (onComplete) -> counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}).repeat({ repeats: 1, wait: 250 }) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ if counter is 1 - test.equal job.doc._id, res + assert.equal job.doc._id, res job.done "Result1", { repeatId: true }, (err, res) -> - test.fail(err) if err - test.ok res? - test.notEqual res, true + assert.fail(err) if err + assert.ok res? + assert.notEqual res, true testColl.getJob res, (err, j) -> - test.fail(err) if err - test.equal j._doc._id, res + assert.fail(err) if err + assert.equal j._doc._id, res cb() else - test.notEqual job.doc._id, res + assert.notEqual job.doc._id, res job.done "Result2", { repeatId: true }, (err, res) -> - test.fail(err) if err - test.equal res, true + assert.fail(err) if err + assert.equal res, true cb() q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'Dependent jobs run in the correct order', (test, onComplete) -> +it 'Dependent jobs run in the correct order', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.delay 1000 # Ensure that job 1 has the opportunity to run first job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] job2.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" count = 0 q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> count++ - test.equal count, job.data.order + assert.equal count, job.data.order job.done() cb() if count is 2 @@ -218,7 +220,7 @@ Tinytest.addAsync 'Dependent jobs run in the correct order', (test, onComplete) onComplete() if Meteor.isServer - Tinytest.addAsync 'Dry run of dependency check returns status object', (test, onComplete) -> + it 'Dry run of dependency check returns status object', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } @@ -231,137 +233,140 @@ if Meteor.isServer job4.save() job5.depends [job, job2, job3, job4] job5.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job2.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job2.save() failed in callback result" # This creates an inconsistent state testColl.update { _id: job.doc._id, status: 'ready' }, { $set: { status: 'cancelled' }} testColl.update { _id: job2.doc._id, status: 'ready' }, { $set: { status: 'failed' }} testColl.update { _id: job3.doc._id, status: 'ready' }, { $set: { status: 'completed' }} testColl.remove { _id: job4.doc._id } dryRunRes = testColl._checkDeps job5.doc - test.equal dryRunRes.cancelled.length, 1 - test.equal dryRunRes.cancelled[0], job.doc._id - test.equal dryRunRes.failed.length, 1 - test.equal dryRunRes.failed[0], job2.doc._id - test.equal dryRunRes.resolved.length, 1 - test.equal dryRunRes.resolved[0], job3.doc._id - test.equal dryRunRes.removed.length, 1 - test.equal dryRunRes.removed[0], job4.doc._id + assert.equal dryRunRes.cancelled.length, 1 + assert.equal dryRunRes.cancelled[0], job.doc._id + assert.equal dryRunRes.failed.length, 1 + assert.equal dryRunRes.failed[0], job2.doc._id + assert.equal dryRunRes.resolved.length, 1 + assert.equal dryRunRes.resolved[0], job3.doc._id + assert.equal dryRunRes.removed.length, 1 + assert.equal dryRunRes.removed[0], job4.doc._id onComplete() -Tinytest.addAsync 'Dependent job saved after completion of antecedent still runs', (test, onComplete) -> +it 'Dependent job saved after completion of antecedent still runs', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] count = 0 q = testColl.processJobs jobType, { pollInterval: 250 }, (j, cb) -> count++ j.done "Job #{j.data.order} Done", (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res if j.data.order is 1 job2.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job2.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job2.save() failed in callback result" else q.shutdown { level: 'soft', quiet: true }, () -> onComplete() cb() -Tinytest.addAsync 'Dependent job saved after failure of antecedent is cancelled', (test, onComplete) -> +it 'Dependent job saved after failure of antecedent is cancelled', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] q = testColl.processJobs jobType, { pollInterval: 250 }, (j, cb) -> j.fail "Job #{j.data.order} Failed", (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res job2.save (err, res) -> - test.fail(err) if err - test.isNull res, "job2.save() failed in callback result" + assert.fail(err) if err + assert.isNull res, "job2.save() failed in callback result" q.shutdown { level: 'soft', quiet: true }, () -> onComplete() cb() -Tinytest.addAsync 'Dependent job saved after cancelled antecedent is also cancelled', (test, onComplete) -> +it 'Dependent job saved after cancelled antecedent is also cancelled', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] job.cancel (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res job2.save (err, res) -> - test.fail(err) if err - test.isNull res, "job2.save() failed in callback result" + assert.fail(err) if err + assert.isNull res, "job2.save() failed in callback result" onComplete() -Tinytest.addAsync 'Dependent job saved after removed antecedent is cancelled', (test, onComplete) -> +it 'Dependent job saved after removed antecedent is cancelled', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] job.cancel (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res job.remove (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res job2.save (err, res) -> - test.fail(err) if err - test.isNull res, "job2.save() failed in callback result" + assert.fail(err) if err + assert.isNull res, "job2.save() failed in callback result" onComplete() -Tinytest.addAsync 'Cancel succeeds for job without deps, with using option dependents: false', (test, onComplete) -> +it 'Cancel succeeds for job without deps, with using option dependents: false', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, {} job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job.cancel { dependents: false }, (err, res) -> - test.fail(err) if err - test.ok res + assert.fail(err) if err + assert.ok res onComplete() -Tinytest.addAsync 'Dependent job with delayDeps is delayed', (test, onComplete) -> +it 'Dependent job with delayDeps is delayed', (onComplete) -> + @timeout 10000 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job testColl, jobType, { order: 1 } job2 = new Job testColl, jobType, { order: 2 } job.delay 1000 # Ensure that job2 has the opportunity to run first job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" job2.depends [job] job2.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" count = 0 q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> count++ - test.equal count, job.data.order + assert.equal count, job.data.order timer = new Date() job.done(null, { delayDeps: 1500 }) cb() if count is 2 - test.ok new Date() > timer + 1500 + console.log "#{(new Date()).valueOf()} is greater than" + console.log "#{timer.valueOf() + 1500}" + assert.ok (new Date()).valueOf() > timer.valueOf() + 1500 q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'Job priority is respected', (test, onComplete) -> +it 'Job priority is respected', (onComplete) -> counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" jobs = [] @@ -370,33 +375,33 @@ Tinytest.addAsync 'Job priority is respected', (test, onComplete) -> jobs[2] = new Job(testColl, jobType, {count: 2}) jobs[0].save (err, res) -> - test.fail(err) if err - test.ok validId(res), "jobs[0].save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "jobs[0].save() failed in callback result" jobs[1].save (err, res) -> - test.fail(err) if err - test.ok validId(res), "jobs[1].save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "jobs[1].save() failed in callback result" jobs[2].save (err, res) -> - test.fail(err) if err - test.ok validId(res), "jobs[2].save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "jobs[2].save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ - test.equal job.data.count, counter + assert.equal job.data.count, counter job.done() cb() if counter is 3 q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'A forever retrying job can be scheduled and run', (test, onComplete) -> +it 'A forever retrying job can be scheduled and run', (onComplete) -> counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}).retry({retries: testColl.forever, wait: 0}) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ - test.equal job.doc._id, res + assert.equal job.doc._id, res if counter < 3 job.fail('Fail test') cb() @@ -406,16 +411,16 @@ Tinytest.addAsync 'A forever retrying job can be scheduled and run', (test, onCo q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'Retrying job with exponential backoff', (test, onComplete) -> +it 'Retrying job with exponential backoff', (onComplete) -> counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}).retry({retries: 2, wait: 200, backoff: 'exponential'}) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ - test.equal job.doc._id, res + assert.equal job.doc._id, res if counter < 3 job.fail('Fail test') cb() @@ -425,37 +430,39 @@ Tinytest.addAsync 'Retrying job with exponential backoff', (test, onComplete) -> q.shutdown { level: 'soft', quiet: true }, () -> onComplete() -Tinytest.addAsync 'A forever retrying job with "until"', (test, onComplete) -> +it 'A forever retrying job with "until"', (onComplete) -> + @timeout 10000 counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}).retry({until: new Date(new Date().valueOf() + 1500), wait: 500}) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ - test.equal job.doc._id, res + assert.equal job.doc._id, res job.fail('Fail test') cb() Meteor.setTimeout(() -> job.refresh () -> - test.equal job._doc.status, 'failed', "Until didn't cause job to stop retrying" + assert.equal job._doc.status, 'failed', "Until didn't cause job to stop retrying" q.shutdown { level: 'soft', quiet: true }, () -> onComplete() , 2500 ) -Tinytest.addAsync 'Autofail and retry a job', (test, onComplete) -> +it 'Autofail and retry a job', (onComplete) -> + @timeout 10000 counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}).retry({retries: 2, wait: 0}) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250, workTimeout: 500 }, (job, cb) -> counter++ - test.equal job.doc._id, res + assert.equal job.doc._id, res if counter is 2 job.done('Success') # Will be called without done/fail on first attempt @@ -463,7 +470,7 @@ Tinytest.addAsync 'Autofail and retry a job', (test, onComplete) -> Meteor.setTimeout(() -> job.refresh () -> - test.equal job._doc.status, 'completed', "Job didn't successfully autofail and retry" + assert.equal job._doc.status, 'completed', "Job didn't successfully autofail and retry" q.shutdown { level: 'soft', quiet: true }, () -> onComplete() , @@ -472,41 +479,42 @@ Tinytest.addAsync 'Autofail and retry a job', (test, onComplete) -> if Meteor.isServer - Tinytest.addAsync 'Save, cancel, restart, refresh: retries are correct.', (test, onComplete) -> + it 'Save, cancel, restart, refresh: retries are correct.', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" j = new Job(testColl, jobType, { foo: "bar" }) j.save() j.cancel() j.restart({ retries: 0 }) j.refresh() - test.equal j._doc.repeatRetries, j._doc.retries + j._doc.retried + assert.equal j._doc.repeatRetries, j._doc.retries + j._doc.retried onComplete() - Tinytest.addAsync 'Add, cancel and remove a large number of jobs', (test, onComplete) -> + it 'Add, cancel and remove a large number of jobs', (onComplete) -> c = count = 500 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" for i in [1..count] j = new Job(testColl, jobType, { idx: i }) j.save (err, res) -> - test.fail(err) if err - test.fail("job.save() Invalid _id value returned") unless validId(res) + assert.fail(err) if err + assert.fail("job.save() Invalid _id value returned") unless validId(res) c-- unless c ids = testColl.find({ type: jobType, status: 'ready'}).map((d) -> d._id) - test.equal count, ids.length + assert.equal count, ids.length testColl.cancelJobs ids, (err, res) -> - test.fail(err) if err - test.fail("cancelJobs Failed") unless res + assert.fail(err) if err + assert.fail("cancelJobs Failed") unless res ids = testColl.find({ type: jobType, status: 'cancelled'}).map((d) -> d._id) - test.equal count, ids.length + assert.equal count, ids.length testColl.removeJobs ids, (err, res) -> - test.fail(err) if err - test.fail("removeJobs Failed") unless res + assert.fail(err) if err + assert.fail("removeJobs Failed") unless res ids = testColl.find { type: jobType } - test.equal 0, ids.count() + assert.equal 0, ids.count() onComplete() - Tinytest.addAsync 'A forever repeating job with "schedule" and "until"', (test, onComplete) -> + it 'A forever repeating job with "schedule" and "until"', (onComplete) -> + @timeout 10000 counter = 0 jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job(testColl, jobType, {some: 'data'}) @@ -515,65 +523,65 @@ if Meteor.isServer schedule: testColl.later.parse.text("every 1 second")}) .delay(1000) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = testColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> counter++ if counter is 1 - test.equal job.doc._id, res + assert.equal job.doc._id, res else - test.notEqual job.doc._id, res + assert.notEqual job.doc._id, res job.done({}, { repeatId: true }) cb() listener = (msg) -> if counter is 2 job.refresh () -> - test.equal job._doc.status, 'completed' + assert.equal job._doc.status, 'completed' q.shutdown { level: 'soft', quiet: true }, () -> ev.removeListener 'jobDone', listener onComplete() ev = testColl.events.on 'jobDone', listener -# Tinytest.addAsync 'Run stopJobs on the job collection', (test, onComplete) -> +# it 'Run stopJobs on the job collection', (onComplete) -> # testColl.stopJobs { timeout: 1 }, (err, res) -> -# test.fail(err) if err -# test.equal res, true, "stopJobs failed in callback result" +# assert.fail(err) if err +# assert.equal res, true, "stopJobs failed in callback result" # if Meteor.isServer -# test.notEqual testColl.stopped, false, "stopJobs didn't stop job collection" +# assert.notEqual testColl.stopped, false, "stopJobs didn't stop job collection" # onComplete() -Tinytest.addAsync 'Run shutdownJobServer on the job collection', (test, onComplete) -> +it 'Run shutdownJobServer on the job collection', (onComplete) -> testColl.shutdownJobServer { timeout: 1 }, (err, res) -> - test.fail(err) if err - test.equal res, true, "shutdownJobServer failed in callback result" + assert.fail(err) if err + assert.equal res, true, "shutdownJobServer failed in callback result" if Meteor.isServer - test.notEqual testColl.stopped, false, "shutdownJobServer didn't stop job collection" + assert.notEqual testColl.stopped, false, "shutdownJobServer didn't stop job collection" onComplete() if Meteor.isClient - Tinytest.addAsync 'Run startJobServer on remote job collection', (test, onComplete) -> + it 'Run startJobServer on remote job collection', (onComplete) -> remoteServerTestColl.startJobServer (err, res) -> - test.fail(err) if err - test.equal res, true, "startJobServer failed in callback result" + assert.fail(err) if err + assert.equal res, true, "startJobServer failed in callback result" onComplete() - Tinytest.addAsync 'Create a job and see that it is added to a remote server collection and runs', (test, onComplete) -> + it 'Create a job and see that it is added to a remote server collection and runs', (onComplete) -> jobType = "TestJob_#{Math.round(Math.random()*1000000000)}" job = new Job remoteServerTestColl, jobType, { some: 'data' } - test.ok validJobDoc(job.doc) + assert.ok validJobDoc(job.doc) job.save (err, res) -> - test.fail(err) if err - test.ok validId(res), "job.save() failed in callback result" + assert.fail(err) if err + assert.ok validId(res), "job.save() failed in callback result" q = remoteServerTestColl.processJobs jobType, { pollInterval: 250 }, (job, cb) -> - test.equal job._doc._id, res + assert.equal job._doc._id, res job.done() cb() q.shutdown { level: 'soft', quiet: true }, () -> onComplete() - Tinytest.addAsync 'Run shutdownJobServer on remote job collection', (test, onComplete) -> + it 'Run shutdownJobServer on remote job collection', (onComplete) -> remoteServerTestColl.shutdownJobServer { timeout: 1 }, (err, res) -> - test.fail(err) if err - test.equal res, true, "shutdownJobServer failed in callback result" + assert.fail(err) if err + assert.equal res, true, "shutdownJobServer failed in callback result" onComplete() From ca4b34a9046ded087c1d719d923a6ca79eb537e5 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Mon, 30 Apr 2018 12:29:26 +0400 Subject: [PATCH 2/2] Fixed import for npm module 'chai' --- test/job_collection_tests.coffee | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/job_collection_tests.coffee b/test/job_collection_tests.coffee index 996eb9b..6bcb260 100644 --- a/test/job_collection_tests.coffee +++ b/test/job_collection_tests.coffee @@ -1,4 +1,5 @@ -assert = require('chai').assert; +chai = Npm.require 'chai' +assert = chai.assert ############################################################################ # Copyright (C) 2014-2017 by Vaughn Iverson