Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to retrieve the wallaby worker id from the actual test code? #774

Closed
joachimboggild opened this issue Sep 5, 2016 · 9 comments
Labels

Comments

@joachimboggild
Copy link

joachimboggild commented Sep 5, 2016

Issue description or question

I am doing a large suite of tests that all interact with a database. I have setup and teardown code that creates the database and then drops it afterwards. This means that if I run tests in parallel, they attempt to access the same database.

Currently I solve this by setting workers: { initial: 1, regular: 1 } in my wallaby config file, but it would be great if I could run tests in parallel to speed up things.

My thought was to use the worker Id in the database name to guarantee that each separate worker gets its own database to work with. But is it possible to retrieve the worker Id from the test code?

Edit: I found another issue, #707 , which mentions the same thing. But I am not always running my tests with Wallaby, so I do not want to put my setup code in the wallaby configuration file, as that will not be hit in those circumstances. That is why I would like to export the worker id somehow, if possible.

Thanks :-)

Code editor or IDE name and version

WebStorm v2016.2

OS name and version

OSX

@ArtemGovorov
Copy link
Member

ArtemGovorov commented Sep 5, 2016

Depending on how you're planning to reuse it, you may make the worker id available to any other code by assigning a global variable:

 module.exports = function () {
   return {
     ...
     setup: function (wallaby) {
       global.workerId = wallaby.workerId;
       // or process.env.workerId = wallaby.workerId;
     }
   };
 };

and then accessing it anywhere from your code. Or create a file where it's used (to set up your DB for example) and just pass it there:

 module.exports = function () {
   return {
     files: [
       ...
       'setupFile.js'
     ]
     ...
     setup: function (wallaby) {
       require('./setupFile')(wallaby.workerId);
     }
   };
 };

and the setupFile.js:

module.exports = function (workerId) {
  // your initialization logic 
  // that can be reused by other runners (that can pass some worked id)
}

Let me know if it addresses your question.

@joachimboggild
Copy link
Author

That is fine - the global one should work the easiest. ... if it is not overwritten by the other workers? If I have e.g. six different workers running, will they have different global variables? (This might be off-topic, I know...)

@ArtemGovorov
Copy link
Member

Correct. Each worker has its own separate node process, the setup function is invoked in each of those worker processes, so each will have an individual worker id.

@joachimboggild
Copy link
Author

Thanks very much!

@joachimboggild
Copy link
Author

joachimboggild commented Sep 5, 2016

.... a tiny hiccup: I can't seem to get the setup function to work. My wallaby.js config file looks like this:

module.exports = function () {
  //noinspection JSUnusedGlobalSymbols
    return {
        env: {
          type: 'node',
          params: {
            runner: "--harmony"
          }
        },

        setup: function(wallaby) {
            console.log(`******************* workerId: ${wallaby.workerId} ***********************`);
            global.workerId = wallaby.workerId;
        },

        bootstrap: function() {
          var path = require('path');
          require('co-mocha')(require(path.join(path.dirname(process.argv[1]), 'runners/node/mocha@2.1.0/framework/')));
        },

        files: [
          "bl/**/*.js",
          "dal/**/*.js",
          "model/**/*.js",
          "plugins_v3/**/*.js",
          "plugins/**/*.js",
            "utils/**/*.js",
            "config/**/*.js",
            "test/test_config.js",
            "test/test_db.js",
            "test/test_graph.js",
            "test/mocha.config.test.js",
            "app.js",
        ],

        tests: [
            'test/dal/**/*.test.js',
            'test/plugins/**/*.test.js',
            'test/bl/**/*.test.js',
        ],

        testFramework: "mocha",

        debug: true,

        workers: {
          initial: 6,
          regular: 2
        }
    };
};

And I am looking for the comment specifying the worker id in the Wallaby Console, but I cannot see it anywhere. Am I looking in the wrong place?

@ArtemGovorov
Copy link
Member

You need to merge the setup and the bootstrap function into one, these are the alias of the same thing, so I suspect only the bootstrap gets called.

setup: function(wallaby) {
            console.log(`******************* workerId: ${wallaby.workerId} ***********************`);
            global.workerId = wallaby.workerId;
            var path = require('path');
            require('co-mocha')(require(path.join(path.dirname(process.argv[1]), 'runners/node/mocha@2.1.0/framework/')));
        },

@joachimboggild
Copy link
Author

Ah, thanks. I will try that.

@joachimboggild
Copy link
Author

How about using console.log from the setup function: Shouldn't its output appear in the wallaby console window (using WebStorm)?

@joachimboggild
Copy link
Author

Nvm - it started appearing after I merged the two functions. Thanks very much for your help!

@ArtemGovorov ArtemGovorov mentioned this issue Sep 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants