Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ pagination_next: null
pagination_prev: null
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Using Knapsack Pro with `parallel_tests`

:::caution
Expand Down Expand Up @@ -54,26 +57,69 @@ bundle exec parallel_test -n $PARALLEL_TESTS_CONCURRENCY -e './bin/parallel_test

Create `bin/parallel_tests` and make it executable `chmod u+x`:

```bash
#!/bin/bash

# The Knapsack Pro API sees 3 * 2 = 6 parallel nodes
export KNAPSACK_PRO_CI_NODE_TOTAL=$(( $PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_TOTAL ))

if [ "$TEST_ENV_NUMBER" == "" ]; then
export PARALLEL_TESTS_CONCURRENCY_INDEX=0
else
export PARALLEL_TESTS_CONCURRENCY_INDEX=$(( $TEST_ENV_NUMBER - 1 ))
fi

# The current index for the Knapsack Pro API is {0,1,2} + (3 * {0,1}) in other words either {0,1,2,3,4,5}
KNAPSACK_PRO_CI_NODE_INDEX=$(( $PARALLEL_TESTS_CONCURRENCY_INDEX + ($PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_INDEX) ))

# Debug log
echo KNAPSACK_PRO_CI_NODE_TOTAL=$KNAPSACK_PRO_CI_NODE_TOTAL KNAPSACK_PRO_CI_NODE_INDEX=$KNAPSACK_PRO_CI_NODE_INDEX PARALLEL_TESTS_CONCURRENCY=$PARALLEL_TESTS_CONCURRENCY

bundle exec rake knapsack_pro:queue:rspec
```
<Tabs>
<TabItem value="parallel_tests-on-parallel-nodes-default" label="Default" default>
```bash
#!/bin/bash

# The Knapsack Pro API sees 3 * 2 = 6 parallel nodes
export KNAPSACK_PRO_CI_NODE_TOTAL=$(( $PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_TOTAL ))

if [ "$TEST_ENV_NUMBER" == "" ]; then
export PARALLEL_TESTS_CONCURRENCY_INDEX=0
else
export PARALLEL_TESTS_CONCURRENCY_INDEX=$(( $TEST_ENV_NUMBER - 1 ))
fi

# The current index for the Knapsack Pro API is {0,1,2} + (3 * {0,1}) in other words either {0,1,2,3,4,5}
KNAPSACK_PRO_CI_NODE_INDEX=$(( $PARALLEL_TESTS_CONCURRENCY_INDEX + ($PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_INDEX) ))

# Debug log
echo KNAPSACK_PRO_CI_NODE_TOTAL=$KNAPSACK_PRO_CI_NODE_TOTAL KNAPSACK_PRO_CI_NODE_INDEX=$KNAPSACK_PRO_CI_NODE_INDEX PARALLEL_TESTS_CONCURRENCY=$PARALLEL_TESTS_CONCURRENCY

bundle exec rake knapsack_pro:queue:rspec
```
</TabItem>
<TabItem value="parallel_tests-on-parallel-nodes-spot-instances" label="with Spot Instances">
If you use AWS Spot Instances as CI nodes, ensure that the termination signal is properly propagated down the script hierarchy.

```bash
#!/bin/bash

handle_signal() {
local signal=$1
if [[ -n $knapsack_pro_pid ]]; then
echo "KNAPSACK_PRO_CI_NODE_INDEX=$KNAPSACK_PRO_CI_NODE_INDEX Received signal $signal in bin/parallel_tests, terminating the Knapsack Pro process with TERM"
kill -TERM $knapsack_pro_pid
echo "KNAPSACK_PRO_CI_NODE_INDEX=$KNAPSACK_PRO_CI_NODE_INDEX Waiting for the Knapsack Pro process with PID $knapsack_pro_pid to terminate"
wait $knapsack_pro_pid
fi
}

trap 'handle_signal TERM' TERM
trap 'handle_signal INT' INT

# The Knapsack Pro API sees 3 * 2 = 6 parallel nodes
export KNAPSACK_PRO_CI_NODE_TOTAL=$(( $PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_TOTAL ))

if [ "$TEST_ENV_NUMBER" == "" ]; then
export PARALLEL_TESTS_CONCURRENCY_INDEX=0
else
export PARALLEL_TESTS_CONCURRENCY_INDEX=$(( $TEST_ENV_NUMBER - 1 ))
fi

# The current index for the Knapsack Pro API is {0,1,2} + (3 * {0,1}) in other words either {0,1,2,3,4,5}
KNAPSACK_PRO_CI_NODE_INDEX=$(( $PARALLEL_TESTS_CONCURRENCY_INDEX + ($PARALLEL_TESTS_CONCURRENCY * $KNAPSACK_PRO_CI_NODE_INDEX) ))

# Debug log
echo KNAPSACK_PRO_CI_NODE_TOTAL=$KNAPSACK_PRO_CI_NODE_TOTAL KNAPSACK_PRO_CI_NODE_INDEX=$KNAPSACK_PRO_CI_NODE_INDEX PARALLEL_TESTS_CONCURRENCY=$PARALLEL_TESTS_CONCURRENCY

bundle exec rake knapsack_pro:queue:rspec &
knapsack_pro_pid=$!
wait $knapsack_pro_pid
```
</TabItem>
</Tabs>

Running the above on CI should result in:

Expand Down
8 changes: 4 additions & 4 deletions docusaurus/docs/ruby/rspec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ You need to install the [`rspec_junit_formatter`](https://github.com/sj26/rspec_
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format RspecJunitFormatter --out tmp/rspec_$MY_CI_NODE_INDEX.xml]"
```

This also applies if you are running parallel test processes on each CI node (see how to [integrate Knapsack Pro with `parallel_tests` here](parallel_tests.md)).
This also applies if you are running parallel test processes on each CI node (see how to [integrate Knapsack Pro with `parallel_tests` here](parallel_tests.mdx)).

<details>
<summary>For legacy versions of `knapsack_pro` older than 7.0, please click here.</summary>
Expand Down Expand Up @@ -111,7 +111,7 @@ You need to install the [`rspec_junit_formatter`](https://github.com/sj26/rspec_
FINAL_REPORT = "tmp/final_rspec_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.xml"
```

This applies also if you are running parallel test processes on each CI node (see our page on to integrate Knapsack Pro with [`parallel_tests`](parallel_tests.md) for an example).
This applies also if you are running parallel test processes on each CI node (see our page on to integrate Knapsack Pro with [`parallel_tests`](parallel_tests.mdx) for an example).
</details>
</TabItem>
<TabItem value="json-formatter" label="Regular Mode">
Expand All @@ -136,7 +136,7 @@ You need to install the [`rspec_junit_formatter`](https://github.com/sj26/rspec_
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format json --out tmp/rspec_$MY_CI_NODE_INDEX.json]"
```

This also applies if you are running parallel test processes on each CI node (see how to [integrate Knapsack Pro with `parallel_tests` here](parallel_tests.md)).
This also applies if you are running parallel test processes on each CI node (see how to [integrate Knapsack Pro with `parallel_tests` here](parallel_tests.mdx)).

<details>
<summary>For legacy versions of `knapsack_pro` older than 7.0, please click here.</summary>
Expand Down Expand Up @@ -168,7 +168,7 @@ You need to install the [`rspec_junit_formatter`](https://github.com/sj26/rspec_
FINAL_REPORT = "tmp/final_rspec_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.json"
```

This applies also if you are running parallel test processes on each CI node (see our page on to integrate Knapsack Pro with [`parallel_tests`](parallel_tests.md) for an example).
This applies also if you are running parallel test processes on each CI node (see our page on to integrate Knapsack Pro with [`parallel_tests`](parallel_tests.mdx) for an example).
</details>
</TabItem>
<TabItem value="json-formatter" label="Regular Mode">
Expand Down