From 28d85665d853cf7bc6b274a8a8727caece3203cf Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:36:03 -0700 Subject: [PATCH] chore: Fixed leaked context settings from async tests --- .github/workflows/master-2.yml | 4 ++-- .../switcherapi/client/ContextBuilder.java | 12 ++++++++++- .../SwitcherSnapshotAutoUpdateTest.java | 21 +++++++++++++------ .../utils/SnapshotWatcherErrorTest.java | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.github/workflows/master-2.yml b/.github/workflows/master-2.yml index 1f212e12..d173e78f 100644 --- a/.github/workflows/master-2.yml +++ b/.github/workflows/master-2.yml @@ -9,7 +9,7 @@ on: jobs: build-scan: name: SonarCloud Scan - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')" steps: @@ -35,7 +35,7 @@ jobs: fail-fast: false matrix: java: [ '11', '17', '18', '21'] - os: [ ubuntu-20.04, windows-latest ] + os: [ ubuntu-latest, windows-latest ] runs-on: ${{ matrix.os }} if: "! contains(toJSON(github.event.commits.*.message), '[skip ci]')" diff --git a/src/main/java/com/github/switcherapi/client/ContextBuilder.java b/src/main/java/com/github/switcherapi/client/ContextBuilder.java index 46a18373..abcdfdfd 100644 --- a/src/main/java/com/github/switcherapi/client/ContextBuilder.java +++ b/src/main/java/com/github/switcherapi/client/ContextBuilder.java @@ -16,12 +16,22 @@ public static void preConfigure(SwitcherProperties switcherProperties) { context = builder(); context.preBuild(switcherProperties); } - + + /** + * Initialize the ContextBuilder preserving the existing context + * @return ContextBuilder + */ public static ContextBuilder builder() { context = builder(false); return context; } + /** + * Initialize the ContextBuilder using existing context or create a new one + * + * @param init true/false to create a new context + * @return ContextBuilder + */ public static ContextBuilder builder(boolean init) { if (context == null || init) context = new ContextBuilder(); diff --git a/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java b/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java index aac904aa..4b6dce04 100644 --- a/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java +++ b/src/test/java/com/github/switcherapi/client/SwitcherSnapshotAutoUpdateTest.java @@ -142,7 +142,8 @@ void shouldUpdateSnapshot_local() { givenSnapshotUpdateResponse(false); //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) .snapshotLocation(SNAPSHOTS_LOCAL) .environment("generated_mock_default_2") @@ -165,8 +166,12 @@ void shouldUpdateSnapshot_remote() { givenSnapshotUpdateResponse(false); //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) + .apiKey("[API_KEY]") + .domain("Test") + .component("switcher-test") .snapshotLocation(SNAPSHOTS_LOCAL) .environment("generated_mock_default_3") .local(false) @@ -188,7 +193,8 @@ void shouldNotUpdateSnapshot_whenNoUpdateAvailable() { givenSnapshotUpdateResponse(true); //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) .snapshotLocation(SNAPSHOTS_LOCAL) .environment("generated_mock_default_4") @@ -214,7 +220,8 @@ void shouldUpdateSnapshot_remote_inMemory() { givenResponse(generateSnapshotResponse("default.json")); //graphql //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) .snapshotLocation(null) .environment("generated_mock_default_5") @@ -238,7 +245,8 @@ void shouldNotKillThread_whenAPI_wentLocal() { givenResponse(generateSnapshotResponse("default_outdated.json")); //graphql //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) .snapshotLocation(null) .environment("generated_mock_default_6") @@ -268,7 +276,8 @@ void shouldPreventSnapshotAutoUpdateToStart_whenAlreadySetup() { givenResponse(generateSnapshotResponse("default.json")); //graphql //that - Switchers.configure(ContextBuilder.builder() + Switchers.configure(ContextBuilder.builder(true) + .contextLocation(Switchers.class.getCanonicalName()) .url(String.format("http://localhost:%s", mockBackEnd.getPort())) .snapshotLocation(null) .environment("generated_mock_default_6") diff --git a/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java index a8c6e7ed..c2e3895d 100644 --- a/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java +++ b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java @@ -13,7 +13,7 @@ class SnapshotWatcherErrorTest { @Test void shouldNotWatchSnapshotWhenRemote() { //given - SwitchersBase.configure(ContextBuilder.builder() + SwitchersBase.configure(ContextBuilder.builder(true) .contextLocation(SwitchersBase.class.getCanonicalName()) .url("https://api.switcherapi.com") .apiKey("[API_KEY]")