Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit ca4588c

Browse files
author
Alex Walker
authored
When testing Grakn, randomize its port and poll to detect its startup (typedb#180)
## What is the goal of this PR? Previously when running tests, Grakn would always start on port 1729. This rendered it impossible for us to parallelise our tests. Now, we pick a random port number between 40000 and 60000. Also, previously we waited N milliseconds and then ran the tests. This was wasteful, because often Grakn would start way in advance; and unreliable, because sometimes Grakn would not start in time and CI would fail. Now, we add a polling loop that checks if the server port is in use, and signals that the tests may begin once the port is in use. ## What are the changes implemented in this PR? - Randomize port when running Grakn during BDD tests - During BDD tests, poll the ports to detect when Grakn has actually started - Enable test parallelisation in CI
1 parent 85803e7 commit ca4588c

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

.bazelrc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,3 @@
2020
build --incompatible_strict_action_env
2121
run --incompatible_strict_action_env
2222
test --incompatible_strict_action_env --test_env=PATH
23-
24-
# what is defined in this section will be applied when bazel is invoked like this: bazel ... --config=rbe ...
25-
build:rbe --project_id=grakn-dev
26-
build:rbe --remote_cache=cloud.buildbuddy.io
27-
build:rbe --bes_backend=cloud.buildbuddy.io
28-
build:rbe --bes_results_url=https://app.buildbuddy.io/invocation/
29-
build:rbe --tls_client_certificate=/opt/credentials/buildbuddy-cert.pem
30-
build:rbe --tls_client_key=/opt/credentials/buildbuddy-key.pem
31-
build:rbe --remote_timeout=3600
32-
build:rbe --remote_download_minimal

.grabl/automation.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ build:
7373
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
7474
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
7575
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
76-
bazel test //tests/behaviour/concept/... --test_output=errors --jobs=1
76+
bazel test //tests/behaviour/concept/... --test_output=errors
7777
test-behaviour-match:
7878
image: graknlabs-ubuntu-20.04
7979
type: foreground
@@ -86,8 +86,8 @@ build:
8686
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
8787
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
8888
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
89-
bazel test //tests/behaviour/graql/language/match/... --test_output=errors --jobs=1
90-
bazel test //tests/behaviour/graql/language/get/... --test_output=errors --jobs=1
89+
bazel test //tests/behaviour/graql/language/match/... --test_output=errors
90+
bazel test //tests/behaviour/graql/language/get/... --test_output=errors
9191
test-behaviour-writable:
9292
image: graknlabs-ubuntu-20.04
9393
type: foreground
@@ -100,10 +100,10 @@ build:
100100
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
101101
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
102102
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
103-
bazel test //tests/behaviour/graql/language/insert/... --test_output=errors --jobs=1
104-
bazel test //tests/behaviour/graql/language/delete/... --test_output=errors --jobs=1
105-
bazel test //tests/behaviour/graql/language/update:checkstyle --test_output=errors --jobs=1
106-
bazel test //tests/behaviour/graql/language/update:test-core --test_output=errors --jobs=1
103+
bazel test //tests/behaviour/graql/language/insert/... --test_output=errors
104+
bazel test //tests/behaviour/graql/language/delete/... --test_output=errors
105+
bazel test //tests/behaviour/graql/language/update:checkstyle --test_output=errors
106+
bazel test //tests/behaviour/graql/language/update:test-core --test_output=errors
107107
test-behaviour-definable:
108108
image: graknlabs-ubuntu-20.04
109109
type: foreground
@@ -116,10 +116,10 @@ build:
116116
export ARTIFACT_USERNAME=$REPO_GRAKN_USERNAME
117117
export ARTIFACT_PASSWORD=$REPO_GRAKN_PASSWORD
118118
bazel run @graknlabs_dependencies//distribution/artifact:create-netrc
119-
bazel test //tests/behaviour/graql/language/define:checkstyle --test_output=errors --jobs=1
120-
bazel test //tests/behaviour/graql/language/define:test-core --test_output=errors --jobs=1
121-
bazel test //tests/behaviour/graql/language/undefine:checkstyle --test_output=errors --jobs=1
122-
bazel test //tests/behaviour/graql/language/undefine:test-core --test_output=errors --jobs=1
119+
bazel test //tests/behaviour/graql/language/define:checkstyle --test_output=errors
120+
bazel test //tests/behaviour/graql/language/define:test-core --test_output=errors
121+
bazel test //tests/behaviour/graql/language/undefine:checkstyle --test_output=errors
122+
bazel test //tests/behaviour/graql/language/undefine:test-core --test_output=errors
123123
test-cluster-failover:
124124
image: graknlabs-ubuntu-20.04
125125
type: foreground

BUILD

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ load("@graknlabs_dependencies//distribution:deployment.bzl", "deployment")
3232
load(":deployment.bzl", github_deployment = "deployment")
3333

3434

35-
3635
py_library(
3736
name = "client_python",
3837
srcs = glob(["grakn/**/*.py"]),
@@ -114,10 +113,8 @@ release_validate_python_deps(
114113
filegroup(
115114
name = "ci",
116115
data = [
117-
"@graknlabs_dependencies//tool/bazelrun:rbe",
116+
"@graknlabs_dependencies//tool/checkstyle:test-coverage",
118117
"@graknlabs_dependencies//distribution/artifact:create-netrc",
119-
"@graknlabs_dependencies//tool/sync:dependencies",
120-
"@graknlabs_dependencies//tool/release:approval",
121118
"@graknlabs_dependencies//tool/release:create-notes",
122119
],
123120
)

dependencies/graknlabs/artifacts.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ def graknlabs_grakn_cluster_artifacts():
3737
artifact_name = "grakn-cluster-server-{platform}-{version}.{ext}",
3838
tag_source = deployment_private["artifact.release"],
3939
commit_source = deployment_private["artifact.snapshot"],
40-
commit = "892073639b024904a413154011fb42d9dae090f3",
40+
commit = "93cbc149d7b9ce588e52d8132c8a0b2265658a3c",
4141
)

tests/behaviour/background/cluster/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
def before_all(context: Context):
2929
environment_base.before_all(context)
30-
context.client = GraknClient.cluster([GraknClient.DEFAULT_ADDRESS])
30+
context.client = GraknClient.cluster(addresses=["localhost:" + context.config.userdata["port"]])
3131

3232

3333
def before_scenario(context: Context, scenario):

tests/behaviour/background/core/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
def before_all(context: Context):
2929
environment_base.before_all(context)
30-
context.client = GraknClient.core()
30+
context.client = GraknClient.core(address="localhost:%d" % int(context.config.userdata["port"]))
3131

3232

3333
def before_scenario(context: Context, scenario):

tests/behaviour/background/environment_base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
def before_all(context: Context):
3030
context.THREAD_POOL_SIZE = 32
31-
context.client = GraknClient.core()
3231

3332

3433
def before_scenario(context: Context, scenario):

tests/behaviour/context.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
ConceptSubtype: Concept = Union[ThingSubtype, TypeSubtype]
5151

5252

53+
class Config:
54+
"""
55+
Type definitions for Config.
56+
57+
This class should not be instantiated. The initialisation of the actual Config object occurs in environment.py.
58+
"""
59+
def __init__(self):
60+
self.userdata = {}
61+
62+
5363
class Context(behave.runner.Context):
5464
"""
5565
Type definitions for Context.
@@ -69,6 +79,7 @@ def __init__(self):
6979
self.numeric_answer: Optional[Numeric] = None
7080
self.answer_groups: Optional[List[ConceptMapGroup]] = None
7181
self.numeric_answer_groups: Optional[List[NumericGroup]] = None
82+
self.config = Config()
7283

7384
def tx(self) -> Transaction:
7485
return self.sessions_to_transactions[self.sessions[0]][0]

tools/behave_rule.bzl

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,57 @@ def _rule_implementation(ctx):
6060
fi
6161
fi
6262
DIRECTORY=$(ls ./grakn_distribution)
63-
echo Successfully unarchived Grakn distribution.
64-
echo Starting Grakn Server
63+
64+
if [[ $GRAKN_DISTRO == *"cluster"* ]]; then
65+
PRODUCT=Cluster
66+
else
67+
PRODUCT=Core
68+
fi
69+
70+
echo Successfully unarchived Grakn $PRODUCT distribution.
71+
72+
RND=20001
73+
while [ $RND -gt 20000 ] # Guarantee fair distribution of random ports
74+
do
75+
RND=$RANDOM
76+
done
77+
PORT=$((40000 + $RND))
78+
79+
echo Starting Grakn $PRODUCT Server.
6580
mkdir ./grakn_distribution/"$DIRECTORY"/grakn_test
66-
./grakn_distribution/"$DIRECTORY"/grakn server --data grakn_test &
67-
sleep 9
81+
if [[ $PRODUCT == "Core" ]]; then
82+
./grakn_distribution/"$DIRECTORY"/grakn server --port $PORT --data grakn_test &
83+
else
84+
./grakn_distribution/"$DIRECTORY"/grakn server --address "127.0.0.1:$PORT:$(($PORT+1))" --data grakn_test &
85+
fi
86+
87+
POLL_INTERVAL_SECS=0.5
88+
MAX_RETRIES=60
89+
RETRY_NUM=0
90+
while [[ $RETRY_NUM -lt $MAX_RETRIES ]]; do
91+
RETRY_NUM=$(($RETRY_NUM + 1))
92+
if [[ $(($RETRY_NUM % 4)) -eq 0 ]]; then
93+
echo Waiting for Grakn $PRODUCT server to start \($(($RETRY_NUM / 2))s\)...
94+
fi
95+
lsof -i :$PORT && STARTED=1 || STARTED=0
96+
if [[ $STARTED -eq 1 ]]; then
97+
break
98+
fi
99+
sleep $POLL_INTERVAL_SECS
100+
done
101+
if [[ $STARTED -eq 0 ]]; then
102+
echo Failed to start Grakn $PRODUCT server
103+
exit 1
104+
fi
105+
echo Grakn $PRODUCT database server started
68106
69107
"""
70108
# TODO: If two step files have the same name, we should rename the second one to prevent conflict
71109
cmd += "cp %s %s" % (ctx.files.background[0].path, feats_dir)
72110
cmd += " && rm -rf " + steps_out_dir
73111
cmd += " && mkdir " + steps_out_dir + " && "
74112
cmd += " && ".join(["cp %s %s" % (step_file.path, steps_out_dir) for step_file in ctx.files.steps])
75-
cmd += " && behave %s && export RESULT=0 || export RESULT=1" % feats_dir
113+
cmd += " && behave %s -D port=$PORT && export RESULT=0 || export RESULT=1" % feats_dir
76114
cmd += """
77115
echo Tests concluded with exit value $RESULT
78116
echo Stopping server.

0 commit comments

Comments
 (0)