mocha-distributed 0.8.1 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,160 +1,267 @@
1
1
  # mocha-distributed
2
2
 
3
+ Run mocha tests faster.
4
+
5
+ Speed up your mocha tests by running them in parallel in multiple machines all
6
+ at once without changing a single line of code. You only need a redis server.
7
+
8
+ ## Purpose
9
+
3
10
  The aim of this project is to provide a simple way of running distributed mocha
4
- tests without having to change too many lines of code, nor having to decide
5
- what to run where.
11
+ tests without having to change any line of code, nor having to decide
12
+ what to run where. Tests spread automatically according to the nodes you have.
13
+
14
+ The concept is very simple, basically you spawn as many runners as you wish
15
+ on as many nodes as you wish, and each node decides whether they should run
16
+ a test or the test has already been executed or is being executed somewhere
17
+ else.
6
18
 
7
19
  It does not matter if you run the tests in one machine as subprocesses or in
8
20
  many machines with multiple processes each.
9
21
 
10
- Hopefully you will only need to include a single line of code on each of your
11
- mocha files in order for them to run in parallel.
22
+ Because you don't need to change a single line of code, which means that you
23
+ can still run mocha locally as usual.
12
24
 
13
- Your test files will still be 100% compatible with mocha, so you can run them
14
- without any side-effects locally, using mocha, as if nothing was changed.
25
+ ## Quick start
15
26
 
16
- A idea for the future is to create a mocha-compatible runner so that you don't
17
- even have to change source files in any way. But let's go one step at a time.
27
+ You don't need to change a single line of code on your tests, this project uses
28
+ mocha hooks in order to work, so the only thing you'll need to do in preparation
29
+ is:
18
30
 
19
- ## How it works
31
+ ```bash
32
+ $ npm install -s mocha-distributed
33
+ ```
20
34
 
21
- ### Brief example
35
+ Make sure you have a redis running somewhere with IP visibility from the machine
36
+ or machines where you want to run the tests on.
22
37
 
23
- First, add the following line in each of your test files you want to distribute.
24
- If you try to run the tests without this line, the tests will run on ALL machines,
25
- including the master node.
38
+ Finally, on each of the runners just run:
26
39
 
27
- ```javascript
28
- require('mocha-distributed');
40
+ ```bash
41
+ $ export MOCHA_DISTRIBUTED_EXECUTION_ID="execution__2021-01-01__20:10"
42
+ $ export MOCHA_DISTRIBUTED="redis://redis.address"
43
+ $ mocha --require mocha-distributed test/**/*.js
29
44
  ```
30
45
 
31
- Then, if you want to run mocha in one computer (e.g your dev computer):
46
+ There are several environment variables that allow you to control the behaviour
47
+ of distributed tests, but this is the simplest way to launch them.
32
48
 
33
- ```bash
34
- $ mocha test/**/*.js
35
- ```
49
+ MOCHA_DISTRIBUTED is the one holding the redis address, this is the only
50
+ requirement to make mocha-distributed work.
36
51
 
37
- To run mocha distributed:
52
+ MOCHA_DISTRIBUTED_EXECUTION_ID is the other variable you want to pay attention
53
+ to. Make sure you use a different value for each group of runners every time
54
+ you launch a test. This variable is what makes possible to make a runner know
55
+ whether a test has already been executed or not by other of their peers.
38
56
 
39
- - Execute as master in only one machine (imagine it is running on 1.2.3.4 IP address):
57
+ ## Environment Variables
40
58
 
41
- ```bash
42
- $ MOCHA_DISTRIBUTED="master" mocha test/**/*.js
43
- ```
59
+ - **MOCHA_DISTRIBUTED** (required)
44
60
 
45
- - Execute runners on one or a thousand machines/processes as:
61
+ Right now this variable is the one used to specify the node that will hold
62
+ information about tests being run. This project only supports redis right
63
+ now. This variable can take the form:
64
+
65
+ redis[s]://[[username][:password]@][host][:port]
46
66
 
47
- ```bash
48
- $ MOCHA_DISTRIBUTED="1.2.3.4" mocha test/**/*.js
49
- ```
67
+ Please make sure it has visibility to the desired redis server.
50
68
 
69
+ - **MOCHA_DISTRIBUTED_EXECUTION_ID** (required)
51
70
 
71
+ Make sure this value is different every time you launch your tests. You can
72
+ use any string here, but it should be different across test executions or
73
+ your tests will just be skipped after the second execution.
52
74
 
53
- ### Conceptual overview
75
+ Execution ID is used in order to differentiate different runs of the same
76
+ tests among parallel executions. If you launch 10 instances and you want
77
+ tests to be distributed among them, all need to have the same value for this
78
+ variable, otherwise each of them will run all the tests on its own.
54
79
 
55
- The concept is very simple, this module hooks all mocha calls and does some magic
56
- to allow running tests across machines without you having to decide what runs
57
- where, or splitting tests beforehand, etc...
80
+ Reusing this variable in different executions will cause your tests to be
81
+ skipped.
58
82
 
59
- To distribute tests you only need to create several processess across one
60
- or more machines (system won't care), and set one of them as the master,
61
- and the rest as the runners. Only one master allowed.
83
+ Use a random uuid or other random value, a kubernetes job_name, your
84
+ build system job id, ...
62
85
 
63
- Runners connect to the master and for each suite they ask whether they are
64
- the 'owners' to run the tests on that suite or not. If they are, they run it.
65
- If they are not, they just skip the tests and continue running the next suite.
86
+ - **MOCHA_DISTRIBUTED_GRANULARITY** = test
87
+
88
+ - test (default)
89
+ Potentially all tests can be executed by any runner in any order. This
90
+ is the default, but if you have trouble running your tests in parallel
91
+ please use "suite" instead
66
92
 
67
- For simplicity this is all granularity you'll get for now. If you need two
68
- suites to run one after another on the same machine, then create a suite
69
- that encloses those.
93
+ - suite (safest)
70
94
 
71
- All test results (with error information) are sent to the master, and the master
72
- will display the output of all tests.
95
+ Launch all tests from the same suite in the same runner. This prevents
96
+ some parallelization errors if your tests are not prepared for full
97
+ paralelization.
73
98
 
74
- If you like to save test results, etc... run the master with the right mocha
75
- parameters. Using those parameters on the runners won't hurt either.
99
+ - **MOCHA_DISTRIBUTED_RUNNER_ID** = random-id
76
100
 
77
- ### How to run in practice
101
+ By default this value is initialized automatically with a different random
102
+ string in each machine, BUT you can override this in case you need it for
103
+ whatever reason, although in theory you probably shouldn't.
78
104
 
79
- There is a magic environment variable called MOCHA_DISTRIBUTED.
105
+ - **MOCHA_DISTRIBUTED_EXPIRATION_TIME** = 86400
80
106
 
81
- When unset or empty, this module does nothing at all. Mocha runs normally,
82
- as if you would have not installed this module.
107
+ Configures to how long the data is kept in redis before it expires (in
108
+ seconds). The amount of data in redis is minimal, so you probably don't want
109
+ to play with it.
83
110
 
84
- When set to the special keyword 'master', the mocha will automatically create
85
- an HTTP server and listen to other processes or machines to connect to it and
86
- ask/inform about running the tests.
111
+ It might be helpful to increase it though, if you want to build some sort of
112
+ reporting on top of it, because you can directly explore test results in
113
+ redis. See Tests results in Redis for more info.
87
114
 
88
- For the runners, you would need to set MOCHA_DISTRIBUTED variable with the IP
89
- of the master computer that is running the test. If you are running all the
90
- tests in multiple processes you can set it to 127.0.0.1, otherwise it should
91
- be the IP of that machine in your private network, or the public IP if the
92
- machines are distributed around the world.
93
115
 
94
- You can also append the port to both the master and the runners, but in
95
- that case, the port must match.
116
+ - **MOCHA_DISTRIBUTED_VERBOSE** = false
117
+ - false (default)
118
+ Avoid printing verbose information
96
119
 
97
- ## Examples
120
+ - true
121
+ Prints some extra information about the variables, the server, ...
122
+ that might be useful for debugging issues and/or informational.
98
123
 
99
- ### Run tests in one machine and one process
124
+ ## Reading test results from Redis
100
125
 
101
- Just don't use the MOCHA_DISTRIBUTED variable, or set it to empty string.
126
+ All runners write the test result in JSON format in a specific redis list.
102
127
 
103
- ```bash
104
- $ mocha test/**/*.js
128
+ The list is basically the execution ID from the variable
129
+ MOCHA_DISTRIBUTED_EXECUTION_ID concatenated to ':test_result'
130
+
131
+ For example, if you are using: MOCHA_DISTRIBUTED_EXECUTION_ID="abcdefg"
132
+
133
+ Then the key you should look at in redis will be "abcdefg:test_result"
134
+
135
+ You can access this list and explore the result of all tests. Each item
136
+ on the list will contain information about the test suite, test id, ...
137
+ test name, if it timed out or not, duration of the test, result of the test,
138
+ if there were any errors, ... all that info is extracted from mocha itself.
139
+
140
+ You will see something like this on each of the items of the list:
141
+
142
+ ```json
143
+ {
144
+ "id": [
145
+ "suite-1-async",
146
+ "test-1.1-async"
147
+ ],
148
+ "type": "test",
149
+ "title": "test-1.1-async",
150
+ "timedOut": false,
151
+ "startTime": 1642705594300,
152
+ "endTime": 1642705594802,
153
+ "duration": 502,
154
+ "file": "/home/psanchez/github/mocha-distributed/example/suite-1.js",
155
+ "state": "passed",
156
+ "failed": false,
157
+ "speed": "slow",
158
+ "err": 0
159
+ }
105
160
  ```
106
161
 
107
- ### Run tests in one machine, multiple processes
162
+ The JSON formatting will differ since it is saved in a single line.
108
163
 
109
- To keep things simple, do something like this:
164
+ Keep in mind that:
165
+
166
+ * Duration and start/end times are in milliseconds.
167
+ * Some fields are duplicated in a way, like "state" and "failed" by design
168
+ because sometimes is handy to have this when reading results back.
169
+ * You can access test_result, passed_count and failed_count in redis
170
+ * Skipped tests are never saved in redis by design, unfortunately
171
+
172
+ You might have a look at list-tests-from-redis.js for an example on how to
173
+ query redis and list all tests.
174
+
175
+ ## Examples
176
+
177
+ ### Environment-agnostic
178
+
179
+ Make sure at least the following variables are set:
110
180
 
111
181
  ```bash
112
- $ MOCHA_DISTRIBUTED="master" mocha test/**/*.js
113
- $ MOCHA_DISTRIBUTED="localhost" mocha test/**/*.js > /dev/null &
114
- $ MOCHA_DISTRIBUTED="localhost" mocha test/**/*.js > /dev/null &
115
- ...
116
- $ MOCHA_DISTRIBUTED="localhost" mocha test/**/*.js > /dev/null &
182
+ MOCHA_DISTRIBUTED="redis://1.2.3.4"
183
+ MOCHA_DISTRIBUTED_EXECUTION_ID="a5ce4d8a-5b06-4ec8-aea2-37d7e4b2ffe1"
117
184
  ```
118
185
 
119
- Run as many processes as you'd like
120
-
121
- ### Run tests in several processes across several machines
186
+ Again, execution ID should be a different random number each time you want to
187
+ launch tests in parallel.
122
188
 
123
- On one machine do:
189
+ Example:
124
190
 
125
191
  ```bash
126
- $ MOCHA_DISTRIBUTED="master" mocha test/**/*.js
192
+ $ mocha --require mocha-distributed test/**/*.js
127
193
  ```
128
194
 
129
- You can also run some runners in that machine if you wish (see previous example).
195
+ Of course, this assumes you have already installed mocha-distributed.
196
+
197
+ ### Run tests in parallel in the same machine
130
198
 
131
- Figure out the IP address of the master. For this example let's say the master
132
- IP address is 1.2.3.4. Now on the rest of machines, just do:
199
+ To keep things simple, do something like this:
133
200
 
134
201
  ```bash
135
- $ MOCHA_DISTRIBUTED="1.2.3.4" mocha test/**/*.js > /dev/null &
136
- $ MOCHA_DISTRIBUTED="1.2.3.4" mocha test/**/*.js > /dev/null &
202
+ $ MOCHA_DISTRIBUTED_EXECUTION_ID=`uuidgen`
203
+ $ MOCHA_DISTRIBUTED="redis://redis-server"
204
+
205
+ $ mocha --require mocha-distributed test/**/*.js > output01.txt &
206
+ $ mocha --require mocha-distributed test/**/*.js > output02.txt &
137
207
  ...
138
- $ MOCHA_DISTRIBUTED="1.2.3.4" mocha test/**/*.js > /dev/null &
208
+ $ mocha --require mocha-distributed test/**/*.js > output0N.txt &
139
209
  ```
140
210
 
141
- Again, spawn as many processes as you'd like.
211
+ Run as many processes as you'd like.
212
+
213
+ ### Using kubernetes parallel jobs to launch tests
214
+
215
+ If you plan to use kubernetes to launch parallel jobs, make sure the backoff
216
+ limit is set to 1, so it does not retry the job after it fails, and make sure
217
+ you set execution ID to a different value each time (but common across all
218
+ parallel executions).
219
+
220
+ The easiest is to use the job ID (not the pod ID). You can do that by exposing
221
+ pod metadata information as environment variables.
222
+
223
+ See https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
224
+
225
+ ### Conceptual overview
226
+
227
+ The concept is very simple, this module hooks all mocha calls and does some magic
228
+ to allow running tests across machines without you having to decide what runs
229
+ where, or splitting tests beforehand, etc...
230
+
231
+ To distribute tests you only need to create several processess across one
232
+ or more machines (this method won't care how you spawn your runners), and either
233
+ set one of them as the master or use a redis database, and launch as many runners
234
+ as you wish.
235
+
236
+ Each runners connects to the redis instance and for each suite or test,
237
+ depending on the granularity, they ask whether they are the 'owners' to run the
238
+ tests on that suite or not. If they are, they run it. If they are not, they just
239
+ skip the tests and continue running the next suite/tests.
240
+
241
+ ### Caveats
242
+
243
+ When running with redis, all tests are executed by independent runners, which
244
+ means you need to take a look at the output of all the runners and see which
245
+ ones were skipped and which ones were executed for you to see if some of those
246
+ executed failed.
247
+
248
+ Also the exit code of the different mocha runners will differ. The
249
+ ones whose tests fail, will return an error, and the ones whose tests work well
250
+ or have been skipped will return 0.
142
251
 
143
252
  ## Build systems
144
253
 
145
254
  ### jenkins, bamboo, circle-ci, gitlab, travis...
146
255
 
147
- If you use jenkins, bamboo or any other build system, only one runner should
148
- be defined as the master. The master never runs tests, only waits.
256
+ If you use jenkins, bamboo or any other build system, make sure
257
+ one redis is installed somewhere and all runners can access to it.
149
258
 
150
- You should create more processes or launch more docker or kubernetes instances
151
- or spread test on several nodes... do it as you wish, but for each of the
152
- runners that you create, make sure they have visibility to the master (e.g
153
- make sure you can send a ping from all the runners to the master).
259
+ Create as many processes, nodes, dockers, kubernetes pods as you wish,
260
+ but for each of the runners that you create, make sure each of them can connect
261
+ to the redis instance (e.g are in the same network).
154
262
 
155
- In case you run multiple masters on the same machine, make sure you setup
156
- a different port each time, otherwise runner's from different projects will
157
- inform the wrong master.
263
+ You can use the project name and build ID or job id as the execution ID for
264
+ mocha-distributed. Use something unique among the builds of all your projects.
158
265
 
159
266
  ## MIT License
160
267
 
@@ -0,0 +1,18 @@
1
+ version: "3.7"
2
+
3
+ services:
4
+
5
+ redis:
6
+ image: redis:latest
7
+ ports:
8
+ - 6379:6379
9
+
10
+ redis-commander:
11
+ image: rediscommander/redis-commander:latest
12
+ environment:
13
+ - REDIS_HOSTS=local:redis:6379
14
+ ports:
15
+ - 8081:8081
16
+ depends_on:
17
+ - redis
18
+
@@ -24,7 +24,7 @@ describe ('suite-1-async', async function () {
24
24
 
25
25
  describe ('suite-1-sync', function () {
26
26
  it ('test-1.1-async', async function () {
27
- await util.sleep(0.5);
27
+ await util.sleep(1.5);
28
28
  })
29
29
 
30
30
  it ('test-1.2-sync', function () {
@@ -39,3 +39,27 @@ describe ('suite-1.2-sync', function () {
39
39
  });
40
40
  });
41
41
  });
42
+
43
+ describe ('suite-1.3-io', function () {
44
+ it ('console.log', function () {
45
+ console.log ("Writing from console.log\nAnother line")
46
+ });
47
+
48
+ it ('console.error', function () {
49
+ console.error ("Writing from console.error\nAnother line")
50
+ });
51
+
52
+ it ('process.stdout', function () {
53
+ process.stdout.write("Writing from process.stdout. No newline.")
54
+ });
55
+
56
+ it ('process.stderr', function () {
57
+ process.stderr.write("Writing from process.stderr. No newline.")
58
+ });
59
+
60
+ it ('process.stdout & process.stderr', function () {
61
+ process.stdout.write("stdout output\nanother line")
62
+ process.stderr.write("stderr output\nanother line\nand yet another one.")
63
+ });
64
+
65
+ });