From 2341390b1661db3b28780a99b53dc6d1ac1cbc19 Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Fri, 4 Oct 2024 13:05:02 +0200 Subject: [PATCH] Fix race conditions in unit tests and enable race check in tests (#521) --- neo4j/internal/bolt/bolt3_test.go | 8 +++++--- neo4j/internal/bolt/bolt4_test.go | 8 +++++--- neo4j/internal/bolt/bolt5_test.go | 8 +++++--- testkit/integration.py | 2 +- testkit/unittests.py | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/neo4j/internal/bolt/bolt3_test.go b/neo4j/internal/bolt/bolt3_test.go index 65a219e2..3bfb9d29 100644 --- a/neo4j/internal/bolt/bolt3_test.go +++ b/neo4j/internal/bolt/bolt3_test.go @@ -240,11 +240,12 @@ func TestBolt3(outer *testing.T) { } inner.Parallel() for _, test := range testCases { + testCopy := test inner.Run(fmt.Sprintf("%s for %s", test.description, test.Method), func(t *testing.T) { bolt, cleanup := connectToServer(t, func(srv *bolt3server) { srv.accept(3) - if !test.ExpectError { - if test.Method == "run" { + if !testCopy.ExpectError { + if testCopy.Method == "run" { srv.waitForRun() } else { srv.waitForTxBegin() @@ -872,10 +873,11 @@ func TestBolt3(outer *testing.T) { } for _, callback := range callbacks { + callbackCopy := callback inner.Run(callback.scenario, func(t *testing.T) { bolt, cleanup := connectToServer(inner, func(srv *bolt3server) { srv.accept(3) - callback.server(t, srv) + callbackCopy.server(t, srv) }) defer cleanup() defer bolt.Close(ctx) diff --git a/neo4j/internal/bolt/bolt4_test.go b/neo4j/internal/bolt/bolt4_test.go index 7474acb8..cc716a49 100644 --- a/neo4j/internal/bolt/bolt4_test.go +++ b/neo4j/internal/bolt/bolt4_test.go @@ -459,11 +459,12 @@ func TestBolt4(outer *testing.T) { } inner.Parallel() for _, test := range testCases { + testCopy := test inner.Run(fmt.Sprintf("%s for %s", test.description, test.Method), func(t *testing.T) { bolt, cleanup := connectToServer(t, func(srv *bolt4server) { srv.acceptWithMinor(4, 4) - if !test.ExpectError { - if test.Method == "run" { + if !testCopy.ExpectError { + if testCopy.Method == "run" { srv.waitForRun(nil) } else { srv.waitForTxBegin() @@ -1418,10 +1419,11 @@ func TestBolt4(outer *testing.T) { } for _, callback := range callbacks { + callbackCopy := callback inner.Run(callback.scenario, func(t *testing.T) { bolt, cleanup := connectToServer(inner, func(srv *bolt4server) { srv.accept(4) - callback.server(t, srv) + callbackCopy.server(t, srv) }) defer cleanup() defer bolt.Close(ctx) diff --git a/neo4j/internal/bolt/bolt5_test.go b/neo4j/internal/bolt/bolt5_test.go index efa0c808..04578ec6 100644 --- a/neo4j/internal/bolt/bolt5_test.go +++ b/neo4j/internal/bolt/bolt5_test.go @@ -707,11 +707,12 @@ func TestBolt5(outer *testing.T) { } inner.Parallel() for _, test := range testCases { + testCopy := test inner.Run(fmt.Sprintf("%s for %s", test.description, test.Method), func(t *testing.T) { bolt, cleanup := connectToServer(t, func(srv *bolt5server) { srv.acceptWithMinor(5, 1) - if !test.ExpectError { - if test.Method == "run" { + if !testCopy.ExpectError { + if testCopy.Method == "run" { srv.waitForRun(nil) } else { srv.waitForTxBegin(nil) @@ -1633,10 +1634,11 @@ func TestBolt5(outer *testing.T) { } for _, callback := range callbacks { + callbackCopy := callback inner.Run(callback.scenario, func(t *testing.T) { bolt, cleanup := connectToServer(inner, func(srv *bolt5server) { srv.accept(5) - callback.server(t, srv) + callbackCopy.server(t, srv) }) defer cleanup() defer bolt.Close(ctx) diff --git a/testkit/integration.py b/testkit/integration.py index 63b5aa89..d95f9678 100644 --- a/testkit/integration.py +++ b/testkit/integration.py @@ -12,7 +12,7 @@ def run(args): if __name__ == "__main__": package = os.path.join(".", "neo4j", "test-integration", "...") - cmd = ["go", "test", "-buildvcs=false"] + cmd = ["go", "test", "-race", "-buildvcs=false"] if os.environ.get("TEST_IN_TEAMCITY", False): cmd = cmd + ["-v", "-json"] run(cmd + [package]) diff --git a/testkit/unittests.py b/testkit/unittests.py index 088ffb5d..f4ad3a9f 100644 --- a/testkit/unittests.py +++ b/testkit/unittests.py @@ -24,7 +24,7 @@ def run(args): for extra_args in ( (), ("-tags", "internal_time_mock") ): - cmd = ["go", "test", *extra_args] + cmd = ["go", "test", "-race", *extra_args] if os.environ.get("TEST_IN_TEAMCITY", False): cmd = cmd + ["-v", "-json"]