diff --git a/.ci.settings.xml b/.ci.settings.xml new file mode 100644 index 00000000..7aed84b6 --- /dev/null +++ b/.ci.settings.xml @@ -0,0 +1,47 @@ + + + + + + maven-dev + ${env.ARTIFACTORY_USER} + ${env.ARTIFACTORY_TOKEN} + + + + maven-all-virtual + ${env.ARTIFACTORY_USER} + ${env.ARTIFACTORY_TOKEN} + + + + harness-maven + ${env.HARNESS_MAVEN_USERNAME} + ${env.HARNESS_MAVEN_PASSWORD} + + + + org.sonarsource.scanner.maven + + + + sonar + + true + + + java-client + ${env.SONARQUBE_HOST_URL} + ${env.SONAR_TOKEN} + . + pom.xml,src/main/** + . + src/test/** + .csv + **/matchers/**/*.* + https://travis-ci.com/splitio/java-client + https://github.com/splitio/java-client + + + + diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 22873164..00000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: "Code scanning - action" - -on: - push: - pull_request: - schedule: - - cron: '0 21 * * 3' - -jobs: - CodeQL-Build: - - # CodeQL runs on ubuntu-latest and windows-latest - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v2 - with: - # We must fetch at least the immediate parents so that if this is - # a pull request then we can checkout the head. - fetch-depth: 2 - - # If this run was triggered by a pull request event, then checkout - # the head of the pull request instead of the merge commit. - - run: git checkout HEAD^2 - if: ${{ github.event_name == 'pull_request' }} - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v1 - # Override language selection by uncommenting this and choosing your languages - # with: - # languages: go, javascript, csharp, python, cpp, java - - # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v1 - - # ℹ️ Command-line programs to run using the OS shell. - # 📚 https://git.io/JvXDl - - # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines - # and modify them (or add more) to build your code if your project - # uses a compiled language - - #- run: | - # make bootstrap - # make release - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..e48ea8ee --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "jvm-commons"] + path = jvm-commons + url = git@github.com:splitio/jvm-commons.git diff --git a/.harness/linter/checkstyle-suppressions.xml b/.harness/linter/checkstyle-suppressions.xml new file mode 100644 index 00000000..81d23539 --- /dev/null +++ b/.harness/linter/checkstyle-suppressions.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/.harness/linter/google-java-style.xml b/.harness/linter/google-java-style.xml new file mode 100644 index 00000000..e885305e --- /dev/null +++ b/.harness/linter/google-java-style.xml @@ -0,0 +1,380 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci.yaml b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci.yaml new file mode 100644 index 00000000..8b931a0c --- /dev/null +++ b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci.yaml @@ -0,0 +1,94 @@ +pipeline: + name: java-client-ci + identifier: java_client_ci + projectIdentifier: Harness_Split + orgIdentifier: PROD + tags: {} + properties: + ci: + codebase: + connectorRef: fmegithubrunnersci + repoName: java-client + build: <+input> + depth: 0 + stages: + - stage: + name: test + identifier: test + type: CI + spec: + cloneCodebase: true + caching: + enabled: true + override: true + platform: + os: Linux + arch: Amd64 + runtime: + type: Cloud + spec: {} + execution: + steps: + - step: + type: Run + name: test jdk8 + identifier: test_jdk8 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-8 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -XX:+PrintCommandLineFlags -XX:ThreadStackSize=65536 -XX:-TieredCompilation -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: |- + apt-get update -qq && apt-get install -y --no-install-recommends redis-server + redis-server --daemonize yes + for i in $(seq 1 30); do + redis-cli ping 2>/dev/null | grep -q PONG && { echo "Redis is up"; break; } + echo "Waiting for Redis... ($i/30)"; sleep 1 + done + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + mvn --batch-mode clean install + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" + - step: + type: Run + name: checkstyle jdk8 + identifier: checkstyle_jdk8 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-8 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -XX:+PrintCommandLineFlags -XX:ThreadStackSize=65536 -XX:-TieredCompilation -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: mvn --batch-mode checkstyle:check + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" + - step: + type: Run + name: unsafe check jdk26 + identifier: unsafe_check_jdk26 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-26 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -XX:+PrintCommandLineFlags -XX:ThreadStackSize=65536 -XX:-TieredCompilation -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: |- + set -eo pipefail + apt-get update -qq && apt-get install -y --no-install-recommends redis-server + redis-server --daemonize yes + for i in $(seq 1 30); do + redis-cli ping 2>/dev/null | grep -q PONG && { echo "Redis is up"; break; } + echo "Waiting for Redis... ($i/30)"; sleep 1 + done + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + mvn --batch-mode -pl client -am test -Dtest=BloomFilterImpTest,ImpressionObserverTest -Dsurefire.failIfNoSpecifiedTests=false 2>&1 | tee /tmp/unsafe-check.log + if grep -q "sun.misc.Unsafe" /tmp/unsafe-check.log; then + echo "sun.misc.Unsafe usage detected (see warnings above)" + exit 1 + fi + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" diff --git a/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/pr.yaml b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/pr.yaml new file mode 100644 index 00000000..799b2126 --- /dev/null +++ b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/pr.yaml @@ -0,0 +1,14 @@ +inputSet: + name: pr + identifier: pr + orgIdentifier: PROD + projectIdentifier: Harness_Split + pipeline: + identifier: java_client_ci + properties: + ci: + codebase: + build: + type: PR + spec: + number: <+trigger.prNumber> diff --git a/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/push.yaml b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/push.yaml new file mode 100644 index 00000000..58e96c1e --- /dev/null +++ b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci/input_sets/push.yaml @@ -0,0 +1,14 @@ +inputSet: + name: push + identifier: push + orgIdentifier: PROD + projectIdentifier: Harness_Split + pipeline: + identifier: java_client_ci + properties: + ci: + codebase: + build: + type: branch + spec: + branch: <+trigger.branch> diff --git a/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci_hc.yaml b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci_hc.yaml new file mode 100644 index 00000000..d6d3417b --- /dev/null +++ b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_ci_hc.yaml @@ -0,0 +1,135 @@ +pipeline: + name: java-client-ci-hc + identifier: java_client_ci_hc + projectIdentifier: Harness_Split + orgIdentifier: PROD + tags: {} + properties: + ci: + codebase: + repoName: java-client + build: <+input> + depth: 0 + stages: + - stage: + name: test + identifier: test + type: CI + spec: + cloneCodebase: true + caching: + enabled: true + override: true + platform: + os: Linux + arch: Amd64 + runtime: + type: Cloud + spec: {} + execution: + steps: + - step: + type: Run + name: init submodules + identifier: init_submodules + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-8 + shell: Bash + command: |- + # .gitmodules points to GitHub for developers; CI pulls from the Harness Code mirror + git config submodule.jvm-commons.url https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Split/jvm-commons.git + git submodule update --init --recursive + - step: + type: Run + name: test jdk8 + identifier: test_jdk8 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-8 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -XX:+PrintCommandLineFlags -XX:ThreadStackSize=65536 -XX:-TieredCompilation -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: |- + apt-get update -qq && apt-get install -y --no-install-recommends redis-server python3 + redis-server --daemonize yes + for i in $(seq 1 30); do + redis-cli ping 2>/dev/null | grep -q PONG && { echo "Redis is up"; break; } + echo "Waiting for Redis... ($i/30)"; sleep 1 + done + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + mvn --batch-mode -f jvm-commons/pom.xml -pl events -am install -DskipTests + mvn --batch-mode clean install + mapfile -t reports < <(find . -path '*/target/site/jacoco/jacoco.xml' ! -path './coverage/*' ! -path './jvm-commons/*' ! -path './e2e/*') + python3 scripts/merge-jacoco.py "${reports[@]}" coverage/jacoco.xml + test -s coverage/jacoco.xml + # print where surefire wrote the reports that get published below + find . -name 'TEST-*.xml' -path '*surefire-reports*' | head + reports: + type: JUnit + spec: + paths: + - "**/surefire-reports/*.xml" + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" + - step: + name: harnessCoverage + identifier: harnessCoverage + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" + template: + templateRef: account.codeCoverage + versionLabel: v1 + templateInputs: + type: Run + spec: + envVariables: + patch_coverage_percent: "80" + file_path: coverage/jacoco.xml + overall_coverage_percent: "" + ignore_failure: "false" + exclude: "**/target/**,**/src/test/**,**/*Test.java,**/*Tests.java" + - step: + type: Run + name: e2e non-shaded jdk17 + identifier: e2e_non_shaded_jdk17 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-17 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: |- + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + # One explicit local repo for Maven and Gradle: in this container Maven's default repo (/harness/.m2) + # and the JVM's user.home (/root) disagree, so each tool would otherwise use a different one. + M2_REPO="${HOME}/.m2/repository" + mvn --batch-mode -f jvm-commons/pom.xml -pl events -am install -DskipTests -Dmaven.repo.local="${M2_REPO}" + cd e2e && ./gradlew test --console=plain -Dmaven.repo.local="${M2_REPO}" + reports: + type: JUnit + spec: + paths: + - e2e/build/test-results/*/*.xml + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" + - step: + type: Run + name: checkstyle jdk8 + identifier: checkstyle_jdk8 + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-8 + shell: Bash + envVariables: + MAVEN_OPTS: "-XX:InitialHeapSize=2G -XX:MaxHeapSize=2G -XX:+PrintCommandLineFlags -XX:ThreadStackSize=65536 -XX:-TieredCompilation -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" + command: |- + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + mvn --batch-mode -f jvm-commons/pom.xml -pl events -am install -DskipTests + mvn --batch-mode checkstyle:check + when: + stageStatus: Success + condition: <+codebase.build.type> == "PR" diff --git a/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_publish.yaml b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_publish.yaml new file mode 100644 index 00000000..ae6b4973 --- /dev/null +++ b/.harness/orgs/PROD/projects/Harness_Split/pipelines/java_client_publish.yaml @@ -0,0 +1,61 @@ +pipeline: + name: java-client-publish + identifier: java_client_publish + projectIdentifier: Harness_Split + orgIdentifier: PROD + tags: {} + description: >- + On-demand publish of java-client (all modules) to the Harness Maven registry (fme-mvn). Pick the branch to publish at run time. + Versions are immutable: publishing a version that already exists fails, so bump the -rcN suffix first. + properties: + ci: + codebase: + repoName: java-client + build: <+input> + depth: 0 + stages: + - stage: + name: publish + identifier: publish + description: Build and deploy every module with the harness-maven profile + type: CI + spec: + cloneCodebase: true + caching: + enabled: false + platform: + os: Linux + arch: Amd64 + runtime: + type: Cloud + spec: {} + execution: + steps: + # jvm-commons submodule: CI pulls it from the Harness Code mirror + - step: + type: Run + name: init submodules + identifier: init_submodules + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-17 + shell: Bash + command: |- + git config submodule.jvm-commons.url https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Split/jvm-commons.git + git submodule update --init --recursive + - step: + type: Run + name: deploy to harness maven + identifier: deploy_to_harness_maven + spec: + connectorRef: dockerhub + image: maven:3.9-eclipse-temurin-17 + shell: Bash + envVariables: + HARNESS_MAVEN_USERNAME: fme-mvn@service.harness.io + HARNESS_MAVEN_PASSWORD: <+secrets.getValue("fme-mvn-publish")> + command: |- + # The credentials are read from the environment by .ci.settings.xml. Nothing here echoes them. + mkdir -p ${HOME}/.m2 && cp .ci.settings.xml ${HOME}/.m2/settings.xml + mvn --batch-mode -f jvm-commons/pom.xml -pl events -am install -DskipTests + mvn --batch-mode -Pharness-maven clean deploy -DskipTests diff --git a/.travis.settings.xml b/.travis.settings.xml deleted file mode 100644 index b82c3871..00000000 --- a/.travis.settings.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - org.sonarsource.scanner.maven - - - - sonar - - true - - - https://sonarqube.split-internal.com - ${env.SONAR_TOKEN} - 300 - https://travis-ci.com/splitio/java-client - https://github.com/splitio/java-client - java-client - ./src - **/test/**/*.*,**/testing/**/*.* - **/ai/**/*.*,**/jdbc/**/*.*,**/mpt/**/*.*,**/jcr/**/*.*,**/JDBC* - - - - diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5bf396ec..00000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -dist: trusty - -language: java - -jdk: - - oraclejdk8 - -git: - depth: false - -cache: - directories: - - $HOME/.m2 - -before_script: - - cp .travis.settings.xml ${HOME}/.m2/settings.xml - -script: - - mvn clean install - -after_success: - - bash sonar-scanner.sh diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 00000000..858d9952 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,397 @@ +4.19.1 (Oct 29, 2026) +- Fixed SSE forced-stop handling. Stopping an SSE connection that is not open no longer sets the forced-stop flag, connection errors are recorded with the correct telemetry type, and a forced stop reports FORCED_STOP to the status callback. +- Updated Guava to 33.4.8-jre. Avoid usage of sun.misc.Unsafe to prevent errors in future JDK releases. + +4.19.0 (Oct 8, 2026) + - Added the ability to listen to different events triggered by the SDK. Read more in our docs. + - SDK_UPDATE notify when a flag or user segment has changed + - SDK_READY notify when the SDK is ready to evaluate + +4.18.4 (Oct 1, 2026) +- Updated `org.apache.httpcomponents.client5` dependency to 5.6.4 to address CVE-2026-71290. + +4.18.3 (Feb 27, 2025) + -Added support for non-shaded dependencies, allowing customers to manage transitive dependencies manually and avoid classpath conflicts. + +4.18.2 (Oct 15, 2025) +- Fixed an issue where Manager.splitNames() return incorrect formatted result using redis storage and no custom prefix. +- Added using String only parameter for treatments in FallbackTreatmentConfiguration class. + +4.18.1 (Sep 30, 2025) +- Fixed an issue where Streaming client hangs during token renew process. + +4.18.0 (Sep 12, 2025) +- Added new configuration for Fallback Treatments, which allows setting a treatment value and optional config to be returned in place of "control", either globally or by flag. Read more in our docs. + +4.17.0 (Aug 22, 2025) +- Added a maximum size payload when posting unique keys telemetry in batches +- Added ProxyConfiguration parameter to support proxies, including Harness Forward Proxy, allowing also for more secured authentication options: MTLS, Bearer token and user/password authentication. Read more in our docs. + +4.16.1 (Jul 21, 2025) +- Fixed vulnerabilities: + - Upgraded org.apache.commons-commons-lang3 to 3.18.0 + - Upgraded com.google.code.gson.gson to 2.13.1 + +4.16.0 (May 28, 2025) +- Added support for rule-based segments. These segments determine membership at runtime by evaluating their configured rules against the user attributes provided to the SDK. +- Added support for feature flag prerequisites. This allows customers to define dependency conditions between flags, which are evaluated before any allowlists or targeting rules. + +4.15.0 (Apr 18, 2025) +- Prevent polling threads from starting when the SDK calls destroy method. +- Added a new optional argument to the client `getTreatment` methods to allow passing additional evaluation options, such as a map of properties to append to the generated impressions sent to Split backend. Read more in our docs. + +4.14.0 (Jan 17, 2025) +- Added support for the new impressions tracking toggle available on feature flags, both respecting the setting and including the new field being returned on SplitView type objects. Read more in our docs. +- Cleaned unused imports to fix a collision issue. + +4.13.1 (Dec 5, 2024) +- Updated `org.apache.httpcomponents.client5` dependency to 5.4.1 to fix vulnerabilities. +- Updated `redis.clients` dependency to 4.4.8 to fix vulnerabilities. + +4.13.0 (Sep 13, 2024) +- Added support for Kerberos Proxy authentication. + +4.12.1 (Jun 10, 2024) +- Fixed deadlock for virtual thread in Push Manager and SSE Client. + +4.12.0 (May 15, 2024) +- Added support for targeting rules based on semantic versions (https://semver.org/). +- Added the logic to handle correctly when the SDK receives an unsupported Matcher type. +- Enhanced SDK Headers for Authorization Frameworks +- Cleaned unused imports and renaming some methods +- Fixed empty token handler thanks to @hpark-miovision + +4.11.1 (Feb 29, 2024) +- Fixed deadlock in UniqueKeysTracker when sending Unique Keys. + +4.11.0 (Jan 9, 2024) +- Added impressionsListener method in the IntegrationConfig builder to set Sync or Async Listener execution. +- Fixed localhost to read files with yml ending. + +4.10.2 (Dec 1, 2023) +- Added getTreatmentsByFlagSets without attributes. +- Fixed some issues for flag sets: Not logging a warning when using flag sets that don't contain cached feature flags. + +4.10.1 (Nov 8, 2023) +- Fixed handler for response http headers. + +4.10.0 (Nov 2, 2023) +- Added support for Flag Sets on the SDK, which enables grouping feature flags and interacting with the group rather than individually (more details in our documentation): + - Added new variations of the get treatment methods to support evaluating flags in given flag set/s. + - getTreatmentsByFlagSet and getTreatmentsByFlagSets + - getTreatmentWithConfigByFlagSets and getTreatmentsWithConfigByFlagSets + - Added a new optional Flag Sets Filter configuration option. This allows the SDK and Split services to only synchronize the flags in the specified flag sets, avoiding unused or unwanted flags from being synced on the SDK instance, bringing all the benefits from a reduced payload. + - Note: Only applicable when the SDK is in charge of the rollout data synchronization. When not applicable, the SDK will log a warning on init. + - Updated the following SDK manager methods to expose flag sets on flag views. +- Added `defaultTreatment` property to the `SplitView` object returned by the `split` and `splits` methods of the SDK manager. +- Added new `threadFactory` property in SDK config. It allows to use of Virtual Threading. + +4.9.0 (Sep 8, 2023) +- Added InputStream config for localhost mode providing a solution when the file is inside a jar. +- Fixed track impressions to send all impressions to the listener. +- Fixed SyncManager shutdown to stop SSE only when is streaming mode on. + +4.8.1 (Aug 1, 2023) +- Applied linting rules to the code. +- Fixed an issue when the prefix is empty for Redis settings. + +4.8.0 (Jul 18, 2023) +- Improved streaming architecture implementation to apply feature flag updates from the notification received which is now enhanced, improving efficiency and reliability of the whole update system. +- Updated `com.google.guava` dependence to 32.0.1 for fixing a vulnerability. +- Updated SegmentFetcher for better readability. + +4.7.2 (May 16, 2023) +- Updated default treatment to be control for yaml and json localhost. +- Updated terminology on the SDKs codebase to be more aligned with current standard without causing a breaking change. The core change is the term split for feature flag on things like logs and javadoc comments. + +4.7.1 (Apr 10, 2023) +- Added SHA for feature flag and segment fetcher in localhost json. +- Updated `org.yaml.snakeyaml` dependence to 2.0 for fixing a vulnerability. +- Fixed Redis integration, changing []dtos.Key to dtos.Key +- Fixed destroy for consumer mode. + +4.7.0 (Jan 30, 2023) +- Added support to use JSON files in localhost mode. +- Improved logs to have more information. +- Made streaming connection retryable in the case goes down. +- Fixed the logs messages in SplitClientImpl. + +4.6.0 (Nov 28, 2022) +- Added support redis cluster by providing JedisCluster object. +- Updated Jedis to 4.3.0 to support tls. + +4.5.0 (Oct 12, 2022) +- Added a new impressions mode for the SDK called NONE, to be used in factory when there is no desire to capture impressions on an SDK factory to feed Split's analytics engine. Running NONE mode, the SDK will only capture unique keys evaluated for a particular feature flag instead of full blown impressions. + +4.4.8 (Sep 16, 2022) +- Updated `org.yaml.snakeyaml` dependence to 1.32 for fixing a vulnerability. +- Improved SplitClientIntegrationTest removing ignore annotation in some test cases. + +4.4.7 (Sep 14, 2022) +- Fixed fromImpression in KeyImpression to map previous time. +- Updated `org.yaml.snakeyaml` dependence to 1.31 for fixing a vulnerability. +- Fixed buildKeyWithPrefix and added CommonRedis to share it. + +4.4.6 (Sep 6, 2022) +- Made junit as provided dependency. +- Fixed destroy logic to stop all the threads in execution. +- Refactored SyncManager to be able to start/stop tasks and removed that logic from destroy method. +- Updated EventTask, decoupled Storage logic, and added a new scheduler task in charge of flushing events. + +4.4.5 (Jul 29, 2022) +- Fixed in Synchronizer not properly synchronizing newly referenced segments. +- Update pom files to have profiles for releases. + +4.4.4 (May 24, 2022) +- Updated `com.google.code.gson` to 2.9.0 for fixing vulnerability. + +4.4.3 (May 9, 2022) +- Updated shaded dependencies: `org.apache`, `org.checkerframework` and `org.yaml.snakeyaml` +- Updated `org.slf4j` to 1.7.36 for fixing vulnerability. + +4.4.2 (Feb 22, 2022) +- Cleaned up log messages in segments logic. + +4.4.1 (Feb 08, 2022) +- Added improvements in segment fetcher to work as threading pool. +- Fixed backoff handler for streaming auth calls due to an issue that stopped retrying after multiple subsequent errors. +- Updated snakeyaml dependencies and reorganized shaded dependencies. + +4.4.0 (Jan 11, 2022) +- Added support for Redis to keep consistency across multiple SDK instances. +- Added logic to fetch multiple feature flags at once on GetTreatments/GetTreatmentsWithChanges. + +4.3.0 (Oct 19, 2021) +- Added support for the SDK to run with a custom implementation of it's internal storage modules, enabling customers to implement this caching in any storage technology of choice and connect it to the SDK instance itself which will use it instead of the in-memory structures. +- Fixed a possible OOM when there were too many Streaming events being captured, applying the expected limit of 20. +- Fixed an NPE caused in a race condition on telemetry which generated noisy logs. (https://github.com/splitio/java-client/issues/251)[#251] + +4.2.1 (Jun 18, 2021) +- Updated settings for httpClient instances to add stale check to more effectively manage connection pools after it was turned off by default on httpClient 5.x.x. +- Updated TelemetryRefreshRate setting default value to 3600s. + +4.2.0 (Jun 7, 2021) +- Updated SDK telemetry storage, metrics and updater to be more effective and send less often. +- Improved the synchronization flow to be more reliable in the event of an edge case generating delay in cache purge propagation, keeping the SDK cache properly synced. +- Fixed issue where the SDK was validating no Feature flag had over 50 conditions (legacy code). +- Bumped guava version to 30. + +4.1.6 (Apr 15, 2021) +- Updated log level and message in some messages. + +4.1.5 (Apr 6, 2021) +- Updated streaming logic to use limited fetch retry attempts. + +4.1.4 (Mar 19, 2021) +- Updated Internal cache structure refactor. +- Updated Streaming revamp with several bugfixes and improved log messages. +- Added Cache-Control header for on-demand requests to sdk-server. +- Updated Localhost Client revamp & bugfix for missing feature flags. + +4.1.3 (Dec 2, 2020) +- Fix Issue when closing SSE Connection +- Updated log-level for some messages + +4.1.2 (Nov 25, 2020) +- Updated junit from 4.12 to 4.13.1 +- Updated HttpClient from 4.5.2 to 5.0.3 + +4.1.1 (Sep 30, 2020) +- Fixed fetch retries after received an SPLIT_CHANGE. + +4.1.0 (Sep 25, 2020) +- Added `OPTIMIZED` and `DEBUG` modes in order to enabling/disabling how impressions are going to be sent into Split servers, + - `OPTIMIZED`: will send unique impressions in a timeframe in order to reduce how many times impressions are posted to Split. + - `DEBUG`: will send every impression generated to Split. + +4.0.1 (Sep 4, 2020) +- Remove jersey. Use custom SSE implementation +- Bumped guava version to 29 + +4.0.0 (Aug 19, 2020) +- Deprecated Java 7 support. Java 8 is the minimum supported version for this and future releases. +- Added support for the new Split streaming architecture. When enabled (default), the SDK will not poll for updates but instead receive notifications every time there's a change in your environments, allowing to process those much quicker. If disabled or in the event of an issue, the SDK will fallback to the known polling mechanism to provide a seamless experience. +- Updated the default of featuresRefreshRate to 60 seconds. + +3.3.4 (Jul 15, 2020) +- Added Impression observer. + +3.3.3 (Apr 7, 2020) + - Fix issue regarding special characters come from feature flags/segments fetchers. + +3.3.2 (Jan 24, 2020) + - Shade com.google.guava as well + +3.3.1 (Nov 1, 2019) + - Allow client to disable sending the IP address & hostname. + +3.3.0 (Sep 23, 2019) +- rename version.properties to splitversion.properties to avoid conflicts with other tools that use the former file name and causes this SDK to not properly report the version +- Adds New Relic Integration. Now if the New Relic agent is running, it will attach Impression data to the current transaction in New Relic, + +3.2.4 (Sep 9, 2019) +- Fix small issue regarding traffic type validation on .track() calls. + +3.2.3 (Aug 1, 2019) +- allow to push impressions more often than one every 30 seconds and events flush rate is now customizable + +3.2.2 +- log warn and not error when Feature flag doesn't exist in the environment + +3.2.1 (May 29, 2019) +- Fix issue with events not forwarding the value when sending properties as well. + +3.2.0 (May 24, 2019) +- Add ability to send events with properties + +3.1.1 (May 2, 2019) +- Fix issue with locating localhost mode in windows +- Fix issue when user falls out of traffic allocation and has config data + +3.1.0 (Apr 16, 2019) +- Add Dynamic Configurations +- Support Yaml files for localhost mode + +3.0.9 (Mar 21, 2019) +- Validate feature flag names on getTreatment and manager calls +- Validate traffic type name when calling track +- Check for multiple factories instantiated + +3.0.8 (Mar 7, 2019) +- Ability to disable automatic destroy() on graceful shutdowns. + +3.0.7 +- Removing unused thrown exceptions from build signature +- Setting feature refresh rate to 5 seconds by default + +3.0.6 +- Setting CookieSpec to Standard + +3.0.5 (Jan 5, 2019) +- Adding input validation for fast feedback of incorrect use of the client. + +3.0.4 (Dec 14, 2018) +- Fix potential race condition when segment wait for readiness before they are registered to the readiness gates + +3.0.3 (Dec 12, 2018) +- Expose param to increase thread pool size for segments + +3.0.2 (Dec 12, 2018) +- Fixed traffic allocation issue on 1% + +3.0.1 +- Fix Metric Counters when using Split Proxy. + +3.0.0 +- Moving block until ready out of the factory into the split client and manager client + +2.3.2 (Jul 18, 2018) +- Add support for handling different relative path in the endpoints of the Split Synchronizer + +2.3.1 (Apr 18, 2018) +- Added support for key overrides in localhost mode + +2.3.0 (Jan 15, 2018) +- Add support for .track() + +2.2.2 (Dec 22, 2017) +- Patch for localhost mode to daemonize the thread which currently prevents the JVM from exiting. + +2.2.1 (Dec 22, 2017) +- Transitioning from "no rule matched" to "default rule" and from "rules not found" to "definition not found" + +2.2.0 (Sep 22, 2017) +- Patch DependencyMatcherData to not error out when dependencies are used +- Default logging is less verbose now +- Add support for Proxy + +2.1.0 (July 19th, 2017) +- Add support for new boolean and regular expression matchers +- Support for Dependency matcher: 'in feature flag "xxx" treatments ["xxx","yyy"]' + +2.0.4 (June 2nd, 2017) +- Support to destroy all clients and background processes before shutdown via splitFactory.destroy() or splitClient.destroy() +- Define SLF4j in the root pom instead of twice. + +2.0.2 (May 16th, 2017) +- Support for Set based matchers: 'has any of', 'has all of', 'is equal to', and 'is part of' +- Support for string matchers: 'starts with', 'ends with', and 'contains' +- Added ability to attach metadata to impressions for getTreatment calls + +2.0.1 (May 3rd, 2017) +- Fix issue for Java 7 and compatibility with TLS1.2 + +2.0 (Apr 26, 2017) +- Transitioning to Murmur3 for key hashing +- Hashing Algorithm dependent on Split algo field + +1.1.7 (Mar 31, 2017) +- We can now specify a percentage of traffic that can be in the Split - i.e. traffic allocation. +- You can now register your own ImpressionListener. SplitClientConfig#impressionListener. +- You can no longer set any refresh rates to be less than 30 seconds. + +1.1.6 (Feb 27, 2017) +- Allowing users to set the Off-The-Grid directory +- Adding Live Reload of Split File in Off-The-Grid mode + +1.1.5 (Feb 8, 2017) +- Introducing SplitClientForTest and SplitTestRunner for JUnit. More information at split.io/blog/feature-flag-testing + +1.1.4 (Feb 8, 2017) +- Use less guava. + +1.1.3 (Jan 9, 2017) +- Impression to include the changeNumber of the Split that served a getTreatment call. +- expose disableLabels + +1.1.2 (Dec 19, 2016) +- Adding SplitManager.splitNames() support to retrieve feature names. +- Improving Java 1.7 compatibility for Hashcodes + +1.1.1 +- Consistently labeling Rules in messages + +1.1 +- Move java-client into it's own repo. + +1.0.10 +- The SDK now sends rule labels back to the server + +1.0.9 +- Introduced new API: getTreatment(Key key, String feature, Map attributes); +- Deprecated SplitClientBuilder. Clients are expected to use SplitFactoryBuilder instead. + +1.0.8 +- Fixed NPE in SplitManager + +1.0.7 +- Implement Factory pattern and expose Split manager +- Stop parsing a Feature flag when there are more than 50 conditions to prevent DDOS. + +1.0.6 +- Replaced Jersey with HttpComponents +- Dropped SplitClientConfig#eventsEndpoint +- Modified SplitClientConfig#endpoint to take in both sdk and events api. +- Move away from Guava cache for impressions and use an BlockingArrayList instead. + +1.0.5 +- shade jackson-databind to split.shade.xxxxx +- remove hamcrest and mockito from fat jar +- include only io.split, io.codigo and (shaded) guava in the fat jar +- Clean up JAVA 1.8 dependencies making sure they all are major version 51. + +1.0.4 +- blockUntilReady support +- impressions and metrics to events.split.io + +1.0.3 +- add https support + +1.0.2 +- corrects pom file issues + +1.0.1 +- add support for attributes + +1.0.0 +- First release diff --git a/LICENSE b/LICENSE index fb8a05ab..b6579621 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright © 2020 Split Software, Inc. +Copyright © 2026 Split Software, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 2771f2a1..e7ebca56 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,13 @@ # Split Java SDK - -[![Build Status](https://api.travis-ci.com/splitio/java-client.svg?branch=master)](https://api.travis-ci.com/splitio/java-client) +![Build Status](https://github.com/splitio/java-client/actions/workflows/ci-cd.yml/badge.svg?branch=master) ## Overview -This SDK is designed to work with Split, the platform for controlled rollouts, serving features to your users via the Split feature flag to manage your complete customer experience. +This SDK is designed to work with Split, the platform for controlled rollouts, serving features to your users via feature flags to manage your complete customer experience. [![Twitter Follow](https://img.shields.io/twitter/follow/splitsoftware.svg?style=social&label=Follow&maxAge=1529000)](https://twitter.com/intent/follow?screen_name=splitsoftware) ## Compatibility -This SDK is compatible with Java 6 and higher. +This SDK is compatible with Java 8 and higher. ## Getting started Below is a simple example that describes the instantiation and most basic usage of our SDK: @@ -23,19 +22,19 @@ public class App { SplitClientConfig config = SplitClientConfig.builder() .setBlockUntilReadyTimeout(10000) .build(); - SplitFactory splitFactory = SplitFactoryBuilder.build("SDK_API_KEY", config); + SplitFactory splitFactory = SplitFactoryBuilder.build("YOUR_SDK_KEY", config); SplitClient client = splitFactory.client(); try { client.blockUntilReady(); } catch (TimeoutException | InterruptedException e) { - // log & handle + // log & handle } - String treatment = client.getTreatment("CUSTOMER_ID", "SPLIT_NAME"); + String treatment = client.getTreatment("CUSTOMER_ID", "FEATURE_FLAG_NAME"); if (treatment.equals("on")) { - // Feature is enabled for this user! + // Feature flag is enabled for this user! } else if (treatment.equals("off")) { - // Feature is disabled for this user! + // Feature flag is disabled for this user! } else { // Unable to perform evaluation. } @@ -44,7 +43,7 @@ public class App { ``` ## Submitting issues - + The Split team monitors all issues submitted to this [issue tracker](https://github.com/splitio/java-client/issues). We encourage you to use this issue tracker to submit any bug reports, feedback, and feature enhancements. We'll do our best to respond in a timely manner. ## Contributing @@ -54,26 +53,31 @@ Please see [Contributors Guide](CONTRIBUTORS-GUIDE.md) to find all you need to s Licensed under the Apache License, Version 2.0. See: [Apache License](http://www.apache.org/licenses/). ## About Split - + Split is the leading Feature Delivery Platform for engineering teams that want to confidently deploy features as fast as they can develop them. Split’s fine-grained management, real-time monitoring, and data-driven experimentation ensure that new features will improve the customer experience without breaking or degrading performance. Companies like Twilio, Salesforce, GoDaddy and WePay trust Split to power their feature delivery. - + To learn more about Split, contact hello@split.io, or get started with feature flags for free at https://www.split.io/signup. - + Split has built and maintains SDKs for: - + * Java [Github](https://github.com/splitio/java-client) [Docs](https://help.split.io/hc/en-us/articles/360020405151-Java-SDK) -* Javascript [Github](https://github.com/splitio/javascript-client) [Docs](https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK) +* JavaScript [Github](https://github.com/splitio/javascript-client) [Docs](https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK) +* JavaScript for Browser [Github](https://github.com/splitio/javascript-browser-client) [Docs](https://help.split.io/hc/en-us/articles/360058730852-Browser-SDK) * Node [Github](https://github.com/splitio/javascript-client) [Docs](https://help.split.io/hc/en-us/articles/360020564931-Node-js-SDK) -* .NET [Github](https://github.com/splitio/.net-core-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) +* .NET [Github](https://github.com/splitio/dotnet-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) * Ruby [Github](https://github.com/splitio/ruby-client) [Docs](https://help.split.io/hc/en-us/articles/360020673251-Ruby-SDK) * PHP [Github](https://github.com/splitio/php-client) [Docs](https://help.split.io/hc/en-us/articles/360020350372-PHP-SDK) * Python [Github](https://github.com/splitio/python-client) [Docs](https://help.split.io/hc/en-us/articles/360020359652-Python-SDK) * GO [Github](https://github.com/splitio/go-client) [Docs](https://help.split.io/hc/en-us/articles/360020093652-Go-SDK) * Android [Github](https://github.com/splitio/android-client) [Docs](https://help.split.io/hc/en-us/articles/360020343291-Android-SDK) * iOS [Github](https://github.com/splitio/ios-client) [Docs](https://help.split.io/hc/en-us/articles/360020401491-iOS-SDK) - +* Angular [Github](https://github.com/splitio/angular-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/6495326064397-Angular-utilities) +* React [Github](https://github.com/splitio/react-client) [Docs](https://help.split.io/hc/en-us/articles/360038825091-React-SDK) +* React Native [Github](https://github.com/splitio/react-native-client) [Docs](https://help.split.io/hc/en-us/articles/4406066357901-React-Native-SDK) +* Redux [Github](https://github.com/splitio/redux-client) [Docs](https://help.split.io/hc/en-us/articles/360038851551-Redux-SDK) + For a comprehensive list of open source projects visit our [Github page](https://github.com/splitio?utf8=%E2%9C%93&query=%20only%3Apublic%20). - + **Learn more about Split:** - + Visit [split.io/product](https://www.split.io/product) for an overview of Split, or visit our documentation at [help.split.io](http://help.split.io) for more detailed information. diff --git a/client/CHANGES.txt b/client/CHANGES.txt deleted file mode 100644 index e02d816d..00000000 --- a/client/CHANGES.txt +++ /dev/null @@ -1,219 +0,0 @@ -CHANGES - -4.1.6 (Apr 15, 2021) --Updated log level and message in some messages. - -4.1.5 (Apr 6, 2021) --Updated: Streaming retry fix. - -4.1.4 (Mar 19, 2021) -- Updated: Internal cache structure refactor. -- Updated: Streaming revamp with several bugfixes and improved log messages. -- Added: Cache-Control header for on-demand requests to sdk-server. -- Updated: Localhost Client revamp & bugfix for missing splits. - -4.1.3 (Dec 2, 2020) -- Fix Issue when closing SSE Connection -- Updated log-level for some messages - -4.1.2 (Nov 25, 2020) -- Updated junit from 4.12 to 4.13.1 -- Updated HttpClient from 4.5.2 to 5.0.3 - -4.1.1 (Sep 30, 2020) -- Fixed fetch retries after received an SPLIT_CHANGE. - -4.1.0 (Sep 25, 2020) -- Add local impressions deduping (enabled by default) - -4.0.1 (Sep 4, 2020) -- Remove jersey. Use custom SSE implementation -- Bumped guava version to 29 - -4.0.0 (Aug 19, 2020) -- Deprecated Java 7 support. Java 8 is the minimum supported version for this and future releases. -- Added support for the new Split streaming architecture. When enabled (default), the SDK will not poll for updates but instead receive notifications every time there's a change in your environments, allowing to process those much quicker. If disabled or in the event of an issue, the SDK will fallback to the known polling mechanism to provide a seamless experience. -- Updated the default of featuresRefreshRate to 60 seconds. - -3.3.4 (Jul 15, 2020) -- Added Impression observer. - -3.3.3 (Apr 7, 2020) - - Fix issue regarding special characters come from split/segments fetchers. - -3.3.2 (Jan 24, 2020) - - Shade com.google.guava as well - -3.3.1 (Nov 1, 2019) - - Allow client to disable sending the IP address & hostname. - -3.3.0 (Sep 23, 2019) -- rename version.properties to splitversion.properties to avoid conflicts with other tools that use the former file name and causes this SDK to not properly report the version -- Adds New Relic Integration. Now if the New Relic agent is running, it will attach Impression data to the current transaction in New Relic, - -3.2.4 (Sep 9, 2019) -- Fix small issue regarding traffic type validation on .track() calls. - -3.2.3 (Aug 1, 2019) -- allow to push impressions more often than one every 30 seconds and events flush rate is now customizable - -3.2.2 -- log warn and not error when Split doesn't exist in the environment - -3.2.1 (May 29, 2019) -- Fix issue with events not forwarding the value when sending properties as well. - -3.2.0 (May 24, 2019) -- Add ability to send events with properties - -3.1.1 (May 2, 2019) -- Fix issue with locating localhost mode in windows -- Fix issue when user falls out of traffic allocation and has config data - -3.1.0 (Apr 16, 2019) -- Add Dynamic Configurations -- Support Yaml files for localhost mode - -3.0.9 (Mar 21, 2019) -- Validate split names on getTreatment and manager calls -- Validate traffic type name when calling track -- Check for multiple factories instantiated - -3.0.8 (Mar 7, 2019) -- Ability to disable automatic destroy() on graceful shutdowns. - -3.0.7 -- Removing unused thrown exceptions from build signature -- Setting feature refresh rate to 5 seconds by default - -3.0.6 -- Setting CookieSpec to Standard - -3.0.5 (Jan 5, 2019) -- Adding input validation for fast feedback of incorrect use of the client. - -3.0.4 (Dec 14, 2018) -- Fix potential race condition when segment wait for readiness before they are registered to the readiness gates - -3.0.3 (Dec 12, 2018) -- Expose param to increase thread pool size for segments - -3.0.2 (Dec 12, 2018) -- Fixed traffic allocation issue on 1% - -3.0.1 -- Fix Metric Counters when using Split Proxy. - -3.0.0 -- Moving block until ready out of the factory into the split client and manager client - -2.3.2 (Jul 18, 2018) -- Add support for handling different relative path in the endpoints of the Split Synchronizer - -2.3.1 (Apr 18, 2018) -- Added support for key overrides in localhost mode - -2.3.0 (Jan 15, 2018) -- Add support for .track() - -2.2.2 (Dec 22, 2017) -- Patch for localhost mode to daemonize the thread which currently prevents the JVM from exiting. - -2.2.1 (Dec 22, 2017) -- Transitioning from "no rule matched" to "default rule" and from "rules not found" to "definition not found" - -2.2.0 (Sep 22, 2017) -- Patch DependencyMatcherData to not error out when dependencies are used -- Default logging is less verbose now -- Add support for Proxy - -2.1.0 (July 19th, 2017) -- Add support for new boolean and regular expression matchers -- Support for Dependency matcher: 'in split "xxx" treatments ["xxx","yyy"]' - -2.0.4 (June 2nd, 2017) -- Support to destroy all clients and background processes before shutdown via splitFactory.destroy() or splitClient.destroy() -- Define SLF4j in the root pom instead of twice. - -2.0.2 (May 16th, 2017) -- Support for Set based matchers: 'has any of', 'has all of', 'is equal to', and 'is part of' -- Support for string matchers: 'starts with', 'ends with', and 'contains' -- Added ability to attach metadata to impressions for getTreatment calls - -2.0.1 (May 3rd, 2017) -- Fix issue for Java 7 and compatibility with TLS1.2 - -2.0 (Apr 26, 2017) -- Transitioning to Murmur3 for key hashing -- Hashing Algorithm dependent on Split algo field - -1.1.7 (Mar 31, 2017) -- We can now specify a percentage of traffic that can be in the Split - i.e. traffic allocation. -- You can now register your own ImpressionListener. SplitClientConfig#impressionListener. -- You can no longer set any refresh rates to be less than 30 seconds. - -1.1.6 (Feb 27, 2017) -- Allowing users to set the Off-The-Grid directory -- Adding Live Reload of Split File in Off-The-Grid mode - -1.1.5 (Feb 8, 2017) -- Introducing SplitClientForTest and SplitTestRunner for JUnit. More information at split.io/blog/feature-flag-testing - -1.1.4 (Feb 8, 2017) -- Use less guava. - -1.1.3 (Jan 9, 2017) -- Impression to include the changeNumber of the Split that served a getTreatment call. -- expose disableLabels - -1.1.2 (Dec 19, 2016) -- Adding SplitManager.splitNames() support to retrieve feature names. -- Improving Java 1.7 compatibility for Hashcodes - -1.1.1 -- Consistently labeling Rules in messages - -1.1 -- Move java-client into it's own repo. - -1.0.10 -- The SDK now sends rule labels back to the server - -1.0.9 -- Introduced new API: getTreatment(Key key, String feature, Map attributes); -- Deprecated SplitClientBuilder. Clients are expected to use SplitFactoryBuilder instead. - -1.0.8 -- Fixed NPE in SplitManager - -1.0.7 -- Implement Factory pattern and expose Split manager -- Stop parsing a Split when there are more than 50 conditions to prevent DDOS. - -1.0.6 -- Replaced Jersey with HttpComponents -- Dropped SplitClientConfig#eventsEndpoint -- Modified SplitClientConfig#endpoint to take in both sdk and events api. -- Move away from Guava cache for impressions and use an BlockingArrayList instead. - -1.0.5 -- shade jackson-databind to split.shade.xxxxx -- remove hamcrest and mockito from fat jar -- include only io.split, io.codigo and (shaded) guava in the fat jar -- Clean up JAVA 1.8 dependencies making sure they all are major version 51. - -1.0.4 -- blockUntilReady support -- impressions and metrics to events.split.io - -1.0.3 -- add https support - -1.0.2 -- corrects pom file issues - -1.0.1 -- add support for attributes - -1.0.0 -- First release diff --git a/client/pom.xml b/client/pom.xml index 5336cf31..882cc9c9 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -5,12 +5,36 @@ io.split.client java-client-parent - 4.1.6 + 4.19.1 + 4.19.1 java-client jar Java Client Java SDK for Split + + + + release + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + false + central + false + published + + + + + + + @@ -19,17 +43,67 @@ 3.3 false - 8 - 8 + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + true + org.apache.maven.plugins maven-shade-plugin 3.2.4 + + + shade-non-shaded + package + + shade + + + true + non-shaded + false + + + io.split.client:java-client + io.harness:events + + + + + io.harness.events + split.io.harness.events + + + + + io.harness:events + + META-INF/** + + + + + + shade-fat package shade @@ -41,22 +115,42 @@ io.split.engine:* io.split.client:* + io.harness:events io.split.schemas:* io.codigo.grammar:* - org.apache.httpcomponents:httpclient - org.apache.httpcomponents:httpcore + org.apache.httpcomponents.* + org.apache.hc.* com.google.code.gson:gson com.google.guava:guava + org.yaml:snakeyaml:* + + + org.jspecify:* + + + commons-codec:* - - + + - org.apache.http - split.org.apache.http + io.harness.events + split.io.harness.events + + + org.apache + split.org.apache + + + org.jspecify + split.org.jspecify + + + org.yaml.snakeyaml + split.org.yaml.snakeyaml com.google @@ -67,10 +161,7 @@ *:* - META-INF/license/** - META-INF/* - META-INF/maven/** - META-INF/services/** + META-INF/** LICENSE NOTICE /*.txt @@ -94,18 +185,6 @@ - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.3 - true - - ossrh - https://oss.sonatype.org/ - true - - src/main/resources/splitversion.properties @@ -117,37 +196,54 @@ + + 1.7.36 + 8 + 8 + + + + io.harness + events + 1.0.0 + + + io.split.client + pluggable-storage + 2.1.0 + compile + com.google.guava guava - 29.0-jre + 33.4.8-jre org.slf4j slf4j-api + ${slf4j.api.version} org.apache.httpcomponents.client5 httpclient5 - 5.0.3 + 5.6.4 com.google.code.gson gson - 2.6.2 + 2.13.1 org.yaml snakeyaml - 1.21 + 2.0 - org.apache.commons commons-lang3 - 3.4 + 3.18.0 test @@ -170,7 +266,7 @@ org.slf4j slf4j-log4j12 - 1.7.21 + ${slf4j.api.version} test @@ -203,5 +299,17 @@ 4.0.3 test + + org.powermock + powermock-module-junit4 + 1.7.4 + test + + + org.powermock + powermock-api-mockito + 1.7.4 + test + diff --git a/client/src/main/java/io/split/Spec.java b/client/src/main/java/io/split/Spec.java new file mode 100644 index 00000000..b2c7de4b --- /dev/null +++ b/client/src/main/java/io/split/Spec.java @@ -0,0 +1,12 @@ +package io.split; + +public final class Spec { + + private Spec() { + // restrict instantiation + } + + public static final String SPEC_1_3 = "1.3"; + public static final String SPEC_1_1 = "1.1"; +} + diff --git a/client/src/main/java/io/split/cache/InMemoryCacheImp.java b/client/src/main/java/io/split/cache/InMemoryCacheImp.java deleted file mode 100644 index decc31d2..00000000 --- a/client/src/main/java/io/split/cache/InMemoryCacheImp.java +++ /dev/null @@ -1,126 +0,0 @@ -package io.split.cache; - -import com.google.common.collect.ConcurrentHashMultiset; -import com.google.common.collect.Maps; -import com.google.common.collect.Multiset; -import com.google.common.collect.Sets; -import io.split.engine.experiments.ParsedSplit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicLong; - -public class InMemoryCacheImp implements SplitCache { - - private static final Logger _log = LoggerFactory.getLogger(InMemoryCacheImp.class); - - private final ConcurrentMap _concurrentMap; - private final Multiset _concurrentTrafficTypeNameSet; - - private AtomicLong _changeNumber; - - public InMemoryCacheImp() { - this(-1); - } - - public InMemoryCacheImp(long startingChangeNumber) { - _concurrentMap = Maps.newConcurrentMap(); - _changeNumber = new AtomicLong(startingChangeNumber); - _concurrentTrafficTypeNameSet = ConcurrentHashMultiset.create(); - } - - @Override - public void put(ParsedSplit split) { - _concurrentMap.put(split.feature(), split); - - if (split.trafficTypeName() != null) { - _concurrentTrafficTypeNameSet.add(split.trafficTypeName()); - } - } - - @Override - public boolean remove(String name) { - ParsedSplit removed = _concurrentMap.remove(name); - - if (removed != null && removed.trafficTypeName() != null) { - _concurrentTrafficTypeNameSet.remove(removed.trafficTypeName()); - } - - return removed != null; - } - - @Override - public ParsedSplit get(String name) { - return _concurrentMap.get(name); - } - - @Override - public Collection getAll() { - return _concurrentMap.values(); - } - - @Override - public Collection getMany(List names) { - List splits = new ArrayList<>(); - - for (String name : names) { - ParsedSplit split = _concurrentMap.get(name); - - if (split != null) { - splits.add(split); - } - } - - return splits; - } - - @Override - public long getChangeNumber() { - return _changeNumber.get(); - } - - @Override - public void setChangeNumber(long changeNumber) { - if (changeNumber < _changeNumber.get()) { - _log.error("ChangeNumber for splits cache is less than previous"); - } - - _changeNumber.set(changeNumber); - } - - @Override - public boolean trafficTypeExists(String trafficTypeName) { - // If the multiset has [{"user",2}.{"account",0}], elementSet only returns - // ["user"] (it ignores "account") - return Sets.newHashSet(_concurrentTrafficTypeNameSet.elementSet()).contains(trafficTypeName); - } - - @Override - public void kill(String splitName, String defaultTreatment, long changeNumber) { - ParsedSplit parsedSplit = _concurrentMap.get(splitName); - - ParsedSplit updatedSplit = new ParsedSplit(parsedSplit.feature(), - parsedSplit.seed(), - true, - defaultTreatment, - parsedSplit.parsedConditions(), - parsedSplit.trafficTypeName(), - changeNumber, - parsedSplit.trafficAllocation(), - parsedSplit.trafficAllocationSeed(), - parsedSplit.algo(), - parsedSplit.configurations()); - - _concurrentMap.put(splitName, updatedSplit); - } - - @Override - public void clear() { - _concurrentMap.clear(); - _concurrentTrafficTypeNameSet.clear(); - } -} diff --git a/client/src/main/java/io/split/cache/SegmentCache.java b/client/src/main/java/io/split/cache/SegmentCache.java deleted file mode 100644 index d75fe11d..00000000 --- a/client/src/main/java/io/split/cache/SegmentCache.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.split.cache; - -import java.util.List; - -/** - * Memory for segments - * @author lucasecheverz - */ -public interface SegmentCache { - - /** - * update segment - * @param segmentName - * @param toAdd - * @param toRemove - */ - void updateSegment(String segmentName, List toAdd, List toRemove) ; - - /** - * evaluates if a key belongs to a segment - * @param segmentName - * @param key - * @return - */ - boolean isInSegment(String segmentName, String key); - - /** - * update the changeNumber of a segment - * @param segmentName - * @param changeNumber - */ - void setChangeNumber(String segmentName, long changeNumber); - - /** - * returns the changeNumber of a segment - * @param segmentName - * @return - */ - long getChangeNumber(String segmentName); - - /** - * clear all segments - */ - void clear(); -} diff --git a/client/src/main/java/io/split/cache/SplitCache.java b/client/src/main/java/io/split/cache/SplitCache.java deleted file mode 100644 index c9769d17..00000000 --- a/client/src/main/java/io/split/cache/SplitCache.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.split.cache; - -import io.split.engine.experiments.ParsedSplit; - -import java.util.Collection; -import java.util.List; - -public interface SplitCache { - void put(ParsedSplit split); - boolean remove(String name); - ParsedSplit get(String name); - Collection getAll(); - Collection getMany(List names); - long getChangeNumber(); - void setChangeNumber(long changeNumber); - boolean trafficTypeExists(String trafficTypeName); - void kill(String splitName, String defaultTreatment, long changeNumber); - void clear(); -} diff --git a/client/src/main/java/io/split/client/AbstractLocalhostSplitFile.java b/client/src/main/java/io/split/client/AbstractLocalhostSplitFile.java deleted file mode 100644 index 2027c6b0..00000000 --- a/client/src/main/java/io/split/client/AbstractLocalhostSplitFile.java +++ /dev/null @@ -1,104 +0,0 @@ -package io.split.client; - -import com.google.common.base.Preconditions; -import com.sun.nio.file.SensitivityWatchEventModifier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.StandardWatchEventKinds; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -public abstract class AbstractLocalhostSplitFile extends Thread { - private static final Logger _log = LoggerFactory.getLogger(AbstractLocalhostSplitFile.class); - - protected final LocalhostSplitFactory _splitFactory; - protected final File _file; - protected final WatchService _watcher; - protected final AtomicBoolean _stop; - - public AbstractLocalhostSplitFile(LocalhostSplitFactory splitFactory, String directory, String fileName) throws IOException { - Preconditions.checkNotNull(directory); - Preconditions.checkNotNull(fileName); - - _splitFactory = Preconditions.checkNotNull(splitFactory); - - // If no directory is set, instantiate the file without parent, otherwise the path separator is inserted - // before the filename in the java.io.File.File(java.lang.String, java.lang.String) class (see line 319). - _file = (directory.length() > 0) ? - new File(directory, fileName) : - new File(fileName); - - _watcher = FileSystems.getDefault().newWatchService(); - _stop = new AtomicBoolean(false); - } - - public boolean isStopped() { - return _stop.get(); - } - - public void stopThread() { - _stop.set(true); - } - - public void registerWatcher() throws IOException { - Path path = _file.toPath().toAbsolutePath().getParent(); - path.register(_watcher, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_MODIFY}, SensitivityWatchEventModifier.HIGH); - } - - @Override - public void run() { - try { - while (!isStopped()) { - WatchKey key; - try { - key = _watcher.poll(250, TimeUnit.MILLISECONDS); - } catch (InterruptedException e) { - stopThread(); - return; - } - if (key == null) { - Thread.yield(); - continue; - } - - for (WatchEvent event : key.pollEvents()) { - WatchEvent.Kind kind = event.kind(); - - @SuppressWarnings("unchecked") - WatchEvent ev = (WatchEvent) event; - Path filename = ev.context(); - - if (kind == StandardWatchEventKinds.OVERFLOW) { - Thread.yield(); - continue; - } else if (kind == StandardWatchEventKinds.ENTRY_MODIFY - && filename.toString().equals(_file.getName())) { - Map featureToSplitMap = readOnSplits(); - _splitFactory.updateFeatureToTreatmentMap(featureToSplitMap); - _log.info("Detected change in Local Splits file - Splits Reloaded! file={}", _file.getPath()); - } - boolean valid = key.reset(); - if (!valid) { - break; - } - } - Thread.yield(); - } - } catch (IOException e) { - _log.error("Error reading file: path={}", _file.getPath(), e); - stopThread(); - } - } - - public abstract Map readOnSplits() throws IOException; - -} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/AlwaysReturnControlSplitClient.java b/client/src/main/java/io/split/client/AlwaysReturnControlSplitClient.java deleted file mode 100644 index 7c957371..00000000 --- a/client/src/main/java/io/split/client/AlwaysReturnControlSplitClient.java +++ /dev/null @@ -1,80 +0,0 @@ -package io.split.client; - -import io.split.client.api.Key; -import io.split.client.api.SplitResult; -import io.split.grammar.Treatments; - -import java.util.Map; -import java.util.concurrent.TimeoutException; - -/** - * A SplitClient that ensures that all features are turned off for all users. - * Useful for testing - * - * @author adil - */ -public class AlwaysReturnControlSplitClient implements SplitClient { - - private static final SplitResult RESULT_CONTROL = new SplitResult(Treatments.CONTROL, null); - - @Override - public String getTreatment(String key, String split) { - return Treatments.CONTROL; - } - - @Override - public String getTreatment(String key, String split, Map attributes) { - return Treatments.CONTROL; - } - - @Override - public String getTreatment(Key key, String split, Map attributes) { - return Treatments.CONTROL; - } - - @Override - public SplitResult getTreatmentWithConfig(String key, String split) { - return RESULT_CONTROL; - } - - @Override - public SplitResult getTreatmentWithConfig(String key, String split, Map attributes) { - return RESULT_CONTROL; - } - - @Override - public SplitResult getTreatmentWithConfig(Key key, String split, Map attributes) { - return RESULT_CONTROL; - } - - @Override - public void destroy() { - - } - - @Override - public boolean track(String key, String trafficType, String eventType) { - return false; - } - - @Override - public boolean track(String key, String trafficType, String eventType, double value) { - return false; - } - - @Override - public boolean track(String key, String trafficType, String eventType, Map properties) { - return false; - } - - @Override - public boolean track(String key, String trafficType, String eventType, double value, Map properties) { - return false; - } - - @Override - public void blockUntilReady() throws TimeoutException, InterruptedException { - //AlwaysReturnControl is always ready - } - -} diff --git a/client/src/main/java/io/split/client/ApiKeyCounter.java b/client/src/main/java/io/split/client/ApiKeyCounter.java index 8c39394d..4ac8c16c 100644 --- a/client/src/main/java/io/split/client/ApiKeyCounter.java +++ b/client/src/main/java/io/split/client/ApiKeyCounter.java @@ -6,6 +6,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; + public class ApiKeyCounter { private static final Logger _log = LoggerFactory.getLogger(ApiKeyCounter.class); @@ -23,13 +26,13 @@ private static class ApyKeyCounterHolder private static final ApiKeyCounter INSTANCE = new ApiKeyCounter(); } - public void add(String apiKey) { + public void add(String sdkKey) { String message; - if (USED_API_KEYS.contains(apiKey)) { - message = String.format("factory instantiation: You already have %s with this API Key. " + + if (USED_API_KEYS.contains(sdkKey)) { + message = String.format("factory instantiation: You already have %s with this SDK Key. " + "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing " + "it throughout your application.", - USED_API_KEYS.count(apiKey) == 1 ? "1 factory" : String.format("%s factories", USED_API_KEYS.count(apiKey))); + USED_API_KEYS.count(sdkKey) == 1 ? "1 factory" : String.format("%s factories", USED_API_KEYS.count(sdkKey))); _log.warn(message); } else if (!USED_API_KEYS.isEmpty()) { message = "factory instantiation: You already have an instance of the Split factory. " + @@ -37,30 +40,43 @@ public void add(String apiKey) { "the factory at all times (Singleton pattern) and reusing it throughout your application.“"; _log.warn(message); } - USED_API_KEYS.add(apiKey); + USED_API_KEYS.add(sdkKey); } - public void remove(String apiKey) { - USED_API_KEYS.remove(apiKey); + public void remove(String sdkKey) { + USED_API_KEYS.remove(sdkKey); } /** * Just for test - * @param apiKey + * @param sdkKey * @return */ @VisibleForTesting - boolean isApiKeyPresent(String apiKey) { - return USED_API_KEYS.contains(apiKey); + boolean isApiKeyPresent(String sdkKey) { + return USED_API_KEYS.contains(sdkKey); } /** * Just for test - * @param apiKey + * @param sdkKey * @return */ @VisibleForTesting - int getCount(String apiKey) { - return USED_API_KEYS.count(apiKey); + int getCount(String sdkKey) { + return USED_API_KEYS.count(sdkKey); + } + + public Map getFactoryInstances() { + Map factoryInstances = new HashMap<>(); + for (String factory :USED_API_KEYS) { + factoryInstances.putIfAbsent(factory, new Long(getCount(factory))); + } + + return factoryInstances; + } + + public void clearApiKeys() { + USED_API_KEYS.clear(); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/CacheUpdaterService.java b/client/src/main/java/io/split/client/CacheUpdaterService.java index 6823fdef..63b42663 100644 --- a/client/src/main/java/io/split/client/CacheUpdaterService.java +++ b/client/src/main/java/io/split/client/CacheUpdaterService.java @@ -1,7 +1,6 @@ package io.split.client; import com.google.common.collect.Lists; -import io.split.cache.SplitCache; import io.split.client.dtos.ConditionType; import io.split.client.dtos.MatcherCombiner; import io.split.client.dtos.Partition; @@ -12,46 +11,56 @@ import io.split.engine.matchers.CombiningMatcher; import io.split.engine.matchers.strings.WhitelistMatcher; import io.split.grammar.Treatments; +import io.split.storages.SplitCacheProducer; import java.util.ArrayList; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Comparator; +import java.util.HashMap; import java.util.stream.Collectors; +import static com.google.common.base.Preconditions.checkNotNull; + public final class CacheUpdaterService { private static String LOCALHOST = "localhost"; - private SplitCache _splitCache; + private SplitCacheProducer _splitCacheProducer; - public CacheUpdaterService(SplitCache splitCache) { - _splitCache = splitCache; + public CacheUpdaterService(SplitCacheProducer splitCacheProducer) { + _splitCacheProducer = checkNotNull(splitCacheProducer); } public void updateCache(Map map) { - _splitCache.clear(); + _splitCacheProducer.clear(); + List parsedSplits = new ArrayList<>(); for (Map.Entry entrySplit : map.entrySet()) { SplitAndKey splitAndKey = entrySplit.getKey(); String splitName = splitAndKey.split(); String splitKey = splitAndKey.key(); LocalhostSplit localhostSplit = entrySplit.getValue(); - ParsedSplit split = _splitCache.get(splitName); - List conditions = getConditions(splitKey, split, localhostSplit.treatment); - String treatment = conditions.size() > 0 ? Treatments.CONTROL : localhostSplit.treatment; + Optional splitOptional = parsedSplits.stream().filter(ps -> ps.feature().equals(splitName)).findFirst(); + ParsedSplit split = splitOptional.orElse(null); Map configurations = new HashMap<>(); if(split != null && split.configurations().size() > 0) { configurations = split.configurations(); } + List conditions = getConditions(splitKey, split, localhostSplit.treatment); + String treatment = conditions.size() > 0 ? Treatments.CONTROL : localhostSplit.treatment; configurations.put(localhostSplit.treatment, localhostSplit.config); - split = new ParsedSplit(splitName, 0, false, treatment,conditions, LOCALHOST, 0, 100, 0, 0, configurations); - _splitCache.put(split); + split = new ParsedSplit(splitName, 0, false, treatment,conditions, LOCALHOST, 0, 100, 0, 0, configurations, new HashSet<>(), true, null); + parsedSplits.removeIf(parsedSplit -> parsedSplit.feature().equals(splitName)); + parsedSplits.add(split); } + _splitCacheProducer.putMany(parsedSplits); + _splitCacheProducer.setChangeNumber(_splitCacheProducer.getChangeNumber()); } private List getConditions(String splitKey, ParsedSplit split, String treatment){ - List conditions = split == null ? new ArrayList<>() : split.parsedConditions().stream().collect(Collectors.toList()); + List conditions = split == null ? new ArrayList<>() : new ArrayList<>(split.parsedConditions()); Partition partition = new Partition(); partition.treatment = treatment; partition.size = 100; diff --git a/client/src/main/java/io/split/client/CustomHeaderDecorator.java b/client/src/main/java/io/split/client/CustomHeaderDecorator.java new file mode 100644 index 00000000..934c4368 --- /dev/null +++ b/client/src/main/java/io/split/client/CustomHeaderDecorator.java @@ -0,0 +1,15 @@ +package io.split.client; + +import io.split.client.dtos.RequestContext; + +import java.util.Map; +import java.util.List; + +public interface CustomHeaderDecorator +{ + /** + * Get the additional headers needed for all http operations + * @return HashMap of addition headers + */ + Map> getHeaderOverrides(RequestContext context); +} diff --git a/client/src/main/java/io/split/client/EventClientImpl.java b/client/src/main/java/io/split/client/EventClientImpl.java deleted file mode 100644 index 5c3ae8c1..00000000 --- a/client/src/main/java/io/split/client/EventClientImpl.java +++ /dev/null @@ -1,238 +0,0 @@ -package io.split.client; - -import com.google.common.annotations.VisibleForTesting; -import io.split.client.dtos.Event; -import io.split.client.utils.GenericClientUtil; -import io.split.client.utils.Utils; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import static java.lang.Thread.MIN_PRIORITY; - -/** - * Responsible for sending events added via .track() to Split collection services - */ -public class EventClientImpl implements EventClient { - - public static final Long MAX_SIZE_BYTES = 5 * 1024 * 1024L; - - private final BlockingQueue _eventQueue; - private final int _maxQueueSize; - private final long _flushIntervalMillis; - - private final ExecutorService _senderExecutor; - private final ExecutorService _consumerExecutor; - - private final ScheduledExecutorService _flushScheduler; - - static final Event CENTINEL = new Event(); - private static final Logger _log = LoggerFactory.getLogger(EventClientImpl.class); - private final CloseableHttpClient _httpclient; - private final URI _target; - private final int _waitBeforeShutdown; - - ThreadFactory eventClientThreadFactory(final String name) { - return new ThreadFactory() { - @Override - public Thread newThread(final Runnable r) { - return new Thread(new Runnable() { - @Override - public void run() { - Thread.currentThread().setPriority(MIN_PRIORITY); - r.run(); - } - }, name); - } - }; - } - - - public static EventClientImpl create(CloseableHttpClient httpclient, URI eventsRootTarget, int maxQueueSize, long flushIntervalMillis, int waitBeforeShutdown) throws URISyntaxException { - return new EventClientImpl(new LinkedBlockingQueue(), - httpclient, - Utils.appendPath(eventsRootTarget, "api/events/bulk"), - maxQueueSize, - flushIntervalMillis, - waitBeforeShutdown); - } - - EventClientImpl(BlockingQueue eventQueue, CloseableHttpClient httpclient, URI target, int maxQueueSize, - long flushIntervalMillis, int waitBeforeShutdown) throws URISyntaxException { - - _httpclient = httpclient; - - _target = target; - - _eventQueue = eventQueue; - _waitBeforeShutdown = waitBeforeShutdown; - - _maxQueueSize = maxQueueSize; - _flushIntervalMillis = flushIntervalMillis; - - _senderExecutor = new ThreadPoolExecutor( - 1, - 1, - 0L, - TimeUnit.MILLISECONDS, - new LinkedBlockingQueue(50), - eventClientThreadFactory("eventclient-sender"), - new RejectedExecutionHandler() { - @Override - public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { - _log.warn("Executor queue full. Dropping events."); - } - }); - - _consumerExecutor = Executors.newSingleThreadExecutor(eventClientThreadFactory("eventclient-consumer")); - _consumerExecutor.submit(new Consumer()); - - _flushScheduler = Executors.newScheduledThreadPool(1, eventClientThreadFactory("eventclient-flush")); - _flushScheduler.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - flush(); - } - }, _flushIntervalMillis, _flushIntervalMillis, TimeUnit.MILLISECONDS); - } - - /** - * the existence of this message in the queue triggers a send event in the consumer thread. - */ - public void flush() { - track(CENTINEL, 0); - } // CENTINEL event won't be queued, so no size needed. - - public boolean track(Event event, int eventSize) { - try { - if (event == null) { - return false; - } - _eventQueue.put(new WrappedEvent(event, eventSize)); - - } catch (InterruptedException e) { - _log.warn("Interruption when adding event withed while adding message %s.", event); - return false; - } - return true; - } - - public void close() { - try { - _consumerExecutor.shutdownNow(); - _flushScheduler.shutdownNow(); - _senderExecutor.awaitTermination(_waitBeforeShutdown, TimeUnit.MILLISECONDS); - } catch (Exception e) { - _log.warn("Error when shutting down EventClientImpl", e); - } - } - - /** - * Infinite loop that listens to event from the event queue, dequeue them and send them over once: - * - a CENTINEL message has arrived, or - * - the queue reached a specific size - * - */ - class Consumer implements Runnable { - @Override - public void run() { - List events = new ArrayList<>(); - long accumulated = 0; - try { - while (true) { - WrappedEvent data = _eventQueue.take(); - Event event = data.event(); - Long size = data.size(); - - if (event != CENTINEL) { - events.add(event); - accumulated += size; - } else if (events.size() < 1) { - - if (_log.isDebugEnabled()) { - _log.debug("No messages to publish."); - } - - continue; - } - - if (events.size() >= _maxQueueSize || accumulated >= MAX_SIZE_BYTES || event == CENTINEL) { - - // Send over the network - if (_log.isDebugEnabled()) { - _log.debug(String.format("Sending %d events", events.size())); - } - - // Dispatch - _senderExecutor.submit(EventSenderTask.create(_httpclient, _target, events)); - - // Clear the queue of events for the next batch. - events = new ArrayList<>(); - accumulated = 0; - } - } - } catch (InterruptedException e) { - _log.debug("Consumer thread was interrupted. Exiting..."); - } - } - } - - static class WrappedEvent { - private final Event _event; - private final long _size; - - public WrappedEvent(Event event, long size) { - _event = event; - _size = size; - } - - public Event event() { - return _event; - } - - public long size() { - return _size; - } - } - - static class EventSenderTask implements Runnable { - - private final List _data; - private final URI _endpoint; - private final CloseableHttpClient _client; - - static EventSenderTask create(CloseableHttpClient httpclient, URI eventsTarget, List events) { - return new EventSenderTask(httpclient, eventsTarget, events); - } - - EventSenderTask(CloseableHttpClient httpclient, URI eventsTarget, List events) { - _client = httpclient; - _data = events; - _endpoint = eventsTarget; - } - - @Override - public void run() { - GenericClientUtil.process(_data, _endpoint, _client); - } - } - - @VisibleForTesting - URI getTarget() { - return _target ; - } -} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/HttpClientDynamicCredentials.java b/client/src/main/java/io/split/client/HttpClientDynamicCredentials.java new file mode 100644 index 00000000..ebcbe667 --- /dev/null +++ b/client/src/main/java/io/split/client/HttpClientDynamicCredentials.java @@ -0,0 +1,26 @@ +package io.split.client; + +import io.split.client.dtos.BearerCredentialsProvider; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.BearerToken; +import org.apache.hc.client5.http.auth.Credentials; +import org.apache.hc.core5.http.protocol.HttpContext; + +class HttpClientDynamicCredentials implements org.apache.hc.client5.http.auth.CredentialsProvider { + + private final BearerCredentialsProvider _bearerCredentialsProvider; + + public HttpClientDynamicCredentials (BearerCredentialsProvider bearerCredentialsProvider) { + _bearerCredentialsProvider = bearerCredentialsProvider; + } + + @Override + public Credentials getCredentials(AuthScope authScope, HttpContext context) { + + // This Provider is invoked every time a request is made. + // This should invoke a user-custom provider responsible for: + return new BearerToken(_bearerCredentialsProvider.getToken()); + } + +} + diff --git a/client/src/main/java/io/split/client/HttpSegmentChangeFetcher.java b/client/src/main/java/io/split/client/HttpSegmentChangeFetcher.java index 7d7d735f..bd365590 100644 --- a/client/src/main/java/io/split/client/HttpSegmentChangeFetcher.java +++ b/client/src/main/java/io/split/client/HttpSegmentChangeFetcher.java @@ -2,21 +2,23 @@ import com.google.common.annotations.VisibleForTesting; import io.split.client.dtos.SegmentChange; +import io.split.client.dtos.SplitHttpResponse; import io.split.client.utils.Json; import io.split.client.utils.Utils; -import io.split.engine.metrics.Metrics; +import io.split.engine.common.FetchOptions; import io.split.engine.segments.SegmentChangeFetcher; -import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; -import org.apache.hc.core5.http.io.entity.EntityUtils; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.net.URIBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; import static com.google.common.base.Preconditions.checkNotNull; @@ -27,77 +29,63 @@ public final class HttpSegmentChangeFetcher implements SegmentChangeFetcher { private static final Logger _log = LoggerFactory.getLogger(HttpSegmentChangeFetcher.class); private static final String SINCE = "since"; - private static final String PREFIX = "segmentChangeFetcher"; - private static final String NAME_CACHE = "Cache-Control"; - private static final String VALUE_CACHE = "no-cache"; + private static final String TILL = "till"; - private final CloseableHttpClient _client; + private final SplitHttpClient _client; private final URI _target; - private final Metrics _metrics; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; - public static HttpSegmentChangeFetcher create(CloseableHttpClient client, URI root) throws URISyntaxException { - return create(client, root, new Metrics.NoopMetrics()); + public static HttpSegmentChangeFetcher create(SplitHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer) + throws URISyntaxException { + return new HttpSegmentChangeFetcher(client, Utils.appendPath(root, "api/segmentChanges"), telemetryRuntimeProducer); } - public static HttpSegmentChangeFetcher create(CloseableHttpClient client, URI root, Metrics metrics) throws URISyntaxException { - return new HttpSegmentChangeFetcher(client, Utils.appendPath(root, "api/segmentChanges"), metrics); - } - - private HttpSegmentChangeFetcher(CloseableHttpClient client, URI uri, Metrics metrics) { + private HttpSegmentChangeFetcher(SplitHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer) { _client = client; _target = uri; - _metrics = metrics; checkNotNull(_target); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); } @Override - public SegmentChange fetch(String segmentName, long since, boolean addCacheHeader) { + public SegmentChange fetch(String segmentName, long since, FetchOptions options) { long start = System.currentTimeMillis(); - CloseableHttpResponse response = null; - try { String path = _target.getPath() + "/" + segmentName; - URI uri = new URIBuilder(_target).setPath(path).addParameter(SINCE, "" + since).build(); - HttpGet request = new HttpGet(uri); - if(addCacheHeader) { - request.setHeader(NAME_CACHE, VALUE_CACHE); - } - response = _client.execute(request); - - int statusCode = response.getCode(); - - if (statusCode < 200 || statusCode >= 300) { - _log.error("Response status was: " + statusCode); - if (statusCode == 403) { - _log.error("factory instantiation: you passed a browser type api_key, " + - "please grab an api key from the Split console that is of type sdk"); - } - _metrics.count(PREFIX + ".status." + statusCode, 1); - throw new IllegalStateException("Could not retrieve segment changes for " + segmentName + "; http return code " + statusCode); + URIBuilder uriBuilder = new URIBuilder(_target) + .setPath(path) + .addParameter(SINCE, "" + since); + if (options.hasCustomCN()) { + uriBuilder.addParameter(TILL, "" + options.targetCN()); } + URI uri = uriBuilder.build(); + SplitHttpResponse response = _client.get(uri, options, null); - String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); - if (_log.isDebugEnabled()) { - _log.debug("Received json: " + json); + if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + _telemetryRuntimeProducer.recordSyncError(ResourceEnum.SEGMENT_SYNC, response.statusCode()); + if (response.statusCode() == HttpStatus.SC_FORBIDDEN) { + _log.error("factory instantiation: you passed a client side type sdkKey, " + + "please grab an sdk key from the Split user interface that is of type server side"); + } + throw new IllegalStateException(String.format("Could not retrieve segment changes for %s, since %s; http return code %s", + segmentName, since, response.statusCode())); } + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.SEGMENTS, System.currentTimeMillis()); - return Json.fromJson(json, SegmentChange.class); - } catch (Throwable t) { - _metrics.count(PREFIX + ".exception", 1); - throw new IllegalStateException("Problem fetching segmentChanges: " + t.getMessage(), t); + return Json.fromJson(response.body(), SegmentChange.class); + } catch (Exception e) { + throw new IllegalStateException(String.format("Error occurred when trying to sync segment: %s, since: %s. Details: %s", + segmentName, since, e), e); } finally { - Utils.forceClose(response); - _metrics.time(PREFIX + ".time", System.currentTimeMillis() - start); + _telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SEGMENTS, System.currentTimeMillis()-start); } - - } @VisibleForTesting URI getTarget() { return _target; } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/HttpSplitChangeFetcher.java b/client/src/main/java/io/split/client/HttpSplitChangeFetcher.java index 3c5f9b8f..49eb66a9 100644 --- a/client/src/main/java/io/split/client/HttpSplitChangeFetcher.java +++ b/client/src/main/java/io/split/client/HttpSplitChangeFetcher.java @@ -1,24 +1,31 @@ package io.split.client; import com.google.common.annotations.VisibleForTesting; + +import io.split.Spec; import io.split.client.dtos.SplitChange; +import io.split.client.dtos.SplitHttpResponse; +import io.split.client.dtos.SplitChangesOldPayloadDto; +import io.split.client.exceptions.UriTooLongException; import io.split.client.utils.Json; import io.split.client.utils.Utils; +import io.split.engine.common.FetchOptions; import io.split.engine.experiments.SplitChangeFetcher; -import io.split.engine.metrics.Metrics; -import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; -import org.apache.hc.core5.http.io.entity.EntityUtils; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.net.URIBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; import java.net.URISyntaxException; -import java.nio.charset.StandardCharsets; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.Spec.SPEC_1_3; +import static io.split.Spec.SPEC_1_1; /** * Created by adilaijaz on 5/30/15. @@ -27,70 +34,102 @@ public final class HttpSplitChangeFetcher implements SplitChangeFetcher { private static final Logger _log = LoggerFactory.getLogger(HttpSplitChangeFetcher.class); private static final String SINCE = "since"; - private static final String PREFIX = "splitChangeFetcher"; - private static final String NAME_CACHE = "Cache-Control"; - private static final String VALUE_CACHE = "no-cache"; - - private final CloseableHttpClient _client; + private static final String RB_SINCE = "rbSince"; + private static final String TILL = "till"; + private static final String SETS = "sets"; + private static final String SPEC = "s"; + private String specVersion = SPEC_1_3; + private int PROXY_CHECK_INTERVAL_MILLISECONDS_SS = 24 * 60 * 60 * 1000; + private Long _lastProxyCheckTimestamp = 0L; + private final SplitHttpClient _client; private final URI _target; - private final Metrics _metrics; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final boolean _rootURIOverriden; - public static HttpSplitChangeFetcher create(CloseableHttpClient client, URI root) throws URISyntaxException { - return create(client, root, new Metrics.NoopMetrics()); + public static HttpSplitChangeFetcher create(SplitHttpClient client, URI root, TelemetryRuntimeProducer telemetryRuntimeProducer, + boolean rootURIOverriden) + throws URISyntaxException { + return new HttpSplitChangeFetcher(client, Utils.appendPath(root, "api/splitChanges"), telemetryRuntimeProducer, rootURIOverriden); } - public static HttpSplitChangeFetcher create(CloseableHttpClient client, URI root, Metrics metrics) throws URISyntaxException { - return new HttpSplitChangeFetcher(client, Utils.appendPath(root, "api/splitChanges"), metrics); - } - - private HttpSplitChangeFetcher(CloseableHttpClient client, URI uri, Metrics metrics) { + private HttpSplitChangeFetcher(SplitHttpClient client, URI uri, TelemetryRuntimeProducer telemetryRuntimeProducer, boolean rootURIOverriden) { _client = client; _target = uri; - _metrics = metrics; checkNotNull(_target); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _rootURIOverriden = rootURIOverriden; } - @Override - public SplitChange fetch(long since, boolean addCacheHeader) { - - long start = System.currentTimeMillis(); + long makeRandomTill() { - CloseableHttpResponse response = null; + return (-1)*(long)Math.floor(Math.random()*(Math.pow(2, 63))); + } + @Override + public SplitChange fetch(long since, long sinceRBS, FetchOptions options) { + long start = System.currentTimeMillis(); try { - URI uri = new URIBuilder(_target).addParameter(SINCE, "" + since).build(); - - HttpGet request = new HttpGet(uri); - if(addCacheHeader) { - request.setHeader(NAME_CACHE, VALUE_CACHE); + URI uri = buildURL(options, since, sinceRBS); + if (specVersion.equals(SPEC_1_1) && (System.currentTimeMillis() - _lastProxyCheckTimestamp >= PROXY_CHECK_INTERVAL_MILLISECONDS_SS)) { + _log.info("Switching to new Feature flag spec ({}) and fetching.", SPEC_1_3); + specVersion = SPEC_1_3; + uri = buildURL(options, -1,-1); } - response = _client.execute(request); - - int statusCode = response.getCode(); - if (statusCode < 200 || statusCode >= 300) { - _metrics.count(PREFIX + ".status." + statusCode, 1); - throw new IllegalStateException("Could not retrieve splitChanges; http return code " + statusCode); + SplitHttpResponse response = _client.get(uri, options, null); + if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + if (response.statusCode() == HttpStatus.SC_REQUEST_URI_TOO_LONG) { + _log.error("The amount of flag sets provided are big causing uri length error."); + throw new UriTooLongException(String.format("Status code: %s. Message: %s", response.statusCode(), response.statusMessage())); + } + + if (response.statusCode() == HttpStatus.SC_BAD_REQUEST && specVersion.equals(Spec.SPEC_1_3) && _rootURIOverriden) { + specVersion = Spec.SPEC_1_1; + _log.warn("Detected proxy without support for Feature flags spec {} version, will switch to spec version {}", + SPEC_1_3, SPEC_1_1); + _lastProxyCheckTimestamp = System.currentTimeMillis(); + return fetch(since, sinceRBS, options); + } + + _telemetryRuntimeProducer.recordSyncError(ResourceEnum.SPLIT_SYNC, response.statusCode()); + throw new IllegalStateException( + String.format("Could not retrieve splitChanges since %s; http return code %s", since, response.statusCode()) + ); } - - String json = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); - if (_log.isDebugEnabled()) { - _log.debug("Received json: " + json); + if (specVersion.equals(Spec.SPEC_1_1)) { + return Json.fromJson(response.body(), SplitChangesOldPayloadDto.class).toSplitChange(); } - return Json.fromJson(json, SplitChange.class); - } catch (Throwable t) { - _metrics.count(PREFIX + ".exception", 1); - throw new IllegalStateException("Problem fetching splitChanges: " + t.getMessage(), t); + SplitChange splitChange = Json.fromJson(response.body(), SplitChange.class); + splitChange.clearCache = _lastProxyCheckTimestamp != 0; + _lastProxyCheckTimestamp = 0L; + return splitChange; + } catch (Exception e) { + throw new IllegalStateException(String.format("Problem fetching splitChanges since %s: %s", since, e), e); } finally { - Utils.forceClose(response); - _metrics.time(PREFIX + ".time", System.currentTimeMillis() - start); + _telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.SPLITS, System.currentTimeMillis() - start); + } + } + + + private URI buildURL(FetchOptions options, long since, long sinceRBS) throws URISyntaxException { + URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + specVersion); + uriBuilder.addParameter(SINCE, "" + since); + if (specVersion.equals(SPEC_1_3)) { + uriBuilder.addParameter(RB_SINCE, "" + sinceRBS); + } + if (!options.flagSetsFilter().isEmpty()) { + uriBuilder.addParameter(SETS, "" + options.flagSetsFilter()); + } + if (options.hasCustomCN()) { + uriBuilder.addParameter(TILL, "" + options.targetCN()); } + return uriBuilder.build(); } @VisibleForTesting URI getTarget() { return _target; } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java new file mode 100644 index 00000000..03530d09 --- /dev/null +++ b/client/src/main/java/io/split/client/JsonLocalhostSplitChangeFetcher.java @@ -0,0 +1,90 @@ +package io.split.client; + +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonReader; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.SplitChangesOldPayloadDto; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.Json; +import io.split.client.utils.LocalhostSanitizer; +import io.split.engine.common.FetchOptions; +import io.split.engine.experiments.SplitChangeFetcher; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +import static io.split.client.utils.Utils.checkExitConditions; + +public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher { + + private static final Logger _log = LoggerFactory.getLogger(JsonLocalhostSplitChangeFetcher.class); + private final InputStreamProvider _inputStreamProvider; + private byte [] lastHashFeatureFlags; + private byte [] lastHashRuleBasedSegments; + + public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider) { + _inputStreamProvider = inputStreamProvider; + lastHashFeatureFlags = new byte[0]; + lastHashRuleBasedSegments = new byte[0]; + } + + @Override + public SplitChange fetch(long since, long sinceRBS, FetchOptions options) { + try { + JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8))); + if (checkOldSpec(new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8))))) { + return Json.fromJson(jsonReader, SplitChangesOldPayloadDto.class).toSplitChange(); + } + SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class); + return processSplitChange(splitChange, since, sinceRBS); + } catch (Exception e) { + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } + + private boolean checkOldSpec(JsonReader jsonReader) { + return Json.fromJson(jsonReader, JsonObject.class).has("splits"); + } + + private SplitChange processSplitChange(SplitChange splitChange, long changeNumber, long changeNumberRBS) throws NoSuchAlgorithmException { + SplitChange splitChangeToProcess = LocalhostSanitizer.sanitization(splitChange); + // if the till is less than storage CN and different from the default till ignore the change + if (checkExitConditions(splitChangeToProcess.featureFlags, changeNumber) || + checkExitConditions(splitChangeToProcess.ruleBasedSegments, changeNumberRBS)) { + _log.warn("The till is lower than the change number or different to -1"); + return null; + } + + byte [] currHashFeatureFlags = getStringDigest(splitChange.featureFlags.d.toString()); + byte [] currHashRuleBasedSegments = getStringDigest(splitChange.ruleBasedSegments.d.toString()); + //if sha exist and is equal to before sha, or if till is equal to default till returns the same segmentChange with till equals to storage CN + if (Arrays.equals(lastHashFeatureFlags, currHashFeatureFlags) || splitChangeToProcess.featureFlags.t == -1) { + splitChangeToProcess.featureFlags.t = changeNumber; + } + if (Arrays.equals(lastHashRuleBasedSegments, currHashRuleBasedSegments) || splitChangeToProcess.ruleBasedSegments.t == -1) { + splitChangeToProcess.ruleBasedSegments.t = changeNumberRBS; + } + + lastHashFeatureFlags = currHashFeatureFlags; + lastHashRuleBasedSegments = currHashRuleBasedSegments; + splitChangeToProcess.featureFlags.s = changeNumber; + splitChangeToProcess.ruleBasedSegments.s = changeNumberRBS; + + return splitChangeToProcess; + } + + private byte[] getStringDigest(String json) throws NoSuchAlgorithmException { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.reset(); + digest.update(json.getBytes()); + // calculate the json sha + return digest.digest(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java new file mode 100644 index 00000000..c67055ec --- /dev/null +++ b/client/src/main/java/io/split/client/LegacyLocalhostSplitChangeFetcher.java @@ -0,0 +1,111 @@ +package io.split.client; + +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.Status; +import io.split.client.dtos.ChangeDto; +import io.split.client.utils.LocalhostConstants; +import io.split.client.utils.LocalhostSanitizer; +import io.split.engine.common.FetchOptions; +import io.split.engine.experiments.SplitChangeFetcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Optional; + +public class LegacyLocalhostSplitChangeFetcher implements SplitChangeFetcher { + + private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class); + static final String FILENAME = ".split"; + private final File _splitFile; + + public LegacyLocalhostSplitChangeFetcher(String directory) { + if (directory == null || directory.isEmpty()){ + directory = System.getProperty("user.home"); + } + _splitFile = new File(directory, FILENAME); + } + + @Override + public SplitChange fetch(long since, long sinceRBS, FetchOptions options) { + + try (BufferedReader reader = new BufferedReader(new FileReader(_splitFile))) { + SplitChange splitChange = new SplitChange(); + splitChange.featureFlags = new ChangeDto<>(); + splitChange.featureFlags.d = new ArrayList<>(); + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + String lineTrim = line.trim(); + if (lineTrim.isEmpty() || lineTrim.startsWith("#")) { + continue; + } + + String[] featureTreatment = lineTrim.split("\\s+"); + + if (featureTreatment.length < 2 || featureTreatment.length > 3) { + _log.info("Ignoring line since it does not have 2 or 3 columns: " + lineTrim); + continue; + } + Optional splitOptional = splitChange.featureFlags.d.stream(). + filter(split -> split.name.equals(featureTreatment[0])).findFirst(); + Split split = splitOptional.orElse(null); + if(split == null) { + split = new Split(); + split.name = featureTreatment[0]; + split.configurations = new HashMap<>(); + split.conditions = new ArrayList<>(); + } else { + splitChange.featureFlags.d.remove(split); + } + split.status = Status.ACTIVE; + split.defaultTreatment = featureTreatment[1]; + split.trafficTypeName = LocalhostConstants.USER; + split.trafficAllocation = LocalhostConstants.SIZE_100; + split.trafficAllocationSeed = LocalhostConstants.SIZE_1; + + Condition condition = checkCondition(featureTreatment); + if(condition.conditionType != ConditionType.ROLLOUT){ + split.conditions.add(0, condition); + } else { + split.conditions.add(condition); + } + splitChange.featureFlags.d.add(split); + } + splitChange.featureFlags.t = since; + splitChange.featureFlags.s = since; + splitChange.ruleBasedSegments = new ChangeDto<>(); + splitChange.ruleBasedSegments.s = -1; + splitChange.ruleBasedSegments.t = -1; + splitChange.ruleBasedSegments.d = new ArrayList<>(); + return splitChange; + } catch (FileNotFoundException f) { + _log.warn("There was no file named " + _splitFile.getPath() + " found. " + + "We created a split client that returns default treatments for all feature flags for all of your users. " + + "If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " + + "treatment name separated by whitespace in " + _splitFile.getPath() + + "; one pair per line. Empty lines or lines starting with '#' are considered comments", f); + throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f); + } catch (Exception e) { + _log.warn(String.format("Problem to fetch split change using the file %s", + _splitFile.getPath()), e); + throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e); + } + } + + private Condition checkCondition(String[] featureTreatment) { + Condition condition; + if (featureTreatment.length == 2) { + condition = LocalhostSanitizer.createCondition(null, featureTreatment[1]); + } else { + condition = LocalhostSanitizer.createCondition(featureTreatment[2], featureTreatment[1]); + } + return condition; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/LegacyLocalhostSplitFile.java b/client/src/main/java/io/split/client/LegacyLocalhostSplitFile.java deleted file mode 100644 index 72006418..00000000 --- a/client/src/main/java/io/split/client/LegacyLocalhostSplitFile.java +++ /dev/null @@ -1,57 +0,0 @@ -package io.split.client; - -import com.google.common.collect.Maps; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.Map; - -public class LegacyLocalhostSplitFile extends AbstractLocalhostSplitFile { - private static final Logger _log = LoggerFactory.getLogger(LegacyLocalhostSplitFile.class); - - public LegacyLocalhostSplitFile(LocalhostSplitFactory splitFactory, String directory, String fileName) throws IOException { - super(splitFactory, directory, fileName); - } - - - public Map readOnSplits() throws IOException { - Map onSplits = Maps.newHashMap(); - - try (BufferedReader reader = new BufferedReader(new FileReader(_file))) { - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - line = line.trim(); - if (line.isEmpty() || line.startsWith("#")) { - continue; - } - - String[] feature_treatment = line.split("\\s+"); - - if (feature_treatment.length < 2 || feature_treatment.length > 3) { - _log.info("Ignoring line since it does not have 2 or 3 columns: " + line); - continue; - } - - SplitAndKey splitAndKey = null; - if (feature_treatment.length == 2) { - splitAndKey = SplitAndKey.of(feature_treatment[0]); - } else { - splitAndKey = SplitAndKey.of(feature_treatment[0], feature_treatment[2]); - } - - onSplits.put(splitAndKey, new LocalhostSplit(feature_treatment[1], null)); - } - } catch (FileNotFoundException e) { - _log.warn("There was no file named " + _file.getPath() + " found. " + - "We created a split client that returns default treatments for all features for all of your users. " + - "If you wish to return a specific treatment for a feature, enter the name of that feature name and " + - "treatment name separated by whitespace in " + _file.getPath() + - "; one pair per line. Empty lines or lines starting with '#' are considered comments", e); - } - - return onSplits; - } -} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/LocalhostSegmentChangeFetcher.java b/client/src/main/java/io/split/client/LocalhostSegmentChangeFetcher.java new file mode 100644 index 00000000..d3c57506 --- /dev/null +++ b/client/src/main/java/io/split/client/LocalhostSegmentChangeFetcher.java @@ -0,0 +1,73 @@ +package io.split.client; + +import com.google.gson.stream.JsonReader; +import io.split.client.dtos.SegmentChange; +import io.split.client.utils.Json; +import io.split.client.utils.LocalhostSanitizer; +import io.split.engine.common.FetchOptions; +import io.split.engine.segments.SegmentChangeFetcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class LocalhostSegmentChangeFetcher implements SegmentChangeFetcher { + + private static final Logger _log = LoggerFactory.getLogger(LocalhostSegmentChangeFetcher.class); + private final File _file; + + private Map lastHash; + + public LocalhostSegmentChangeFetcher(String filePath){ + _file = new File(filePath); + lastHash = new HashMap(); + } + + @Override + public SegmentChange fetch(String segmentName, long changesSinceThisChangeNumber, FetchOptions options) { + try { + JsonReader jsonReader = new JsonReader(new FileReader(String.format("%s/%s.json", _file, segmentName))); + SegmentChange segmentChange = Json.fromJson(jsonReader, SegmentChange.class); + return processSegmentChange(segmentName, changesSinceThisChangeNumber, segmentChange); + } catch (FileNotFoundException f){ + _log.warn(String.format("There was no file named %s/%s found.", _file.getPath(), segmentName), f); + throw new IllegalStateException(String.format("Problem fetching segment %s: %s", segmentName, f.getMessage()), f); + } catch (Exception e) { + _log.warn(String.format("Problem to fetch segment change for the segment %s in the directory %s.", segmentName, _file.getPath()), e); + throw new IllegalStateException(String.format("Problem fetching segment %s: %s", segmentName, e.getMessage()), e); + } + } + + private SegmentChange processSegmentChange(String segmentName, long changeNumber, SegmentChange segmentChange) throws NoSuchAlgorithmException { + SegmentChange segmentChangeToProcess = LocalhostSanitizer.sanitization(segmentChange); + if (segmentChangeToProcess == null){ + return null; + } + // if the till is less than storage CN and different from the default till ignore the change + if (segmentChangeToProcess.till < changeNumber && segmentChangeToProcess.till != -1){ + _log.warn("The segmentChange till is lower than the change number or different to -1"); + return null; + } + String toHash = segmentChangeToProcess.added.toString() + segmentChangeToProcess.removed.toString(); + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.reset(); + digest.update(toHash.getBytes()); + // calculate the json sha + byte [] currHash = digest.digest(); + //if sha exist and is equal to before sha, or if till is equal to default till returns the same segmentChange with till equals to storage CN + if ((lastHash.containsKey(segmentName) && Arrays.equals((byte[]) lastHash.get(segmentName), currHash)) || + segmentChangeToProcess.till == -1) { + segmentChangeToProcess.till = changeNumber; + } + lastHash.put(segmentName, currHash); + segmentChangeToProcess.since = changeNumber; + return segmentChangeToProcess; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/LocalhostSegmentFetcherNoop.java b/client/src/main/java/io/split/client/LocalhostSegmentFetcherNoop.java new file mode 100644 index 00000000..03285c49 --- /dev/null +++ b/client/src/main/java/io/split/client/LocalhostSegmentFetcherNoop.java @@ -0,0 +1,13 @@ +package io.split.client; + +import io.split.client.dtos.SegmentChange; +import io.split.engine.common.FetchOptions; +import io.split.engine.segments.SegmentChangeFetcher; + +public class LocalhostSegmentFetcherNoop implements SegmentChangeFetcher { + + @Override + public SegmentChange fetch(String segmentName, long changesSinceThisChangeNumber, FetchOptions options) { + return new SegmentChange(); + } +} diff --git a/client/src/main/java/io/split/client/LocalhostSplitFactory.java b/client/src/main/java/io/split/client/LocalhostSplitFactory.java deleted file mode 100644 index 0ec01f8c..00000000 --- a/client/src/main/java/io/split/client/LocalhostSplitFactory.java +++ /dev/null @@ -1,94 +0,0 @@ -package io.split.client; - -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SplitCache; -import io.split.client.impressions.ImpressionsManager; -import io.split.engine.SDKReadinessGates; -import io.split.engine.evaluator.EvaluatorImp; -import io.split.engine.metrics.Metrics; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.util.Map; - -/** - * An implementation of SplitClient that considers all partitions - * passed in the constructor to be 100% on for all users, and - * any other split to be 100% off for all users. This implementation - * is useful for using Split in localhost environment. - * - * The startup order is as follows: - * - Split will use config.splitFile (full path) if set and will look for a yaml (new) format. - * - otherwise Split will look for $user.home/.split file if it exists (for backward compatibility with older versions) - * - */ -public final class LocalhostSplitFactory implements SplitFactory { - private static final Logger _log = LoggerFactory.getLogger(LocalhostSplitFactory.class); - - static final String FILENAME = ".split"; - static final String LOCALHOST = "localhost"; - - private final SplitClient _client; - private final LocalhostSplitManager _manager; - private final AbstractLocalhostSplitFile _splitFile; - private final CacheUpdaterService _cacheUpdaterService; - - public static LocalhostSplitFactory createLocalhostSplitFactory(SplitClientConfig config) throws IOException { - String directory = System.getProperty("user.home"); - return new LocalhostSplitFactory(directory, config.splitFile()); - } - - public LocalhostSplitFactory(String directory, String file) throws IOException { - - if (file != null && !file.isEmpty() && (file.endsWith(".yaml") || file.endsWith(".yml"))) { - _splitFile = new YamlLocalhostSplitFile(this, "", file); - _log.info("Starting Split in localhost mode with file at " + _splitFile._file.getAbsolutePath()); - } else { - _splitFile = new LegacyLocalhostSplitFile(this, directory, FILENAME); - _log.warn("(Deprecated) Starting Split in localhost mode using legacy file located at " + _splitFile._file.getAbsolutePath() - + "\nPlease set SplitClientConfig.builder().splitFile(...) to point to the new split.yaml location."); - } - - Map splitAndKeyToTreatment = _splitFile.readOnSplits(); - SplitCache splitCache = new InMemoryCacheImp(); - SDKReadinessGates sdkReadinessGates = new SDKReadinessGates(); - - sdkReadinessGates.splitsAreReady(); - _cacheUpdaterService = new CacheUpdaterService(splitCache); - _cacheUpdaterService.updateCache(splitAndKeyToTreatment); - _client = new SplitClientImpl(this, splitCache, - new ImpressionsManager.NoOpImpressionsManager(), new Metrics.NoopMetrics(), new NoopEventClient(), - SplitClientConfig.builder().setBlockUntilReadyTimeout(1).build(), sdkReadinessGates, new EvaluatorImp(splitCache)); - _manager = LocalhostSplitManager.of(splitAndKeyToTreatment); - - _splitFile.registerWatcher(); - _splitFile.setDaemon(true); - _splitFile.start(); - } - - @Override - public SplitClient client() { - return _client; - } - - @Override - public SplitManager manager() { - return _manager; - } - - @Override - public void destroy() { - _splitFile.stopThread(); - } - - @Override - public boolean isDestroyed() { - return _splitFile.isStopped(); - } - - public void updateFeatureToTreatmentMap(Map featureToTreatmentMap) { - _cacheUpdaterService.updateCache(featureToTreatmentMap); - _manager.updateFeatureToTreatmentMap(featureToTreatmentMap); - } -} diff --git a/client/src/main/java/io/split/client/LocalhostSplitManager.java b/client/src/main/java/io/split/client/LocalhostSplitManager.java index dc72ea4d..f117338d 100644 --- a/client/src/main/java/io/split/client/LocalhostSplitManager.java +++ b/client/src/main/java/io/split/client/LocalhostSplitManager.java @@ -76,12 +76,12 @@ public void blockUntilReady() throws TimeoutException, InterruptedException { } @Override - public SplitView split(String featureName) { - if (!_splitToTreatmentsMap.containsKey(featureName)) { + public SplitView split(String featureFlagName) { + if (!_splitToTreatmentsMap.containsKey(featureFlagName)) { return null; } - return toSplitView(featureName, _splitToTreatmentsMap.get(featureName)); + return toSplitView(featureFlagName, _splitToTreatmentsMap.get(featureFlagName)); } void updateFeatureToTreatmentMap(Map featureToTreatmentMap) { @@ -91,9 +91,9 @@ void updateFeatureToTreatmentMap(Map featureToTreat } - private SplitView toSplitView(String featureName, Set treatments) { + private SplitView toSplitView(String featureFlagName, Set treatments) { SplitView view = new SplitView(); - view.name = featureName; + view.name = featureFlagName; view.killed = false; view.trafficType = null; view.changeNumber = 0; diff --git a/client/src/main/java/io/split/client/NoOpHeaderDecorator.java b/client/src/main/java/io/split/client/NoOpHeaderDecorator.java new file mode 100644 index 00000000..8ce04fdb --- /dev/null +++ b/client/src/main/java/io/split/client/NoOpHeaderDecorator.java @@ -0,0 +1,18 @@ +package io.split.client; + +import io.split.client.CustomHeaderDecorator; +import io.split.client.dtos.RequestContext; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class NoOpHeaderDecorator implements CustomHeaderDecorator { + public NoOpHeaderDecorator() { + } + + @Override + public Map> getHeaderOverrides(RequestContext context) { + return new HashMap<>(); + } +} diff --git a/client/src/main/java/io/split/client/NoopEventClient.java b/client/src/main/java/io/split/client/NoopEventClient.java deleted file mode 100644 index 7976656c..00000000 --- a/client/src/main/java/io/split/client/NoopEventClient.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.split.client; - -import io.split.client.dtos.Event; - -public class NoopEventClient implements EventClient { - - @Override - public boolean track(Event event, int eventSize) { - return true; - } - - @Override - public void close() { - // Nothing to close - } - - public static EventClient create() { - return new NoopEventClient(); - } -} diff --git a/client/src/main/java/io/split/client/RequestDecorator.java b/client/src/main/java/io/split/client/RequestDecorator.java new file mode 100644 index 00000000..33059e61 --- /dev/null +++ b/client/src/main/java/io/split/client/RequestDecorator.java @@ -0,0 +1,47 @@ +package io.split.client; + +import io.split.client.dtos.RequestContext; + + +import java.util.HashSet; +import java.util.Map; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +public final class RequestDecorator { + CustomHeaderDecorator _headerDecorator; + + private static final Set forbiddenHeaders = new HashSet<>(Arrays.asList( + "splitsdkversion", + "splitmachineip", + "splitmachinename", + "splitimpressionsmode", + "host", + "referrer", + "content-type", + "content-length", + "content-encoding", + "accept", + "keep-alive", + "x-fastly-debug")); + + public RequestDecorator(CustomHeaderDecorator headerDecorator) { + _headerDecorator = (headerDecorator == null) + ? new NoOpHeaderDecorator() + : headerDecorator; + } + + public RequestContext decorateHeaders(RequestContext request) { + try { + return new RequestContext(_headerDecorator.getHeaderOverrides(request) + .entrySet() + .stream() + .filter(e -> !forbiddenHeaders.contains(e.getKey().toLowerCase())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + } catch (Exception e) { + throw new IllegalArgumentException( + String.format("Problem adding custom headers to request decorator: %s", e), e); + } + } +} diff --git a/client/src/main/java/io/split/client/SplitClient.java b/client/src/main/java/io/split/client/SplitClient.java index 88436bce..22838078 100644 --- a/client/src/main/java/io/split/client/SplitClient.java +++ b/client/src/main/java/io/split/client/SplitClient.java @@ -1,8 +1,12 @@ package io.split.client; import io.split.client.api.Key; +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventListener; import io.split.client.api.SplitResult; +import io.split.client.dtos.EvaluationOptions; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; @@ -12,36 +16,36 @@ public interface SplitClient { /** - * Returns the treatment to show this key for this feature. The set of treatments - * for a feature can be configured on the Split web console. + * Returns the treatment to show this key for this feature flag. The set of treatments + * for a feature flag can be configured on the Split user interface. *

*

* This method returns the string 'control' if: *

    *
  1. Any of the parameters were null
  2. *
  3. There was an exception in evaluating the treatment
  4. - *
  5. The SDK does not know of the existence of this feature
  6. - *
  7. The feature was deleted through the web console.
  8. + *
  9. The SDK does not know of the existence of this feature flag
  10. + *
  11. The feature flag was deleted through the Split user interface.
  12. *
* 'control' is a reserved treatment (you cannot create a treatment with the * same name) to highlight these exceptional circumstances. *

*

- * The sdk returns the default treatment of this feature if: + * The sdk returns the default treatment of this feature flag if: *

    - *
  1. The feature was killed
  2. - *
  3. The key did not match any of the conditions in the feature roll-out plan
  4. + *
  5. The feature flag was killed
  6. + *
  7. The key did not match any of the conditions in the feature flag roll-out plan
  8. *
- * The default treatment of a feature is set on the Split web console. + * The default treatment of a feature flag is set on the Split user interface. *

*

* This method does not throw any exceptions. It also never returns null. * - * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. - * @param split the feature we want to evaluate. MUST NOT be null. - * @return the evaluated treatment, the default treatment of this feature, or 'control'. + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. */ - String getTreatment(String key, String split); + String getTreatment(String key, String featureFlagName); /** * This method is useful when you want to determine the treatment to show @@ -53,18 +57,18 @@ public interface SplitClient { * vs. premium plan. Another example is to show a different treatment * to users created after a certain date. * - * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. - * @param split the feature we want to evaluate. MUST NOT be null. + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. - * @return the evaluated treatment, the default treatment of this feature, or 'control'. + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. */ - String getTreatment(String key, String split, Map attributes); + String getTreatment(String key, String featureFlagName, Map attributes); /** - * To understand why this method is useful, consider the following simple Split as an example: + * To understand why this method is useful, consider the following simple Feature Flag as an example: * - * if user is in segment employees then split 100%:on - * else if user is in segment all then split 20%:on,80%:off + * if user is in segment employees then feature flag 100%:on + * else if user is in segment all then feature flag 20%:on,80%:off * * There are two concepts here: matching and bucketing. Matching * refers to ‘user is in segment employees’ or ‘user is in segment @@ -86,59 +90,862 @@ public interface SplitClient { * * * @param key the matching and bucketing keys. MUST NOT be null. - * @param split the feature we want to evaluate. MUST NOT be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. * - * @return the evaluated treatment, the default treatment of this feature, or 'control'. + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. */ - String getTreatment(Key key, String split, Map attributes); + String getTreatment(Key key, String featureFlagName, Map attributes); /** * Same as {@link #getTreatment(String, String)} but it returns the configuration associated to the - * matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null. + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. *

*

* Examples include showing a different treatment to users on trial plan * vs. premium plan. Another example is to show a different treatment * to users created after a certain date. * - * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. - * @param split the feature we want to evaluate. MUST NOT be null. - * @return SplitResult containing the evaluated treatment (the default treatment of this feature, or 'control') and + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and * a configuration associated to this treatment if set. */ - SplitResult getTreatmentWithConfig(String key, String split); + SplitResult getTreatmentWithConfig(String key, String featureFlagName); /** * Same as {@link #getTreatment(String, String, Map)} but it returns the configuration associated to the - * matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null. + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. *

*

* Examples include showing a different treatment to users on trial plan * vs. premium plan. Another example is to show a different treatment * to users created after a certain date. * - * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. - * @param split the feature we want to evaluate. MUST NOT be null. + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. - * @return SplitResult containing the evaluated treatment (the default treatment of this feature, or 'control') and + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and * a configuration associated to this treatment if set. */ - SplitResult getTreatmentWithConfig(String key, String split, Map attributes); + SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes); /** * Same as {@link #getTreatment(Key, String, Map)} but it returns the configuration associated to the - * matching treatment if any. Otherwise {@link SplitResult.configurations()} will be null. + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. * * @param key the matching and bucketing keys. MUST NOT be null. - * @param split the feature we want to evaluate. MUST NOT be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. * - * @return SplitResult containing the evaluated treatment (the default treatment of this feature, or 'control') and + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and * a configuration associated to this treatment if set. */ - SplitResult getTreatmentWithConfig(Key key, String split, Map attributes); + SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes); + /** + * Returns a map of feature flag name and treatments to show this key for these feature flags. The set of treatments + * for a feature flag can be configured on the Split user interface. + *

+ *

+ * This method returns for each feature flag the string 'control' if: + *

    + *
  1. Any of the parameters were null
  2. + *
  3. There was an exception in evaluating the treatment
  4. + *
  5. The SDK does not know of the existence of this feature flag
  6. + *
  7. The feature flag was deleted through the Split user interface.
  8. + *
+ * 'control' is a reserved treatment (you cannot create a treatment with the + * same name) to highlight these exceptional circumstances. + *

+ *

+ * The sdk returns for each feature flag the default treatment of this feature flag if: + *

    + *
  1. The feature flag was killed
  2. + *
  3. The key did not match any of the conditions in the feature flag roll-out plan
  4. + *
+ * The default treatment of a feature flag is set on the Split user interface. + *

+ *

+ * This method does not throw any exceptions. It also never returns null. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @return for each feature flag the evaluated treatment, the default treatment for each feature flag, or 'control'. + */ + Map getTreatments(String key, List featureFlagNames); + + /** + * This method is useful when you want to determine the treatments to show + * to a customer (user, account etc.) based on an attribute of that customer + * instead of their key. + *

+ *

+ * Examples include showing different treatments to users on trial plan + * vs. premium plan. Another example is to show different treatments + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatments(String key, List featureFlagNames, Map attributes); + + /** + * To understand why this method is useful, consider the following simple Feature Flag as an example: + * + * if user is in segment employees then feature flag 100%:on + * else if user is in segment all then feature flag 20%:on,80%:off + * + * There are two concepts here: matching and bucketing. Matching + * refers to ‘user is in segment employees’ or ‘user is in segment + * all’ whereas bucketing refers to ‘100%:on’ or ‘20%:on,80%:off’. + * + * By default, the same customer key is used for both matching and + * bucketing. However, for some advanced use cases, you may want + * to use different keys. For such cases, use this method. + * + * As an example, suppose you want to rollout to percentages of + * users in specific accounts. You can achieve that by matching + * via account id, but bucketing by user id. + * + * Another example is when you want to ensure that a user continues to get + * the same treatment after they sign up for your product that they used + * to get when they were simply a visitor to your site. In that case, + * before they sign up, you can use their visitor id for both matching and bucketing, but + * post log-in you can use their user id for matching and visitor id for bucketing. + * + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * + * @return for each feature flag the evaluated treatment, the default treatment of the feature flag, or 'control'. + */ + Map getTreatments(Key key, List featureFlagNames, Map attributes); + + /** + * Same as {@link #getTreatments(String, List)} but it returns the configuration associated to the + * matching treatments if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @return Map containing for each feature flag the evaluated treatment (the default treatment of + * this feature flag, or 'control') and a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(String key, List featureFlagNames); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag a SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes); + + /** + * Same as {@link #getTreatments(Key, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * + * @return for each feature flag a SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(String key, String flagSet); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(String key, List flagSets); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(String key, String flagSet); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(String key, List flagSets); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes); + + /** + * Returns the treatment to show this key for this feature flag. The set of treatments + * for a feature flag can be configured on the Split user interface. + *

+ *

+ * This method returns the string 'control' if: + *

    + *
  1. Any of the parameters were null
  2. + *
  3. There was an exception in evaluating the treatment
  4. + *
  5. The SDK does not know of the existence of this feature flag
  6. + *
  7. The feature flag was deleted through the Split user interface.
  8. + *
+ * 'control' is a reserved treatment (you cannot create a treatment with the + * same name) to highlight these exceptional circumstances. + *

+ *

+ * The sdk returns the default treatment of this feature flag if: + *

    + *
  1. The feature flag was killed
  2. + *
  3. The key did not match any of the conditions in the feature flag roll-out plan
  4. + *
+ * The default treatment of a feature flag is set on the Split user interface. + *

+ *

+ * This method does not throw any exceptions. It also never returns null. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param evaluationOptions additional data for evaluation. + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + String getTreatment(String key, String featureFlagName, EvaluationOptions evaluationOptions); + + /** + * This method is useful when you want to determine the treatment to show + * to an customer (user, account etc.) based on an attribute of that customer + * instead of it's key. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + String getTreatment(String key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions); + + /** + * To understand why this method is useful, consider the following simple Feature Flag as an example: + * + * if user is in segment employees then feature flag 100%:on + * else if user is in segment all then feature flag 20%:on,80%:off + * + * There are two concepts here: matching and bucketing. Matching + * refers to ‘user is in segment employees’ or ‘user is in segment + * all’ whereas bucketing refers to ‘100%:on’ or ‘20%:on,80%:off’. + * + * By default, the same customer key is used for both matching and + * bucketing. However, for some advanced use cases, you may want + * to use different keys. For such cases, use this method. + * + * As an example, suppose you want to rollout to percentages of + * users in specific accounts. You can achieve that by matching + * via account id, but bucketing by user id. + * + * Another example is when you want to ensure that a user continues to get + * the same treatment after they sign up for your product that they used + * to get when they were simply a visitor to your site. In that case, + * before they sign up, you can use their visitor id for both matching and bucketing, but + * post log-in you can use their user id for matching and visitor id for bucketing. + * + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + String getTreatment(Key key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Returns a map of feature flag name and treatments to show this key for these feature flags. The set of treatments + * for a feature flag can be configured on the Split user interface. + *

+ *

+ * This method returns for each feature flag the string 'control' if: + *

    + *
  1. Any of the parameters were null
  2. + *
  3. There was an exception in evaluating the treatment
  4. + *
  5. The SDK does not know of the existence of this feature flag
  6. + *
  7. The feature flag was deleted through the Split user interface.
  8. + *
+ * 'control' is a reserved treatment (you cannot create a treatment with the + * same name) to highlight these exceptional circumstances. + *

+ *

+ * The sdk returns for each feature flag the default treatment of this feature flag if: + *

    + *
  1. The feature flag was killed
  2. + *
  3. The key did not match any of the conditions in the feature flag roll-out plan
  4. + *
+ * The default treatment of a feature flag is set on the Split user interface. + *

+ *

+ * This method does not throw any exceptions. It also never returns null. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment for each feature flag, or 'control'. + */ + Map getTreatments(String key, List featureFlagNames, EvaluationOptions evaluationOptions); + + /** + * This method is useful when you want to determine the treatments to show + * to a customer (user, account etc.) based on an attribute of that customer + * instead of their key. + *

+ *

+ * Examples include showing different treatments to users on trial plan + * vs. premium plan. Another example is to show different treatments + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatments(String key, List featureFlagNames, Map attributes, EvaluationOptions evaluationOptions); + + /** + * To understand why this method is useful, consider the following simple Feature Flag as an example: + * + * if user is in segment employees then feature flag 100%:on + * else if user is in segment all then feature flag 20%:on,80%:off + * + * There are two concepts here: matching and bucketing. Matching + * refers to ‘user is in segment employees’ or ‘user is in segment + * all’ whereas bucketing refers to ‘100%:on’ or ‘20%:on,80%:off’. + * + * By default, the same customer key is used for both matching and + * bucketing. However, for some advanced use cases, you may want + * to use different keys. For such cases, use this method. + * + * As an example, suppose you want to rollout to percentages of + * users in specific accounts. You can achieve that by matching + * via account id, but bucketing by user id. + * + * Another example is when you want to ensure that a user continues to get + * the same treatment after they sign up for your product that they used + * to get when they were simply a visitor to your site. In that case, + * before they sign up, you can use their visitor id for both matching and bucketing, but + * post log-in you can use their user id for matching and visitor id for bucketing. + * + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * + * @return for each feature flag the evaluated treatment, the default treatment of the feature flag, or 'control'. + */ + Map getTreatments(Key key, List featureFlagNames, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatment(String, String)} but it returns the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param evaluationOptions additional data for evaluation + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + SplitResult getTreatmentWithConfig(String key, String featureFlagName, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatment(Key, String, Map)} but it returns the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatment(String, String, Map)} but it returns the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagName the name of the feature flag we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag a SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List)} but it returns the configuration associated to the + * matching treatments if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param evaluationOptions additional data for evaluation + * @return Map containing for each feature flag the evaluated treatment (the default treatment of + * this feature flag, or 'control') and a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(String key, List featureFlagNames, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(Key, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + * + * @param key the matching and bucketing keys. MUST NOT be null. + * @param featureFlagNames the names of the feature flags we want to evaluate. MUST NOT be null. + * @param attributes of the entity (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * + * @return for each feature flag a SplitResult containing the evaluated treatment (the default treatment of this feature flag, or 'control') and + * a configuration associated to this treatment if set. + */ + Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key a unique key of your customer (e.g. user_id, user_email, account_id, etc.) MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes, EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment, the default treatment of this feature flag, or 'control'. + */ + Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSet the Flag Set name that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions); + + /** + * Same as {@link #getTreatments(String, List, Map)} but it returns for each feature flag the configuration associated to the + * matching treatment if any. Otherwise {@link SplitResult.config()} will be null. + *

+ *

+ * Examples include showing a different treatment to users on trial plan + * vs. premium plan. Another example is to show a different treatment + * to users created after a certain date. + * + * @param key the matching and bucketing keys. MUST not be null or empty. + * @param flagSets the names of Flag Sets that you want to evaluate. MUST not be null or empty. + * @param attributes of the customer (user, account etc.) to use in evaluation. Can be null or empty. + * @param evaluationOptions additional data for evaluation + * @return for each feature flag the evaluated treatment (the default treatment of this feature flag, or 'control') and a configuration + * associated to this treatment if set. + */ + Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions); /** * Destroys the background processes and clears the cache, releasing the resources used by @@ -146,6 +953,53 @@ public interface SplitClient { */ void destroy(); + /** + * Registers {@code callback} to run when {@code event} fires, ignoring the event details. + * Equivalent to {@link #on(SdkEvent, SdkEventListener)} with a listener that discards its metadata. + * + * @param event the event to subscribe to. If null a warning is logged and the call is ignored. + * @param callback invoked each time the event fires. If null a warning is logged and the call is ignored. + * @see #on(SdkEvent, SdkEventListener) + */ + void on(SdkEvent event, Runnable callback); + + /** + * Registers {@code listener} to be invoked when {@code event} fires. + * + *

Any number of listeners can be registered for the same event, including the same listener more than once; + * each registration is invoked, in registration order. Listeners run on an SDK-owned thread, never on the caller's + * thread, and an exception thrown by one listener does not prevent the others from running. + * + *

Per event: + *

    + *
  • {@link SdkEvent#SDK_READY}: fires once, when feature flags and segments are available. Metadata has + * {@code isInitialCacheLoad()} and {@code getLastUpdateTimestamp()} populated (the timestamp may be null); + * {@code getType()} is null and {@code getNames()} is empty. A listener registered after the event already + * fired is still invoked once, asynchronously, with {@code null} metadata.
  • + *
  • {@link SdkEvent#SDK_READY_TIMED_OUT}: fires once if {@link #blockUntilReady()} times out, and is not + * emitted if the SDK became ready first. Metadata is always {@code null}; it is replayed to late + * subscribers like {@code SDK_READY}.
  • + *
  • {@link SdkEvent#SDK_UPDATE}: fires every time feature flags or segments change after the SDK became + * ready. Metadata has {@code getType()} and {@code getNames()} populated (the flag names for a flag update; + * empty for segment updates). It is never replayed to late subscribers. It is not available in consumer + * mode, where no update event is ever produced.
  • + *
+ * + *

Registering after {@link #destroy()} is a silent no-op. + * + * @param event the event to subscribe to. If null a warning is logged and the call is ignored. + * @param listener invoked when the event fires. If null a warning is logged and the call is ignored. + */ + void on(SdkEvent event, SdkEventListener listener); + + /** + * Removes every listener registered for {@code event}. Other events are not affected, and new listeners can + * still be registered for {@code event} afterwards. + * + * @param event the event whose listeners are removed. If null a warning is logged and the call is ignored. + */ + void off(SdkEvent event); + /** * Enqueue a new event to be sent to split data collection services * @@ -184,7 +1038,6 @@ public interface SplitClient { * @param key the identifier of the entity * @param trafficType the type of the event * @param eventType the type of the event - * @param value the value of the event * * @return true if the track was successful, false otherwise */ @@ -218,4 +1071,4 @@ public interface SplitClient { *

*/ void blockUntilReady() throws TimeoutException, InterruptedException; -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/SplitClientConfig.java b/client/src/main/java/io/split/client/SplitClientConfig.java index cafd52eb..3a38fccd 100644 --- a/client/src/main/java/io/split/client/SplitClientConfig.java +++ b/client/src/main/java/io/split/client/SplitClientConfig.java @@ -1,13 +1,31 @@ package io.split.client; - +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.dtos.ProxyConfiguration; import io.split.client.impressions.ImpressionListener; import io.split.client.impressions.ImpressionsManager; +import io.split.client.utils.FileTypeEnum; import io.split.integrations.IntegrationsConfig; +import io.split.service.CustomHttpModule; +import io.split.storages.enums.OperationMode; +import io.split.storages.enums.StorageMode; import org.apache.hc.core5.http.HttpHost; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; import java.io.IOException; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.concurrent.ThreadFactory; +import java.io.InputStream; + +import static io.split.inputValidation.FallbackTreatmentValidator.isValidByFlagTreatment; +import static io.split.inputValidation.FallbackTreatmentValidator.isValidTreatment; +import static io.split.inputValidation.FlagSetsValidator.cleanup; /** * Configurations for the SplitClient. @@ -16,7 +34,21 @@ */ public class SplitClientConfig { + private static final org.slf4j.Logger _log = LoggerFactory.getLogger(SplitClientConfig.class); public static final String LOCALHOST_DEFAULT_FILE = "split.yaml"; + public static final String SDK_ENDPOINT = "https://sdk.split.io"; + public static final String EVENTS_ENDPOINT = "https://events.split.io"; + public static final String AUTH_ENDPOINT = "https://auth.split.io/api/v2/auth"; + public static final String STREAMING_ENDPOINT = "https://streaming.split.io/sse"; + public static final String TELEMETRY_ENDPOINT = "https://telemetry.split.io/api/v1"; + + public static class HttpScheme { + private HttpScheme() { + throw new IllegalStateException("Utility class"); + } + public static final String HTTP = "http"; + public static final String HTTPS = "https"; + } private final String _endpoint; private final String _eventsEndpoint; @@ -33,28 +65,52 @@ public class SplitClientConfig { private final boolean _debugEnabled; private final boolean _labelsEnabled; private final boolean _ipAddressEnabled; + private final boolean _localhostRefreshEnabled; private final int _ready; private final int _waitBeforeShutdown; private final int _eventsQueueSize; - private final long _eventFlushIntervalInMillis; + private final long _eventSendIntervalInMillis; private final int _maxStringLength; private final boolean _destroyOnShutDown; private final String _splitFile; + private final FileTypeEnum _fileType; + private final InputStream _inputStream; + private final String _segmentDirectory; private final IntegrationsConfig _integrationsConfig; private final boolean _streamingEnabled; private final int _authRetryBackoffBase; private final int _streamingReconnectBackoffBase; private final String _authServiceURL; private final String _streamingServiceURL; + private final String _telemetryURL; + private final int _telemetryRefreshRate; + private final int _onDemandFetchRetryDelayMs; + private final int _onDemandFetchMaxRetries; + private final int _failedAttemptsBeforeLogging; + private final int _uniqueKeysRefreshRateInMemory; + private final int _uniqueKeysRefreshRateRedis; + private static int _filterUniqueKeysRefreshRate; + private final OperationMode _operationMode; + private long _validateAfterInactivityInMillis; + private final long _startingSyncCallBackoffBaseMs; + private final CustomStorageWrapper _customStorageWrapper; + private final StorageMode _storageMode; + private final ThreadFactory _threadFactory; + private final FallbackTreatmentsConfiguration _fallbackTreatments; // Proxy configs + private final ProxyConfiguration _proxyConfiguration; private final HttpHost _proxy; private final String _proxyUsername; private final String _proxyPassword; // To be set during startup public static String splitSdkVersion; - + private final long _lastSeenCacheSize; + private final HashSet _flagSetsFilter; + private final int _invalidSets; + private final CustomHeaderDecorator _customHeaderDecorator; + private final CustomHttpModule _alternativeHTTPModule; public static Builder builder() { return new Builder(); @@ -75,21 +131,46 @@ private SplitClientConfig(String endpoint, boolean debugEnabled, boolean labelsEnabled, boolean ipAddressEnabled, + boolean localhostRefreshEnabled, int waitBeforeShutdown, HttpHost proxy, String proxyUsername, String proxyPassword, + ProxyConfiguration proxyConfiguration, int eventsQueueSize, - long eventFlushIntervalInMillis, + long eventSendIntervalInMillis, int maxStringLength, boolean destroyOnShutDown, String splitFile, + FileTypeEnum fileType, + InputStream inputStream, + String segmentDirectory, IntegrationsConfig integrationsConfig, boolean streamingEnabled, int authRetryBackoffBase, int streamingReconnectBackoffBase, String authServiceURL, - String streamingServiceURL) { + String streamingServiceURL, + String telemetryURL, + int telemetryRefreshRate, + int onDemandFetchRetryDelayMs, + int onDemandFetchMaxRetries, + int failedAttemptsBeforeLogging, + OperationMode operationMode, + long validateAfterInactivityInMillis, + long startingSyncCallBackoffBaseMs, + CustomStorageWrapper customStorageWrapper, + StorageMode storageMode, + int uniqueKeysRefreshRateInMemory, + int uniqueKeysRefreshRateRedis, + int filterUniqueKeysRefreshRate, + long lastSeenCacheSize, + ThreadFactory threadFactory, + HashSet flagSetsFilter, + int invalidSets, + CustomHeaderDecorator customHeaderDecorator, + CustomHttpModule alternativeHTTPModule, + FallbackTreatmentsConfiguration fallbackTreatments) { _endpoint = endpoint; _eventsEndpoint = eventsEndpoint; _featuresRefreshRate = pollForFeatureChangesEveryNSeconds; @@ -105,21 +186,46 @@ private SplitClientConfig(String endpoint, _debugEnabled = debugEnabled; _labelsEnabled = labelsEnabled; _ipAddressEnabled = ipAddressEnabled; + _localhostRefreshEnabled = localhostRefreshEnabled; _waitBeforeShutdown = waitBeforeShutdown; _proxy = proxy; _proxyUsername = proxyUsername; _proxyPassword = proxyPassword; + _proxyConfiguration = proxyConfiguration; _eventsQueueSize = eventsQueueSize; - _eventFlushIntervalInMillis = eventFlushIntervalInMillis; + _eventSendIntervalInMillis = eventSendIntervalInMillis; _maxStringLength = maxStringLength; _destroyOnShutDown = destroyOnShutDown; _splitFile = splitFile; + _fileType = fileType; + _inputStream = inputStream; + _segmentDirectory = segmentDirectory; _integrationsConfig = integrationsConfig; _streamingEnabled = streamingEnabled; _authRetryBackoffBase = authRetryBackoffBase; _streamingReconnectBackoffBase = streamingReconnectBackoffBase; _authServiceURL = authServiceURL; _streamingServiceURL = streamingServiceURL; + _telemetryURL = telemetryURL; + _telemetryRefreshRate = telemetryRefreshRate; + _uniqueKeysRefreshRateInMemory = uniqueKeysRefreshRateInMemory; + _uniqueKeysRefreshRateRedis = uniqueKeysRefreshRateRedis; + _filterUniqueKeysRefreshRate = filterUniqueKeysRefreshRate; + _onDemandFetchRetryDelayMs = onDemandFetchRetryDelayMs; + _onDemandFetchMaxRetries = onDemandFetchMaxRetries; + _failedAttemptsBeforeLogging = failedAttemptsBeforeLogging; + _operationMode = operationMode; + _storageMode = storageMode; + _validateAfterInactivityInMillis = validateAfterInactivityInMillis; + _startingSyncCallBackoffBaseMs = startingSyncCallBackoffBaseMs; + _customStorageWrapper = customStorageWrapper; + _lastSeenCacheSize = lastSeenCacheSize; + _threadFactory = threadFactory; + _flagSetsFilter = flagSetsFilter; + _invalidSets = invalidSets; + _customHeaderDecorator = customHeaderDecorator; + _alternativeHTTPModule = alternativeHTTPModule; + _fallbackTreatments = fallbackTreatments; Properties props = new Properties(); try { @@ -158,6 +264,16 @@ public int impressionsRefreshRate() { return _impressionsRefreshRate; } + public int uniqueKeysRefreshRateInMemory() { + return _uniqueKeysRefreshRateInMemory; + } + public int uniqueKeysRefreshRateRedis() { + return _uniqueKeysRefreshRateRedis; + } + public static int filterUniqueKeysRefreshRate() { + return _filterUniqueKeysRefreshRate; + } + public int impressionsQueueSize() { return _impressionsQueueSize; } @@ -184,6 +300,10 @@ public boolean debugEnabled() { public boolean ipAddressEnabled() { return _ipAddressEnabled; } + public boolean localhostRefreshEnabled() { + return _localhostRefreshEnabled; + } + public int blockUntilReady() { return _ready; } @@ -204,8 +324,12 @@ public String proxyPassword() { return _proxyPassword; } - public long eventFlushIntervalInMillis() { - return _eventFlushIntervalInMillis; + public ProxyConfiguration proxyConfiguration() { + return _proxyConfiguration; + } + + public long eventSendIntervalInMillis() { + return _eventSendIntervalInMillis; } public int eventsQueueSize() { @@ -224,6 +348,18 @@ public String splitFile() { return _splitFile; } + public FileTypeEnum fileType() { + return _fileType; + } + + public InputStream inputStream(){ + return _inputStream; + } + + public String segmentDirectory() { + return _segmentDirectory; + } + public IntegrationsConfig integrationsConfig() { return _integrationsConfig; } @@ -248,11 +384,72 @@ public String streamingServiceURL() { return _streamingServiceURL; } - public static final class Builder { + public String telemetryURL() { + return _telemetryURL; + } + + /** + * @deprecated As of release 4.X.X, replaced by {@link #getTelemetryRefreshRate()} } //todo update version + **/ + @Deprecated + public int get_telemetryRefreshRate() { + return _telemetryRefreshRate; + } + + public int getTelemetryRefreshRate() { + return _telemetryRefreshRate; + } + public int streamingRetryDelay() {return _onDemandFetchRetryDelayMs;} + + public int streamingFetchMaxRetries() {return _onDemandFetchMaxRetries;} + + public int failedAttemptsBeforeLogging() {return _failedAttemptsBeforeLogging;} + + public OperationMode operationMode() { return _operationMode;} + + public long validateAfterInactivityInMillis() { + return _validateAfterInactivityInMillis; + } + public long startingSyncCallBackoffBaseMs(){ return _startingSyncCallBackoffBaseMs;} + + public CustomStorageWrapper customStorageWrapper() { + return _customStorageWrapper; + } + + public StorageMode storageMode() { return _storageMode;} + + public long getLastSeenCacheSize() { + return _lastSeenCacheSize; + } + + public ThreadFactory getThreadFactory() { + return _threadFactory; + } - private String _endpoint = "https://sdk.split.io"; + public HashSet getSetsFilter() { + return _flagSetsFilter; + } + + public int getInvalidSets() { + return _invalidSets; + } + + public CustomHeaderDecorator customHeaderDecorator() { + return _customHeaderDecorator; + } + + public boolean isSdkEndpointOverridden() { + return !_endpoint.equals(SDK_ENDPOINT); + } + + public CustomHttpModule alternativeHTTPModule() { return _alternativeHTTPModule; } + + public FallbackTreatmentsConfiguration fallbackTreatments() { return _fallbackTreatments; } + + public static final class Builder { + private String _endpoint = SDK_ENDPOINT; private boolean _endpointSet = false; - private String _eventsEndpoint = "https://events.split.io"; + private String _eventsEndpoint = EVENTS_ENDPOINT; private boolean _eventsEndpointSet = false; private int _featuresRefreshRate = 60; private int _segmentsRefreshRate = 60; @@ -261,28 +458,53 @@ public static final class Builder { private ImpressionsManager.Mode _impressionsMode = ImpressionsManager.Mode.OPTIMIZED; private int _connectionTimeout = 15000; private int _readTimeout = 15000; - private int _numThreadsForSegmentFetch = 2; + private int _numThreadsForSegmentFetch = 10; private boolean _debugEnabled = false; private int _ready = -1; // -1 means no blocking private int _metricsRefreshRate = 60; private boolean _labelsEnabled = true; - private boolean _ipAddressEnabled = true; + private boolean _ipAddressEnabled = true; + private boolean _localhostRefreshEnable = false; private int _waitBeforeShutdown = 5000; private String _proxyHost = "localhost"; private int _proxyPort = -1; private String _proxyUsername; private String _proxyPassword; + private ProxyConfiguration _proxyConfiguration; private int _eventsQueueSize = 500; - private long _eventFlushIntervalInMillis = 30 * 1000; + private long _eventSendIntervalInMillis = 30 * (long)1000; private int _maxStringLength = 250; private boolean _destroyOnShutDown = true; private String _splitFile = null; + private FileTypeEnum _fileType = null; + private InputStream _inputStream = null; + private String _segmentDirectory = null; private IntegrationsConfig _integrationsConfig = null; private boolean _streamingEnabled = true; private int _authRetryBackoffBase = 1; private int _streamingReconnectBackoffBase = 1; - private String _authServiceURL = "https://auth.split.io/api/auth"; - private String _streamingServiceURL = "https://streaming.split.io/sse"; + private String _authServiceURL = AUTH_ENDPOINT; + private String _streamingServiceURL = STREAMING_ENDPOINT; + private String _telemetryURl = TELEMETRY_ENDPOINT; + private int _telemetryRefreshRate = 600; + private final int _uniqueKeysRefreshRateInMemory = 900; + private final int _uniqueKeysRefreshRateRedis = 300; + private final int _filterUniqueKeysRefreshRate = 86400; + private int _onDemandFetchRetryDelayMs = 50; + private final int _onDemandFetchMaxRetries = 10; + private final int _failedAttemptsBeforeLogging = 10; + private OperationMode _operationMode = OperationMode.STANDALONE; + private long _validateAfterInactivityInMillis = 1000; + private static final long STARTING_SYNC_CALL_BACKOFF_BASE_MS = 1000; //backoff base starting at 1 seconds + private CustomStorageWrapper _customStorageWrapper; + private StorageMode _storageMode = StorageMode.MEMORY; + private final long _lastSeenCacheSize = 500000; + private ThreadFactory _threadFactory; + private HashSet _flagSetsFilter = new HashSet<>(); + private int _invalidSetsCount = 0; + private CustomHeaderDecorator _customHeaderDecorator = null; + private CustomHttpModule _alternativeHTTPModule = null; + private FallbackTreatmentsConfiguration _fallbackTreatments; public Builder() { } @@ -322,7 +544,7 @@ public Builder eventsQueueSize(int eventsQueueSize) { * @return this builder */ public Builder eventFlushIntervalInMillis(long eventFlushIntervalInMillis) { - _eventFlushIntervalInMillis = eventFlushIntervalInMillis; + _eventSendIntervalInMillis = eventFlushIntervalInMillis; return this; } @@ -542,10 +764,14 @@ public Builder waitBeforeShutdown(int waitTime) { /** * The host location of the proxy. Default is localhost. + * @deprecated + * This method is deprecated. + *

Use {@link ProxyConfiguration)} instead. * * @param proxyHost location of the proxy * @return this builder */ + @Deprecated public Builder proxyHost(String proxyHost) { _proxyHost = proxyHost; return this; @@ -553,10 +779,14 @@ public Builder proxyHost(String proxyHost) { /** * The port of the proxy. Default is -1. + * @deprecated + * This method is deprecated. + *

Use {@link ProxyConfiguration)} instead. * * @param proxyPort port for the proxy * @return this builder */ + @Deprecated public Builder proxyPort(int proxyPort) { _proxyPort = proxyPort; return this; @@ -564,10 +794,14 @@ public Builder proxyPort(int proxyPort) { /** * Set the username for authentication against the proxy (if proxy settings are enabled). (Optional). + * @deprecated + * This method is deprecated. + *

Use {@link ProxyConfiguration)} instead. * * @param proxyUsername * @return this builder */ + @Deprecated public Builder proxyUsername(String proxyUsername) { _proxyUsername = proxyUsername; return this; @@ -584,6 +818,17 @@ public Builder proxyPassword(String proxyPassword) { return this; } + /** + * Set the mtls authentication against the proxy (if proxy settings are enabled). (Optional). + * + * @param proxyConfiguration + * @return this builder + */ + public Builder proxyConfiguration(ProxyConfiguration proxyConfiguration) { + _proxyConfiguration = proxyConfiguration; + return this; + } + /** * Disables running destroy() on shutdown by default. * @@ -614,6 +859,24 @@ public Builder splitFile(String splitFile) { return this; } + public Builder splitFile(InputStream inputStream, FileTypeEnum fileType) { + _fileType = fileType; + _inputStream = inputStream; + return this; + } + + /** + * Set the location of the directory where are the segment json files for localhost mode. + * This setting is optional. + * + * @param sementDirectory location + * @return this builder + */ + public Builder segmentDirectory(String sementDirectory){ + _segmentDirectory = sementDirectory; + return this; + } + /** * Sets up integrations for the Split SDK (Currently Impressions outgoing integrations supported only). * @param config @@ -634,6 +897,16 @@ public Builder streamingEnabled(boolean streamingEnabled) { return this; } + /** + * Set if refresh is enabled or not for localhost mode. Default is false. + * @param localhostRefreshEnable + * @return + */ + public Builder localhostRefreshEnable(boolean localhostRefreshEnable) { + _localhostRefreshEnable = localhostRefreshEnable; + return this; + } + /** * Set how many seconds to wait before re attempting to authenticate for push notifications. Default 1 second. Minimum 1 second. * @param authRetryBackoffBase @@ -674,7 +947,115 @@ public Builder streamingServiceURL(String streamingServiceURL) { return this; } - public SplitClientConfig build() { + /** Set telemetry service URL. + * @param telemetryURL + * @return + */ + public Builder telemetryURL(String telemetryURL) { + _telemetryURl = telemetryURL; + return this; + } + + /** + * How often send telemetry data + * + * @param telemetryRefreshRate + * @return this builder + */ + public Builder telemetryRefreshRate(int telemetryRefreshRate) { + _telemetryRefreshRate = telemetryRefreshRate; + return this; + } + + /** + * Type of storage + * + * @param mode + * @return this builder + */ + public Builder operationMode(OperationMode mode) { + _operationMode = mode; + return this; + } + + /** + * + * @param mode + * @return this builder + */ + public Builder storageMode(StorageMode mode) { + _storageMode = mode; + return this; + } + + /** + * Storage wrapper + * + * @param customStorageWrapper + * @return this builder + */ + public Builder customStorageWrapper(CustomStorageWrapper customStorageWrapper) { + _customStorageWrapper = customStorageWrapper; + return this; + } + + /** + * Flag Sets Filter + * + * @param flagSetsFilter + * @return this builder + */ + public Builder flagSetsFilter(List flagSetsFilter) { + _flagSetsFilter = new LinkedHashSet<>(cleanup(flagSetsFilter)); + _invalidSetsCount = flagSetsFilter.size() - _flagSetsFilter.size(); + return this; + } + + /** + * User Custom Header Decorator + * + * @param customHeaderDecorator + * @return this builder + */ + public Builder customHeaderDecorator(CustomHeaderDecorator customHeaderDecorator) { + _customHeaderDecorator = customHeaderDecorator; + return this; + } + + /** + * Alternative Http Client + * + * @param alternativeHTTPModule + * @return this builder + */ + public Builder alternativeHTTPModule(CustomHttpModule alternativeHTTPModule) { + _alternativeHTTPModule = alternativeHTTPModule; + return this; + } + + /** + * Fallback Treatments + * + * @param fallbackTreatments + * @return this builder + */ + public Builder fallbackTreatments(FallbackTreatmentsConfiguration fallbackTreatments) { + _fallbackTreatments = fallbackTreatments; + return this; + } + + /** + * Thread Factory + * + * @param threadFactory + * @return this builder + */ + public Builder threadFactory(ThreadFactory threadFactory) { + _threadFactory = threadFactory; + return this; + } + + private void verifyRates() { if (_featuresRefreshRate < 5 ) { throw new IllegalArgumentException("featuresRefreshRate must be >= 5: " + _featuresRefreshRate); } @@ -683,6 +1064,46 @@ public SplitClientConfig build() { throw new IllegalArgumentException("segmentsRefreshRate must be >= 30: " + _segmentsRefreshRate); } + if (_eventSendIntervalInMillis < 1000) { + throw new IllegalArgumentException("_eventSendIntervalInMillis must be >= 1000: " + _eventSendIntervalInMillis); + } + + if (_metricsRefreshRate < 30) { + throw new IllegalArgumentException("metricsRefreshRate must be >= 30: " + _metricsRefreshRate); + } + + if(_telemetryRefreshRate < 60) { + throw new IllegalStateException("_telemetryRefreshRate must be >= 60"); + } + } + + private void verifyEndPoints() { + if (_endpoint == null) { + throw new IllegalArgumentException("endpoint must not be null"); + } + + if (_eventsEndpoint == null) { + throw new IllegalArgumentException("events endpoint must not be null"); + } + + if (_endpointSet && !_eventsEndpointSet) { + throw new IllegalArgumentException("If endpoint is set, you must also set the events endpoint"); + } + + if (_authServiceURL == null) { + throw new IllegalArgumentException("authServiceURL must not be null"); + } + + if (_streamingServiceURL == null) { + throw new IllegalArgumentException("streamingServiceURL must not be null"); + } + + if (_telemetryURl == null) { + throw new IllegalArgumentException("telemetryURl must not be null"); + } + } + + private void verifyAllModes() { switch (_impressionsMode) { case OPTIMIZED: _impressionsRefreshRate = (_impressionsRefreshRate <= 0) ? 300 : Math.max(60, _impressionsRefreshRate); @@ -690,20 +1111,26 @@ public SplitClientConfig build() { case DEBUG: _impressionsRefreshRate = (_impressionsRefreshRate <= 0) ? 60 : _impressionsRefreshRate; break; + case NONE: + break; } - if (_eventFlushIntervalInMillis < 1000) { - throw new IllegalArgumentException("_eventFlushIntervalInMillis must be >= 1000: " + _eventFlushIntervalInMillis); + if (_impressionsQueueSize <=0 ) { + throw new IllegalArgumentException("impressionsQueueSize must be > 0: " + _impressionsQueueSize); } - - if (_metricsRefreshRate < 30) { - throw new IllegalArgumentException("metricsRefreshRate must be >= 30: " + _metricsRefreshRate); + if(_storageMode == null) { + _storageMode = StorageMode.MEMORY; } - if (_impressionsQueueSize <=0 ) { - throw new IllegalArgumentException("impressionsQueueSize must be > 0: " + _impressionsQueueSize); + if(OperationMode.CONSUMER.equals(_operationMode)){ + if(_customStorageWrapper == null) { + throw new IllegalStateException("Custom Storage must not be null on Consumer mode."); + } + _storageMode = StorageMode.PLUGGABLE; } + } + private void verifyNetworkParams() { if (_connectionTimeout <= 0) { throw new IllegalArgumentException("connectionTimeOutInMs must be > 0: " + _connectionTimeout); } @@ -711,40 +1138,89 @@ public SplitClientConfig build() { if (_readTimeout <= 0) { throw new IllegalArgumentException("readTimeout must be > 0: " + _readTimeout); } + if (_authRetryBackoffBase <= 0) { + throw new IllegalArgumentException("authRetryBackoffBase: must be >= 1"); + } - if (_endpoint == null) { - throw new IllegalArgumentException("endpoint must not be null"); + if (_streamingReconnectBackoffBase <= 0) { + throw new IllegalArgumentException("streamingReconnectBackoffBase: must be >= 1"); } - if (_eventsEndpoint == null) { - throw new IllegalArgumentException("events endpoint must not be null"); + if (_onDemandFetchRetryDelayMs <= 0) { + throw new IllegalStateException("streamingRetryDelay must be > 0"); } - if (_endpointSet && !_eventsEndpointSet) { - throw new IllegalArgumentException("If endpoint is set, you must also set the events endpoint"); + if(_onDemandFetchMaxRetries <= 0) { + throw new IllegalStateException("_onDemandFetchMaxRetries must be > 0"); } + } - if (_numThreadsForSegmentFetch <= 0) { - throw new IllegalArgumentException("Number of threads for fetching segments MUST be greater than zero"); + private void verifyAlternativeClient() { + if (_alternativeHTTPModule != null && _streamingEnabled) { + throw new IllegalArgumentException("Streaming feature is not supported with Alternative HTTP Client"); } + } - if (_authRetryBackoffBase <= 0) { - throw new IllegalArgumentException("authRetryBackoffBase: must be >= 1"); + private void verifyProxy() { + if (_proxyConfiguration == null) + return; + + if (_proxyPort != -1) { + _log.warn("Both the deprecated proxy configuration methods (`proxyHost`, `proxyPort`, `proxyUsername`, or `proxyPassword`) " + + "and the new `ProxyConfiguration` builder are being used. `ProxyConfiguration` will take precedence."); } - if (_streamingReconnectBackoffBase <= 0) { - throw new IllegalArgumentException("streamingReconnectBackoffBase: must be >= 1"); + if (!(_proxyConfiguration.getHost().getSchemeName().equals(HttpScheme.HTTP) || + _proxyConfiguration.getHost().getSchemeName().equals(HttpScheme.HTTPS))) { + throw new IllegalArgumentException("Proxy scheme must be either http or https."); } - if (_authServiceURL == null) { - throw new IllegalArgumentException("authServiceURL must not be null"); + if ((_proxyConfiguration.getP12File() != null && _proxyConfiguration.getPassKey() == null) || + (_proxyConfiguration.getP12File() == null && _proxyConfiguration.getPassKey() != null)) { + throw new IllegalArgumentException("Proxy mTLS must have p12 file path and name, and pass phrase."); } + } - if (_streamingServiceURL == null) { - throw new IllegalArgumentException("streamingServiceURL must not be null"); + private void verifyFallbackTreatments() { + if (_fallbackTreatments == null) + return; + + FallbackTreatment processedGlobalFallbackTreatment = _fallbackTreatments.getGlobalFallbackTreatment(); + Map processedByFlagFallbackTreatment = _fallbackTreatments.getByFlagFallbackTreatment(); + + if (_fallbackTreatments.getGlobalFallbackTreatment() != null) { + processedGlobalFallbackTreatment = new FallbackTreatment( + isValidTreatment(_fallbackTreatments.getGlobalFallbackTreatment().getTreatment(), "Fallback treatments"), + _fallbackTreatments.getGlobalFallbackTreatment().getConfig()); + } + + if (_fallbackTreatments.getByFlagFallbackTreatment() != null) { + processedByFlagFallbackTreatment = isValidByFlagTreatment(_fallbackTreatments.getByFlagFallbackTreatment(), "config"); + } + _fallbackTreatments = new FallbackTreatmentsConfiguration(processedGlobalFallbackTreatment, processedByFlagFallbackTreatment); + } + + public SplitClientConfig build() { + + verifyRates(); + + verifyAllModes(); + + verifyEndPoints(); + + verifyNetworkParams(); + + verifyAlternativeClient(); + + verifyProxy(); + + verifyFallbackTreatments(); + + if (_numThreadsForSegmentFetch <= 0) { + throw new IllegalArgumentException("Number of threads for fetching segments MUST be greater than zero"); } - return new SplitClientConfig( + return new SplitClientConfig( _endpoint, _eventsEndpoint, _featuresRefreshRate, @@ -760,21 +1236,46 @@ public SplitClientConfig build() { _debugEnabled, _labelsEnabled, _ipAddressEnabled, + _localhostRefreshEnable, _waitBeforeShutdown, proxy(), _proxyUsername, _proxyPassword, + _proxyConfiguration, _eventsQueueSize, - _eventFlushIntervalInMillis, + _eventSendIntervalInMillis, _maxStringLength, _destroyOnShutDown, _splitFile, + _fileType, + _inputStream, + _segmentDirectory, _integrationsConfig, _streamingEnabled, _authRetryBackoffBase, _streamingReconnectBackoffBase, _authServiceURL, - _streamingServiceURL); + _streamingServiceURL, + _telemetryURl, + _telemetryRefreshRate, + _onDemandFetchRetryDelayMs, + _onDemandFetchMaxRetries, + _failedAttemptsBeforeLogging, + _operationMode, + _validateAfterInactivityInMillis, + STARTING_SYNC_CALL_BACKOFF_BASE_MS, + _customStorageWrapper, + _storageMode, + _uniqueKeysRefreshRateInMemory, + _uniqueKeysRefreshRateRedis, + _filterUniqueKeysRefreshRate, + _lastSeenCacheSize, + _threadFactory, + _flagSetsFilter, + _invalidSetsCount, + _customHeaderDecorator, + _alternativeHTTPModule, + _fallbackTreatments); } } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/SplitClientImpl.java b/client/src/main/java/io/split/client/SplitClientImpl.java index 7792f5a1..2c55729a 100644 --- a/client/src/main/java/io/split/client/SplitClientImpl.java +++ b/client/src/main/java/io/split/client/SplitClientImpl.java @@ -1,31 +1,48 @@ package io.split.client; -import io.split.cache.SplitCache; +import com.google.gson.GsonBuilder; import io.split.client.api.Key; +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventListener; import io.split.client.api.SplitResult; -import io.split.client.dtos.Event; +import io.split.client.dtos.*; +import io.split.client.events.EventsStorageProducer; import io.split.client.impressions.Impression; +import io.split.client.lifecycle.SdkEventsManager; +import io.split.client.lifecycle.SdkInternalEvent; import io.split.client.impressions.ImpressionsManager; +import io.split.client.interceptors.FlagSetsFilter; import io.split.engine.SDKReadinessGates; import io.split.engine.evaluator.Evaluator; import io.split.engine.evaluator.EvaluatorImp; import io.split.engine.evaluator.Labels; -import io.split.engine.metrics.Metrics; -import io.split.grammar.Treatments; import io.split.inputValidation.EventsValidator; import io.split.inputValidation.KeyValidator; import io.split.inputValidation.SplitNameValidator; import io.split.inputValidation.TrafficTypeValidator; +import io.split.inputValidation.ImpressionPropertiesValidator; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.storage.TelemetryConfigProducer; +import io.split.telemetry.storage.TelemetryEvaluationProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.inputValidation.FlagSetsValidator.cleanup; /** * A basic implementation of SplitClient. @@ -33,68 +50,378 @@ * @author adil */ public final class SplitClientImpl implements SplitClient { - public static final SplitResult SPLIT_RESULT_CONTROL = new SplitResult(Treatments.CONTROL, null); - - private static final String GET_TREATMENT = "getTreatment"; - private static final String GET_TREATMENT_WITH_CONFIG = "getTreatmentWithConfig"; + private static final String CLIENT_DESTROY = "Client has already been destroyed - no calls possible"; + private static final String CATCHALL_EXCEPTION = "CatchAll Exception"; + private static final String MATCHING_KEY = "matchingKey"; private static final Logger _log = LoggerFactory.getLogger(SplitClientImpl.class); private final SplitFactory _container; - private final SplitCache _splitCache; + private final SdkEventsManager _eventsManager; + private final SplitCacheConsumer _splitCacheConsumer; private final ImpressionsManager _impressionManager; - private final Metrics _metrics; private final SplitClientConfig _config; - private final EventClient _eventClient; + private final EventsStorageProducer _eventsStorageProducer; private final SDKReadinessGates _gates; private final Evaluator _evaluator; + private final TelemetryEvaluationProducer _telemetryEvaluationProducer; + private final TelemetryConfigProducer _telemetryConfigProducer; + private final FlagSetsFilter _flagSetsFilter; + private final FallbackTreatmentCalculator _fallbackTreatmentCalculator; public SplitClientImpl(SplitFactory container, - SplitCache splitCache, + SplitCacheConsumer splitCacheConsumer, ImpressionsManager impressionManager, - Metrics metrics, - EventClient eventClient, + EventsStorageProducer eventsStorageProducer, SplitClientConfig config, SDKReadinessGates gates, - Evaluator evaluator) { + Evaluator evaluator, + TelemetryEvaluationProducer telemetryEvaluationProducer, + TelemetryConfigProducer telemetryConfigProducer, + FlagSetsFilter flagSetsFilter, + FallbackTreatmentCalculator fallbackTreatmentCalculator) { + this(container, splitCacheConsumer, impressionManager, eventsStorageProducer, config, gates, evaluator, + telemetryEvaluationProducer, telemetryConfigProducer, flagSetsFilter, fallbackTreatmentCalculator, null); + } + + + public SplitClientImpl(SplitFactory container, + SplitCacheConsumer splitCacheConsumer, + ImpressionsManager impressionManager, + EventsStorageProducer eventsStorageProducer, + SplitClientConfig config, + SDKReadinessGates gates, + Evaluator evaluator, + TelemetryEvaluationProducer telemetryEvaluationProducer, + TelemetryConfigProducer telemetryConfigProducer, + FlagSetsFilter flagSetsFilter, + FallbackTreatmentCalculator fallbackTreatmentCalculator, + SdkEventsManager eventsManager) { _container = container; - _splitCache = checkNotNull(splitCache); + _eventsManager = eventsManager; + _splitCacheConsumer = checkNotNull(splitCacheConsumer); _impressionManager = checkNotNull(impressionManager); - _metrics = metrics; - _eventClient = eventClient; + _eventsStorageProducer = eventsStorageProducer; _config = config; _gates = checkNotNull(gates); _evaluator = checkNotNull(evaluator); + _telemetryEvaluationProducer = checkNotNull(telemetryEvaluationProducer); + _telemetryConfigProducer = checkNotNull(telemetryConfigProducer); + _flagSetsFilter = flagSetsFilter; + _fallbackTreatmentCalculator = fallbackTreatmentCalculator; + } + + @Override + public String getTreatment(String key, String featureFlagName) { + return getTreatment(key, featureFlagName, Collections.emptyMap()); + } + + @Override + public String getTreatment(String key, String featureFlagName, Map attributes) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, attributes, new EvaluationOptions(null), MethodEnum.TREATMENT).treatment(); + } + + @Override + public String getTreatment(Key key, String featureFlagName, Map attributes) { + return getTreatmentWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagName, attributes, new EvaluationOptions(null), + MethodEnum.TREATMENT).treatment(); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, Collections.emptyMap(), new EvaluationOptions(null), + MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, attributes, new EvaluationOptions(null), MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes) { + return getTreatmentWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagName, attributes, new EvaluationOptions(null), + MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public Map getTreatments(String key, List featureFlagNames) { + return getTreatments(key, featureFlagNames, Collections.emptyMap()); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, Map attributes) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS) + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatments(Key key, List featureFlagNames, Map attributes) { + return getTreatmentsWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagNames, attributes, + new EvaluationOptions(null), MethodEnum.TREATMENTS) + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, Collections.emptyMap(), new EvaluationOptions(null), + MethodEnum.TREATMENTS_WITH_CONFIG); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, attributes, + new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG); + } + + @Override + public Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes) { + return getTreatmentsWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagNames, attributes, new EvaluationOptions(null), + MethodEnum.TREATMENTS_WITH_CONFIG); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + null, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), new ArrayList<>(Arrays.asList(flagSet)), + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + null, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), flagSets, + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + null, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), new ArrayList<>(Arrays.asList(flagSet)), + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + null, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), flagSets, + attributes, new EvaluationOptions(null), MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); + } + + @Override + public String getTreatment(String key, String featureFlagName, EvaluationOptions evaluationOptions) { + return getTreatment(key, featureFlagName, Collections.emptyMap(), evaluationOptions); + } + + @Override + public String getTreatment(String key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, attributes, evaluationOptions, MethodEnum.TREATMENT).treatment(); + } + + @Override + public String getTreatment(Key key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions) { + return getTreatmentWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagName, attributes, evaluationOptions, + MethodEnum.TREATMENT).treatment(); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, + EvaluationOptions evaluationOptions) { + return getTreatments(key, featureFlagNames, Collections.emptyMap(), evaluationOptions); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, attributes, evaluationOptions, MethodEnum.TREATMENTS) + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatments(Key key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagNames, attributes, evaluationOptions, + MethodEnum.TREATMENTS) + .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, EvaluationOptions evaluationOptions) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, Collections.emptyMap(), evaluationOptions, + MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagName, attributes, evaluationOptions, + MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentWithConfigInternal(key, null, featureFlagName, attributes, evaluationOptions, + MethodEnum.TREATMENT_WITH_CONFIG); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, attributes, evaluationOptions, + MethodEnum.TREATMENTS_WITH_CONFIG); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfigInternal(key, null, featureFlagNames, null, evaluationOptions, + MethodEnum.TREATMENTS_WITH_CONFIG); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + attributes, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + null, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + attributes, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + null, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + attributes, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + null, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); } @Override - public String getTreatment(String key, String split) { - return getTreatment(key, split, Collections.emptyMap()); + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, flagSets, + attributes, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); } @Override - public String getTreatment(String key, String split, Map attributes) { - return getTreatmentWithConfigInternal(GET_TREATMENT, key, null, split, attributes).treatment(); + public Map getTreatmentsByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key, null, new ArrayList<>(Arrays.asList(flagSet)), + null, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); } @Override - public String getTreatment(Key key, String split, Map attributes) { - return getTreatmentWithConfigInternal(GET_TREATMENT, key.matchingKey(), key.bucketingKey(), split, attributes).treatment(); + public Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfigInternal(key.matchingKey(), key.bucketingKey(), featureFlagNames, attributes, evaluationOptions, + MethodEnum.TREATMENTS_WITH_CONFIG); } @Override - public SplitResult getTreatmentWithConfig(String key, String split) { - return getTreatmentWithConfigInternal(GET_TREATMENT_WITH_CONFIG, key, null, split, Collections.emptyMap()); + public Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes, EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), new ArrayList<>(Arrays.asList(flagSet)), + attributes, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SET).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); } @Override - public SplitResult getTreatmentWithConfig(String key, String split, Map attributes) { - return getTreatmentWithConfigInternal(GET_TREATMENT_WITH_CONFIG, key, null, split, attributes); + public Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), flagSets, + attributes, evaluationOptions, MethodEnum.TREATMENTS_BY_FLAG_SETS).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().treatment())); } @Override - public SplitResult getTreatmentWithConfig(Key key, String split, Map attributes) { - return getTreatmentWithConfigInternal(GET_TREATMENT_WITH_CONFIG, key.matchingKey(), key.bucketingKey(), split, attributes); + public Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), new ArrayList<>(Arrays.asList(flagSet)), + attributes, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsBySetsWithConfigInternal(key.matchingKey(), key.bucketingKey(), flagSets, + attributes, evaluationOptions, MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); } @Override @@ -132,8 +459,11 @@ public void blockUntilReady() throws TimeoutException, InterruptedException { if (_config.blockUntilReady() <= 0) { throw new IllegalArgumentException("setBlockUntilReadyTimeout must be positive but in config was: " + _config.blockUntilReady()); } - if (!_gates.isSDKReady(_config.blockUntilReady())) { - throw new TimeoutException("SDK was not ready in " + _config.blockUntilReady()+ " milliseconds"); + if (!_gates.waitUntilInternalReady(_config.blockUntilReady())) { + if (_eventsManager != null) { + _eventsManager.notifyInternalEvent(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED, null); + } + throw new TimeoutException("SDK was not ready in " + _config.blockUntilReady() + " milliseconds"); } _log.debug(String.format("Split SDK ready in %d ms", (System.currentTimeMillis() - startTime))); } @@ -143,14 +473,50 @@ public void destroy() { _container.destroy(); } + @Override + public void on(SdkEvent event, Runnable callback) { + if (callback == null) { + _log.warn("on: callback must not be null, the registration was ignored"); + return; + } + on(event, metadata -> callback.run()); + } + + @Override + public void on(SdkEvent event, SdkEventListener listener) { + if (event == null) { + _log.warn("on: event must not be null, the registration was ignored"); + return; + } + if (listener == null) { + _log.warn("on: listener must not be null, the registration was ignored"); + return; + } + if (_eventsManager != null) { + _eventsManager.on(event, listener); + } + } + + @Override + public void off(SdkEvent event) { + if (event == null) { + _log.warn("off: event must not be null, the call was ignored"); + return; + } + if (_eventsManager != null) { + _eventsManager.off(event); + } + } + private boolean track(Event event) { + long initTime = System.currentTimeMillis(); if (_container.isDestroyed()) { - _log.error("Client has already been destroyed - no calls possible"); + _log.error(CLIENT_DESTROY); return false; } // Traffic Type validations - Optional trafficTypeResult = TrafficTypeValidator.isValid(event.trafficTypeName, _splitCache, "track"); + Optional trafficTypeResult = TrafficTypeValidator.isValid(event.trafficTypeName, _splitCacheConsumer, "track"); if (!trafficTypeResult.isPresent()) { return false; } @@ -173,69 +539,242 @@ private boolean track(Event event) { } event.properties = propertiesResult.getValue(); + _telemetryEvaluationProducer.recordLatency(MethodEnum.TRACK, System.currentTimeMillis() - initTime); - return _eventClient.track(event, propertiesResult.getEventSize()); + return _eventsStorageProducer.track(event, propertiesResult.getEventSize()); } - private SplitResult getTreatmentWithConfigInternal(String method, String matchingKey, String bucketingKey, String split, Map attributes) { + private SplitResult getTreatmentWithConfigInternal(String matchingKey, String bucketingKey, String featureFlag, Map attributes, EvaluationOptions evaluationOptions, MethodEnum methodEnum) { + long initTime = System.currentTimeMillis(); try { + checkSDKReady(methodEnum, Arrays.asList(featureFlag)); + if (_container.isDestroyed()) { - _log.error("Client has already been destroyed - no calls possible"); - return SPLIT_RESULT_CONTROL; + _log.error(CLIENT_DESTROY); + return checkFallbackTreatment(featureFlag); } - if (!KeyValidator.isValid(matchingKey, "matchingKey", _config.maxStringLength(), method)) { - return SPLIT_RESULT_CONTROL; + if (!KeyValidator.isValid(matchingKey, MATCHING_KEY, _config.maxStringLength(), methodEnum.getMethod())) { + return checkFallbackTreatment(featureFlag); } - if (!KeyValidator.bucketingKeyIsValid(bucketingKey, _config.maxStringLength(), method)) { - return SPLIT_RESULT_CONTROL; + if (!KeyValidator.bucketingKeyIsValid(bucketingKey, _config.maxStringLength(), methodEnum.getMethod())) { + return checkFallbackTreatment(featureFlag); } - Optional splitNameResult = SplitNameValidator.isValid(split, method); + Optional splitNameResult = SplitNameValidator.isValid(featureFlag, methodEnum.getMethod()); if (!splitNameResult.isPresent()) { - return SPLIT_RESULT_CONTROL; + return checkFallbackTreatment(featureFlag); } - split = splitNameResult.get(); - + featureFlag = splitNameResult.get(); long start = System.currentTimeMillis(); - EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(matchingKey, bucketingKey, split, attributes); - - if (result.treatment.equals(Treatments.CONTROL) && result.label.equals(Labels.DEFINITION_NOT_FOUND) && _gates.isSDKReadyNow()) { - _log.warn( - "getTreatment: you passed \"" + split + "\" that does not exist in this environment, " + - "please double check what Splits exist in the web console."); + EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(matchingKey, bucketingKey, featureFlag, attributes); + String label = result.label; + if (result.label != null && result.label.contains(Labels.DEFINITION_NOT_FOUND)) { + if (_gates.isSDKReady()) { + _log.warn(String.format( + "%s: you passed \"%s\" that does not exist in this environment, " + + "please double check what feature flags exist in the Split user interface.", methodEnum.getMethod(), featureFlag)); + return checkFallbackTreatment(featureFlag); + } + label = result.label.replace(Labels.DEFINITION_NOT_FOUND, Labels.NOT_READY); } recordStats( matchingKey, bucketingKey, - split, + featureFlag, start, result.treatment, - String.format("sdk.%s", method), - _config.labelsEnabled() ? result.label : null, + String.format("sdk.%s", methodEnum.getMethod()), + _config.labelsEnabled() ? label : null, result.changeNumber, - attributes + attributes, + result.track, + validateProperties(evaluationOptions.getProperties()) ); - + _telemetryEvaluationProducer.recordLatency(methodEnum, System.currentTimeMillis() - initTime); return new SplitResult(result.treatment, result.configurations); } catch (Exception e) { try { - _log.error("CatchAll Exception", e); + _telemetryEvaluationProducer.recordException(methodEnum); + _log.error(CATCHALL_EXCEPTION, e); } catch (Exception e1) { // ignore } - return SPLIT_RESULT_CONTROL; + return checkFallbackTreatment(featureFlag); } } - private void recordStats(String matchingKey, String bucketingKey, String split, long start, String result, - String operation, String label, Long changeNumber, Map attributes) { + private SplitResult checkFallbackTreatment(String featureName) { + FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, ""); + String config = null; + if (fallbackTreatment.getConfig() != null) { + config = fallbackTreatment.getConfig(); + } + return new SplitResult(fallbackTreatment.getTreatment(), config); + } + + private String validateProperties(Map properties) { + if (properties == null){ + return null; + } + + ImpressionPropertiesValidator.ImpressionPropertiesValidatorResult iPValidatorResult = ImpressionPropertiesValidator.propertiesAreValid( + properties); + return new GsonBuilder().create().toJson(iPValidatorResult.getValue()); + } + + private Map getTreatmentsWithConfigInternal(String matchingKey, String bucketingKey, List featureFlagNames, + Map attributes, + EvaluationOptions evaluationOptions, MethodEnum methodEnum) { + long initTime = System.currentTimeMillis(); + if (featureFlagNames == null) { + _log.error(String.format("%s: featureFlagNames must be a non-empty array", methodEnum.getMethod())); + return new HashMap<>(); + } + try { - _impressionManager.track(new Impression(matchingKey, bucketingKey, split, result, System.currentTimeMillis(), label, changeNumber, attributes)); - _metrics.time(operation, System.currentTimeMillis() - start); + checkSDKReady(methodEnum, featureFlagNames); + Map result = validateBeforeEvaluate(featureFlagNames, matchingKey, methodEnum, bucketingKey); + if(result != null) { + return result; + } + featureFlagNames = SplitNameValidator.areValid(featureFlagNames, methodEnum.getMethod()); + Map evaluatorResult = _evaluator.evaluateFeatures(matchingKey, + bucketingKey, featureFlagNames, attributes); + + return processEvaluatorResult(evaluatorResult, methodEnum, matchingKey, bucketingKey, attributes, initTime, + validateProperties(evaluationOptions.getProperties())); + } catch (Exception e) { + try { + _telemetryEvaluationProducer.recordException(methodEnum); + _log.error(CATCHALL_EXCEPTION, e); + } catch (Exception e1) { + // ignore + } + return createMapControl(featureFlagNames); + } + } + + private Map getTreatmentsBySetsWithConfigInternal(String matchingKey, String bucketingKey, + List sets, Map attributes, + EvaluationOptions evaluationOptions, + MethodEnum methodEnum) { + + long initTime = System.currentTimeMillis(); + if (sets == null || sets.isEmpty()) { + _log.warn(String.format("%s: sets must be a non-empty array", methodEnum.getMethod())); + return new HashMap<>(); + } + Set cleanFlagSets = cleanup(sets); + cleanFlagSets = filterSetsAreInConfig(cleanFlagSets, methodEnum); + if (cleanFlagSets.isEmpty()) { + return new HashMap<>(); + } + checkSDKReady(methodEnum); + Map result = validateBeforeEvaluateByFlagSets(matchingKey, methodEnum,bucketingKey); + if(result != null) { + return result; + } + Map evaluatorResult = _evaluator.evaluateFeaturesByFlagSets(matchingKey, + bucketingKey, new ArrayList<>(cleanFlagSets), attributes); + + evaluatorResult.entrySet().forEach(flag -> { + if (flag.getValue().label != null && + flag.getValue().label.contains(io.split.engine.evaluator.Labels.EXCEPTION)) { + _telemetryEvaluationProducer.recordException(methodEnum); + } + }); + return processEvaluatorResult(evaluatorResult, methodEnum, matchingKey, bucketingKey, attributes, initTime, + validateProperties(evaluationOptions.getProperties())); + } + + private Map processEvaluatorResult(Map evaluatorResult, + MethodEnum methodEnum, String matchingKey, String bucketingKey, Map attributes, long initTime, String properties){ + List decoratedImpressions = new ArrayList<>(); + Map result = new HashMap<>(); + evaluatorResult.keySet().forEach(flag -> { + String label = evaluatorResult.get(flag).label; + if (evaluatorResult.get(flag).label != null && + evaluatorResult.get(flag).label.contains(Labels.DEFINITION_NOT_FOUND)) { + if (_gates.isSDKReady()) { + _log.warn(String.format("%s: you passed \"%s\" that does not exist in this environment please double check " + + "what feature flags exist in the Split user interface.", methodEnum.getMethod(), flag)); + result.put(flag, checkFallbackTreatment(flag)); + return; + } + label = evaluatorResult.get(flag).label.replace(Labels.DEFINITION_NOT_FOUND, Labels.NOT_READY); + } + result.put(flag, new SplitResult(evaluatorResult.get(flag).treatment, evaluatorResult.get(flag).configurations)); + decoratedImpressions.add( + new DecoratedImpression( + new Impression(matchingKey, bucketingKey, flag, evaluatorResult.get(flag).treatment, System.currentTimeMillis(), + label, evaluatorResult.get(flag).changeNumber, attributes, properties), + evaluatorResult.get(flag).track)); + }); + _telemetryEvaluationProducer.recordLatency(methodEnum, System.currentTimeMillis() - initTime); + if (!decoratedImpressions.isEmpty()) { + _impressionManager.track(decoratedImpressions); + } + return result; + } + + private Map validateBeforeEvaluateByFlagSets(String matchingKey, MethodEnum methodEnum, + String bucketingKey) { + if (_container.isDestroyed()) { + _log.error(CLIENT_DESTROY); + return new HashMap<>(); + } + if (!KeyValidator.isValid(matchingKey, MATCHING_KEY, _config.maxStringLength(), methodEnum.getMethod())) { + return new HashMap<>(); + } + if (!KeyValidator.bucketingKeyIsValid(bucketingKey, _config.maxStringLength(), methodEnum.getMethod())) { + return new HashMap<>(); + } + return null; + } + + private Map validateBeforeEvaluate(List featureFlagNames, String matchingKey, MethodEnum methodEnum, + String bucketingKey) { + if (_container.isDestroyed()) { + _log.error(CLIENT_DESTROY); + return createMapControl(featureFlagNames); + } + if (!KeyValidator.isValid(matchingKey, MATCHING_KEY, _config.maxStringLength(), methodEnum.getMethod())) { + return createMapControl(featureFlagNames); + } + if (!KeyValidator.bucketingKeyIsValid(bucketingKey, _config.maxStringLength(), methodEnum.getMethod())) { + return createMapControl(featureFlagNames); + } else if (featureFlagNames.isEmpty()) { + _log.error(String.format("%s: featureFlagNames must be a non-empty array", methodEnum.getMethod())); + return new HashMap<>(); + } + return null; + } + private Set filterSetsAreInConfig(Set sets, MethodEnum methodEnum) { + Set setsToReturn = new HashSet<>(); + for (String set : sets) { + if (!_flagSetsFilter.intersect(set)) { + _log.warn(String.format("%s: you passed %s which is not part of the configured FlagSetsFilter, " + + "ignoring Flag Set.", methodEnum, set)); + continue; + } + setsToReturn.add(set); + } + return setsToReturn; + } + private void recordStats(String matchingKey, String bucketingKey, String featureFlagName, long start, String result, + String operation, String label, Long changeNumber, Map attributes, boolean track, String properties) { + try { + _impressionManager.track(Stream.of( + new DecoratedImpression( + new Impression(matchingKey, bucketingKey, featureFlagName, result, System.currentTimeMillis(), + label, changeNumber, attributes, properties), + track)).collect(Collectors.toList())); } catch (Throwable t) { _log.error("Exception", t); } @@ -249,4 +788,27 @@ private Event createEvent(String key, String trafficType, String eventType) { event.timestamp = System.currentTimeMillis(); return event; } -} + + private void checkSDKReady(MethodEnum methodEnum, List featureFlagNames) { + String toPrint = featureFlagNames.size() == 1 ? featureFlagNames.get(0): String.join(",", featureFlagNames); + if (!_gates.isSDKReady()) { + _log.warn(String.format("%s: the SDK is not ready, results may be incorrect for feature flag %s. Make sure to wait for " + + "SDK readiness before using this method", methodEnum.getMethod(), toPrint)); + _telemetryConfigProducer.recordNonReadyUsage(); + } + } + + private void checkSDKReady(MethodEnum methodEnum) { + if (!_gates.isSDKReady()) { + _log.warn(String.format("%s: the SDK is not ready, results may be incorrect. Make sure to wait for " + + "SDK readiness before using this method", methodEnum.getMethod())); + _telemetryConfigProducer.recordNonReadyUsage(); + } + } + + private Map createMapControl(List featureFlags) { + Map result = new HashMap<>(); + featureFlags.forEach(s -> result.put(s, checkFallbackTreatment(s))); + return result; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/SplitFactoryBuilder.java b/client/src/main/java/io/split/client/SplitFactoryBuilder.java index f1803241..c2271ec4 100644 --- a/client/src/main/java/io/split/client/SplitFactoryBuilder.java +++ b/client/src/main/java/io/split/client/SplitFactoryBuilder.java @@ -2,6 +2,7 @@ import io.split.inputValidation.ApiKeyValidator; import io.split.grammar.Treatments; +import io.split.storages.enums.StorageMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,13 +10,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URISyntaxException; -import java.util.concurrent.TimeoutException; /** * Builds an instance of SplitClient. */ public class SplitFactoryBuilder { private static final Logger _log = LoggerFactory.getLogger(SplitFactoryBuilder.class); + static final String LOCALHOST = "localhost"; + /** * Instantiates a SplitFactory with default config @@ -38,35 +40,16 @@ public static SplitFactory build(String apiToken) throws IOException, URISyntaxE */ public static synchronized SplitFactory build(String apiToken, SplitClientConfig config) throws IOException, URISyntaxException { ApiKeyValidator.validate(apiToken); - - if (LocalhostSplitFactory.LOCALHOST.equals(apiToken)) { - return LocalhostSplitFactory.createLocalhostSplitFactory(config); - } else { - return new SplitFactoryImpl(apiToken, config); - + if (LOCALHOST.equals(apiToken)) { + return new SplitFactoryImpl(config); } + if (StorageMode.PLUGGABLE.equals(config.storageMode()) || StorageMode.REDIS.equals(config.storageMode())){ + return new SplitFactoryImpl(apiToken, config, config.customStorageWrapper()); + } + return new SplitFactoryImpl(apiToken, config); } - /** - * Instantiates a local Off-The-Grid SplitFactory - * - * @throws IOException if there were problems reading the override file from disk. - */ - public static SplitFactory local() throws IOException, URISyntaxException { - return LocalhostSplitFactory.createLocalhostSplitFactory(SplitClientConfig.builder().build()); - } - - /** - * Instantiates a local Off-The-Grid SplitFactory - * - * @return config Split config file - * @throws IOException if there were problems reading the override file from disk. - */ - public static SplitFactory local(SplitClientConfig config) throws IOException, URISyntaxException { - return LocalhostSplitFactory.createLocalhostSplitFactory(config); - } - - public static void main(String... args) throws IOException, InterruptedException, TimeoutException, URISyntaxException { + public static void main(String... args) throws IOException, URISyntaxException { if (args.length != 1) { System.out.println("Usage: "); System.exit(1); @@ -98,4 +81,4 @@ public static void main(String... args) throws IOException, InterruptedException _log.error(io.getMessage(), io); } } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/SplitFactoryImpl.java b/client/src/main/java/io/split/client/SplitFactoryImpl.java index 8b576d0d..1da1d1f8 100644 --- a/client/src/main/java/io/split/client/SplitFactoryImpl.java +++ b/client/src/main/java/io/split/client/SplitFactoryImpl.java @@ -1,31 +1,102 @@ package io.split.client; +import com.google.common.io.Files; +import io.split.client.dtos.BearerCredentialsProvider; +import io.split.client.dtos.FallbackTreatmentCalculatorImp; +import io.split.client.dtos.Metadata; +import io.split.client.events.EventsSender; +import io.split.client.events.EventsStorage; +import io.split.client.events.EventsTask; +import io.split.client.events.InMemoryEventsStorage; +import io.split.client.events.NoopEventsStorageImp; import io.split.client.impressions.AsynchronousImpressionListener; +import io.split.client.impressions.HttpImpressionsSender; +import io.split.client.impressions.ImpressionCounter; import io.split.client.impressions.ImpressionListener; +import io.split.client.impressions.ImpressionObserver; +import io.split.client.impressions.ImpressionsManager; import io.split.client.impressions.ImpressionsManagerImpl; -import io.split.client.interceptors.AddSplitHeadersFilter; +import io.split.client.impressions.ImpressionsSender; +import io.split.client.impressions.ImpressionsStorage; +import io.split.client.impressions.ImpressionsStorageConsumer; +import io.split.client.impressions.ImpressionsStorageProducer; +import io.split.client.impressions.InMemoryImpressionsStorage; +import io.split.client.impressions.PluggableImpressionSender; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.client.impressions.UniqueKeysTrackerImp; +import io.split.client.impressions.strategy.ProcessImpressionDebug; +import io.split.client.impressions.strategy.ProcessImpressionNone; +import io.split.client.impressions.strategy.ProcessImpressionOptimized; +import io.split.client.impressions.strategy.ProcessImpressionStrategy; +import io.split.client.interceptors.ClientKeyInterceptorFilter; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; import io.split.client.interceptors.GzipDecoderResponseInterceptor; import io.split.client.interceptors.GzipEncoderRequestInterceptor; -import io.split.client.metrics.CachedMetrics; -import io.split.client.metrics.FireAndForgetMetrics; -import io.split.client.metrics.HttpMetrics; -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SplitCache; -import io.split.engine.evaluator.Evaluator; -import io.split.engine.evaluator.EvaluatorImp; +import io.split.client.interceptors.SdkMetadataInterceptorFilter; +import io.split.client.utils.FileInputStreamProvider; +import io.split.client.utils.FileTypeEnum; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.SDKMetadata; +import io.split.client.utils.StaticContentInputStreamProvider; import io.split.engine.SDKReadinessGates; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsManager; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.common.ConsumerSyncManager; +import io.split.engine.common.ConsumerSynchronizer; +import io.split.engine.common.LocalhostSyncManager; +import io.split.engine.common.LocalhostSynchronizer; +import io.split.engine.common.SplitAPI; +import io.split.engine.common.SplitTasks; import io.split.engine.common.SyncManager; import io.split.engine.common.SyncManagerImp; +import io.split.engine.common.Synchronizer; +import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluatorImp; import io.split.engine.experiments.SplitChangeFetcher; import io.split.engine.experiments.SplitFetcher; import io.split.engine.experiments.SplitFetcherImp; import io.split.engine.experiments.SplitParser; import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.experiments.RuleBasedSegmentParser; import io.split.engine.segments.SegmentChangeFetcher; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; import io.split.engine.segments.SegmentSynchronizationTaskImp; import io.split.integrations.IntegrationsConfig; +import io.split.service.SplitHttpClientImpl; +import io.split.service.SplitHttpClient; + +import io.split.storages.SegmentCache; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.SplitCache; +import io.split.storages.SplitCacheConsumer; +import io.split.storages.SplitCacheProducer; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.enums.OperationMode; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.pluggable.adapters.UserCustomEventAdapterProducer; +import io.split.storages.pluggable.adapters.UserCustomImpressionAdapterConsumer; +import io.split.storages.pluggable.adapters.UserCustomImpressionAdapterProducer; +import io.split.storages.pluggable.adapters.UserCustomSegmentAdapterConsumer; +import io.split.storages.pluggable.adapters.UserCustomSplitAdapterConsumer; +import io.split.storages.pluggable.adapters.UserCustomTelemetryAdapterProducer; +import io.split.storages.pluggable.adapters.UserCustomRuleBasedSegmentAdapterConsumer; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.synchronizer.TelemetryConsumerSubmitter; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.NoopTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.storage.TelemetryStorageProducer; +import io.split.telemetry.synchronizer.TelemetryInMemorySubmitter; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; + import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.Credentials; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; @@ -40,125 +111,408 @@ import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder; +import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.http.ssl.TLS; import org.apache.hc.core5.ssl.SSLContexts; +import org.apache.hc.core5.util.TimeValue; import org.apache.hc.core5.util.Timeout; -import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; +import javax.net.ssl.SSLContext; import java.io.IOException; +import java.io.InputStream; +import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.concurrent.TimeUnit; +import java.security.KeyStore; +import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; +import java.util.HashSet; +import java.util.List; +import java.util.ArrayList; + +import static io.split.client.utils.SplitExecutorFactory.buildExecutorService; public class SplitFactoryImpl implements SplitFactory { - private static final Logger _log = LoggerFactory.getLogger(SplitFactory.class); + private static final org.slf4j.Logger _log = LoggerFactory.getLogger(SplitFactoryImpl.class); + private static final String LEGACY_LOG_MESSAGE = "The sdk initialize in localhost mode using Legacy file. The splitFile or " + + + "inputStream are not added to the config."; private final static long SSE_CONNECT_TIMEOUT = 30000; private final static long SSE_SOCKET_TIMEOUT = 70000; - private static Random RANDOM = new Random(); - - private final URI _rootTarget; - private final URI _eventsRootTarget; - private final CloseableHttpClient _httpclient; private final SDKReadinessGates _gates; - private final HttpMetrics _httpMetrics; - private final FireAndForgetMetrics _unCachedFireAndForget; - private final SegmentSynchronizationTaskImp _segmentSynchronizationTaskImp; - private final SplitFetcher _splitFetcher; - private final SplitSynchronizationTask _splitSynchronizationTask; - private final ImpressionsManagerImpl _impressionsManager; - private final FireAndForgetMetrics _cachedFireAndForgetMetrics; - private final EventClient _eventClient; - private final SyncManager _syncManager; + private final ImpressionsManager _impressionsManager; private final Evaluator _evaluator; private final String _apiToken; - // Caches - private final SegmentCache _segmentCache; - private final SplitCache _splitCache; - // Client and Manager private final SplitClient _client; private final SplitManager _manager; + // Cache + private final SplitCacheConsumer _splitCache; + private final SegmentCacheConsumer _segmentCache; + private boolean isTerminated = false; private final ApiKeyCounter _apiKeyCounter; - - public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyntaxException { + private final TelemetryStorageProducer _telemetryStorageProducer; + private final TelemetrySynchronizer _telemetrySynchronizer; + private long _startTime; + private final SDKMetadata _sdkMetadata; + private OperationMode _operationMode; + + // Depending on mode are not mandatory + private final TelemetrySyncTask _telemetrySyncTask; + private final SegmentSynchronizationTaskImp _segmentSynchronizationTaskImp; + private final SplitFetcher _splitFetcher; + private final SplitSynchronizationTask _splitSynchronizationTask; + private final EventsTask _eventsTask; + private final SyncManager _syncManager; + private final SdkEventsManager _eventsManager; + private SplitHttpClient _splitHttpClient; + private final UserStorageWrapper _userStorageWrapper; + private final ImpressionsSender _impressionsSender; + private final URI _rootTarget; + private final URI _eventsRootTarget; + private final UniqueKeysTracker _uniqueKeysTracker; + private RequestDecorator _requestDecorator; + + // Constructor for standalone mode + public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyntaxException, IOException { + _userStorageWrapper = null; + _operationMode = config.operationMode(); + _startTime = System.currentTimeMillis(); _apiToken = apiToken; _apiKeyCounter = ApiKeyCounter.getApiKeyCounterInstance(); _apiKeyCounter.add(apiToken); + _sdkMetadata = createSdkMetadata(config.ipAddressEnabled(), SplitClientConfig.splitSdkVersion); - if (config.blockUntilReady() == -1) { - //BlockUntilReady not been set - _log.warn("no setBlockUntilReadyTimeout parameter has been set - incorrect control treatments could be logged” " + - "if no ready config has been set when building factory"); + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + _telemetryStorageProducer = telemetryStorage; + if (config.blockUntilReady() == -1) { + // BlockUntilReady not been set + _log.warn( + "no setBlockUntilReadyTimeout parameter has been set - incorrect control treatments could be logged” " + + + "if no ready config has been set when building factory"); } // SDKReadinessGates _gates = new SDKReadinessGates(); + _requestDecorator = new RequestDecorator(config.customHeaderDecorator()); // HttpClient - _httpclient = buildHttpClient(apiToken, config); + if (config.alternativeHTTPModule() == null) { + _splitHttpClient = buildSplitHttpClient(apiToken, config, _sdkMetadata, _requestDecorator); + } else { + _splitHttpClient = config.alternativeHTTPModule().createClient(apiToken, _sdkMetadata, _requestDecorator); + } // Roots _rootTarget = URI.create(config.endpoint()); _eventsRootTarget = URI.create(config.eventsEndpoint()); - // HttpMetrics - _httpMetrics = HttpMetrics.create(_httpclient, _eventsRootTarget); - // Cache Initialisations - _segmentCache = new SegmentCacheInMemoryImpl(); - _splitCache = new InMemoryCacheImp(); - - // Metrics - _unCachedFireAndForget = FireAndForgetMetrics.instance(_httpMetrics, 2, 1000); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(config.getSetsFilter()); + SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter); + ImpressionsStorage impressionsStorage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + _splitCache = splitCache; + _segmentCache = segmentCache; + _telemetrySynchronizer = new TelemetryInMemorySubmitter(_splitHttpClient, URI.create(config.telemetryURL()), + telemetryStorage, + splitCache, _segmentCache, telemetryStorage, _startTime); + + // Events manager: must exist (dispatchers registered) before any sync task can start, so SDK_READY can't be lost. + _eventsManager = SdkEventsManager.create(config.getThreadFactory()); + SdkEventsNotifier notifier = _eventsManager::notifyInternalEvent; // Segments - _segmentSynchronizationTaskImp = buildSegments(config); + _segmentSynchronizationTaskImp = buildSegments(config, segmentCache, splitCache, ruleBasedSegmentCache, notifier); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); // SplitFetcher - _splitFetcher = buildSplitFetcher(); + _splitFetcher = buildSplitFetcher(splitCache, splitParser, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCache, config.isSdkEndpointOverridden(), notifier); // SplitSynchronizationTask - _splitSynchronizationTask = new SplitSynchronizationTask(_splitFetcher, _splitCache, findPollingPeriod(RANDOM, config.featuresRefreshRate())); + _splitSynchronizationTask = new SplitSynchronizationTask(_splitFetcher, + splitCache, + config.featuresRefreshRate(), + config.getThreadFactory()); - // Impressions - _impressionsManager = buildImpressionsManager(config); + // ImpressionSender + _impressionsSender = HttpImpressionsSender.create(_splitHttpClient, URI.create(config.eventsEndpoint()), + config.impressionsMode(), + _telemetryStorageProducer); - // CachedFireAndForgetMetrics - _cachedFireAndForgetMetrics = buildCachedFireAndForgetMetrics(config); + // UniqueKeysTracker + _uniqueKeysTracker = createUniqueKeysTracker(config); + + // Impressions + _impressionsManager = buildImpressionsManager(config, impressionsStorage, impressionsStorage); // EventClient - _eventClient = EventClientImpl.create(_httpclient, _eventsRootTarget, config.eventsQueueSize(), config.eventFlushIntervalInMillis(), config.waitBeforeShutdown()); + EventsStorage eventsStorage = new InMemoryEventsStorage(config.eventsQueueSize(), _telemetryStorageProducer); + EventsSender eventsSender = EventsSender.create(_splitHttpClient, _eventsRootTarget, _telemetryStorageProducer); + _eventsTask = EventsTask.create(config.eventSendIntervalInMillis(), eventsStorage, eventsSender, + config.getThreadFactory()); + _telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer, + config.getThreadFactory()); + + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(config.fallbackTreatments()); + // Evaluator + _evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, fallbackTreatmentCalculatorImp); + + // SplitClient + _client = new SplitClientImpl(this, + splitCache, + _impressionsManager, + eventsStorage, + config, + _gates, + _evaluator, + _telemetryStorageProducer, // TelemetryEvaluation instance + _telemetryStorageProducer, // TelemetryConfiguration instance + flagSetsFilter, + fallbackTreatmentCalculatorImp, + _eventsManager + ); + + // SplitManager + _manager = new SplitManagerImpl(splitCache, config, _gates, _telemetryStorageProducer, notifier); + + // SyncManager + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = SplitAPI.build(_splitHttpClient, buildSSEdHttpClient(apiToken, config, _sdkMetadata), + _requestDecorator); + + _syncManager = SyncManagerImp.build(splitTasks, _splitFetcher, splitCache, splitAPI, + segmentCache, _gates, _telemetryStorageProducer, _telemetrySynchronizer, config, splitParser, + ruleBasedSegmentParser, flagSetsFilter, ruleBasedSegmentCache, notifier); + _syncManager.start(); + + // DestroyOnShutDown + if (config.destroyOnShutDown()) { + Thread shutdown = new Thread(() -> { + // Using the full path to avoid conflicting with Thread.destroy() + SplitFactoryImpl.this.destroy(); + }); + shutdown.setName("split-destroy-worker"); + Runtime.getRuntime().addShutdownHook(shutdown); + } + } + + // Constructor for consumer mode + protected SplitFactoryImpl(String apiToken, SplitClientConfig config, CustomStorageWrapper customStorageWrapper) + throws URISyntaxException { + // Variables that are not used in Consumer mode. + _segmentSynchronizationTaskImp = null; + _splitFetcher = null; + _splitSynchronizationTask = null; + _eventsTask = null; + _splitHttpClient = null; + _rootTarget = null; + _eventsRootTarget = null; + + Metadata metadata = new Metadata(config.ipAddressEnabled(), SplitClientConfig.splitSdkVersion); + _userStorageWrapper = new UserStorageWrapper(customStorageWrapper); + UserCustomSegmentAdapterConsumer userCustomSegmentAdapterConsumer = new UserCustomSegmentAdapterConsumer( + customStorageWrapper); + UserCustomSplitAdapterConsumer userCustomSplitAdapterConsumer = new UserCustomSplitAdapterConsumer( + customStorageWrapper); + // TODO migrate impressions sender to Task instead manager and not instantiate + // Producer here. + UserCustomImpressionAdapterConsumer userCustomImpressionAdapterConsumer = new UserCustomImpressionAdapterConsumer(); + UserCustomImpressionAdapterProducer userCustomImpressionAdapterProducer = new UserCustomImpressionAdapterProducer( + customStorageWrapper, + metadata); + UserCustomEventAdapterProducer userCustomEventAdapterProducer = new UserCustomEventAdapterProducer( + customStorageWrapper, metadata); + + _operationMode = config.operationMode(); + _sdkMetadata = createSdkMetadata(config.ipAddressEnabled(), SplitClientConfig.splitSdkVersion); + _startTime = System.currentTimeMillis(); + _apiToken = apiToken; + _apiKeyCounter = ApiKeyCounter.getApiKeyCounterInstance(); + _apiKeyCounter.add(apiToken); + _telemetryStorageProducer = new UserCustomTelemetryAdapterProducer(customStorageWrapper, _sdkMetadata); + + _splitCache = userCustomSplitAdapterConsumer; + _segmentCache = userCustomSegmentAdapterConsumer; + + if (config.blockUntilReady() == -1) { + // BlockUntilReady not been set + _log.warn( + "no setBlockUntilReadyTimeout parameter has been set - incorrect control treatments could be logged” " + + + "if no ready config has been set when building factory"); + } + + // SDKReadinessGates + _gates = new SDKReadinessGates(); + + // Events manager. Consumer mode has no sync engine, so nothing is wired to notify it: SDK_UPDATE is not + // supported here (spec p.3) and only SDK_READY is produced (FME-5). + _eventsManager = SdkEventsManager.create(config.getThreadFactory()); + + _telemetrySynchronizer = new TelemetryConsumerSubmitter(customStorageWrapper, _sdkMetadata); + UserCustomRuleBasedSegmentAdapterConsumer userCustomRuleBasedSegmentAdapterConsumer = + new UserCustomRuleBasedSegmentAdapterConsumer(customStorageWrapper); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(config.fallbackTreatments()); + _evaluator = new EvaluatorImp(userCustomSplitAdapterConsumer, userCustomSegmentAdapterConsumer, + userCustomRuleBasedSegmentAdapterConsumer, fallbackTreatmentCalculatorImp); + _impressionsSender = PluggableImpressionSender.create(customStorageWrapper); + _uniqueKeysTracker = createUniqueKeysTracker(config); + _impressionsManager = buildImpressionsManager(config, userCustomImpressionAdapterConsumer, + userCustomImpressionAdapterProducer); + _telemetrySyncTask = new TelemetrySyncTask(config.getTelemetryRefreshRate(), _telemetrySynchronizer, + config.getThreadFactory()); + + SplitTasks splitTasks = SplitTasks.build(null, null, + _impressionsManager, null, _telemetrySyncTask, _uniqueKeysTracker); + + // Synchronizer + Synchronizer synchronizer = new ConsumerSynchronizer(splitTasks); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + if (!config.getSetsFilter().isEmpty()) { + _log.warn( + "FlagSets filter is not applicable for Consumer modes where the SDK does not keep rollout data in sync. FlagSet " + + + "filter was discarded"); + } + _client = new SplitClientImpl(this, + userCustomSplitAdapterConsumer, + _impressionsManager, + userCustomEventAdapterProducer, + config, + _gates, + _evaluator, + _telemetryStorageProducer, // TelemetryEvaluation instance + _telemetryStorageProducer, // TelemetryConfiguration instance + flagSetsFilter, + fallbackTreatmentCalculatorImp, + _eventsManager + ); // SyncManager - _syncManager = SyncManagerImp.build(config.streamingEnabled(), _splitSynchronizationTask, _splitFetcher, _segmentSynchronizationTaskImp, _splitCache, config.authServiceURL(), _httpclient, config.streamingServiceURL(), config.authRetryBackoffBase(), buildSSEdHttpClient(config), _segmentCache); + _syncManager = new ConsumerSyncManager(synchronizer); _syncManager.start(); + _manager = new SplitManagerImpl(userCustomSplitAdapterConsumer, config, _gates, _telemetryStorageProducer, + _eventsManager::notifyInternalEvent); + manageSdkReady(config); + } + + // Localhost + protected SplitFactoryImpl(SplitClientConfig config) { + _userStorageWrapper = null; + _apiToken = "localhost"; + _apiKeyCounter = ApiKeyCounter.getApiKeyCounterInstance(); + _apiKeyCounter.add("localhost"); + _sdkMetadata = createSdkMetadata(config.ipAddressEnabled(), SplitClientConfig.splitSdkVersion); + _telemetrySynchronizer = null; + _telemetrySyncTask = null; + _eventsTask = null; + _splitHttpClient = null; + _impressionsSender = null; + _rootTarget = null; + _eventsRootTarget = null; + _uniqueKeysTracker = null; + _telemetryStorageProducer = new NoopTelemetryStorage(); + + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(config.getSetsFilter()); + SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + _splitCache = splitCache; + _gates = new SDKReadinessGates(); + _segmentCache = segmentCache; + + // Events manager: constructed before any sync task starts. + _eventsManager = SdkEventsManager.create(config.getThreadFactory()); + SdkEventsNotifier notifier = _eventsManager::notifyInternalEvent; + + // SegmentFetcher + + SegmentChangeFetcher segmentChangeFetcher = new LocalhostSegmentFetcherNoop(); + if (config.segmentDirectory() != null) { + segmentChangeFetcher = new LocalhostSegmentChangeFetcher(config.segmentDirectory()); + } + + _segmentSynchronizationTaskImp = new SegmentSynchronizationTaskImp(segmentChangeFetcher, + config.segmentsRefreshRate(), + config.numThreadsForSegmentFetch(), + segmentCache, + _telemetryStorageProducer, + _splitCache, + config.getThreadFactory(), + ruleBasedSegmentCache, + notifier); + + // SplitFetcher + SplitChangeFetcher splitChangeFetcher = createSplitChangeFetcher(config); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + _splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer, + flagSetsFilter, ruleBasedSegmentParser, ruleBasedSegmentCache, notifier); + + // SplitSynchronizationTask + _splitSynchronizationTask = new SplitSynchronizationTask(_splitFetcher, splitCache, + config.featuresRefreshRate(), config.getThreadFactory()); + + _impressionsManager = new ImpressionsManager.NoOpImpressionsManager(); + + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, null, null, null); + + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(config.fallbackTreatments()); // Evaluator - _evaluator = new EvaluatorImp(_splitCache); + _evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, fallbackTreatmentCalculatorImp); + + EventsStorage eventsStorage = new NoopEventsStorageImp(); // SplitClient - _client = new SplitClientImpl(this, _splitCache, _impressionsManager, _cachedFireAndForgetMetrics, _eventClient, config, _gates, _evaluator); + _client = new SplitClientImpl(this, + splitCache, + _impressionsManager, + eventsStorage, + config, + _gates, + _evaluator, + _telemetryStorageProducer, // TelemetryEvaluation instance + _telemetryStorageProducer, // TelemetryConfiguration instance + flagSetsFilter, + fallbackTreatmentCalculatorImp, + _eventsManager + ); + + // Synchronizer + Synchronizer synchronizer = new LocalhostSynchronizer(splitTasks, _splitFetcher, + config.localhostRefreshEnabled()); // SplitManager - _manager = new SplitManagerImpl(_splitCache, config, _gates); + _manager = new SplitManagerImpl(splitCache, config, _gates, _telemetryStorageProducer, notifier); + // SyncManager + _syncManager = new LocalhostSyncManager(synchronizer, _gates, notifier); + _syncManager.start(); // DestroyOnShutDown if (config.destroyOnShutDown()) { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { + Thread shutdown = new Thread(() -> { // Using the full path to avoid conflicting with Thread.destroy() SplitFactoryImpl.this.destroy(); - })); + }); + shutdown.setName("split-destroy-worker"); + Runtime.getRuntime().addShutdownHook(shutdown); } } @@ -174,31 +528,24 @@ public SplitManager manager() { @Override public synchronized void destroy() { - if (!isTerminated) { + if (isTerminated) { + return; + } + try { _log.info("Shutdown called for split"); - try { - _segmentSynchronizationTaskImp.close(); - _log.info("Successful shutdown of segment fetchers"); - _splitSynchronizationTask.close(); - _log.info("Successful shutdown of splits"); - _impressionsManager.close(); - _log.info("Successful shutdown of impressions manager"); - _unCachedFireAndForget.close(); - _log.info("Successful shutdown of metrics 1"); - _cachedFireAndForgetMetrics.close(); - _log.info("Successful shutdown of metrics 2"); - _httpclient.close(); - _log.info("Successful shutdown of httpclient"); - _eventClient.close(); - _log.info("Successful shutdown of eventClient"); - _syncManager.shutdown(); - _log.info("Successful shutdown of syncManager"); - } catch (IOException e) { - _log.error("We could not shutdown split", e); + _eventsManager.destroy(); + _syncManager.shutdown(); + _log.info("Successful shutdown of syncManager"); + if (OperationMode.STANDALONE.equals(_operationMode)) { + _telemetryStorageProducer.recordSessionLength(System.currentTimeMillis() - _startTime); + } else if (OperationMode.CONSUMER.equals(_operationMode)) { + _userStorageWrapper.disconnect(); } - _apiKeyCounter.remove(_apiToken); - isTerminated = true; + } catch (IOException e) { + _log.error("We could not shutdown split", e); } + _apiKeyCounter.remove(_apiToken); + isTerminated = true; } @Override @@ -206,10 +553,14 @@ public boolean isDestroyed() { return isTerminated; } - private static CloseableHttpClient buildHttpClient(String apiToken, SplitClientConfig config) { + protected static SplitHttpClient buildSplitHttpClient(String apiToken, SplitClientConfig config, + SDKMetadata sdkMetadata, RequestDecorator requestDecorator) + throws URISyntaxException, IOException { + + SSLContext sslContext = buildSSLContext(config); SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create() - .setSslContext(SSLContexts.createSystemDefault()) + .setSslContext(sslContext) .setTlsVersions(TLS.V_1_1, TLS.V_1_2) .build(); @@ -223,6 +574,7 @@ private static CloseableHttpClient buildHttpClient(String apiToken, SplitClientC .setDefaultSocketConfig(SocketConfig.custom() .setSoTimeout(Timeout.ofMilliseconds(config.readTimeout())) .build()) + .setValidateAfterInactivity(TimeValue.ofMilliseconds(config.validateAfterInactivityInMillis())) .build(); cm.setMaxTotal(20); cm.setDefaultMaxPerRoute(20); @@ -230,25 +582,30 @@ private static CloseableHttpClient buildHttpClient(String apiToken, SplitClientC HttpClientBuilder httpClientbuilder = HttpClients.custom() .setConnectionManager(cm) .setDefaultRequestConfig(requestConfig) - .addRequestInterceptorLast(AddSplitHeadersFilter.instance(apiToken, config.ipAddressEnabled())) .addRequestInterceptorLast(new GzipEncoderRequestInterceptor()) .addResponseInterceptorLast((new GzipDecoderResponseInterceptor())); // Set up proxy is it exists - if (config.proxy() != null) { + if (config.proxy() != null || config.proxyConfiguration() != null) { httpClientbuilder = setupProxy(httpClientbuilder, config); } - return httpClientbuilder.build(); + return SplitHttpClientImpl.create(httpClientbuilder.build(), + requestDecorator, + apiToken, + sdkMetadata); } - private static CloseableHttpClient buildSSEdHttpClient(SplitClientConfig config) { + private static CloseableHttpClient buildSSEdHttpClient(String apiToken, SplitClientConfig config, + SDKMetadata sdkMetadata) throws IOException { RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(Timeout.ofMilliseconds(SSE_CONNECT_TIMEOUT)) .build(); + SSLContext sslContext = buildSSLContext(config); + SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create() - .setSslContext(SSLContexts.createSystemDefault()) + .setSslContext(sslContext) .setTlsVersions(TLS.V_1_1, TLS.V_1_2) .build(); @@ -263,56 +620,130 @@ private static CloseableHttpClient buildSSEdHttpClient(SplitClientConfig config) HttpClientBuilder httpClientbuilder = HttpClients.custom() .setConnectionManager(cm) - .setDefaultRequestConfig(requestConfig); + .setDefaultRequestConfig(requestConfig) + .addRequestInterceptorLast(SdkMetadataInterceptorFilter.instance(sdkMetadata)) + .addRequestInterceptorLast(ClientKeyInterceptorFilter.instance(apiToken)); // Set up proxy is it exists - if (config.proxy() != null) { + if (config.proxy() != null || config.proxyConfiguration() != null) { httpClientbuilder = setupProxy(httpClientbuilder, config); } return httpClientbuilder.build(); } + private static SSLContext buildSSLContext(SplitClientConfig config) throws IOException, NullPointerException { + SSLContext sslContext; + if (config.proxyConfiguration() != null && config.proxyConfiguration().getP12File() != null) { + _log.debug("Proxy setup using mTLS"); + InputStream keystoreStream = null; + try { + KeyStore keyStore = KeyStore.getInstance("PKCS12"); + keystoreStream = config.proxyConfiguration().getP12File(); + keyStore.load(keystoreStream, config.proxyConfiguration().getPassKey().toCharArray()); + sslContext = SSLContexts.custom() + .loadKeyMaterial(keyStore, config.proxyConfiguration().getPassKey().toCharArray()) + .build(); + } catch (Exception e) { + _log.error("Exception caught while processing p12 file for Proxy mTLS auth: ", e); + _log.warn("Ignoring p12 mTLS config and switching to default context"); + sslContext = SSLContexts.createSystemDefault(); + } finally { + if (keystoreStream != null) { + keystoreStream.close(); + } + } + } else { + sslContext = SSLContexts.createSystemDefault(); + } + return sslContext; + } + private static HttpClientBuilder setupProxy(HttpClientBuilder httpClientbuilder, SplitClientConfig config) { _log.info("Initializing Split SDK with proxy settings"); - DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(config.proxy()); - httpClientbuilder.setRoutePlanner(routePlanner); + if (config.proxyConfiguration() != null) { + return useProxyConfiguration(httpClientbuilder, config); + } else { + return useLegacyProxyConfiguration(httpClientbuilder, config); + } + } + private static HttpClientBuilder useLegacyProxyConfiguration(HttpClientBuilder httpClientbuilder, SplitClientConfig config) { + HttpHost proxyHost = config.proxy(); + addProxyHost(httpClientbuilder, proxyHost); if (config.proxyUsername() != null && config.proxyPassword() != null) { - _log.debug("Proxy setup using credentials"); - BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); - AuthScope siteScope = new AuthScope(config.proxy().getHostName(), config.proxy().getPort()); - Credentials siteCreds = new UsernamePasswordCredentials(config.proxyUsername(), config.proxyPassword().toCharArray()); - credsProvider.setCredentials(siteScope, siteCreds); - httpClientbuilder.setDefaultCredentialsProvider(credsProvider); + return addProxyBasicAuth(httpClientbuilder, proxyHost, config.proxyUsername(), config.proxyPassword()); + } + + return httpClientbuilder; + } + + private static HttpClientBuilder useProxyConfiguration(HttpClientBuilder httpClientbuilder, SplitClientConfig config) { + HttpHost proxyHost = config.proxyConfiguration().getHost(); + addProxyHost(httpClientbuilder, proxyHost); + if (config.proxyConfiguration().getProxyCredentialsProvider() == null) { + return httpClientbuilder; + } + + if (config.proxyConfiguration().getProxyCredentialsProvider() instanceof io.split.client.dtos.BasicCredentialsProvider) { + io.split.client.dtos.BasicCredentialsProvider basicAuth = + (io.split.client.dtos.BasicCredentialsProvider) config.proxyConfiguration().getProxyCredentialsProvider(); + return addProxyBasicAuth(httpClientbuilder, proxyHost, basicAuth.getUsername(), basicAuth.getPassword()); } - return httpClientbuilder; + _log.debug("Proxy setup using Bearer token"); + httpClientbuilder.setDefaultCredentialsProvider(new HttpClientDynamicCredentials( + (BearerCredentialsProvider) config.proxyConfiguration().getProxyCredentialsProvider())); + return httpClientbuilder; + } + + private static void addProxyHost(HttpClientBuilder httpClientbuilder, HttpHost proxyHost) { + DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost); + httpClientbuilder.setRoutePlanner(routePlanner); } - private static int findPollingPeriod(Random rand, int max) { - int min = max / 2; - return rand.nextInt((max - min) + 1) + min; + private static HttpClientBuilder addProxyBasicAuth(HttpClientBuilder httpClientbuilder, HttpHost proxyHost, String userName, String password) { + _log.debug("Proxy setup using Basic authentication"); + BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); + AuthScope siteScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort()); + Credentials siteCreds = new UsernamePasswordCredentials(userName, + password.toCharArray()); + credsProvider.setCredentials(siteScope, siteCreds); + httpClientbuilder.setDefaultCredentialsProvider(credsProvider); + return httpClientbuilder; } - private SegmentSynchronizationTaskImp buildSegments(SplitClientConfig config) throws URISyntaxException { - SegmentChangeFetcher segmentChangeFetcher = HttpSegmentChangeFetcher.create(_httpclient, _rootTarget, _unCachedFireAndForget); + private SegmentSynchronizationTaskImp buildSegments(SplitClientConfig config, + SegmentCacheProducer segmentCacheProducer, + SplitCacheConsumer splitCacheConsumer, RuleBasedSegmentCacheConsumer ruleBasedSegmentCache, + SdkEventsNotifier notifier) throws URISyntaxException { + SegmentChangeFetcher segmentChangeFetcher = HttpSegmentChangeFetcher.create(_splitHttpClient, _rootTarget, + _telemetryStorageProducer); return new SegmentSynchronizationTaskImp(segmentChangeFetcher, - findPollingPeriod(RANDOM, config.segmentsRefreshRate()), + config.segmentsRefreshRate(), config.numThreadsForSegmentFetch(), - _gates, - _segmentCache); + segmentCacheProducer, + _telemetryStorageProducer, + splitCacheConsumer, + config.getThreadFactory(), + ruleBasedSegmentCache, + notifier); } - private SplitFetcher buildSplitFetcher() throws URISyntaxException { - SplitChangeFetcher splitChangeFetcher = HttpSplitChangeFetcher.create(_httpclient, _rootTarget, _unCachedFireAndForget); - SplitParser splitParser = new SplitParser(_segmentSynchronizationTaskImp, _segmentCache); - - return new SplitFetcherImp(splitChangeFetcher, splitParser, _gates, _splitCache); + private SplitFetcher buildSplitFetcher(SplitCacheProducer splitCacheProducer, SplitParser splitParser, + FlagSetsFilter flagSetsFilter, RuleBasedSegmentParser ruleBasedSegmentParser, + RuleBasedSegmentCacheProducer ruleBasedSegmentCache, boolean isRootURIOverriden, + SdkEventsNotifier notifier) throws URISyntaxException { + SplitChangeFetcher splitChangeFetcher = HttpSplitChangeFetcher.create(_splitHttpClient, _rootTarget, + _telemetryStorageProducer, isRootURIOverriden); + return new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, _telemetryStorageProducer, + flagSetsFilter, ruleBasedSegmentParser, ruleBasedSegmentCache, notifier); } - private ImpressionsManagerImpl buildImpressionsManager(SplitClientConfig config) throws URISyntaxException { + private ImpressionsManagerImpl buildImpressionsManager(SplitClientConfig config, + ImpressionsStorageConsumer impressionsStorageConsumer, + ImpressionsStorageProducer impressionsStorageProducer) throws URISyntaxException { List impressionListeners = new ArrayList<>(); if (config.integrationsConfig() != null) { config.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC).stream() @@ -323,13 +754,126 @@ private ImpressionsManagerImpl buildImpressionsManager(SplitClientConfig config) .map(IntegrationsConfig.ImpressionListenerWithMeta::listener) .collect(Collectors.toCollection(() -> impressionListeners)); } + ProcessImpressionStrategy processImpressionStrategy = null; + ImpressionCounter counter = new ImpressionCounter(); + ImpressionListener listener = !impressionListeners.isEmpty() + ? new ImpressionListener.FederatedImpressionListener(impressionListeners) + : null; + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(listener != null, _uniqueKeysTracker, counter); + + switch (config.impressionsMode()) { + case OPTIMIZED: + ImpressionObserver impressionObserver = new ImpressionObserver(config.getLastSeenCacheSize()); + processImpressionStrategy = new ProcessImpressionOptimized(listener != null, impressionObserver, + counter, _telemetryStorageProducer); + break; + case DEBUG: + impressionObserver = new ImpressionObserver(config.getLastSeenCacheSize()); + processImpressionStrategy = new ProcessImpressionDebug(listener != null, impressionObserver); + break; + case NONE: + processImpressionStrategy = processImpressionNone; + break; + } + return ImpressionsManagerImpl.instance(config, _telemetryStorageProducer, impressionsStorageConsumer, + impressionsStorageProducer, + _impressionsSender, processImpressionNone, processImpressionStrategy, counter, listener); + } + + private SDKMetadata createSdkMetadata(boolean ipAddressEnabled, String splitSdkVersion) { + String machineName = ""; + String ip = ""; + + if (ipAddressEnabled) { + try { + InetAddress localHost = InetAddress.getLocalHost(); + machineName = localHost.getHostName(); + ip = localHost.getHostAddress(); + } catch (Exception e) { + _log.error("Could not resolve InetAddress", e); + } + } + return new SDKMetadata(splitSdkVersion, ip, machineName); + } + + private void manageSdkReady(SplitClientConfig config) { + ExecutorService executorService = buildExecutorService(config.getThreadFactory(), + "SPLIT-SDKReadyForConsumer-%d"); + executorService.submit(() -> { + while (!_userStorageWrapper.connect()) { + try { + Thread.currentThread().sleep(1000); + } catch (InterruptedException e) { + _log.warn("Sdk Initializer thread interrupted"); + Thread.currentThread().interrupt(); + } + } + _gates.sdkInternalReady(); + _eventsManager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(false, null)); + _telemetrySynchronizer.synchronizeConfig(config, System.currentTimeMillis(), + ApiKeyCounter.getApiKeyCounterInstance().getFactoryInstances(), new ArrayList<>()); + }); + } + + private UniqueKeysTracker createUniqueKeysTracker(SplitClientConfig config) { + int uniqueKeysRefreshRate = config.operationMode().equals(OperationMode.STANDALONE) + ? config.uniqueKeysRefreshRateInMemory() + : config.uniqueKeysRefreshRateRedis(); + return new UniqueKeysTrackerImp(_telemetrySynchronizer, uniqueKeysRefreshRate, + config.filterUniqueKeysRefreshRate(), + config.getThreadFactory()); + } + + private SplitChangeFetcher createSplitChangeFetcher(SplitClientConfig splitClientConfig) { + String splitFile = splitClientConfig.splitFile(); + InputStream inputStream = splitClientConfig.inputStream(); + FileTypeEnum fileType = splitClientConfig.fileType(); + InputStreamProvider inputStreamProvider; + if (splitFile != null || !isInputStreamConfigValid(inputStream, fileType)) { + if (splitFile == null) { + _log.warn("The InputStream config is invalid"); + } + fileType = getFileTypeFromFileName(splitFile); + inputStreamProvider = new FileInputStreamProvider(splitFile); + } else { + inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + } + try { + switch (fileType) { + case JSON: + return new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + case YAML: + case YML: + return new YamlLocalhostSplitChangeFetcher(inputStreamProvider); + default: + _log.warn(LEGACY_LOG_MESSAGE); + return new LegacyLocalhostSplitChangeFetcher(splitFile); + } + } catch (Exception e) { + _log.warn(String.format("There was no file named %s found. " + + "We created a split client that returns default treatments for all feature flags for all of your users. " + + + "If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " + + + "treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " + + + "considered comments", + splitFile, splitFile), e); + } + _log.warn(LEGACY_LOG_MESSAGE); + return new LegacyLocalhostSplitChangeFetcher(splitFile); + } - return ImpressionsManagerImpl.instance(_httpclient, config, impressionListeners); + private Boolean isInputStreamConfigValid(InputStream inputStream, FileTypeEnum fileType) { + return inputStream != null && fileType != null; } - private FireAndForgetMetrics buildCachedFireAndForgetMetrics(SplitClientConfig config) { - CachedMetrics cachedMetrics = new CachedMetrics(_httpMetrics, TimeUnit.SECONDS.toMillis(config.metricsRefreshRate())); + private FileTypeEnum getFileTypeFromFileName(String fileName) { + try { + return FileTypeEnum.valueOf(Files.getFileExtension(fileName).toUpperCase()); + } catch (Exception e) { + return FileTypeEnum.LEGACY; + } - return FireAndForgetMetrics.instance(cachedMetrics, 2, 1000); } } diff --git a/client/src/main/java/io/split/client/SplitManager.java b/client/src/main/java/io/split/client/SplitManager.java index e93c929b..29691a5f 100644 --- a/client/src/main/java/io/split/client/SplitManager.java +++ b/client/src/main/java/io/split/client/SplitManager.java @@ -10,7 +10,7 @@ */ public interface SplitManager { /** - * Retrieves the features (or Splits) that are currently registered with the + * Retrieves the feature flags that are currently registered with the * SDK. * * @return a List of SplitView or empty @@ -18,23 +18,23 @@ public interface SplitManager { List splits(); /** - * Returns the feature (or Split) registered with the SDK of this name. + * Returns the feature flag registered with the SDK of this name. * * @return SplitView or null */ - SplitView split(String featureName); + SplitView split(String featureFlagName); /** - * Returns the names of features (or Splits) registered with the SDK. + * Returns the names of feature flags registered with the SDK. * - * @return a List of String (Split Feature Names) or empty + * @return a List of String (Feature Flag Names) or empty */ List splitNames(); /** * The SDK kicks off background threads to download data necessary * for using the SDK. You can choose to block until the SDK has - * downloaded split definitions so that you will not get + * downloaded feature flag definitions so that you will not get * the 'control' treatment. *

* @@ -43,4 +43,4 @@ public interface SplitManager { *

*/ void blockUntilReady() throws TimeoutException, InterruptedException; -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/SplitManagerImpl.java b/client/src/main/java/io/split/client/SplitManagerImpl.java index 5304b591..8ada5931 100644 --- a/client/src/main/java/io/split/client/SplitManagerImpl.java +++ b/client/src/main/java/io/split/client/SplitManagerImpl.java @@ -2,10 +2,13 @@ import com.google.common.base.Preconditions; import io.split.client.api.SplitView; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; import io.split.engine.SDKReadinessGates; -import io.split.cache.SplitCache; import io.split.engine.experiments.ParsedSplit; import io.split.inputValidation.SplitNameValidator; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.storage.TelemetryConfigProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,23 +26,40 @@ public class SplitManagerImpl implements SplitManager { private static final Logger _log = LoggerFactory.getLogger(SplitManagerImpl.class); - private final SplitCache _splitCache; + private final SplitCacheConsumer _splitCacheConsumer; private final SplitClientConfig _config; + private final SdkEventsNotifier _notifier; private final SDKReadinessGates _gates; + private final TelemetryConfigProducer _telemetryConfigProducer; - public SplitManagerImpl(SplitCache splitCache, + public SplitManagerImpl(SplitCacheConsumer splitCacheConsumer, SplitClientConfig config, - SDKReadinessGates gates) { + SDKReadinessGates gates, + TelemetryConfigProducer telemetryConfigProducer) { + this(splitCacheConsumer, config, gates, telemetryConfigProducer, SdkEventsNotifier.NOOP); + } + + public SplitManagerImpl(SplitCacheConsumer splitCacheConsumer, + SplitClientConfig config, + SDKReadinessGates gates, + TelemetryConfigProducer telemetryConfigProducer, + SdkEventsNotifier notifier) { + _notifier = Preconditions.checkNotNull(notifier); _config = Preconditions.checkNotNull(config); - _splitCache = Preconditions.checkNotNull(splitCache); + _splitCacheConsumer = Preconditions.checkNotNull(splitCacheConsumer); _gates = Preconditions.checkNotNull(gates); + _telemetryConfigProducer = telemetryConfigProducer; } @Override public List splits() { + if (!_gates.isSDKReady()) { { + _log.warn("splits: the SDK is not ready, results may be incorrect. Make sure to wait for SDK readiness before using this method"); + _telemetryConfigProducer.recordNonReadyUsage(); + }} List result = new ArrayList<>(); - Collection parsedSplits = _splitCache.getAll(); + Collection parsedSplits = _splitCacheConsumer.getAll(); for (ParsedSplit split : parsedSplits) { result.add(SplitView.fromParsedSplit(split)); } @@ -48,18 +68,23 @@ public List splits() { } @Override - public SplitView split(String featureName) { - Optional result = SplitNameValidator.isValid(featureName, "split"); + public SplitView split(String featureFlagName) { + if (!_gates.isSDKReady()) { { + _log.warn(String.format("the SDK is not ready, results may be incorrect for feature flag %s. Make sure to wait " + + "for SDK readiness before using this method", featureFlagName)); + _telemetryConfigProducer.recordNonReadyUsage(); + }} + Optional result = SplitNameValidator.isValid(featureFlagName, "split"); if (!result.isPresent()) { return null; } - featureName = result.get(); + featureFlagName = result.get(); - ParsedSplit parsedSplit = _splitCache.get(featureName); + ParsedSplit parsedSplit = _splitCacheConsumer.get(featureFlagName); if (parsedSplit == null) { - if (_gates.isSDKReadyNow()) { - _log.warn("split: you passed \"" + featureName + "\" that does not exist in this environment, " + - "please double check what Splits exist in the web console."); + if (_gates.isSDKReady()) { + _log.warn("split: you passed \"" + featureFlagName + "\" that does not exist in this environment, " + + "please double check what feature flags exist in the Split user interface."); } return null; } @@ -69,13 +94,11 @@ public SplitView split(String featureName) { @Override public List splitNames() { - List result = new ArrayList<>(); - Collection parsedSplits = _splitCache.getAll(); - for (ParsedSplit split : parsedSplits) { - result.add(split.feature()); - } - - return result; + if (!_gates.isSDKReady()) { { + _log.warn("splitNames: the SDK is not ready, results may be incorrect. Make sure to wait for SDK readiness before using this method"); + _telemetryConfigProducer.recordNonReadyUsage(); + }} + return _splitCacheConsumer.splitNames(); } @Override @@ -83,8 +106,10 @@ public void blockUntilReady() throws TimeoutException, InterruptedException { if (_config.blockUntilReady() <= 0) { throw new IllegalArgumentException("setBlockUntilReadyTimeout must be positive but in config was: " + _config.blockUntilReady()); } - if (!_gates.isSDKReady(_config.blockUntilReady())) { + if (!_gates.waitUntilInternalReady(_config.blockUntilReady())) { + _telemetryConfigProducer.recordBURTimeout(); + _notifier.notify(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED, null); throw new TimeoutException("SDK was not ready in " + _config.blockUntilReady()+ " milliseconds"); } } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java b/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java new file mode 100644 index 00000000..b2dccfdc --- /dev/null +++ b/client/src/main/java/io/split/client/YamlLocalhostSplitChangeFetcher.java @@ -0,0 +1,87 @@ +package io.split.client; + +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.Status; +import io.split.client.dtos.ChangeDto; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.LocalhostConstants; +import io.split.engine.common.FetchOptions; +import io.split.engine.experiments.SplitChangeFetcher; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static io.split.client.utils.LocalhostSanitizer.createCondition; + +public class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher { + + private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class); + private final InputStreamProvider _inputStreamProvider; + + public YamlLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider) { + _inputStreamProvider = inputStreamProvider; + } + + @Override + public SplitChange fetch(long since, long sinceRBS, FetchOptions options) { + try { + Yaml yaml = new Yaml(); + List>> yamlSplits = yaml.load(_inputStreamProvider.get()); + SplitChange splitChange = new SplitChange(); + splitChange.featureFlags = new ChangeDto<>(); + splitChange.featureFlags.d = new ArrayList<>(); + for(Map> aSplit : yamlSplits) { + // The outter map is a map with one key, the split name + Map.Entry> splitAndValues = aSplit.entrySet().iterator().next(); + + Optional splitOptional = splitChange.featureFlags.d.stream(). + filter(split -> split.name.equals(splitAndValues.getKey())).findFirst(); + Split split = splitOptional.orElse(null); + if(split == null) { + split = new Split(); + split.name = splitAndValues.getKey(); + split.configurations = new HashMap<>(); + split.conditions = new ArrayList<>(); + } else { + splitChange.featureFlags.d.remove(split); + } + String treatment = (String) splitAndValues.getValue().get("treatment"); + String configurations = splitAndValues.getValue().get("config") != null ? (String) splitAndValues.getValue().get("config") : null; + Object keyOrKeys = splitAndValues.getValue().get("keys"); + split.configurations.put(treatment, configurations); + + Condition condition = createCondition(keyOrKeys, treatment); + if(condition.conditionType != ConditionType.ROLLOUT){ + split.conditions.add(0, condition); + } else { + split.conditions.add(condition); + } + split.status = Status.ACTIVE; + split.defaultTreatment = LocalhostConstants.CONTROL; + split.trafficTypeName = LocalhostConstants.USER; + split.trafficAllocation = LocalhostConstants.SIZE_100; + split.trafficAllocationSeed = LocalhostConstants.SIZE_1; + splitChange.featureFlags.d.add(split); + } + splitChange.featureFlags.t = since; + splitChange.featureFlags.s = since; + splitChange.ruleBasedSegments = new ChangeDto<>(); + splitChange.ruleBasedSegments.s = -1; + splitChange.ruleBasedSegments.t = -1; + splitChange.ruleBasedSegments.d = new ArrayList<>(); + return splitChange; + } catch (Exception e) { + throw new IllegalStateException("Problem fetching splitChanges using a yaml file: " + e.getMessage(), e); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/YamlLocalhostSplitFile.java b/client/src/main/java/io/split/client/YamlLocalhostSplitFile.java deleted file mode 100644 index 926bab16..00000000 --- a/client/src/main/java/io/split/client/YamlLocalhostSplitFile.java +++ /dev/null @@ -1,65 +0,0 @@ -package io.split.client; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.yaml.snakeyaml.Yaml; - -import java.io.FileReader; -import java.io.IOException; -import java.util.List; -import java.util.Map; - -public class YamlLocalhostSplitFile extends AbstractLocalhostSplitFile { - - private static final Logger _log = LoggerFactory.getLogger(LegacyLocalhostSplitFile.class); - - public YamlLocalhostSplitFile(LocalhostSplitFactory localhostSplitFactory, String directory, String filenameYaml) throws IOException { - super(localhostSplitFactory, directory, filenameYaml); - } - - public Map readOnSplits() throws IOException { - Map onSplits = Maps.newHashMap(); - try { - - Yaml yaml = new Yaml(); - List>> yamlSplits = yaml.load(new FileReader(_file)); - - for(Map> aSplit : yamlSplits) { - // The outter map is a map with one key, the split name - Map.Entry> splitAndValues = aSplit.entrySet().iterator().next(); - - SplitAndKey splitAndKey = null; - String splitName = splitAndValues.getKey(); - String treatment = (String) splitAndValues.getValue().get("treatment"); - String configurations = splitAndValues.getValue().get("config") != null? (String) splitAndValues.getValue().get("config") : null; - Object keyOrKeys = splitAndValues.getValue().get("keys"); - - if (keyOrKeys == null) { - splitAndKey = SplitAndKey.of(splitName); // Key in this line is splitName - onSplits.put(splitAndKey, LocalhostSplit.of(treatment, configurations)); - } else { - if (keyOrKeys instanceof String) { - splitAndKey = SplitAndKey.of(splitName, (String) keyOrKeys); - onSplits.put(splitAndKey, LocalhostSplit.of(treatment, configurations)); - } else { - Preconditions.checkArgument(keyOrKeys instanceof List, "'keys' is not a String nor a List."); - for (String aKey : (List) keyOrKeys) { - splitAndKey = SplitAndKey.of(splitName, aKey); - onSplits.put(splitAndKey, LocalhostSplit.of(treatment, configurations)); - } - } - } - } - } catch (Exception e) { - _log.warn("There was no file named " + _file.getPath() + " found. " + - "We created a split client that returns default treatments for all features for all of your users. " + - "If you wish to return a specific treatment for a feature, enter the name of that feature name and " + - "treatment name separated by whitespace in " + _file.getPath() + - "; one pair per line. Empty lines or lines starting with '#' are considered comments", e); - } - - return onSplits; - } -} diff --git a/client/src/main/java/io/split/client/api/SdkEvent.java b/client/src/main/java/io/split/client/api/SdkEvent.java new file mode 100644 index 00000000..e96983ba --- /dev/null +++ b/client/src/main/java/io/split/client/api/SdkEvent.java @@ -0,0 +1,26 @@ +package io.split.client.api; + +/** + * SDK lifecycle events a consumer can subscribe to. + * + *

{@code sdkReadyFromCache} is intentionally not part of this enum: it only applies to SDKs with a persistent cache. + */ +public enum SdkEvent { + /** + * The SDK has fetched feature flags and segments and is ready to evaluate. Fires once and is replayed + * (with {@code null} metadata) to listeners registered after it happened. + * Metadata: {@link SdkEventMetadata#isInitialCacheLoad()} and {@link SdkEventMetadata#getLastUpdateTimestamp()}. + */ + SDK_READY, + + /** + * The SDK did not become ready within the configured {@code blockUntilReady} time. Metadata is always {@code null}. + */ + SDK_READY_TIMED_OUT, + + /** + * Feature flags or segments changed after the SDK became ready. Fires every time, never replayed. + * Metadata: {@link SdkEventMetadata#getType()} and {@link SdkEventMetadata#getNames()}. + */ + SDK_UPDATE +} diff --git a/client/src/main/java/io/split/client/api/SdkEventListener.java b/client/src/main/java/io/split/client/api/SdkEventListener.java new file mode 100644 index 00000000..a56d64a5 --- /dev/null +++ b/client/src/main/java/io/split/client/api/SdkEventListener.java @@ -0,0 +1,13 @@ +package io.split.client.api; + +/** + * Callback invoked when a {@link SdkEvent} fires. + */ +@FunctionalInterface +public interface SdkEventListener { + /** + * @param metadata event details; may be {@code null} for {@link SdkEvent#SDK_READY_TIMED_OUT} + * and for a {@link SdkEvent#SDK_READY} replayed to a late subscriber + */ + void handle(SdkEventMetadata metadata); +} diff --git a/client/src/main/java/io/split/client/api/SdkEventMetadata.java b/client/src/main/java/io/split/client/api/SdkEventMetadata.java new file mode 100644 index 00000000..bcb82226 --- /dev/null +++ b/client/src/main/java/io/split/client/api/SdkEventMetadata.java @@ -0,0 +1,95 @@ +package io.split.client.api; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +/** + * Immutable details of a {@link SdkEvent}. + * + *

The metadata handed to a {@link SdkEventListener} can itself be {@code null}: always for + * {@link SdkEvent#SDK_READY_TIMED_OUT}, and for a once-only event replayed to a late subscriber. + */ +public final class SdkEventMetadata { + + private final SdkEventType type; + private final Set names; + private final boolean initialCacheLoad; + private final Long lastUpdateTimestamp; + + private SdkEventMetadata(SdkEventType type, Set names, boolean initialCacheLoad, Long lastUpdateTimestamp) { + this.type = type; + this.names = Collections.unmodifiableSet(names); + this.initialCacheLoad = initialCacheLoad; + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + /** + * Metadata for {@link SdkEvent#SDK_UPDATE}. Null and blank names are dropped. + */ + public static SdkEventMetadata update(SdkEventType type, Collection names) { + Set sanitized = new HashSet<>(); + if (names != null) { + for (String name : names) { + if (name != null && !name.trim().isEmpty()) { + sanitized.add(name); + } + } + } + return new SdkEventMetadata(type, sanitized, false, null); + } + + /** + * Metadata for {@link SdkEvent#SDK_READY}. + */ + public static SdkEventMetadata ready(boolean initialCacheLoad, Long lastUpdateTimestamp) { + return new SdkEventMetadata(null, new HashSet(), initialCacheLoad, lastUpdateTimestamp); + } + + /** @return the update type, or {@code null} for SDK_READY metadata */ + public SdkEventType getType() { + return type; + } + + /** @return the affected flag names; never null, unmodifiable */ + public Set getNames() { + return names; + } + + public boolean isInitialCacheLoad() { + return initialCacheLoad; + } + + /** @return milliseconds since epoch of the last update, or {@code null} if not applicable */ + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SdkEventMetadata that = (SdkEventMetadata) o; + return initialCacheLoad == that.initialCacheLoad + && type == that.type + && names.equals(that.names) + && Objects.equals(lastUpdateTimestamp, that.lastUpdateTimestamp); + } + + @Override + public int hashCode() { + return Objects.hash(type, names, initialCacheLoad, lastUpdateTimestamp); + } + + @Override + public String toString() { + return "SdkEventMetadata{type=" + type + ", names=" + names + ", initialCacheLoad=" + initialCacheLoad + + ", lastUpdateTimestamp=" + lastUpdateTimestamp + "}"; + } +} diff --git a/client/src/main/java/io/split/client/api/SdkEventType.java b/client/src/main/java/io/split/client/api/SdkEventType.java new file mode 100644 index 00000000..104842f1 --- /dev/null +++ b/client/src/main/java/io/split/client/api/SdkEventType.java @@ -0,0 +1,12 @@ +package io.split.client.api; + +/** + * What kind of change triggered an {@link SdkEvent#SDK_UPDATE}. + */ +public enum SdkEventType { + /** One or more feature flags were added, updated, removed or killed. */ + FLAGS_UPDATE, + + /** Segments or rule-based segments changed. */ + SEGMENTS_UPDATE +} diff --git a/client/src/main/java/io/split/client/api/SplitView.java b/client/src/main/java/io/split/client/api/SplitView.java index c053c895..cc217fe1 100644 --- a/client/src/main/java/io/split/client/api/SplitView.java +++ b/client/src/main/java/io/split/client/api/SplitView.java @@ -1,6 +1,7 @@ package io.split.client.api; import io.split.client.dtos.Partition; +import io.split.client.dtos.Prerequisites; import io.split.engine.experiments.ParsedCondition; import io.split.engine.experiments.ParsedSplit; @@ -24,6 +25,10 @@ public class SplitView { public List treatments; public long changeNumber; public Map configs; + public List sets; + public String defaultTreatment; + public boolean impressionsDisabled; + public List prerequisites; public static SplitView fromParsedSplit(ParsedSplit parsedSplit) { SplitView splitView = new SplitView(); @@ -31,6 +36,8 @@ public static SplitView fromParsedSplit(ParsedSplit parsedSplit) { splitView.trafficType = parsedSplit.trafficTypeName(); splitView.killed = parsedSplit.killed(); splitView.changeNumber = parsedSplit.changeNumber(); + splitView.sets = parsedSplit.flagSets() != null ? new ArrayList<>(parsedSplit.flagSets()): new ArrayList<>(); + splitView.defaultTreatment = parsedSplit.defaultTreatment(); Set treatments = new HashSet(); for (ParsedCondition condition : parsedSplit.parsedConditions()) { @@ -42,7 +49,10 @@ public static SplitView fromParsedSplit(ParsedSplit parsedSplit) { splitView.treatments = new ArrayList(treatments); splitView.configs = parsedSplit.configurations() == null? Collections.emptyMap() : parsedSplit.configurations() ; + splitView.impressionsDisabled = parsedSplit.impressionsDisabled(); + splitView.prerequisites = parsedSplit.prerequisitesMatcher() != null ? + parsedSplit.prerequisitesMatcher().getPrerequisites(): new ArrayList<>(); return splitView; } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/dtos/BasicCredentialsProvider.java b/client/src/main/java/io/split/client/dtos/BasicCredentialsProvider.java new file mode 100644 index 00000000..b77c9f59 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/BasicCredentialsProvider.java @@ -0,0 +1,7 @@ +package io.split.client.dtos; + +public interface BasicCredentialsProvider extends ProxyCredentialsProvider +{ + String getUsername(); + String getPassword(); +} diff --git a/client/src/main/java/io/split/client/dtos/BearerCredentialsProvider.java b/client/src/main/java/io/split/client/dtos/BearerCredentialsProvider.java new file mode 100644 index 00000000..d4e98c5f --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/BearerCredentialsProvider.java @@ -0,0 +1,6 @@ +package io.split.client.dtos; + +public interface BearerCredentialsProvider extends ProxyCredentialsProvider +{ + String getToken(); +} diff --git a/client/src/main/java/io/split/client/dtos/BetweenStringMatcherData.java b/client/src/main/java/io/split/client/dtos/BetweenStringMatcherData.java new file mode 100644 index 00000000..3af1cc0c --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/BetweenStringMatcherData.java @@ -0,0 +1,11 @@ +package io.split.client.dtos; + +/** + * Metadata to support the between matcher. + * + * @author adil + */ +public class BetweenStringMatcherData { + public String start; + public String end; +} diff --git a/client/src/main/java/io/split/client/dtos/ChangeDto.java b/client/src/main/java/io/split/client/dtos/ChangeDto.java new file mode 100644 index 00000000..596c05e0 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/ChangeDto.java @@ -0,0 +1,9 @@ +package io.split.client.dtos; + +import java.util.List; + +public class ChangeDto { + public long s; + public long t; + public List d; +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/dtos/DecoratedImpression.java b/client/src/main/java/io/split/client/dtos/DecoratedImpression.java new file mode 100644 index 00000000..34b3b468 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/DecoratedImpression.java @@ -0,0 +1,18 @@ +package io.split.client.dtos; + +import io.split.client.impressions.Impression; + +public class DecoratedImpression { + private Impression impression; + private boolean disabled; + + public DecoratedImpression(Impression impression, boolean disabled) { + this.impression = impression; + this.disabled = disabled; + } + + public Impression impression() { return this.impression;} + + public boolean disabled() { return this.disabled;} +} + diff --git a/client/src/main/java/io/split/client/dtos/EvaluationOptions.java b/client/src/main/java/io/split/client/dtos/EvaluationOptions.java new file mode 100644 index 00000000..7248f64e --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/EvaluationOptions.java @@ -0,0 +1,14 @@ +package io.split.client.dtos; + +import java.util.Map; + +public class EvaluationOptions { + private Map _properties; + + public EvaluationOptions(Map properties) { + _properties = properties; + } + public Map getProperties() { + return _properties; + } +} diff --git a/client/src/main/java/io/split/client/dtos/Event.java b/client/src/main/java/io/split/client/dtos/Event.java index ecc060f7..daf1c66d 100644 --- a/client/src/main/java/io/split/client/dtos/Event.java +++ b/client/src/main/java/io/split/client/dtos/Event.java @@ -1,18 +1,32 @@ package io.split.client.dtos; import com.google.common.base.Objects; +import com.google.gson.annotations.SerializedName; import java.util.Map; public class Event { + /* package private */ static final String FIELD_EVENT_TYPE_ID = "eventTypeId"; + /* package private */ static final String FIELD_TRAFFIC_TYPE_NAME = "trafficTypeName"; + /* package private */ static final String FIELD_KEY = "key"; + /* package private */ static final String FIELD_VALUE = "value"; + /* package private */ static final String FIELD_PROPERTIES = "properties"; + /* package private */ static final String FIELD_TIMESTAMP = "timestamp"; + public static int MAX_PROPERTIES_LENGTH_BYTES = 32 * 1024; + @SerializedName(FIELD_EVENT_TYPE_ID) public String eventTypeId; + @SerializedName(FIELD_TRAFFIC_TYPE_NAME) public String trafficTypeName; + @SerializedName(FIELD_KEY) public String key; + @SerializedName(FIELD_VALUE) public double value; + @SerializedName(FIELD_PROPERTIES) public Map properties; + @SerializedName(FIELD_TIMESTAMP) public long timestamp; @Override diff --git a/client/src/main/java/io/split/client/dtos/Excluded.java b/client/src/main/java/io/split/client/dtos/Excluded.java new file mode 100644 index 00000000..e23afa4b --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/Excluded.java @@ -0,0 +1,8 @@ +package io.split.client.dtos; + +import java.util.List; + +public class Excluded { + public List keys; + public List segments; +} diff --git a/client/src/main/java/io/split/client/dtos/ExcludedSegments.java b/client/src/main/java/io/split/client/dtos/ExcludedSegments.java new file mode 100644 index 00000000..9e65fa60 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/ExcludedSegments.java @@ -0,0 +1,27 @@ +package io.split.client.dtos; + +public class ExcludedSegments { + static final String STANDARD_TYPE = "standard"; + static final String RULE_BASED_TYPE = "rule-based"; + + public ExcludedSegments() {} + public ExcludedSegments(String type, String name) { + this.type = type; + this.name = name; + } + + public String type; + public String name; + + public boolean isStandard() { + return STANDARD_TYPE.equals(type); + } + + public boolean isRuleBased() { + return RULE_BASED_TYPE.equals(type); + } + + public String getSegmentName(){ + return name; + } +} diff --git a/client/src/main/java/io/split/client/dtos/FallbackTreatment.java b/client/src/main/java/io/split/client/dtos/FallbackTreatment.java new file mode 100644 index 00000000..291db4f4 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/FallbackTreatment.java @@ -0,0 +1,37 @@ +package io.split.client.dtos; + +public class FallbackTreatment { + private final String _config; + private final String _treatment; + private final String _label; + + public FallbackTreatment(String treatment, String config) { + _treatment = treatment; + _config = config; + _label = null; + } + + public FallbackTreatment(String treatment) { + _treatment = treatment; + _config = null; + _label = null; + } + + public FallbackTreatment(String treatment, String config, String label) { + _treatment = treatment; + _config = config; + _label = label; + } + + public String getConfig() { + return _config; + } + + public String getTreatment() { + return _treatment; + } + + public String getLabel() { + return _label; + } +} diff --git a/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculator.java b/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculator.java new file mode 100644 index 00000000..b172a1cb --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculator.java @@ -0,0 +1,6 @@ +package io.split.client.dtos; + +public interface FallbackTreatmentCalculator +{ + FallbackTreatment resolve(String flagName, String label); +} diff --git a/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculatorImp.java b/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculatorImp.java new file mode 100644 index 00000000..936abc49 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/FallbackTreatmentCalculatorImp.java @@ -0,0 +1,40 @@ +package io.split.client.dtos; + +import io.split.grammar.Treatments; + +public class FallbackTreatmentCalculatorImp implements FallbackTreatmentCalculator +{ + private final FallbackTreatmentsConfiguration _fallbackTreatmentsConfiguration; + private final static String labelPrefix = "fallback - "; + + public FallbackTreatmentCalculatorImp(FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration) { + _fallbackTreatmentsConfiguration = fallbackTreatmentsConfiguration; + } + + public FallbackTreatment resolve(String flagName, String label) { + if (_fallbackTreatmentsConfiguration != null) { + if (_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment() != null + && _fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName) != null) { + return copyWithLabel(_fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get(flagName), + resolveLabel(label)); + } + if (_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment() != null) { + return copyWithLabel(_fallbackTreatmentsConfiguration.getGlobalFallbackTreatment(), + resolveLabel(label)); + } + } + + return new FallbackTreatment(Treatments.CONTROL, null, label); + } + + private String resolveLabel(String label) { + if (label == null) { + return null; + } + return labelPrefix + label; + } + + private FallbackTreatment copyWithLabel(FallbackTreatment fallbackTreatment, String label) { + return new FallbackTreatment(fallbackTreatment.getTreatment(), fallbackTreatment.getConfig(), label); + } +} diff --git a/client/src/main/java/io/split/client/dtos/FallbackTreatmentsConfiguration.java b/client/src/main/java/io/split/client/dtos/FallbackTreatmentsConfiguration.java new file mode 100644 index 00000000..55a0a27d --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/FallbackTreatmentsConfiguration.java @@ -0,0 +1,65 @@ +package io.split.client.dtos; + +import java.util.HashMap; +import java.util.Map; + +public class FallbackTreatmentsConfiguration { + private final FallbackTreatment _globalFallbackTreatment; + private final Map _byFlagFallbackTreatment; + + public FallbackTreatmentsConfiguration(FallbackTreatment globalFallbackTreatment, Map byFlagFallbackTreatment) { + _globalFallbackTreatment = globalFallbackTreatment; + _byFlagFallbackTreatment = byFlagFallbackTreatment; + } + + public FallbackTreatmentsConfiguration(Map byFlagFallbackTreatment) { + _globalFallbackTreatment = null; + _byFlagFallbackTreatment = byFlagFallbackTreatment; + } + + public FallbackTreatmentsConfiguration(HashMap byFlagFallbackTreatment) { + _globalFallbackTreatment = null; + _byFlagFallbackTreatment = buildByFlagFallbackTreatment(byFlagFallbackTreatment); + } + + public FallbackTreatmentsConfiguration(FallbackTreatment globalFallbackTreatment) { + _globalFallbackTreatment = globalFallbackTreatment; + _byFlagFallbackTreatment = null; + } + + public FallbackTreatmentsConfiguration(String globalFallbackTreatment, Map byFlagFallbackTreatment) { + _globalFallbackTreatment = new FallbackTreatment(globalFallbackTreatment); + _byFlagFallbackTreatment = byFlagFallbackTreatment; + } + + public FallbackTreatmentsConfiguration(String globalFallbackTreatment) { + _globalFallbackTreatment = new FallbackTreatment(globalFallbackTreatment); + _byFlagFallbackTreatment = null; + } + + + public FallbackTreatmentsConfiguration(String globalFallbackTreatment, HashMap byFlagFallbackTreatment) { + _globalFallbackTreatment = new FallbackTreatment(globalFallbackTreatment); + _byFlagFallbackTreatment = buildByFlagFallbackTreatment(byFlagFallbackTreatment); + } + + public FallbackTreatmentsConfiguration(FallbackTreatment globalFallbackTreatment, HashMap byFlagFallbackTreatment) { + _globalFallbackTreatment = globalFallbackTreatment; + _byFlagFallbackTreatment = buildByFlagFallbackTreatment(byFlagFallbackTreatment); + } + + public FallbackTreatment getGlobalFallbackTreatment() { + return _globalFallbackTreatment; + } + + public Map getByFlagFallbackTreatment() { return _byFlagFallbackTreatment;} + + private Map buildByFlagFallbackTreatment(Map byFlagFallbackTreatment) { + Map result = new HashMap<>(); + for (Map.Entry entry : byFlagFallbackTreatment.entrySet()) { + result.put(entry.getKey(), new FallbackTreatment(entry.getValue())); + } + + return result; + } +} diff --git a/client/src/main/java/io/split/client/dtos/KeyImpression.java b/client/src/main/java/io/split/client/dtos/KeyImpression.java index 4d6a580c..980fc117 100644 --- a/client/src/main/java/io/split/client/dtos/KeyImpression.java +++ b/client/src/main/java/io/split/client/dtos/KeyImpression.java @@ -15,6 +15,7 @@ public class KeyImpression { /* package private */ static final String FIELD_TIME = "m"; /* package private */ static final String FIELD_CHANGE_NUMBER = "c"; /* package private */ static final String FIELD_PREVIOUS_TIME = "pt"; + /* package private */ static final String FIELD_PROPERTIES = "properties"; public transient String feature; // Non-serializable @@ -39,6 +40,9 @@ public class KeyImpression { @SerializedName(FIELD_PREVIOUS_TIME) public Long previousTime; + @SerializedName(FIELD_PROPERTIES) + public String properties; + @Override public boolean equals(Object o) { if (this == o) return true; @@ -50,6 +54,7 @@ public boolean equals(Object o) { if (!Objects.equals(feature, that.feature)) return false; if (!keyName.equals(that.keyName)) return false; if (!treatment.equals(that.treatment)) return false; + if (properties != null && !properties.equals(that.properties)) return false; if (bucketingKey == null) { return that.bucketingKey == null; @@ -77,6 +82,8 @@ public static KeyImpression fromImpression(Impression i) { ki.changeNumber = i.changeNumber(); ki.treatment = i.treatment(); ki.label = i.appliedRule(); + ki.previousTime = i.pt(); + ki.properties = i.properties(); return ki; } } diff --git a/client/src/main/java/io/split/client/dtos/Matcher.java b/client/src/main/java/io/split/client/dtos/Matcher.java index 12c824d2..fc2c6515 100644 --- a/client/src/main/java/io/split/client/dtos/Matcher.java +++ b/client/src/main/java/io/split/client/dtos/Matcher.java @@ -13,6 +13,7 @@ public class Matcher { public WhitelistMatcherData whitelistMatcherData; public UnaryNumericMatcherData unaryNumericMatcherData; public BetweenMatcherData betweenMatcherData; + public BetweenStringMatcherData betweenStringMatcherData; public DependencyMatcherData dependencyMatcherData; public Boolean booleanMatcherData; public String stringMatcherData; diff --git a/client/src/main/java/io/split/client/dtos/MatcherType.java b/client/src/main/java/io/split/client/dtos/MatcherType.java index ec22baec..22f22adb 100644 --- a/client/src/main/java/io/split/client/dtos/MatcherType.java +++ b/client/src/main/java/io/split/client/dtos/MatcherType.java @@ -30,5 +30,15 @@ public enum MatcherType { EQUAL_TO_BOOLEAN, /* Dependency Matcher */ - IN_SPLIT_TREATMENT + IN_SPLIT_TREATMENT, + + /* Semver matchers */ + EQUAL_TO_SEMVER, + GREATER_THAN_OR_EQUAL_TO_SEMVER, + LESS_THAN_OR_EQUAL_TO_SEMVER, + IN_LIST_SEMVER, + BETWEEN_SEMVER, + + /* Rule based segment */ + IN_RULE_BASED_SEGMENT } diff --git a/client/src/main/java/io/split/client/dtos/Metadata.java b/client/src/main/java/io/split/client/dtos/Metadata.java new file mode 100644 index 00000000..d8133560 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/Metadata.java @@ -0,0 +1,42 @@ +package io.split.client.dtos; + +import com.google.gson.annotations.SerializedName; +import io.split.client.interceptors.SdkMetadataInterceptorFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; + +public class Metadata { + + /* package private */ static final String FIELD_HOSTNAME = "n"; + /* package private */ static final String FIELD_IP = "i"; + /* package private */ static final String FIELD_SDK_VERSION = "s"; + + private static final Logger _log = LoggerFactory.getLogger(Metadata.class); + + @SerializedName(FIELD_HOSTNAME) + private final String _hostname; + @SerializedName(FIELD_IP) + private final String _ip; + @SerializedName(FIELD_SDK_VERSION) + private final String _sdkVersion; + + public Metadata(boolean ipAddressEnabled, String sdkVersion) { + String hostName = null; + String ip = null; + + if (ipAddressEnabled) { + try { + InetAddress localHost = InetAddress.getLocalHost(); + hostName = localHost.getHostName(); + ip = localHost.getHostAddress(); + } catch (Exception e) { + _log.error("Could not resolve InetAddress", e); + } + } + _sdkVersion = sdkVersion; + _hostname = hostName; + _ip = ip; + } +} diff --git a/client/src/main/java/io/split/client/dtos/Prerequisites.java b/client/src/main/java/io/split/client/dtos/Prerequisites.java new file mode 100644 index 00000000..644cb5fc --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/Prerequisites.java @@ -0,0 +1,12 @@ +package io.split.client.dtos; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class Prerequisites { + @SerializedName("n") + public String featureFlagName; + @SerializedName("ts") + public List treatments; +} diff --git a/client/src/main/java/io/split/client/dtos/ProxyConfiguration.java b/client/src/main/java/io/split/client/dtos/ProxyConfiguration.java new file mode 100644 index 00000000..c1ed2b40 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/ProxyConfiguration.java @@ -0,0 +1,66 @@ +package io.split.client.dtos; + +import org.apache.hc.core5.http.HttpHost; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; + +public class ProxyConfiguration { + private final HttpHost _proxyHost; + private ProxyCredentialsProvider _provider; + private final InputStream _p12File; + private final String _passKey; + + private ProxyConfiguration(HttpHost proxyHost, + ProxyCredentialsProvider proxyCredentialsProvider, + InputStream p12File, String passKey) { + _proxyHost = proxyHost; + _p12File = p12File; + _passKey = passKey; + _provider = proxyCredentialsProvider; + } + + public HttpHost getHost() { return _proxyHost; } + public InputStream getP12File() { return _p12File; } + public String getPassKey() { return _passKey; } + public ProxyCredentialsProvider getProxyCredentialsProvider() { return _provider; } + + public static ProxyConfiguration.Builder builder() { + return new ProxyConfiguration.Builder(); + } + + public static class Builder { + private ProxyCredentialsProvider _provider; + private HttpHost _proxyHost; + private InputStream _p12File; + private String _passKey; + + public ProxyConfiguration.Builder credentialsProvider(ProxyCredentialsProvider provider) { + _provider = provider; + return this; + } + + public ProxyConfiguration.Builder url(URL url) throws MalformedURLException { + try { + _proxyHost = new HttpHost(url.getProtocol(), url.getHost(), url.getPort()); + } catch (Exception exc) { + throw new MalformedURLException("Proxy configuration is invalid. The proxy `url` is malformed"); + } + return this; + } + + public ProxyConfiguration.Builder mtls(InputStream p12File, String passKey) { + _passKey = passKey; + _p12File = p12File; + return this; + } + + public ProxyConfiguration build() { + if (_proxyHost == null) { + throw new IllegalArgumentException("Proxy configuration is invalid. The proxy `url` was not provided"); + } + return new ProxyConfiguration(_proxyHost, _provider, _p12File, _passKey); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/dtos/ProxyCredentialsProvider.java b/client/src/main/java/io/split/client/dtos/ProxyCredentialsProvider.java new file mode 100644 index 00000000..e1653d5f --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/ProxyCredentialsProvider.java @@ -0,0 +1,4 @@ +package io.split.client.dtos; + +public interface ProxyCredentialsProvider +{} diff --git a/client/src/main/java/io/split/client/dtos/RequestContext.java b/client/src/main/java/io/split/client/dtos/RequestContext.java new file mode 100644 index 00000000..29fb7f77 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/RequestContext.java @@ -0,0 +1,20 @@ +package io.split.client.dtos; + +import java.util.Map; +import java.util.List; +/** + * A structure returning a context for RequestDecorator class + */ + +public class RequestContext +{ + private final Map> _headers; + + public RequestContext(Map> headers) { + _headers = headers; + } + + public Map> headers() { + return _headers; + } +} diff --git a/client/src/main/java/io/split/client/dtos/RuleBasedSegment.java b/client/src/main/java/io/split/client/dtos/RuleBasedSegment.java new file mode 100644 index 00000000..56c4756d --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/RuleBasedSegment.java @@ -0,0 +1,37 @@ +package io.split.client.dtos; + +import java.util.ArrayList; +import java.util.List; + +public class RuleBasedSegment { + public String name; + public Status status; + public String trafficTypeName; + public long changeNumber; + public List conditions; + public Excluded excluded; + + @Override + public String toString() { + return "RuleBasedSegment{" + + "name='" + name + '\'' + + ", status=" + status + + ", trafficTypeName='" + trafficTypeName + '\'' + + ", changeNumber=" + changeNumber + '\'' + + excludedToString() + '\'' + + '}'; + } + + public String excludedToString() { + Excluded ts = excluded != null ? excluded : new Excluded(); + if (ts.keys == null) { + ts.keys = new ArrayList<>(); + } + + if (ts.segments == null) { + ts.segments = new ArrayList<>(); + } + + return ", excludedKeys=" + ts.keys + '\'' + ", excludedSegments=" + ts.segments; + } +} diff --git a/client/src/main/java/io/split/client/dtos/Split.java b/client/src/main/java/io/split/client/dtos/Split.java index 15e1a945..1b9a01e3 100644 --- a/client/src/main/java/io/split/client/dtos/Split.java +++ b/client/src/main/java/io/split/client/dtos/Split.java @@ -1,5 +1,6 @@ package io.split.client.dtos; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -16,7 +17,9 @@ public class Split { public Integer trafficAllocationSeed; public int algo; public Map configurations; - + public HashSet sets; + public Boolean impressionsDisabled = null; + public List prerequisites; @Override public String toString() { diff --git a/client/src/main/java/io/split/client/dtos/SplitChange.java b/client/src/main/java/io/split/client/dtos/SplitChange.java index ba113088..f3676bf7 100644 --- a/client/src/main/java/io/split/client/dtos/SplitChange.java +++ b/client/src/main/java/io/split/client/dtos/SplitChange.java @@ -1,9 +1,11 @@ package io.split.client.dtos; -import java.util.List; +import com.google.gson.annotations.SerializedName; public class SplitChange { - public List splits; - public long since; - public long till; + @SerializedName("ff") + public ChangeDto featureFlags; + @SerializedName("rbs") + public ChangeDto ruleBasedSegments; + public boolean clearCache; } diff --git a/client/src/main/java/io/split/client/dtos/SplitChangesOldPayloadDto.java b/client/src/main/java/io/split/client/dtos/SplitChangesOldPayloadDto.java new file mode 100644 index 00000000..aa292f91 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/SplitChangesOldPayloadDto.java @@ -0,0 +1,34 @@ +package io.split.client.dtos; + +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.List; + +public class SplitChangesOldPayloadDto { + @SerializedName("since") + public long s; + + @SerializedName("till") + public long t; + + @SerializedName("splits") + public List d; + + public SplitChange toSplitChange() { + SplitChange splitChange = new SplitChange(); + ChangeDto ff = new ChangeDto<>(); + ff.s = this.s; + ff.t = this.t; + ff.d = this.d; + ChangeDto rbs = new ChangeDto<>(); + rbs.d = new ArrayList<>(); + rbs.t = -1; + rbs.s = -1; + + splitChange.featureFlags = ff; + splitChange.ruleBasedSegments = rbs; + + return splitChange; + } +} diff --git a/client/src/main/java/io/split/client/dtos/SplitHttpResponse.java b/client/src/main/java/io/split/client/dtos/SplitHttpResponse.java new file mode 100644 index 00000000..259ed079 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/SplitHttpResponse.java @@ -0,0 +1,61 @@ +package io.split.client.dtos; + +import java.util.List; + +/** + * A structure for returning http call results information + */ +public class SplitHttpResponse { + private final Integer _statusCode; + private final String _statusMessage; + private final String _body; + private final Header[] _responseHeaders; + + public static class Header { + private String _name; + private List _values; + + public Header(String name, List values) { + _name = name; + _values = values; + } + + public String getName() { + return _name; + } + + public List getValues() { + return _values; + } + }; + + public SplitHttpResponse(Integer statusCode, String statusMessage, String body, Header[] headers) { + _statusCode = statusCode; + _statusMessage = statusMessage; + _body = body; + _responseHeaders = headers; + } + + public SplitHttpResponse(Integer statusCode, String statusMessage, String body, List

headers) { + _statusCode = statusCode; + _statusMessage = statusMessage; + _body = body; + _responseHeaders = headers.toArray(new Header[0]); + } + + public Integer statusCode() { + return _statusCode; + } + + public String statusMessage() { + return _statusMessage; + } + + public String body() { + return _body; + } + + public Header[] responseHeaders() { + return _responseHeaders; + } +} diff --git a/client/src/main/java/io/split/client/dtos/UniqueKeys.java b/client/src/main/java/io/split/client/dtos/UniqueKeys.java new file mode 100644 index 00000000..a0866a15 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/UniqueKeys.java @@ -0,0 +1,34 @@ +package io.split.client.dtos; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class UniqueKeys { + + static final String KEYS = "keys"; + + @SerializedName(KEYS) + public List uniqueKeys; + + public UniqueKeys(List uniqueKeys) { + this.uniqueKeys = uniqueKeys; + } + + public static class UniqueKey { + static final String FEATURE = "f"; + + static final String FEATURE_KEYS = "ks"; + + @SerializedName(FEATURE) + public String featureName; + + @SerializedName(FEATURE_KEYS) + public List keysDto; + + public UniqueKey(String featureName, List keysDto) { + this.featureName = featureName; + this.keysDto = keysDto; + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/dtos/pluggable/ConfigPluggable.java b/client/src/main/java/io/split/client/dtos/pluggable/ConfigPluggable.java new file mode 100644 index 00000000..499d5c5c --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/pluggable/ConfigPluggable.java @@ -0,0 +1,22 @@ +package io.split.client.dtos.pluggable; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.Metadata; +import io.split.telemetry.domain.Config; + +public class ConfigPluggable extends Config { + + /* package private */ static final String FIELD_METADATA = "m"; + + @SerializedName(FIELD_METADATA) + private final Metadata _metadata; + + public ConfigPluggable(Metadata metadata) { + super(); + _metadata = metadata; + } + + public Metadata getMetadata() { + return _metadata; + } +} diff --git a/client/src/main/java/io/split/client/dtos/pluggable/EventsPluggable.java b/client/src/main/java/io/split/client/dtos/pluggable/EventsPluggable.java new file mode 100644 index 00000000..6509069f --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/pluggable/EventsPluggable.java @@ -0,0 +1,21 @@ +package io.split.client.dtos.pluggable; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.Event; +import io.split.client.dtos.Metadata; + +public class EventsPluggable extends Event { + + /* package private */ static final String FIELD_METADATA = "m"; + @SerializedName(FIELD_METADATA) + private final Metadata _metadata; + + public EventsPluggable(Metadata metadata) { + super(); + _metadata = metadata; + } + + public Metadata getMetadata() { + return _metadata; + } +} diff --git a/client/src/main/java/io/split/client/dtos/pluggable/ImpressionsPluggable.java b/client/src/main/java/io/split/client/dtos/pluggable/ImpressionsPluggable.java new file mode 100644 index 00000000..cbd0f842 --- /dev/null +++ b/client/src/main/java/io/split/client/dtos/pluggable/ImpressionsPluggable.java @@ -0,0 +1,25 @@ +package io.split.client.dtos.pluggable; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.KeyImpression; +import io.split.client.dtos.Metadata; +import io.split.client.dtos.TestImpressions; + +import java.util.List; + +public class ImpressionsPluggable extends TestImpressions { + + /* package private */ static final String FIELD_METADATA = "m"; + + @SerializedName(FIELD_METADATA) + private final Metadata _metadata; + + public ImpressionsPluggable(Metadata metadata, String testName, List keyImpressions) { + super(testName, keyImpressions); + _metadata = metadata; + } + + public Metadata getMetadata() { + return _metadata; + } +} diff --git a/client/src/main/java/io/split/client/events/EventsSender.java b/client/src/main/java/io/split/client/events/EventsSender.java new file mode 100644 index 00000000..d83969dc --- /dev/null +++ b/client/src/main/java/io/split/client/events/EventsSender.java @@ -0,0 +1,45 @@ +package io.split.client.events; + +import com.google.common.annotations.VisibleForTesting; +import io.split.client.dtos.Event; +import io.split.client.utils.Utils; +import io.split.service.HttpPostImp; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.enums.HttpParamsWrapper; +import io.split.telemetry.storage.TelemetryRuntimeProducer; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class EventsSender { + + private static final String BULK_ENDPOINT_PATH = "api/events/bulk"; + private final URI _bulkEndpoint; + private final SplitHttpClient _client; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final HttpPostImp _httpPostImp; + + public static EventsSender create(SplitHttpClient splitHttpclient, URI eventsTarget, TelemetryRuntimeProducer telemetryRuntimeProducer) + throws URISyntaxException { + return new EventsSender(splitHttpclient, Utils.appendPath(eventsTarget, BULK_ENDPOINT_PATH), telemetryRuntimeProducer); + } + + EventsSender(SplitHttpClient splitHttpclient, URI eventsTarget, TelemetryRuntimeProducer telemetryRuntimeProducer) { + _client = splitHttpclient; + _bulkEndpoint = checkNotNull(eventsTarget); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _httpPostImp = new HttpPostImp(_client, telemetryRuntimeProducer); + } + + public void sendEvents(List _data) { + _httpPostImp.post(_bulkEndpoint, _data, "Events ", HttpParamsWrapper.EVENTS); + } + + @VisibleForTesting + URI getBulkEndpoint() { + return _bulkEndpoint; + } +} diff --git a/client/src/main/java/io/split/client/events/EventsStorage.java b/client/src/main/java/io/split/client/events/EventsStorage.java new file mode 100644 index 00000000..ede0d0aa --- /dev/null +++ b/client/src/main/java/io/split/client/events/EventsStorage.java @@ -0,0 +1,4 @@ +package io.split.client.events; + +public interface EventsStorage extends EventsStorageConsumer, EventsStorageProducer{ +} diff --git a/client/src/main/java/io/split/client/events/EventsStorageConsumer.java b/client/src/main/java/io/split/client/events/EventsStorageConsumer.java new file mode 100644 index 00000000..488be9d9 --- /dev/null +++ b/client/src/main/java/io/split/client/events/EventsStorageConsumer.java @@ -0,0 +1,9 @@ +package io.split.client.events; + +import java.util.List; + +public interface EventsStorageConsumer { + WrappedEvent pop(); + List popAll(); + boolean isFull(); +} diff --git a/client/src/main/java/io/split/client/EventClient.java b/client/src/main/java/io/split/client/events/EventsStorageProducer.java similarity index 53% rename from client/src/main/java/io/split/client/EventClient.java rename to client/src/main/java/io/split/client/events/EventsStorageProducer.java index 61ed0127..ab986310 100644 --- a/client/src/main/java/io/split/client/EventClient.java +++ b/client/src/main/java/io/split/client/events/EventsStorageProducer.java @@ -1,10 +1,7 @@ -package io.split.client; +package io.split.client.events; import io.split.client.dtos.Event; -public interface EventClient { - +public interface EventsStorageProducer { boolean track(Event event, int eventSize); - - void close(); } diff --git a/client/src/main/java/io/split/client/events/EventsTask.java b/client/src/main/java/io/split/client/events/EventsTask.java new file mode 100644 index 00000000..fb71a724 --- /dev/null +++ b/client/src/main/java/io/split/client/events/EventsTask.java @@ -0,0 +1,90 @@ +package io.split.client.events; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import io.split.client.dtos.Event; +import io.split.client.utils.SplitExecutorFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Responsible for sending events added via .track() to Split collection services + */ +public class EventsTask{ + + private final EventsStorageConsumer _eventsStorageConsumer; + private final EventsSender _eventsSender; + private final long _sendIntervalMillis; + + private final ScheduledExecutorService _senderScheduledExecutorService; + private static final Logger _log = LoggerFactory.getLogger(EventsTask.class); + + public static EventsTask create(long sendIntervalMillis, EventsStorageConsumer eventsStorageConsumer, EventsSender eventsSender, + ThreadFactory threadFactory) throws URISyntaxException { + return new EventsTask(eventsStorageConsumer, + sendIntervalMillis, + eventsSender, + threadFactory); + } + + EventsTask(EventsStorageConsumer eventsStorageConsumer, + long sendIntervalMillis, EventsSender eventsSender, ThreadFactory threadFactory) { + + _eventsStorageConsumer = checkNotNull(eventsStorageConsumer); + _sendIntervalMillis = sendIntervalMillis; + _eventsSender = checkNotNull(eventsSender); + _senderScheduledExecutorService = SplitExecutorFactory.buildSingleThreadScheduledExecutor(threadFactory, "Sender-events-%d"); + } + + ThreadFactory eventClientThreadFactory(final String name) { + return new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat(name) + .build(); + } + + public void start(){ + _senderScheduledExecutorService.scheduleWithFixedDelay(() -> { + try { + sendEvents(); + } catch (Exception e) { + _log.error("Error executing Event Action", e); + } + }, _sendIntervalMillis, _sendIntervalMillis, TimeUnit.MILLISECONDS); + } + + public void close() { + try { + sendEvents(); + _senderScheduledExecutorService.shutdown(); + } catch (Exception e) { + _log.warn("Error when shutting down EventClientImpl", e); + } + } + + void sendEvents(){ + if (_eventsStorageConsumer.isFull()) { + _log.warn("Split SDK events queue is full. Events may have been dropped. Consider increasing capacity."); + } + + List wrappedEventList = _eventsStorageConsumer.popAll(); + List eventsToSend = new ArrayList<>(); + for (WrappedEvent wrappedEvent: wrappedEventList){ + Event event = wrappedEvent.event(); + eventsToSend.add(event); + } + + if (eventsToSend.isEmpty()){ + return; + } + _eventsSender.sendEvents(eventsToSend); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/events/InMemoryEventsStorage.java b/client/src/main/java/io/split/client/events/InMemoryEventsStorage.java new file mode 100644 index 00000000..a3463f0e --- /dev/null +++ b/client/src/main/java/io/split/client/events/InMemoryEventsStorage.java @@ -0,0 +1,79 @@ +package io.split.client.events; + +import com.google.common.annotations.VisibleForTesting; +import io.split.client.dtos.Event; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class InMemoryEventsStorage implements EventsStorage{ + + private static final Logger _log = LoggerFactory.getLogger(InMemoryEventsStorage.class); + private final BlockingQueue _eventQueue; + private final int _maxQueueSize; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + + public InMemoryEventsStorage(int maxQueueSize, TelemetryRuntimeProducer telemetryRuntimeProducer) { + _eventQueue = new LinkedBlockingQueue<>(maxQueueSize); + _maxQueueSize = maxQueueSize; + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + } + + @Override + public WrappedEvent pop() { + try { + return _eventQueue.take(); + } catch (InterruptedException e) { + _log.warn("Got interrupted while waiting for an event in the queue."); + } + return null; + } + + @Override + public List popAll() { + ArrayList popped = new ArrayList<>(); + _eventQueue.drainTo(popped); + return popped; + } + + @Override + public boolean isFull() { + return _eventQueue.remainingCapacity() == 0; + } + + @Override + public boolean track(Event event, int eventSize) { + try { + if (event == null) { + return false; + } + if(_eventQueue.offer(new WrappedEvent(event, eventSize))) { + _telemetryRuntimeProducer.recordEventStats(EventsDataRecordsEnum.EVENTS_QUEUED, 1); + } + else { + _log.warn("Event queue is full, dropping event."); + _telemetryRuntimeProducer.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 1); + return false; + } + + } catch (ClassCastException | NullPointerException | IllegalArgumentException e) { + _telemetryRuntimeProducer.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 1); + _log.warn("Interruption when adding event withed while adding message %s.", event); + return false; + } + return true; + } + + @VisibleForTesting + int queueSize() { + return _maxQueueSize - _eventQueue.remainingCapacity(); + } +} diff --git a/client/src/main/java/io/split/client/events/NoopEventsStorageImp.java b/client/src/main/java/io/split/client/events/NoopEventsStorageImp.java new file mode 100644 index 00000000..5e6eb99e --- /dev/null +++ b/client/src/main/java/io/split/client/events/NoopEventsStorageImp.java @@ -0,0 +1,35 @@ +package io.split.client.events; + + +import io.split.client.dtos.Event; + +import java.util.ArrayList; +import java.util.List; + +public class NoopEventsStorageImp implements EventsStorage { + + @Override + public boolean track(Event event, int eventSize) { + return true; + } + + public static NoopEventsStorageImp create() { + return new NoopEventsStorageImp(); + } + + @Override + public WrappedEvent pop() { + return new WrappedEvent(new Event(), 0l); + } + + @Override + public List popAll() { + //no-op + return new ArrayList<>(); + } + + @Override + public boolean isFull() { + return false; + } +} diff --git a/client/src/main/java/io/split/client/events/WrappedEvent.java b/client/src/main/java/io/split/client/events/WrappedEvent.java new file mode 100644 index 00000000..82c0196e --- /dev/null +++ b/client/src/main/java/io/split/client/events/WrappedEvent.java @@ -0,0 +1,21 @@ +package io.split.client.events; + +import io.split.client.dtos.Event; + +public class WrappedEvent { + private final Event _event; + private final long _size; + + public WrappedEvent(Event event, long size) { + _event = event; + _size = size; + } + + public Event event() { + return _event; + } + + public long size() { + return _size; + } +} diff --git a/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java b/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java new file mode 100644 index 00000000..33fba556 --- /dev/null +++ b/client/src/main/java/io/split/client/exceptions/InputStreamProviderException.java @@ -0,0 +1,8 @@ +package io.split.client.exceptions; + +public class InputStreamProviderException extends Exception { + + public InputStreamProviderException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/exceptions/SemverParseException.java b/client/src/main/java/io/split/client/exceptions/SemverParseException.java new file mode 100644 index 00000000..892ba753 --- /dev/null +++ b/client/src/main/java/io/split/client/exceptions/SemverParseException.java @@ -0,0 +1,7 @@ +package io.split.client.exceptions; + +public class SemverParseException extends Exception { + public SemverParseException(String message) { + super(message); + } +} diff --git a/client/src/main/java/io/split/client/exceptions/UriTooLongException.java b/client/src/main/java/io/split/client/exceptions/UriTooLongException.java new file mode 100644 index 00000000..40183852 --- /dev/null +++ b/client/src/main/java/io/split/client/exceptions/UriTooLongException.java @@ -0,0 +1,7 @@ +package io.split.client.exceptions; + +public class UriTooLongException extends Exception { + public UriTooLongException (String message) { + super(message); + } +} diff --git a/client/src/main/java/io/split/client/impressions/AsynchronousImpressionListener.java b/client/src/main/java/io/split/client/impressions/AsynchronousImpressionListener.java index ed6a1e81..66f1d17d 100644 --- a/client/src/main/java/io/split/client/impressions/AsynchronousImpressionListener.java +++ b/client/src/main/java/io/split/client/impressions/AsynchronousImpressionListener.java @@ -44,12 +44,7 @@ public AsynchronousImpressionListener(ImpressionListener delegate, ExecutorServi @Override public void log(final Impression impression) { try { - _executor.execute(new Runnable() { - @Override - public void run() { - _delegate.log(impression); - } - }); + _executor.execute(() -> _delegate.log(impression)); } catch (Exception e) { _log.warn("Unable to send impression to impression listener", e); diff --git a/client/src/main/java/io/split/client/impressions/HttpImpressionsSender.java b/client/src/main/java/io/split/client/impressions/HttpImpressionsSender.java index 336e7f2f..7c346a90 100644 --- a/client/src/main/java/io/split/client/impressions/HttpImpressionsSender.java +++ b/client/src/main/java/io/split/client/impressions/HttpImpressionsSender.java @@ -2,21 +2,29 @@ import com.google.common.annotations.VisibleForTesting; import io.split.client.dtos.ImpressionCount; +import io.split.client.dtos.SplitHttpResponse; import io.split.client.dtos.TestImpressions; +import io.split.client.utils.Json; import io.split.client.utils.Utils; -import org.apache.hc.client5.http.classic.methods.HttpPost; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; -import org.apache.hc.core5.http.HttpEntity; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.apache.hc.core5.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; /** * Created by patricioe on 6/20/16. @@ -29,67 +37,76 @@ public class HttpImpressionsSender implements ImpressionsSender { private static final Logger _logger = LoggerFactory.getLogger(HttpImpressionsSender.class); - private final CloseableHttpClient _client; + private final SplitHttpClient _client; private final URI _impressionBulkTarget; private final URI _impressionCountTarget; private final ImpressionsManager.Mode _mode; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; - public static HttpImpressionsSender create(CloseableHttpClient client, URI eventsRootEndpoint, ImpressionsManager.Mode mode) throws URISyntaxException { + public static HttpImpressionsSender create(SplitHttpClient client, URI eventsRootEndpoint, + ImpressionsManager.Mode mode, + TelemetryRuntimeProducer telemetryRuntimeProducer) throws URISyntaxException { return new HttpImpressionsSender(client, Utils.appendPath(eventsRootEndpoint, BULK_ENDPOINT_PATH), Utils.appendPath(eventsRootEndpoint, COUNT_ENDPOINT_PATH), - mode); + mode, + telemetryRuntimeProducer); } - private HttpImpressionsSender(CloseableHttpClient client, URI impressionBulkTarget, URI impressionCountTarget, ImpressionsManager.Mode mode) { + private HttpImpressionsSender(SplitHttpClient client, URI impressionBulkTarget, URI impressionCountTarget, + ImpressionsManager.Mode mode, + TelemetryRuntimeProducer telemetryRuntimeProducer) { _client = client; _mode = mode; _impressionBulkTarget = impressionBulkTarget; _impressionCountTarget = impressionCountTarget; + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); } @Override public void postImpressionsBulk(List impressions) { - - CloseableHttpResponse response = null; - + long initTime = System.currentTimeMillis(); try { - HttpEntity entity = Utils.toJsonEntity(impressions); - - HttpPost request = new HttpPost(_impressionBulkTarget); - request.addHeader(IMPRESSIONS_MODE_HEADER, _mode.toString()); - request.setEntity(entity); - - response = _client.execute(request); + Map> additionalHeaders = new HashMap<>(); + additionalHeaders.put(IMPRESSIONS_MODE_HEADER, Collections.singletonList(_mode.toString())); + additionalHeaders.put("Content-Type", Collections.singletonList("application/json")); - int status = response.getCode(); + SplitHttpResponse response = _client.post(_impressionBulkTarget, Json.toJson(impressions), + additionalHeaders); - if (status < 200 || status >= 300) { - _logger.warn("Response status was: " + status); + if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + _telemetryRuntimeProducer.recordSyncError(ResourceEnum.IMPRESSION_SYNC, response.statusCode()); } + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS, + System.currentTimeMillis()); } catch (Throwable t) { _logger.warn("Exception when posting impressions" + impressions, t); } finally { - Utils.forceClose(response); + _telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS, + System.currentTimeMillis() - initTime); } - } @Override public void postCounters(HashMap raw) { - if (_mode.equals(ImpressionsManager.Mode.DEBUG)) { - _logger.warn("Attempted to submit counters in impressions debugging mode. Ignoring"); - return; - } + long initTime = System.currentTimeMillis(); + try { + + Map> additionalHeaders = new HashMap<>(); + additionalHeaders.put("Content-Type", Collections.singletonList("application/json")); + + SplitHttpResponse response = _client.post(_impressionCountTarget, + Json.toJson(ImpressionCount.fromImpressionCounterData(raw)), + null); - HttpPost request = new HttpPost(_impressionCountTarget); - request.setEntity(Utils.toJsonEntity(ImpressionCount.fromImpressionCounterData(raw))); - try (CloseableHttpResponse response = _client.execute(request)) { - int status = response.getCode(); - if (status < 200 || status >= 300) { - _logger.warn("Response status was: " + status); + if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + _telemetryRuntimeProducer.recordSyncError(ResourceEnum.IMPRESSION_COUNT_SYNC, response.statusCode()); } + _telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS_COUNT, + System.currentTimeMillis() - initTime); + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT, + System.currentTimeMillis()); } catch (IOException exc) { _logger.warn("Exception when posting impression counters: ", exc); } diff --git a/client/src/main/java/io/split/client/impressions/Impression.java b/client/src/main/java/io/split/client/impressions/Impression.java index fdabb553..fc7b7314 100644 --- a/client/src/main/java/io/split/client/impressions/Impression.java +++ b/client/src/main/java/io/split/client/impressions/Impression.java @@ -16,17 +16,20 @@ public class Impression { private final Long _changeNumber; private Long _pt; private final Map _attributes; + private final String _properties; - public Impression(String key, String bucketingKey, String split, String treatment, long time, String appliedRule, Long changeNumber, Map atributes) { + public Impression(String key, String bucketingKey, String featureFlag, String treatment, long time, String appliedRule, + Long changeNumber, Map atributes, String properties) { _key = key; _bucketingKey = bucketingKey; - _split = split; + _split = featureFlag; _treatment = treatment; _time = time; _appliedRule = appliedRule; _changeNumber = changeNumber; _attributes = atributes; + _properties = properties; } public String key() { @@ -66,4 +69,8 @@ public Long pt() { } public Impression withPreviousTime(Long pt) { _pt = pt; return this; } + + public String properties() { + return _properties; + } } diff --git a/client/src/main/java/io/split/client/impressions/ImpressionCounter.java b/client/src/main/java/io/split/client/impressions/ImpressionCounter.java index ca1d5ff7..381177a8 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionCounter.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionCounter.java @@ -13,12 +13,12 @@ public static class Key { private final String _featureName; private final long _timeFrame; - public Key(String featureName, long timeframe) { - _featureName = checkNotNull(featureName); + public Key(String featureFlagName, long timeframe) { + _featureName = checkNotNull(featureFlagName); _timeFrame = timeframe; } - public String featureName() { return _featureName; } + public String featureName() { return _featureName; } public long timeFrame() { return _timeFrame; } @Override @@ -43,8 +43,8 @@ public ImpressionCounter() { _counts = new ConcurrentHashMap<>(); } - public void inc(String featureName, long timeFrame, int amount) { - Key key = new Key(featureName, ImpressionUtils.truncateTimeframe(timeFrame)); + public void inc(String featureFlagName, long timeFrame, int amount) { + Key key = new Key(featureFlagName, ImpressionUtils.truncateTimeframe(timeFrame)); AtomicInteger count = _counts.get(key); if (Objects.isNull(count)) { count = new AtomicInteger(); @@ -66,4 +66,4 @@ public HashMap popAll() { } public boolean isEmpty() { return _counts.isEmpty(); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/impressions/ImpressionListener.java b/client/src/main/java/io/split/client/impressions/ImpressionListener.java index 3726b598..dc15511f 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionListener.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionListener.java @@ -21,18 +21,6 @@ public interface ImpressionListener { */ void close(); - final class NoopImpressionListener implements ImpressionListener { - @Override - public void log(Impression impression) { - // noop - } - - @Override - public void close() { - // noop - } - } - final class FederatedImpressionListener implements ImpressionListener { private List _delegates; diff --git a/client/src/main/java/io/split/client/impressions/ImpressionsManager.java b/client/src/main/java/io/split/client/impressions/ImpressionsManager.java index e55d23b4..ce7c6201 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionsManager.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionsManager.java @@ -1,17 +1,34 @@ package io.split.client.impressions; +import io.split.client.dtos.DecoratedImpression; + +import java.util.List; + public interface ImpressionsManager { public enum Mode { OPTIMIZED, - DEBUG + DEBUG, + NONE } - void track(Impression impression); + void track(List decoratedImpressions); + void start(); + void close(); final class NoOpImpressionsManager implements ImpressionsManager { @Override - public void track(Impression impression) { /* do nothing */ } + public void track(List decoratedImpressions) { /* do nothing */ } + + @Override + public void start(){ + /* do nothing */ + } + + @Override + public void close() { + /* do nothing */ + } } } diff --git a/client/src/main/java/io/split/client/impressions/ImpressionsManagerImpl.java b/client/src/main/java/io/split/client/impressions/ImpressionsManagerImpl.java index ef20caff..3b784aba 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionsManagerImpl.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionsManagerImpl.java @@ -1,23 +1,27 @@ package io.split.client.impressions; import com.google.common.annotations.VisibleForTesting; -import com.google.common.util.concurrent.ThreadFactoryBuilder; import io.split.client.SplitClientConfig; +import io.split.client.dtos.DecoratedImpression; import io.split.client.dtos.KeyImpression; import io.split.client.dtos.TestImpressions; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import io.split.client.impressions.strategy.ProcessImpressionNone; +import io.split.client.impressions.strategy.ProcessImpressionStrategy; +import io.split.client.utils.SplitExecutorFactory; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; -import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.List; import java.util.Objects; -import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Preconditions.checkNotNull; @@ -31,85 +35,140 @@ public class ImpressionsManagerImpl implements ImpressionsManager, Closeable { private static final long BULK_INITIAL_DELAY_SECONDS = 10L; private static final long COUNT_INITIAL_DELAY_SECONDS = 100L; private static final long COUNT_REFRESH_RATE_SECONDS = 30 * 60; - private static final long LAST_SEEN_CACHE_SIZE = 500000; // cache up to 500k impression hashes - private final SplitClientConfig _config; - private final ImpressionsStorage _storage; + private final ImpressionsStorageProducer _impressionsStorageProducer; + private final ImpressionsStorageConsumer _impressionsStorageConsumer; private final ScheduledExecutorService _scheduler; private final ImpressionsSender _impressionsSender; - private final ImpressionObserver _impressionObserver; - private final ImpressionCounter _counter; private final ImpressionListener _listener; - private final ImpressionsManager.Mode _mode; - - public static ImpressionsManagerImpl instance(CloseableHttpClient client, - SplitClientConfig config, - List listeners) throws URISyntaxException { - return new ImpressionsManagerImpl(client, config, null, listeners); + private final ImpressionsManager.Mode _impressionsMode; + private TelemetryRuntimeProducer _telemetryRuntimeProducer; + private ImpressionCounter _counter; + private ProcessImpressionStrategy _processImpressionStrategy; + private ProcessImpressionNone _processImpressionNone; + + private final int _impressionsRefreshRate; + + public static ImpressionsManagerImpl instance(SplitClientConfig config, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ImpressionsStorageConsumer impressionsStorageConsumer, + ImpressionsStorageProducer impressionsStorageProducer, + ImpressionsSender impressionsSender, + ProcessImpressionNone processImpressionNone, + ProcessImpressionStrategy processImpressionStrategy, + ImpressionCounter counter, + ImpressionListener listener) throws URISyntaxException { + return new ImpressionsManagerImpl(config, impressionsSender, telemetryRuntimeProducer, impressionsStorageConsumer, + impressionsStorageProducer, processImpressionNone, processImpressionStrategy, counter, listener); } - public static ImpressionsManagerImpl instanceForTest(CloseableHttpClient client, - SplitClientConfig config, + public static ImpressionsManagerImpl instanceForTest(SplitClientConfig config, ImpressionsSender impressionsSender, - List listeners) throws URISyntaxException { - return new ImpressionsManagerImpl(client, config, impressionsSender, listeners); + TelemetryRuntimeProducer telemetryRuntimeProducer, + ImpressionsStorageConsumer impressionsStorageConsumer, + ImpressionsStorageProducer impressionsStorageProducer, + ProcessImpressionNone processImpressionNone, + ProcessImpressionStrategy processImpressionStrategy, + ImpressionCounter counter, + ImpressionListener listener) { + return new ImpressionsManagerImpl(config, impressionsSender, telemetryRuntimeProducer, impressionsStorageConsumer, + impressionsStorageProducer, processImpressionNone, processImpressionStrategy, counter, listener); } - private ImpressionsManagerImpl(CloseableHttpClient client, - SplitClientConfig config, + private ImpressionsManagerImpl(SplitClientConfig config, ImpressionsSender impressionsSender, - List listeners) throws URISyntaxException { + TelemetryRuntimeProducer telemetryRuntimeProducer, + ImpressionsStorageConsumer impressionsStorageConsumer, + ImpressionsStorageProducer impressionsStorageProducer, + ProcessImpressionNone processImpressionNone, + ProcessImpressionStrategy processImpressionStrategy, + ImpressionCounter impressionCounter, + ImpressionListener impressionListener) { _config = checkNotNull(config); - _mode = checkNotNull(config.impressionsMode()); - _storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); - _impressionObserver = new ImpressionObserver(LAST_SEEN_CACHE_SIZE); - _counter = new ImpressionCounter(); - _impressionsSender = (null != impressionsSender) ? impressionsSender - : HttpImpressionsSender.create(client, URI.create(config.eventsEndpoint()), _mode); - - _scheduler = buildExecutor(); - _scheduler.scheduleAtFixedRate(this::sendImpressions, BULK_INITIAL_DELAY_SECONDS,config.impressionsRefreshRate(), TimeUnit.SECONDS); - if (Mode.OPTIMIZED.equals(_mode)) { - _scheduler.scheduleAtFixedRate(this::sendImpressionCounters, COUNT_INITIAL_DELAY_SECONDS, COUNT_REFRESH_RATE_SECONDS, TimeUnit.SECONDS); - } - - _listener = (null != listeners && !listeners.isEmpty()) ? new ImpressionListener.FederatedImpressionListener(listeners) - : new ImpressionListener.NoopImpressionListener(); + _impressionsMode = checkNotNull(config.impressionsMode()); + _impressionsStorageConsumer = checkNotNull(impressionsStorageConsumer); + _impressionsStorageProducer = checkNotNull(impressionsStorageProducer); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _processImpressionNone = checkNotNull(processImpressionNone); + _processImpressionStrategy = checkNotNull(processImpressionStrategy); + _impressionsSender = impressionsSender; + _counter = impressionCounter; + + _scheduler = SplitExecutorFactory.buildScheduledExecutorService(config.getThreadFactory(), "Split-ImpressionsManager-%d", 2); + _listener = impressionListener; + + _impressionsRefreshRate = config.impressionsRefreshRate(); } - private static boolean shouldQueueImpression(Impression i) { - return Objects.isNull(i.pt()) || - ImpressionUtils.truncateTimeframe(i.pt()) != ImpressionUtils.truncateTimeframe(i.time()); + @Override + public void start(){ + switch (_impressionsMode){ + case OPTIMIZED: + _scheduler.scheduleAtFixedRate(this::sendImpressionCounters, COUNT_INITIAL_DELAY_SECONDS, COUNT_REFRESH_RATE_SECONDS, + TimeUnit.SECONDS); + _scheduler.scheduleAtFixedRate(this::sendImpressions, BULK_INITIAL_DELAY_SECONDS, _impressionsRefreshRate, TimeUnit.SECONDS); + break; + case DEBUG: + _scheduler.scheduleAtFixedRate(this::sendImpressions, BULK_INITIAL_DELAY_SECONDS, _impressionsRefreshRate, TimeUnit.SECONDS); + _scheduler.scheduleAtFixedRate(this::sendImpressionCounters, COUNT_INITIAL_DELAY_SECONDS, COUNT_REFRESH_RATE_SECONDS, + TimeUnit.SECONDS); + break; + case NONE: + _scheduler.scheduleAtFixedRate(this::sendImpressionCounters, COUNT_INITIAL_DELAY_SECONDS, COUNT_REFRESH_RATE_SECONDS, + TimeUnit.SECONDS); + break; + } } @Override - public void track(Impression impression) { - if (null == impression) { + public void track(List decoratedImpressions) { + if (null == decoratedImpressions) { return; } - - impression = impression.withPreviousTime(_impressionObserver.testAndSet(impression)); - _listener.log(impression); - - if (Mode.OPTIMIZED.equals(_mode)) { - _counter.inc(impression.split(), impression.time(), 1); + List impressionsForLogs = new ArrayList<>(); + List impressionsToListener = new ArrayList<>(); + + for (int i = 0; i < decoratedImpressions.size(); i++) { + ImpressionsResult impressionsResult; + if (!decoratedImpressions.get(i).disabled()) { + impressionsResult = _processImpressionStrategy.process(Stream.of( + decoratedImpressions.get(i).impression()).collect(Collectors.toList())); + } else { + impressionsResult = _processImpressionNone.process(Stream.of( + decoratedImpressions.get(i).impression()).collect(Collectors.toList())); + } + if (!Objects.isNull(impressionsResult.getImpressionsToQueue())) { + impressionsForLogs.addAll(impressionsResult.getImpressionsToQueue()); + } + if (!Objects.isNull(impressionsResult.getImpressionsToListener())) + impressionsToListener.addAll(impressionsResult.getImpressionsToListener()); } + int totalImpressions = impressionsForLogs.size(); + long queued = _impressionsStorageProducer.put(impressionsForLogs.stream().map(KeyImpression::fromImpression).collect(Collectors.toList())); + if (queued < totalImpressions) { + _telemetryRuntimeProducer.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, totalImpressions-queued); + } + _telemetryRuntimeProducer.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_QUEUED, queued); - if (Mode.DEBUG.equals(_mode) || shouldQueueImpression(impression)) { - _storage.put(KeyImpression.fromImpression(impression)); + if (_listener!=null){ + impressionsToListener.forEach(imp -> _listener.log(imp)); } } @Override public void close() { try { - _listener.close(); - _log.info("Successful shutdown of ImpressionListener"); + if(_listener!= null){ + _listener.close(); + _log.info("Successful shutdown of ImpressionListener"); + } _scheduler.shutdown(); sendImpressions(); - _scheduler.awaitTermination(_config.waitBeforeShutdown(), TimeUnit.MILLISECONDS); + if(_counter != null) { + sendImpressionCounters(); + } } catch (Exception e) { _log.warn("Unable to close ImpressionsManager properly", e); } @@ -118,35 +177,32 @@ public void close() { @VisibleForTesting /* package private */ void sendImpressions() { - if (_storage.isFull()) { + if (_impressionsStorageConsumer.isFull()) { _log.warn("Split SDK impressions queue is full. Impressions may have been dropped. Consider increasing capacity."); } long start = System.currentTimeMillis(); - List impressions = _storage.pop(); + List impressions = _impressionsStorageConsumer.pop(); if (impressions.isEmpty()) { return; // Nothing to send } _impressionsSender.postImpressionsBulk(TestImpressions.fromKeyImpressions(impressions)); - if(_config.debugEnabled()) { + if (_config.debugEnabled()) { _log.info(String.format("Posting %d Split impressions took %d millis", impressions.size(), (System.currentTimeMillis() - start))); } } @VisibleForTesting - /* package private */ void sendImpressionCounters() { + /* package private */ void sendImpressionCounters() { if (!_counter.isEmpty()) { _impressionsSender.postCounters(_counter.popAll()); } } - private ScheduledExecutorService buildExecutor() { - ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("Split-ImpressionsManager-%d") - .build(); - return Executors.newScheduledThreadPool(2, threadFactory); + @VisibleForTesting + /* package private */ ImpressionCounter getCounter() { + return _counter; } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/impressions/ImpressionsResult.java b/client/src/main/java/io/split/client/impressions/ImpressionsResult.java new file mode 100644 index 00000000..1467ebb7 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/ImpressionsResult.java @@ -0,0 +1,22 @@ +package io.split.client.impressions; + +import java.util.List; + +public class ImpressionsResult { + + private List impressionsToListener; + private List impressionsToQueue; + + public ImpressionsResult(List impressionsForLogs, List impressionsToListener) { + this.impressionsToListener = impressionsToListener; + this.impressionsToQueue = impressionsForLogs; + } + + public List getImpressionsToQueue() { + return impressionsToQueue; + } + + public List getImpressionsToListener() { + return impressionsToListener; + } +} diff --git a/client/src/main/java/io/split/client/impressions/ImpressionsStorage.java b/client/src/main/java/io/split/client/impressions/ImpressionsStorage.java index d9e569b6..988f3d11 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionsStorage.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionsStorage.java @@ -1,8 +1,4 @@ package io.split.client.impressions; -import io.split.client.dtos.KeyImpression; - public interface ImpressionsStorage extends ImpressionsStorageConsumer, ImpressionsStorageProducer { - - boolean put(KeyImpression imp); } diff --git a/client/src/main/java/io/split/client/impressions/ImpressionsStorageProducer.java b/client/src/main/java/io/split/client/impressions/ImpressionsStorageProducer.java index f134a9b6..51bfb977 100644 --- a/client/src/main/java/io/split/client/impressions/ImpressionsStorageProducer.java +++ b/client/src/main/java/io/split/client/impressions/ImpressionsStorageProducer.java @@ -5,6 +5,5 @@ import java.util.List; public interface ImpressionsStorageProducer { - boolean put(KeyImpression imps); - boolean put(List imps); + long put(List imps); } diff --git a/client/src/main/java/io/split/client/impressions/InMemoryImpressionsStorage.java b/client/src/main/java/io/split/client/impressions/InMemoryImpressionsStorage.java index 0ed5b7af..90d6b9a4 100644 --- a/client/src/main/java/io/split/client/impressions/InMemoryImpressionsStorage.java +++ b/client/src/main/java/io/split/client/impressions/InMemoryImpressionsStorage.java @@ -38,8 +38,7 @@ public boolean isFull() { return _queue.remainingCapacity() == 0; } - @Override - public boolean put(KeyImpression imp) { + private boolean put(KeyImpression imp) { try { return _queue.offer(imp); } catch (ClassCastException | NullPointerException | IllegalArgumentException e) { @@ -49,7 +48,12 @@ public boolean put(KeyImpression imp) { } @Override - public boolean put(List imps) { - return false; + public long put(List imps) { + return imps.stream().reduce(0, (accum, current) -> { + if(this.put(current)) { + return accum + 1; + } + return accum; + }, Integer::sum).longValue(); } } \ No newline at end of file diff --git a/client/src/main/java/io/split/client/impressions/PluggableImpressionSender.java b/client/src/main/java/io/split/client/impressions/PluggableImpressionSender.java new file mode 100644 index 00000000..2162f303 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/PluggableImpressionSender.java @@ -0,0 +1,48 @@ +package io.split.client.impressions; + +import io.split.client.dtos.TestImpressions; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserPipelineWrapper; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; + +import java.util.HashMap; +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class PluggableImpressionSender implements ImpressionsSender{ + + private final UserStorageWrapper _userStorageWrapper; + + private static final Logger _logger = LoggerFactory.getLogger(PluggableImpressionSender.class); + + public static PluggableImpressionSender create(CustomStorageWrapper customStorageWrapper){ + return new PluggableImpressionSender(customStorageWrapper); + } + + private PluggableImpressionSender(CustomStorageWrapper customStorageWrapper) { + this._userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + + @Override + public void postImpressionsBulk(List impressions) { + //No-Op + } + + @Override + public void postCounters(HashMap counts) { + try { + UserPipelineWrapper pipelineExecution = _userStorageWrapper.pipeline(); + for(ImpressionCounter.Key countsKey: counts.keySet()){ + String key = PrefixAdapter.buildImpressionsCount(); + pipelineExecution.hIncrement(key, countsKey.featureName() + "::" + countsKey.timeFrame(), counts.get(countsKey)); + } + pipelineExecution.exec(); + } catch (Exception e){ + _logger.warn("Redis pipeline exception when posting counters: ", e); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/impressions/UniqueKeysTracker.java b/client/src/main/java/io/split/client/impressions/UniqueKeysTracker.java new file mode 100644 index 00000000..6d813d8a --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/UniqueKeysTracker.java @@ -0,0 +1,8 @@ +package io.split.client.impressions; + +public interface UniqueKeysTracker { + + boolean track (String featureName, String key); + void start(); + void stop(); +} diff --git a/client/src/main/java/io/split/client/impressions/UniqueKeysTrackerImp.java b/client/src/main/java/io/split/client/impressions/UniqueKeysTrackerImp.java new file mode 100644 index 00000000..c0034b6b --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/UniqueKeysTrackerImp.java @@ -0,0 +1,207 @@ +package io.split.client.impressions; + +import com.google.common.collect.Lists; +import io.split.client.dtos.UniqueKeys; +import io.split.client.impressions.filters.BloomFilterImp; +import io.split.client.impressions.filters.Filter; +import io.split.client.impressions.filters.FilterAdapter; +import io.split.client.impressions.filters.FilterAdapterImpl; +import io.split.client.utils.SplitExecutorFactory; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +public class UniqueKeysTrackerImp implements UniqueKeysTracker{ + private static final Logger _log = LoggerFactory.getLogger(UniqueKeysTrackerImp.class); + private static final double MARGIN_ERROR = 0.01; + private static final int MAX_UNIQUE_KEYS_POST_SIZE = 5000; + private static final int MAX_AMOUNT_OF_KEYS = 10000000; + private final AtomicInteger trackerKeysSize = new AtomicInteger(0); + private FilterAdapter filterAdapter; + private final TelemetrySynchronizer _telemetrySynchronizer; + private final ScheduledExecutorService _uniqueKeysSyncScheduledExecutorService; + private final ScheduledExecutorService _cleanFilterScheduledExecutorService; + private final ConcurrentHashMap> uniqueKeysTracker; + private final int _uniqueKeysRefreshRate; + private final int _filterRefreshRate; + private final AtomicBoolean sendGuard = new AtomicBoolean(false); + private static final Logger _logger = LoggerFactory.getLogger(UniqueKeysTrackerImp.class); + + public UniqueKeysTrackerImp(TelemetrySynchronizer telemetrySynchronizer, int uniqueKeysRefreshRate, int filterRefreshRate, + ThreadFactory threadFactory) { + Filter bloomFilter = new BloomFilterImp(MAX_AMOUNT_OF_KEYS, MARGIN_ERROR); + this.filterAdapter = new FilterAdapterImpl(bloomFilter); + uniqueKeysTracker = new ConcurrentHashMap<>(); + _telemetrySynchronizer = telemetrySynchronizer; + _uniqueKeysRefreshRate = uniqueKeysRefreshRate; + _filterRefreshRate = filterRefreshRate; + _uniqueKeysSyncScheduledExecutorService = SplitExecutorFactory.buildSingleThreadScheduledExecutor(threadFactory,"UniqueKeys-sync-%d"); + _cleanFilterScheduledExecutorService = SplitExecutorFactory.buildSingleThreadScheduledExecutor(threadFactory,"Filter-%d"); + } + + @Override + public boolean track(String featureFlagName, String key) { + if (!filterAdapter.add(featureFlagName, key)) { + _logger.debug("The feature flag " + featureFlagName + " and key " + key + " exist in the UniqueKeysTracker"); + return false; + } + uniqueKeysTracker.compute(featureFlagName, + (feature, current) -> { + HashSet keysByFeature = Optional.ofNullable(current).orElse(new HashSet<>()); + keysByFeature.add(key); + trackerKeysSize.incrementAndGet(); + return keysByFeature; + }); + _logger.debug("The feature flag " + featureFlagName + " and key " + key + " was added"); + if (trackerKeysSize.intValue() >= MAX_UNIQUE_KEYS_POST_SIZE){ + _logger.warn("The UniqueKeysTracker size reached the maximum limit"); + try { + sendUniqueKeys(); + } catch (Exception e) { + _log.error("Error sending unique keys.", e); + } + } + return true; + } + + @Override + public void start() { + scheduleWithFixedDelay(_uniqueKeysSyncScheduledExecutorService, _uniqueKeysRefreshRate, new ExecuteSendUniqueKeys()); + scheduleWithFixedDelay(_cleanFilterScheduledExecutorService, _filterRefreshRate, new ExecuteCleanFilter()); + } + + private void scheduleWithFixedDelay(ScheduledExecutorService scheduledExecutorService, int refreshRate, + ExecuteUniqueKeysAction executeUniqueKeysAction) { + scheduledExecutorService.scheduleWithFixedDelay(() -> { + try { + executeUniqueKeysAction.execute(); + } catch (Exception e) { + _log.error("Error executing an Unique Key Action.", e); + } + }, refreshRate, refreshRate, TimeUnit.SECONDS); + } + + @Override + public void stop() { + try { + sendUniqueKeys(); + } catch (Exception e) { + _log.error("Error sending unique keys."); + } + _uniqueKeysSyncScheduledExecutorService.shutdown(); + _cleanFilterScheduledExecutorService.shutdown(); + } + + public HashMap> popAll(){ + HashMap> toReturn = new HashMap<>(); + for (String key : uniqueKeysTracker.keySet()) { + HashSet value = uniqueKeysTracker.remove(key); + toReturn.put(key, value); + } + trackerKeysSize.set(0); + return toReturn; + } + + private void sendUniqueKeys(){ + if (!sendGuard.compareAndSet(false, true)) { + _log.debug("SendUniqueKeys already running"); + return; + } + + try { + if (uniqueKeysTracker.isEmpty()) { + _log.debug("The Unique Keys Tracker is empty"); + return; + } + + HashMap> uniqueKeysHashMap = popAll(); + List uniqueKeysFromPopAll = new ArrayList<>(); + for (Map.Entry> uniqueKeyEntry : uniqueKeysHashMap.entrySet()) { + UniqueKeys.UniqueKey uniqueKey = new UniqueKeys.UniqueKey(uniqueKeyEntry.getKey(), new ArrayList<>(uniqueKeyEntry.getValue())); + uniqueKeysFromPopAll.add(uniqueKey); + } + uniqueKeysFromPopAll = capChunksToMaxSize(uniqueKeysFromPopAll); + + for (List chunk : getChunks(uniqueKeysFromPopAll)) { + _telemetrySynchronizer.synchronizeUniqueKeys(new UniqueKeys(chunk)); + } + } finally { + sendGuard.set(false); + } + } + + private List capChunksToMaxSize(List uniqueKeys) { + List finalChunk = new ArrayList<>(); + for (UniqueKeys.UniqueKey uniqueKey : uniqueKeys) { + if (uniqueKey.keysDto.size() > MAX_UNIQUE_KEYS_POST_SIZE) { + for(List subChunk : Lists.partition(uniqueKey.keysDto, MAX_UNIQUE_KEYS_POST_SIZE)) { + finalChunk.add(new UniqueKeys.UniqueKey(uniqueKey.featureName, subChunk)); + } + continue; + } + finalChunk.add(uniqueKey); + } + return finalChunk; + } + + private List> getChunks(List uniqueKeys) { + List> chunks = new ArrayList<>(); + List intermediateChunk = new ArrayList<>(); + for (UniqueKeys.UniqueKey uniqueKey : uniqueKeys) { + if ((getChunkSize(intermediateChunk) + uniqueKey.keysDto.size()) > MAX_UNIQUE_KEYS_POST_SIZE) { + chunks.add(intermediateChunk); + intermediateChunk = new ArrayList<>(); + } + intermediateChunk.add(uniqueKey); + } + if (!intermediateChunk.isEmpty()) { + chunks.add(intermediateChunk); + } + return chunks; + } + + private int getChunkSize(List uniqueKeysChunk) { + int totalSize = 0; + for (UniqueKeys.UniqueKey uniqueKey : uniqueKeysChunk) { + totalSize += uniqueKey.keysDto.size(); + } + return totalSize; + } + + private interface ExecuteUniqueKeysAction{ + void execute(); + } + + private class ExecuteCleanFilter implements ExecuteUniqueKeysAction { + + @Override + public void execute() { + filterAdapter.clear(); + } + } + + private class ExecuteSendUniqueKeys implements ExecuteUniqueKeysAction { + + @Override + public void execute() { + sendUniqueKeys(); + } + } + + public AtomicBoolean getSendGuard() { + return sendGuard; + } +} diff --git a/client/src/main/java/io/split/client/impressions/filters/BloomFilterImp.java b/client/src/main/java/io/split/client/impressions/filters/BloomFilterImp.java new file mode 100644 index 00000000..f048711f --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/filters/BloomFilterImp.java @@ -0,0 +1,34 @@ +package io.split.client.impressions.filters; + +import com.google.common.hash.BloomFilter; +import com.google.common.hash.Funnels; +import java.nio.charset.StandardCharsets; + +public class BloomFilterImp implements Filter { + + private BloomFilter bloomFilter; + private final int size; + private final double errorMargin; + + public BloomFilterImp(int size, double errorMargin) { + this.size = size; + this.errorMargin = errorMargin; + this.bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), size, errorMargin); + } + + @Override + public boolean add(String data) { + return bloomFilter.put(data); + } + + @Override + public boolean contains(String data) { + return bloomFilter.mightContain(data); + } + + @Override + public void clear() { + bloomFilter = BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_16), size, errorMargin); + + } +} diff --git a/client/src/main/java/io/split/client/impressions/filters/Filter.java b/client/src/main/java/io/split/client/impressions/filters/Filter.java new file mode 100644 index 00000000..af5e8f8b --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/filters/Filter.java @@ -0,0 +1,8 @@ +package io.split.client.impressions.filters; + +public interface Filter { + + boolean add(String data); + boolean contains(String data); + void clear(); +} diff --git a/client/src/main/java/io/split/client/impressions/filters/FilterAdapter.java b/client/src/main/java/io/split/client/impressions/filters/FilterAdapter.java new file mode 100644 index 00000000..e81e0bd9 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/filters/FilterAdapter.java @@ -0,0 +1,8 @@ +package io.split.client.impressions.filters; + +public interface FilterAdapter { + + boolean add(String featureFlagName, String key); + boolean contains(String featureFlagName, String key); + void clear(); +} diff --git a/client/src/main/java/io/split/client/impressions/filters/FilterAdapterImpl.java b/client/src/main/java/io/split/client/impressions/filters/FilterAdapterImpl.java new file mode 100644 index 00000000..0bdb8a16 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/filters/FilterAdapterImpl.java @@ -0,0 +1,25 @@ +package io.split.client.impressions.filters; + +public class FilterAdapterImpl implements FilterAdapter { + + private final Filter filter; + + public FilterAdapterImpl(Filter filter) { + this.filter = filter; + } + + @Override + public boolean add(String featureFlagName, String key) { + return filter.add(featureFlagName + key); + } + + @Override + public boolean contains(String featureFlagName, String key) { + return filter.contains(featureFlagName + key); + } + + @Override + public void clear() { + filter.clear(); + } +} diff --git a/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionDebug.java b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionDebug.java new file mode 100644 index 00000000..0ac5a20d --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionDebug.java @@ -0,0 +1,30 @@ +package io.split.client.impressions.strategy; + +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionObserver; +import io.split.client.impressions.ImpressionsResult; + +import java.util.List; + +public class ProcessImpressionDebug implements ProcessImpressionStrategy{ + + private final ImpressionObserver _impressionObserver; + private final boolean _listenerEnabled; + + public ProcessImpressionDebug(boolean listenerEnabled, ImpressionObserver impressionObserver) { + _listenerEnabled = listenerEnabled; + _impressionObserver = impressionObserver; + } + + @Override + public ImpressionsResult process(List impressions) { + for(Impression impression : impressions) { + if (impression.properties() != null) { + continue; + } + impression.withPreviousTime(_impressionObserver.testAndSet(impression)); + } + List impressionForListener = this._listenerEnabled ? impressions : null; + return new ImpressionsResult(impressions, impressionForListener); + } +} diff --git a/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionNone.java b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionNone.java new file mode 100644 index 00000000..4764323d --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionNone.java @@ -0,0 +1,33 @@ +package io.split.client.impressions.strategy; + +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionCounter; +import io.split.client.impressions.ImpressionsResult; +import io.split.client.impressions.UniqueKeysTracker; + +import java.util.ArrayList; +import java.util.List; + +public class ProcessImpressionNone implements ProcessImpressionStrategy{ + + private final UniqueKeysTracker _uniqueKeysTracker; + private final ImpressionCounter _impressionCounter; + private final boolean _listenerEnabled; + + public ProcessImpressionNone(boolean listenerEnabled,UniqueKeysTracker uniqueKeysTracker, ImpressionCounter impressionCounter) { + _listenerEnabled = listenerEnabled; + _uniqueKeysTracker = uniqueKeysTracker; + _impressionCounter = impressionCounter; + } + + @Override + public ImpressionsResult process(List impressions) { + + for(Impression impression: impressions){ + _impressionCounter.inc(impression.split(), impression.time(), 1); + _uniqueKeysTracker.track(impression.split(),impression.key()); + } + List impressionForListener = this._listenerEnabled ? impressions : null; + return new ImpressionsResult(new ArrayList<>(), impressionForListener); + } +} diff --git a/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionOptimized.java b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionOptimized.java new file mode 100644 index 00000000..65fd9ab4 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionOptimized.java @@ -0,0 +1,58 @@ +package io.split.client.impressions.strategy; + +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionObserver; +import io.split.client.impressions.ImpressionUtils; +import io.split.client.impressions.ImpressionsResult; +import io.split.client.impressions.ImpressionCounter; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class ProcessImpressionOptimized implements ProcessImpressionStrategy{ + + private final ImpressionObserver _impressionObserver; + private final ImpressionCounter _impressionCounter; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final boolean _listenerEnabled; + + + public ProcessImpressionOptimized(boolean listenerEnabled, ImpressionObserver impressionObserver, ImpressionCounter impressionCounter, + TelemetryRuntimeProducer telemetryRuntimeProducer) { + _telemetryRuntimeProducer = telemetryRuntimeProducer; + _listenerEnabled = listenerEnabled; + _impressionObserver = impressionObserver; + _impressionCounter = impressionCounter; + } + + @Override + public ImpressionsResult process(List impressions) { + List impressionsToQueue = new ArrayList<>(); + for(Impression impression : impressions) { + if (impression.properties() == null) { + impression = impression.withPreviousTime(_impressionObserver.testAndSet(impression)); + if (!Objects.isNull(impression.pt()) && impression.pt() != 0) { + _impressionCounter.inc(impression.split(), impression.time(), 1); + } + if (shouldntQueueImpression(impression)) { + continue; + } + } + impressionsToQueue.add(impression); + } + List impressionForListener = this._listenerEnabled ? impressions : null; + + _telemetryRuntimeProducer.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, impressions.size()- + (long)impressionsToQueue.size()); + + return new ImpressionsResult(impressionsToQueue, impressionForListener); + } + + private boolean shouldntQueueImpression(Impression i) { + return !Objects.isNull(i.pt()) && + ImpressionUtils.truncateTimeframe(i.pt()) == ImpressionUtils.truncateTimeframe(i.time()); + } +} diff --git a/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionStrategy.java b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionStrategy.java new file mode 100644 index 00000000..48791001 --- /dev/null +++ b/client/src/main/java/io/split/client/impressions/strategy/ProcessImpressionStrategy.java @@ -0,0 +1,11 @@ +package io.split.client.impressions.strategy; + +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionsResult; + +import java.util.List; + +public interface ProcessImpressionStrategy { + + ImpressionsResult process(List impressions); +} diff --git a/client/src/main/java/io/split/client/interceptors/AddSplitHeadersFilter.java b/client/src/main/java/io/split/client/interceptors/AddSplitHeadersFilter.java deleted file mode 100644 index 3367ac53..00000000 --- a/client/src/main/java/io/split/client/interceptors/AddSplitHeadersFilter.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.split.client.interceptors; - -import io.split.client.SplitClientConfig; -import org.apache.hc.core5.http.EntityDetails; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.HttpRequest; -import org.apache.hc.core5.http.HttpRequestInterceptor; -import org.apache.hc.core5.http.protocol.HttpContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.InetAddress; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Created by adilaijaz on 5/22/15. - */ -public class AddSplitHeadersFilter implements HttpRequestInterceptor { - private static final Logger _log = LoggerFactory.getLogger(AddSplitHeadersFilter.class); - - /* package private for testing purposes */ - static final String AUTHORIZATION_HEADER = "Authorization"; - static final String CLIENT_MACHINE_NAME_HEADER = "SplitSDKMachineName"; - static final String CLIENT_MACHINE_IP_HEADER = "SplitSDKMachineIP"; - static final String CLIENT_VERSION = "SplitSDKVersion"; - - private final String _apiTokenBearer; - private final String _hostname; - private final String _ip; - - public static AddSplitHeadersFilter instance(String apiToken, boolean ipAddressEnabled) { - if (!ipAddressEnabled) { - return new AddSplitHeadersFilter(apiToken, null, null); - } - - String hostname = null; - String ip = null; - - try { - InetAddress localHost = InetAddress.getLocalHost(); - hostname = localHost.getHostName(); - ip = localHost.getHostAddress(); - } catch (Exception e) { - _log.error("Could not resolve InetAddress", e); - } - - return new AddSplitHeadersFilter(apiToken, hostname, ip); - } - - private AddSplitHeadersFilter(String apiToken, String hostname, String ip) { - checkNotNull(apiToken); - - _apiTokenBearer = "Bearer " + apiToken; - _hostname = hostname; - _ip = ip; - } - - @Override - public void process(HttpRequest request, EntityDetails entity, HttpContext context) throws HttpException, IOException { - request.addHeader(AUTHORIZATION_HEADER, _apiTokenBearer); - request.addHeader(CLIENT_VERSION, SplitClientConfig.splitSdkVersion); - - if (_hostname != null) { - request.addHeader(CLIENT_MACHINE_NAME_HEADER, _hostname); - } - - if (_ip != null) { - request.addHeader(CLIENT_MACHINE_IP_HEADER, _ip); - } - } -} diff --git a/client/src/main/java/io/split/client/interceptors/AuthorizationInterceptorFilter.java b/client/src/main/java/io/split/client/interceptors/AuthorizationInterceptorFilter.java new file mode 100644 index 00000000..43b50949 --- /dev/null +++ b/client/src/main/java/io/split/client/interceptors/AuthorizationInterceptorFilter.java @@ -0,0 +1,30 @@ +package io.split.client.interceptors; + +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequestInterceptor; +import org.apache.hc.core5.http.protocol.HttpContext; + +import java.io.IOException; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class AuthorizationInterceptorFilter implements HttpRequestInterceptor { + static final String AUTHORIZATION_HEADER = "Authorization"; + + private final String _apiTokenBearer; + + public static AuthorizationInterceptorFilter instance(String apiToken) { + return new AuthorizationInterceptorFilter(apiToken); + } + + private AuthorizationInterceptorFilter(String apiToken) { + _apiTokenBearer = "Bearer " + checkNotNull(apiToken); + } + + @Override + public void process(HttpRequest httpRequest, EntityDetails entityDetails, HttpContext httpContext) throws HttpException, IOException { + httpRequest.addHeader(AUTHORIZATION_HEADER, _apiTokenBearer); + } +} diff --git a/client/src/main/java/io/split/client/interceptors/ClientKeyInterceptorFilter.java b/client/src/main/java/io/split/client/interceptors/ClientKeyInterceptorFilter.java new file mode 100644 index 00000000..ad3c8259 --- /dev/null +++ b/client/src/main/java/io/split/client/interceptors/ClientKeyInterceptorFilter.java @@ -0,0 +1,32 @@ +package io.split.client.interceptors; + +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequestInterceptor; +import org.apache.hc.core5.http.protocol.HttpContext; + +import java.io.IOException; + +public class ClientKeyInterceptorFilter implements HttpRequestInterceptor { + static final String CLIENT_KEY = "SplitSDKClientKey"; + + private final String _clientKey; + + public static ClientKeyInterceptorFilter instance(String apiToken) { + return new ClientKeyInterceptorFilter(getKey(apiToken)); + } + + private ClientKeyInterceptorFilter(String clientKey) { + _clientKey = clientKey; + } + + @Override + public void process(HttpRequest httpRequest, EntityDetails entityDetails, HttpContext httpContext) throws HttpException, IOException { + httpRequest.addHeader(CLIENT_KEY, _clientKey); + } + + private static String getKey(String clientKey) { + return clientKey.length() >4 ? clientKey.substring(clientKey.length() - 4) : clientKey; + } +} diff --git a/client/src/main/java/io/split/client/interceptors/FlagSetsFilter.java b/client/src/main/java/io/split/client/interceptors/FlagSetsFilter.java new file mode 100644 index 00000000..f571f134 --- /dev/null +++ b/client/src/main/java/io/split/client/interceptors/FlagSetsFilter.java @@ -0,0 +1,9 @@ +package io.split.client.interceptors; + +import java.util.Set; + +public interface FlagSetsFilter { + + boolean intersect(Set sets); + boolean intersect(String set); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/interceptors/FlagSetsFilterImpl.java b/client/src/main/java/io/split/client/interceptors/FlagSetsFilterImpl.java new file mode 100644 index 00000000..e97da9f6 --- /dev/null +++ b/client/src/main/java/io/split/client/interceptors/FlagSetsFilterImpl.java @@ -0,0 +1,40 @@ +package io.split.client.interceptors; + +import java.util.Set; + +public class FlagSetsFilterImpl implements FlagSetsFilter { + + private final Set _flagSets; + private final boolean _shouldFilter; + + public FlagSetsFilterImpl(Set flagSets) { + _shouldFilter = !flagSets.isEmpty(); + _flagSets = flagSets; + } + @Override + public boolean intersect(Set sets) { + if (!_shouldFilter) { + return true; + } + if (sets == null || sets.isEmpty()) { + return false; + } + for (String set: sets) { + if (_flagSets.contains(set)) { + return true; + } + } + return false; + } + + @Override + public boolean intersect(String set) { + if (!_shouldFilter) { + return true; + } + if (set.isEmpty()){ + return false; + } + return _flagSets.contains(set); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/interceptors/SdkMetadataInterceptorFilter.java b/client/src/main/java/io/split/client/interceptors/SdkMetadataInterceptorFilter.java new file mode 100644 index 00000000..731ba136 --- /dev/null +++ b/client/src/main/java/io/split/client/interceptors/SdkMetadataInterceptorFilter.java @@ -0,0 +1,48 @@ +package io.split.client.interceptors; + +import io.split.client.SplitClientConfig; +import io.split.client.utils.SDKMetadata; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequestInterceptor; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +public class SdkMetadataInterceptorFilter implements HttpRequestInterceptor { + private static final Logger _log = LoggerFactory.getLogger(SdkMetadataInterceptorFilter.class); + + static final String CLIENT_MACHINE_NAME_HEADER = "SplitSDKMachineName"; + static final String CLIENT_MACHINE_IP_HEADER = "SplitSDKMachineIP"; + static final String CLIENT_VERSION = "SplitSDKVersion"; + + private final String _hostname; + private final String _ip; + private final String _sdkVersion; + + public static SdkMetadataInterceptorFilter instance(SDKMetadata sdkMetadata) { + return new SdkMetadataInterceptorFilter(sdkMetadata.getMachineName(), sdkMetadata.getMachineIp(), sdkMetadata.getMachineName()); + } + + private SdkMetadataInterceptorFilter(String hostName, String ip, String sdkVersion) { + _sdkVersion = sdkVersion; + _hostname = hostName; + _ip = ip; + } + + @Override + public void process(HttpRequest httpRequest, EntityDetails entityDetails, HttpContext httpContext) throws HttpException, IOException { + httpRequest.addHeader(CLIENT_VERSION, SplitClientConfig.splitSdkVersion); + + if (!"".equals(_hostname)) { + httpRequest.addHeader(CLIENT_MACHINE_NAME_HEADER, _hostname); + } + + if (!"".equals(_ip)) { + httpRequest.addHeader(CLIENT_MACHINE_IP_HEADER, _ip); + } + } +} diff --git a/client/src/main/java/io/split/client/jmx/JmxMonitor.java b/client/src/main/java/io/split/client/jmx/JmxMonitor.java index ce2e3ff4..0acd7ded 100644 --- a/client/src/main/java/io/split/client/jmx/JmxMonitor.java +++ b/client/src/main/java/io/split/client/jmx/JmxMonitor.java @@ -130,4 +130,4 @@ private String getContextPath() { return null; } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/jmx/SplitJmxMonitor.java b/client/src/main/java/io/split/client/jmx/SplitJmxMonitor.java index e5d49e11..fa5ad58a 100644 --- a/client/src/main/java/io/split/client/jmx/SplitJmxMonitor.java +++ b/client/src/main/java/io/split/client/jmx/SplitJmxMonitor.java @@ -1,11 +1,12 @@ package io.split.client.jmx; -import io.split.cache.SegmentCache; -import io.split.cache.SplitCache; import io.split.client.SplitClient; +import io.split.engine.common.FetchOptions; import io.split.engine.experiments.SplitFetcher; import io.split.engine.segments.SegmentFetcher; import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,21 +21,22 @@ public class SplitJmxMonitor implements SplitJmxMonitorMBean { private final SplitClient _client; private final SplitFetcher _featureFetcher; - private final SplitCache _splitCache; + private final SplitCacheConsumer _splitCacheConsumer; private final SegmentSynchronizationTask _segmentSynchronizationTask; - private SegmentCache _segmentCache; + private SegmentCacheConsumer segmentCacheConsumer; - public SplitJmxMonitor(SplitClient splitClient, SplitFetcher featureFetcher, SplitCache splitCache, SegmentSynchronizationTask segmentSynchronizationTask, SegmentCache segmentCache) { + public SplitJmxMonitor(SplitClient splitClient, SplitFetcher featureFetcher, SplitCacheConsumer splitCacheConsumer, + SegmentSynchronizationTask segmentSynchronizationTask, SegmentCacheConsumer segmentCacheConsumer) { _client = checkNotNull(splitClient); _featureFetcher = checkNotNull(featureFetcher); - _splitCache = checkNotNull(splitCache); + _splitCacheConsumer = checkNotNull(splitCacheConsumer); _segmentSynchronizationTask = checkNotNull(segmentSynchronizationTask); - _segmentCache = checkNotNull(segmentCache); + this.segmentCacheConsumer = checkNotNull(segmentCacheConsumer); } @Override public boolean forceSyncFeatures() { - _featureFetcher.forceRefresh(true); + _featureFetcher.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(true).build()); _log.info("Features successfully refreshed via JMX"); return true; } @@ -43,7 +45,7 @@ public boolean forceSyncFeatures() { public boolean forceSyncSegment(String segmentName) { SegmentFetcher fetcher = _segmentSynchronizationTask.getFetcher(segmentName); try{ - fetcher.fetch(true); + fetcher.fetch(new FetchOptions.Builder().build()); } //We are sure this will never happen because getFetcher firts initiate the segment. This try/catch is for safe only. catch (NullPointerException np){ @@ -61,11 +63,11 @@ public String getTreatment(String key, String featureName) { @Override public String fetchDefinition(String featureName) { - return _splitCache.get(featureName).toString(); + return _splitCacheConsumer.get(featureName).toString(); } @Override public boolean isKeyInSegment(String key, String segmentName) { - return _segmentCache.isInSegment(segmentName, key); + return segmentCacheConsumer.isInSegment(segmentName, key); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/jmx/SplitJmxMonitorMBean.java b/client/src/main/java/io/split/client/jmx/SplitJmxMonitorMBean.java index 6b492043..94d1728b 100644 --- a/client/src/main/java/io/split/client/jmx/SplitJmxMonitorMBean.java +++ b/client/src/main/java/io/split/client/jmx/SplitJmxMonitorMBean.java @@ -36,4 +36,4 @@ public interface SplitJmxMonitorMBean { */ boolean isKeyInSegment(String key, String segmentName); -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/lifecycle/SdkEventsConfig.java b/client/src/main/java/io/split/client/lifecycle/SdkEventsConfig.java new file mode 100644 index 00000000..15bb6ee6 --- /dev/null +++ b/client/src/main/java/io/split/client/lifecycle/SdkEventsConfig.java @@ -0,0 +1,47 @@ +package io.split.client.lifecycle; + +import io.harness.events.EventsManagerConfig; +import io.split.client.api.SdkEvent; + +import java.util.Collections; + +/** + * Declarative rule graph wiring {@link SdkInternalEvent}s to the public {@link SdkEvent}s. + * + *

Package-private: this is the only place a raw commons {@code EventsManagerConfig} type is used, keeping it + * unreachable from outside {@code io.split.client.lifecycle}. + */ +final class SdkEventsConfig { + + private SdkEventsConfig() { + // utility class + } + + static EventsManagerConfig build() { + return EventsManagerConfig.builder() + .requireAll(SdkEvent.SDK_READY, SdkInternalEvent.SDK_READY) + .executionLimit(SdkEvent.SDK_READY, 1) + .metadataSource(SdkEvent.SDK_READY, SdkInternalEvent.SDK_READY) + + .requireAll(SdkEvent.SDK_READY_TIMED_OUT, SdkInternalEvent.SDK_READY_TIMEOUT_REACHED) + .executionLimit(SdkEvent.SDK_READY_TIMED_OUT, 1) + .metadataSource(SdkEvent.SDK_READY_TIMED_OUT, SdkInternalEvent.SDK_READY_TIMEOUT_REACHED) + + .requireAny(SdkEvent.SDK_UPDATE, + SdkInternalEvent.FLAGS_UPDATED, + SdkInternalEvent.FLAG_KILLED_NOTIFICATION, + SdkInternalEvent.SEGMENTS_UPDATED, + SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED) + .prerequisite(SdkEvent.SDK_UPDATE, SdkEvent.SDK_READY) + .executionLimit(SdkEvent.SDK_UPDATE, -1) + .metadataSource(SdkEvent.SDK_UPDATE, + Collections.singleton(SdkInternalEvent.FLAGS_UPDATED), SdkInternalEvent.FLAGS_UPDATED) + .metadataSource(SdkEvent.SDK_UPDATE, + Collections.singleton(SdkInternalEvent.FLAG_KILLED_NOTIFICATION), SdkInternalEvent.FLAG_KILLED_NOTIFICATION) + .metadataSource(SdkEvent.SDK_UPDATE, + Collections.singleton(SdkInternalEvent.SEGMENTS_UPDATED), SdkInternalEvent.SEGMENTS_UPDATED) + .metadataSource(SdkEvent.SDK_UPDATE, + Collections.singleton(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED), SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED) + .build(); + } +} diff --git a/client/src/main/java/io/split/client/lifecycle/SdkEventsManager.java b/client/src/main/java/io/split/client/lifecycle/SdkEventsManager.java new file mode 100644 index 00000000..83ad194e --- /dev/null +++ b/client/src/main/java/io/split/client/lifecycle/SdkEventsManager.java @@ -0,0 +1,198 @@ +package io.split.client.lifecycle; + +import io.harness.events.EventHandler; +import io.harness.events.EventsManager; +import io.harness.events.EventsManagers; +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventListener; +import io.split.client.api.SdkEventMetadata; +import io.split.client.utils.SplitExecutorFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadFactory; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Wraps the commons {@code io.harness.events} library to expose {@link SdkEvent} registration/notification in terms + * of {@code Sdk*} types only. No {@code io.harness.events} type appears in any public method of this class. + * + *

Registers exactly one dispatcher per {@link SdkEvent} with the underlying commons manager, because commons + * stores multiple handlers for the same event in a {@code HashSet} (unordered) and delivers with no per-handler + * error isolation. This class owns its own per-event ordered listener list and its own try/catch per listener, + * so registration-order delivery and error isolation are guaranteed regardless of commons' internals. + * + *

Delivery runs on a dedicated single-thread executor, never on the caller's thread and never on commons' + * own internal processing thread: commons calls our {@code EventDelivery} synchronously from the caller's thread + * when replaying to a late subscriber, and serializes all internal-event processing for every event on one + * background thread. Handing delivery off to our own executor keeps a slow listener from blocking either. + * + *

This layer is purely additive to readiness. {@code SDKReadinessGates} stays the authoritative source for + * {@code isSDKReady()}, deliberately not {@link #eventAlreadyTriggered(SdkEvent)}: the commons implementation waits on + * its internal process queue with no timeout, which must never happen on the evaluation path. + */ +public final class SdkEventsManager { + + private static final Logger LOG = LoggerFactory.getLogger(SdkEventsManager.class); + + private final EventsManager commonsManager; + private final ExecutorService deliveryExecutor; + private final Object registrationLock = new Object(); + private final Map> listeners = new HashMap<>(); + private final Set delivered = new HashSet<>(); + private boolean destroyed = false; + + public static SdkEventsManager create(ThreadFactory threadFactory) { + return new SdkEventsManager(threadFactory); + } + + SdkEventsManager(ThreadFactory threadFactory) { + for (SdkEvent event : SdkEvent.values()) { + listeners.put(event, new ArrayList()); + } + deliveryExecutor = SplitExecutorFactory.buildExecutorService(threadFactory, "SPLIT-EventsManager-%d"); + commonsManager = EventsManagers.create(SdkEventsConfig.build(), this::deliverAsync); + for (SdkEvent event : SdkEvent.values()) { + commonsManager.register(event, this::dispatch); + } + } + + /** + * Registers a listener for {@code event}. If the event is once-only (SDK_READY, SDK_READY_TIMED_OUT) and has + * already fired, the listener is delivered {@code null} metadata asynchronously instead of being registered + * for future delivery (there won't be one). No-op after {@link #destroy()}. + */ + public void on(SdkEvent event, SdkEventListener listener) { + checkNotNull(event); + checkNotNull(listener); + boolean replay; + synchronized (registrationLock) { + if (destroyed) { + return; + } + if (delivered.contains(event)) { + replay = true; + } else { + listeners.get(event).add(listener); + replay = false; + } + } + if (replay) { + submitQuietly(() -> safeInvoke(listener, event, null)); + } + } + + /** + * Removes all listeners registered for {@code event}. Does not affect whether the event can fire again in + * the future, or whether new listeners can be registered afterward. + */ + public void off(SdkEvent event) { + checkNotNull(event); + synchronized (registrationLock) { + if (destroyed) { + return; + } + listeners.get(event).clear(); + } + // Deliberately not calling the commons manager's unregister(event): that would remove our permanent + // per-event dispatcher, and this event could never fire again for the lifetime of this manager. + } + + public void notifyInternalEvent(SdkInternalEvent event, SdkEventMetadata metadata) { + if (isDestroyed()) { + return; + } + LOG.debug("Internal event {} notified, metadata: {}", event, metadata); + commonsManager.notifyInternalEvent(event, metadata); + } + + public boolean eventAlreadyTriggered(SdkEvent event) { + if (isDestroyed()) { + return false; + } + return commonsManager.eventAlreadyTriggered(event); + } + + /** + * Stops this manager: no further events fire, {@link #on(SdkEvent, SdkEventListener)} becomes a no-op, and all + * registered listeners are dropped. + */ + public void destroy() { + synchronized (registrationLock) { + if (destroyed) { + return; + } + destroyed = true; + listeners.clear(); + } + commonsManager.destroy(); + deliveryExecutor.shutdown(); + } + + private boolean isDestroyed() { + synchronized (registrationLock) { + return destroyed; + } + } + + private void deliverAsync(EventHandler handler, SdkEvent event, SdkEventMetadata metadata) { + submitQuietly(() -> { + try { + handler.handle(event, metadata); + } catch (Throwable t) { + LOG.warn("Unexpected error delivering " + event, t); + } + }); + } + + // destroy() can shut the executor down while a delivery is in flight; dropping it is the intended outcome. + private void submitQuietly(Runnable task) { + try { + deliveryExecutor.submit(task); + } catch (RejectedExecutionException e) { + LOG.debug("Delivery dropped: events manager was destroyed"); + } + } + + private void dispatch(SdkEvent event, SdkEventMetadata metadata) { + List snapshot; + synchronized (registrationLock) { + if (destroyed) { + return; + } + // Readiness won the race: a timeout that was notified after SDK_READY was dispatched is dropped. + if (event == SdkEvent.SDK_READY_TIMED_OUT && delivered.contains(SdkEvent.SDK_READY)) { + return; + } + snapshot = new ArrayList<>(listeners.get(event)); + if (isOnceOnly(event)) { + delivered.add(event); + } + } + LOG.debug("Emitting {} to {} listener(s), metadata: {}", event, snapshot.size(), metadata); + for (SdkEventListener listener : snapshot) { + safeInvoke(listener, event, metadata); + } + } + + private void safeInvoke(SdkEventListener listener, SdkEvent event, SdkEventMetadata metadata) { + try { + listener.handle(metadata); + } catch (Throwable t) { + LOG.warn("SdkEventListener threw an exception handling " + event, t); + } + } + + private static boolean isOnceOnly(SdkEvent event) { + return event == SdkEvent.SDK_READY || event == SdkEvent.SDK_READY_TIMED_OUT; + } +} diff --git a/client/src/main/java/io/split/client/lifecycle/SdkEventsNotifier.java b/client/src/main/java/io/split/client/lifecycle/SdkEventsNotifier.java new file mode 100644 index 00000000..5c57ef5f --- /dev/null +++ b/client/src/main/java/io/split/client/lifecycle/SdkEventsNotifier.java @@ -0,0 +1,13 @@ +package io.split.client.lifecycle; + +import io.split.client.api.SdkEventMetadata; + +/** + * Write-path hook used by the sync engine to report that cached data changed. Implementations must not throw. + */ +public interface SdkEventsNotifier { + + SdkEventsNotifier NOOP = (event, metadata) -> { }; + + void notify(SdkInternalEvent event, SdkEventMetadata metadata); +} diff --git a/client/src/main/java/io/split/client/lifecycle/SdkInternalEvent.java b/client/src/main/java/io/split/client/lifecycle/SdkInternalEvent.java new file mode 100644 index 00000000..d9fe7214 --- /dev/null +++ b/client/src/main/java/io/split/client/lifecycle/SdkInternalEvent.java @@ -0,0 +1,14 @@ +package io.split.client.lifecycle; + +/** + * Internal signals fed into the events manager (the {@code I} type parameter of the commons + * {@code EventsManager}). Not part of the supported public API. + */ +public enum SdkInternalEvent { + SDK_READY, + FLAGS_UPDATED, + FLAG_KILLED_NOTIFICATION, + SEGMENTS_UPDATED, + RULE_BASED_SEGMENTS_UPDATED, + SDK_READY_TIMEOUT_REACHED +} diff --git a/client/src/main/java/io/split/client/metrics/BinarySearchLatencyTracker.java b/client/src/main/java/io/split/client/metrics/BinarySearchLatencyTracker.java deleted file mode 100644 index 35efff10..00000000 --- a/client/src/main/java/io/split/client/metrics/BinarySearchLatencyTracker.java +++ /dev/null @@ -1,131 +0,0 @@ -package io.split.client.metrics; - -import java.util.Arrays; - -/** - * Tracks latencies pero bucket of time. - * Each bucket represent a latency greater than the one before - * and each number within each bucket is a number of calls in the range. - *

- * (1) 1.00 - * (2) 1.50 - * (3) 2.25 - * (4) 3.38 - * (5) 5.06 - * (6) 7.59 - * (7) 11.39 - * (8) 17.09 - * (9) 25.63 - * (10) 38.44 - * (11) 57.67 - * (12) 86.50 - * (13) 129.75 - * (14) 194.62 - * (15) 291.93 - * (16) 437.89 - * (17) 656.84 - * (18) 985.26 - * (19) 1,477.89 - * (20) 2,216.84 - * (21) 3,325.26 - * (22) 4,987.89 - * (23) 7,481.83 - *

- * Thread-safety: This class is not thread safe. - *

- * Created by patricioe on 2/10/16. - */ -public class BinarySearchLatencyTracker implements ILatencyTracker { - - static final long[] BUCKETS = { - 1000, 1500, 2250, 3375, 5063, - 7594, 11391, 17086, 25629, 38443, - 57665, 86498, 129746, 194620, 291929, - 437894, 656841, 985261, 1477892, 2216838, - 3325257, 4987885, 7481828 - }; - - static final long MAX_LATENCY = 7481828; - - long[] latencies = new long[BUCKETS.length]; - - /** - * Increment the internal counter for the bucket this latency falls into. - * - * @param millis - */ - public void addLatencyMillis(long millis) { - int index = findIndex(millis * 1000); - latencies[index]++; - } - - /** - * Increment the internal counter for the bucket this latency falls into. - * - * @param micros - */ - public void addLatencyMicros(long micros) { - int index = findIndex(micros); - latencies[index]++; - } - - /** - * Returns the list of latencies buckets as an array. - * - * @return the list of latencies buckets as an array. - */ - public long[] getLatencies() { - return latencies; - } - - @Override - public long getLatency(int index) { - return latencies[index]; - } - - public void clear() { - latencies = new long[BUCKETS.length]; - } - - /** - * Returns the counts in the bucket this latency falls into. - * The latencies will no be updated. - * - * @param latency - * @return the bucket content for the latency. - */ - public long getBucketForLatencyMillis(long latency) { - return latencies[findIndex(latency * 1000)]; - } - - /** - * Returns the counts in the bucket this latency falls into. - * The latencies will no be updated. - * - * @param latency - * @return the bucket content for the latency. - */ - public long getBucketForLatencyMicros(long latency) { - return latencies[findIndex(latency)]; - } - - - private int findIndex(long micros) { - if (micros > MAX_LATENCY) { - return BUCKETS.length - 1; - } - - int index = Arrays.binarySearch(BUCKETS, micros); - - if (index < 0) { - - // Adjust the index based on Java Array javadocs. <0 means the value wasn't found and it's module value - // is where it should be inserted (in this case, it means the counter it applies - unless it's equals to the - // length of the array). - - index = -(index + 1); - } - return index; - } - -} diff --git a/client/src/main/java/io/split/client/metrics/CachedMetrics.java b/client/src/main/java/io/split/client/metrics/CachedMetrics.java deleted file mode 100644 index 41f066a3..00000000 --- a/client/src/main/java/io/split/client/metrics/CachedMetrics.java +++ /dev/null @@ -1,142 +0,0 @@ - -package io.split.client.metrics; - -import com.google.common.collect.Maps; -import com.google.common.primitives.Longs; -import io.split.client.dtos.Counter; -import io.split.client.dtos.Latency; -import io.split.engine.metrics.Metrics; - -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; - -import static com.google.common.base.Preconditions.checkArgument; - - -/** - * Created by adilaijaz on 9/4/15. - */ -public class CachedMetrics implements Metrics { - - private final DTOMetrics _metrics; - - private final Map _latencyMap; - private final Map _countMap; - - - private final Object _latencyLock = new Object(); - private AtomicLong _latencyLastUpdateTimeMillis = new AtomicLong(System.currentTimeMillis()); - - private final Object _counterLock = new Object(); - private AtomicLong _counterLastUpdateTimeMillis = new AtomicLong(System.currentTimeMillis()); - - private long _refreshPeriodInMillis; - - private final int _queueForTheseManyCalls; - - /** - * For unit testing only. - * - * @param httpMetrics - * @param queueForTheseManyCalls - */ - /*package private*/ CachedMetrics(DTOMetrics httpMetrics, int queueForTheseManyCalls) { - this(httpMetrics, queueForTheseManyCalls, TimeUnit.MINUTES.toMillis(1)); - } - - public CachedMetrics(DTOMetrics httpMetrics, long refreshPeriodInMillis) { - this(httpMetrics, 100, refreshPeriodInMillis); - } - - private CachedMetrics(DTOMetrics metrics, int queueForTheseManyCalls, long refreshPeriodInMillis) { - _metrics = metrics; - _latencyMap = Maps.newHashMap(); - _countMap = Maps.newHashMap(); - checkArgument(queueForTheseManyCalls > 0, "queue for cache should be greater than zero"); - _queueForTheseManyCalls = queueForTheseManyCalls; - _refreshPeriodInMillis = refreshPeriodInMillis; - } - - @Override - public void count(String counter, long delta) { - if (delta <= 0) { - return; - } - - if (counter == null || counter.trim().isEmpty()) { - return; - } - - synchronized (_counterLock) { - SumAndCount sumAndCount = _countMap.get(counter); - if (sumAndCount == null) { - sumAndCount = new SumAndCount(); - _countMap.put(counter, sumAndCount); - } - - sumAndCount.addDelta(delta); - - if (sumAndCount._count >= _queueForTheseManyCalls || hasTimeElapsed(_counterLastUpdateTimeMillis)) { - Counter dto = new Counter(); - dto.name = counter; - dto.delta = sumAndCount._sum; - - sumAndCount.clear(); - _counterLastUpdateTimeMillis.set(System.currentTimeMillis()); - _metrics.count(dto); - } - } - } - - private boolean hasTimeElapsed(AtomicLong lastRefreshTime) { - return (System.currentTimeMillis() - lastRefreshTime.get()) > _refreshPeriodInMillis; - } - - @Override - public void time(String operation, long timeInMs) { - if (operation == null || operation.trim().isEmpty() || timeInMs < 0L) { - // error - return; - } - synchronized (_latencyLock) { - if (!_latencyMap.containsKey(operation)) { - ILatencyTracker latencies = new BinarySearchLatencyTracker(); - _latencyMap.put(operation, latencies); - } - - ILatencyTracker tracker = _latencyMap.get(operation); - tracker.addLatencyMillis((int) timeInMs); - - if (hasTimeElapsed(_latencyLastUpdateTimeMillis)) { - - Latency dto = new Latency(); - dto.name = operation; - dto.latencies = Longs.asList(tracker.getLatencies()); - - tracker.clear(); - _latencyLastUpdateTimeMillis.set(System.currentTimeMillis()); - _metrics.time(dto); - - } - } - } - - - private static final class SumAndCount { - private int _count = 0; - private long _sum = 0L; - - public void addDelta(long delta) { - _count++; - _sum += delta; - } - - public void clear() { - _count = 0; - _sum = 0L; - } - - } - -} diff --git a/client/src/main/java/io/split/client/metrics/DTOMetrics.java b/client/src/main/java/io/split/client/metrics/DTOMetrics.java deleted file mode 100644 index 86c793cb..00000000 --- a/client/src/main/java/io/split/client/metrics/DTOMetrics.java +++ /dev/null @@ -1,13 +0,0 @@ -package io.split.client.metrics; - -import io.split.client.dtos.Counter; -import io.split.client.dtos.Latency; - -/** - * Created by adilaijaz on 6/14/16. - */ -public interface DTOMetrics { - void time(Latency dto); - - void count(Counter dto); -} diff --git a/client/src/main/java/io/split/client/metrics/FireAndForgetMetrics.java b/client/src/main/java/io/split/client/metrics/FireAndForgetMetrics.java deleted file mode 100644 index 43aa702d..00000000 --- a/client/src/main/java/io/split/client/metrics/FireAndForgetMetrics.java +++ /dev/null @@ -1,123 +0,0 @@ -package io.split.client.metrics; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.split.engine.metrics.Metrics; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.Closeable; -import java.util.List; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -/** - * Created by adilaijaz on 9/4/15. - */ -public class FireAndForgetMetrics implements Metrics, Closeable { - - private static final Logger _log = LoggerFactory.getLogger(FireAndForgetMetrics.class); - - private final ExecutorService _executorService; - private final Metrics _delegate; - - public static FireAndForgetMetrics instance(Metrics delegate, int numberOfThreads, int queueSize) { - ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder(); - threadFactoryBuilder.setDaemon(true); - threadFactoryBuilder.setNameFormat("split-fireAndForgetMetrics-%d"); - threadFactoryBuilder.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread t, Throwable e) { - _log.error("Error in thread: " + t.getName(), e); - } - }); - - final ExecutorService executorService = new ThreadPoolExecutor(numberOfThreads, - numberOfThreads, - 0L, - TimeUnit.MILLISECONDS, - new ArrayBlockingQueue(queueSize), - threadFactoryBuilder.build(), - new ThreadPoolExecutor.DiscardPolicy()); - - - return new FireAndForgetMetrics(delegate, executorService); - } - - private FireAndForgetMetrics(Metrics delegate, ExecutorService executorService) { - _delegate = delegate; - _executorService = executorService; - } - - - @Override - public void count(String counter, long delta) { - try { - _executorService.submit(new CountRunnable(_delegate, counter, delta)); - } catch (Throwable t) { - _log.warn("CountRunnable failed", t); - } - } - - @Override - public void time(String operation, long timeInMs) { - try { - _executorService.submit(new TimeRunnable(_delegate, operation, timeInMs)); - } catch (Throwable t) { - _log.warn("TimeRunnable failed", t); - } - } - - public void close() { - _executorService.shutdown(); - try { - if (!_executorService.awaitTermination(10L, TimeUnit.SECONDS)) { //optional * - _log.info("Executor did not terminate in the specified time."); - List droppedTasks = _executorService.shutdownNow(); //optional ** - _log.info("Executor was abruptly shut down. These tasks will not be executed: " + droppedTasks); - } - } catch (InterruptedException e) { - // reset the interrupt. - Thread.currentThread().interrupt(); - } - } - - - private static final class CountRunnable implements Runnable { - - private final Metrics _delegate; - private final String _name; - private final long _delta; - - public CountRunnable(Metrics delegate, String name, long delta) { - _delegate = delegate; - _name = name; - _delta = delta; - } - - @Override - public void run() { - _delegate.count(_name, _delta); - } - } - - private static final class TimeRunnable implements Runnable { - - private final Metrics _delegate; - private final String _name; - private final long _timeInMs; - - public TimeRunnable(Metrics delegate, String name, long timeInMs) { - _delegate = delegate; - _name = name; - _timeInMs = timeInMs; - } - - @Override - public void run() { - _delegate.time(_name, _timeInMs); - } - } - -} diff --git a/client/src/main/java/io/split/client/metrics/HttpMetrics.java b/client/src/main/java/io/split/client/metrics/HttpMetrics.java deleted file mode 100644 index 14d63b0f..00000000 --- a/client/src/main/java/io/split/client/metrics/HttpMetrics.java +++ /dev/null @@ -1,135 +0,0 @@ -package io.split.client.metrics; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; -import io.split.client.dtos.Counter; -import io.split.client.dtos.Latency; -import io.split.client.utils.Utils; -import io.split.engine.metrics.Metrics; -import org.apache.hc.client5.http.classic.methods.HttpPost; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; -import org.apache.hc.core5.http.HttpEntity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URI; -import java.net.URISyntaxException; - -/** - * Created by adilaijaz on 9/4/15. - */ -public class HttpMetrics implements Metrics, DTOMetrics { - private static final Logger _log = LoggerFactory.getLogger(HttpMetrics.class); - - private final CloseableHttpClient _client; - private final URI _timeTarget; - private final URI _counterTarget; - - - public static HttpMetrics create(CloseableHttpClient client, URI root) throws URISyntaxException { - return new HttpMetrics(client, root); - } - - - public HttpMetrics(CloseableHttpClient client, URI root) throws URISyntaxException { - Preconditions.checkNotNull(root); - _client = Preconditions.checkNotNull(client); - _timeTarget = Utils.appendPath(root, "api/metrics/time"); - _counterTarget = Utils.appendPath(root, "api/metrics/counter"); - } - - - @Override - public void time(Latency dto) { - if (dto.latencies.isEmpty()) { - return; - } - - try { - post(_timeTarget, dto); - } catch (Throwable t) { - _log.warn("Exception when posting metric " + dto, t); - } - ; - - } - - @Override - public void count(Counter dto) { - try { - post(_counterTarget, dto); - } catch (Throwable t) { - _log.warn("Exception when posting metric " + dto, t); - } - - } - - private void post(URI uri, Object dto) { - - CloseableHttpResponse response = null; - - try { - HttpEntity entity = Utils.toJsonEntity(dto); - - HttpPost request = new HttpPost(uri); - request.setEntity(entity); - - response = _client.execute(request); - - int status = response.getCode(); - - if (status < 200 || status >= 300) { - _log.warn("Response status was: " + status); - } - - } catch (Throwable t) { - _log.warn("Exception when posting metrics: " + t.getMessage()); - if (_log.isDebugEnabled()) { - _log.debug("Reason: ", t); - } - } finally { - Utils.forceClose(response); - } - - } - - @Override - public void count(String counter, long delta) { - try { - Counter dto = new Counter(); - dto.name = counter; - dto.delta = delta; - - count(dto); - } catch (Throwable t) { - _log.info("Could not count metric " + counter, t); - } - - } - - @Override - public void time(String operation, long timeInMs) { - try { - Latency dto = new Latency(); - dto.name = operation; - dto.latencies = Lists.newArrayList(timeInMs); - - time(dto); - } catch (Throwable t) { - _log.info("Could not time metric " + operation, t); - } - } - - @VisibleForTesting - URI getTimeTarget() { - return _timeTarget; - } - - @VisibleForTesting - URI getCounterTarget() { - return _counterTarget; - } - -} diff --git a/client/src/main/java/io/split/client/metrics/ILatencyTracker.java b/client/src/main/java/io/split/client/metrics/ILatencyTracker.java deleted file mode 100644 index 282db06c..00000000 --- a/client/src/main/java/io/split/client/metrics/ILatencyTracker.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.split.client.metrics; - -/** - * Created by patricioe on 2/10/16. - */ -public interface ILatencyTracker { - - void addLatencyMillis(long millis); - - void addLatencyMicros(long micros); - - long[] getLatencies(); - - long getLatency(int index); - - void clear(); - - long getBucketForLatencyMillis(long latency); - - long getBucketForLatencyMicros(long latency); - -} diff --git a/client/src/main/java/io/split/client/metrics/LogarithmicSearchLatencyTracker.java b/client/src/main/java/io/split/client/metrics/LogarithmicSearchLatencyTracker.java deleted file mode 100644 index 4034d8de..00000000 --- a/client/src/main/java/io/split/client/metrics/LogarithmicSearchLatencyTracker.java +++ /dev/null @@ -1,116 +0,0 @@ -package io.split.client.metrics; - -/** - * Tracks latencies pero bucket of time. - * Each bucket represent a latency greater than the one before - * and each number within each bucket is a number of calls in the range. - *

- * (1) 1.00 - * (2) 1.50 - * (3) 2.25 - * (4) 3.38 - * (5) 5.06 - * (6) 7.59 - * (7) 11.39 - * (8) 17.09 - * (9) 25.63 - * (10) 38.44 - * (11) 57.67 - * (12) 86.50 - * (13) 129.75 - * (14) 194.62 - * (15) 291.93 - * (16) 437.89 - * (17) 656.84 - * (18) 985.26 - * (19) 1,477.89 - * (20) 2,216.84 - * (21) 3,325.26 - * (22) 4,987.89 - * (23) 7,481.83 - *

- * Thread-safety: This class is not thread safe. - *

- * Created by patricioe on 2/10/16. - */ -public class LogarithmicSearchLatencyTracker implements ILatencyTracker { - - static final int BUCKETS = 23; - private static final double LOG_10_1000_MICROS = Math.log10(1000); - private static final double LOG_10_1_5_MICROS = Math.log10(Double.valueOf("1.5").doubleValue()); - - - long[] latencies = new long[BUCKETS]; - - /** - * Increment the internal counter for the bucket this latency falls into. - * - * @param millis - */ - public void addLatencyMillis(long millis) { - int index = findIndex(millis * 1000); - latencies[index]++; - } - - /** - * Increment the internal counter for the bucket this latency falls into. - * - * @param micros - */ - public void addLatencyMicros(long micros) { - int index = findIndex(micros); - latencies[index]++; - } - - /** - * Returns the list of latencies buckets as an array. - * - * @return the list of latencies buckets as an array. - */ - public long[] getLatencies() { - return latencies; - } - - @Override - public long getLatency(int index) { - return latencies[index]; - } - - public void clear() { - latencies = new long[BUCKETS]; - } - - /** - * Returns the counts in the bucket this latency falls into. - * The latencies will no be updated. - * - * @param latency - * @return the bucket content for the latency. - */ - public long getBucketForLatencyMillis(long latency) { - return latencies[findIndex(latency * 1000)]; - } - - /** - * Returns the counts in the bucket this latency falls into. - * The latencies will no be updated. - * - * @param latency - * @return the bucket content for the latency. - */ - public long getBucketForLatencyMicros(long latency) { - return latencies[findIndex(latency)]; - } - - - private int findIndex(long micros) { - - if (micros <= 1000) return 0; - if (micros > 4987885) return 22; - - double raw = (Math.log10(micros) - LOG_10_1000_MICROS) / LOG_10_1_5_MICROS; - double rounded = Math.round(raw * 1000000d) / 1000000d; - return (int) Math.ceil(rounded); - } - -} diff --git a/client/src/main/java/io/split/client/utils/ApacheRequestDecorator.java b/client/src/main/java/io/split/client/utils/ApacheRequestDecorator.java new file mode 100644 index 00000000..c64d9d46 --- /dev/null +++ b/client/src/main/java/io/split/client/utils/ApacheRequestDecorator.java @@ -0,0 +1,43 @@ +package io.split.client.utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpRequest; + +import io.split.client.RequestDecorator; +import io.split.client.dtos.RequestContext; + +public class ApacheRequestDecorator { + + public static HttpRequest decorate(HttpRequest request, RequestDecorator decorator) { + + RequestContext ctx = new RequestContext(convertToMap(request.getHeaders())); + for (Map.Entry> entry : decorator.decorateHeaders(ctx).headers().entrySet()) { + List values = entry.getValue(); + for (int i = 0; i < values.size(); i++) { + if (i == 0) { + request.setHeader(entry.getKey(), values.get(i)); + } else { + request.addHeader(entry.getKey(), values.get(i)); + } + } + } + + return request; + } + + private static Map> convertToMap(Header[] to_convert) { + Map> to_return = new HashMap>(); + for (Integer i = 0; i < to_convert.length; i++) { + if (!to_return.containsKey(to_convert[i].getName())) { + to_return.put(to_convert[i].getName(), new ArrayList()); + } + to_return.get(to_convert[i].getName()).add(to_convert[i].getValue()); + } + return to_return; + } +} diff --git a/client/src/main/java/io/split/client/utils/FeatureFlagProcessor.java b/client/src/main/java/io/split/client/utils/FeatureFlagProcessor.java new file mode 100644 index 00000000..f6e4878a --- /dev/null +++ b/client/src/main/java/io/split/client/utils/FeatureFlagProcessor.java @@ -0,0 +1,43 @@ +package io.split.client.utils; + +import io.split.client.dtos.Split; +import io.split.client.dtos.Status; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.engine.experiments.ParsedSplit; +import io.split.engine.experiments.SplitParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class FeatureFlagProcessor { + private static final Logger _log = LoggerFactory.getLogger(FeatureFlagProcessor.class); + + public static FeatureFlagsToUpdate processFeatureFlagChanges(SplitParser splitParser, List splits, FlagSetsFilter flagSetsFilter) { + List toAdd = new ArrayList<>(); + List toRemove = new ArrayList<>(); + Set segments = new HashSet<>(); + for (Split split : splits) { + if (split.status != Status.ACTIVE) { + // archive. + toRemove.add(split.name); + continue; + } + if (!flagSetsFilter.intersect(split.sets)) { + toRemove.add(split.name); + continue; + } + ParsedSplit parsedSplit = splitParser.parse(split); + if (parsedSplit == null) { + _log.debug(String.format("We could not parse the feature flag definition for: %s", split.name)); + continue; + } + segments.addAll(parsedSplit.getSegmentsNames()); + toAdd.add(parsedSplit); + } + return new FeatureFlagsToUpdate(toAdd, toRemove, segments); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/FeatureFlagsToUpdate.java b/client/src/main/java/io/split/client/utils/FeatureFlagsToUpdate.java new file mode 100644 index 00000000..3eb03c7b --- /dev/null +++ b/client/src/main/java/io/split/client/utils/FeatureFlagsToUpdate.java @@ -0,0 +1,30 @@ +package io.split.client.utils; + +import io.split.engine.experiments.ParsedSplit; + +import java.util.List; +import java.util.Set; + +public class FeatureFlagsToUpdate { + List toAdd; + List toRemove; + Set segments; + + public FeatureFlagsToUpdate(List toAdd, List toRemove, Set segments) { + this.toAdd = toAdd; + this.toRemove = toRemove; + this.segments = segments; + } + + public List getToAdd() { + return toAdd; + } + + public List getToRemove() { + return toRemove; + } + + public Set getSegments() { + return segments; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java b/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java new file mode 100644 index 00000000..c1d9f981 --- /dev/null +++ b/client/src/main/java/io/split/client/utils/FileInputStreamProvider.java @@ -0,0 +1,26 @@ +package io.split.client.utils; + +import io.split.client.exceptions.InputStreamProviderException; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +public class FileInputStreamProvider implements InputStreamProvider { + + private final String _fileName; + + public FileInputStreamProvider(String fileName) { + _fileName = fileName; + } + + @Override + public InputStream get() throws InputStreamProviderException { + try { + return new FileInputStream(_fileName); + } catch (FileNotFoundException f) { + throw new InputStreamProviderException(String.format("Problem fetching splitChanges using file named %s: %s", + _fileName, f.getMessage())); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/FileTypeEnum.java b/client/src/main/java/io/split/client/utils/FileTypeEnum.java new file mode 100644 index 00000000..202b701d --- /dev/null +++ b/client/src/main/java/io/split/client/utils/FileTypeEnum.java @@ -0,0 +1,8 @@ +package io.split.client.utils; + +public enum FileTypeEnum { + LEGACY, + YAML, + YML, + JSON +} diff --git a/client/src/main/java/io/split/client/utils/GenericClientUtil.java b/client/src/main/java/io/split/client/utils/GenericClientUtil.java index ac631df8..7953fe5b 100644 --- a/client/src/main/java/io/split/client/utils/GenericClientUtil.java +++ b/client/src/main/java/io/split/client/utils/GenericClientUtil.java @@ -41,3 +41,4 @@ public static void process(List data, URI endpoint, CloseableHttpClient cl } } + diff --git a/client/src/main/java/io/split/client/utils/InputStreamProvider.java b/client/src/main/java/io/split/client/utils/InputStreamProvider.java new file mode 100644 index 00000000..02df2839 --- /dev/null +++ b/client/src/main/java/io/split/client/utils/InputStreamProvider.java @@ -0,0 +1,10 @@ +package io.split.client.utils; + +import io.split.client.exceptions.InputStreamProviderException; + +import java.io.InputStream; + +public interface InputStreamProvider { + + InputStream get() throws InputStreamProviderException; +} diff --git a/client/src/main/java/io/split/client/utils/Json.java b/client/src/main/java/io/split/client/utils/Json.java index 82ce01d1..4709e891 100644 --- a/client/src/main/java/io/split/client/utils/Json.java +++ b/client/src/main/java/io/split/client/utils/Json.java @@ -6,8 +6,11 @@ import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; +import com.google.gson.stream.JsonReader; import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.List; /** * Created by adilaijaz on 6/10/16. @@ -36,4 +39,7 @@ public static T fromJson(String json, Class clz) { return _json.fromJson(json, clz); } + public static T fromJson(JsonReader json, Class clz) { + return _json.fromJson(json, clz); + } } diff --git a/client/src/main/java/io/split/client/utils/LocalhostConstants.java b/client/src/main/java/io/split/client/utils/LocalhostConstants.java new file mode 100644 index 00000000..e883eabd --- /dev/null +++ b/client/src/main/java/io/split/client/utils/LocalhostConstants.java @@ -0,0 +1,18 @@ +package io.split.client.utils; + +public final class LocalhostConstants { + public static final String CONTROL = "control"; + public static final String USER = "user"; + static final String TREATMENT_ON = "on"; + static final String TREATMENT_OFF = "off"; + public static final Integer SIZE_100 = 100; + public static final Integer SIZE_1 = 1; + static final Integer SIZE_0 = 0; + static final int ALGO = 2; + static final int MILLI_SECONDS = 1000; + static final Long DEFAULT_TS = -1L; + + private LocalhostConstants() { + + } +} diff --git a/client/src/main/java/io/split/client/utils/LocalhostSanitizer.java b/client/src/main/java/io/split/client/utils/LocalhostSanitizer.java new file mode 100644 index 00000000..28282adc --- /dev/null +++ b/client/src/main/java/io/split/client/utils/LocalhostSanitizer.java @@ -0,0 +1,291 @@ +package io.split.client.utils; + +import com.google.common.base.Preconditions; +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.KeySelector; +import io.split.client.dtos.Matcher; +import io.split.client.dtos.MatcherCombiner; +import io.split.client.dtos.MatcherGroup; +import io.split.client.dtos.MatcherType; +import io.split.client.dtos.Partition; +import io.split.client.dtos.SegmentChange; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.Status; +import io.split.client.dtos.WhitelistMatcherData; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.ChangeDto; + +import java.security.SecureRandom; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public final class LocalhostSanitizer { + private static final String DEFAULT_RULE = "default rule"; + + private LocalhostSanitizer() { + throw new IllegalStateException("Utility class"); + } + + public static SplitChange sanitization(SplitChange splitChange) { + sanitizeTillAndSince(splitChange); + splitChange.featureFlags.d = sanitizeFeatureFlags(splitChange.featureFlags.d); + splitChange.ruleBasedSegments.d = sanitizeRuleBasedSegments(splitChange.ruleBasedSegments.d); + + return splitChange; + } + + private static List sanitizeRuleBasedSegments(List ruleBasedSegments) { + List ruleBasedSegmentsToRemove = new ArrayList<>(); + if (ruleBasedSegments != null) { + for (RuleBasedSegment ruleBasedSegment : ruleBasedSegments) { + if (ruleBasedSegment.name == null) { + ruleBasedSegmentsToRemove.add(ruleBasedSegment); + continue; + } + ruleBasedSegment.trafficTypeName = sanitizeIfNullOrEmpty(ruleBasedSegment.trafficTypeName, LocalhostConstants.USER); + ruleBasedSegment.status = sanitizeStatus(ruleBasedSegment.status); + ruleBasedSegment.changeNumber = sanitizeChangeNumber(ruleBasedSegment.changeNumber, 0); + ruleBasedSegment.conditions = sanitizeConditions((ArrayList) ruleBasedSegment.conditions, false, + ruleBasedSegment.trafficTypeName); + ruleBasedSegment.excluded.segments = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.segments); + ruleBasedSegment.excluded.keys = sanitizeExcluded((ArrayList) ruleBasedSegment.excluded.keys); + } + ruleBasedSegments.removeAll(ruleBasedSegmentsToRemove); + } else { + ruleBasedSegments = new ArrayList<>(); + } + return ruleBasedSegments; + } + + private static List sanitizeFeatureFlags(List featureFlags) { + List splitsToRemove = new ArrayList<>(); + if (featureFlags != null) { + for (Split split : featureFlags) { + if (split.name == null) { + splitsToRemove.add(split); + continue; + } + split.trafficTypeName = sanitizeIfNullOrEmpty(split.trafficTypeName, LocalhostConstants.USER); + split.status = sanitizeStatus(split.status); + split.defaultTreatment = sanitizeIfNullOrEmpty(split.defaultTreatment, LocalhostConstants.CONTROL); + split.changeNumber = sanitizeChangeNumber(split.changeNumber, 0); + split.trafficAllocation = sanitizeTrafficAllocation(split.trafficAllocation); + split.trafficAllocationSeed = sanitizeSeed(split.trafficAllocationSeed); + split.seed = sanitizeSeed(split.seed); + split.algo = sanitizeAlgo(split.algo); + split.conditions = sanitizeConditions((ArrayList) split.conditions, false, split.trafficTypeName); + } + featureFlags.removeAll(splitsToRemove); + } else { + featureFlags = new ArrayList<>(); + } + return featureFlags; + } + + private static int sanitizeSeed(Integer seed) { + SecureRandom random = new SecureRandom(); + if (seed == null || seed == 0) { + seed = -random.nextInt(10) * LocalhostConstants.MILLI_SECONDS; + } + return seed; + } + + private static int sanitizeAlgo(int algo) { + if (algo != LocalhostConstants.ALGO) { + algo = LocalhostConstants.ALGO; + } + return algo; + } + + private static int sanitizeTrafficAllocation(Integer trafficAllocation) { + if (trafficAllocation == null || trafficAllocation < 0 || trafficAllocation > LocalhostConstants.SIZE_100) { + trafficAllocation = LocalhostConstants.SIZE_100; + } + return trafficAllocation; + } + + private static ArrayList sanitizeConditions(ArrayList conditions, boolean createPartition, String trafficTypeName) { + if (conditions == null) { + conditions = new ArrayList<>(); + } + + Condition condition = new Condition(); + if (!conditions.isEmpty()){ + condition = conditions.get(conditions.size() - 1); + } + + if (conditions.isEmpty() || !condition.conditionType.equals(ConditionType.ROLLOUT) || + condition.matcherGroup.matchers == null || + condition.matcherGroup.matchers.isEmpty() || + !condition.matcherGroup.matchers.get(0).matcherType.equals(MatcherType.ALL_KEYS)) { + Condition rolloutCondition = new Condition(); + conditions.add(createRolloutCondition(rolloutCondition, trafficTypeName, null, createPartition)); + } + return conditions; + } + private static String sanitizeIfNullOrEmpty(String toBeSanitized, String defaultValue) { + if (toBeSanitized == null || toBeSanitized.isEmpty()) { + return defaultValue; + } + return toBeSanitized; + } + + private static long sanitizeChangeNumber(long toBeSanitized, long defaultValue) { + if (toBeSanitized < 0) { + return defaultValue; + } + return toBeSanitized; + } + + private static Status sanitizeStatus(Status toBeSanitized) { + if (toBeSanitized == null || toBeSanitized != Status.ACTIVE && toBeSanitized != Status.ARCHIVED) { + return Status.ACTIVE; + } + return toBeSanitized; + + } + + private static ArrayList sanitizeExcluded(ArrayList excluded) + { + if (excluded == null) { + return new ArrayList<>(); + } + return excluded; + } + + private static SplitChange sanitizeTillAndSince(SplitChange splitChange) { + if (checkTillConditions(splitChange.featureFlags)) { + splitChange.featureFlags.t = LocalhostConstants.DEFAULT_TS; + } + if (checkSinceConditions(splitChange.featureFlags)) { + splitChange.featureFlags.s = splitChange.featureFlags.t; + } + + if (checkTillConditions(splitChange.ruleBasedSegments)) { + splitChange.ruleBasedSegments.t = LocalhostConstants.DEFAULT_TS; + } + if (checkSinceConditions(splitChange.ruleBasedSegments)) { + splitChange.ruleBasedSegments.s = splitChange.ruleBasedSegments.t; + } + return splitChange; + } + + private static boolean checkTillConditions(ChangeDto change) { + return change.t < LocalhostConstants.DEFAULT_TS || change.t == 0; + } + + private static boolean checkSinceConditions(ChangeDto change) { + return change.s < LocalhostConstants.DEFAULT_TS || change.s > change.t; + } + + public static SegmentChange sanitization(SegmentChange segmentChange) { + if (segmentChange.name == null || segmentChange.name.isEmpty()) { + return null; + } + if (segmentChange.added == null) { + segmentChange.added = new ArrayList<>(); + } + if (segmentChange.removed == null) { + segmentChange.removed = new ArrayList<>(); + } + List addedToRemoved = segmentChange.added.stream().filter(add -> segmentChange.removed.contains(add)).collect(Collectors.toList()); + segmentChange.removed.removeAll(addedToRemoved); + + if (segmentChange.till < LocalhostConstants.DEFAULT_TS || segmentChange.till == 0){ + segmentChange.till = LocalhostConstants.DEFAULT_TS; + } + if (segmentChange.since < LocalhostConstants.DEFAULT_TS || segmentChange.since > segmentChange.till) { + segmentChange.since = segmentChange.till; + } + return segmentChange; + } + + public static Condition createRolloutCondition(Condition condition, String trafficType, String treatment, boolean createPartition) { + condition.conditionType = ConditionType.ROLLOUT; + condition.matcherGroup = new MatcherGroup(); + condition.matcherGroup.combiner = MatcherCombiner.AND; + Matcher matcher = new Matcher(); + KeySelector keySelector = new KeySelector(); + keySelector.trafficType = trafficType; + + matcher.keySelector = keySelector; + matcher.matcherType = MatcherType.ALL_KEYS; + matcher.negate = false; + + condition.matcherGroup.matchers = new ArrayList<>(); + condition.matcherGroup.matchers.add(matcher); + + if (createPartition) { + condition.partitions = new ArrayList<>(); + Partition partition1 = new Partition(); + Partition partition2 = new Partition(); + partition1.size = LocalhostConstants.SIZE_100; + partition2.size = LocalhostConstants.SIZE_0; + if (treatment != null) { + partition1.treatment = treatment; + } else { + partition1.treatment = LocalhostConstants.TREATMENT_OFF; + partition2.treatment = LocalhostConstants.TREATMENT_ON; + } + condition.partitions.add(partition1); + condition.partitions.add(partition2); + } + condition.label = DEFAULT_RULE; + + return condition; + } + + public static Condition createCondition(Object keyOrKeys, String treatment) { + Condition condition = new Condition(); + if (keyOrKeys == null) { + return LocalhostSanitizer.createRolloutCondition(condition, "user", treatment, true); + } else { + if (keyOrKeys instanceof String) { + List keys = new ArrayList<>(); + keys.add(keyOrKeys); + return createWhitelistCondition(condition, "user", treatment, keys); + } else { + Preconditions.checkArgument(keyOrKeys instanceof List, "'keys' is not a String nor a List."); + return createWhitelistCondition(condition, "user", treatment, (List) keyOrKeys); + } + } + } + + public static Condition createWhitelistCondition(Condition condition, String trafficType, String treatment, List keys) { + condition.conditionType = ConditionType.WHITELIST; + condition.matcherGroup = new MatcherGroup(); + condition.matcherGroup.combiner = MatcherCombiner.AND; + Matcher matcher = new Matcher(); + KeySelector keySelector = new KeySelector(); + keySelector.trafficType = trafficType; + + matcher.keySelector = keySelector; + matcher.matcherType = MatcherType.WHITELIST; + matcher.negate = false; + matcher.whitelistMatcherData = new WhitelistMatcherData(); + matcher.whitelistMatcherData.whitelist = new ArrayList<>(keys); + + condition.matcherGroup.matchers = new ArrayList<>(); + condition.matcherGroup.matchers.add(matcher); + + condition.partitions = new ArrayList<>(); + Partition partition1 = new Partition(); + Partition partition2 = new Partition(); + partition1.size = LocalhostConstants.SIZE_100; + partition2.size = LocalhostConstants.SIZE_0; + if (treatment != null) { + partition1.treatment = treatment; + } else { + partition1.treatment = LocalhostConstants.TREATMENT_OFF; + partition2.treatment = LocalhostConstants.TREATMENT_ON; + } + condition.partitions.add(partition1); + condition.partitions.add(partition2); + condition.label = "default rule"; + + return condition; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/RuleBasedSegmentProcessor.java b/client/src/main/java/io/split/client/utils/RuleBasedSegmentProcessor.java new file mode 100644 index 00000000..7720367b --- /dev/null +++ b/client/src/main/java/io/split/client/utils/RuleBasedSegmentProcessor.java @@ -0,0 +1,65 @@ +package io.split.client.utils; + +import io.split.client.dtos.Excluded; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Status; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.engine.experiments.RuleBasedSegmentParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class RuleBasedSegmentProcessor { + private static final Logger _log = LoggerFactory.getLogger(RuleBasedSegmentProcessor.class); + + private RuleBasedSegmentProcessor() { + throw new IllegalStateException("Utility class"); + } + + public static RuleBasedSegmentsToUpdate processRuleBasedSegmentChanges(RuleBasedSegmentParser ruleBasedSegmentParser, + List ruleBasedSegments) { + List toAdd = new ArrayList<>(); + List toRemove = new ArrayList<>(); + Set segments = new HashSet<>(); + for (RuleBasedSegment ruleBasedSegment : ruleBasedSegments) { + ruleBasedSegment.excluded = checkExcluded(ruleBasedSegment.excluded); + if (ruleBasedSegment.status != Status.ACTIVE) { + // archive. + toRemove.add(ruleBasedSegment.name); + continue; + } + ParsedRuleBasedSegment parsedRuleBasedSegment = ruleBasedSegmentParser.parse(ruleBasedSegment); + if (parsedRuleBasedSegment == null) { + _log.debug(String.format("We could not parse the rule based segment definition for: %s", ruleBasedSegment.name)); + } else { + segments.addAll(parsedRuleBasedSegment.getSegmentsNames()); + toAdd.add(parsedRuleBasedSegment); + } + } + return new RuleBasedSegmentsToUpdate(toAdd, toRemove, segments); + } + + private static Excluded createEmptyExcluded() { + Excluded excluded = new Excluded(); + excluded.segments = new ArrayList<>(); + excluded.keys = new ArrayList<>(); + return excluded; + } + + private static Excluded checkExcluded(Excluded excluded) { + if (excluded == null) { + excluded = createEmptyExcluded(); + } + if (excluded.segments == null) { + excluded.segments = new ArrayList<>(); + } + if (excluded.keys == null) { + excluded.keys = new ArrayList<>(); + } + return excluded; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/RuleBasedSegmentsToUpdate.java b/client/src/main/java/io/split/client/utils/RuleBasedSegmentsToUpdate.java new file mode 100644 index 00000000..850ae849 --- /dev/null +++ b/client/src/main/java/io/split/client/utils/RuleBasedSegmentsToUpdate.java @@ -0,0 +1,30 @@ +package io.split.client.utils; + +import io.split.engine.experiments.ParsedRuleBasedSegment; + +import java.util.List; +import java.util.Set; + +public class RuleBasedSegmentsToUpdate { + List toAdd; + List toRemove; + Set segments; + + public RuleBasedSegmentsToUpdate(List toAdd, List toRemove, Set segments) { + this.toAdd = toAdd; + this.toRemove = toRemove; + this.segments = segments; + } + + public List getToAdd() { + return toAdd; + } + + public List getToRemove() { + return toRemove; + } + + public Set getSegments() { + return segments; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/SDKMetadata.java b/client/src/main/java/io/split/client/utils/SDKMetadata.java new file mode 100644 index 00000000..e4bfc08b --- /dev/null +++ b/client/src/main/java/io/split/client/utils/SDKMetadata.java @@ -0,0 +1,25 @@ +package io.split.client.utils; + +public class SDKMetadata { + private String _sdkVersion; + private String _machineIp; + private String _machineName; + + public SDKMetadata(String sdkVersion, String machineIp, String machineName) { + this._sdkVersion = sdkVersion; + this._machineIp = machineIp; + this._machineName = machineName; + } + + public String getSdkVersion() { + return _sdkVersion; + } + + public String getMachineIp() { + return _machineIp; + } + + public String getMachineName() { + return _machineName; + } +} diff --git a/client/src/main/java/io/split/client/utils/SplitExecutorFactory.java b/client/src/main/java/io/split/client/utils/SplitExecutorFactory.java new file mode 100644 index 00000000..f9897edf --- /dev/null +++ b/client/src/main/java/io/split/client/utils/SplitExecutorFactory.java @@ -0,0 +1,33 @@ +package io.split.client.utils; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; + +public class SplitExecutorFactory { + + public static ScheduledExecutorService buildScheduledExecutorService(ThreadFactory threadFactory, String name, Integer size) { + return Executors.newScheduledThreadPool(size, buildThreadFactory(threadFactory, name)); + } + + public static ScheduledExecutorService buildSingleThreadScheduledExecutor(ThreadFactory threadFactory, String name){ + return Executors.newSingleThreadScheduledExecutor(buildThreadFactory(threadFactory, name)); + } + + public static ExecutorService buildExecutorService(ThreadFactory threadFactory, String name) { + return Executors.newSingleThreadExecutor(buildThreadFactory(threadFactory, name)); + } + + private static ThreadFactory buildThreadFactory(ThreadFactory threadFactory, String name) { + ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat(name); + if (threadFactory != null) { + threadFactoryBuilder.setThreadFactory(threadFactory); + } + return threadFactoryBuilder.build(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/client/utils/StaticContentInputStreamProvider.java b/client/src/main/java/io/split/client/utils/StaticContentInputStreamProvider.java new file mode 100644 index 00000000..d129b77c --- /dev/null +++ b/client/src/main/java/io/split/client/utils/StaticContentInputStreamProvider.java @@ -0,0 +1,26 @@ +package io.split.client.utils; + +import io.split.client.exceptions.InputStreamProviderException; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + +public class StaticContentInputStreamProvider implements InputStreamProvider { + + private final String _streamContents; + + public StaticContentInputStreamProvider(InputStream inputStream){ + _streamContents = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining("\n")); + } + + @Override + public InputStream get() throws InputStreamProviderException { + return new ByteArrayInputStream(_streamContents.getBytes()); + } +} diff --git a/client/src/main/java/io/split/client/utils/Utils.java b/client/src/main/java/io/split/client/utils/Utils.java index a83a322d..9a386db5 100644 --- a/client/src/main/java/io/split/client/utils/Utils.java +++ b/client/src/main/java/io/split/client/utils/Utils.java @@ -1,11 +1,10 @@ package io.split.client.utils; -import com.google.common.base.Charsets; +import io.split.client.dtos.ChangeDto; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.io.entity.HttpEntities; -import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.net.URIBuilder; import java.io.IOException; @@ -42,4 +41,8 @@ public static URI appendPath(URI root, String pathToAppend) throws URISyntaxExce String path = String.format("%s%s%s", root.getPath(), root.getPath().endsWith("/") ? "" : "/", pathToAppend); return new URIBuilder(root).setPath(path).build(); } -} + + public static boolean checkExitConditions(ChangeDto change, long cn) { + return change.t < cn && change.t != -1; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/SDKReadinessGates.java b/client/src/main/java/io/split/engine/SDKReadinessGates.java index ae0fd8ad..bb909c04 100644 --- a/client/src/main/java/io/split/engine/SDKReadinessGates.java +++ b/client/src/main/java/io/split/engine/SDKReadinessGates.java @@ -3,27 +3,28 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * Created by adilaijaz on 4/27/16. + * + *

Authoritative source for {@code isSDKReady()} and {@code blockUntilReady()}. + * + *

The SDK events layer ({@code SdkEventsManager}) is additive and is notified alongside {@link #sdkInternalReady()} + * by its callers; it is deliberately not consulted here. {@code eventAlreadyTriggered} in the commons library blocks + * on an internal queue with no timeout, which would add a cross-thread wait to the evaluation path. */ public class SDKReadinessGates { private static final Logger _log = LoggerFactory.getLogger(SDKReadinessGates.class); - private final CountDownLatch _splitsAreReady = new CountDownLatch(1); - private final ConcurrentMap _segmentsAreReady = new ConcurrentHashMap<>(); - + private final CountDownLatch _internalReady = new CountDownLatch(1); /** * Returns true if the SDK is ready. The SDK is ready when: *

    - *
  1. It has fetched Split definitions the first time.
  2. - *
  3. It has downloaded segment memberships for segments in use in the initial split definitions
  4. + *
  5. It has fetched Feature flag definitions the first time.
  6. + *
  7. It has downloaded segment memberships for segments in use in the initial Feature flag definitions
  8. *
*

* This operation will block until the SDK is ready or 'milliseconds' have passed. If the milliseconds @@ -34,136 +35,15 @@ public class SDKReadinessGates { * @return true if the sdk is ready, false otherwise. * @throws InterruptedException if this operation was interrupted. */ - public boolean isSDKReady(long milliseconds) throws InterruptedException { - long end = System.currentTimeMillis() + milliseconds; - long timeLeft = milliseconds; - - boolean splits = areSplitsReady(timeLeft); - if (!splits) { - return false; - } - - timeLeft = end - System.currentTimeMillis(); - - return areSegmentsReady(timeLeft); - } - - public boolean isSDKReadyNow() { - try { - return isSDKReady(0); - } catch (InterruptedException e) { - return false; - } - } - - /** - * Records that the SDK split initialization is done. - * This operation is atomic and idempotent. Repeated invocations - * will not have any impact on the state. - */ - public void splitsAreReady() { - long originalCount = _splitsAreReady.getCount(); - _splitsAreReady.countDown(); - if (originalCount > 0L) { - _log.info("splits are ready"); - } - } - - /** - * Registers a segment that the SDK should download before it is ready. - * This method should be called right after the first successful download - * of split definitions. - *

- * Note that if this method is called in subsequent fetches of splits, - * it will return false; meaning any segments used in new splits - * will not be able to block the SDK from being marked as complete. - * - * @param segmentName the segment to register - * @return true if the segments were registered, false otherwise. - * @throws InterruptedException - */ - public boolean registerSegment(String segmentName) throws InterruptedException { - if (segmentName == null || segmentName.isEmpty() || areSplitsReady(0L)) { - return false; - } - - _segmentsAreReady.putIfAbsent(segmentName, new CountDownLatch(1)); - _log.info("Registered segment: " + segmentName); - return true; - } - - /** - * Records that the SDK segment initialization for this segment is done. - * This operation is atomic and idempotent. Repeated invocations - * will not have any impact on the state. - */ - public void segmentIsReady(String segmentName) { - CountDownLatch cdl = _segmentsAreReady.get(segmentName); - if (cdl == null) { - return; - } - - long originalCount = cdl.getCount(); - - cdl.countDown(); - - if (originalCount > 0L) { - _log.info(segmentName + " segment is ready"); - } - } - - public boolean isSegmentRegistered(String segmentName) { - return _segmentsAreReady.get(segmentName) != null; + public boolean waitUntilInternalReady(long milliseconds) throws InterruptedException { + return _internalReady.await(milliseconds, TimeUnit.MILLISECONDS); } - /** - * Returns true if the SDK is ready w.r.t segments. In other words, this method returns true if: - *

    - *
  1. The SDK has fetched segment definitions the first time.
  2. - *
- *

- * This operation will block until the SDK is ready or 'milliseconds' have passed. If the milliseconds - * are less than or equal to zero, the operation will not block and return immediately - * - * @param milliseconds time to wait for an answer. if the value is zero or negative, we will not - * block for an answer. - * @return true if the sdk is ready w.r.t splits, false otherwise. - * @throws InterruptedException if this operation was interrupted. - */ - public boolean areSegmentsReady(long milliseconds) throws InterruptedException { - long end = System.currentTimeMillis() + milliseconds; - long timeLeft = milliseconds; - - for (Map.Entry entry : _segmentsAreReady.entrySet()) { - String segmentName = entry.getKey(); - CountDownLatch cdl = entry.getValue(); - - if (!cdl.await(timeLeft, TimeUnit.MILLISECONDS)) { - _log.error(segmentName + " is not ready yet"); - return false; - } - - timeLeft = end - System.currentTimeMillis(); - } - - return true; + public boolean isSDKReady() { + return _internalReady.getCount() == 0; } - /** - * Returns true if the SDK is ready w.r.t splits. In other words, this method returns true if: - *

    - *
  1. The SDK has fetched Split definitions the first time.
  2. - *
- *

- * This operation will block until the SDK is ready or 'milliseconds' have passed. If the milliseconds - * are less than or equal to zero, the operation will not block and return immediately - * - * @param milliseconds time to wait for an answer. if the value is zero or negative, we will not - * block for an answer. - * @return true if the sdk is ready w.r.t splits, false otherwise. - * @throws InterruptedException if this operation was interrupted. - */ - public boolean areSplitsReady(long milliseconds) throws InterruptedException { - return _splitsAreReady.await(milliseconds, TimeUnit.MILLISECONDS); + public void sdkInternalReady() { + _internalReady.countDown(); } } diff --git a/client/src/main/java/io/split/engine/common/Backoff.java b/client/src/main/java/io/split/engine/common/Backoff.java index 13bcb542..56d9c426 100644 --- a/client/src/main/java/io/split/engine/common/Backoff.java +++ b/client/src/main/java/io/split/engine/common/Backoff.java @@ -5,20 +5,24 @@ import static com.google.common.base.Preconditions.checkNotNull; public class Backoff { - private static final long BACKOFF_MAX_SECONDS_ALLOWED = 1800; + private static final long BACKOFF_MAX_ALLOWED = 1800; private final long _backoffBase; private AtomicInteger _attempt; + private final long _maxAllowed; public Backoff(long backoffBase) { + this(backoffBase, BACKOFF_MAX_ALLOWED); + } + + public Backoff(long backoffBase, long maxAllowed) { _backoffBase = checkNotNull(backoffBase); _attempt = new AtomicInteger(0); + _maxAllowed = maxAllowed; } public long interval() { - long interval = _backoffBase * (long) Math.pow(2, _attempt.getAndIncrement()); - - return interval >= BACKOFF_MAX_SECONDS_ALLOWED ? BACKOFF_MAX_SECONDS_ALLOWED : interval; + return Math.min(_backoffBase * (long) Math.pow(2, _attempt.getAndIncrement()), _maxAllowed); } public synchronized void reset() { diff --git a/client/src/main/java/io/split/engine/common/ConsumerSyncManager.java b/client/src/main/java/io/split/engine/common/ConsumerSyncManager.java new file mode 100644 index 00000000..ee6f2d62 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/ConsumerSyncManager.java @@ -0,0 +1,21 @@ +package io.split.engine.common; + +import java.io.IOException; + +public class ConsumerSyncManager implements SyncManager { + private final Synchronizer _redisSynchronizer; + + public ConsumerSyncManager(Synchronizer redisSynchronizer){ + _redisSynchronizer = redisSynchronizer; + } + + @Override + public void start() { + _redisSynchronizer.startPeriodicDataRecording(); + } + + @Override + public void shutdown() throws IOException { + _redisSynchronizer.stopPeriodicDataRecording(); + } +} diff --git a/client/src/main/java/io/split/engine/common/ConsumerSynchronizer.java b/client/src/main/java/io/split/engine/common/ConsumerSynchronizer.java new file mode 100644 index 00000000..c9209253 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/ConsumerSynchronizer.java @@ -0,0 +1,89 @@ +package io.split.engine.common; + +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.engine.sse.dtos.SplitKillNotification; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConsumerSynchronizer implements Synchronizer{ + + private static final Logger _log = LoggerFactory.getLogger(ConsumerSynchronizer.class); + private final UniqueKeysTracker _uniqueKeysTracker; + private final ImpressionsManager _impressionManager; + private final TelemetrySyncTask _telemetrySyncTask; + + public ConsumerSynchronizer(SplitTasks splitTasks){ + _uniqueKeysTracker = splitTasks.getUniqueKeysTracker(); + _impressionManager = splitTasks.getImpressionManager(); + _telemetrySyncTask = splitTasks.getTelemetrySyncTask(); + } + @Override + public boolean syncAll() { + return false; + } + + @Override + public void startPeriodicFetching() { + //No-Op + } + + @Override + public void stopPeriodicFetching() { + //No-Op + } + + @Override + public void refreshSplits(Long targetChangeNumber, Long ruleBasedSegmentChangeNumber) { + //No-Op + } + + @Override + public void localKillSplit(SplitKillNotification splitKillNotification) { + //No-Op + } + + @Override + public void refreshSegment(String segmentName, Long targetChangeNumber) { + //No-Op + } + + @Override + public void startPeriodicDataRecording() { + try { + _impressionManager.start(); + } catch (Exception e) { + _log.error("Error trying to init Impression Manager synchronizer task.", e); + } + if (_uniqueKeysTracker != null){ + try { + _uniqueKeysTracker.start(); + } catch (Exception e) { + _log.error("Error trying to init Unique Keys Tracker synchronizer task.", e); + } + } + try { + _telemetrySyncTask.startScheduledTask(); + } catch (Exception e) { + _log.error("Error trying to Telemetry synchronizer task.", e); + } + } + + @Override + public void stopPeriodicDataRecording() { + _impressionManager.close(); + _log.info("Successful shutdown of impressions manager"); + if (_uniqueKeysTracker != null){ + _uniqueKeysTracker.stop(); + _log.info("Successful stop of UniqueKeysTracker"); + } + _telemetrySyncTask.stopScheduledTask(); + _log.info("Successful shutdown of telemetry sync task"); + } + + @Override + public void forceRefreshSegment(String segmentName) { + //No-Op + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/common/FastlyHeadersCaptor.java b/client/src/main/java/io/split/engine/common/FastlyHeadersCaptor.java new file mode 100644 index 00000000..143f9523 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/FastlyHeadersCaptor.java @@ -0,0 +1,35 @@ +package io.split.engine.common; + +import java.util.*; +import java.util.stream.Collectors; + +public class FastlyHeadersCaptor { + + private static final Set HEADERS_TO_CAPTURE = new HashSet<>(Arrays.asList( + "Fastly-Debug-Path", + "Fastly-Debug-TTL", + "Fastly-Debug-Digest", + "X-Served-By", + "X-Cache", + "X-Cache-Hits", + "X-Timer", + "Surrogate-Key", + "ETag", + "Cache-Control", + "X-Request-ID", + "Last-Modified" + )); + + private final List> _headers = new ArrayList<>(); + + public Void handle(Map responseHeaders) { + _headers.add(responseHeaders.entrySet().stream() + .filter(e -> HEADERS_TO_CAPTURE.contains(e.getKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); + return null; + } + + public List> get() { + return _headers; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/common/FetchOptions.java b/client/src/main/java/io/split/engine/common/FetchOptions.java new file mode 100644 index 00000000..ff7a3d49 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/FetchOptions.java @@ -0,0 +1,98 @@ +package io.split.engine.common; + +import java.util.Objects; + +public class FetchOptions { + + public static final Long DEFAULT_TARGET_CHANGENUMBER = -1L; + + public static class Builder { + + public Builder() {} + + public Builder(FetchOptions opts) { + _targetCN = opts._targetCN; + _targetCnRBS = opts._targetCnRBS; + _cacheControlHeaders = opts._cacheControlHeaders; + _flagSetsFilter = opts._flagSetsFilter; + } + + public Builder cacheControlHeaders(boolean on) { + _cacheControlHeaders = on; + return this; + } + + public Builder targetChangeNumber(long targetCN) { + _targetCN = targetCN; + return this; + } + + public Builder targetChangeNumberRBS(long targetCnRBS) { + _targetCnRBS = targetCnRBS; + return this; + } + + public Builder flagSetsFilter(String flagSetsFilter) { + _flagSetsFilter = flagSetsFilter; + return this; + } + + public FetchOptions build() { + return new FetchOptions(_cacheControlHeaders, _targetCN, _targetCnRBS, _flagSetsFilter); + } + + private long _targetCN = DEFAULT_TARGET_CHANGENUMBER; + private long _targetCnRBS = DEFAULT_TARGET_CHANGENUMBER; + private boolean _cacheControlHeaders = false; + private String _flagSetsFilter = ""; + } + + public boolean cacheControlHeadersEnabled() { + return _cacheControlHeaders; + } + + public long targetCN() { return _targetCN; } + + public long targetCnRBS() { return _targetCnRBS; } + + public boolean hasCustomCN() { return _targetCN != DEFAULT_TARGET_CHANGENUMBER; } + + public String flagSetsFilter() { + return _flagSetsFilter; + } + + private FetchOptions(boolean cacheControlHeaders, + long targetCN, + long targetCnRBS, + String flagSetsFilter) { + _cacheControlHeaders = cacheControlHeaders; + _targetCN = targetCN; + _targetCnRBS = targetCnRBS; + _flagSetsFilter = flagSetsFilter; + } + + @Override + public boolean equals(Object obj) { + if (null == obj) return false; + if (this == obj) return true; + if (!(obj instanceof FetchOptions)) return false; + + FetchOptions other = (FetchOptions) obj; + + return Objects.equals(_cacheControlHeaders, other._cacheControlHeaders) + && Objects.equals(_targetCN, other._targetCN) + && Objects.equals(_targetCnRBS, other._targetCnRBS) + && Objects.equals(_flagSetsFilter, other._flagSetsFilter); + } + + @Override + public int hashCode() { + return com.google.common.base.Objects.hashCode(_cacheControlHeaders, + _targetCN, _flagSetsFilter); + } + + private final boolean _cacheControlHeaders; + private final long _targetCN; + private final long _targetCnRBS; + private final String _flagSetsFilter; +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/common/LocalhostSyncManager.java b/client/src/main/java/io/split/engine/common/LocalhostSyncManager.java new file mode 100644 index 00000000..1ec75711 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/LocalhostSyncManager.java @@ -0,0 +1,48 @@ +package io.split.engine.common; + +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.SDKReadinessGates; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class LocalhostSyncManager implements SyncManager { + + private static final Logger _log = LoggerFactory.getLogger(LocalhostSyncManager.class); + private final Synchronizer _localhostSynchronizer; + private final SDKReadinessGates _gates; + + private final SdkEventsNotifier _notifier; + + public LocalhostSyncManager(Synchronizer localhostSynchronizer, SDKReadinessGates sdkReadinessGates){ + this(localhostSynchronizer, sdkReadinessGates, SdkEventsNotifier.NOOP); + } + + public LocalhostSyncManager(Synchronizer localhostSynchronizer, SDKReadinessGates sdkReadinessGates, + SdkEventsNotifier notifier) { + _localhostSynchronizer = checkNotNull(localhostSynchronizer); + _gates = sdkReadinessGates; + _notifier = checkNotNull(notifier); + } + + @Override + public void start() { + if(!_localhostSynchronizer.syncAll()){ + _log.error("Could not synchronize feature flag and segment files"); + return; + } + _gates.sdkInternalReady(); + _notifier.notify(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + _localhostSynchronizer.startPeriodicFetching(); + } + + @Override + public void shutdown() throws IOException { + _localhostSynchronizer.stopPeriodicFetching(); + } +} diff --git a/client/src/main/java/io/split/engine/common/LocalhostSynchronizer.java b/client/src/main/java/io/split/engine/common/LocalhostSynchronizer.java new file mode 100644 index 00000000..21114880 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/LocalhostSynchronizer.java @@ -0,0 +1,94 @@ +package io.split.engine.common; + +import io.split.engine.experiments.FetchResult; +import io.split.engine.experiments.SplitFetcher; +import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.segments.SegmentFetcher; +import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.engine.sse.dtos.SplitKillNotification; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class LocalhostSynchronizer implements Synchronizer{ + + private static final Logger _log = LoggerFactory.getLogger(LocalhostSynchronizer.class); + private final SplitSynchronizationTask _splitSynchronizationTask; + private final SplitFetcher _splitFetcher; + private final SegmentSynchronizationTask _segmentSynchronizationTaskImp; + private final boolean _refreshEnable; + + public LocalhostSynchronizer(SplitTasks splitTasks, + SplitFetcher splitFetcher, + boolean refreshEnable){ + _splitSynchronizationTask = checkNotNull(splitTasks.getSplitSynchronizationTask()); + _splitFetcher = checkNotNull(splitFetcher); + _segmentSynchronizationTaskImp = splitTasks.getSegmentSynchronizationTask(); + _refreshEnable = refreshEnable; + } + + @Override + public boolean syncAll() { + FetchResult fetchResult = _splitFetcher.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(true).build()); + return fetchResult.isSuccess() && _segmentSynchronizationTaskImp.fetchAllSynchronous(); + } + + @Override + public void startPeriodicFetching() { + _log.debug("Starting Periodic Fetching ..."); + if(!_refreshEnable){ + _log.info("Refresh enable is false. The synchronization tasks are not going to start"); + return; + } + _splitSynchronizationTask.start(); + _segmentSynchronizationTaskImp.start(); + } + + @Override + public void stopPeriodicFetching() { + _log.debug("Stop Periodic Fetching ..."); + if(!_refreshEnable){ + return; + } + _splitSynchronizationTask.stop(); + _segmentSynchronizationTaskImp.stop(); + } + + @Override + public void refreshSplits(Long targetChangeNumber, Long ruleBasedSegmentChangeNumber) { + FetchResult fetchResult = _splitFetcher.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(true).build()); + if (fetchResult.isSuccess()){ + _log.debug("Refresh feature flags completed"); + fetchResult.getSegments().stream().forEach(segmentName -> refreshSegment(segmentName, null)); + } else { + _log.debug("No changes fetched"); + } + } + + @Override + public void localKillSplit(SplitKillNotification splitKillNotification) { + //No-Op + } + + @Override + public void refreshSegment(String segmentName, Long targetChangeNumber) { + SegmentFetcher segmentFetcher = _segmentSynchronizationTaskImp.getFetcher(segmentName); + segmentFetcher.fetch(new FetchOptions.Builder().cacheControlHeaders(true).build()); + } + + @Override + public void startPeriodicDataRecording() { + //No-Op + } + + @Override + public void stopPeriodicDataRecording() { + //No-Op + } + + @Override + public void forceRefreshSegment(String segmentName) { + //No-Op + } +} diff --git a/client/src/main/java/io/split/engine/common/PushManagerImp.java b/client/src/main/java/io/split/engine/common/PushManagerImp.java index 1d770b6b..12e061da 100644 --- a/client/src/main/java/io/split/engine/common/PushManagerImp.java +++ b/client/src/main/java/io/split/engine/common/PushManagerImp.java @@ -1,7 +1,10 @@ package io.split.engine.common; import com.google.common.annotations.VisibleForTesting; -import com.google.common.util.concurrent.ThreadFactoryBuilder; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.experiments.SplitParser; import io.split.engine.sse.AuthApiClient; import io.split.engine.sse.AuthApiClientImp; import io.split.engine.sse.EventSourceClient; @@ -12,100 +15,150 @@ import io.split.engine.sse.dtos.AuthenticationResponse; import io.split.engine.sse.dtos.SegmentQueueDto; import io.split.engine.sse.workers.SegmentsWorkerImp; -import io.split.engine.sse.workers.SplitsWorker; -import io.split.engine.sse.workers.SplitsWorkerImp; +import io.split.engine.sse.workers.FeatureFlagsWorker; +import io.split.engine.sse.workers.FeatureFlagWorkerImp; import io.split.engine.sse.workers.Worker; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.StreamEventsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.SplitExecutorFactory.buildSingleThreadScheduledExecutor; public class PushManagerImp implements PushManager { private static final Logger _log = LoggerFactory.getLogger(PushManager.class); private final AuthApiClient _authApiClient; private final EventSourceClient _eventSourceClient; - private final SplitsWorker _splitsWorker; + private final FeatureFlagsWorker _featureFlagsWorker; private final Worker _segmentWorker; private final PushStatusTracker _pushStatusTracker; + private static final Lock lock = new ReentrantLock(); private Future _nextTokenRefreshTask; private final ScheduledExecutorService _scheduledExecutorService; private AtomicLong _expirationTime; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; @VisibleForTesting /* package private */ PushManagerImp(AuthApiClient authApiClient, - EventSourceClient eventSourceClient, - SplitsWorker splitsWorker, - Worker segmentWorker, - PushStatusTracker pushStatusTracker) { + EventSourceClient eventSourceClient, + FeatureFlagsWorker featureFlagsWorker, + Worker segmentWorker, + PushStatusTracker pushStatusTracker, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory) { _authApiClient = checkNotNull(authApiClient); _eventSourceClient = checkNotNull(eventSourceClient); - _splitsWorker = splitsWorker; + _featureFlagsWorker = featureFlagsWorker; _segmentWorker = segmentWorker; _pushStatusTracker = pushStatusTracker; _expirationTime = new AtomicLong(); - _scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("Split-SSERefreshToken-%d") - .build()); + _scheduledExecutorService = buildSingleThreadScheduledExecutor(threadFactory, "Split-SSERefreshToken-%d"); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); } public static PushManagerImp build(Synchronizer synchronizer, String streamingUrl, String authUrl, - CloseableHttpClient httpClient, + SplitAPI splitAPI, LinkedBlockingQueue statusMessages, - CloseableHttpClient sseHttpClient) { - SplitsWorker splitsWorker = new SplitsWorkerImp(synchronizer); + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory, + SplitParser splitParser, + SplitCacheProducer splitCacheProducer, + FlagSetsFilter flagSetsFilter, + RuleBasedSegmentCache ruleBasedSegmentCache, + RuleBasedSegmentParser ruleBasedSegmentParser) { + return build(synchronizer, streamingUrl, authUrl, splitAPI, statusMessages, telemetryRuntimeProducer, threadFactory, + splitParser, splitCacheProducer, flagSetsFilter, ruleBasedSegmentCache, ruleBasedSegmentParser, + SdkEventsNotifier.NOOP); + } + + public static PushManagerImp build(Synchronizer synchronizer, + String streamingUrl, + String authUrl, + SplitAPI splitAPI, + LinkedBlockingQueue statusMessages, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory, + SplitParser splitParser, + SplitCacheProducer splitCacheProducer, + FlagSetsFilter flagSetsFilter, + RuleBasedSegmentCache ruleBasedSegmentCache, + RuleBasedSegmentParser ruleBasedSegmentParser, + SdkEventsNotifier notifier) { + FeatureFlagsWorker featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, + ruleBasedSegmentCache, telemetryRuntimeProducer, flagSetsFilter, notifier); Worker segmentWorker = new SegmentsWorkerImp(synchronizer); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(statusMessages); - return new PushManagerImp(new AuthApiClientImp(authUrl, httpClient), - EventSourceClientImp.build(streamingUrl, splitsWorker, segmentWorker, pushStatusTracker, sseHttpClient), - splitsWorker, + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(statusMessages, telemetryRuntimeProducer); + + return new PushManagerImp(new AuthApiClientImp(authUrl, splitAPI.getHttpClient(), telemetryRuntimeProducer), + EventSourceClientImp.build(streamingUrl, featureFlagsWorker, segmentWorker, pushStatusTracker, splitAPI.getSseHttpClient(), + telemetryRuntimeProducer, threadFactory, splitAPI.getRequestDecorator()), + featureFlagsWorker, segmentWorker, - pushStatusTracker); + pushStatusTracker, + telemetryRuntimeProducer, + threadFactory); } @Override - public synchronized void start() { - AuthenticationResponse response = _authApiClient.Authenticate(); - _log.debug(String.format("Auth service response pushEnabled: %s", response.isPushEnabled())); - if (response.isPushEnabled() && startSse(response.getToken(), response.getChannels())) { - _expirationTime.set(response.getExpiration()); - return; - } - - stop(); - if (response.isRetry()) { - _pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); - } else { - _pushStatusTracker.forcePushDisable(); + public void start() { + try { + lock.lock(); + AuthenticationResponse response = _authApiClient.Authenticate(); + _log.debug(String.format("Auth service response pushEnabled: %s", response.isPushEnabled())); + if (response.isPushEnabled() && startSse(response.getToken(), response.getChannels())) { + _expirationTime.set(response.getExpiration()); + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.TOKEN_REFRESH.getType(), + response.getExpiration(), System.currentTimeMillis())); + return; + } + + cleanUpResources(); + if (response.isRetry()) { + _pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); + } else { + _pushStatusTracker.forcePushDisable(); + } + } catch (Exception e) { + _log.debug("Exception in PushManager start: " + e.getMessage()); + } finally { + lock.unlock(); } } @Override - public synchronized void stop() { - _eventSourceClient.stop(); - stopWorkers(); - if (_nextTokenRefreshTask != null) { - _log.debug("Cancel nextTokenRefreshTask"); - _nextTokenRefreshTask.cancel(false); + public void stop() { + try { + lock.lock(); + _log.debug("Stopping PushManagerImp"); + cleanUpResources(); + } catch (Exception e) { + _log.debug("Exception in stopping push manager: " + e.getMessage()); + } finally { + lock.unlock(); } } @Override - public synchronized void scheduleConnectionReset() { + public void scheduleConnectionReset() { _log.debug(String.format("scheduleNextTokenRefresh in %s SECONDS", _expirationTime)); _nextTokenRefreshTask = _scheduledExecutorService.schedule(() -> { _log.debug("Starting scheduleNextTokenRefresh ..."); @@ -125,14 +178,31 @@ private boolean startSse(String token, String channels) { } @Override - public synchronized void startWorkers() { - _splitsWorker.start(); - _segmentWorker.start(); + public void startWorkers() { + try { + _featureFlagsWorker.start(); + _segmentWorker.start(); + } catch (Exception e) { + _log.debug("Exception in starting workers: " + e.getMessage()); + } } @Override - public synchronized void stopWorkers() { - _splitsWorker.stop(); - _segmentWorker.stop(); + public void stopWorkers() { + try { + _featureFlagsWorker.stop(); + _segmentWorker.stop(); + } catch (Exception e) { + _log.debug("Exception in stopping workers: " + e.getMessage()); + } + } + + private void cleanUpResources() { + _eventSourceClient.stop(); + stopWorkers(); + if (_nextTokenRefreshTask != null) { + _log.debug("Cancel nextTokenRefreshTask"); + _nextTokenRefreshTask.cancel(false); + } } -} \ No newline at end of file +} diff --git a/client/src/main/java/io/split/engine/common/SplitAPI.java b/client/src/main/java/io/split/engine/common/SplitAPI.java new file mode 100644 index 00000000..adb0500d --- /dev/null +++ b/client/src/main/java/io/split/engine/common/SplitAPI.java @@ -0,0 +1,51 @@ +package io.split.engine.common; + +import io.split.client.RequestDecorator; +import io.split.service.SplitHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SplitAPI { + + private final SplitHttpClient _httpClient; + private final CloseableHttpClient _sseHttpClient; + private final RequestDecorator _requestDecorator; + private static final Logger _log = LoggerFactory.getLogger(SplitAPI.class); + + private SplitAPI(SplitHttpClient httpClient, CloseableHttpClient sseHttpClient, RequestDecorator requestDecorator) { + _httpClient = httpClient; + _sseHttpClient = sseHttpClient; + _requestDecorator = requestDecorator; + } + + public static SplitAPI build(SplitHttpClient httpClient, CloseableHttpClient sseHttpClient, + RequestDecorator requestDecorator) { + return new SplitAPI(httpClient, sseHttpClient, requestDecorator); + } + + public SplitHttpClient getHttpClient() { + return _httpClient; + } + + public CloseableHttpClient getSseHttpClient() { + return _sseHttpClient; + } + + public RequestDecorator getRequestDecorator() { + return _requestDecorator; + } + + public void close() { + try { + _httpClient.close(); + } catch (Exception e) { + _log.error("Error trying to close regular http client", e); + } + try { + _sseHttpClient.close(); + } catch (Exception e) { + _log.error("Error trying to close sseHttpClient", e); + } + } +} diff --git a/client/src/main/java/io/split/engine/common/SplitTasks.java b/client/src/main/java/io/split/engine/common/SplitTasks.java new file mode 100644 index 00000000..d2960d82 --- /dev/null +++ b/client/src/main/java/io/split/engine/common/SplitTasks.java @@ -0,0 +1,69 @@ +package io.split.engine.common; + +import io.split.client.events.EventsTask; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.telemetry.synchronizer.TelemetrySyncTask; + +public class SplitTasks { + private final SplitSynchronizationTask _splitSynchronizationTask; + private final SegmentSynchronizationTask _segmentSynchronizationTask; + private final ImpressionsManager _impressionManager; + private final EventsTask _eventsTask; + private final TelemetrySyncTask _telemetrySyncTask; + private final UniqueKeysTracker _uniqueKeysTracker; + + private SplitTasks (SplitSynchronizationTask splitSynchronizationTask, + SegmentSynchronizationTask segmentSynchronizationTask, + ImpressionsManager impressionsManager, + EventsTask eventsTask, + TelemetrySyncTask telemetrySyncTask, + UniqueKeysTracker uniqueKeysTracker){ + _splitSynchronizationTask = splitSynchronizationTask; + _segmentSynchronizationTask = segmentSynchronizationTask; + _impressionManager = impressionsManager; + _eventsTask = eventsTask; + _uniqueKeysTracker = uniqueKeysTracker; + _telemetrySyncTask = telemetrySyncTask; + } + + public static SplitTasks build (SplitSynchronizationTask splitSynchronizationTask, + SegmentSynchronizationTask segmentSynchronizationTask, + ImpressionsManager impressionsManager, + EventsTask eventsTask, + TelemetrySyncTask telemetrySyncTask, + UniqueKeysTracker uniqueKeysTracker) { + return new SplitTasks ( splitSynchronizationTask, + segmentSynchronizationTask, + impressionsManager, + eventsTask, + telemetrySyncTask, + uniqueKeysTracker); + } + + public SplitSynchronizationTask getSplitSynchronizationTask() { + return _splitSynchronizationTask; + } + + public SegmentSynchronizationTask getSegmentSynchronizationTask() { + return _segmentSynchronizationTask; + } + + public ImpressionsManager getImpressionManager() { + return _impressionManager; + } + + public EventsTask getEventsTask() { + return _eventsTask; + } + + public TelemetrySyncTask getTelemetrySyncTask() { + return _telemetrySyncTask; + } + + public UniqueKeysTracker getUniqueKeysTracker() { + return _uniqueKeysTracker; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/common/SyncManager.java b/client/src/main/java/io/split/engine/common/SyncManager.java index b6a3c9b3..f955e4c4 100644 --- a/client/src/main/java/io/split/engine/common/SyncManager.java +++ b/client/src/main/java/io/split/engine/common/SyncManager.java @@ -1,9 +1,8 @@ package io.split.engine.common; -import io.split.engine.sse.listeners.FeedbackLoopListener; -import io.split.engine.sse.listeners.NotificationKeeperListener; +import java.io.IOException; public interface SyncManager { void start(); - void shutdown(); + void shutdown() throws IOException; } diff --git a/client/src/main/java/io/split/engine/common/SyncManagerImp.java b/client/src/main/java/io/split/engine/common/SyncManagerImp.java index 2d0fe2fa..f8bef316 100644 --- a/client/src/main/java/io/split/engine/common/SyncManagerImp.java +++ b/client/src/main/java/io/split/engine/common/SyncManagerImp.java @@ -1,100 +1,227 @@ package io.split.engine.common; import com.google.common.annotations.VisibleForTesting; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.split.cache.SegmentCache; -import io.split.cache.SplitCache; +import io.split.client.ApiKeyCounter; +import io.split.client.SplitClientConfig; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.engine.SDKReadinessGates; +import io.split.engine.experiments.RuleBasedSegmentParser; import io.split.engine.experiments.SplitFetcher; +import io.split.engine.experiments.SplitParser; import io.split.engine.experiments.SplitSynchronizationTask; -import io.split.engine.segments.SegmentSynchronizationTaskImp; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.StreamEventsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.ArrayList; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.SplitExecutorFactory.buildExecutorService; public class SyncManagerImp implements SyncManager { - private static final Logger _log = LoggerFactory.getLogger(SyncManager.class); + private static final Logger _log = LoggerFactory.getLogger(SyncManagerImp.class); private final AtomicBoolean _streamingEnabledConfig; private final Synchronizer _synchronizer; private final PushManager _pushManager; - private final AtomicBoolean _shutdown; + private final AtomicBoolean _shuttedDown; private final LinkedBlockingQueue _incomingPushStatus; - private final ExecutorService _executorService; + private final ExecutorService _pushMonitorExecutorService; + private final ExecutorService _initializationtExecutorService; + private final SDKReadinessGates _gates; private Future _pushStatusMonitorTask; private Backoff _backoff; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final TelemetrySynchronizer _telemetrySynchronizer; + private final SplitClientConfig _config; + private final long _startingSyncCallBackoffBaseMs; + private final SegmentSynchronizationTask _segmentSynchronizationTaskImp; + private final SplitSynchronizationTask _splitSynchronizationTask; + private static final long STARTING_SYNC_ALL_BACKOFF_MAX_WAIT_MS = 10000; // 10 seconds max wait + private final SplitAPI _splitAPI; + private final SdkEventsNotifier _notifier; @VisibleForTesting - /* package private */ SyncManagerImp(boolean streamingEnabledConfig, + /* package private */ SyncManagerImp(SplitTasks splitTasks, + boolean streamingEnabledConfig, Synchronizer synchronizer, PushManager pushManager, LinkedBlockingQueue pushMessages, - int authRetryBackOffBase) { + SDKReadinessGates gates, + TelemetryRuntimeProducer telemetryRuntimeProducer, + TelemetrySynchronizer telemetrySynchronizer, + SplitClientConfig config, + SplitAPI splitAPI) { + this(splitTasks, streamingEnabledConfig, synchronizer, pushManager, pushMessages, gates, telemetryRuntimeProducer, + telemetrySynchronizer, config, splitAPI, SdkEventsNotifier.NOOP); + } + + /* package private */ SyncManagerImp(SplitTasks splitTasks, + boolean streamingEnabledConfig, + Synchronizer synchronizer, + PushManager pushManager, + LinkedBlockingQueue pushMessages, + SDKReadinessGates gates, + TelemetryRuntimeProducer telemetryRuntimeProducer, + TelemetrySynchronizer telemetrySynchronizer, + SplitClientConfig config, + SplitAPI splitAPI, + SdkEventsNotifier notifier) { + _notifier = checkNotNull(notifier); _streamingEnabledConfig = new AtomicBoolean(streamingEnabledConfig); _synchronizer = checkNotNull(synchronizer); _pushManager = checkNotNull(pushManager); - _shutdown = new AtomicBoolean(false); + _shuttedDown = new AtomicBoolean(false); _incomingPushStatus = pushMessages; - _executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() - .setNameFormat("SPLIT-PushStatusMonitor-%d") - .setDaemon(true) - .build()); - _backoff = new Backoff(authRetryBackOffBase); + _pushMonitorExecutorService = buildExecutorService(config.getThreadFactory(), "SPLIT-PushStatusMonitor-%d"); + _initializationtExecutorService = buildExecutorService(config.getThreadFactory(), "SPLIT-Initialization-%d"); + _backoff = new Backoff(config.authRetryBackoffBase()); + _gates = checkNotNull(gates); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _telemetrySynchronizer = checkNotNull(telemetrySynchronizer); + _config = checkNotNull(config); + _startingSyncCallBackoffBaseMs = config.startingSyncCallBackoffBaseMs(); + _segmentSynchronizationTaskImp = checkNotNull(splitTasks.getSegmentSynchronizationTask()); + _splitSynchronizationTask = checkNotNull(splitTasks.getSplitSynchronizationTask()); + _splitAPI = splitAPI; } - public static SyncManagerImp build(boolean streamingEnabledConfig, - SplitSynchronizationTask splitSynchronizationTask, + public static SyncManagerImp build(SplitTasks splitTasks, SplitFetcher splitFetcher, - SegmentSynchronizationTaskImp segmentSynchronizationTaskImp, - SplitCache splitCache, - String authUrl, - CloseableHttpClient httpClient, - String streamingServiceUrl, - int authRetryBackOffBase, - CloseableHttpClient sseHttpClient, - SegmentCache segmentCache) { + SplitCacheProducer splitCacheProducer, + SplitAPI splitAPI, + SegmentCacheProducer segmentCacheProducer, + SDKReadinessGates gates, + TelemetryRuntimeProducer telemetryRuntimeProducer, + TelemetrySynchronizer telemetrySynchronizer, + SplitClientConfig config, + SplitParser splitParser, + RuleBasedSegmentParser ruleBasedSegmentParser, + FlagSetsFilter flagSetsFilter, + RuleBasedSegmentCache ruleBasedSegmentCache, + SdkEventsNotifier notifier) { LinkedBlockingQueue pushMessages = new LinkedBlockingQueue<>(); - Synchronizer synchronizer = new SynchronizerImp(splitSynchronizationTask, splitFetcher, segmentSynchronizationTaskImp, splitCache, segmentCache); - PushManager pushManager = PushManagerImp.build(synchronizer, streamingServiceUrl, authUrl, httpClient, pushMessages, sseHttpClient); - return new SyncManagerImp(streamingEnabledConfig, synchronizer, pushManager, pushMessages, authRetryBackOffBase); + Synchronizer synchronizer = new SynchronizerImp(splitTasks, + splitFetcher, + splitCacheProducer, + segmentCacheProducer, + ruleBasedSegmentCache, + config.streamingRetryDelay(), + config.streamingFetchMaxRetries(), + config.failedAttemptsBeforeLogging(), + config.getSetsFilter(), + notifier); + + PushManager pushManager = PushManagerImp.build(synchronizer, + config.streamingServiceURL(), + config.authServiceURL(), + splitAPI, + pushMessages, + telemetryRuntimeProducer, + config.getThreadFactory(), + splitParser, + splitCacheProducer, + flagSetsFilter, + ruleBasedSegmentCache, + ruleBasedSegmentParser, + notifier); + + return new SyncManagerImp(splitTasks, + config.streamingEnabled(), + synchronizer, + pushManager, + pushMessages, + gates, + telemetryRuntimeProducer, + telemetrySynchronizer, + config, + splitAPI, + notifier); } @Override public void start() { - if (_streamingEnabledConfig.get()) { - startStreamingMode(); - } else { - startPollingMode(); - } + _initializationtExecutorService.submit(() -> { + Backoff startBackoff = new Backoff(_startingSyncCallBackoffBaseMs, STARTING_SYNC_ALL_BACKOFF_MAX_WAIT_MS); + while(!_synchronizer.syncAll()) { + try{ + long howLong = startBackoff.interval(); + Thread.currentThread().sleep(howLong); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + break; + } + } + if (_shuttedDown.get()) { + return; + } + if (_log.isDebugEnabled()) { + _log.debug("SyncAll Ready"); + } + _gates.sdkInternalReady(); + _notifier.notify(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + if (_streamingEnabledConfig.get()) { + startStreamingMode(); + } else { + startPollingMode(); + } + _synchronizer.startPeriodicDataRecording(); + _telemetrySynchronizer.synchronizeConfig(_config, System.currentTimeMillis(), ApiKeyCounter.getApiKeyCounterInstance(). + getFactoryInstances(), new ArrayList<>()); + }); } @Override - public void shutdown() { - _shutdown.set(true); + public void shutdown() throws IOException { + _log.info("Shutting down SyncManagerImp"); + if(_shuttedDown.get()) { + return; + } + _shuttedDown.set(true); + _initializationtExecutorService.shutdownNow(); _synchronizer.stopPeriodicFetching(); - _pushManager.stop(); + if (_streamingEnabledConfig.get()) { + _pushManager.stop(); + _pushMonitorExecutorService.shutdownNow(); + } + _segmentSynchronizationTaskImp.close(); + _log.info("Successful shutdown of segment fetchers"); + _splitSynchronizationTask.close(); + _log.info("Successful shutdown of splits"); + _synchronizer.stopPeriodicDataRecording(); + _splitAPI.close(); } private void startStreamingMode() { _log.debug("Starting in streaming mode ..."); - _synchronizer.syncAll(); if (null == _pushStatusMonitorTask) { - _pushStatusMonitorTask = _executorService.submit(this::incomingPushStatusHandler); + _pushStatusMonitorTask = _pushMonitorExecutorService.submit(this::incomingPushStatusHandler); } _pushManager.start(); - + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.SYNC_MODE_UPDATE.getType(), + StreamEventsEnum.SyncModeUpdateValues.STREAMING_EVENT.getValue(), System.currentTimeMillis())); } private void startPollingMode() { _log.debug("Starting in polling mode ..."); _synchronizer.startPeriodicFetching(); + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.SYNC_MODE_UPDATE.getType(), + StreamEventsEnum.SyncModeUpdateValues.POLLING_EVENT.getValue(), System.currentTimeMillis())); } @VisibleForTesting @@ -110,22 +237,31 @@ private void startPollingMode() { _pushManager.startWorkers(); _pushManager.scheduleConnectionReset(); _backoff.reset(); + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.STREAMING_STATUS.getType(), + StreamEventsEnum.StreamingStatusValues.STREAMING_ENABLED.getValue(), System.currentTimeMillis())); + _log.info("Streaming up and running."); break; case STREAMING_DOWN: + _log.info("Streaming service temporarily unavailable, working in polling mode."); _pushManager.stopWorkers(); + // if the whole SDK is being shutdown, don't start polling, + // in case the polling threads are not terminated and a graceful shutdown will fail. + if(_shuttedDown.get()) { + break; + } _synchronizer.startPeriodicFetching(); break; case STREAMING_BACKOFF: - long howLong = _backoff.interval() * 1000; - _log.error(String.format("Retryable error in streaming subsystem. Switching to polling and retrying in %d seconds", howLong/1000)); + long howLong = _backoff.interval(); + _log.info(String.format("Retryable error in streaming subsystem. Switching to polling and retrying in %d seconds", howLong)); _synchronizer.startPeriodicFetching(); - _pushManager.stopWorkers(); _pushManager.stop(); - Thread.sleep(howLong); + Thread.sleep(howLong * 1000); _incomingPushStatus.clear(); _pushManager.start(); break; case STREAMING_OFF: + _log.info("Unrecoverable error in streaming subsystem. SDK will work in polling-mode and will not retry an SSE connection."); _pushManager.stop(); _synchronizer.startPeriodicFetching(); if (null != _pushStatusMonitorTask) { @@ -139,4 +275,4 @@ private void startPollingMode() { } } } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/common/Synchronizer.java b/client/src/main/java/io/split/engine/common/Synchronizer.java index ab8467a5..d685a3ed 100644 --- a/client/src/main/java/io/split/engine/common/Synchronizer.java +++ b/client/src/main/java/io/split/engine/common/Synchronizer.java @@ -1,10 +1,15 @@ package io.split.engine.common; +import io.split.engine.sse.dtos.SplitKillNotification; + public interface Synchronizer { - void syncAll(); + boolean syncAll(); void startPeriodicFetching(); void stopPeriodicFetching(); - void refreshSplits(long targetChangeNumber); - void localKillSplit(String splitName, String defaultTreatment, long newChangeNumber); - void refreshSegment(String segmentName, long targetChangeNumber); + void refreshSplits(Long targetChangeNumber, Long ruleBasedSegmentChangeNumber); + void localKillSplit(SplitKillNotification splitKillNotification); + void refreshSegment(String segmentName, Long targetChangeNumber); + void startPeriodicDataRecording(); + void stopPeriodicDataRecording(); + void forceRefreshSegment(String segmentName); } diff --git a/client/src/main/java/io/split/engine/common/SynchronizerImp.java b/client/src/main/java/io/split/engine/common/SynchronizerImp.java index 0d22ff9f..9679354a 100644 --- a/client/src/main/java/io/split/engine/common/SynchronizerImp.java +++ b/client/src/main/java/io/split/engine/common/SynchronizerImp.java @@ -1,64 +1,108 @@ package io.split.engine.common; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.split.cache.SegmentCache; -import io.split.cache.SplitCache; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.events.EventsTask; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.engine.experiments.FetchResult; import io.split.engine.experiments.SplitFetcher; import io.split.engine.experiments.SplitSynchronizationTask; import io.split.engine.segments.SegmentFetcher; import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.engine.sse.dtos.SplitKillNotification; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.synchronizer.TelemetrySyncTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; +import java.util.Collections; +import java.util.HashSet; +import java.util.function.Function; +import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkNotNull; public class SynchronizerImp implements Synchronizer { - private static final Logger _log = LoggerFactory.getLogger(Synchronizer.class); - private static final int RETRIES_NUMBER = 10; + // The boxing here IS necessary, so that the constants are not inlined by the compiler + // and can be modified for the test (we don't want to wait that much in an UT) + private static final long ON_DEMAND_FETCH_BACKOFF_BASE_MS = new Long(10000); //backoff base starting at 10 seconds (!) + private static final long ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS = new Long(60000); // don't sleep for more than 1 second + private static final int ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES = 10; + + private static final Logger _log = LoggerFactory.getLogger(Synchronizer.class); private final SplitSynchronizationTask _splitSynchronizationTask; private final SplitFetcher _splitFetcher; private final SegmentSynchronizationTask _segmentSynchronizationTaskImp; - private final ScheduledExecutorService _syncAllScheduledExecutorService; - private final SplitCache _splitCache; - private final SegmentCache _segmentCache; + private final SplitCacheProducer _splitCacheProducer; + private final RuleBasedSegmentCacheProducer _ruleBasedSegmentCacheProducer; + private final SegmentCacheProducer segmentCacheProducer; + private final ImpressionsManager _impressionManager; + private final EventsTask _eventsTask; + private final TelemetrySyncTask _telemetrySyncTask; + private final UniqueKeysTracker _uniqueKeysTracker; + private final int _onDemandFetchRetryDelayMs; + private final int _onDemandFetchMaxRetries; + private final int _failedAttemptsBeforeLogging; + private final String _sets; + private final SdkEventsNotifier _notifier; - public SynchronizerImp(SplitSynchronizationTask splitSynchronizationTask, + public SynchronizerImp(SplitTasks splitTasks, SplitFetcher splitFetcher, - SegmentSynchronizationTask segmentSynchronizationTaskImp, - SplitCache splitCache, - SegmentCache segmentCache) { - _splitSynchronizationTask = checkNotNull(splitSynchronizationTask); - _splitFetcher = checkNotNull(splitFetcher); - _segmentSynchronizationTaskImp = checkNotNull(segmentSynchronizationTaskImp); - _splitCache = checkNotNull(splitCache); - _segmentCache = checkNotNull(segmentCache); + SplitCacheProducer splitCacheProducer, + SegmentCacheProducer segmentCacheProducer, + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer, + int onDemandFetchRetryDelayMs, + int onDemandFetchMaxRetries, + int failedAttemptsBeforeLogging, + HashSet sets) { + this(splitTasks, splitFetcher, splitCacheProducer, segmentCacheProducer, ruleBasedSegmentCacheProducer, + onDemandFetchRetryDelayMs, onDemandFetchMaxRetries, failedAttemptsBeforeLogging, sets, SdkEventsNotifier.NOOP); + } - ThreadFactory splitsThreadFactory = new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("Split-SyncAll-%d") - .build(); - _syncAllScheduledExecutorService = Executors.newSingleThreadScheduledExecutor(splitsThreadFactory); + public SynchronizerImp(SplitTasks splitTasks, + SplitFetcher splitFetcher, + SplitCacheProducer splitCacheProducer, + SegmentCacheProducer segmentCacheProducer, + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer, + int onDemandFetchRetryDelayMs, + int onDemandFetchMaxRetries, + int failedAttemptsBeforeLogging, + HashSet sets, + SdkEventsNotifier notifier) { + _notifier = checkNotNull(notifier); + _splitSynchronizationTask = checkNotNull(splitTasks.getSplitSynchronizationTask()); + _splitFetcher = checkNotNull(splitFetcher); + _segmentSynchronizationTaskImp = checkNotNull(splitTasks.getSegmentSynchronizationTask()); + _splitCacheProducer = checkNotNull(splitCacheProducer); + _ruleBasedSegmentCacheProducer = checkNotNull(ruleBasedSegmentCacheProducer); + this.segmentCacheProducer = checkNotNull(segmentCacheProducer); + _onDemandFetchRetryDelayMs = checkNotNull(onDemandFetchRetryDelayMs); + _onDemandFetchMaxRetries = onDemandFetchMaxRetries; + _failedAttemptsBeforeLogging = failedAttemptsBeforeLogging; + _impressionManager = splitTasks.getImpressionManager(); + _eventsTask = splitTasks.getEventsTask(); + _telemetrySyncTask = splitTasks.getTelemetrySyncTask(); + _uniqueKeysTracker = splitTasks.getUniqueKeysTracker(); + _sets = sets.stream().collect(Collectors.joining(",")); } @Override - public void syncAll() { - _syncAllScheduledExecutorService.schedule(() -> { - _splitFetcher.fetchAll(true); - _segmentSynchronizationTaskImp.fetchAll(true); - }, 0, TimeUnit.SECONDS); + public boolean syncAll() { + FetchResult fetchResult = _splitFetcher.forceRefresh(new FetchOptions.Builder().flagSetsFilter(_sets).cacheControlHeaders(true).build()); + return fetchResult.isSuccess() && _segmentSynchronizationTaskImp.fetchAllSynchronous(); } @Override public void startPeriodicFetching() { _log.debug("Starting Periodic Fetching ..."); - _splitSynchronizationTask.startPeriodicFetching(); - _segmentSynchronizationTaskImp.startPeriodicFetching(); + _splitSynchronizationTask.start(); + _segmentSynchronizationTaskImp.start(); } @Override @@ -68,36 +112,216 @@ public void stopPeriodicFetching() { _segmentSynchronizationTaskImp.stop(); } - @Override - public void refreshSplits(long targetChangeNumber) { - int retries = 1; - while(targetChangeNumber > _splitCache.getChangeNumber() && retries <= RETRIES_NUMBER) { - _splitFetcher.forceRefresh(true); - retries++; + private static class SyncResult { + + /* package private */ SyncResult(boolean success, int remainingAttempts, FetchResult fetchResult) { + _success = success; + _remainingAttempts = remainingAttempts; + _fetchResult = fetchResult; + } + + public boolean success() { return _success; } + public int remainingAttempts() { return _remainingAttempts; } + + private final boolean _success; + private final int _remainingAttempts; + private final FetchResult _fetchResult; + } + + private SyncResult attemptSplitsSync(long targetChangeNumber, long ruleBasedSegmentChangeNumber, + FetchOptions opts, + Function nextWaitMs, + int maxRetries) { + int remainingAttempts = maxRetries; + while(true) { + remainingAttempts--; + FetchResult fetchResult = _splitFetcher.forceRefresh(opts); + if (fetchResult != null && !fetchResult.retry() && !fetchResult.isSuccess()) { + return new SyncResult(false, remainingAttempts, fetchResult); + } + if (targetChangeNumber <= _splitCacheProducer.getChangeNumber() + && ruleBasedSegmentChangeNumber <= _ruleBasedSegmentCacheProducer.getChangeNumber()) { + return new SyncResult(true, remainingAttempts, fetchResult); + } else if (remainingAttempts <= 0) { + return new SyncResult(false, remainingAttempts, fetchResult); + } + try { + long howLong = nextWaitMs.apply(null); + Thread.sleep(howLong); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + _log.debug("Error trying to sleep current Thread."); + } } } @Override - public void localKillSplit(String splitName, String defaultTreatment, long newChangeNumber) { - if (newChangeNumber > _splitCache.getChangeNumber()) { - _splitCache.kill(splitName, defaultTreatment, newChangeNumber); - refreshSplits(newChangeNumber); + public void refreshSplits(Long targetChangeNumber, Long ruleBasedSegmentChangeNumber) { + + if (targetChangeNumber == null || targetChangeNumber == 0) { + targetChangeNumber = _splitCacheProducer.getChangeNumber(); + } + if (ruleBasedSegmentChangeNumber == null || ruleBasedSegmentChangeNumber == 0) { + ruleBasedSegmentChangeNumber = _ruleBasedSegmentCacheProducer.getChangeNumber(); + } + + if (targetChangeNumber <= _splitCacheProducer.getChangeNumber() + && ruleBasedSegmentChangeNumber <= _ruleBasedSegmentCacheProducer.getChangeNumber()) { + return; + } + + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); + FetchOptions opts = new FetchOptions.Builder() + .cacheControlHeaders(true) + .flagSetsFilter(_sets) + .build(); + + SyncResult regularResult = attemptSplitsSync(targetChangeNumber, ruleBasedSegmentChangeNumber, opts, + (discard) -> (long) _onDemandFetchRetryDelayMs, _onDemandFetchMaxRetries); + + int attempts = _onDemandFetchMaxRetries - regularResult.remainingAttempts(); + if (regularResult.success()) { + _log.debug(String.format("Refresh completed in %s attempts.", attempts)); + + regularResult._fetchResult.getSegments().stream() + .forEach(segmentName -> forceRefreshSegment(segmentName)); + return; + } + + _log.info(String.format("No changes fetched after %s attempts. Will retry bypassing CDN.", attempts)); + FetchOptions withCdnBypass = new FetchOptions.Builder(opts).targetChangeNumber(targetChangeNumber).build(); + Backoff backoff = new Backoff(ON_DEMAND_FETCH_BACKOFF_BASE_MS, ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS); + SyncResult withCDNBypassed = attemptSplitsSync(targetChangeNumber, ruleBasedSegmentChangeNumber, withCdnBypass, + (discard) -> backoff.interval(), ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); + + int withoutCDNAttempts = ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - withCDNBypassed._remainingAttempts; + if (withCDNBypassed.success()) { + _log.debug(String.format("Refresh completed bypassing the CDN in %s attempts.", withoutCDNAttempts)); + withCDNBypassed._fetchResult.getSegments().stream() + .forEach(segmentName -> forceRefreshSegment(segmentName)); + } else { + _log.debug(String.format("No changes fetched after %s attempts with CDN bypassed.", withoutCDNAttempts)); } } @Override - public void refreshSegment(String segmentName, long changeNumber) { - int retries = 1; - while(changeNumber > _segmentCache.getChangeNumber(segmentName) && retries <= RETRIES_NUMBER) { - SegmentFetcher fetcher = _segmentSynchronizationTaskImp.getFetcher(segmentName); - try{ - fetcher.fetch(true); + public void localKillSplit(SplitKillNotification splitKillNotification) { + if (splitKillNotification.getChangeNumber() > _splitCacheProducer.getChangeNumber()) { + _splitCacheProducer.kill(splitKillNotification.getSplitName(), splitKillNotification.getDefaultTreatment(), + splitKillNotification.getChangeNumber()); + _notifier.notify(SdkInternalEvent.FLAG_KILLED_NOTIFICATION, + SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Collections.singleton(splitKillNotification.getSplitName()))); + refreshSplits(splitKillNotification.getChangeNumber(), 0L); + } + } + + public SyncResult attemptSegmentSync(String segmentName, + long targetChangeNumber, + FetchOptions opts, + Function nextWaitMs, + int maxRetries) { + + int remainingAttempts = maxRetries; + SegmentFetcher fetcher = _segmentSynchronizationTaskImp.getFetcher(segmentName); + checkNotNull(fetcher); + + while(true) { + remainingAttempts--; + fetcher.fetch(opts); + if (targetChangeNumber <= segmentCacheProducer.getChangeNumber(segmentName)) { + return new SyncResult(true, remainingAttempts, new FetchResult(false, true, new HashSet<>())); + } else if (remainingAttempts <= 0) { + return new SyncResult(false, remainingAttempts, new FetchResult(false, true, new HashSet<>())); + } + try { + long howLong = nextWaitMs.apply(null); + Thread.sleep(howLong); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + _log.debug("Error trying to sleep current Thread."); } - //We are sure this will never happen because getFetcher firts initiate the segment. This try/catch is for safe only. - catch (NullPointerException np){ - throw new NullPointerException(); + } + } + + @Override + public void refreshSegment(String segmentName, Long targetChangeNumber) { + + if (targetChangeNumber <= segmentCacheProducer.getChangeNumber(segmentName)) { + return; + } + + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); + FetchOptions opts = new FetchOptions.Builder() + .cacheControlHeaders(true) + .build(); + + SyncResult regularResult = attemptSegmentSync(segmentName, targetChangeNumber, opts, + (discard) -> (long) _onDemandFetchRetryDelayMs, _onDemandFetchMaxRetries); + + int attempts = _onDemandFetchMaxRetries - regularResult.remainingAttempts(); + if (regularResult.success()) { + _log.debug(String.format("Segment %s refresh completed in %s attempts.", segmentName, attempts)); + + return; + } + + _log.info(String.format("No changes fetched for segment %s after %s attempts. Will retry bypassing CDN.", segmentName, attempts)); + FetchOptions withCdnBypass = new FetchOptions.Builder(opts).targetChangeNumber(targetChangeNumber).build(); + Backoff backoff = new Backoff(ON_DEMAND_FETCH_BACKOFF_BASE_MS, ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS); + SyncResult withCDNBypassed = attemptSegmentSync(segmentName, targetChangeNumber, withCdnBypass, + (discard) -> backoff.interval(), ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); + + int withoutCDNAttempts = ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES - withCDNBypassed._remainingAttempts; + if (withCDNBypassed.success()) { + _log.debug(String.format("Segment %s refresh completed bypassing the CDN in %s attempts.", segmentName, withoutCDNAttempts)); + } else { + _log.debug(String.format("No changes fetched for segment %s after %s attempts with CDN bypassed.", segmentName, withoutCDNAttempts)); + } + } + + @Override + public void startPeriodicDataRecording() { + try { + _impressionManager.start(); + } catch (Exception e) { + _log.error("Error trying to init Impression Manager synchronizer task.", e); + } + if (_uniqueKeysTracker != null){ + try { + _uniqueKeysTracker.start(); + } catch (Exception e) { + _log.error("Error trying to init Unique Keys Tracker synchronizer task.", e); } - retries++; } + try { + _eventsTask.start(); + } catch (Exception e) { + _log.error("Error trying to init Events synchronizer task.", e); + } + try { + _telemetrySyncTask.startScheduledTask(); + } catch (Exception e) { + _log.error("Error trying to Telemetry synchronizer task.", e); + } + } + + @Override + public void stopPeriodicDataRecording() { + _impressionManager.close(); + _log.info("Successful shutdown of impressions manager"); + if (_uniqueKeysTracker != null){ + _uniqueKeysTracker.stop(); + _log.info("Successful stop of UniqueKeysTracker"); + } + _eventsTask.close(); + _log.info("Successful shutdown of eventsTask"); + _telemetrySyncTask.stopScheduledTask(); + _log.info("Successful shutdown of telemetry sync task"); + } + + @Override + public void forceRefreshSegment(String segmentName){ + SegmentFetcher segmentFetcher = _segmentSynchronizationTaskImp.getFetcher(segmentName); + segmentFetcher.fetch(new FetchOptions.Builder().build()); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/evaluator/EvaluationContext.java b/client/src/main/java/io/split/engine/evaluator/EvaluationContext.java new file mode 100644 index 00000000..540acc5d --- /dev/null +++ b/client/src/main/java/io/split/engine/evaluator/EvaluationContext.java @@ -0,0 +1,31 @@ +package io.split.engine.evaluator; + +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.SegmentCacheConsumer; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class EvaluationContext { + private final Evaluator _evaluator; + private final SegmentCacheConsumer _segmentCacheConsumer; + private final RuleBasedSegmentCacheConsumer _ruleBasedSegmentCacheConsumer; + + public EvaluationContext(Evaluator evaluator, SegmentCacheConsumer segmentCacheConsumer, + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) { + _evaluator = checkNotNull(evaluator); + _segmentCacheConsumer = checkNotNull(segmentCacheConsumer); + _ruleBasedSegmentCacheConsumer = checkNotNull(ruleBasedSegmentCacheConsumer); + } + + public Evaluator getEvaluator() { + return _evaluator; + } + + public SegmentCacheConsumer getSegmentCache() { + return _segmentCacheConsumer; + } + + public RuleBasedSegmentCacheConsumer getRuleBasedSegmentCache() { + return _ruleBasedSegmentCacheConsumer; + } +} diff --git a/client/src/main/java/io/split/engine/evaluator/Evaluator.java b/client/src/main/java/io/split/engine/evaluator/Evaluator.java index fbcc52c3..f745b8d7 100644 --- a/client/src/main/java/io/split/engine/evaluator/Evaluator.java +++ b/client/src/main/java/io/split/engine/evaluator/Evaluator.java @@ -1,7 +1,13 @@ package io.split.engine.evaluator; +import java.util.List; import java.util.Map; public interface Evaluator { - EvaluatorImp.TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map attributes); -} + EvaluatorImp.TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String featureFlag, + Map attributes); + Map evaluateFeatures(String matchingKey, String bucketingKey, + List featureFlags, Map attributes); + Map evaluateFeaturesByFlagSets(String key, String bucketingKey, + List flagSets, Map attributes); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java b/client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java index c4efe70c..8d7147aa 100644 --- a/client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java +++ b/client/src/main/java/io/split/engine/evaluator/EvaluatorImp.java @@ -1,48 +1,103 @@ package io.split.engine.evaluator; import io.split.client.dtos.ConditionType; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentCalculator; import io.split.client.exceptions.ChangeNumberExceptionWrapper; -import io.split.cache.SplitCache; import io.split.engine.experiments.ParsedCondition; import io.split.engine.experiments.ParsedSplit; import io.split.engine.splitter.Splitter; -import io.split.grammar.Treatments; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import static com.google.common.base.Preconditions.checkNotNull; public class EvaluatorImp implements Evaluator { - - private static final Logger _log = LoggerFactory.getLogger(EvaluatorImp.class); - private final SplitCache _splitCache; + private final SegmentCacheConsumer _segmentCacheConsumer; + private final EvaluationContext _evaluationContext; + private final SplitCacheConsumer _splitCacheConsumer; + private final FallbackTreatmentCalculator _fallbackTreatmentCalculator; + private final String _evaluatorException = "Evaluator Exception"; + + public EvaluatorImp(SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCache, + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer, + FallbackTreatmentCalculator fallbackTreatmentCalculator) { + _splitCacheConsumer = checkNotNull(splitCacheConsumer); + _segmentCacheConsumer = checkNotNull(segmentCache); + _evaluationContext = new EvaluationContext(this, _segmentCacheConsumer, ruleBasedSegmentCacheConsumer); + _fallbackTreatmentCalculator = fallbackTreatmentCalculator; + } - public EvaluatorImp(SplitCache splitCache) { - _splitCache = checkNotNull(splitCache); + @Override + public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String featureFlag, Map attributes) { + ParsedSplit parsedSplit = _splitCacheConsumer.get(featureFlag); + return evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplit, featureFlag); } @Override - public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String bucketingKey, String split, Map attributes) { + public Map evaluateFeatures(String matchingKey, String bucketingKey, List featureFlags, + Map attributes) { + Map results = new HashMap<>(); + Map parsedSplits = _splitCacheConsumer.fetchMany(featureFlags); + if (parsedSplits == null) { + return results; + } + featureFlags.forEach(s -> results.put(s, evaluateParsedSplit(matchingKey, bucketingKey, attributes, parsedSplits.get(s), s))); + return results; + } + + @Override + public Map evaluateFeaturesByFlagSets(String key, String bucketingKey, + List flagSets, Map attributes) { + List flagSetsWithNames = getFeatureFlagNamesByFlagSets(flagSets); try { - ParsedSplit parsedSplit = _splitCache.get(split); + return evaluateFeatures(key, bucketingKey, flagSetsWithNames, attributes); + } catch (Exception e) { + _log.error(_evaluatorException, e); + return createMapControl(flagSetsWithNames, io.split.engine.evaluator.Labels.EXCEPTION); + } + } - if (parsedSplit == null) { - return new TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.DEFINITION_NOT_FOUND); - } + private Map createMapControl(List featureFlags, String label) { + Map result = new HashMap<>(); + featureFlags.forEach(s -> result.put(s, checkFallbackTreatment(s, label))); + return result; + } - return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes); - } - catch (ChangeNumberExceptionWrapper e) { - _log.error("Evaluator Exception", e.wrappedException()); - return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION, e.changeNumber()); - } catch (Exception e) { - _log.error("Evaluator Exception", e); - return new EvaluatorImp.TreatmentLabelAndChangeNumber(Treatments.CONTROL, Labels.EXCEPTION); + private EvaluatorImp.TreatmentLabelAndChangeNumber checkFallbackTreatment(String featureName, String label) { + FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, label); + return new EvaluatorImp.TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), + fallbackTreatment.getLabel(), + null, + getFallbackConfig(fallbackTreatment), + false); + } + + private List getFeatureFlagNamesByFlagSets(List flagSets) { + HashSet ffNamesToReturn = new HashSet<>(); + Map> namesByFlagSets = _splitCacheConsumer.getNamesByFlagSets(flagSets); + for (String set: flagSets) { + HashSet flags = namesByFlagSets.get(set); + if (flags == null || flags.isEmpty()) { + _log.warn(String.format("You passed %s Flag Set that does not contain cached feature flag names, please double check " + + "what Flag Sets are in use in the Split user interface.", set)); + continue; + } + ffNamesToReturn.addAll(flags); } + return new ArrayList<>(ffNamesToReturn); } /** @@ -53,26 +108,41 @@ public TreatmentLabelAndChangeNumber evaluateFeature(String matchingKey, String * @return * @throws ChangeNumberExceptionWrapper */ - private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bucketingKey, ParsedSplit parsedSplit, Map attributes) throws ChangeNumberExceptionWrapper { + private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bucketingKey, ParsedSplit parsedSplit, Map attributes) throws ChangeNumberExceptionWrapper { try { + String config = getConfig(parsedSplit, parsedSplit.defaultTreatment()); if (parsedSplit.killed()) { - String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(parsedSplit.defaultTreatment()) : null; - return new TreatmentLabelAndChangeNumber(parsedSplit.defaultTreatment(), Labels.KILLED, parsedSplit.changeNumber(), config); + return new TreatmentLabelAndChangeNumber( + parsedSplit.defaultTreatment(), + Labels.KILLED, + parsedSplit.changeNumber(), + config, + parsedSplit.impressionsDisabled()); + } + + String bk = getBucketingKey(bucketingKey, matchingKey); + + if (!parsedSplit.prerequisitesMatcher().match(matchingKey, bk, attributes, _evaluationContext)) { + return new TreatmentLabelAndChangeNumber( + parsedSplit.defaultTreatment(), + Labels.PREREQUISITES_NOT_MET, + parsedSplit.changeNumber(), + config, + parsedSplit.impressionsDisabled()); } /* - * There are three parts to a single Split: 1) Whitelists 2) Traffic Allocation + * There are three parts to a single Feature flag: 1) Whitelists 2) Traffic Allocation * 3) Rollout. The flag inRollout is there to understand when we move into the Rollout * section. This is because we need to make sure that the Traffic Allocation * computation happens after the whitelist but before the rollout. */ boolean inRollout = false; - String bk = (bucketingKey == null) ? matchingKey : bucketingKey; - for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { - if (!inRollout && parsedCondition.conditionType() == ConditionType.ROLLOUT) { + if (checkRollout(inRollout, parsedCondition)) { if (parsedSplit.trafficAllocation() < 100) { // if the traffic allocation is 100%, no need to do anything special. @@ -80,47 +150,104 @@ private TreatmentLabelAndChangeNumber getTreatment(String matchingKey, String bu if (bucket > parsedSplit.trafficAllocation()) { // out of split - String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(parsedSplit.defaultTreatment()) : null; - return new TreatmentLabelAndChangeNumber(parsedSplit.defaultTreatment(), Labels.NOT_IN_SPLIT, parsedSplit.changeNumber(), config); + config = getConfig(parsedSplit, parsedSplit.defaultTreatment()); + return new TreatmentLabelAndChangeNumber(parsedSplit.defaultTreatment(), Labels.NOT_IN_SPLIT, + parsedSplit.changeNumber(), config, parsedSplit.impressionsDisabled()); } } inRollout = true; } - if (parsedCondition.matcher().match(matchingKey, bucketingKey, attributes, this)) { + if (parsedCondition.matcher().match(matchingKey, bucketingKey, attributes, _evaluationContext)) { String treatment = Splitter.getTreatment(bk, parsedSplit.seed(), parsedCondition.partitions(), parsedSplit.algo()); - String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(treatment) : null; - return new TreatmentLabelAndChangeNumber(treatment, parsedCondition.label(), parsedSplit.changeNumber(), config); + config = getConfig(parsedSplit, treatment); + return new TreatmentLabelAndChangeNumber( + treatment, + parsedCondition.label(), + parsedSplit.changeNumber(), + config, + parsedSplit.impressionsDisabled()); } } - String config = parsedSplit.configurations() != null ? parsedSplit.configurations().get(parsedSplit.defaultTreatment()) : null; - return new TreatmentLabelAndChangeNumber(parsedSplit.defaultTreatment(), Labels.DEFAULT_RULE, parsedSplit.changeNumber(), config); + config = getConfig(parsedSplit, parsedSplit.defaultTreatment()); + + return new TreatmentLabelAndChangeNumber( + parsedSplit.defaultTreatment(), + Labels.DEFAULT_RULE, + parsedSplit.changeNumber(), + config, + parsedSplit.impressionsDisabled()); } catch (Exception e) { throw new ChangeNumberExceptionWrapper(e, parsedSplit.changeNumber()); } } + private boolean checkRollout(boolean inRollout, ParsedCondition parsedCondition) { + return (!inRollout && parsedCondition.conditionType() == ConditionType.ROLLOUT); + } + + private String getBucketingKey(String bucketingKey, String matchingKey) { + return (bucketingKey == null) ? matchingKey : bucketingKey; + } + + private String getConfig(ParsedSplit parsedSplit, String returnedTreatment) { + return parsedSplit.configurations() != null ? parsedSplit.configurations().get(returnedTreatment) : null; + } + + private String getFallbackConfig(FallbackTreatment fallbackTreatment) { + if (fallbackTreatment.getConfig() != null) { + return fallbackTreatment.getConfig(); + } + + return null; + } + + private TreatmentLabelAndChangeNumber evaluateParsedSplit(String matchingKey, String bucketingKey, Map attributes, + ParsedSplit parsedSplit, String featureName) { + try { + if (parsedSplit == null) { + FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.DEFINITION_NOT_FOUND); + return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), + fallbackTreatment.getLabel(), + null, + getFallbackConfig(fallbackTreatment), + false); + } + return getTreatment(matchingKey, bucketingKey, parsedSplit, attributes); + } catch (ChangeNumberExceptionWrapper e) { + _log.error(_evaluatorException, e.wrappedException()); + FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION); + return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel(), e.changeNumber()); + } catch (Exception e) { + _log.error(_evaluatorException, e); + FallbackTreatment fallbackTreatment = _fallbackTreatmentCalculator.resolve(featureName, Labels.EXCEPTION); + return new TreatmentLabelAndChangeNumber(fallbackTreatment.getTreatment(), fallbackTreatment.getLabel()); + } + } + public static final class TreatmentLabelAndChangeNumber { public final String treatment; public final String label; public final Long changeNumber; public final String configurations; + public final boolean track; public TreatmentLabelAndChangeNumber(String treatment, String label) { - this(treatment, label, null, null); + this(treatment, label, null, null, true); } public TreatmentLabelAndChangeNumber(String treatment, String label, Long changeNumber) { - this(treatment, label, changeNumber, null); + this(treatment, label, changeNumber, null, true); } - public TreatmentLabelAndChangeNumber(String treatment, String label, Long changeNumber, String configurations) { + public TreatmentLabelAndChangeNumber(String treatment, String label, Long changeNumber, String configurations, boolean track) { this.treatment = treatment; this.label = label; this.changeNumber = changeNumber; this.configurations = configurations; + this.track = track; } } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/evaluator/Labels.java b/client/src/main/java/io/split/engine/evaluator/Labels.java index 97e486b9..28966d51 100644 --- a/client/src/main/java/io/split/engine/evaluator/Labels.java +++ b/client/src/main/java/io/split/engine/evaluator/Labels.java @@ -6,4 +6,7 @@ public class Labels { public static final String KILLED = "killed"; public static final String DEFINITION_NOT_FOUND = "definition not found"; public static final String EXCEPTION = "exception"; -} + public static final String UNSUPPORTED_MATCHER = "targeting rule type unsupported by sdk"; + public static final String PREREQUISITES_NOT_MET = "prerequisites not met"; + public static final String NOT_READY = "not ready"; +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/experiments/FetchResult.java b/client/src/main/java/io/split/engine/experiments/FetchResult.java new file mode 100644 index 00000000..c0d678c0 --- /dev/null +++ b/client/src/main/java/io/split/engine/experiments/FetchResult.java @@ -0,0 +1,26 @@ +package io.split.engine.experiments; + +import java.util.Set; + +public class FetchResult { + private boolean _success; + private boolean _retry; + private Set _segments; + + public FetchResult(boolean success, boolean retry, Set segments) { + _success = success; + _retry = retry; + _segments = segments; + } + + public boolean isSuccess() { + return _success; + } + public boolean retry() { + return _retry; + } + + public Set getSegments() { + return _segments; + } +} diff --git a/client/src/main/java/io/split/engine/experiments/ParsedCondition.java b/client/src/main/java/io/split/engine/experiments/ParsedCondition.java index 5c2b06b6..ad2e32a5 100644 --- a/client/src/main/java/io/split/engine/experiments/ParsedCondition.java +++ b/client/src/main/java/io/split/engine/experiments/ParsedCondition.java @@ -53,11 +53,12 @@ public int hashCode() { result = 31 * result + _matcher.hashCode(); int partitionsHashCode = 17; - for (Partition p : _partitions) { - partitionsHashCode = 31 * partitionsHashCode + p.treatment.hashCode(); - partitionsHashCode = 31 * partitionsHashCode + p.size; + if (_partitions != null) { + for (Partition p : _partitions) { + partitionsHashCode = 31 * partitionsHashCode + p.treatment.hashCode(); + partitionsHashCode = 31 * partitionsHashCode + p.size; + } } - result = 31 * result + partitionsHashCode; return result; } @@ -75,7 +76,9 @@ public boolean equals(Object obj) { if (!result) { return result; } - + if (_partitions == null) { + return result & (_partitions == other._partitions); + } if (_partitions.size() != other._partitions.size()) { return result; } @@ -97,6 +100,9 @@ public String toString() { bldr.append(_matcher); bldr.append(" then split "); boolean first = true; + if (_partitions == null) { + return bldr.toString(); + } for (Partition partition : _partitions) { if (!first) { bldr.append(','); diff --git a/client/src/main/java/io/split/engine/experiments/ParsedRuleBasedSegment.java b/client/src/main/java/io/split/engine/experiments/ParsedRuleBasedSegment.java new file mode 100644 index 00000000..c0043970 --- /dev/null +++ b/client/src/main/java/io/split/engine/experiments/ParsedRuleBasedSegment.java @@ -0,0 +1,134 @@ +package io.split.engine.experiments; + +import com.google.common.collect.ImmutableList; +import io.split.client.dtos.ExcludedSegments; +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class ParsedRuleBasedSegment { + + private final String _ruleBasedSegment; + private final ImmutableList _parsedCondition; + private final String _trafficTypeName; + private final long _changeNumber; + private final List _excludedKeys; + private final List _excludedSegments; + + public static ParsedRuleBasedSegment createParsedRuleBasedSegmentForTests( + String ruleBasedSegment, + List matcherAndSplits, + String trafficTypeName, + long changeNumber, + List excludedKeys, + List excludedSegments + ) { + return new ParsedRuleBasedSegment( + ruleBasedSegment, + matcherAndSplits, + trafficTypeName, + changeNumber, + excludedKeys, + excludedSegments + ); + } + + public ParsedRuleBasedSegment( + String ruleBasedSegment, + List matcherAndSplits, + String trafficTypeName, + long changeNumber, + List excludedKeys, + List excludedSegments + ) { + _ruleBasedSegment = ruleBasedSegment; + _parsedCondition = ImmutableList.copyOf(matcherAndSplits); + _trafficTypeName = trafficTypeName; + _changeNumber = changeNumber; + _excludedKeys = excludedKeys; + _excludedSegments = excludedSegments; + } + + public String ruleBasedSegment() { + return _ruleBasedSegment; + } + + public List parsedConditions() { + return _parsedCondition; + } + + public String trafficTypeName() {return _trafficTypeName;} + + public long changeNumber() {return _changeNumber;} + + public List excludedKeys() {return _excludedKeys;} + public List excludedSegments() {return _excludedSegments;} + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _ruleBasedSegment.hashCode(); + result = 31 * result + _parsedCondition.hashCode(); + result = 31 * result + (_trafficTypeName == null ? 0 : _trafficTypeName.hashCode()); + result = 31 * result + (int)(_changeNumber ^ (_changeNumber >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof ParsedRuleBasedSegment)) return false; + + ParsedRuleBasedSegment other = (ParsedRuleBasedSegment) obj; + + return _ruleBasedSegment.equals(other._ruleBasedSegment) + && _parsedCondition.equals(other._parsedCondition) + && _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName) + && _changeNumber == other._changeNumber; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("name:"); + bldr.append(_ruleBasedSegment); + bldr.append(", parsedConditions:"); + bldr.append(_parsedCondition); + bldr.append(", trafficTypeName:"); + bldr.append(_trafficTypeName); + bldr.append(", changeNumber:"); + bldr.append(_changeNumber); + return bldr.toString(); + + } + + public Set getSegmentsNames() { + Set segmentNames = excludedSegments() + .stream() + .filter(ExcludedSegments::isStandard) + .map(ExcludedSegments::getSegmentName) + .collect(Collectors.toSet()); + + segmentNames.addAll(parsedConditions().stream() + .flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream()) + .filter(ParsedRuleBasedSegment::isSegmentMatcher) + .map(ParsedRuleBasedSegment::asSegmentMatcherForEach) + .map(UserDefinedSegmentMatcher::getSegmentName) + .collect(Collectors.toSet())); + + return segmentNames; + } + + private static boolean isSegmentMatcher(AttributeMatcher attributeMatcher) { + return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof UserDefinedSegmentMatcher; + } + + private static UserDefinedSegmentMatcher asSegmentMatcherForEach(AttributeMatcher attributeMatcher) { + return (UserDefinedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate(); + } + +} diff --git a/client/src/main/java/io/split/engine/experiments/ParsedSplit.java b/client/src/main/java/io/split/engine/experiments/ParsedSplit.java index f28221a1..e202474f 100644 --- a/client/src/main/java/io/split/engine/experiments/ParsedSplit.java +++ b/client/src/main/java/io/split/engine/experiments/ParsedSplit.java @@ -1,9 +1,16 @@ package io.split.engine.experiments; import com.google.common.collect.ImmutableList; +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.PrerequisitesMatcher; +import io.split.engine.matchers.RuleBasedSegmentMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; /** * a value class representing an io.codigo.dtos.Experiment. Why are we not using @@ -26,6 +33,9 @@ public class ParsedSplit { private final int _trafficAllocationSeed; private final int _algo; private final Map _configurations; + private final HashSet _flagSets; + private final boolean _impressionsDisabled; + private PrerequisitesMatcher _prerequisitesMatcher; public static ParsedSplit createParsedSplitForTests( String feature, @@ -35,7 +45,10 @@ public static ParsedSplit createParsedSplitForTests( List matcherAndSplits, String trafficTypeName, long changeNumber, - int algo + int algo, + HashSet flagSets, + boolean impressionsDisabled, + PrerequisitesMatcher prerequisitesMatcher ) { return new ParsedSplit( feature, @@ -48,7 +61,10 @@ public static ParsedSplit createParsedSplitForTests( 100, seed, algo, - null + null, + flagSets, + impressionsDisabled, + prerequisitesMatcher ); } @@ -61,7 +77,10 @@ public static ParsedSplit createParsedSplitForTests( String trafficTypeName, long changeNumber, int algo, - Map configurations + Map configurations, + HashSet flagSets, + boolean impressionsDisabled, + PrerequisitesMatcher prerequisitesMatcher ) { return new ParsedSplit( feature, @@ -74,7 +93,10 @@ public static ParsedSplit createParsedSplitForTests( 100, seed, algo, - configurations + configurations, + flagSets, + impressionsDisabled, + prerequisitesMatcher ); } @@ -89,7 +111,10 @@ public ParsedSplit( int trafficAllocation, int trafficAllocationSeed, int algo, - Map configurations + Map configurations, + HashSet flagSets, + boolean impressionsDisabled, + PrerequisitesMatcher prerequisitesMatcher ) { _split = feature; _seed = seed; @@ -105,10 +130,11 @@ public ParsedSplit( _trafficAllocation = trafficAllocation; _trafficAllocationSeed = trafficAllocationSeed; _configurations = configurations; + _flagSets = flagSets; + _impressionsDisabled = impressionsDisabled; + _prerequisitesMatcher = prerequisitesMatcher; } - - public String feature() { return _split; } @@ -142,11 +168,19 @@ public List parsedConditions() { public long changeNumber() {return _changeNumber;} public int algo() {return _algo;} + public HashSet flagSets() { + return _flagSets; + } public Map configurations() { return _configurations; } + public boolean impressionsDisabled() { + return _impressionsDisabled; + } + public PrerequisitesMatcher prerequisitesMatcher() { return _prerequisitesMatcher; } + @Override public int hashCode() { int result = 17; @@ -159,6 +193,7 @@ public int hashCode() { result = 31 * result + (int)(_changeNumber ^ (_changeNumber >>> 32)); result = 31 * result + (_algo ^ (_algo >>> 32)); result = 31 * result + (_configurations == null? 0 : _configurations.hashCode()); + result = 31 * result + (_impressionsDisabled ? 1 : 0); return result; } @@ -169,16 +204,20 @@ public boolean equals(Object obj) { if (!(obj instanceof ParsedSplit)) return false; ParsedSplit other = (ParsedSplit) obj; + boolean trafficTypeCond = _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName); + boolean configCond = _configurations == null ? other._configurations == null : _configurations.equals(other._configurations); return _split.equals(other._split) && _seed == other._seed && _killed == other._killed && _defaultTreatment.equals(other._defaultTreatment) && _parsedCondition.equals(other._parsedCondition) - && _trafficTypeName == null ? other._trafficTypeName == null : _trafficTypeName.equals(other._trafficTypeName) + && trafficTypeCond && _changeNumber == other._changeNumber && _algo == other._algo - && _configurations == null ? other._configurations == null : _configurations.equals(other._configurations); + && configCond + && _impressionsDisabled == other._impressionsDisabled + && _prerequisitesMatcher == other._prerequisitesMatcher; } @Override @@ -202,7 +241,46 @@ public String toString() { bldr.append(_algo); bldr.append(", config:"); bldr.append(_configurations); + bldr.append(", impressionsDisabled:"); + bldr.append(_impressionsDisabled); + bldr.append(", prerequisites:"); + bldr.append(_prerequisitesMatcher); + return bldr.toString(); } + + public Set getSegmentsNames() { + return parsedConditions().stream() + .flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream()) + .filter(ParsedSplit::isSegmentMatcher) + .map(ParsedSplit::asSegmentMatcherForEach) + .map(UserDefinedSegmentMatcher::getSegmentName) + .collect(Collectors.toSet()); + } + + public Set getRuleBasedSegmentsNames() { + return parsedConditions().stream() + .flatMap(parsedCondition -> parsedCondition.matcher().attributeMatchers().stream()) + .filter(ParsedSplit::isRuleBasedSegmentMatcher) + .map(ParsedSplit::asRuleBasedSegmentMatcherForEach) + .map(RuleBasedSegmentMatcher::getSegmentName) + .collect(Collectors.toSet()); + } + + private static boolean isSegmentMatcher(AttributeMatcher attributeMatcher) { + return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof UserDefinedSegmentMatcher; + } + + private static UserDefinedSegmentMatcher asSegmentMatcherForEach(AttributeMatcher attributeMatcher) { + return (UserDefinedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate(); + } + + private static boolean isRuleBasedSegmentMatcher(AttributeMatcher attributeMatcher) { + return ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate() instanceof RuleBasedSegmentMatcher; + } + + private static RuleBasedSegmentMatcher asRuleBasedSegmentMatcherForEach(AttributeMatcher attributeMatcher) { + return (RuleBasedSegmentMatcher) ((AttributeMatcher.NegatableMatcher) attributeMatcher.matcher()).delegate(); + } } diff --git a/client/src/main/java/io/split/engine/experiments/ParserUtils.java b/client/src/main/java/io/split/engine/experiments/ParserUtils.java new file mode 100644 index 00000000..3b135512 --- /dev/null +++ b/client/src/main/java/io/split/engine/experiments/ParserUtils.java @@ -0,0 +1,204 @@ +package io.split.engine.experiments; + +import com.google.common.collect.Lists; +import io.split.client.dtos.MatcherType; +import io.split.client.dtos.Partition; +import io.split.client.dtos.MatcherGroup; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Matcher; +import io.split.engine.evaluator.Labels; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.AllKeysMatcher; +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; +import io.split.engine.matchers.EqualToMatcher; +import io.split.engine.matchers.GreaterThanOrEqualToMatcher; +import io.split.engine.matchers.LessThanOrEqualToMatcher; +import io.split.engine.matchers.BetweenMatcher; +import io.split.engine.matchers.DependencyMatcher; +import io.split.engine.matchers.BooleanMatcher; +import io.split.engine.matchers.EqualToSemverMatcher; +import io.split.engine.matchers.GreaterThanOrEqualToSemverMatcher; +import io.split.engine.matchers.LessThanOrEqualToSemverMatcher; +import io.split.engine.matchers.InListSemverMatcher; +import io.split.engine.matchers.BetweenSemverMatcher; +import io.split.engine.matchers.RuleBasedSegmentMatcher; +import io.split.engine.matchers.collections.ContainsAllOfSetMatcher; +import io.split.engine.matchers.collections.ContainsAnyOfSetMatcher; +import io.split.engine.matchers.collections.EqualToSetMatcher; +import io.split.engine.matchers.collections.PartOfSetMatcher; +import io.split.engine.matchers.strings.WhitelistMatcher; +import io.split.engine.matchers.strings.StartsWithAnyOfMatcher; +import io.split.engine.matchers.strings.EndsWithAnyOfMatcher; +import io.split.engine.matchers.strings.ContainsAnyOfMatcher; +import io.split.engine.matchers.strings.RegularExpressionMatcher; + +import java.util.List; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +public final class ParserUtils { + + private ParserUtils() { + throw new IllegalStateException("Utility class"); + } + + public static boolean checkUnsupportedMatcherExist(List matchers) { + MatcherType typeCheck = null; + for (Matcher matcher : matchers) { + typeCheck = null; + try { + typeCheck = matcher.matcherType; + } catch (NullPointerException e) { + // If the exception is caught, it means unsupported matcher + break; + } + } + return (typeCheck == null); + } + + public static ParsedCondition getTemplateCondition() { + List templatePartitions = Lists.newArrayList(); + Partition partition = new Partition(); + partition.treatment = "control"; + partition.size = 100; + templatePartitions.add(partition); + return new ParsedCondition( + ConditionType.ROLLOUT, + CombiningMatcher.of(new AllKeysMatcher()), + templatePartitions, + Labels.UNSUPPORTED_MATCHER); + } + + public static CombiningMatcher toMatcher(MatcherGroup matcherGroup) { + List matchers = matcherGroup.matchers; + checkArgument(!matchers.isEmpty()); + + List toCombine = Lists.newArrayList(); + + for (Matcher matcher : matchers) { + toCombine.add(toMatcher(matcher)); + } + + return new CombiningMatcher(matcherGroup.combiner, toCombine); + } + + + public static AttributeMatcher toMatcher(Matcher matcher) { + io.split.engine.matchers.Matcher delegate = null; + switch (matcher.matcherType) { + case ALL_KEYS: + delegate = new AllKeysMatcher(); + break; + case IN_SEGMENT: + checkNotNull(matcher.userDefinedSegmentMatcherData); + String segmentName = matcher.userDefinedSegmentMatcherData.segmentName; + delegate = new UserDefinedSegmentMatcher(segmentName); + break; + case WHITELIST: + checkNotNull(matcher.whitelistMatcherData); + delegate = new WhitelistMatcher(matcher.whitelistMatcherData.whitelist); + break; + case EQUAL_TO: + checkNotNull(matcher.unaryNumericMatcherData); + delegate = new EqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); + break; + case GREATER_THAN_OR_EQUAL_TO: + checkNotNull(matcher.unaryNumericMatcherData); + delegate = new GreaterThanOrEqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); + break; + case LESS_THAN_OR_EQUAL_TO: + checkNotNull(matcher.unaryNumericMatcherData); + delegate = new LessThanOrEqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); + break; + case BETWEEN: + checkNotNull(matcher.betweenMatcherData); + delegate = new BetweenMatcher(matcher.betweenMatcherData.start, matcher.betweenMatcherData.end, matcher.betweenMatcherData.dataType); + break; + case EQUAL_TO_SET: + checkNotNull(matcher.whitelistMatcherData); + delegate = new EqualToSetMatcher(matcher.whitelistMatcherData.whitelist); + break; + case PART_OF_SET: + checkNotNull(matcher.whitelistMatcherData); + delegate = new PartOfSetMatcher(matcher.whitelistMatcherData.whitelist); + break; + case CONTAINS_ALL_OF_SET: + checkNotNull(matcher.whitelistMatcherData); + delegate = new ContainsAllOfSetMatcher(matcher.whitelistMatcherData.whitelist); + break; + case CONTAINS_ANY_OF_SET: + checkNotNull(matcher.whitelistMatcherData); + delegate = new ContainsAnyOfSetMatcher(matcher.whitelistMatcherData.whitelist); + break; + case STARTS_WITH: + checkNotNull(matcher.whitelistMatcherData); + delegate = new StartsWithAnyOfMatcher(matcher.whitelistMatcherData.whitelist); + break; + case ENDS_WITH: + checkNotNull(matcher.whitelistMatcherData); + delegate = new EndsWithAnyOfMatcher(matcher.whitelistMatcherData.whitelist); + break; + case CONTAINS_STRING: + checkNotNull(matcher.whitelistMatcherData); + delegate = new ContainsAnyOfMatcher(matcher.whitelistMatcherData.whitelist); + break; + case MATCHES_STRING: + checkNotNull(matcher.stringMatcherData); + delegate = new RegularExpressionMatcher(matcher.stringMatcherData); + break; + case IN_SPLIT_TREATMENT: + checkNotNull(matcher.dependencyMatcherData, + "MatcherType is " + matcher.matcherType + + ". matcher.dependencyMatcherData() MUST NOT BE null"); + delegate = new DependencyMatcher(matcher.dependencyMatcherData.split, matcher.dependencyMatcherData.treatments); + break; + case EQUAL_TO_BOOLEAN: + checkNotNull(matcher.booleanMatcherData, + "MatcherType is " + matcher.matcherType + + ". matcher.booleanMatcherData() MUST NOT BE null"); + delegate = new BooleanMatcher(matcher.booleanMatcherData); + break; + case EQUAL_TO_SEMVER: + checkNotNull(matcher.stringMatcherData, "stringMatcherData is required for EQUAL_TO_SEMVER matcher type"); + delegate = new EqualToSemverMatcher(matcher.stringMatcherData); + break; + case GREATER_THAN_OR_EQUAL_TO_SEMVER: + checkNotNull(matcher.stringMatcherData, "stringMatcherData is required for GREATER_THAN_OR_EQUAL_TO_SEMVER matcher type"); + delegate = new GreaterThanOrEqualToSemverMatcher(matcher.stringMatcherData); + break; + case LESS_THAN_OR_EQUAL_TO_SEMVER: + checkNotNull(matcher.stringMatcherData, "stringMatcherData is required for LESS_THAN_OR_EQUAL_SEMVER matcher type"); + delegate = new LessThanOrEqualToSemverMatcher(matcher.stringMatcherData); + break; + case IN_LIST_SEMVER: + checkNotNull(matcher.whitelistMatcherData, "whitelistMatcherData is required for IN_LIST_SEMVER matcher type"); + delegate = new InListSemverMatcher(matcher.whitelistMatcherData.whitelist); + break; + case BETWEEN_SEMVER: + checkNotNull(matcher.betweenStringMatcherData, "betweenStringMatcherData is required for BETWEEN_SEMVER matcher type"); + delegate = new BetweenSemverMatcher(matcher.betweenStringMatcherData.start, matcher.betweenStringMatcherData.end); + break; + case IN_RULE_BASED_SEGMENT: + checkNotNull(matcher.userDefinedSegmentMatcherData); + String ruleBasedSegmentName = matcher.userDefinedSegmentMatcherData.segmentName; + delegate = new RuleBasedSegmentMatcher(ruleBasedSegmentName); + break; + default: + throw new IllegalArgumentException("Unknown matcher type: " + matcher.matcherType); + } + + checkNotNull(delegate, "We were not able to create a matcher for: " + matcher.matcherType); + + String attribute = null; + if (matcher.keySelector != null && matcher.keySelector.attribute != null) { + attribute = matcher.keySelector.attribute; + } + + boolean negate = matcher.negate; + + + return new AttributeMatcher(attribute, delegate, negate); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/experiments/RuleBasedSegmentParser.java b/client/src/main/java/io/split/engine/experiments/RuleBasedSegmentParser.java new file mode 100644 index 00000000..b67c5e35 --- /dev/null +++ b/client/src/main/java/io/split/engine/experiments/RuleBasedSegmentParser.java @@ -0,0 +1,54 @@ +package io.split.engine.experiments; + +import com.google.common.collect.Lists; +import io.split.client.dtos.Condition; +import io.split.client.dtos.RuleBasedSegment; +import io.split.engine.matchers.CombiningMatcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; + +import static io.split.engine.experiments.ParserUtils.checkUnsupportedMatcherExist; +import static io.split.engine.experiments.ParserUtils.getTemplateCondition; +import static io.split.engine.experiments.ParserUtils.toMatcher; + +public final class RuleBasedSegmentParser { + + private static final Logger _log = LoggerFactory.getLogger(RuleBasedSegmentParser.class); + + public RuleBasedSegmentParser() { + } + + public ParsedRuleBasedSegment parse(RuleBasedSegment ruleBasedSegment) { + try { + return parseWithoutExceptionHandling(ruleBasedSegment); + } catch (Throwable t) { + _log.error("Could not parse rule based segment: " + ruleBasedSegment, t); + return null; + } + } + + private ParsedRuleBasedSegment parseWithoutExceptionHandling(RuleBasedSegment ruleBasedSegment) { + List parsedConditionList = Lists.newArrayList(); + for (Condition condition : ruleBasedSegment.conditions) { + if (checkUnsupportedMatcherExist(condition.matcherGroup.matchers)) { + _log.error("Unsupported matcher type found for rule based segment: " + ruleBasedSegment.name + + " , will revert to default template matcher."); + parsedConditionList.clear(); + parsedConditionList.add(getTemplateCondition()); + break; + } + CombiningMatcher matcher = toMatcher(condition.matcherGroup); + parsedConditionList.add(new ParsedCondition(condition.conditionType, matcher, null, condition.label)); + } + + return new ParsedRuleBasedSegment( + ruleBasedSegment.name, + parsedConditionList, + ruleBasedSegment.trafficTypeName, + ruleBasedSegment.changeNumber, + ruleBasedSegment.excluded.keys, + ruleBasedSegment.excluded.segments); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java b/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java index 63298a5e..da6e185f 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java +++ b/client/src/main/java/io/split/engine/experiments/SplitChangeFetcher.java @@ -1,6 +1,7 @@ package io.split.engine.experiments; import io.split.client.dtos.SplitChange; +import io.split.engine.common.FetchOptions; /** * Created by adilaijaz on 5/11/15. @@ -31,5 +32,5 @@ public interface SplitChangeFetcher { * @return SegmentChange * @throws java.lang.RuntimeException if there was a problem computing split changes */ - SplitChange fetch(long since, boolean addCacheHeader); + SplitChange fetch(long since, long sinceRBS, FetchOptions options); } diff --git a/client/src/main/java/io/split/engine/experiments/SplitFetcher.java b/client/src/main/java/io/split/engine/experiments/SplitFetcher.java index 4266659b..688312c6 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitFetcher.java +++ b/client/src/main/java/io/split/engine/experiments/SplitFetcher.java @@ -1,5 +1,7 @@ package io.split.engine.experiments; +import io.split.engine.common.FetchOptions; + /** * Created by adilaijaz on 5/8/15. */ @@ -8,11 +10,5 @@ public interface SplitFetcher extends Runnable { * Forces a sync of splits, outside of any scheduled * syncs. This method MUST NOT throw any exceptions. */ - void forceRefresh(boolean addCacheHeader); - - /** - * Forces a sync of ALL splits, outside of any scheduled - * syncs. This method MUST NOT throw any exceptions. - */ - void fetchAll(boolean addCacheHeader); + FetchResult forceRefresh(FetchOptions options); } diff --git a/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java b/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java index 51000115..15ad9e40 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java +++ b/client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java @@ -1,14 +1,31 @@ package io.split.engine.experiments; -import io.split.client.dtos.Split; import io.split.client.dtos.SplitChange; -import io.split.client.dtos.Status; -import io.split.engine.SDKReadinessGates; -import io.split.cache.SplitCache; +import io.split.client.exceptions.UriTooLongException; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.utils.FeatureFlagsToUpdate; +import io.split.client.utils.RuleBasedSegmentsToUpdate; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.engine.common.FetchOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.FeatureFlagProcessor.processFeatureFlagChanges; +import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; +import static io.split.client.utils.Utils.checkExitConditions; /** * An ExperimentFetcher that refreshes experiment definitions periodically. @@ -21,9 +38,13 @@ public class SplitFetcherImp implements SplitFetcher { private final SplitParser _parser; private final SplitChangeFetcher _splitChangeFetcher; - private final SplitCache _splitCache; - private final SDKReadinessGates _gates; + private final SplitCacheProducer _splitCacheProducer; private final Object _lock = new Object(); + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final FlagSetsFilter _flagSetsFilter; + private final RuleBasedSegmentCacheProducer _ruleBasedSegmentCacheProducer; + private final RuleBasedSegmentParser _parserRBS; + private final SdkEventsNotifier _notifier; /** * Contains all the traffic types that are currently being used by the splits and also the count @@ -35,128 +56,141 @@ public class SplitFetcherImp implements SplitFetcher { * an ARCHIVED split is received, we know if we need to remove a traffic type from the multiset. */ - public SplitFetcherImp(SplitChangeFetcher splitChangeFetcher, SplitParser parser, SDKReadinessGates gates, SplitCache splitCache) { + public SplitFetcherImp(SplitChangeFetcher splitChangeFetcher, SplitParser parser, SplitCacheProducer splitCacheProducer, + TelemetryRuntimeProducer telemetryRuntimeProducer, FlagSetsFilter flagSetsFilter, + RuleBasedSegmentParser parserRBS, RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer) { + this(splitChangeFetcher, parser, splitCacheProducer, telemetryRuntimeProducer, flagSetsFilter, parserRBS, + ruleBasedSegmentCacheProducer, SdkEventsNotifier.NOOP); + } + + public SplitFetcherImp(SplitChangeFetcher splitChangeFetcher, SplitParser parser, SplitCacheProducer splitCacheProducer, + TelemetryRuntimeProducer telemetryRuntimeProducer, FlagSetsFilter flagSetsFilter, + RuleBasedSegmentParser parserRBS, RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer, + SdkEventsNotifier notifier) { + _notifier = checkNotNull(notifier); _splitChangeFetcher = checkNotNull(splitChangeFetcher); _parser = checkNotNull(parser); - _gates = checkNotNull(gates); - _splitCache = checkNotNull(splitCache); + _parserRBS = checkNotNull(parserRBS); + _splitCacheProducer = checkNotNull(splitCacheProducer); + _ruleBasedSegmentCacheProducer = checkNotNull(ruleBasedSegmentCacheProducer); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _flagSetsFilter = flagSetsFilter; } @Override - public void forceRefresh(boolean addCacheHeader) { - _log.debug("Force Refresh splits starting ..."); + public FetchResult forceRefresh(FetchOptions options) { + _log.debug("Force Refresh feature flags starting ..."); + final long INITIAL_CN = _splitCacheProducer.getChangeNumber(); + final long RBS_INITIAL_CN = _ruleBasedSegmentCacheProducer.getChangeNumber(); + Set segments = new HashSet<>(); try { while (true) { - long start = _splitCache.getChangeNumber(); - runWithoutExceptionHandling(addCacheHeader); - long end = _splitCache.getChangeNumber(); + long start = _splitCacheProducer.getChangeNumber(); + long startRBS = _ruleBasedSegmentCacheProducer.getChangeNumber(); + segments.addAll(runWithoutExceptionHandling(options)); + long end = _splitCacheProducer.getChangeNumber(); + long endRBS = _ruleBasedSegmentCacheProducer.getChangeNumber(); + + // If the previous execution was the first one, clear the `cdnBypass` flag + // for the next fetches. (This will clear a local copy of the fetch options, + // not the original object that was passed to this method). + FetchOptions.Builder optionsBuilder = new FetchOptions.Builder(options); + if (INITIAL_CN == start) { + optionsBuilder.targetChangeNumber(FetchOptions.DEFAULT_TARGET_CHANGENUMBER); + } - if (start >= end) { - break; + if (RBS_INITIAL_CN == startRBS) { + optionsBuilder.targetChangeNumberRBS(FetchOptions.DEFAULT_TARGET_CHANGENUMBER); + } + + options = optionsBuilder.build(); + + if (start >= end && startRBS >= endRBS) { + return new FetchResult(true, false, segments); } } + } catch (UriTooLongException u) { + return new FetchResult(false, false, new HashSet<>()); } catch (InterruptedException e) { _log.warn("Interrupting split fetcher task"); Thread.currentThread().interrupt(); - } catch (Throwable t) { - _log.error("RefreshableSplitFetcher failed: " + t.getMessage()); + return new FetchResult(false, true, new HashSet<>()); + } catch (Exception e) { + _log.error("SplitFetcherImp failed: " + e.getMessage()); + if (_log.isDebugEnabled()) { + _log.debug("Reason:", e); + } + return new FetchResult(false, true, new HashSet<>()); } } @Override public void run() { - this.fetchAll(false); + this.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(false).build()); } - private void runWithoutExceptionHandling(boolean addCacheHeader) throws InterruptedException { - SplitChange change = _splitChangeFetcher.fetch(_splitCache.getChangeNumber(), addCacheHeader); + private Set runWithoutExceptionHandling(FetchOptions options) throws InterruptedException, UriTooLongException { + SplitChange change = _splitChangeFetcher.fetch(_splitCacheProducer.getChangeNumber(), + _ruleBasedSegmentCacheProducer.getChangeNumber(), options); + Set segments = new HashSet<>(); if (change == null) { throw new IllegalStateException("SplitChange was null"); } - if (change.till == _splitCache.getChangeNumber()) { - // no change. - return; + if (change.clearCache) { + _splitCacheProducer.clear(); + _ruleBasedSegmentCacheProducer.clear(); } - if (change.since != _splitCache.getChangeNumber() || change.till < _splitCache.getChangeNumber()) { - // some other thread may have updated the shared state. exit - return; + if (checkExitConditions(change.featureFlags, _splitCacheProducer.getChangeNumber()) || + checkExitConditions(change.ruleBasedSegments, _ruleBasedSegmentCacheProducer.getChangeNumber())) { + return segments; } - if (change.splits.isEmpty()) { - // there are no changes. weird! - _splitCache.setChangeNumber(change.till); - return; + if (change.featureFlags.d.isEmpty()) { + _splitCacheProducer.setChangeNumber(change.featureFlags.t); } + if (change.ruleBasedSegments.d.isEmpty()) { + _ruleBasedSegmentCacheProducer.setChangeNumber(change.ruleBasedSegments.t); + } + + if (change.featureFlags.d.isEmpty() && change.ruleBasedSegments.d.isEmpty()) { + return segments; + } + + synchronized (_lock) { // check state one more time. - if (change.since != _splitCache.getChangeNumber() - || change.till < _splitCache.getChangeNumber()) { + if (checkExitConditions(change.featureFlags, _splitCacheProducer.getChangeNumber()) || + checkExitConditions(change.ruleBasedSegments, _ruleBasedSegmentCacheProducer.getChangeNumber())) { // some other thread may have updated the shared state. exit - return; - } - - for (Split split : change.splits) { - if (Thread.currentThread().isInterrupted()) { - throw new InterruptedException(); - } - - if (split.status != Status.ACTIVE) { - // archive. - _splitCache.remove(split.name); - continue; - } - - ParsedSplit parsedSplit = _parser.parse(split); - if (parsedSplit == null) { - _log.info("We could not parse the experiment definition for: " + split.name + " so we are removing it completely to be careful"); - - _splitCache.remove(split.name); - _log.debug("Deleted feature: " + split.name); - - continue; - } - - // If the split already exists, this is either an update, or the split has been - // deleted and recreated (possibly with a different traffic type). - // If it's an update, the traffic type should NOT be increased. - // If it's deleted & recreated, the old one should be decreased and the new one increased. - // To handle both cases, we simply delete the old one if the split is present. - // The new one is always increased. - ParsedSplit current = _splitCache.get(split.name); - if (current != null) { - _splitCache.remove(split.name); - } - - _splitCache.put(parsedSplit); - _log.debug("Updated feature: " + parsedSplit.feature()); + return segments; } - - _splitCache.setChangeNumber(change.till); - } - } - @Override - public void fetchAll(boolean addCacheHeader) { - _log.debug("Fetch splits starting ..."); - long start = _splitCache.getChangeNumber(); - try { - runWithoutExceptionHandling(addCacheHeader); - _gates.splitsAreReady(); - } catch (InterruptedException e) { - _log.warn("Interrupting split fetcher task"); - Thread.currentThread().interrupt(); - } catch (Throwable t) { - _log.error("RefreshableSplitFetcher failed: " + t.getMessage()); - if (_log.isDebugEnabled()) { - _log.debug("Reason:", t); - } - } finally { - if (_log.isDebugEnabled()) { - _log.debug("split fetch before: " + start + ", after: " + _splitCache.getChangeNumber()); + FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(_parser, change.featureFlags.d, _flagSetsFilter); + segments = featureFlagsToUpdate.getSegments(); + _splitCacheProducer.update(featureFlagsToUpdate.getToAdd(), featureFlagsToUpdate.getToRemove(), change.featureFlags.t); + + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(_parserRBS, + change.ruleBasedSegments.d); + segments.addAll(ruleBasedSegmentsToUpdate.getSegments()); + _ruleBasedSegmentCacheProducer.update(ruleBasedSegmentsToUpdate.getToAdd(), + ruleBasedSegmentsToUpdate.getToRemove(), change.ruleBasedSegments.t); + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.SPLITS, System.currentTimeMillis()); + + Set flagNames = new HashSet<>(featureFlagsToUpdate.getToRemove()); + featureFlagsToUpdate.getToAdd().forEach(p -> flagNames.add(p.feature())); + boolean rbsChanged = !ruleBasedSegmentsToUpdate.getToAdd().isEmpty() + || !ruleBasedSegmentsToUpdate.getToRemove().isEmpty(); + if (!flagNames.isEmpty()) { + _notifier.notify(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, flagNames)); + } else if (rbsChanged) { + _notifier.notify(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED, + SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, Collections.emptySet())); } } + + return segments; } } diff --git a/client/src/main/java/io/split/engine/experiments/SplitParser.java b/client/src/main/java/io/split/engine/experiments/SplitParser.java index e5829209..5771c9ae 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitParser.java +++ b/client/src/main/java/io/split/engine/experiments/SplitParser.java @@ -1,40 +1,21 @@ package io.split.engine.experiments; import com.google.common.collect.Lists; -import io.split.cache.SegmentCache; + import io.split.client.dtos.Condition; -import io.split.client.dtos.Matcher; -import io.split.client.dtos.MatcherGroup; import io.split.client.dtos.Partition; import io.split.client.dtos.Split; -import io.split.client.dtos.Status; -import io.split.engine.matchers.AllKeysMatcher; -import io.split.engine.matchers.AttributeMatcher; -import io.split.engine.matchers.BetweenMatcher; -import io.split.engine.matchers.BooleanMatcher; import io.split.engine.matchers.CombiningMatcher; -import io.split.engine.matchers.DependencyMatcher; -import io.split.engine.matchers.EqualToMatcher; -import io.split.engine.matchers.GreaterThanOrEqualToMatcher; -import io.split.engine.matchers.LessThanOrEqualToMatcher; -import io.split.engine.matchers.UserDefinedSegmentMatcher; -import io.split.engine.matchers.collections.ContainsAllOfSetMatcher; -import io.split.engine.matchers.collections.ContainsAnyOfSetMatcher; -import io.split.engine.matchers.collections.EqualToSetMatcher; -import io.split.engine.matchers.collections.PartOfSetMatcher; -import io.split.engine.matchers.strings.ContainsAnyOfMatcher; -import io.split.engine.matchers.strings.EndsWithAnyOfMatcher; -import io.split.engine.matchers.strings.RegularExpressionMatcher; -import io.split.engine.matchers.strings.StartsWithAnyOfMatcher; -import io.split.engine.matchers.strings.WhitelistMatcher; -import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.engine.matchers.PrerequisitesMatcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; +import java.util.Objects; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.engine.experiments.ParserUtils.checkUnsupportedMatcherExist; +import static io.split.engine.experiments.ParserUtils.getTemplateCondition; +import static io.split.engine.experiments.ParserUtils.toMatcher; /** * Converts io.codigo.dtos.Experiment to io.codigo.engine.splits.ParsedExperiment. @@ -43,16 +24,9 @@ */ public final class SplitParser { - public static final int CONDITIONS_UPPER_LIMIT = 50; private static final Logger _log = LoggerFactory.getLogger(SplitParser.class); - private final SegmentSynchronizationTask _segmentSynchronizationTask; - private final SegmentCache _segmentCache; - - public SplitParser(SegmentSynchronizationTask segmentSynchronizationTaskImp, - SegmentCache segmentCache) { - _segmentSynchronizationTask = checkNotNull(segmentSynchronizationTaskImp); - _segmentCache = checkNotNull(segmentCache); + public SplitParser() { } public ParsedSplit parse(Split split) { @@ -65,133 +39,37 @@ public ParsedSplit parse(Split split) { } private ParsedSplit parseWithoutExceptionHandling(Split split) { - if (split.status != Status.ACTIVE) { - return null; - } - - if (split.conditions.size() > CONDITIONS_UPPER_LIMIT) { - _log.warn(String.format("Dropping Split name=%s due to large number of conditions(%d)", - split.name, split.conditions.size())); - return null; - } - List parsedConditionList = Lists.newArrayList(); - + if (Objects.isNull(split.impressionsDisabled)) { + _log.debug("impressionsDisabled field not detected for Feature flag `" + split.name + "`, setting it to `false`."); + split.impressionsDisabled = false; + } for (Condition condition : split.conditions) { List partitions = condition.partitions; + if (checkUnsupportedMatcherExist(condition.matcherGroup.matchers)) { + _log.error("Unsupported matcher type found for feature flag: " + split.name + " , will revert to default template matcher."); + parsedConditionList.clear(); + parsedConditionList.add(getTemplateCondition()); + break; + } CombiningMatcher matcher = toMatcher(condition.matcherGroup); parsedConditionList.add(new ParsedCondition(condition.conditionType, matcher, partitions, condition.label)); } - return new ParsedSplit(split.name, split.seed, split.killed, split.defaultTreatment, parsedConditionList, split.trafficTypeName, split.changeNumber, split.trafficAllocation, split.trafficAllocationSeed, split.algo, split.configurations); - } - - private CombiningMatcher toMatcher(MatcherGroup matcherGroup) { - List matchers = matcherGroup.matchers; - checkArgument(!matchers.isEmpty()); - - List toCombine = Lists.newArrayList(); - - for (io.split.client.dtos.Matcher matcher : matchers) { - toCombine.add(toMatcher(matcher)); - } - - return new CombiningMatcher(matcherGroup.combiner, toCombine); - } - - - private AttributeMatcher toMatcher(Matcher matcher) { - io.split.engine.matchers.Matcher delegate = null; - switch (matcher.matcherType) { - case ALL_KEYS: - delegate = new AllKeysMatcher(); - break; - case IN_SEGMENT: - checkNotNull(matcher.userDefinedSegmentMatcherData); - String segmentName = matcher.userDefinedSegmentMatcherData.segmentName; - _segmentSynchronizationTask.initializeSegment(segmentName); - delegate = new UserDefinedSegmentMatcher(_segmentCache, segmentName); - break; - case WHITELIST: - checkNotNull(matcher.whitelistMatcherData); - delegate = new WhitelistMatcher(matcher.whitelistMatcherData.whitelist); - break; - case EQUAL_TO: - checkNotNull(matcher.unaryNumericMatcherData); - delegate = new EqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); - break; - case GREATER_THAN_OR_EQUAL_TO: - checkNotNull(matcher.unaryNumericMatcherData); - delegate = new GreaterThanOrEqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); - break; - case LESS_THAN_OR_EQUAL_TO: - checkNotNull(matcher.unaryNumericMatcherData); - delegate = new LessThanOrEqualToMatcher(matcher.unaryNumericMatcherData.value, matcher.unaryNumericMatcherData.dataType); - break; - case BETWEEN: - checkNotNull(matcher.betweenMatcherData); - delegate = new BetweenMatcher(matcher.betweenMatcherData.start, matcher.betweenMatcherData.end, matcher.betweenMatcherData.dataType); - break; - case EQUAL_TO_SET: - checkNotNull(matcher.whitelistMatcherData); - delegate = new EqualToSetMatcher(matcher.whitelistMatcherData.whitelist); - break; - case PART_OF_SET: - checkNotNull(matcher.whitelistMatcherData); - delegate = new PartOfSetMatcher(matcher.whitelistMatcherData.whitelist); - break; - case CONTAINS_ALL_OF_SET: - checkNotNull(matcher.whitelistMatcherData); - delegate = new ContainsAllOfSetMatcher(matcher.whitelistMatcherData.whitelist); - break; - case CONTAINS_ANY_OF_SET: - checkNotNull(matcher.whitelistMatcherData); - delegate = new ContainsAnyOfSetMatcher(matcher.whitelistMatcherData.whitelist); - break; - case STARTS_WITH: - checkNotNull(matcher.whitelistMatcherData); - delegate = new StartsWithAnyOfMatcher(matcher.whitelistMatcherData.whitelist); - break; - case ENDS_WITH: - checkNotNull(matcher.whitelistMatcherData); - delegate = new EndsWithAnyOfMatcher(matcher.whitelistMatcherData.whitelist); - break; - case CONTAINS_STRING: - checkNotNull(matcher.whitelistMatcherData); - delegate = new ContainsAnyOfMatcher(matcher.whitelistMatcherData.whitelist); - break; - case MATCHES_STRING: - checkNotNull(matcher.stringMatcherData); - delegate = new RegularExpressionMatcher(matcher.stringMatcherData); - break; - case IN_SPLIT_TREATMENT: - checkNotNull(matcher.dependencyMatcherData, - "MatcherType is " + matcher.matcherType - + ". matcher.dependencyMatcherData() MUST NOT BE null"); - delegate = new DependencyMatcher(matcher.dependencyMatcherData.split, matcher.dependencyMatcherData.treatments); - break; - case EQUAL_TO_BOOLEAN: - checkNotNull(matcher.booleanMatcherData, - "MatcherType is " + matcher.matcherType - + ". matcher.booleanMatcherData() MUST NOT BE null"); - delegate = new BooleanMatcher(matcher.booleanMatcherData); - break; - default: - throw new IllegalArgumentException("Unknown matcher type: " + matcher.matcherType); - } - - checkNotNull(delegate, "We were not able to create a matcher for: " + matcher.matcherType); - - String attribute = null; - if (matcher.keySelector != null && matcher.keySelector.attribute != null) { - attribute = matcher.keySelector.attribute; - } - - boolean negate = matcher.negate; - - - return new AttributeMatcher(attribute, delegate, negate); + return new ParsedSplit( + split.name, + split.seed, + split.killed, + split.defaultTreatment, + parsedConditionList, + split.trafficTypeName, + split.changeNumber, + split.trafficAllocation, + split.trafficAllocationSeed, + split.algo, + split.configurations, + split.sets, + split.impressionsDisabled, + new PrerequisitesMatcher(split.prerequisites)); } - - -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/experiments/SplitSynchronizationTask.java b/client/src/main/java/io/split/engine/experiments/SplitSynchronizationTask.java index c98b35b3..6f4f99ab 100644 --- a/client/src/main/java/io/split/engine/experiments/SplitSynchronizationTask.java +++ b/client/src/main/java/io/split/engine/experiments/SplitSynchronizationTask.java @@ -1,34 +1,34 @@ package io.split.engine.experiments; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.split.cache.SplitCache; +import io.split.storages.SplitCacheProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; import java.util.List; -import java.util.concurrent.Executors; + import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.SplitExecutorFactory.buildSingleThreadScheduledExecutor; /** * Provides an instance of RefreshableExperimentFetcher that is guaranteed to be a singleton. * * @author adil */ -public class SplitSynchronizationTask implements Closeable { +public class SplitSynchronizationTask implements SyncTask, Closeable { private static final Logger _log = LoggerFactory.getLogger(SplitSynchronizationTask.class); private final AtomicReference _splitFetcher = new AtomicReference<>(); - private final AtomicReference _splitCache = new AtomicReference(); + private final AtomicReference _splitCacheProducer = new AtomicReference(); private final AtomicReference _executorService = new AtomicReference<>(); private final AtomicLong _refreshEveryNSeconds; private final ScheduledExecutorService _scheduledExecutorService; @@ -36,30 +36,26 @@ public class SplitSynchronizationTask implements Closeable { private ScheduledFuture _scheduledFuture; - public SplitSynchronizationTask(SplitFetcher splitFetcher, SplitCache splitCache, long refreshEveryNSeconds) { + public SplitSynchronizationTask(SplitFetcher splitFetcher, SplitCacheProducer splitCachesplitCacheProducer, long refreshEveryNSeconds, + ThreadFactory threadFactory) { _splitFetcher.set(checkNotNull(splitFetcher)); - _splitCache.set(checkNotNull(splitCache)); + _splitCacheProducer.set(checkNotNull(splitCachesplitCacheProducer)); checkArgument(refreshEveryNSeconds >= 0L); _refreshEveryNSeconds = new AtomicLong(refreshEveryNSeconds); - ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("split-splitFetcher-%d") - .build(); - - _scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(threadFactory); + _scheduledExecutorService = buildSingleThreadScheduledExecutor(threadFactory, "split-splitFetcher-%d"); _executorService.set(_scheduledExecutorService); _running = new AtomicBoolean(); } - public void startPeriodicFetching() { + public void start() { if (_running.getAndSet(true)) { _log.debug("Splits PeriodicFetching is running..."); return; } - _log.debug("Starting PeriodicFetching Splits ..."); + _log.debug("Starting PeriodicFetching Feature flags ..."); _scheduledFuture = _scheduledExecutorService.scheduleWithFixedDelay(_splitFetcher.get(), 0L, _refreshEveryNSeconds.get(), TimeUnit.SECONDS); } @@ -70,7 +66,7 @@ public void stop() { } _scheduledFuture.cancel(false); - _log.debug("Stopped PeriodicFetching Splits ..."); + _log.debug("Stopped PeriodicFetching Feature flags ..."); } @Override @@ -80,7 +76,7 @@ public void close() { } if (_splitFetcher.get() != null) { - _splitCache.get().clear(); + _splitCacheProducer.get().clear(); } stop(); @@ -99,8 +95,13 @@ public void close() { } } catch (InterruptedException e) { // reset the interrupt. - _log.warn("Shutdown hook for split fetchers has been interrupted"); + _log.warn("Shutdown hook for feature flag fetchers has been interrupted"); Thread.currentThread().interrupt(); } } -} + + @Override + public boolean isRunning() { + return _running.get(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/experiments/SyncTask.java b/client/src/main/java/io/split/engine/experiments/SyncTask.java new file mode 100644 index 00000000..622eb58f --- /dev/null +++ b/client/src/main/java/io/split/engine/experiments/SyncTask.java @@ -0,0 +1,9 @@ +package io.split.engine.experiments; + +public interface SyncTask { + + void start(); + void stop(); + void close(); + boolean isRunning(); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/matchers/AllKeysMatcher.java b/client/src/main/java/io/split/engine/matchers/AllKeysMatcher.java index bad14245..790224ab 100644 --- a/client/src/main/java/io/split/engine/matchers/AllKeysMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/AllKeysMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -12,7 +12,7 @@ public final class AllKeysMatcher implements Matcher { @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/AttributeMatcher.java b/client/src/main/java/io/split/engine/matchers/AttributeMatcher.java index b4670509..92deb014 100644 --- a/client/src/main/java/io/split/engine/matchers/AttributeMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/AttributeMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; import java.util.Objects; @@ -27,9 +27,9 @@ public AttributeMatcher(String attribute, Matcher matcher, boolean negate) { _matcher = new NegatableMatcher(matcher, negate); } - public boolean match(String key, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(String key, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (_attribute == null) { - return _matcher.match(key, bucketingKey, attributes, evaluator); + return _matcher.match(key, bucketingKey, attributes, evaluationContext); } if (attributes == null) { @@ -95,8 +95,8 @@ public NegatableMatcher(Matcher matcher, boolean negate) { @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { - boolean result = _delegate.match(matchValue, bucketingKey, attributes, evaluator); + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + boolean result = _delegate.match(matchValue, bucketingKey, attributes, evaluationContext); return (_negate) ? !result : result; } diff --git a/client/src/main/java/io/split/engine/matchers/BetweenMatcher.java b/client/src/main/java/io/split/engine/matchers/BetweenMatcher.java index dd77d981..a0ccfc1b 100644 --- a/client/src/main/java/io/split/engine/matchers/BetweenMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/BetweenMatcher.java @@ -1,7 +1,7 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -36,7 +36,7 @@ public BetweenMatcher(long start, long end, DataType dataType) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { Long keyAsLong; if (_dataType == DataType.DATETIME) { diff --git a/client/src/main/java/io/split/engine/matchers/BetweenSemverMatcher.java b/client/src/main/java/io/split/engine/matchers/BetweenSemverMatcher.java new file mode 100644 index 00000000..326e2183 --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/BetweenSemverMatcher.java @@ -0,0 +1,58 @@ +package io.split.engine.matchers; + +import io.split.engine.evaluator.EvaluationContext; + +import java.util.Map; + +public class BetweenSemverMatcher implements Matcher { + + private final Semver _semverStart; + private final Semver _semverEnd; + + public BetweenSemverMatcher(String semverStart, String semverEnd) { + _semverStart = Semver.build(semverStart); + _semverEnd = Semver.build(semverEnd); + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String) || _semverStart == null || _semverEnd == null) { + return false; + } + Semver matchSemver = Semver.build(matchValue.toString()); + if (matchSemver == null) { + return false; + } + + return matchSemver.compare(_semverStart) >= 0 && matchSemver.compare(_semverEnd) <= 0; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("between semver "); + bldr.append(_semverStart.version()); + bldr.append(" and "); + bldr.append(_semverEnd.version()); + return bldr.toString(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _semverStart.hashCode() + _semverEnd.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof BetweenSemverMatcher)) return false; + + BetweenSemverMatcher other = (BetweenSemverMatcher) obj; + + return _semverStart == other._semverStart && _semverEnd == other._semverEnd; + } + +} diff --git a/client/src/main/java/io/split/engine/matchers/BooleanMatcher.java b/client/src/main/java/io/split/engine/matchers/BooleanMatcher.java index 28b3783a..79d5a303 100644 --- a/client/src/main/java/io/split/engine/matchers/BooleanMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/BooleanMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -14,7 +14,7 @@ public BooleanMatcher(boolean booleanValue) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/CombiningMatcher.java b/client/src/main/java/io/split/engine/matchers/CombiningMatcher.java index 52a7d087..4097ef85 100644 --- a/client/src/main/java/io/split/engine/matchers/CombiningMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/CombiningMatcher.java @@ -2,9 +2,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import io.split.client.SplitClientImpl; import io.split.client.dtos.MatcherCombiner; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.List; import java.util.Map; @@ -39,24 +38,24 @@ public CombiningMatcher(MatcherCombiner combiner, List delegat checkArgument(_delegates.size() > 0); } - public boolean match(String key, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(String key, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (_delegates.isEmpty()) { return false; } switch (_combiner) { case AND: - return and(key, bucketingKey, attributes, evaluator); + return and(key, bucketingKey, attributes, evaluationContext); default: throw new IllegalArgumentException("Unknown combiner: " + _combiner); } } - private boolean and(String key, String bucketingKey, Map attributes, Evaluator evaluator) { + private boolean and(String key, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { boolean result = true; for (AttributeMatcher delegate : _delegates) { - result &= (delegate.match(key, bucketingKey, attributes, evaluator)); + result &= (delegate.match(key, bucketingKey, attributes, evaluationContext)); } return result; } diff --git a/client/src/main/java/io/split/engine/matchers/DependencyMatcher.java b/client/src/main/java/io/split/engine/matchers/DependencyMatcher.java index 5c6b3f2a..a3c3c464 100644 --- a/client/src/main/java/io/split/engine/matchers/DependencyMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/DependencyMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.List; import java.util.Map; @@ -10,16 +10,16 @@ * Supports the logic: if user is in split "feature" treatments ["on","off"] */ public class DependencyMatcher implements Matcher { - private String _split; + private String _featureFlag; private List _treatments; - public DependencyMatcher(String split, List treatments) { - _split = split; + public DependencyMatcher(String featureFlag, List treatments) { + _featureFlag = featureFlag; _treatments = treatments; } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } @@ -28,7 +28,7 @@ public boolean match(Object matchValue, String bucketingKey, Map return false; } - String result = evaluator.evaluateFeature((String) matchValue, bucketingKey, _split, attributes).treatment; + String result = evaluationContext.getEvaluator().evaluateFeature((String) matchValue, bucketingKey, _featureFlag, attributes).treatment; return _treatments.contains(result); } @@ -37,7 +37,7 @@ public boolean match(Object matchValue, String bucketingKey, Map public String toString() { StringBuilder bldr = new StringBuilder(); bldr.append("in split \""); - bldr.append(this._split); + bldr.append(this._featureFlag); bldr.append("\" treatment "); bldr.append(this._treatments); return bldr.toString(); @@ -50,13 +50,13 @@ public boolean equals(Object o) { DependencyMatcher that = (DependencyMatcher) o; - if (!Objects.equals(_split, that._split)) return false; + if (!Objects.equals(_featureFlag, that._featureFlag)) return false; return Objects.equals(_treatments, that._treatments); } @Override public int hashCode() { - int result = _split != null ? _split.hashCode() : 0; + int result = _featureFlag != null ? _featureFlag.hashCode() : 0; result = 31 * result + (_treatments != null ? _treatments.hashCode() : 0); return result; } diff --git a/client/src/main/java/io/split/engine/matchers/EqualToMatcher.java b/client/src/main/java/io/split/engine/matchers/EqualToMatcher.java index c2d853a2..9a1e32f3 100644 --- a/client/src/main/java/io/split/engine/matchers/EqualToMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/EqualToMatcher.java @@ -1,7 +1,7 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -29,7 +29,7 @@ public EqualToMatcher(long compareTo, DataType dataType) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { Long keyAsLong; if (_dataType == DataType.DATETIME) { diff --git a/client/src/main/java/io/split/engine/matchers/EqualToSemverMatcher.java b/client/src/main/java/io/split/engine/matchers/EqualToSemverMatcher.java new file mode 100644 index 00000000..64d9135d --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/EqualToSemverMatcher.java @@ -0,0 +1,54 @@ +package io.split.engine.matchers; + +import io.split.engine.evaluator.EvaluationContext; + +import java.util.Map; + +public class EqualToSemverMatcher implements Matcher { + + private final Semver _semVer; + + public EqualToSemverMatcher(String semVer) { + _semVer = Semver.build(semVer); + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String) || _semVer == null) { + return false; + } + Semver matchSemver = Semver.build(matchValue.toString()); + if (matchSemver == null) { + return false; + } + + return matchSemver.version().equals(_semVer.version()); + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("== semver "); + bldr.append(_semVer.version()); + return bldr.toString(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _semVer.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof EqualToSemverMatcher)) return false; + + EqualToSemverMatcher other = (EqualToSemverMatcher) obj; + + return _semVer == other._semVer; + } + +} diff --git a/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToMatcher.java b/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToMatcher.java index 7804fbe3..1b83dc2c 100644 --- a/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToMatcher.java @@ -1,7 +1,7 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -29,7 +29,7 @@ public GreaterThanOrEqualToMatcher(long compareTo, DataType dataType) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { Long keyAsLong; if (_dataType == DataType.DATETIME) { diff --git a/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcher.java b/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcher.java new file mode 100644 index 00000000..ffc714cc --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcher.java @@ -0,0 +1,54 @@ +package io.split.engine.matchers; + +import io.split.engine.evaluator.EvaluationContext; + +import java.util.Map; + +public class GreaterThanOrEqualToSemverMatcher implements Matcher { + + private final Semver _semVer; + + public GreaterThanOrEqualToSemverMatcher(String semVer) { + _semVer = Semver.build(semVer); + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String)|| _semVer == null) { + return false; + } + Semver matchSemver = Semver.build(matchValue.toString()); + if (matchSemver == null) { + return false; + } + + return matchSemver.compare(_semVer) >= 0; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append(">= semver "); + bldr.append(_semVer.version()); + return bldr.toString(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _semVer.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof GreaterThanOrEqualToSemverMatcher)) return false; + + GreaterThanOrEqualToSemverMatcher other = (GreaterThanOrEqualToSemverMatcher) obj; + + return _semVer == other._semVer; + } + +} diff --git a/client/src/main/java/io/split/engine/matchers/InListSemverMatcher.java b/client/src/main/java/io/split/engine/matchers/InListSemverMatcher.java new file mode 100644 index 00000000..69fd1ea4 --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/InListSemverMatcher.java @@ -0,0 +1,77 @@ +package io.split.engine.matchers; + +import io.split.engine.evaluator.EvaluationContext; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class InListSemverMatcher implements Matcher { + + private final Set _semverlist = new HashSet<>(); + + public InListSemverMatcher(Collection whitelist) { + for (String item : whitelist) { + Semver semver = Semver.build(item); + if (semver == null) continue; + + _semverlist.add(semver); + } + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String) || _semverlist.isEmpty()) { + return false; + } + Semver matchSemver = Semver.build(matchValue.toString()); + if (matchSemver == null) { + return false; + } + + for (Semver semverItem : _semverlist) { + if (semverItem.version().equals(matchSemver.version())) return true; + } + return false; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("in semver list ["); + boolean first = true; + + for (Semver item : _semverlist) { + if (!first) { + bldr.append(','); + } + bldr.append('"'); + bldr.append(item.version()); + bldr.append('"'); + first = false; + } + + bldr.append("]"); + return bldr.toString(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _semverlist.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof InListSemverMatcher)) return false; + + InListSemverMatcher other = (InListSemverMatcher) obj; + + return _semverlist == other._semverlist; + } + +} diff --git a/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToMatcher.java b/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToMatcher.java index 5afa35dc..24a74aab 100644 --- a/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToMatcher.java @@ -1,7 +1,7 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -28,7 +28,7 @@ public LessThanOrEqualToMatcher(long compareTo, DataType dataType) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { Long keyAsLong; if (_dataType == DataType.DATETIME) { diff --git a/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcher.java b/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcher.java new file mode 100644 index 00000000..dd05f8c4 --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcher.java @@ -0,0 +1,54 @@ +package io.split.engine.matchers; + +import io.split.engine.evaluator.EvaluationContext; + +import java.util.Map; + +public class LessThanOrEqualToSemverMatcher implements Matcher { + + private final Semver _semVer; + + public LessThanOrEqualToSemverMatcher(String semVer) { + _semVer = Semver.build(semVer); + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String) || _semVer == null) { + return false; + } + Semver matchSemver = Semver.build(matchValue.toString()); + if (matchSemver == null) { + return false; + } + + return matchSemver.compare(_semVer) <= 0; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("<= semver "); + bldr.append(_semVer.version()); + return bldr.toString(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _semVer.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof LessThanOrEqualToSemverMatcher)) return false; + + LessThanOrEqualToSemverMatcher other = (LessThanOrEqualToSemverMatcher) obj; + + return _semVer == other._semVer; + } + +} diff --git a/client/src/main/java/io/split/engine/matchers/Matcher.java b/client/src/main/java/io/split/engine/matchers/Matcher.java index 3acabb7b..ecdee1e7 100644 --- a/client/src/main/java/io/split/engine/matchers/Matcher.java +++ b/client/src/main/java/io/split/engine/matchers/Matcher.java @@ -1,9 +1,9 @@ package io.split.engine.matchers; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; public interface Matcher { - boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator); + boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext); } diff --git a/client/src/main/java/io/split/engine/matchers/PrerequisitesMatcher.java b/client/src/main/java/io/split/engine/matchers/PrerequisitesMatcher.java new file mode 100644 index 00000000..12278449 --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/PrerequisitesMatcher.java @@ -0,0 +1,71 @@ +package io.split.engine.matchers; + +import io.split.client.dtos.Prerequisites; +import io.split.engine.evaluator.EvaluationContext; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +public class PrerequisitesMatcher implements Matcher { + private List _prerequisites; + + public PrerequisitesMatcher(List prerequisites) { + _prerequisites = prerequisites; + } + + public List getPrerequisites() { return _prerequisites; } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (matchValue == null) { + return false; + } + + if (!(matchValue instanceof String)) { + return false; + } + + if (_prerequisites == null) { + return true; + } + + for (Prerequisites prerequisites : _prerequisites) { + String treatment = evaluationContext.getEvaluator().evaluateFeature((String) matchValue, bucketingKey, + prerequisites.featureFlagName, attributes). treatment; + if (!prerequisites.treatments.contains(treatment)) { + return false; + } + } + return true; + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("prerequisites: "); + if (this._prerequisites != null) { + bldr.append(this._prerequisites.stream().map(pr -> pr.featureFlagName + " " + + pr.treatments.toString()).map(Object::toString).collect(Collectors.joining(", "))); + } + return bldr.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + PrerequisitesMatcher that = (PrerequisitesMatcher) o; + + return Objects.equals(_prerequisites, that._prerequisites); + } + + @Override + public int hashCode() { + int result = _prerequisites != null ? _prerequisites.hashCode() : 0; + result = 31 * result + (_prerequisites != null ? _prerequisites.hashCode() : 0); + return result; + } +} diff --git a/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java new file mode 100644 index 00000000..4c74527b --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java @@ -0,0 +1,105 @@ +package io.split.engine.matchers; + +import io.split.client.dtos.ExcludedSegments; +import io.split.engine.evaluator.EvaluationContext; +import io.split.engine.experiments.ParsedCondition; +import io.split.engine.experiments.ParsedRuleBasedSegment; + +import java.util.List; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A matcher that checks if the key is part of a user defined segment. This class + * assumes that the logic for refreshing what keys are part of a segment is delegated + * to SegmentFetcher. + * + * @author adil + */ +public class RuleBasedSegmentMatcher implements Matcher { + private final String _segmentName; + + public RuleBasedSegmentMatcher(String segmentName) { + _segmentName = checkNotNull(segmentName); + } + + @Override + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + if (!(matchValue instanceof String)) { + return false; + } + ParsedRuleBasedSegment parsedRuleBasedSegment = evaluationContext.getRuleBasedSegmentCache().get(_segmentName); + if (parsedRuleBasedSegment == null) { + return false; + } + + if (parsedRuleBasedSegment.excludedKeys().contains(matchValue)) { + return false; + } + + if (matchExcludedSegments(parsedRuleBasedSegment.excludedSegments(), matchValue, bucketingKey, attributes, evaluationContext)) { + return false; + } + + return matchConditions(parsedRuleBasedSegment.parsedConditions(), matchValue, bucketingKey, attributes, evaluationContext); + } + + private boolean matchExcludedSegments(List excludedSegments, Object matchValue, String bucketingKey, + Map attributes, EvaluationContext evaluationContext) { + for (ExcludedSegments excludedSegment: excludedSegments) { + if (excludedSegment.isStandard() && evaluationContext.getSegmentCache().isInSegment(excludedSegment.name, (String) matchValue)) { + return true; + } + + if (excludedSegment.isRuleBased()) { + RuleBasedSegmentMatcher excludedRbsMatcher = new RuleBasedSegmentMatcher(excludedSegment.name); + if (excludedRbsMatcher.match(matchValue, bucketingKey, attributes, evaluationContext)) { + return true; + } + } + } + + return false; + } + + private boolean matchConditions(List conditions, Object matchValue, String bucketingKey, + Map attributes, EvaluationContext evaluationContext) { + for (ParsedCondition parsedCondition : conditions) { + if (parsedCondition.matcher().match((String) matchValue, bucketingKey, attributes, evaluationContext)) { + return true; + } + } + return false; + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + _segmentName.hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) return false; + if (this == obj) return true; + if (!(obj instanceof RuleBasedSegmentMatcher)) return false; + + RuleBasedSegmentMatcher other = (RuleBasedSegmentMatcher) obj; + + return _segmentName.equals(other._segmentName); + } + + @Override + public String toString() { + StringBuilder bldr = new StringBuilder(); + bldr.append("in segment "); + bldr.append(_segmentName); + return bldr.toString(); + } + + public String getSegmentName() { + return _segmentName; + } +} diff --git a/client/src/main/java/io/split/engine/matchers/Semver.java b/client/src/main/java/io/split/engine/matchers/Semver.java new file mode 100644 index 00000000..7a85a0d7 --- /dev/null +++ b/client/src/main/java/io/split/engine/matchers/Semver.java @@ -0,0 +1,176 @@ +package io.split.engine.matchers; + +import io.split.client.exceptions.SemverParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +public class Semver { + private static final String METADATA_DELIMITER = "+"; + private static final String PRERELEASE_DELIMITER = "-"; + private static final String VALUE_DELIMITER = "\\."; + private static final Logger _log = LoggerFactory.getLogger(Semver.class); + + private Long _major; + private Long _minor; + private Long _patch; + private String[] _preRelease = new String[] {}; + private boolean _isStable; + private String _metadata; + private String _version; + + public static Semver build(String version) { + if (version.isEmpty()) return null; + try { + return new Semver(version); + } catch (Exception ex) { + _log.error("An error occurred during the creation of a Semver instance:", ex.getMessage()); + return null; + } + } + + public String version() { + return _version; + } + + public Long major() { + return _major; + } + + public Long minor() { + return _minor; + } + + public Long patch() { + return _patch; + } + + public String[] prerelease() { + return _preRelease; + } + + public String metadata() { + return _metadata; + } + + public boolean isStable() { + return _isStable; + } + + /** + * Precedence comparision between 2 Semver objects. + * + * @return the value {@code 0} if {@code this == toCompare}; + * a value less than {@code 0} if {@code this < toCompare}; and + * a value greater than {@code 0} if {@code this > toCompare} + */ + public int compare(Semver toCompare) { + if (_version.equals(toCompare.version())) { + return 0; + } + // Compare major, minor, and patch versions numerically + int result = Long.compare(_major, toCompare.major()); + if (result != 0) { + return result; + } + result = Long.compare(_minor, toCompare.minor()); + if (result != 0) { + return result; + } + result = Long.compare(_patch, toCompare.patch()); + if (result != 0) { + return result; + } + if (!_isStable && toCompare.isStable()) { + return -1; + } else if (_isStable && !toCompare.isStable()) { + return 1; + } + // Compare pre-release versions lexically + int minLength = Math.min(_preRelease.length, toCompare.prerelease().length); + for (int i = 0; i < minLength; i++) { + if (_preRelease[i].equals(toCompare.prerelease()[i])) { + continue; + } + if ( isNumeric(_preRelease[i]) && isNumeric(toCompare._preRelease[i])) { + return Long.compare(Integer.parseInt(_preRelease[i]), Long.parseLong(toCompare._preRelease[i])); + } + return adjustNumber(_preRelease[i].compareTo(toCompare._preRelease[i])); + } + // Compare lengths of pre-release versions + return Integer.compare(_preRelease.length, toCompare._preRelease.length); + } + + private int adjustNumber(int number) { + if (number > 0) return 1; + if (number < 0) return -1; + return 0; + } + private Semver(String version) throws SemverParseException { + String vWithoutMetadata = setAndRemoveMetadataIfExists(version); + String vWithoutPreRelease = setAndRemovePreReleaseIfExists(vWithoutMetadata); + setMajorMinorAndPatch(vWithoutPreRelease); + _version = setVersion(); + } + private String setAndRemoveMetadataIfExists(String version) throws SemverParseException { + int index = version.indexOf(METADATA_DELIMITER); + if (index == -1) { + return version; + } + _metadata = version.substring(index+1); + if (_metadata == null || _metadata.isEmpty()) { + throw new SemverParseException("Unable to convert to Semver, incorrect pre release data"); + } + return version.substring(0, index); + } + private String setAndRemovePreReleaseIfExists(String vWithoutMetadata) throws SemverParseException { + int index = vWithoutMetadata.indexOf(PRERELEASE_DELIMITER); + if (index == -1) { + _isStable = true; + return vWithoutMetadata; + } + String preReleaseData = vWithoutMetadata.substring(index+1); + _preRelease = preReleaseData.split(VALUE_DELIMITER); + if (_preRelease == null || Arrays.stream(_preRelease).allMatch(pr -> pr == null || pr.isEmpty())) { + throw new SemverParseException("Unable to convert to Semver, incorrect pre release data"); + } + return vWithoutMetadata.substring(0, index); + } + private void setMajorMinorAndPatch(String version) throws SemverParseException { + String[] vParts = version.split(VALUE_DELIMITER); + if (vParts.length != 3) + throw new SemverParseException("Unable to convert to Semver, incorrect format: " + version); + _major = Long.parseLong(vParts[0]); + _minor = Long.parseLong(vParts[1]); + _patch = Long.parseLong(vParts[2]); + } + + private String setVersion() { + String toReturn = _major + VALUE_DELIMITER + _minor + VALUE_DELIMITER + _patch; + if (_preRelease != null && _preRelease.length != 0) + { + for (int i = 0; i < _preRelease.length; i++) + { + if (isNumeric(_preRelease[i])) + { + _preRelease[i] = Long.toString(Long.parseLong(_preRelease[i])); + } + } + toReturn = toReturn + PRERELEASE_DELIMITER + String.join(VALUE_DELIMITER, _preRelease); + } + if (_metadata != null && !_metadata.isEmpty()) { + toReturn = toReturn + METADATA_DELIMITER + _metadata; + } + return toReturn; + } + + private static boolean isNumeric(String str) { + try { + Double.parseDouble(str); + return true; + } catch(NumberFormatException e){ + return false; + } + } +} diff --git a/client/src/main/java/io/split/engine/matchers/UserDefinedSegmentMatcher.java b/client/src/main/java/io/split/engine/matchers/UserDefinedSegmentMatcher.java index f25e4fec..1ba1c5c2 100644 --- a/client/src/main/java/io/split/engine/matchers/UserDefinedSegmentMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/UserDefinedSegmentMatcher.java @@ -1,7 +1,6 @@ package io.split.engine.matchers; -import io.split.cache.SegmentCache; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import java.util.Map; @@ -16,21 +15,19 @@ */ public class UserDefinedSegmentMatcher implements Matcher { private final String _segmentName; - private final SegmentCache _segmentCache; - public UserDefinedSegmentMatcher(SegmentCache segmentCache, String segmentName) { - _segmentCache = checkNotNull(segmentCache); + public UserDefinedSegmentMatcher(String segmentName) { _segmentName = checkNotNull(segmentName); } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (!(matchValue instanceof String)) { return false; } - return _segmentCache.isInSegment(_segmentName, (String) matchValue); + return evaluationContext.getSegmentCache().isInSegment(_segmentName, (String) matchValue); } @Override @@ -58,4 +55,8 @@ public String toString() { bldr.append(_segmentName); return bldr.toString(); } + + public String getSegmentName() { + return _segmentName; + } } diff --git a/client/src/main/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcher.java b/client/src/main/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcher.java index f55e207e..5f4f9433 100644 --- a/client/src/main/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.collections; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -24,7 +24,7 @@ public ContainsAllOfSetMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcher.java b/client/src/main/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcher.java index 9747cd68..3a251440 100644 --- a/client/src/main/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.collections; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -25,7 +25,7 @@ public ContainsAnyOfSetMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/collections/EqualToSetMatcher.java b/client/src/main/java/io/split/engine/matchers/collections/EqualToSetMatcher.java index 467cfcd1..4a09c9ef 100644 --- a/client/src/main/java/io/split/engine/matchers/collections/EqualToSetMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/collections/EqualToSetMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.collections; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -25,7 +25,7 @@ public EqualToSetMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/collections/PartOfSetMatcher.java b/client/src/main/java/io/split/engine/matchers/collections/PartOfSetMatcher.java index 4f9fc921..8bb5f139 100644 --- a/client/src/main/java/io/split/engine/matchers/collections/PartOfSetMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/collections/PartOfSetMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.collections; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -25,7 +25,7 @@ public PartOfSetMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/strings/ContainsAnyOfMatcher.java b/client/src/main/java/io/split/engine/matchers/strings/ContainsAnyOfMatcher.java index 28646229..b8cbe8fc 100644 --- a/client/src/main/java/io/split/engine/matchers/strings/ContainsAnyOfMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/strings/ContainsAnyOfMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.strings; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -23,7 +23,7 @@ public ContainsAnyOfMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; diff --git a/client/src/main/java/io/split/engine/matchers/strings/EndsWithAnyOfMatcher.java b/client/src/main/java/io/split/engine/matchers/strings/EndsWithAnyOfMatcher.java index 33a63ac0..32ac9f7f 100644 --- a/client/src/main/java/io/split/engine/matchers/strings/EndsWithAnyOfMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/strings/EndsWithAnyOfMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.strings; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -23,7 +23,7 @@ public EndsWithAnyOfMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; diff --git a/client/src/main/java/io/split/engine/matchers/strings/RegularExpressionMatcher.java b/client/src/main/java/io/split/engine/matchers/strings/RegularExpressionMatcher.java index b651bae0..f64b3264 100644 --- a/client/src/main/java/io/split/engine/matchers/strings/RegularExpressionMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/strings/RegularExpressionMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.strings; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Map; @@ -16,7 +16,7 @@ public RegularExpressionMatcher(String matcherValue) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/strings/StartsWithAnyOfMatcher.java b/client/src/main/java/io/split/engine/matchers/strings/StartsWithAnyOfMatcher.java index b758dba3..7f1ed2ca 100644 --- a/client/src/main/java/io/split/engine/matchers/strings/StartsWithAnyOfMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/strings/StartsWithAnyOfMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.strings; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -23,7 +23,7 @@ public StartsWithAnyOfMatcher(Collection compareTo) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { if (matchValue == null) { return false; } diff --git a/client/src/main/java/io/split/engine/matchers/strings/WhitelistMatcher.java b/client/src/main/java/io/split/engine/matchers/strings/WhitelistMatcher.java index afedc405..5068c143 100644 --- a/client/src/main/java/io/split/engine/matchers/strings/WhitelistMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/strings/WhitelistMatcher.java @@ -1,6 +1,6 @@ package io.split.engine.matchers.strings; -import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluationContext; import io.split.engine.matchers.Matcher; import java.util.Collection; @@ -22,7 +22,7 @@ public WhitelistMatcher(Collection whitelist) { } @Override - public boolean match(Object matchValue, String bucketingKey, Map attributes, Evaluator evaluator) { + public boolean match(Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { return _whitelist.contains(matchValue); } diff --git a/client/src/main/java/io/split/engine/segments/SegmentChangeFetcher.java b/client/src/main/java/io/split/engine/segments/SegmentChangeFetcher.java index f4d46ed1..55fd2326 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentChangeFetcher.java +++ b/client/src/main/java/io/split/engine/segments/SegmentChangeFetcher.java @@ -1,6 +1,7 @@ package io.split.engine.segments; import io.split.client.dtos.SegmentChange; +import io.split.engine.common.FetchOptions; /** * Fetches changes in the segment since a reference point. @@ -22,8 +23,9 @@ public interface SegmentChangeFetcher { * @param segmentName the name of the segment to fetch. * @param changesSinceThisChangeNumber a value less than zero implies that the client is * requesting information on this segment for the first time. + * @param options * @return SegmentChange * @throws java.lang.RuntimeException if there was a problem fetching segment changes */ - SegmentChange fetch(String segmentName, long changesSinceThisChangeNumber, boolean addCacheHeader); + SegmentChange fetch(String segmentName, long changesSinceThisChangeNumber, FetchOptions options); } diff --git a/client/src/main/java/io/split/engine/segments/SegmentFetcher.java b/client/src/main/java/io/split/engine/segments/SegmentFetcher.java index af4bbc76..3d5c3a48 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentFetcher.java +++ b/client/src/main/java/io/split/engine/segments/SegmentFetcher.java @@ -1,5 +1,7 @@ package io.split.engine.segments; +import io.split.engine.common.FetchOptions; + /** * Created by adilaijaz on 5/7/15. */ @@ -7,9 +9,7 @@ public interface SegmentFetcher { /** * fetch */ - void fetch(boolean addCacheHeader); - - void runWhitCacheHeader(); + boolean fetch(FetchOptions opts); - void fetchAll(); + boolean runWhitCacheHeader(); } diff --git a/client/src/main/java/io/split/engine/segments/SegmentFetcherImp.java b/client/src/main/java/io/split/engine/segments/SegmentFetcherImp.java index ac21e846..f0df3bdd 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentFetcherImp.java +++ b/client/src/main/java/io/split/engine/segments/SegmentFetcherImp.java @@ -1,12 +1,20 @@ package io.split.engine.segments; -import io.split.cache.SegmentCache; import io.split.client.dtos.SegmentChange; -import io.split.engine.SDKReadinessGates; +import io.split.storages.SegmentCacheProducer; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.engine.common.FetchOptions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; + import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static com.google.common.base.Preconditions.checkNotNull; @@ -16,66 +24,84 @@ public class SegmentFetcherImp implements SegmentFetcher { private final String _segmentName; private final SegmentChangeFetcher _segmentChangeFetcher; - private final SegmentCache _segmentCache; - private final SDKReadinessGates _gates; + private final SegmentCacheProducer _segmentCacheProducer; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + + private final SdkEventsNotifier _notifier; private final Object _lock = new Object(); - public SegmentFetcherImp(String segmentName, SegmentChangeFetcher segmentChangeFetcher, SDKReadinessGates gates, SegmentCache segmentCache) { + public SegmentFetcherImp(String segmentName, SegmentChangeFetcher segmentChangeFetcher, SegmentCacheProducer segmentCacheProducer, + TelemetryRuntimeProducer telemetryRuntimeProducer) { + this(segmentName, segmentChangeFetcher, segmentCacheProducer, telemetryRuntimeProducer, SdkEventsNotifier.NOOP); + } + + public SegmentFetcherImp(String segmentName, SegmentChangeFetcher segmentChangeFetcher, SegmentCacheProducer segmentCacheProducer, + TelemetryRuntimeProducer telemetryRuntimeProducer, SdkEventsNotifier notifier) { + _notifier = checkNotNull(notifier); _segmentName = checkNotNull(segmentName); _segmentChangeFetcher = checkNotNull(segmentChangeFetcher); - _segmentCache = checkNotNull(segmentCache); - _gates = checkNotNull(gates); + _segmentCacheProducer = checkNotNull(segmentCacheProducer); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); - _segmentCache.updateSegment(segmentName, new ArrayList<>(), new ArrayList<>()); + _segmentCacheProducer.updateSegment(segmentName, new ArrayList<>(), new ArrayList<>(), -1L); } @Override - public void fetch(boolean addCacheHeader){ + public boolean fetch(FetchOptions opts){ try { - callLoopRun(false, addCacheHeader); - } catch (Throwable t) { - _log.error("RefreshableSegmentFetcher failed: " + t.getMessage()); + final long INITIAL_CN = _segmentCacheProducer.getChangeNumber(_segmentName); + while (true) { + long start = _segmentCacheProducer.getChangeNumber(_segmentName); + runWithoutExceptionHandling(opts); + if (INITIAL_CN == start) { + opts = new FetchOptions.Builder(opts).targetChangeNumber(FetchOptions.DEFAULT_TARGET_CHANGENUMBER).build(); + } + long end = _segmentCacheProducer.getChangeNumber(_segmentName); + if (start >= end) { + break; + } + } + return true; + } catch (Exception e){ + _log.error("RefreshableSegmentFetcher failed: " + e.getMessage()); if (_log.isDebugEnabled()) { - _log.debug("Reason:", t); + _log.debug("Reason:", e); } + return false; } } - private void runWithoutExceptionHandling(boolean addCacheHeader) { - SegmentChange change = _segmentChangeFetcher.fetch(_segmentName, _segmentCache.getChangeNumber(_segmentName), addCacheHeader); + private void runWithoutExceptionHandling(FetchOptions options) { + if (_log.isDebugEnabled()) { + _log.debug(String.format("Synchronizing segment %s", _segmentName)); + } + SegmentChange change = _segmentChangeFetcher.fetch(_segmentName, _segmentCacheProducer.getChangeNumber(_segmentName), options); if (change == null) { throw new IllegalStateException("SegmentChange was null"); } - if (change.till == _segmentCache.getChangeNumber(_segmentName)) { - // no change. - return; - } - - if (change.since != _segmentCache.getChangeNumber(_segmentName) - || change.since < _segmentCache.getChangeNumber(_segmentName)) { + if (change.since != _segmentCacheProducer.getChangeNumber(_segmentName) + || change.since < _segmentCacheProducer.getChangeNumber(_segmentName)) { // some other thread may have updated the shared state. exit return; } - if (change.added.isEmpty() && change.removed.isEmpty()) { // there are no changes. weird! - _segmentCache.setChangeNumber(_segmentName,change.till); + _segmentCacheProducer.setChangeNumber(_segmentName,change.till); return; } synchronized (_lock) { // check state one more time. - if (change.since != _segmentCache.getChangeNumber(_segmentName) - || change.till < _segmentCache.getChangeNumber(_segmentName)) { + if (change.since != _segmentCacheProducer.getChangeNumber(_segmentName) + || change.till < _segmentCacheProducer.getChangeNumber(_segmentName)) { // some other thread may have updated the shared state. exit return; } - //updateSegment(sn, toadd, tormv, chngN) - _segmentCache.updateSegment(_segmentName,change.added, change.removed); + _segmentCacheProducer.updateSegment(_segmentName,change.added, change.removed, change.till); if (!change.added.isEmpty()) { _log.info(_segmentName + " added keys: " + summarize(change.added)); @@ -85,7 +111,10 @@ private void runWithoutExceptionHandling(boolean addCacheHeader) { _log.info(_segmentName + " removed keys: " + summarize(change.removed)); } - _segmentCache.setChangeNumber(_segmentName,change.till); + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.SEGMENTS, System.currentTimeMillis()); + + _notifier.notify(SdkInternalEvent.SEGMENTS_UPDATED, + SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, Collections.emptySet())); } } @@ -109,49 +138,8 @@ private String summarize(List changes) { return bldr.toString(); } - private void callLoopRun(boolean isFetch, boolean addCacheHeader){ - while (true) { - long start = _segmentCache.getChangeNumber(_segmentName); - runWithoutExceptionHandling(addCacheHeader); - long end = _segmentCache.getChangeNumber(_segmentName); - if (isFetch && _log.isDebugEnabled()) { - _log.debug(_segmentName + " segment fetch before: " + start + ", after: " + _segmentCache.getChangeNumber(_segmentName) /*+ " size: " + _concurrentKeySet.size()*/); - } - if (start >= end) { - break; - } - } - } - - @Override - public void runWhitCacheHeader(){ - this.fetchAndUpdate(true); - } - - /** - * Calls callLoopRun and after fetchs segment. - * @param addCacheHeader indicates if CacheHeader is required - */ - private void fetchAndUpdate(boolean addCacheHeader) { - try { - // Do this again in case the previous call errored out. - _gates.registerSegment(_segmentName); - callLoopRun(true, addCacheHeader); - - _gates.segmentIsReady(_segmentName); - - } catch (Throwable t) { - _log.error("RefreshableSegmentFetcher failed: " + t.getMessage()); - if (_log.isDebugEnabled()) { - _log.debug("Reason:", t); - } - } - } - @Override - public void fetchAll() { - this.fetchAndUpdate(false); + public boolean runWhitCacheHeader(){ + return this.fetch(new FetchOptions.Builder().cacheControlHeaders(true).build()); } - - -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/segments/SegmentImp.java b/client/src/main/java/io/split/engine/segments/SegmentImp.java index 2d153d1f..27fb8c04 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentImp.java +++ b/client/src/main/java/io/split/engine/segments/SegmentImp.java @@ -33,12 +33,17 @@ public void setChangeNumber(long changeNumber){ _changeNumber.set(changeNumber); } - public void update(List toAdd, List toRemove){ + public void update(List toAdd, List toRemove, long changeNumber){ _concurrentKeySet.removeAll(toRemove); _concurrentKeySet.addAll(toAdd); + _changeNumber.set(changeNumber); } public boolean contains(String key) { return _concurrentKeySet.contains(key); } + + public long getKeysSize() { + return _concurrentKeySet.size(); + } } diff --git a/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTask.java b/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTask.java index 0bed9922..85a10a20 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTask.java +++ b/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTask.java @@ -1,6 +1,8 @@ package io.split.engine.segments; -public interface SegmentSynchronizationTask extends Runnable { +import io.split.engine.experiments.SyncTask; + +public interface SegmentSynchronizationTask extends SyncTask { /** * initializes the segment * @param segmentName @@ -17,7 +19,7 @@ public interface SegmentSynchronizationTask extends Runnable { /** * starts the fetching */ - void startPeriodicFetching(); + void start(); /** * stops the thread @@ -29,4 +31,10 @@ public interface SegmentSynchronizationTask extends Runnable { * @param addCacheHeader */ void fetchAll(boolean addCacheHeader); + + /** + * fetch every Segment Synchronous + */ + boolean fetchAllSynchronous(); + void close(); } diff --git a/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTaskImp.java b/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTaskImp.java index 7f3931f9..095e619b 100644 --- a/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTaskImp.java +++ b/client/src/main/java/io/split/engine/segments/SegmentSynchronizationTaskImp.java @@ -1,23 +1,31 @@ package io.split.engine.segments; import com.google.common.collect.Maps; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import io.split.cache.SegmentCache; -import io.split.engine.SDKReadinessGates; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.utils.SplitExecutorFactory; +import io.split.engine.common.FetchOptions; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Closeable; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; @@ -30,38 +38,42 @@ public class SegmentSynchronizationTaskImp implements SegmentSynchronizationTask private final AtomicBoolean _running; private final Object _lock = new Object(); private final ConcurrentMap _segmentFetchers = Maps.newConcurrentMap(); - private final SegmentCache _segmentCache; - private final SDKReadinessGates _gates; + private final SegmentCacheProducer _segmentCacheProducer; private final ScheduledExecutorService _scheduledExecutorService; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final SplitCacheConsumer _splitCacheConsumer; + private final RuleBasedSegmentCacheConsumer _ruleBasedSegmentCacheConsumer; + + private final SdkEventsNotifier _notifier; private ScheduledFuture _scheduledFuture; - public SegmentSynchronizationTaskImp(SegmentChangeFetcher segmentChangeFetcher, long refreshEveryNSeconds, int numThreads, SDKReadinessGates gates, SegmentCache segmentCache) { + public SegmentSynchronizationTaskImp(SegmentChangeFetcher segmentChangeFetcher, long refreshEveryNSeconds, int numThreads, + SegmentCacheProducer segmentCacheProducer, TelemetryRuntimeProducer telemetryRuntimeProducer, + SplitCacheConsumer splitCacheConsumer, ThreadFactory threadFactory, + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer) { + this(segmentChangeFetcher, refreshEveryNSeconds, numThreads, segmentCacheProducer, telemetryRuntimeProducer, + splitCacheConsumer, threadFactory, ruleBasedSegmentCacheConsumer, SdkEventsNotifier.NOOP); + } + + public SegmentSynchronizationTaskImp(SegmentChangeFetcher segmentChangeFetcher, long refreshEveryNSeconds, int numThreads, + SegmentCacheProducer segmentCacheProducer, TelemetryRuntimeProducer telemetryRuntimeProducer, + SplitCacheConsumer splitCacheConsumer, ThreadFactory threadFactory, + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer, SdkEventsNotifier notifier) { + _notifier = checkNotNull(notifier); _segmentChangeFetcher = checkNotNull(segmentChangeFetcher); checkArgument(refreshEveryNSeconds >= 0L); _refreshEveryNSeconds = new AtomicLong(refreshEveryNSeconds); - - _gates = checkNotNull(gates); - - ThreadFactory threadFactory = new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("split-segmentFetcher-" + "%d") - .build(); - - _scheduledExecutorService = Executors.newScheduledThreadPool(numThreads, threadFactory); - + _scheduledExecutorService = SplitExecutorFactory.buildScheduledExecutorService(threadFactory, "split-segmentFetcher-" + "%d", numThreads); _running = new AtomicBoolean(false); - _segmentCache = checkNotNull(segmentCache); + _segmentCacheProducer = checkNotNull(segmentCacheProducer); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _splitCacheConsumer = checkNotNull(splitCacheConsumer); + _ruleBasedSegmentCacheConsumer = checkNotNull(ruleBasedSegmentCacheConsumer); } - @Override - public void run() { - this.fetchAll(false); - } - - @Override public void initializeSegment(String segmentName) { SegmentFetcher segment = _segmentFetchers.get(segmentName); if (segment != null) { @@ -77,23 +89,17 @@ public void initializeSegment(String segmentName) { return; } - try { - _gates.registerSegment(segmentName); - } catch (InterruptedException e) { - _log.error("Unable to register segment " + segmentName); - } - - segment = new SegmentFetcherImp(segmentName, _segmentChangeFetcher, _gates, _segmentCache); + SegmentFetcher newSegment = new SegmentFetcherImp(segmentName, _segmentChangeFetcher, _segmentCacheProducer, + _telemetryRuntimeProducer, _notifier); if (_running.get()) { - _scheduledExecutorService.submit(segment::fetchAll); + _scheduledExecutorService.submit(() -> newSegment.fetch(new FetchOptions.Builder().build())); } - _segmentFetchers.putIfAbsent(segmentName, segment); + _segmentFetchers.putIfAbsent(segmentName, newSegment); } } - @Override public SegmentFetcher getFetcher(String segmentName) { initializeSegment(segmentName); @@ -101,14 +107,16 @@ public SegmentFetcher getFetcher(String segmentName) { } @Override - public void startPeriodicFetching() { - if (_running.getAndSet(true)) { + public void start() { + if (_running.getAndSet(true) ) { _log.debug("Segments PeriodicFetching is running..."); return; } _log.debug("Starting PeriodicFetching Segments ..."); - _scheduledFuture = _scheduledExecutorService.scheduleWithFixedDelay(this, 0L, _refreshEveryNSeconds.get(), TimeUnit.SECONDS); + _scheduledFuture = _scheduledExecutorService.scheduleWithFixedDelay(() -> { + fetchAll(false); + }, 0L, _refreshEveryNSeconds.get(), TimeUnit.SECONDS); } @Override @@ -142,7 +150,13 @@ public void close() { } @Override + public boolean isRunning() { + return _running.get(); + } + public void fetchAll(boolean addCacheHeader) { + Set names = getSegmentNames(); + names.forEach(this::initialize); for (Map.Entry entry : _segmentFetchers.entrySet()) { SegmentFetcher fetcher = entry.getValue(); @@ -154,7 +168,56 @@ public void fetchAll(boolean addCacheHeader) { _scheduledExecutorService.submit(fetcher::runWhitCacheHeader); continue; } - _scheduledExecutorService.submit(fetcher::fetchAll); + + _scheduledExecutorService.submit(() -> fetcher.fetch(new FetchOptions.Builder().build())); + } + } + + public boolean fetchAllSynchronous() { + Set names = getSegmentNames(); + names.forEach(this::initialize); + List> segmentFetchExecutions = _segmentFetchers.entrySet() + .stream().map(e -> _scheduledExecutorService.submit(e.getValue()::runWhitCacheHeader)) + .collect(Collectors.toList()); + int failures = segmentFetchExecutions.stream() + .mapToInt(f -> { + try { + return f.get() ? 0 : 1; + } catch (InterruptedException e) { + _log.error(e.getMessage()); + } catch (ExecutionException e) { + _log.error(e.getMessage()); + } + return 0; + }) + .sum(); + return failures == 0; + } + + private void initialize(String segmentName) { + SegmentFetcher segment = _segmentFetchers.get(segmentName); + if (segment != null) { + return; + } + // we are locking here since we wanna make sure that we create only ONE RefreshableSegmentFetcher + // per segment. + synchronized (_lock) { + // double check + segment = _segmentFetchers.get(segmentName); + if (segment != null) { + return; + } + + segment = new SegmentFetcherImp(segmentName, _segmentChangeFetcher, _segmentCacheProducer, _telemetryRuntimeProducer, _notifier); + + _segmentFetchers.putIfAbsent(segmentName, segment); } } -} + + private Set getSegmentNames() { + Set names = new HashSet<>(_splitCacheConsumer.getSegments()); + names.addAll(_ruleBasedSegmentCacheConsumer.getSegments()); + + return names; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/splitter/Splitter.java b/client/src/main/java/io/split/engine/splitter/Splitter.java index 60bb6d74..c867a81d 100644 --- a/client/src/main/java/io/split/engine/splitter/Splitter.java +++ b/client/src/main/java/io/split/engine/splitter/Splitter.java @@ -33,15 +33,15 @@ public static String getTreatment(String key, int seed, List partitio static long hash(String key, int seed, int algo) { switch (algo) { case ALGO_MURMUR: - return murmur_hash(key, seed); + return murmurHash(key, seed); case ALGO_LEGACY: default: - return legacy_hash(key, seed); + return legacyHash(key, seed); } } /*package private*/ - static long murmur_hash(String key, int seed) { + static long murmurHash(String key, int seed) { return MurmurHash3.murmurhash3_x86_32(key, 0, key.length(), seed); } @@ -56,7 +56,7 @@ public static int getBucket(String key, int seed, int algo) { } /*package private*/ - static int legacy_hash(String key, int seed) { + static int legacyHash(String key, int seed) { int h = 0; for (int i = 0; i < key.length(); i++) { h = 31 * h + key.charAt(i); diff --git a/client/src/main/java/io/split/engine/sse/AuthApiClientImp.java b/client/src/main/java/io/split/engine/sse/AuthApiClientImp.java index 2912c9ad..5c45e1b7 100644 --- a/client/src/main/java/io/split/engine/sse/AuthApiClientImp.java +++ b/client/src/main/java/io/split/engine/sse/AuthApiClientImp.java @@ -1,53 +1,61 @@ package io.split.engine.sse; import com.google.gson.JsonObject; +import io.split.Spec; +import io.split.client.dtos.SplitHttpResponse; import io.split.client.utils.Json; +import io.split.engine.common.FetchOptions; import io.split.engine.sse.dtos.AuthenticationResponse; import io.split.engine.sse.dtos.RawAuthResponse; -import org.apache.hc.client5.http.classic.methods.HttpGet; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.apache.hc.core5.http.HttpStatus; -import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.net.URIBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; -import java.nio.charset.StandardCharsets; import static com.google.common.base.Preconditions.checkNotNull; public class AuthApiClientImp implements AuthApiClient { - private static final Logger _log = LoggerFactory.getLogger(AuthApiClient.class); + private static final Logger _log = LoggerFactory.getLogger(AuthApiClientImp.class); - private final CloseableHttpClient _httpClient; + private static final String SPEC = "s"; + private final SplitHttpClient _httpClient; private final String _target; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; - public AuthApiClientImp(String url, - CloseableHttpClient httpClient) { + public AuthApiClientImp(String url, SplitHttpClient httpClient, TelemetryRuntimeProducer telemetryRuntimeProducer) { _httpClient = checkNotNull(httpClient); _target = checkNotNull(url); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); } @Override public AuthenticationResponse Authenticate() { try { - URI uri = new URIBuilder(_target).build(); - HttpGet request = new HttpGet(uri); - - CloseableHttpResponse response = _httpClient.execute(request); - Integer statusCode = response.getCode(); + long initTime = System.currentTimeMillis(); + URI uri = new URIBuilder(_target).addParameter(SPEC, "" + Spec.SPEC_1_3).build(); + SplitHttpResponse response = _httpClient.get(uri, new FetchOptions.Builder().cacheControlHeaders(false).build(), null); + Integer statusCode = response.statusCode(); if (statusCode == HttpStatus.SC_OK) { _log.debug(String.format("Success connection to: %s", _target)); - String jsonContent = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); - return getSuccessResponse(jsonContent); + _telemetryRuntimeProducer.recordTokenRefreshes(); + _telemetryRuntimeProducer.recordSuccessfulSync(LastSynchronizationRecordsEnum.TOKEN, System.currentTimeMillis()); + _telemetryRuntimeProducer.recordSyncLatency(HTTPLatenciesEnum.TOKEN, System.currentTimeMillis()-initTime); + return getSuccessResponse(response.body()); } _log.error(String.format("Problem to connect to : %s. Response status: %s", _target, statusCode)); if (statusCode >= HttpStatus.SC_BAD_REQUEST && statusCode < HttpStatus.SC_INTERNAL_SERVER_ERROR) { + if (statusCode == HttpStatus.SC_UNAUTHORIZED) { + _telemetryRuntimeProducer.recordAuthRejections(); + } return new AuthenticationResponse(false,false); } diff --git a/client/src/main/java/io/split/engine/sse/EventSourceClientImp.java b/client/src/main/java/io/split/engine/sse/EventSourceClientImp.java index 7d8bf990..212d929f 100644 --- a/client/src/main/java/io/split/engine/sse/EventSourceClientImp.java +++ b/client/src/main/java/io/split/engine/sse/EventSourceClientImp.java @@ -1,12 +1,15 @@ package io.split.engine.sse; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; +import io.split.client.RequestDecorator; import io.split.engine.sse.client.RawEvent; import io.split.engine.sse.client.SSEClient; import io.split.engine.sse.dtos.SegmentQueueDto; import io.split.engine.sse.exceptions.EventParsingException; -import io.split.engine.sse.workers.SplitsWorker; +import io.split.engine.sse.workers.FeatureFlagsWorker; import io.split.engine.sse.workers.Worker; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.core5.net.URIBuilder; import org.slf4j.Logger; @@ -14,6 +17,7 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; import static com.google.common.base.Preconditions.checkNotNull; @@ -35,7 +39,10 @@ public class EventSourceClientImp implements EventSourceClient { NotificationParser notificationParser, NotificationProcessor notificationProcessor, PushStatusTracker pushStatusTracker, - CloseableHttpClient sseHttpClient) { + CloseableHttpClient sseHttpClient, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory, + RequestDecorator requestDecorator) { _baseStreamingUrl = checkNotNull(baseStreamingUrl); _notificationParser = checkNotNull(notificationParser); _notificationProcessor = checkNotNull(notificationProcessor); @@ -44,20 +51,29 @@ public class EventSourceClientImp implements EventSourceClient { _sseClient = new SSEClient( inboundEvent -> { onMessage(inboundEvent); return null; }, status -> { _pushStatusTracker.handleSseStatus(status); return null; }, - sseHttpClient); + sseHttpClient, + telemetryRuntimeProducer, + threadFactory, + requestDecorator); _firstEvent = new AtomicBoolean(); } public static EventSourceClientImp build(String baseStreamingUrl, - SplitsWorker splitsWorker, + FeatureFlagsWorker featureFlagsWorker, Worker segmentWorker, PushStatusTracker pushStatusTracker, - CloseableHttpClient sseHttpClient) { + CloseableHttpClient sseHttpClient, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory, + RequestDecorator requestDecorator) { return new EventSourceClientImp(baseStreamingUrl, new NotificationParserImp(), - NotificationProcessorImp.build(splitsWorker, segmentWorker, pushStatusTracker), + NotificationProcessorImp.build(featureFlagsWorker, segmentWorker, pushStatusTracker), pushStatusTracker, - sseHttpClient); + sseHttpClient, + telemetryRuntimeProducer, + threadFactory, + requestDecorator); } @Override @@ -77,6 +93,7 @@ public boolean start(String channelList, String token) { @Override public void stop() { + _log.info("Stopping EventSourceClientImp"); if (!_sseClient.isOpen()) { _log.info("Event Source Client is closed."); return; @@ -99,7 +116,7 @@ private void onMessage(RawEvent event) { if(_firstEvent.compareAndSet(false, true) && !ERROR.equals(type)){ _pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.FIRST_EVENT); } - if (payload.length() > 0) { + if (!Strings.isNullOrEmpty(payload)) { _log.debug(String.format("Payload received: %s", payload)); switch (type) { case MESSAGE: @@ -115,7 +132,7 @@ private void onMessage(RawEvent event) { } catch (EventParsingException ex) { _log.debug(String.format("Error parsing the event: %s. Payload: %s", ex.getMessage(), ex.getPayload())); } catch (Exception e) { - _log.debug(String.format("Error onMessage: %s", e.getMessage())); + _log.debug(String.format("Error parsing the event id: %s. OnMessage: %s", event.id(), e.getMessage()), e); } } } \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/NotificationParserImp.java b/client/src/main/java/io/split/engine/sse/NotificationParserImp.java index 802d94b5..8bfaf886 100644 --- a/client/src/main/java/io/split/engine/sse/NotificationParserImp.java +++ b/client/src/main/java/io/split/engine/sse/NotificationParserImp.java @@ -1,7 +1,18 @@ package io.split.engine.sse; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; import io.split.client.utils.Json; -import io.split.engine.sse.dtos.*; + +import io.split.engine.sse.dtos.ControlNotification; +import io.split.engine.sse.dtos.ErrorNotification; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.GenericNotificationData; +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.OccupancyNotification; +import io.split.engine.sse.dtos.RawMessageNotification; +import io.split.engine.sse.dtos.SegmentChangeNotification; +import io.split.engine.sse.dtos.SplitKillNotification; import io.split.engine.sse.exceptions.EventParsingException; public class NotificationParserImp implements NotificationParser { @@ -13,11 +24,9 @@ public IncomingNotification parseMessage(String payload) throws EventParsingExce RawMessageNotification rawMessageNotification = Json.fromJson(payload, RawMessageNotification.class); GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); genericNotificationData.setChannel(rawMessageNotification.getChannel()); - if (rawMessageNotification.getChannel().contains(OCCUPANCY_PREFIX)) { return parseControlChannelMessage(genericNotificationData); } - return parseNotification(genericNotificationData); } catch (Exception ex) { throw new EventParsingException("Error parsing event.", ex, payload); @@ -28,11 +37,9 @@ public IncomingNotification parseMessage(String payload) throws EventParsingExce public ErrorNotification parseError(String payload) throws EventParsingException { try { ErrorNotification messageError = Json.fromJson(payload, ErrorNotification.class); - if (messageError.getMessage() == null || messageError.getStatusCode() == null) { throw new Exception("Wrong notification format."); } - return messageError; } catch (Exception ex) { throw new EventParsingException("Error parsing event.", ex, payload); @@ -42,7 +49,9 @@ public ErrorNotification parseError(String payload) throws EventParsingException private IncomingNotification parseNotification(GenericNotificationData genericNotificationData) throws Exception { switch (genericNotificationData.getType()) { case SPLIT_UPDATE: - return new SplitChangeNotification(genericNotificationData); + return new CommonChangeNotification(genericNotificationData, Split.class); + case RB_SEGMENT_UPDATE: + return new CommonChangeNotification(genericNotificationData, RuleBasedSegment.class); case SPLIT_KILL: return new SplitKillNotification(genericNotificationData); case SEGMENT_UPDATE: @@ -59,7 +68,6 @@ private IncomingNotification parseControlChannelMessage(GenericNotificationData if (genericNotificationData.getControlType() != null) { return new ControlNotification(genericNotificationData); } - return new OccupancyNotification(genericNotificationData); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/NotificationProcessor.java b/client/src/main/java/io/split/engine/sse/NotificationProcessor.java index 20f8af7f..fce86757 100644 --- a/client/src/main/java/io/split/engine/sse/NotificationProcessor.java +++ b/client/src/main/java/io/split/engine/sse/NotificationProcessor.java @@ -1,12 +1,13 @@ package io.split.engine.sse; import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.SplitKillNotification; import io.split.engine.sse.dtos.StatusNotification; public interface NotificationProcessor { void process(IncomingNotification notification); - void processSplitUpdate(long changeNumber); - void processSplitKill(long changeNumber, String splitName, String defaultTreatment); + void processUpdates(IncomingNotification notification); + void processSplitKill(SplitKillNotification splitKillNotification); void processSegmentUpdate(long changeNumber, String segmentName); void processStatus(StatusNotification statusNotification); -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/NotificationProcessorImp.java b/client/src/main/java/io/split/engine/sse/NotificationProcessorImp.java index c8271c9e..b833efc3 100644 --- a/client/src/main/java/io/split/engine/sse/NotificationProcessorImp.java +++ b/client/src/main/java/io/split/engine/sse/NotificationProcessorImp.java @@ -1,46 +1,53 @@ package io.split.engine.sse; import com.google.common.annotations.VisibleForTesting; +import io.split.client.dtos.Split; +import io.split.engine.sse.dtos.GenericNotificationData; import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.SplitKillNotification; import io.split.engine.sse.dtos.StatusNotification; import io.split.engine.sse.dtos.SegmentQueueDto; -import io.split.engine.sse.workers.SplitsWorker; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.workers.FeatureFlagsWorker; import io.split.engine.sse.workers.Worker; import static com.google.common.base.Preconditions.checkNotNull; public class NotificationProcessorImp implements NotificationProcessor { - private final SplitsWorker _splitsWorker; + private final FeatureFlagsWorker _featureFlagsWorker; private final Worker _segmentWorker; private final PushStatusTracker _pushStatusTracker; @VisibleForTesting - /* package private */ NotificationProcessorImp(SplitsWorker splitsWorker, + /* package private */ NotificationProcessorImp(FeatureFlagsWorker featureFlagsWorker, Worker segmentWorker, PushStatusTracker pushStatusTracker) { - _splitsWorker = checkNotNull(splitsWorker); + _featureFlagsWorker = checkNotNull(featureFlagsWorker); _segmentWorker = checkNotNull(segmentWorker); _pushStatusTracker = checkNotNull(pushStatusTracker); } - public static NotificationProcessorImp build(SplitsWorker splitsWorker, Worker segmentWorker, PushStatusTracker pushStatusTracker) { - return new NotificationProcessorImp(splitsWorker, segmentWorker, pushStatusTracker); + public static NotificationProcessorImp build(FeatureFlagsWorker featureFlagsWorker, Worker segmentWorker, + PushStatusTracker pushStatusTracker) { + return new NotificationProcessorImp(featureFlagsWorker, segmentWorker, pushStatusTracker); } - @Override - public void process(IncomingNotification notification) { - notification.handler(this); + public void processUpdates(IncomingNotification notification) { + _featureFlagsWorker.addToQueue(notification); } @Override - public void processSplitUpdate(long changeNumber) { - _splitsWorker.addToQueue(changeNumber); + public void process(IncomingNotification notification) { + notification.handler(this); } @Override - public void processSplitKill(long changeNumber, String splitName, String defaultTreatment) { - _splitsWorker.killSplit(changeNumber, splitName, defaultTreatment); - _splitsWorker.addToQueue(changeNumber); + public void processSplitKill(SplitKillNotification splitKillNotification) { + _featureFlagsWorker.kill(splitKillNotification); + _featureFlagsWorker.addToQueue(new CommonChangeNotification<>(GenericNotificationData.builder() + .changeNumber(splitKillNotification.getChangeNumber()) + .channel(splitKillNotification.getChannel()) + .build(), Split.class)); } @Override diff --git a/client/src/main/java/io/split/engine/sse/PushStatusTrackerImp.java b/client/src/main/java/io/split/engine/sse/PushStatusTrackerImp.java index f76cf691..083fc0d3 100644 --- a/client/src/main/java/io/split/engine/sse/PushStatusTrackerImp.java +++ b/client/src/main/java/io/split/engine/sse/PushStatusTrackerImp.java @@ -7,6 +7,9 @@ import io.split.engine.sse.dtos.ControlType; import io.split.engine.sse.dtos.ErrorNotification; import io.split.engine.sse.dtos.OccupancyNotification; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.StreamEventsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,8 +18,12 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import static com.google.common.base.Preconditions.checkNotNull; + public class PushStatusTrackerImp implements PushStatusTracker { private static final Logger _log = LoggerFactory.getLogger(PushStatusTracker.class); + private static final String CONTROL_PRI_CHANNEL = "control_pri"; + private static final String CONTROL_SEC_CHANNEL = "control_sec"; private final AtomicBoolean _publishersOnline = new AtomicBoolean(true); private final AtomicReference _sseStatus = new AtomicReference<>(SSEClient.StatusMessage.INITIALIZATION_IN_PROGRESS); @@ -24,8 +31,11 @@ public class PushStatusTrackerImp implements PushStatusTracker { private final LinkedBlockingQueue _statusMessages; private final ConcurrentMap regions = Maps.newConcurrentMap(); - public PushStatusTrackerImp(LinkedBlockingQueue statusMessages) { + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + + public PushStatusTrackerImp(LinkedBlockingQueue statusMessages, TelemetryRuntimeProducer telemetryRuntimeProducer) { _statusMessages = statusMessages; + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); } private synchronized void reset() { @@ -42,12 +52,16 @@ public void handleSseStatus(SSEClient.StatusMessage newStatus) { case FIRST_EVENT: if (SSEClient.StatusMessage.CONNECTED.equals(_sseStatus.get())) { _statusMessages.offer(PushManager.Status.STREAMING_READY); + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.CONNECTION_ESTABLISHED.getType(), + 0l, System.currentTimeMillis())); } case CONNECTED: _sseStatus.compareAndSet(SSEClient.StatusMessage.INITIALIZATION_IN_PROGRESS, SSEClient.StatusMessage.CONNECTED); break; case RETRYABLE_ERROR: - if (_sseStatus.compareAndSet(SSEClient.StatusMessage.CONNECTED, SSEClient.StatusMessage.RETRYABLE_ERROR)) { + if (_sseStatus.compareAndSet(SSEClient.StatusMessage.CONNECTED, SSEClient.StatusMessage.RETRYABLE_ERROR) + || _sseStatus.compareAndSet(SSEClient.StatusMessage.INITIALIZATION_IN_PROGRESS, SSEClient.StatusMessage.RETRYABLE_ERROR) + || SSEClient.StatusMessage.RETRYABLE_ERROR == _sseStatus.get()) { _statusMessages.offer(PushManager.Status.STREAMING_BACKOFF); } break; @@ -85,12 +99,16 @@ public void handleIncomingControlEvent(ControlNotification controlNotification) } break; case STREAMING_PAUSED: + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.STREAMING_STATUS.getType(), + StreamEventsEnum.StreamingStatusValues.STREAMING_PAUSED.getValue(), System.currentTimeMillis())); if (_backendStatus.compareAndSet(ControlType.STREAMING_RESUMED, ControlType.STREAMING_PAUSED) && _publishersOnline.get()) { // If there are no publishers online, the STREAMING_DOWN message should have already been sent _statusMessages.offer(PushManager.Status.STREAMING_DOWN); } break; case STREAMING_DISABLED: + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.STREAMING_STATUS.getType(), + StreamEventsEnum.StreamingStatusValues.STREAMING_DISABLED.getValue(), System.currentTimeMillis())); _backendStatus.set(ControlType.STREAMING_DISABLED); _statusMessages.offer(PushManager.Status.STREAMING_OFF); break; @@ -102,6 +120,7 @@ public void handleIncomingOccupancyEvent(OccupancyNotification occupancyNotifica _log.debug(String.format("handleIncomingOccupancyEvent: publishers=%d", occupancyNotification.getMetrics().getPublishers())); int publishers = occupancyNotification.getMetrics().getPublishers(); + recordTelemetryOcuppancy(occupancyNotification, publishers); regions.put(occupancyNotification.getChannel(), publishers); boolean isPublishers = isPublishers(); if (!isPublishers && _publishersOnline.compareAndSet(true, false) && _backendStatus.get().equals(ControlType.STREAMING_RESUMED)) { @@ -114,7 +133,8 @@ public void handleIncomingOccupancyEvent(OccupancyNotification occupancyNotifica @Override public void handleIncomingAblyError(ErrorNotification notification) { _log.debug(String.format("handleIncomingAblyError: %s", notification.getMessage())); - + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.ABLY_ERROR.getType(), notification.getCode(), + System.currentTimeMillis())); if (_backendStatus.get().equals(ControlType.STREAMING_DISABLED)) { return; // Ignore } @@ -145,4 +165,16 @@ private boolean isPublishers() { } return false; } + + private void recordTelemetryOcuppancy(OccupancyNotification occupancyNotification, int publishers) { + if (CONTROL_PRI_CHANNEL.equals(occupancyNotification.getChannel())) { + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.OCCUPANCY_PRI.getType(), + publishers, System.currentTimeMillis())); + } + else if (CONTROL_SEC_CHANNEL.equals(occupancyNotification.getChannel())){ + _telemetryRuntimeProducer.recordStreamingEvents(new StreamingEvent(StreamEventsEnum.OCCUPANCY_SEC.getType(), + publishers, System.currentTimeMillis())); + } + + } } \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/client/SSEClient.java b/client/src/main/java/io/split/engine/sse/client/SSEClient.java index 6f072400..24af0892 100644 --- a/client/src/main/java/io/split/engine/sse/client/SSEClient.java +++ b/client/src/main/java/io/split/engine/sse/client/SSEClient.java @@ -1,10 +1,15 @@ package io.split.engine.sse.client; import com.google.common.base.Strings; -import com.google.common.util.concurrent.ThreadFactoryBuilder; +import io.split.client.RequestDecorator; +import io.split.client.utils.ApacheRequestDecorator; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.StreamEventsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.io.CloseMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -17,13 +22,16 @@ import java.net.URI; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.SplitExecutorFactory.buildExecutorService; public class SSEClient { @@ -44,12 +52,9 @@ private enum ConnectionState { private final static String SOCKET_CLOSED_MESSAGE = "Socket closed"; private final static String KEEP_ALIVE_PAYLOAD = ":keepalive\n"; private final static long CONNECT_TIMEOUT = 30000; + private static final Lock lock = new ReentrantLock(); private static final Logger _log = LoggerFactory.getLogger(SSEClient.class); - - private final ExecutorService _connectionExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("SPLIT-SSEConnection-%d") - .build()); + private final ExecutorService _connectionExecutor; private final CloseableHttpClient _client; private final Function _eventCallback; private final Function _statusCallback; @@ -57,65 +62,87 @@ private enum ConnectionState { private final AtomicReference _ongoingResponse = new AtomicReference<>(); private final AtomicReference _ongoingRequest = new AtomicReference<>(); private AtomicBoolean _forcedStop; + private final RequestDecorator _requestDecorator; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; public SSEClient(Function eventCallback, - Function statusCallback, - CloseableHttpClient client) { + Function statusCallback, + CloseableHttpClient client, + TelemetryRuntimeProducer telemetryRuntimeProducer, + ThreadFactory threadFactory, + RequestDecorator requestDecorator) { _eventCallback = eventCallback; _statusCallback = statusCallback; _client = client; _forcedStop = new AtomicBoolean(); + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + _connectionExecutor = buildExecutorService(threadFactory, "SPLIT-SSEConnection-%d"); + _requestDecorator = requestDecorator; } - public synchronized boolean open(URI uri) { - if (isOpen()) { - _log.info("SSEClient already open."); - return false; - } + public boolean open(URI uri) { + try { + lock.lock(); + if (isOpen()) { + _log.info("SSEClient already open."); + return false; + } - _statusCallback.apply(StatusMessage.INITIALIZATION_IN_PROGRESS); + _statusCallback.apply(StatusMessage.INITIALIZATION_IN_PROGRESS); - CountDownLatch signal = new CountDownLatch(1); - _connectionExecutor.submit(() -> connectAndLoop(uri, signal)); - try { - if (!signal.await(CONNECT_TIMEOUT, TimeUnit.SECONDS)) { + CountDownLatch signal = new CountDownLatch(1); + _connectionExecutor.submit(() -> connectAndLoop(uri, signal)); + try { + if (!signal.await(CONNECT_TIMEOUT, TimeUnit.SECONDS)) { + return false; + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + if (e.getMessage() == null) { + _log.info("The thread was interrupted while opening SSEClient"); + return false; + } + _log.info(e.getMessage()); return false; } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - _log.info(e.getMessage()); - return false; + return isOpen(); + } finally { + lock.unlock(); } - return isOpen(); } public boolean isOpen() { return (ConnectionState.OPEN.equals(_state.get())); } - public synchronized void close() { - _forcedStop.set(true); - if (_state.compareAndSet(ConnectionState.OPEN, ConnectionState.CLOSED)) { - if (_ongoingResponse.get() != null) { - try { + public void close() { + _log.debug("closing SSE client"); + try { + lock.lock(); + if (_state.compareAndSet(ConnectionState.OPEN, ConnectionState.CLOSED)) { + _forcedStop.set(true); + if (_ongoingResponse.get() != null) { _ongoingRequest.get().abort(); - _ongoingResponse.get().close(); - } catch (IOException e) { - _log.debug(String.format("SSEClient close forced: %s", e.getMessage())); + _ongoingResponse.get().close(CloseMode.IMMEDIATE); } } + } catch (Exception e) { + _log.debug("Exception in closing SSE client: " + e.getMessage()); + } finally { + lock.unlock(); } } private void connectAndLoop(URI uri, CountDownLatch signal) { checkNotNull(uri); checkNotNull(signal); - if (!establishConnection(uri, signal)) { - _statusCallback.apply(StatusMessage.NONRETRYABLE_ERROR); - return; - } try { + if (!establishConnection(uri, signal)) { + _statusCallback.apply(StatusMessage.RETRYABLE_ERROR); + return; + } + final InputStream stream = _ongoingResponse.get().getEntity().getContent(); final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); @@ -126,29 +153,48 @@ private void connectAndLoop(URI uri, CountDownLatch signal) { _log.debug(exc.getMessage()); if (SOCKET_CLOSED_MESSAGE.equals(exc.getMessage())) { // Connection closed by us _statusCallback.apply(StatusMessage.FORCED_STOP); + _telemetryRuntimeProducer.recordStreamingEvents( + new StreamingEvent(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), + StreamEventsEnum.SseConnectionErrorValues.REQUESTED_CONNECTION_ERROR.getValue(), + System.currentTimeMillis())); return; } // Connection closed by server _statusCallback.apply(StatusMessage.RETRYABLE_ERROR); + _telemetryRuntimeProducer + .recordStreamingEvents(new StreamingEvent(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), + StreamEventsEnum.SseConnectionErrorValues.NON_REQUESTED_CONNECTION_ERROR.getValue(), + System.currentTimeMillis())); return; } catch (IOException exc) { // Other type of connection error - if(!_forcedStop.get()) { + if (!_forcedStop.get()) { _log.debug(String.format("SSE connection ended abruptly: %s. Retying", exc.getMessage())); + _telemetryRuntimeProducer.recordStreamingEvents( + new StreamingEvent(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), + StreamEventsEnum.SseConnectionErrorValues.NON_REQUESTED_CONNECTION_ERROR.getValue(), + System.currentTimeMillis())); _statusCallback.apply(StatusMessage.RETRYABLE_ERROR); return; } + + _telemetryRuntimeProducer + .recordStreamingEvents(new StreamingEvent(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), + StreamEventsEnum.SseConnectionErrorValues.REQUESTED_CONNECTION_ERROR.getValue(), + System.currentTimeMillis())); + _statusCallback.apply(StatusMessage.FORCED_STOP); + return; } } } catch (Exception e) { // Any other error non related to the connection disables streaming altogether + _log.debug(String.format("SSE connection exception: %s", e.getMessage())); + _telemetryRuntimeProducer + .recordStreamingEvents(new StreamingEvent(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), + StreamEventsEnum.SseConnectionErrorValues.NON_REQUESTED_CONNECTION_ERROR.getValue(), + System.currentTimeMillis())); _log.warn(e.getMessage(), e); _statusCallback.apply(StatusMessage.NONRETRYABLE_ERROR); } finally { - try { - _ongoingResponse.get().close(); - } catch (IOException e) { - _log.debug(e.getMessage()); - } - + _ongoingResponse.get().close(CloseMode.IMMEDIATE); _state.set(ConnectionState.CLOSED); _log.debug("SSEClient finished."); _forcedStop.set(false); @@ -156,18 +202,20 @@ private void connectAndLoop(URI uri, CountDownLatch signal) { } private boolean establishConnection(URI uri, CountDownLatch signal) { - - _ongoingRequest.set(new HttpGet(uri)); - + HttpGet request = new HttpGet(uri); + request = (HttpGet) ApacheRequestDecorator.decorate(request, _requestDecorator); + _ongoingRequest.set(request); try { _ongoingResponse.set(_client.execute(_ongoingRequest.get())); if (_ongoingResponse.get().getCode() != 200) { + _log.error(String.format("Establishing connection, code error: %s. The url is %s", + _ongoingResponse.get().getCode(), uri.toURL())); return false; } _state.set(ConnectionState.OPEN); _statusCallback.apply(StatusMessage.CONNECTED); } catch (IOException exc) { - _log.error(String.format("Error establishConnection: %s", exc)); + _log.error(String.format("Error establishConnection to %s", uri), exc); return false; } finally { signal.countDown(); @@ -198,4 +246,4 @@ private void handleMessage(String message) { RawEvent e = RawEvent.fromString(message); _eventCallback.apply(e); } -} \ No newline at end of file +} diff --git a/client/src/main/java/io/split/engine/sse/dtos/CommonChangeNotification.java b/client/src/main/java/io/split/engine/sse/dtos/CommonChangeNotification.java new file mode 100644 index 00000000..f5d335ae --- /dev/null +++ b/client/src/main/java/io/split/engine/sse/dtos/CommonChangeNotification.java @@ -0,0 +1,88 @@ +package io.split.engine.sse.dtos; + +import io.split.client.utils.Json; +import io.split.engine.segments.SegmentSynchronizationTaskImp; +import io.split.engine.sse.NotificationProcessor; +import io.split.engine.sse.enums.CompressType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.zip.DataFormatException; + +import static io.split.engine.sse.utils.DecompressionUtil.gZipDecompress; +import static io.split.engine.sse.utils.DecompressionUtil.zLibDecompress; + +public class CommonChangeNotification extends IncomingNotification { + private static final Logger _log = LoggerFactory.getLogger(SegmentSynchronizationTaskImp.class); + private final long changeNumber; + private long previousChangeNumber; + private CompressType compressType; + private Y definition; + private Class _definitionClass; + + public CommonChangeNotification(GenericNotificationData genericNotificationData, + Class definitionClass) { + super(genericNotificationData.getType(), genericNotificationData.getChannel()); + changeNumber = genericNotificationData.getChangeNumber(); + _definitionClass = definitionClass; + + if(genericNotificationData.getPreviousChangeNumber() != null) { + previousChangeNumber = genericNotificationData.getPreviousChangeNumber(); + } + compressType = CompressType.from(genericNotificationData.getCompressType()); + if (compressType == null || genericNotificationData.getDefinition() == null) { + return; + } + try { + byte[] decodedBytes = Base64.getDecoder().decode(genericNotificationData.getDefinition()); + switch (compressType) { + case GZIP: + decodedBytes = gZipDecompress(decodedBytes); + break; + case ZLIB: + decodedBytes = zLibDecompress(decodedBytes); + break; + } + + updateDefinition(decodedBytes); + } catch (UnsupportedEncodingException | IllegalArgumentException e) { + _log.warn("Could not decode base64 data in definition", e); + } catch (DataFormatException d) { + _log.warn("Could not decompress definition with zlib algorithm", d); + } catch (IOException i) { + _log.warn("Could not decompress definition with gzip algorithm", i); + } + } + + public long getChangeNumber() { + return changeNumber; + } + public long getPreviousChangeNumber() { + return previousChangeNumber; + } + public CompressType getCompressType() { + return compressType; + } + + public Y getDefinition() { + return definition; + } + + @Override + public void handler(NotificationProcessor notificationProcessor) { + notificationProcessor.processUpdates(this); + } + + @Override + public String toString() { + return String.format("Type: %s; Channel: %s; ChangeNumber: %s", getType(), getChannel(), getChangeNumber()); + } + + private void updateDefinition(byte[] decodedBytes) { + definition = (Y) Json.fromJson(new String(decodedBytes, StandardCharsets.UTF_8), _definitionClass); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/dtos/GenericNotificationData.java b/client/src/main/java/io/split/engine/sse/dtos/GenericNotificationData.java index 416ab837..998434ec 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/GenericNotificationData.java +++ b/client/src/main/java/io/split/engine/sse/dtos/GenericNotificationData.java @@ -1,5 +1,7 @@ package io.split.engine.sse.dtos; +import com.google.gson.annotations.SerializedName; + public class GenericNotificationData { private final Long changeNumber; private final String defaultTreatment; @@ -9,15 +11,24 @@ public class GenericNotificationData { private final String segmentName; private final IncomingNotification.Type type; private String channel; + @SerializedName("pcn") + private Long previousChangeNumber; + @SerializedName("d") + private String featureFlagDefinition; + @SerializedName("c") + private Integer compressType; - public GenericNotificationData (Long changeNumber, + private GenericNotificationData (Long changeNumber, String defaultTreatment, String splitName, ControlType controlType, OccupancyMetrics occupancyMetrics, String segmentName, IncomingNotification.Type type, - String channel) { + String channel, + Long previousChangeNumber, + String data, + Integer compressType) { this.changeNumber = changeNumber; this.defaultTreatment = defaultTreatment; this.splitName = splitName; @@ -26,6 +37,9 @@ public GenericNotificationData (Long changeNumber, this.segmentName = segmentName; this.type = type; this.channel = channel; + this.previousChangeNumber = previousChangeNumber; + this.featureFlagDefinition = data; + this.compressType = compressType; } public long getChangeNumber() { @@ -57,8 +71,100 @@ public IncomingNotification.Type getType() { } public String getChannel() { return channel; } + public Long getPreviousChangeNumber() { + return previousChangeNumber; + } + + public String getDefinition() { + return featureFlagDefinition; + } + + public Integer getCompressType() { + return compressType; + } public void setChannel(String channel) { this.channel = channel; } -} + + public static GenericNotificationData.Builder builder() { + return new GenericNotificationData.Builder(); + } + + public static final class Builder { + private Long changeNumber; + private String defaultTreatment; + private String featureFlagName; + private ControlType controlType; + private OccupancyMetrics metrics; + private String segmentName; + private IncomingNotification.Type type; + private String channel; + private Long previousChangeNumber; + private String featureFlagDefinition; + private Integer compressType; + + public Builder() { + } + + public Builder changeNumber(Long changeNumber) { + this.changeNumber = changeNumber; + return this; + } + + public Builder defaultTreatment(String defaultTreatment) { + this.defaultTreatment = defaultTreatment; + return this; + } + + public Builder featureFlagName(String featureFlagName) { + this.featureFlagName = featureFlagName; + return this; + } + + public Builder controlType(ControlType controlType) { + this.controlType = controlType; + return this; + } + + public Builder metrics(OccupancyMetrics occupancyMetrics) { + this.metrics = occupancyMetrics; + return this; + } + + public Builder segmentName(String segmentName) { + this.segmentName = segmentName; + return this; + } + + public Builder type(IncomingNotification.Type type) { + this.type = type; + return this; + } + + public Builder channel(String channel) { + this.channel = channel; + return this; + } + + public Builder previousChangeNumber(Long previousChangeNumber) { + this.previousChangeNumber = previousChangeNumber; + return this; + } + + public Builder featureFlagDefinition(String featureFlagDefinition) { + this.featureFlagDefinition = featureFlagDefinition; + return this; + } + + public Builder compressType(Integer compressType) { + this.compressType = compressType; + return this; + } + + public GenericNotificationData build() { + return new GenericNotificationData(changeNumber, defaultTreatment, featureFlagName, controlType, metrics, + segmentName, type, channel, previousChangeNumber, featureFlagDefinition, compressType); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/dtos/IncomingNotification.java b/client/src/main/java/io/split/engine/sse/dtos/IncomingNotification.java index aa476e43..ee00bafe 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/IncomingNotification.java +++ b/client/src/main/java/io/split/engine/sse/dtos/IncomingNotification.java @@ -5,6 +5,7 @@ public abstract class IncomingNotification { public enum Type { SPLIT_UPDATE, + RB_SEGMENT_UPDATE, SPLIT_KILL, SEGMENT_UPDATE, CONTROL, diff --git a/client/src/main/java/io/split/engine/sse/dtos/RawAuthResponse.java b/client/src/main/java/io/split/engine/sse/dtos/RawAuthResponse.java index 4e002642..08f21d8a 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/RawAuthResponse.java +++ b/client/src/main/java/io/split/engine/sse/dtos/RawAuthResponse.java @@ -22,7 +22,7 @@ public RawAuthResponse(boolean pushEnabled, String token) { this.pushEnabled = pushEnabled; this.token = token; - if (token != null && token != "") { + if (token != null && !token.isEmpty()) { String tokenDecoded = decodeJwt(); this.jwt = Json.fromJson(tokenDecoded, Jwt.class); } else { @@ -60,4 +60,4 @@ private String addPrefixControlChannels(String channels) { .replace("control_pri", "[?occupancy=metrics.publishers]control_pri") .replace("control_sec", "[?occupancy=metrics.publishers]control_sec"); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/dtos/RawMessageNotification.java b/client/src/main/java/io/split/engine/sse/dtos/RawMessageNotification.java index f39bc8b2..9fc5ad6c 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/RawMessageNotification.java +++ b/client/src/main/java/io/split/engine/sse/dtos/RawMessageNotification.java @@ -1,7 +1,5 @@ package io.split.engine.sse.dtos; -import java.util.Map; - public class RawMessageNotification { private String id; private String clientId; diff --git a/client/src/main/java/io/split/engine/sse/dtos/SegmentChangeNotification.java b/client/src/main/java/io/split/engine/sse/dtos/SegmentChangeNotification.java index de542cfd..7eb2f912 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/SegmentChangeNotification.java +++ b/client/src/main/java/io/split/engine/sse/dtos/SegmentChangeNotification.java @@ -27,6 +27,7 @@ public void handler(NotificationProcessor notificationProcessor) { @Override public String toString() { - return String.format("Type: %s; Channel: %s; ChangeNumber: %s; SegmentName: %s", getType(), getChannel(), getChangeNumber(), getSegmentName()); + return String.format("Type: %s; Channel: %s; ChangeNumber: %s; SegmentName: %s", getType(), getChannel(), getChangeNumber(), + getSegmentName()); } } diff --git a/client/src/main/java/io/split/engine/sse/dtos/SplitChangeNotification.java b/client/src/main/java/io/split/engine/sse/dtos/SplitChangeNotification.java deleted file mode 100644 index 56b8c32c..00000000 --- a/client/src/main/java/io/split/engine/sse/dtos/SplitChangeNotification.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.split.engine.sse.dtos; - -import io.split.engine.sse.NotificationProcessor; - -public class SplitChangeNotification extends IncomingNotification { - private final long changeNumber; - - public SplitChangeNotification(GenericNotificationData genericNotificationData) { - super(Type.SPLIT_UPDATE, genericNotificationData.getChannel()); - this.changeNumber = genericNotificationData.getChangeNumber(); - } - - public long getChangeNumber() { - return changeNumber; - } - - @Override - public void handler(NotificationProcessor notificationProcessor) { - notificationProcessor.processSplitUpdate(getChangeNumber()); - } - - @Override - public String toString() { - return String.format("Type: %s; Channel: %s; ChangeNumber: %s", getType(), getChannel(), getChangeNumber()); - } -} diff --git a/client/src/main/java/io/split/engine/sse/dtos/SplitKillNotification.java b/client/src/main/java/io/split/engine/sse/dtos/SplitKillNotification.java index ed470035..c5a02a66 100644 --- a/client/src/main/java/io/split/engine/sse/dtos/SplitKillNotification.java +++ b/client/src/main/java/io/split/engine/sse/dtos/SplitKillNotification.java @@ -28,11 +28,12 @@ public String getSplitName() { @Override public void handler(NotificationProcessor notificationProcessor) { - notificationProcessor.processSplitKill(getChangeNumber(), getSplitName(), getDefaultTreatment()); + notificationProcessor.processSplitKill(this); } @Override public String toString() { - return String.format("Type: %s; Channel: %s; ChangeNumber: %s; DefaultTreatment: %s; SplitName: %s", getType(), getChannel(), getChangeNumber(), getDefaultTreatment(), getSplitName()); + return String.format("Type: %s; Channel: %s; ChangeNumber: %s; DefaultTreatment: %s; SplitName: %s", getType(), getChannel(), + getChangeNumber(), getDefaultTreatment(), getSplitName()); } -} +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/enums/CompressType.java b/client/src/main/java/io/split/engine/sse/enums/CompressType.java new file mode 100644 index 00000000..ed78a33b --- /dev/null +++ b/client/src/main/java/io/split/engine/sse/enums/CompressType.java @@ -0,0 +1,39 @@ +package io.split.engine.sse.enums; + +import java.util.HashMap; +import java.util.Map; + +public enum CompressType { + NOT_COMPRESSED(0), + GZIP(1), + ZLIB(2); + + private final Integer value; + + CompressType(Integer value) { + this.value = value; + } + + public long getValue() { + return value; + } + + // Mapping compress type to compress type id + private static final Map _map = new HashMap<>(); + static { + for (CompressType compressType : CompressType.values()) + _map.put(compressType.value, compressType); + } + + /** + * Get compress type from value + * @param value value + * @return CompressType + */ + public static CompressType from(Integer value) { + if (value == null || _map.size() <= value){ + return null; + } + return _map.get(value); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/utils/DecompressionUtil.java b/client/src/main/java/io/split/engine/sse/utils/DecompressionUtil.java new file mode 100644 index 00000000..522425e4 --- /dev/null +++ b/client/src/main/java/io/split/engine/sse/utils/DecompressionUtil.java @@ -0,0 +1,42 @@ +package io.split.engine.sse.utils; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.DataFormatException; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; + +public class DecompressionUtil { + + public static byte[] zLibDecompress(byte[] toDecompress) throws DataFormatException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(toDecompress.length); + Inflater decompressor = new Inflater(); + try { + decompressor.setInput(toDecompress); + final byte[] buf = new byte[toDecompress.length]; + while (!decompressor.finished()) { + int count = decompressor.inflate(buf); + byteArrayOutputStream.write(buf, 0, count); + } + } finally { + decompressor.end(); + } + return byteArrayOutputStream.toByteArray(); + } + + public static byte[] gZipDecompress(byte[] toDecompress) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(toDecompress))){ + int res = 0; + byte buf[] = new byte[toDecompress.length]; + while (res >= 0) { + res = gzipInputStream.read(buf, 0, buf.length); + if (res > 0) { + out.write(buf, 0, res); + } + } + } + return out.toByteArray(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/workers/FeatureFlagWorkerImp.java b/client/src/main/java/io/split/engine/sse/workers/FeatureFlagWorkerImp.java new file mode 100644 index 00000000..6001bb22 --- /dev/null +++ b/client/src/main/java/io/split/engine/sse/workers/FeatureFlagWorkerImp.java @@ -0,0 +1,161 @@ +package io.split.engine.sse.workers; + +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.utils.FeatureFlagsToUpdate; +import io.split.client.utils.RuleBasedSegmentsToUpdate; +import io.split.engine.common.Synchronizer; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.experiments.SplitParser; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.SplitKillNotification; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.FeatureFlagProcessor.processFeatureFlagChanges; +import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; + +public class FeatureFlagWorkerImp extends Worker implements FeatureFlagsWorker { + private static final Logger _log = LoggerFactory.getLogger(FeatureFlagWorkerImp.class); + private final Synchronizer _synchronizer; + private final SplitParser _splitParser; + private final RuleBasedSegmentParser _ruleBasedSegmentParser; + private final SplitCacheProducer _splitCacheProducer; + private final RuleBasedSegmentCache _ruleBasedSegmentCache; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + private final FlagSetsFilter _flagSetsFilter; + private final SdkEventsNotifier _notifier; + + public FeatureFlagWorkerImp(Synchronizer synchronizer, SplitParser splitParser, RuleBasedSegmentParser ruleBasedSegmentParser, + SplitCacheProducer splitCacheProducer, + RuleBasedSegmentCache ruleBasedSegmentCache, + TelemetryRuntimeProducer telemetryRuntimeProducer, FlagSetsFilter flagSetsFilter) { + this(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, + telemetryRuntimeProducer, flagSetsFilter, SdkEventsNotifier.NOOP); + } + + public FeatureFlagWorkerImp(Synchronizer synchronizer, SplitParser splitParser, RuleBasedSegmentParser ruleBasedSegmentParser, + SplitCacheProducer splitCacheProducer, + RuleBasedSegmentCache ruleBasedSegmentCache, + TelemetryRuntimeProducer telemetryRuntimeProducer, FlagSetsFilter flagSetsFilter, + SdkEventsNotifier notifier) { + super("Feature flags"); + _notifier = checkNotNull(notifier); + _synchronizer = checkNotNull(synchronizer); + _splitParser = splitParser; + _ruleBasedSegmentParser = ruleBasedSegmentParser; + _splitCacheProducer = splitCacheProducer; + _telemetryRuntimeProducer = telemetryRuntimeProducer; + _flagSetsFilter = flagSetsFilter; + _ruleBasedSegmentCache = ruleBasedSegmentCache; + } + + @Override + public void kill(SplitKillNotification splitKillNotification) { + try { + _synchronizer.localKillSplit(splitKillNotification); + _log.debug(String.format("Kill feature flag: %s, changeNumber: %s, defaultTreatment: %s", splitKillNotification.getSplitName(), + splitKillNotification.getChangeNumber(), splitKillNotification.getDefaultTreatment())); + } catch (Exception ex) { + _log.warn(String.format("Exception on FeatureFlagWorker kill: %s", ex.getMessage())); + } + } + + @Override + protected void executeRefresh(IncomingNotification incomingNotification) { + boolean success; + long changeNumber = 0L; + long changeNumberRBS = 0L; + if (incomingNotification.getType() == IncomingNotification.Type.SPLIT_UPDATE) { + CommonChangeNotification featureFlagChangeNotification = (CommonChangeNotification) incomingNotification; + success = addOrUpdateFeatureFlag(featureFlagChangeNotification); + changeNumber = featureFlagChangeNotification.getChangeNumber(); + } else { + CommonChangeNotification ruleBasedSegmentChangeNotification = (CommonChangeNotification) incomingNotification; + success = addOrUpdateRuleBasedSegment(ruleBasedSegmentChangeNotification); + changeNumberRBS = ruleBasedSegmentChangeNotification.getChangeNumber(); + } + if (!success) + _synchronizer.refreshSplits(changeNumber, changeNumberRBS); + } + + private boolean addOrUpdateRuleBasedSegment(CommonChangeNotification ruleBasedSegmentChangeNotification) { + if (ruleBasedSegmentChangeNotification.getChangeNumber() <= _ruleBasedSegmentCache.getChangeNumber()) { + return true; + } + try { + if (ruleBasedSegmentChangeNotification.getDefinition() != null && + ruleBasedSegmentChangeNotification.getPreviousChangeNumber() == _ruleBasedSegmentCache.getChangeNumber()) { + RuleBasedSegment ruleBasedSegment = (RuleBasedSegment) ruleBasedSegmentChangeNotification.getDefinition(); + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(_ruleBasedSegmentParser, + Collections.singletonList(ruleBasedSegment)); + _ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), ruleBasedSegmentsToUpdate.getToRemove(), + ruleBasedSegmentChangeNotification.getChangeNumber()); + if (!ruleBasedSegmentsToUpdate.getToAdd().isEmpty() || !ruleBasedSegmentsToUpdate.getToRemove().isEmpty()) { + _notifier.notify(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED, + SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, Collections.emptySet())); + } + Set segments = ruleBasedSegmentsToUpdate.getSegments(); + for (String segmentName: segments) { + _synchronizer.forceRefreshSegment(segmentName); + } + // TODO: Add Telemetry once it is spec'd +// _telemetryRuntimeProducer.recordUpdatesFromSSE(UpdatesFromSSEEnum.RULE_BASED_SEGMENTS); + return true; + } + } catch (Exception e) { + _log.warn("Something went wrong processing a Rule based Segment notification", e); + } + return false; + } + private boolean addOrUpdateFeatureFlag(CommonChangeNotification featureFlagChangeNotification) { + if (featureFlagChangeNotification.getChangeNumber() <= _splitCacheProducer.getChangeNumber()) { + return true; + } + try { + if (featureFlagChangeNotification.getDefinition() != null && + featureFlagChangeNotification.getPreviousChangeNumber() == _splitCacheProducer.getChangeNumber()) { + Split featureFlag = (Split) featureFlagChangeNotification.getDefinition(); + FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(_splitParser, Collections.singletonList(featureFlag), + _flagSetsFilter); + _splitCacheProducer.update(featureFlagsToUpdate.getToAdd(), featureFlagsToUpdate.getToRemove(), + featureFlagChangeNotification.getChangeNumber()); + Set flagNames = new HashSet<>(featureFlagsToUpdate.getToRemove()); + featureFlagsToUpdate.getToAdd().forEach(f -> flagNames.add(f.feature())); + if (!flagNames.isEmpty()) { + _notifier.notify(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, flagNames)); + } + Set segments = featureFlagsToUpdate.getSegments(); + for (String segmentName: segments) { + _synchronizer.forceRefreshSegment(segmentName); + } + if (featureFlagsToUpdate.getToAdd().stream().count() > 0) { + Set ruleBasedSegments = featureFlagsToUpdate.getToAdd().get(0).getRuleBasedSegmentsNames(); + if (!ruleBasedSegments.isEmpty() && !_ruleBasedSegmentCache.contains(ruleBasedSegments)) { + return false; + } + } + _telemetryRuntimeProducer.recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + return true; + } + } catch (Exception e) { + _log.warn("Something went wrong processing a Feature Flag notification", e); + } + return false; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/workers/FeatureFlagsWorker.java b/client/src/main/java/io/split/engine/sse/workers/FeatureFlagsWorker.java new file mode 100644 index 00000000..b2cc1fbb --- /dev/null +++ b/client/src/main/java/io/split/engine/sse/workers/FeatureFlagsWorker.java @@ -0,0 +1,11 @@ +package io.split.engine.sse.workers; + +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.SplitKillNotification; + +public interface FeatureFlagsWorker { + void addToQueue(IncomingNotification incomingNotification); + void start(); + void stop(); + void kill(SplitKillNotification splitKillNotification); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/engine/sse/workers/SplitsWorker.java b/client/src/main/java/io/split/engine/sse/workers/SplitsWorker.java deleted file mode 100644 index 3664b7cd..00000000 --- a/client/src/main/java/io/split/engine/sse/workers/SplitsWorker.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.split.engine.sse.workers; - -public interface SplitsWorker { - void addToQueue(Long element); - void start(); - void stop(); - void killSplit(long changeNumber, String splitName, String defaultTreatment); -} diff --git a/client/src/main/java/io/split/engine/sse/workers/SplitsWorkerImp.java b/client/src/main/java/io/split/engine/sse/workers/SplitsWorkerImp.java deleted file mode 100644 index 16e155ed..00000000 --- a/client/src/main/java/io/split/engine/sse/workers/SplitsWorkerImp.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.split.engine.sse.workers; - -import io.split.engine.common.Synchronizer; - -import static com.google.common.base.Preconditions.checkNotNull; - -public class SplitsWorkerImp extends Worker implements SplitsWorker { - private final Synchronizer _synchronizer; - - public SplitsWorkerImp(Synchronizer synchronizer) { - super("Splits"); - _synchronizer = checkNotNull(synchronizer); - } - - @Override - public void killSplit(long changeNumber, String splitName, String defaultTreatment) { - try { - _synchronizer.localKillSplit(splitName, defaultTreatment, changeNumber); - _log.debug(String.format("Kill split: %s, changeNumber: %s, defaultTreatment: %s", splitName, changeNumber, defaultTreatment)); - } catch (Exception ex) { - _log.warn(String.format("Exception on SplitWorker killSplit: %s", ex.getMessage())); - } - } - - @Override - protected void executeRefresh(Long changeNumber) { - _synchronizer.refreshSplits(changeNumber); - } -} diff --git a/client/src/main/java/io/split/engine/sse/workers/Worker.java b/client/src/main/java/io/split/engine/sse/workers/Worker.java index d6b8db5d..b5ba84f2 100644 --- a/client/src/main/java/io/split/engine/sse/workers/Worker.java +++ b/client/src/main/java/io/split/engine/sse/workers/Worker.java @@ -22,10 +22,11 @@ public Worker(String workerName) { public void start() { if (_running.compareAndSet(false, true)) { - _log.debug(String.format("%s Worker starting ...", _workerName)); _queue.clear(); _thread = new Thread( this); + _thread.setName(String.format("%s-worker", _workerName)); _thread.start(); + _log.debug(String.format("%s Worker started ...", _workerName)); } else { _log.debug(String.format("%s Worker already running.", _workerName)); return; diff --git a/client/src/main/java/io/split/inputValidation/FallbackTreatmentValidator.java b/client/src/main/java/io/split/inputValidation/FallbackTreatmentValidator.java new file mode 100644 index 00000000..9fafe5ee --- /dev/null +++ b/client/src/main/java/io/split/inputValidation/FallbackTreatmentValidator.java @@ -0,0 +1,67 @@ +package io.split.inputValidation; + +import io.split.client.dtos.FallbackTreatment; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.regex.Pattern; + +import static io.split.inputValidation.SplitNameValidator.isValid; + +public class FallbackTreatmentValidator { + private static final Logger _log = LoggerFactory.getLogger(FallbackTreatmentValidator.class); + private static final Pattern TREATMENT_MATCHER = Pattern.compile("^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$"); + private static final int MAX_LENGTH = 100; + + public static String isValidTreatment(String name, String method) { + if (name == null) { + _log.error(String.format("%s: you passed a null treatment, fallback treatment must be a non-empty string", method)); + return null; + } + + if (name.isEmpty()) { + _log.error(String.format("%s: you passed an empty treatment, fallback treatment must be a non-empty string", method)); + return null; + } + + String trimmed = name.trim(); + if (!trimmed.equals(name)) { + _log.warn(String.format("%s: fallback treatment %s has extra whitespace, trimming", method, name)); + name = trimmed; + } + + if (name.length() > MAX_LENGTH) { + return null; + } + + if (!TREATMENT_MATCHER.matcher(name).find()) { + _log.error(String.format("%s: you passed %s, treatment must adhere to the regular expression " + + "^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$", method, name)); + return null; + } + + return name; + } + + public static Map isValidByFlagTreatment(Map byFlagTreatment, String method) { + Map result = new HashMap<>(); + for (Map.Entry entry : byFlagTreatment.entrySet()) { + Optional featureName = isValid(entry.getKey(), method); + if (featureName.equals(Optional.empty()) || !featureName.isPresent()) { + continue; + } + + FallbackTreatment fallbackTreatment = entry.getValue(); + String treatment = isValidTreatment(fallbackTreatment.getTreatment(), method); + if (treatment != null) { + result.put(featureName.get(), new FallbackTreatment(treatment, fallbackTreatment.getConfig())); + } + } + + return result; + } +} diff --git a/client/src/main/java/io/split/inputValidation/FlagSetsValidResult.java b/client/src/main/java/io/split/inputValidation/FlagSetsValidResult.java new file mode 100644 index 00000000..a17cb7b3 --- /dev/null +++ b/client/src/main/java/io/split/inputValidation/FlagSetsValidResult.java @@ -0,0 +1,21 @@ +package io.split.inputValidation; + +import java.util.HashSet; + +public class FlagSetsValidResult { + private final Boolean _valid; + private final HashSet _flagSets; + + public FlagSetsValidResult(Boolean valid, HashSet flagSets) { + _valid = valid; + _flagSets = flagSets; + } + + public Boolean getValid() { + return _valid; + } + + public HashSet getFlagSets() { + return _flagSets; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/inputValidation/FlagSetsValidator.java b/client/src/main/java/io/split/inputValidation/FlagSetsValidator.java new file mode 100644 index 00000000..6b6df2d8 --- /dev/null +++ b/client/src/main/java/io/split/inputValidation/FlagSetsValidator.java @@ -0,0 +1,44 @@ +package io.split.inputValidation; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Set; +import java.util.TreeSet; +import java.util.regex.Pattern; + +public final class FlagSetsValidator { + + private static final String FLAG_SET_REGEX = "^[a-z0-9][_a-z0-9]{0,49}$"; + private static final Logger _log = LoggerFactory.getLogger(FlagSetsValidator.class); + + private FlagSetsValidator() { + throw new IllegalStateException("Utility class"); + } + + public static Set cleanup(List flagSets) { + TreeSet cleanFlagSets = new TreeSet<>(); + if (flagSets == null || flagSets.isEmpty()) { + return cleanFlagSets; + } + for (String flagSet: flagSets) { + if(!flagSet.equals(flagSet.toLowerCase())) { + _log.warn(String.format("Flag Set name %s should be all lowercase - converting string to lowercase", flagSet)); + flagSet = flagSet.toLowerCase(); + } + if (!flagSet.equals(flagSet.trim())) { + _log.warn(String.format("Flag Set name %s has extra whitespace, trimming", flagSet)); + flagSet = flagSet.trim(); + } + if (!Pattern.matches(FLAG_SET_REGEX, flagSet)) { + _log.warn(String.format("you passed %s, Flag Set must adhere to the regular expressions %s. This means a Flag Set must be " + + "start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.", + flagSet, FLAG_SET_REGEX, flagSet)); + continue; + } + cleanFlagSets.add(flagSet); + } + return cleanFlagSets; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/inputValidation/ImpressionPropertiesValidator.java b/client/src/main/java/io/split/inputValidation/ImpressionPropertiesValidator.java new file mode 100644 index 00000000..3fca1163 --- /dev/null +++ b/client/src/main/java/io/split/inputValidation/ImpressionPropertiesValidator.java @@ -0,0 +1,21 @@ +package io.split.inputValidation; + +import java.util.Map; + +public class ImpressionPropertiesValidator { + ImpressionPropertiesValidator() { + throw new IllegalStateException("Utility class"); + } + + public static ImpressionPropertiesValidatorResult propertiesAreValid(Map properties) { + EventsValidator.EventValidatorResult result = EventsValidator.propertiesAreValid(properties); + return new ImpressionPropertiesValidatorResult(result.getSuccess(), result.getEventSize(), result.getValue()); + } + + public static class ImpressionPropertiesValidatorResult extends EventsValidator.EventValidatorResult { + public ImpressionPropertiesValidatorResult(boolean success, int eventSize, Map value) { + super(success, eventSize, value); + } + } +} + diff --git a/client/src/main/java/io/split/inputValidation/SplitNameValidator.java b/client/src/main/java/io/split/inputValidation/SplitNameValidator.java index 06f00b72..f138f51c 100644 --- a/client/src/main/java/io/split/inputValidation/SplitNameValidator.java +++ b/client/src/main/java/io/split/inputValidation/SplitNameValidator.java @@ -3,28 +3,51 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.regex.Pattern; +import java.util.stream.Collectors; public class SplitNameValidator { private static final Logger _log = LoggerFactory.getLogger(SplitNameValidator.class); + private static final int MAX_LENGTH = 100; + private static final Pattern NAME_MATCHER = Pattern.compile("^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$"); public static Optional isValid(String name, String method) { if (name == null) { - _log.error(String.format("%s: you passed a null split name, split name must be a non-empty string", method)); + _log.error(String.format("%s: you passed a null feature flag name, feature flag name must be a non-empty string", method)); return Optional.empty(); } if (name.isEmpty()) { - _log.error(String.format("%s: you passed an empty split name, split name must be a non-empty string", method)); + _log.error(String.format("%s: you passed an empty feature flag name, feature flag name must be a non-empty string", method)); return Optional.empty(); } String trimmed = name.trim(); if (!trimmed.equals(name)) { - _log.warn(String.format("%s: split name %s has extra whitespace, trimming", method, name)); + _log.warn(String.format("%s: feature flag name %s has extra whitespace, trimming", method, name)); name = trimmed; } + if (name.length() > MAX_LENGTH) { + return Optional.empty(); + } + + if (!NAME_MATCHER.matcher(name).find()) { + _log.error(String.format("%s: you passed %s, feature flag name must adhere to the regular expression " + + "^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$", method, name)); + return Optional.empty(); + } + return Optional.of(name); } + + public static List areValid(List featureFlags, String method) { + return featureFlags.stream().distinct() + .map(s -> isValid(s, method).orElse(null)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } } diff --git a/client/src/main/java/io/split/inputValidation/TrafficTypeValidator.java b/client/src/main/java/io/split/inputValidation/TrafficTypeValidator.java index 4fc5056a..6533011a 100644 --- a/client/src/main/java/io/split/inputValidation/TrafficTypeValidator.java +++ b/client/src/main/java/io/split/inputValidation/TrafficTypeValidator.java @@ -1,6 +1,6 @@ package io.split.inputValidation; -import io.split.cache.SplitCache; +import io.split.storages.SplitCacheConsumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,7 +9,7 @@ public class TrafficTypeValidator { private static final Logger _log = LoggerFactory.getLogger(TrafficTypeValidator.class); - public static Optional isValid(String trafficTypeName, SplitCache splitCache, String method) { + public static Optional isValid(String trafficTypeName, SplitCacheConsumer splitCacheConsumer, String method) { if (trafficTypeName == null) { _log.error(String.format("%s: you passed a null trafficTypeName, trafficTypeName must be a non-empty string", method)); return Optional.empty(); @@ -25,9 +25,9 @@ public static Optional isValid(String trafficTypeName, SplitCache splitC trafficTypeName = trafficTypeName.toLowerCase(); } - if (!splitCache.trafficTypeExists(trafficTypeName)) { - _log.warn(String.format("%s: Traffic Type %s does not have any corresponding Splits in this environment, " + - "make sure you’re tracking your events to a valid traffic type defined in the Split console.", method, trafficTypeName)); + if (!splitCacheConsumer.trafficTypeExists(trafficTypeName)) { + _log.warn(String.format("%s: Traffic Type %s does not have any corresponding Feature flags in this environment, " + + "make sure you’re tracking your events to a valid traffic type defined in the Split user interface.", method, trafficTypeName)); } return Optional.of(trafficTypeName); diff --git a/client/src/main/java/io/split/integrations/IntegrationsConfig.java b/client/src/main/java/io/split/integrations/IntegrationsConfig.java index b13f6f11..1a0fa484 100644 --- a/client/src/main/java/io/split/integrations/IntegrationsConfig.java +++ b/client/src/main/java/io/split/integrations/IntegrationsConfig.java @@ -51,10 +51,14 @@ public Builder() { } public Builder impressionsListener(ImpressionListener listener, int queueSize) { + return impressionsListener(listener, queueSize, Execution.ASYNC); + } + + public Builder impressionsListener(ImpressionListener listener, int queueSize, Execution executionType) { if (queueSize <= 0) { throw new IllegalArgumentException("An ImpressionListener was provided, but its capacity was non-positive: " + queueSize); } - _listeners.add(new ImpressionListenerWithMeta(listener, Execution.ASYNC, queueSize)); + _listeners.add(new ImpressionListenerWithMeta(listener, executionType, queueSize)); return this; } diff --git a/client/src/main/java/io/split/service/CustomHttpModule.java b/client/src/main/java/io/split/service/CustomHttpModule.java new file mode 100644 index 00000000..001648fb --- /dev/null +++ b/client/src/main/java/io/split/service/CustomHttpModule.java @@ -0,0 +1,11 @@ +package io.split.service; + +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; + +import java.io.IOException; + +public interface CustomHttpModule { + public SplitHttpClient createClient(String apiToken, SDKMetadata sdkMetadata, RequestDecorator decorator) + throws IOException; +} diff --git a/client/src/main/java/io/split/service/HttpPostImp.java b/client/src/main/java/io/split/service/HttpPostImp.java new file mode 100644 index 00000000..b33bf210 --- /dev/null +++ b/client/src/main/java/io/split/service/HttpPostImp.java @@ -0,0 +1,48 @@ +package io.split.service; + +import io.split.client.dtos.SplitHttpResponse; +import io.split.client.utils.Json; +import io.split.telemetry.domain.enums.HttpParamsWrapper; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.apache.hc.core5.http.HttpStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class HttpPostImp { + private static final Logger _logger = LoggerFactory.getLogger(HttpPostImp.class); + private SplitHttpClient _client; + private final TelemetryRuntimeProducer _telemetryRuntimeProducer; + + public HttpPostImp(SplitHttpClient client, TelemetryRuntimeProducer telemetryRuntimeProducer) { + _client = client; + _telemetryRuntimeProducer = checkNotNull(telemetryRuntimeProducer); + } + + public void post(URI uri, Object object, String posted, HttpParamsWrapper httpParamsWrapper) { + long initTime = System.currentTimeMillis(); + + try { + Map> headers = new HashMap<>(); + headers.put("Content-Type", Collections.singletonList("application/json")); + SplitHttpResponse response = _client.post(uri, Json.toJson(object), headers); + if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + _telemetryRuntimeProducer.recordSyncError(httpParamsWrapper.getResourceEnum(), response.statusCode()); + return; + } + _telemetryRuntimeProducer.recordSyncLatency(httpParamsWrapper.getHttpLatenciesEnum(), + System.currentTimeMillis() - initTime); + _telemetryRuntimeProducer.recordSuccessfulSync(httpParamsWrapper.getLastSynchronizationRecordsEnum(), + System.currentTimeMillis()); + } catch (Throwable t) { + _logger.warn("Exception when posting " + posted + object, t); + } + } +} diff --git a/client/src/main/java/io/split/service/SplitHttpClient.java b/client/src/main/java/io/split/service/SplitHttpClient.java new file mode 100644 index 00000000..899fcf56 --- /dev/null +++ b/client/src/main/java/io/split/service/SplitHttpClient.java @@ -0,0 +1,33 @@ +package io.split.service; + +import io.split.engine.common.FetchOptions; +import io.split.client.dtos.SplitHttpResponse; + +import java.io.Closeable; +import java.io.IOException; +import java.net.URI; +import java.util.List; +import java.util.Map; + +public interface SplitHttpClient extends Closeable { + /** + * Wrapper for HTTP get method + * + * @param uri the URL to be used + * @param options The FetchOptions object that contains headers. + * @return The response structure SplitHttpResponse + */ + public SplitHttpResponse get(URI uri, FetchOptions options, Map> additionalHeaders); + + /** + * Wrapper for HTTP post method + * + * @param uri the URL to be used + * @param entity HttpEntity object that has The body load + * @param additionalHeaders Any additional headers to be added. + * @return The response structure SplitHttpResponse + */ + public SplitHttpResponse post(URI uri, + String entity, + Map> additionalHeaders) throws IOException; +} diff --git a/client/src/main/java/io/split/service/SplitHttpClientImpl.java b/client/src/main/java/io/split/service/SplitHttpClientImpl.java new file mode 100644 index 00000000..7d093977 --- /dev/null +++ b/client/src/main/java/io/split/service/SplitHttpClientImpl.java @@ -0,0 +1,171 @@ +package io.split.service; + +import io.split.client.RequestDecorator; +import io.split.client.utils.ApacheRequestDecorator; +import io.split.client.utils.SDKMetadata; +import io.split.client.utils.Utils; +import io.split.engine.common.FetchOptions; +import io.split.client.dtos.SplitHttpResponse; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.HttpEntities; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public final class SplitHttpClientImpl implements SplitHttpClient { + + private static final Logger _log = LoggerFactory.getLogger(SplitHttpClient.class); + private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control"; + private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache"; + private static final String HEADER_API_KEY = "Authorization"; + private static final String HEADER_CLIENT_KEY = "SplitSDKClientKey"; + private static final String HEADER_CLIENT_MACHINE_NAME = "SplitSDKMachineName"; + private static final String HEADER_CLIENT_MACHINE_IP = "SplitSDKMachineIP"; + private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion"; + + private final CloseableHttpClient _client; + private final RequestDecorator _requestDecorator; + private final String _apikey; + private final SDKMetadata _metadata; + + public static SplitHttpClientImpl create(CloseableHttpClient client, + RequestDecorator requestDecorator, + String apikey, + SDKMetadata metadata) throws URISyntaxException { + return new SplitHttpClientImpl(client, requestDecorator, apikey, metadata); + } + + private SplitHttpClientImpl(CloseableHttpClient client, + RequestDecorator requestDecorator, + String apikey, + SDKMetadata metadata) { + _client = client; + _requestDecorator = requestDecorator; + _apikey = apikey; + _metadata = metadata; + } + + public SplitHttpResponse get(URI uri, FetchOptions options, Map> additionalHeaders) { + CloseableHttpResponse response = null; + + try { + HttpGet request = new HttpGet(uri); + setBasicHeaders(request); + if (additionalHeaders != null) { + for (Map.Entry> entry : additionalHeaders.entrySet()) { + for (String value : entry.getValue()) { + request.addHeader(entry.getKey(), value); + } + } + } + if (options.cacheControlHeadersEnabled()) { + request.setHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE); + } + + request = (HttpGet) ApacheRequestDecorator.decorate(request, _requestDecorator); + + response = _client.execute(request); + + if (_log.isDebugEnabled()) { + _log.debug(String.format("[%s] %s. Status code: %s", request.getMethod(), uri.toURL(), + response.getCode())); + } + + String statusMessage = ""; + int code = response.getCode(); + if (code < HttpStatus.SC_OK || code >= HttpStatus.SC_MULTIPLE_CHOICES) { + statusMessage = response.getReasonPhrase(); + _log.warn(String.format("Response status was: %s. Reason: %s", code, statusMessage)); + } + + String body = extractBodyFromResponse(response); + + return new SplitHttpResponse(code, + statusMessage, + body, + Arrays.stream(response.getHeaders()).map( + h -> new SplitHttpResponse.Header(h.getName(), Collections.singletonList(h.getValue()))) + .collect(Collectors.toList())); + } catch (Exception e) { + throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e); + } finally { + Utils.forceClose(response); + } + } + + private String extractBodyFromResponse(CloseableHttpResponse response) { + String body = ""; + try { + body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); + } catch (Exception e) { + _log.warn("Error parsing Response.body", e); + } + return body; + } + + public SplitHttpResponse post(URI uri, String body, Map> additionalHeaders) + throws IOException { + + CloseableHttpResponse response = null; + try { + HttpPost request = new HttpPost(uri); + setBasicHeaders(request); + if (additionalHeaders != null) { + for (Map.Entry> entry : additionalHeaders.entrySet()) { + for (String value : entry.getValue()) { + request.addHeader(entry.getKey(), value); + } + } + } + request.setEntity(HttpEntities.create(body, ContentType.APPLICATION_JSON)); + request = (HttpPost) ApacheRequestDecorator.decorate(request, _requestDecorator); + + response = _client.execute(request); + + String statusMessage = ""; + if (response.getCode() < HttpStatus.SC_OK || response.getCode() >= HttpStatus.SC_MULTIPLE_CHOICES) { + statusMessage = response.getReasonPhrase(); + _log.warn(String.format("Response status was: %s. Reason: %s", response.getCode(), + response.getReasonPhrase())); + } + return new SplitHttpResponse(response.getCode(), statusMessage, "", + Arrays.stream(response.getHeaders()).map( + h -> new SplitHttpResponse.Header(h.getName(), Collections.singletonList(h.getValue()))) + .collect(Collectors.toList())); + } catch (Exception e) { + throw new IOException(String.format("Problem in http post operation: %s", e), e); + } finally { + Utils.forceClose(response); + } + } + + private void setBasicHeaders(HttpRequest request) { + request.setHeader(HEADER_API_KEY, "Bearer " + _apikey); + request.setHeader(HEADER_CLIENT_VERSION, _metadata.getSdkVersion()); + request.setHeader(HEADER_CLIENT_MACHINE_IP, _metadata.getMachineIp()); + request.setHeader(HEADER_CLIENT_MACHINE_NAME, _metadata.getMachineName()); + request.setHeader(HEADER_CLIENT_KEY, _apikey.length() > 4 + ? _apikey.substring(_apikey.length() - 4) + : _apikey); + } + + @Override + public void close() throws IOException { + _client.close(); + } +} diff --git a/client/src/main/java/io/split/storages/RuleBasedSegmentCache.java b/client/src/main/java/io/split/storages/RuleBasedSegmentCache.java new file mode 100644 index 00000000..5ba55b81 --- /dev/null +++ b/client/src/main/java/io/split/storages/RuleBasedSegmentCache.java @@ -0,0 +1,4 @@ +package io.split.storages; + +public interface RuleBasedSegmentCache extends RuleBasedSegmentCacheConsumer, RuleBasedSegmentCacheProducer { +} diff --git a/client/src/main/java/io/split/storages/RuleBasedSegmentCacheConsumer.java b/client/src/main/java/io/split/storages/RuleBasedSegmentCacheConsumer.java new file mode 100644 index 00000000..348159dd --- /dev/null +++ b/client/src/main/java/io/split/storages/RuleBasedSegmentCacheConsumer.java @@ -0,0 +1,16 @@ +package io.split.storages; + +import io.split.engine.experiments.ParsedRuleBasedSegment; + +import java.util.Collection; +import java.util.List; +import java.util.Set; + +public interface RuleBasedSegmentCacheConsumer { + ParsedRuleBasedSegment get(String name); + Collection getAll(); + List ruleBasedSegmentNames(); + boolean contains(Set ruleBasedSegmentNames); + long getChangeNumber(); + Set getSegments(); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/RuleBasedSegmentCacheProducer.java b/client/src/main/java/io/split/storages/RuleBasedSegmentCacheProducer.java new file mode 100644 index 00000000..a87d38a6 --- /dev/null +++ b/client/src/main/java/io/split/storages/RuleBasedSegmentCacheProducer.java @@ -0,0 +1,13 @@ +package io.split.storages; + +import io.split.engine.experiments.ParsedRuleBasedSegment; + +import java.util.List; + +public interface RuleBasedSegmentCacheProducer { + boolean remove(String name); + void setChangeNumber(long changeNumber); + long getChangeNumber(); + void update(List toAdd, List toRemove, long changeNumber); + void clear(); +} diff --git a/client/src/main/java/io/split/storages/SegmentCache.java b/client/src/main/java/io/split/storages/SegmentCache.java new file mode 100644 index 00000000..04e15c40 --- /dev/null +++ b/client/src/main/java/io/split/storages/SegmentCache.java @@ -0,0 +1,8 @@ +package io.split.storages; + +/** + * Memory for segments + * @author lucasecheverz + */ +public interface SegmentCache extends SegmentCacheConsumer, SegmentCacheProducer { +} diff --git a/client/src/main/java/io/split/storages/SegmentCacheCommons.java b/client/src/main/java/io/split/storages/SegmentCacheCommons.java new file mode 100644 index 00000000..98bbfbd4 --- /dev/null +++ b/client/src/main/java/io/split/storages/SegmentCacheCommons.java @@ -0,0 +1,11 @@ +package io.split.storages; + +public interface SegmentCacheCommons { + + /** + * returns the changeNumber of a segment + * @param segmentName + * @return + */ + long getChangeNumber(String segmentName); +} diff --git a/client/src/main/java/io/split/storages/SegmentCacheConsumer.java b/client/src/main/java/io/split/storages/SegmentCacheConsumer.java new file mode 100644 index 00000000..3c3d0222 --- /dev/null +++ b/client/src/main/java/io/split/storages/SegmentCacheConsumer.java @@ -0,0 +1,27 @@ +package io.split.storages; + +import io.split.engine.segments.SegmentImp; + +import java.util.List; + +public interface SegmentCacheConsumer extends SegmentCacheCommons{ + /** + * evaluates if a key belongs to a segment + * @param segmentName + * @param key + * @return + */ + boolean isInSegment(String segmentName, String key); + + /** + * return every segment + * @return + */ + long getSegmentCount(); + + /** + * return key count + * @return + */ + long getKeyCount(); +} diff --git a/client/src/main/java/io/split/storages/SegmentCacheProducer.java b/client/src/main/java/io/split/storages/SegmentCacheProducer.java new file mode 100644 index 00000000..e281c541 --- /dev/null +++ b/client/src/main/java/io/split/storages/SegmentCacheProducer.java @@ -0,0 +1,20 @@ +package io.split.storages; + +import java.util.List; + +public interface SegmentCacheProducer extends SegmentCacheCommons{ + /** + * update segment + * @param segmentName + * @param toAdd + * @param toRemove + */ + void updateSegment(String segmentName, List toAdd, List toRemove, long changeNumber) ; + + /** + * update the changeNumber of a segment + * @param segmentName + * @param changeNumber + */ + void setChangeNumber(String segmentName, long changeNumber); +} diff --git a/client/src/main/java/io/split/storages/SplitCache.java b/client/src/main/java/io/split/storages/SplitCache.java new file mode 100644 index 00000000..5c61d785 --- /dev/null +++ b/client/src/main/java/io/split/storages/SplitCache.java @@ -0,0 +1,4 @@ +package io.split.storages; + +public interface SplitCache extends SplitCacheConsumer, SplitCacheProducer{ +} diff --git a/client/src/main/java/io/split/storages/SplitCacheCommons.java b/client/src/main/java/io/split/storages/SplitCacheCommons.java new file mode 100644 index 00000000..539e3144 --- /dev/null +++ b/client/src/main/java/io/split/storages/SplitCacheCommons.java @@ -0,0 +1,8 @@ +package io.split.storages; + +import java.util.Set; + +public interface SplitCacheCommons { + long getChangeNumber(); + Set getSegments(); +} diff --git a/client/src/main/java/io/split/storages/SplitCacheConsumer.java b/client/src/main/java/io/split/storages/SplitCacheConsumer.java new file mode 100644 index 00000000..7fbc5748 --- /dev/null +++ b/client/src/main/java/io/split/storages/SplitCacheConsumer.java @@ -0,0 +1,17 @@ +package io.split.storages; + +import io.split.engine.experiments.ParsedSplit; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +public interface SplitCacheConsumer extends SplitCacheCommons{ + ParsedSplit get(String name); + Collection getAll(); + Map fetchMany(List names); + boolean trafficTypeExists(String trafficTypeName); + List splitNames(); + Map> getNamesByFlagSets(List flagSets); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/SplitCacheProducer.java b/client/src/main/java/io/split/storages/SplitCacheProducer.java new file mode 100644 index 00000000..02a64ddc --- /dev/null +++ b/client/src/main/java/io/split/storages/SplitCacheProducer.java @@ -0,0 +1,16 @@ +package io.split.storages; + +import io.split.engine.experiments.ParsedSplit; + +import java.util.List; + +public interface SplitCacheProducer extends SplitCacheCommons{ + boolean remove(String name); + void setChangeNumber(long changeNumber); + void kill(String splitName, String defaultTreatment, long changeNumber); + void clear(); + void putMany(List splits); + void increaseTrafficType(String trafficType); + void decreaseTrafficType(String trafficType); + void update(List toAdd, List toRemove, long changeNumber); +} diff --git a/client/src/main/java/io/split/storages/enums/OperationMode.java b/client/src/main/java/io/split/storages/enums/OperationMode.java new file mode 100644 index 00000000..b4434bf5 --- /dev/null +++ b/client/src/main/java/io/split/storages/enums/OperationMode.java @@ -0,0 +1,6 @@ +package io.split.storages.enums; + +public enum OperationMode { + STANDALONE, + CONSUMER +} diff --git a/client/src/main/java/io/split/storages/enums/StorageMode.java b/client/src/main/java/io/split/storages/enums/StorageMode.java new file mode 100644 index 00000000..904a14f6 --- /dev/null +++ b/client/src/main/java/io/split/storages/enums/StorageMode.java @@ -0,0 +1,7 @@ +package io.split.storages.enums; + +public enum StorageMode { + MEMORY, + PLUGGABLE, + REDIS +} diff --git a/client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java b/client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java new file mode 100644 index 00000000..83e9f3b7 --- /dev/null +++ b/client/src/main/java/io/split/storages/memory/InMemoryCacheImp.java @@ -0,0 +1,212 @@ +package io.split.storages.memory; + +import com.google.common.collect.ConcurrentHashMultiset; +import com.google.common.collect.Maps; +import com.google.common.collect.Multiset; +import com.google.common.collect.Sets; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.engine.experiments.ParsedSplit; +import io.split.storages.SplitCache; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; + +public class InMemoryCacheImp implements SplitCache { + + private static final Logger _log = LoggerFactory.getLogger(InMemoryCacheImp.class); + + private final ConcurrentMap _concurrentMap; + private final ConcurrentMap> _flagSets; + private final Multiset _concurrentTrafficTypeNameSet; + private final FlagSetsFilter _flagSetsFilter; + + private AtomicLong _changeNumber; + + public InMemoryCacheImp(FlagSetsFilter flagSets) { + this(-1, flagSets); + } + + public InMemoryCacheImp(long startingChangeNumber, FlagSetsFilter flagSets) { + _concurrentMap = Maps.newConcurrentMap(); + _changeNumber = new AtomicLong(startingChangeNumber); + _concurrentTrafficTypeNameSet = ConcurrentHashMultiset.create(); + _flagSets = Maps.newConcurrentMap(); + _flagSetsFilter = flagSets; + } + + @Override + public boolean remove(String name) { + ParsedSplit removed = _concurrentMap.remove(name); + if (removed != null) { + removeFromFlagSets(removed.feature()); + if (removed.trafficTypeName() != null) { + this.decreaseTrafficType(removed.trafficTypeName()); + } + } + return removed != null; + } + + @Override + public ParsedSplit get(String name) { + return _concurrentMap.get(name); + } + + @Override + public Collection getAll() { + return _concurrentMap.values(); + } + + @Override + public Map fetchMany(List names) { + Map splits = new HashMap<>(); + + names.forEach(s -> splits.put(s, _concurrentMap.get(s))); + + return splits; + } + + @Override + public long getChangeNumber() { + return _changeNumber.get(); + } + + @Override + public void setChangeNumber(long changeNumber) { + if (changeNumber < _changeNumber.get()) { + _log.error("ChangeNumber for feature flags cache is less than previous"); + } + + _changeNumber.set(changeNumber); + } + + @Override + public boolean trafficTypeExists(String trafficTypeName) { + // If the multiset has [{"user",2}.{"account",0}], elementSet only returns + // ["user"] (it ignores "account") + return Sets.newHashSet(_concurrentTrafficTypeNameSet.elementSet()).contains(trafficTypeName); + } + + @Override + public List splitNames() { + List splitNamesList = new ArrayList<>(); + for (String key: _concurrentMap.keySet()) { + splitNamesList.add(_concurrentMap.get(key).feature()); + } + return splitNamesList; + } + + @Override + public Map> getNamesByFlagSets(List flagSets) { + Map> toReturn = new HashMap<>(); + for (String set: flagSets) { + HashSet keys = _flagSets.get(set); + toReturn.put(set, keys); + } + return toReturn; + } + + @Override + public void kill(String splitName, String defaultTreatment, long changeNumber) { + ParsedSplit parsedSplit = _concurrentMap.get(splitName); + + ParsedSplit updatedSplit = new ParsedSplit(parsedSplit.feature(), + parsedSplit.seed(), + true, + defaultTreatment, + parsedSplit.parsedConditions(), + parsedSplit.trafficTypeName(), + changeNumber, + parsedSplit.trafficAllocation(), + parsedSplit.trafficAllocationSeed(), + parsedSplit.algo(), + parsedSplit.configurations(), + parsedSplit.flagSets(), + parsedSplit.impressionsDisabled(), + parsedSplit.prerequisitesMatcher() + ); + + _concurrentMap.put(splitName, updatedSplit); + } + + @Override + public void clear() { + _concurrentMap.clear(); + _changeNumber.set(-1); + _concurrentTrafficTypeNameSet.clear(); + _flagSets.clear(); + } + + @Override + public void putMany(List splits) { + for (ParsedSplit split : splits) { + _concurrentMap.put(split.feature(), split); + if (split.trafficTypeName() != null) { + this.increaseTrafficType(split.trafficTypeName()); + } + removeFromFlagSets(split.feature()); + addToFlagSets(split); + } + } + + @Override + public void increaseTrafficType(String trafficType) { + _concurrentTrafficTypeNameSet.add(trafficType); + } + + @Override + public void decreaseTrafficType(String trafficType) { + _concurrentTrafficTypeNameSet.remove(trafficType); + } + + @Override + public void update(List toAdd, List toRemove, long changeNumber) { + if(toAdd != null) { + putMany(toAdd); + } + if(toRemove != null) { + for(String featureFlag : toRemove) { + remove(featureFlag); + } + } + setChangeNumber(changeNumber); + } + + public Set getSegments() { + return _concurrentMap.values().stream() + .flatMap(parsedSplit -> parsedSplit.getSegmentsNames().stream()).collect(Collectors.toSet()); + } + + private void addToFlagSets(ParsedSplit featureFlag) { + HashSet sets = featureFlag.flagSets(); + if(sets == null) { + return; + } + for (String set: sets) { + if (!_flagSetsFilter.intersect(set)) { + continue; + } + HashSet features = _flagSets.get(set); + if (features == null) { + features = new HashSet<>(); + } + features.add(featureFlag.feature()); + _flagSets.put(set, features); + } + } + + private void removeFromFlagSets(String featureFlagName) { + for (String set: _flagSets.keySet()) { + _flagSets.get(set).remove(featureFlagName); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImp.java b/client/src/main/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImp.java new file mode 100644 index 00000000..660811ca --- /dev/null +++ b/client/src/main/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImp.java @@ -0,0 +1,109 @@ +package io.split.storages.memory; + +import com.google.common.collect.Maps; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.storages.RuleBasedSegmentCache; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.ArrayList; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; +import java.util.Map; + +public class RuleBasedSegmentCacheInMemoryImp implements RuleBasedSegmentCache { + + private static final Logger _log = LoggerFactory.getLogger(RuleBasedSegmentCacheInMemoryImp.class); + + private final ConcurrentMap _concurrentMap; + + private AtomicLong _changeNumber; + + public RuleBasedSegmentCacheInMemoryImp() { + this(-1); + } + + public RuleBasedSegmentCacheInMemoryImp(long startingChangeNumber) { + _concurrentMap = Maps.newConcurrentMap(); + _changeNumber = new AtomicLong(startingChangeNumber); + } + + @Override + public boolean remove(String name) { + ParsedRuleBasedSegment removed = _concurrentMap.remove(name); + return removed != null; + } + + @Override + public ParsedRuleBasedSegment get(String name) { + return _concurrentMap.get(name); + } + + @Override + public Collection getAll() { + return _concurrentMap.values(); + } + + @Override + public long getChangeNumber() { + return _changeNumber.get(); + } + + @Override + public void setChangeNumber(long changeNumber) { + if (changeNumber < _changeNumber.get()) { + _log.error("ChangeNumber for feature flags cache is less than previous"); + } + + _changeNumber.set(changeNumber); + } + + @Override + public List ruleBasedSegmentNames() { + List ruleBasedSegmentNamesList = new ArrayList<>(); + for (Map.Entry key: _concurrentMap.entrySet()) { + ruleBasedSegmentNamesList.add(key.getValue().ruleBasedSegment()); + } + return ruleBasedSegmentNamesList; + } + + @Override + public void clear() { + _changeNumber.set(-1); + _concurrentMap.clear(); + } + + private void putMany(List ruleBasedSegments) { + for (ParsedRuleBasedSegment ruleBasedSegment : ruleBasedSegments) { + _concurrentMap.put(ruleBasedSegment.ruleBasedSegment(), ruleBasedSegment); + } + } + + @Override + public void update(List toAdd, List toRemove, long changeNumber) { + if(toAdd != null) { + putMany(toAdd); + } + if(toRemove != null) { + for(String ruleBasedSegment : toRemove) { + remove(ruleBasedSegment); + } + } + setChangeNumber(changeNumber); + } + + public Set getSegments() { + return _concurrentMap.values().stream() + .flatMap(parsedRuleBasedSegment -> parsedRuleBasedSegment.getSegmentsNames().stream()).collect(Collectors.toSet()); + } + + @Override + public boolean contains(Set ruleBasedSegmentNames) { + return _concurrentMap.keySet().containsAll(ruleBasedSegmentNames); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/cache/SegmentCacheInMemoryImpl.java b/client/src/main/java/io/split/storages/memory/SegmentCacheInMemoryImpl.java similarity index 72% rename from client/src/main/java/io/split/cache/SegmentCacheInMemoryImpl.java rename to client/src/main/java/io/split/storages/memory/SegmentCacheInMemoryImpl.java index 0c705c01..72a375e9 100644 --- a/client/src/main/java/io/split/cache/SegmentCacheInMemoryImpl.java +++ b/client/src/main/java/io/split/storages/memory/SegmentCacheInMemoryImpl.java @@ -1,7 +1,9 @@ -package io.split.cache; +package io.split.storages.memory; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Maps; import io.split.engine.segments.SegmentImp; +import io.split.storages.SegmentCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,12 +20,9 @@ public class SegmentCacheInMemoryImpl implements SegmentCache { private final ConcurrentMap _segments = Maps.newConcurrentMap(); @Override - public void updateSegment(String segmentName, List toAdd, List toRemove) { - if(_segments.get(segmentName) == null){ - _segments.put(segmentName, new SegmentImp(DEFAULT_CHANGE_NUMBER, segmentName,toAdd)); - } - - _segments.get(segmentName).update(toAdd,toRemove); + public void updateSegment(String segmentName, List toAdd, List toRemove, long changeNumber) { + _segments.putIfAbsent(segmentName, new SegmentImp(changeNumber, segmentName,toAdd)); + _segments.get(segmentName).update(toAdd,toRemove, changeNumber); } @Override @@ -49,14 +48,23 @@ public void setChangeNumber(String segmentName, long changeNumber) { public long getChangeNumber(String segmentName) { SegmentImp segmentImp = _segments.get(segmentName); if(segmentImp == null){ - _log.error("Segment " + segmentName + "Not found."); return DEFAULT_CHANGE_NUMBER; } return segmentImp.getChangeNumber(); } - @Override - public void clear() { + @VisibleForTesting + void clear() { _segments.clear(); } + + @Override + public long getSegmentCount() { + return _segments.values().size(); + } + + @Override + public long getKeyCount() { + return _segments.values().stream().mapToLong(SegmentImp::getKeysSize).sum(); + } } diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterConsumer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterConsumer.java new file mode 100644 index 00000000..afecdddc --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterConsumer.java @@ -0,0 +1,27 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.Event; +import io.split.client.events.EventsStorageConsumer; +import io.split.client.events.WrappedEvent; + +import java.util.ArrayList; +import java.util.List; + +public class UserCustomEventAdapterConsumer implements EventsStorageConsumer { + @Override + public WrappedEvent pop() { + //No-Op + return new WrappedEvent(new Event(), 0L); + } + + @Override + public List popAll(){ + //No-op + return new ArrayList<>(); + } + + @Override + public boolean isFull() { + return false; + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducer.java new file mode 100644 index 00000000..6252c303 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducer.java @@ -0,0 +1,34 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.Event; +import io.split.client.dtos.Metadata; +import io.split.client.events.EventsStorageProducer; +import io.split.client.utils.Json; +import io.split.storages.pluggable.domain.EventConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import pluggable.CustomStorageWrapper; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomEventAdapterProducer implements EventsStorageProducer { + + private final UserStorageWrapper _userStorageWrapper; + private Metadata _metadata; + + public UserCustomEventAdapterProducer(CustomStorageWrapper customStorageWrapper, Metadata metadata) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + _metadata = metadata; + } + + @Override + public boolean track(Event event, int eventSize) { + List events = Stream.of(Json.toJson(new EventConsumer(_metadata, event))).collect(Collectors.toList()); + _userStorageWrapper.pushItems(PrefixAdapter.buildEvent(), events); + return true; + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterConsumer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterConsumer.java new file mode 100644 index 00000000..4010012c --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterConsumer.java @@ -0,0 +1,27 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.KeyImpression; +import io.split.client.impressions.ImpressionsStorageConsumer; + +import java.util.ArrayList; +import java.util.List; + +public class UserCustomImpressionAdapterConsumer implements ImpressionsStorageConsumer { + @Override + public List pop(int count) { + //No-Op + return new ArrayList<>(); + } + + @Override + public List pop() { + //No-Op + return new ArrayList<>(); + } + + @Override + public boolean isFull() { + //No-Op + return false; + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducer.java new file mode 100644 index 00000000..c0845f0c --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducer.java @@ -0,0 +1,49 @@ +package io.split.storages.pluggable.adapters; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializer; +import io.split.client.dtos.KeyImpression; +import io.split.client.dtos.Metadata; +import io.split.client.impressions.ImpressionsStorageProducer; +import io.split.storages.pluggable.domain.ImpressionConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Modifier; +import java.util.List; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomImpressionAdapterProducer implements ImpressionsStorageProducer { + + private final UserStorageWrapper _userStorageWrapper; + private final Gson _json = new GsonBuilder() + .serializeNulls() // Send nulls + .excludeFieldsWithModifiers(Modifier.STATIC) + .registerTypeAdapter(Double.class, (JsonSerializer) (src, typeOfSrc, context) -> { + if (src == src.longValue()) + return new JsonPrimitive(src.longValue()); + return new JsonPrimitive(src); + }) + .create(); + private Metadata _metadata; + + public UserCustomImpressionAdapterProducer(CustomStorageWrapper customStorageWrapper, Metadata metadata) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + _metadata = metadata; + } + + @Override + public long put(List imps) { + //Impression + if (imps.isEmpty()){ + return 0; + } + List impressions = imps.stream().map(keyImp -> _json.toJson(new ImpressionConsumer(_metadata, keyImp))).collect(Collectors.toList()); + return _userStorageWrapper.pushItems(PrefixAdapter.buildImpressions(), impressions); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumer.java new file mode 100644 index 00000000..b04e4976 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumer.java @@ -0,0 +1,102 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.utils.Json; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.utils.Helper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomRuleBasedSegmentAdapterConsumer implements RuleBasedSegmentCacheConsumer { + + private static final Logger _log = LoggerFactory.getLogger(UserCustomRuleBasedSegmentAdapterConsumer.class); + + private final RuleBasedSegmentParser _ruleBasedSegmentParser; + private final UserStorageWrapper _userStorageWrapper; + + public UserCustomRuleBasedSegmentAdapterConsumer(CustomStorageWrapper customStorageWrapper) { + _ruleBasedSegmentParser = new RuleBasedSegmentParser(); + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + + @Override + public long getChangeNumber() { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentChangeNumber()); + return Helper.responseToLong(wrapperResponse, -1L); + } + + @Override + public ParsedRuleBasedSegment get(String name) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentKey(name)); + if(wrapperResponse == null) { + return null; + } + RuleBasedSegment ruleBasedSegment = Json.fromJson(wrapperResponse, RuleBasedSegment.class); + if(ruleBasedSegment == null) { + _log.warn("Could not parse RuleBasedSegment."); + return null; + } + return _ruleBasedSegmentParser.parse(ruleBasedSegment); + } + + @Override + public Collection getAll() { + Set keys = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment()); + if(keys == null) { + return new ArrayList<>(); + } + List wrapperResponse = _userStorageWrapper.getMany(new ArrayList<>(keys)); + if(wrapperResponse == null) { + return new ArrayList<>(); + } + return stringsToParsedRuleBasedSegments(wrapperResponse); + } + + @Override + public List ruleBasedSegmentNames() { + Set ruleBasedSegmentNamesWithPrefix = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment()); + ruleBasedSegmentNamesWithPrefix = ruleBasedSegmentNamesWithPrefix.stream(). + map(key -> key.replace(PrefixAdapter.buildRuleBasedSegmentsPrefix(), "")). + collect(Collectors.toSet()); + return new ArrayList<>(ruleBasedSegmentNamesWithPrefix); + } + + @Override + public Set getSegments() { + return getAll().stream() + .flatMap(parsedRuleBasedSegment -> parsedRuleBasedSegment. + getSegmentsNames().stream()).collect(Collectors.toSet()); + } + + + private List stringsToParsedRuleBasedSegments(List elements) { + List result = new ArrayList<>(); + for(String s : elements) { + if(s != null) { + result.add(_ruleBasedSegmentParser.parse(Json.fromJson(s, RuleBasedSegment.class))); + continue; + } + result.add(null); + } + return result; + } + + @Override + public boolean contains(Set ruleBasedSegmentNames) { + return _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment()).containsAll(ruleBasedSegmentNames); + } + +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumer.java new file mode 100644 index 00000000..7ba2d916 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumer.java @@ -0,0 +1,46 @@ +package io.split.storages.pluggable.adapters; + +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.utils.Helper; +import pluggable.CustomStorageWrapper; + +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomSegmentAdapterConsumer implements SegmentCacheConsumer { + + private final UserStorageWrapper _userStorageWrapper; + + public UserCustomSegmentAdapterConsumer(CustomStorageWrapper customStorageWrapper) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + + @Override + public long getChangeNumber(String segmentName) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSegment(segmentName)); + return Helper.responseToLong(wrapperResponse, -1L); + } + + @Override + public boolean isInSegment(String segmentName, String key) { + return _userStorageWrapper.itemContains(PrefixAdapter.buildSegment(segmentName), key); + } + + @Override + public long getSegmentCount() { + Set keys = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildSegmentAll()); + return keys == null ? 0L : keys.size(); + } + + @Override + public long getKeyCount() { + Set keys = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildSegmentAll()); + if(keys == null) { + return 0L; + } + return keys.stream().mapToLong(key -> _userStorageWrapper.getItemsCount(key)).sum(); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducer.java new file mode 100644 index 00000000..faebf835 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducer.java @@ -0,0 +1,39 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.utils.Json; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.utils.Helper; +import pluggable.CustomStorageWrapper; + +import java.util.List; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomSegmentAdapterProducer implements SegmentCacheProducer { + + private final UserStorageWrapper _userStorageWrapper; + + public UserCustomSegmentAdapterProducer(CustomStorageWrapper customStorageWrapper) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + @Override + public long getChangeNumber(String segmentName) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSegment(segmentName)); + return Helper.responseToLong(wrapperResponse, -1L); + } + + @Override + public void updateSegment(String segmentName, List toAdd, List toRemove, long changeNumber) { + String keySegment = PrefixAdapter.buildSegment(segmentName); + _userStorageWrapper.addItems(keySegment, toAdd); + _userStorageWrapper.removeItems(keySegment, toRemove); + _userStorageWrapper.set(PrefixAdapter.buildSegmentTill(segmentName), Json.toJson(changeNumber)); + } + + @Override + public void setChangeNumber(String segmentName, long changeNumber) { + _userStorageWrapper.set(PrefixAdapter.buildSegmentTill(segmentName), Json.toJson(changeNumber)); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumer.java new file mode 100644 index 00000000..ca255c10 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumer.java @@ -0,0 +1,156 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.Split; +import io.split.client.utils.Json; +import io.split.engine.experiments.ParsedSplit; +import io.split.engine.experiments.SplitParser; +import io.split.storages.SplitCacheConsumer; +import io.split.storages.pluggable.domain.UserPipelineWrapper; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.utils.Helper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; +import pluggable.Result; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.HashSet; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomSplitAdapterConsumer implements SplitCacheConsumer { + + private static final Logger _log = LoggerFactory.getLogger(UserCustomSplitAdapterConsumer.class); + + private final SplitParser _splitParser; + private final UserStorageWrapper _userStorageWrapper; + + public UserCustomSplitAdapterConsumer(CustomStorageWrapper customStorageWrapper) { + _splitParser = new SplitParser(); + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + + @Override + public long getChangeNumber() { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber()); + return Helper.responseToLong(wrapperResponse, -1L); + } + + @Override + public ParsedSplit get(String name) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSplitKey(name)); + if(wrapperResponse == null) { + return null; + } + Split split = Json.fromJson(wrapperResponse, Split.class); + if(split == null) { + _log.warn("Could not parse Split."); + return null; + } + return _splitParser.parse(split); + } + + @Override + public Collection getAll() { + Set keys = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllSplit()); + if(keys == null) { + return new ArrayList<>(); + } + List wrapperResponse = _userStorageWrapper.getMany(new ArrayList<>(keys)); + if(wrapperResponse == null) { + return new ArrayList<>(); + } + return stringsToParsedSplits(wrapperResponse); + } + + @Override + public boolean trafficTypeExists(String trafficTypeName) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists(trafficTypeName)); + if(wrapperResponse == null) { + return false; + } + try { + Long value = Json.fromJson(wrapperResponse, Long.class); + return value != null && value > 0; + } + catch(Exception e) { + _log.info("Error getting boolean from String."); + } + return false; + } + + @Override + public List splitNames() { + Set splitNamesWithPrefix = _userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllSplit()); + splitNamesWithPrefix = splitNamesWithPrefix.stream().map(key -> key.replace(PrefixAdapter.buildSplitsPrefix(), "")). + collect(Collectors.toSet()); + return new ArrayList<>(splitNamesWithPrefix); + } + + @Override + public Map> getNamesByFlagSets(List flagSets) { + Map> toReturn = new HashMap<>(); + try { + if (flagSets == null) { + return toReturn; + } + UserPipelineWrapper pipelineExecution = _userStorageWrapper.pipeline(); + for (String set: flagSets) { + pipelineExecution.getMembers(PrefixAdapter.buildFlagSetPrefix(set)); + } + List results = pipelineExecution.exec(); + if (results == null || results.isEmpty()){ + return toReturn; + } + for (int i = 0; i < results.size(); i ++) { + Optional> featureFlags = results.get(i).asHash(); + if(featureFlags.isPresent()) { + toReturn.put(flagSets.get(i), featureFlags.get()); + } + } + } catch (Exception e) { + _log.warn("Redis pipeline exception when getting names by flag sets: ", e); + } + return toReturn; + } + + @Override + public Map fetchMany(List names) { + Map result = new HashMap<>(); + List wrapperResponse = _userStorageWrapper.getItems(PrefixAdapter.buildFetchManySplits(names)); + if(wrapperResponse == null) { + return result; + } + List parsedSplits = stringsToParsedSplits(wrapperResponse); + for(int i=0; i < parsedSplits.size(); i++) { + result.put(names.get(i), parsedSplits.get(i)); + } + return result; + } + + @Override + public Set getSegments() { + //NoOp + return new HashSet<>(); + } + + private List stringsToParsedSplits(List elements) { + List result = new ArrayList<>(); + for(String s : elements) { + if(s != null) { + result.add(_splitParser.parse(Json.fromJson(s, Split.class))); + continue; + } + result.add(null); + } + return result; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducer.java new file mode 100644 index 00000000..b9a034af --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducer.java @@ -0,0 +1,119 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.Split; +import io.split.client.utils.Json; +import io.split.engine.experiments.ParsedSplit; +import io.split.storages.SplitCacheProducer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.storages.pluggable.utils.Helper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomSplitAdapterProducer implements SplitCacheProducer { + + private static final Logger _log = LoggerFactory.getLogger(UserCustomSplitAdapterProducer.class); + + private final UserStorageWrapper _userStorageWrapper; + + public UserCustomSplitAdapterProducer(CustomStorageWrapper customStorageWrapper) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + } + + @Override + public long getChangeNumber() { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber()); + return Helper.responseToLong(wrapperResponse, -1L); + } + + @Override + public boolean remove(String splitName) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSplitKey(splitName)); + if(wrapperResponse == null) { + return false; + } + Split split = Json.fromJson(wrapperResponse, Split.class); + if(split == null) { + _log.info("Could not parse Split."); + return false; + } + _userStorageWrapper.delete(Stream.of(PrefixAdapter.buildSplitKey(splitName)).collect(Collectors.toList())); + if(split.trafficTypeName != null){ + this.decreaseTrafficType(split.trafficTypeName); + } + return true; + } + + @Override + public void setChangeNumber(long changeNumber) { + _userStorageWrapper.set(PrefixAdapter.buildSplitChangeNumber(),Json.toJson(changeNumber)); + } + + @Override + public void kill(String splitName, String defaultTreatment, long changeNumber) { + String wrapperResponse = _userStorageWrapper.get(PrefixAdapter.buildSplitKey(splitName)); + if(wrapperResponse == null) { + return; + } + Split split = Json.fromJson(wrapperResponse, Split.class); + if(split == null) { + _log.info("Could not parse Split."); + return; + } + _userStorageWrapper.set(PrefixAdapter.buildSplitKey(splitName), Json.toJson(split)); + } + + @Override + public void clear() { + //NoOp + } + + @Override + public void putMany(List splits) { + for(ParsedSplit split : splits) { + _userStorageWrapper.set(PrefixAdapter.buildSplitKey(split.feature()), Json.toJson(split)); + this.increaseTrafficType(PrefixAdapter.buildTrafficTypeExists(split.trafficTypeName())); + } + } + + @Override + public void increaseTrafficType(String trafficType) { + _userStorageWrapper.increment(PrefixAdapter.buildTrafficTypeExists(trafficType), 1); + } + + @Override + public void decreaseTrafficType(String trafficType) { + long trafficTypeCount = _userStorageWrapper.decrement(PrefixAdapter.buildTrafficTypeExists(trafficType), 1); + if(trafficTypeCount<=0) { + _userStorageWrapper.delete(Stream.of(PrefixAdapter.buildTrafficTypeExists(trafficType)).collect(Collectors.toList())); + } + } + + @Override + public void update(List toAdd, List toRemove, long changeNumber) { + if(toAdd != null) { + putMany(toAdd); + } + if(toRemove != null) { + for(String featureFlag : toRemove) { + remove(featureFlag); + } + } + setChangeNumber(changeNumber); + } + + @Override + public Set getSegments() { + //NoOp + return new HashSet<>(); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducer.java b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducer.java new file mode 100644 index 00000000..1dd9412b --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducer.java @@ -0,0 +1,108 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.utils.SDKMetadata; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; +import io.split.telemetry.storage.TelemetryStorageProducer; +import io.split.telemetry.utils.BucketCalculator; +import pluggable.CustomStorageWrapper; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserCustomTelemetryAdapterProducer implements TelemetryStorageProducer { + + private final UserStorageWrapper _userStorageWrapper; + private SDKMetadata _sdkMetadata; + + public UserCustomTelemetryAdapterProducer(CustomStorageWrapper customStorageWrapper, SDKMetadata sdkMetadata) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + _sdkMetadata = sdkMetadata; + } + + @Override + public void recordNonReadyUsage() { + //No-op + } + + @Override + public void recordBURTimeout() { + //No-op + } + + @Override + public void recordLatency(MethodEnum method, long latency) { + String key = String.format("%s/%s/%s/%s/%d", _sdkMetadata.getSdkVersion(), _sdkMetadata.getMachineName(), + _sdkMetadata.getMachineIp(), method.getMethod(), BucketCalculator.getBucketForLatency(latency)); + _userStorageWrapper.hIncrement(PrefixAdapter.buildTelemetryLatenciesPrefix(), key, 1); + } + + @Override + public void recordException(MethodEnum method) { + String key = String.format("%s/%s/%s/%s", _sdkMetadata.getSdkVersion(), _sdkMetadata.getMachineName(), + _sdkMetadata.getMachineIp(), method.getMethod()); + _userStorageWrapper.hIncrement(PrefixAdapter.buildTelemetryExceptionsPrefix(), key, 1); + } + + @Override + public void addTag(String tag) { + //No-op + } + + @Override + public void recordImpressionStats(ImpressionsDataTypeEnum dataType, long count) { + //No-op + } + + @Override + public void recordEventStats(EventsDataRecordsEnum dataType, long count) { + //No-op + } + + @Override + public void recordSuccessfulSync(LastSynchronizationRecordsEnum resource, long time) { + //No-op + } + + @Override + public void recordSyncError(ResourceEnum resource, int status) { + //No-op + } + + @Override + public void recordSyncLatency(HTTPLatenciesEnum resource, long latency) { + //No-op + } + + @Override + public void recordAuthRejections() { + //No-op + } + + @Override + public void recordTokenRefreshes() { + //No-op + } + + @Override + public void recordStreamingEvents(StreamingEvent streamingEvent) { + //No-op + } + + @Override + public void recordSessionLength(long sessionLength) { + //No-op + } + + @Override + public void recordUpdatesFromSSE(UpdatesFromSSEEnum updatesFromSSEEnum) { + //No-op + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/domain/ConfigConsumer.java b/client/src/main/java/io/split/storages/pluggable/domain/ConfigConsumer.java new file mode 100644 index 00000000..c2df2b29 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/ConfigConsumer.java @@ -0,0 +1,86 @@ +package io.split.storages.pluggable.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class ConfigConsumer { + /* package private */ static final String FIELD_OPERATION_MODE = "oM"; + /* package private */ static final String FIELD_STORAGE = "st"; + /* package private */ static final String FIELD_ACTIVE_FACTORIES = "aF"; + /* package private */ static final String FIELD_REDUNDANT_FACTORIES = "rF"; + /* package private */ static final String FIELD_TAGS = "t"; + /* package private */ static final String FIELD_FLAG_SETS_TOTAL = "fsT"; + /* package private */ static final String FIELD_FLAG_SETS_INVALID = "fsI"; + + @SerializedName(FIELD_OPERATION_MODE) + private int _operationMode; + @SerializedName(FIELD_STORAGE) + private String _storage; + @SerializedName(FIELD_ACTIVE_FACTORIES) + private long _activeFactories; + @SerializedName(FIELD_REDUNDANT_FACTORIES) + private long _redundantFactories; + @SerializedName(FIELD_TAGS) + private List _tags; + @SerializedName(FIELD_FLAG_SETS_TOTAL) + private int _flagSetsTotal; + @SerializedName(FIELD_FLAG_SETS_INVALID) + private int _flagSetsInvalid; + + public int getOperationMode() { + return _operationMode; + } + + public void setOperationMode(int operationMode) { + this._operationMode = operationMode; + } + + public String getStorage() { + return _storage; + } + + public void setStorage(String storage) { + this._storage = storage; + } + + public long getActiveFactories() { + return _activeFactories; + } + + public void setActiveFactories(long activeFactories) { + this._activeFactories = activeFactories; + } + + public long getRedundantFactories() { + return _redundantFactories; + } + + public void setRedundantFactories(long redundantFactories) { + this._redundantFactories = redundantFactories; + } + + public List getTags() { + return _tags; + } + + public void setTags(List tags) { + this._tags = tags; + } + + public int getFlagSetsTotal() { + return _flagSetsTotal; + } + + public void setFlagSetsTotal(int flagSetsTotal) { + this._flagSetsTotal = flagSetsTotal; + } + + public int getFlagSetsInvalid() { + return _flagSetsInvalid; + } + + public void setFlagSetsInvalid(int flagSetsInvalid) { + this._flagSetsInvalid = flagSetsInvalid; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/domain/EventConsumer.java b/client/src/main/java/io/split/storages/pluggable/domain/EventConsumer.java new file mode 100644 index 00000000..6a92fce0 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/EventConsumer.java @@ -0,0 +1,29 @@ +package io.split.storages.pluggable.domain; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.Event; +import io.split.client.dtos.Metadata; + +public class EventConsumer { + + /* package private */ static final String FIELD_METADATA = "m"; + /* package private */ static final String FIELD_KEY_IMPRESSION = "e"; + + @SerializedName(FIELD_METADATA) + private Metadata _metadata; + @SerializedName(FIELD_KEY_IMPRESSION) + private Event _event; + + public EventConsumer(Metadata metadata, Event event) { + _metadata = metadata; + _event = event; + } + + public Metadata getMetadata() { + return _metadata; + } + + public Event getEventDto() { + return _event; + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/domain/ImpressionConsumer.java b/client/src/main/java/io/split/storages/pluggable/domain/ImpressionConsumer.java new file mode 100644 index 00000000..341ae244 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/ImpressionConsumer.java @@ -0,0 +1,28 @@ +package io.split.storages.pluggable.domain; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.KeyImpression; +import io.split.client.dtos.Metadata; + +public class ImpressionConsumer { + /* package private */ static final String FIELD_METADATA = "m"; + /* package private */ static final String FIELD_KEY_IMPRESSION = "i"; + + @SerializedName(FIELD_METADATA) + private Metadata _metadata; + @SerializedName(FIELD_KEY_IMPRESSION) + private ImpressionDto _impressionDto; + + public ImpressionConsumer(Metadata metadata, KeyImpression keyImpression) { + _metadata = metadata; + _impressionDto = ImpressionDto.fromKeyImpression(keyImpression); + } + + public Metadata getMetadata() { + return _metadata; + } + + public ImpressionDto getKeyImpression() { + return _impressionDto; + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/domain/ImpressionDto.java b/client/src/main/java/io/split/storages/pluggable/domain/ImpressionDto.java new file mode 100644 index 00000000..c4b2baa4 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/ImpressionDto.java @@ -0,0 +1,80 @@ +package io.split.storages.pluggable.domain; + +import com.google.gson.annotations.SerializedName; +import io.split.client.dtos.KeyImpression; + +import java.util.Objects; + +public class ImpressionDto { + + /* package private */ static final String FIELD_FEATURE = "f"; + /* package private */ static final String FIELD_KEY_NAME = "k"; + /* package private */ static final String FIELD_BUCKETING_KEY = "b"; + /* package private */ static final String FIELD_TREATMENT = "t"; + /* package private */ static final String FIELD_LABEL = "r"; + /* package private */ static final String FIELD_TIME = "m"; + /* package private */ static final String FIELD_CHANGE_NUMBER = "c"; + + @SerializedName(FIELD_FEATURE) + public transient String feature; // Non-serializable + + @SerializedName(FIELD_KEY_NAME) + public String keyName; + + @SerializedName(FIELD_BUCKETING_KEY) + public String bucketingKey; + + @SerializedName(FIELD_TREATMENT) + public String treatment; + + @SerializedName(FIELD_LABEL) + public String label; + + @SerializedName(FIELD_TIME) + public long time; + + @SerializedName(FIELD_CHANGE_NUMBER) + public Long changeNumber; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + KeyImpression that = (KeyImpression) o; + + if (time != that.time) return false; + if (!Objects.equals(feature, that.feature)) return false; + if (!keyName.equals(that.keyName)) return false; + if (!treatment.equals(that.treatment)) return false; + + if (bucketingKey == null) { + return that.bucketingKey == null; + } + + return bucketingKey.equals(that.bucketingKey); + } + + @Override + public int hashCode() { + int result = feature != null ? feature.hashCode() : 0; + result = 31 * result + keyName.hashCode(); + result = 31 * result + (bucketingKey == null ? 0 : bucketingKey.hashCode()); + result = 31 * result + treatment.hashCode(); + result = 31 * result + (int) (time ^ (time >>> 32)); + return result; + } + + public static ImpressionDto fromKeyImpression(KeyImpression i) { + ImpressionDto iDto = new ImpressionDto(); + iDto.feature = i.feature; + iDto.keyName = i.keyName; + iDto.bucketingKey = i.bucketingKey; + iDto.time = i.time; + iDto.changeNumber = i.changeNumber; + iDto.treatment = i.treatment; + iDto.label = i.label; + return iDto; + } + +} diff --git a/client/src/main/java/io/split/storages/pluggable/domain/PrefixAdapter.java b/client/src/main/java/io/split/storages/pluggable/domain/PrefixAdapter.java new file mode 100644 index 00000000..a785fbe7 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/PrefixAdapter.java @@ -0,0 +1,112 @@ +package io.split.storages.pluggable.domain; + +import java.util.ArrayList; +import java.util.List; + +public class PrefixAdapter { + + private static final String DEFAULT_PREFIX = "SPLITIO."; + private static final String SPLIT_PREFIX = "split."; + private static final String SPLITS_PREFIX = "splits."; + private static final String TRAFFIC_TYPE_PREFIX = "trafficType."; + private static final String EVENTS = "events"; + private static final String IMPRESSIONS = "impressions"; + private static final String SEGMENT = "segment."; + private static final String COUNT = ".count"; + private static final String UNIQUE_KEYS = "uniquekeys"; + private static final String TILL = "till"; + private static final String TELEMETRY = "telemetry."; + private static final String LATENCIES = "latencies"; + private static final String EXCEPTIONS = "exceptions"; + private static final String INIT = "init"; + private static final String FLAG_SET = "flagSet"; + private static final String RULE_BASED_SEGMENT_PREFIX = "rbsegment"; + private static final String RULE_BASED_SEGMENTS_PREFIX = "rbsegments"; + + public static String buildSplitKey(String name) { + return String.format(DEFAULT_PREFIX+ SPLIT_PREFIX +"%s", name); + } + + public static String buildSplitChangeNumber() { + return DEFAULT_PREFIX+SPLITS_PREFIX+"till"; + } + + public static String buildGetAllSplit() { + return DEFAULT_PREFIX+SPLIT_PREFIX+"*"; + } + + public static String buildSplitsPrefix(){ + return DEFAULT_PREFIX+SPLIT_PREFIX; + } + + public static String buildRuleBasedSegmentKey(String name) { + return String.format(DEFAULT_PREFIX+ RULE_BASED_SEGMENT_PREFIX +"%s", name); + } + + public static String buildRuleBasedSegmentsPrefix(){ + return DEFAULT_PREFIX+RULE_BASED_SEGMENT_PREFIX; + } + + public static String buildRuleBasedSegmentChangeNumber() { + return DEFAULT_PREFIX+RULE_BASED_SEGMENTS_PREFIX+"till"; + } + + public static String buildGetAllRuleBasedSegment() { + return DEFAULT_PREFIX+RULE_BASED_SEGMENT_PREFIX+"*"; + } + + public static String buildTrafficTypeExists(String trafficType) { + return String.format(DEFAULT_PREFIX+TRAFFIC_TYPE_PREFIX+"%s", trafficType); + } + + public static List buildFetchManySplits(List names) { + List prefixes = new ArrayList<>(); + for(String name : names) { + prefixes.add(String.format(DEFAULT_PREFIX+ SPLIT_PREFIX +"%s", name)); + } + return prefixes; + } + + public static String buildEvent() { + return DEFAULT_PREFIX+EVENTS; + } + + public static String buildImpressions() { + return DEFAULT_PREFIX+IMPRESSIONS; + } + + public static String buildSegment(String segmentName) { + return String.format(DEFAULT_PREFIX+SEGMENT+"%s", segmentName); + } + + public static String buildSegmentAll() { + return String.format(DEFAULT_PREFIX+SEGMENT+"*"); + } + + public static String buildSegmentTill(String segmentName) { + return String.format(DEFAULT_PREFIX+SEGMENT+"%s."+TILL, segmentName); + } + + public static String buildImpressionsCount(){ + return String.format(DEFAULT_PREFIX + IMPRESSIONS + COUNT); + } + + public static String buildUniqueKeys() { + return String.format(DEFAULT_PREFIX + UNIQUE_KEYS); + } + + public static String buildTelemetryLatenciesPrefix() { + return String.format(DEFAULT_PREFIX+TELEMETRY+LATENCIES); + } + + public static String buildTelemetryExceptionsPrefix() { + return String.format(DEFAULT_PREFIX+TELEMETRY+EXCEPTIONS); + } + + public static String buildTelemetryInit() { + return String.format(DEFAULT_PREFIX + TELEMETRY + INIT); + } + public static String buildFlagSetPrefix(String set) { + return String.format(DEFAULT_PREFIX + FLAG_SET + ".%s", set); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/domain/UserPipelineWrapper.java b/client/src/main/java/io/split/storages/pluggable/domain/UserPipelineWrapper.java new file mode 100644 index 00000000..81ddc691 --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/UserPipelineWrapper.java @@ -0,0 +1,48 @@ +package io.split.storages.pluggable.domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.Pipeline; +import pluggable.Result; + +import java.util.List; + +public class UserPipelineWrapper implements Pipeline{ + + private static final Logger _logger = LoggerFactory.getLogger(UserPipelineWrapper.class); + + private final Pipeline _pipeline; + + + public UserPipelineWrapper(Pipeline pipeline) { + _pipeline = pipeline; + } + + @Override + public List exec() throws Exception { + try{ + return _pipeline.exec(); + } catch (Exception e) { + _logger.warn("Exception calling Pipeline exec", e); + throw e; + } + } + + @Override + public void hIncrement(String key, String field, long value) { + try { + _pipeline.hIncrement(key, field, value); + } catch (Exception e){ + _logger.warn("Exception calling Pipeline hIncrement", e); + } + } + + @Override + public void getMembers(String key) { + try { + _pipeline.getMembers(key); + } catch (Exception e){ + _logger.warn("Exception calling Pipeline getMembers", e); + } + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/domain/UserStorageWrapper.java b/client/src/main/java/io/split/storages/pluggable/domain/UserStorageWrapper.java new file mode 100644 index 00000000..dfe6820f --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/domain/UserStorageWrapper.java @@ -0,0 +1,244 @@ +package io.split.storages.pluggable.domain; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; +import pluggable.HasPipelineSupport; +import pluggable.NotPipelinedImpl; + +import java.util.List; +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class UserStorageWrapper implements CustomStorageWrapper { + + private static final Logger _log = LoggerFactory.getLogger(UserStorageWrapper.class); + + private final CustomStorageWrapper _customStorageWrapper; + + public UserStorageWrapper(CustomStorageWrapper customStorageWrapper) { + _customStorageWrapper = checkNotNull(customStorageWrapper); + } + + @Override + public String get(String key) { + try { + return _customStorageWrapper.get(key); + } + catch (Exception e) { + _log.error(String.format("error fetching key '%s' from storage. Error: '%s'", key, e.getMessage())); + return null; + } + } + + @Override + public List getMany(List keys) { + try { + return _customStorageWrapper.getMany(keys); + } + catch (Exception e) { + _log.error(String.format("error fetching keys '%s' from storage. Error: '%s'", keys, e.getMessage())); + return null; + } + } + + @Override + public void set(String key, String item) { + try { + _customStorageWrapper.set(key, item); + } + catch (Exception e) { + _log.error(String.format("error updating key '%s' from storage. Error: '%s'", key, e.getMessage())); + } + } + + @Override + public void hSet(String key, String field, String item) { + try { + _customStorageWrapper.hSet(key, field, item); + } + catch (Exception e) { + _log.error(String.format("error updating key by field '%s' from storage. Error: '%s'", key, e.getMessage())); + } + } + + @Override + public void delete(List keys) { + try { + _customStorageWrapper.delete(keys); + } + catch (Exception e) { + _log.error(String.format("error deleting keys '%s' from storage. Error: '%s'", keys, e.getMessage())); + } + } + + @Override + public String getAndSet(String key, String item){ + try { + return _customStorageWrapper.getAndSet(key, item); + } + catch (Exception e) { + _log.error(String.format("error getting and updating key '%s' from storage. Error: '%s'", key, e.getMessage())); + return null; + } + } + + @Override + public Set getKeysByPrefix(String prefix){ + try { + return _customStorageWrapper.getKeysByPrefix(prefix); + } + catch (Exception e) { + _log.error(String.format("error getting keys '%s' from storage. Error: '%s'", prefix, e.getMessage())); + return null; + } + } + + @Override + public long increment(String key, long value) { + try { + return _customStorageWrapper.increment(key, value); + } + catch (Exception e) { + _log.error(String.format("error incrementing key '%s' from storage. Error: '%s'", key, e.getMessage())); + return 0L; + } + } + + @Override + public long hIncrement(String key, String field, long value){ + try { + return _customStorageWrapper.hIncrement(key, field, value); + } + catch (Exception e) { + _log.error(String.format("error incrementing key by field '%s' from storage. Error: '%s'", key, e.getMessage())); + return 0L; + } + } + + @Override + public long decrement(String key, long value) { + try { + return _customStorageWrapper.decrement(key, value); + } + catch (Exception e) { + _log.error(String.format("error decrementing key '%s' from storage. Error: '%s'", key, e.getMessage())); + return 0L; + } + } + + @Override + public long pushItems(String key, List items) { + try { + return _customStorageWrapper.pushItems(key, items); + } + catch (Exception e) { + _log.error(String.format("error pushing items with key '%s' from storage. Error: '%s'", key, e.getMessage())); + return 0; + } + } + + @Override + public List popItems(String key, long count){ + try { + return _customStorageWrapper.popItems(key, count); + } + catch (Exception e) { + _log.error(String.format("error popping key '%s' from storage. Error: '%s'", key, e.getMessage())); + return null; + } + } + + @Override + public long getItemsCount(String key) { + try { + return _customStorageWrapper.getItemsCount(key); + } + catch (Exception e) { + _log.error(String.format("error getting items count key '%s' from storage. Error: '%s'", key, e.getMessage())); + return -1L; + } + } + + @Override + public boolean itemContains(String key, String item){ + try { + return _customStorageWrapper.itemContains(key, item); + } + catch (Exception e) { + _log.error(String.format("error checking if item contains key '%s' from storage. Error: '%s'", key, e.getMessage())); + return false; + } + } + + @Override + public void addItems(String key, List items) { + try { + _customStorageWrapper.addItems(key, items); + } + catch (Exception e) { + _log.error(String.format("error adding items with key '%s' from storage. Error: '%s'", key, e.getMessage())); + } + } + + @Override + public void removeItems(String key, List items) { + try { + _customStorageWrapper.removeItems(key, items); + } + catch (Exception e) { + _log.error(String.format("error removing items with key '%s' from storage. Error: '%s'", key, e.getMessage())); + } + } + + @Override + public List getItems(List keys){ + try { + return _customStorageWrapper.getItems(keys); + } + catch (Exception e) { + _log.error(String.format("error getting items with keys '%s' from storage. Error: '%s'", keys, e.getMessage())); + return null; + } + } + + @Override + public Set getMembers(String key) { + try { + return _customStorageWrapper.getMembers(key); + } + catch (Exception e) { + _log.error(String.format("error getting set members with key '%s' from storage. Error: '%s'", key, e.getMessage())); + return null; + } + } + + @Override + public boolean connect(){ + try { + return _customStorageWrapper.connect(); + } + catch (Exception e) { + _log.error(String.format("error trying to connect. Error: '%s'" , e.getMessage())); + return false; + } + } + + @Override + public boolean disconnect(){ + try { + return _customStorageWrapper.disconnect(); + } + catch (Exception e) { + _log.error(String.format("error trying to disconnect. Error: '%s'" , e.getMessage())); + return false; + } + } + + public UserPipelineWrapper pipeline() throws Exception { + return (_customStorageWrapper instanceof HasPipelineSupport) + ? new UserPipelineWrapper(((HasPipelineSupport) _customStorageWrapper).pipeline()) + : new UserPipelineWrapper(new NotPipelinedImpl(_customStorageWrapper)); + } +} diff --git a/client/src/main/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitter.java b/client/src/main/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitter.java new file mode 100644 index 00000000..2e80abdf --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitter.java @@ -0,0 +1,73 @@ +package io.split.storages.pluggable.synchronizer; + +import com.google.common.annotations.VisibleForTesting; +import io.split.client.SplitClientConfig; +import io.split.client.dtos.UniqueKeys; +import io.split.client.utils.Json; +import io.split.client.utils.SDKMetadata; +import io.split.storages.enums.OperationMode; +import io.split.storages.pluggable.domain.ConfigConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import pluggable.CustomStorageWrapper; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class TelemetryConsumerSubmitter implements TelemetrySynchronizer { + + private static final String STORAGE = "PLUGGABLE"; + + private final UserStorageWrapper _userStorageWrapper; + private final SDKMetadata _sdkMetadata; + + public TelemetryConsumerSubmitter(CustomStorageWrapper customStorageWrapper, SDKMetadata sdkMetadata) { + _userStorageWrapper = new UserStorageWrapper(checkNotNull(customStorageWrapper)); + _sdkMetadata = checkNotNull(sdkMetadata); + } + + @Override + public void synchronizeConfig(SplitClientConfig config, long timeUntilReady, Map factoryInstances, List tags) { + String key = String.format("%s/%s/%s", _sdkMetadata.getSdkVersion(), _sdkMetadata.getMachineName(), _sdkMetadata.getMachineIp()); + _userStorageWrapper.hSet(PrefixAdapter.buildTelemetryInit(), key, Json.toJson(generateConfig(config, factoryInstances, tags))); + } + + @Override + public void synchronizeStats() { + //No-op + } + + @Override + public void synchronizeUniqueKeys(UniqueKeys uniqueKeys) { + List uniqueKeysToSend; + for (UniqueKeys.UniqueKey uniqueKey: uniqueKeys.uniqueKeys) { + uniqueKeysToSend = new ArrayList<>(Arrays.asList(Json.toJson(uniqueKey))); + _userStorageWrapper.pushItems(PrefixAdapter.buildUniqueKeys(), uniqueKeysToSend); + } + } + + @Override + public void finalSynchronization() { + //No-Op + } + + @VisibleForTesting + ConfigConsumer generateConfig(SplitClientConfig splitClientConfig, Map factoryInstances, List tags) { + ConfigConsumer config = new ConfigConsumer(); + config.setOperationMode(splitClientConfig.operationMode()== OperationMode.STANDALONE ? 0 : 1); + config.setStorage(STORAGE); + config.setActiveFactories(factoryInstances.size()); + config.setRedundantFactories(getRedundantFactories(factoryInstances)); + config.setTags(tags.size() < 10 ? tags : tags.subList(0, 10)); + return config; + } + + private long getRedundantFactories(Map factoryInstances) { + return factoryInstances.values().stream().mapToLong(l -> l - 1L).sum(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/storages/pluggable/utils/Helper.java b/client/src/main/java/io/split/storages/pluggable/utils/Helper.java new file mode 100644 index 00000000..430aec1a --- /dev/null +++ b/client/src/main/java/io/split/storages/pluggable/utils/Helper.java @@ -0,0 +1,24 @@ +package io.split.storages.pluggable.utils; + +import io.split.client.utils.Json; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Helper { + + private static final Logger _log = LoggerFactory.getLogger(Helper.class); + + public static long responseToLong(String wrapperResponse, long defaultValue) { + long response = defaultValue; + if(wrapperResponse==null) { + return response; + } + try{ + response = Json.fromJson(wrapperResponse, Long.class); + } + catch(Exception e) { + _log.info("Error getting long value from String."); + } + return response; + } +} diff --git a/client/src/main/java/io/split/telemetry/domain/Config.java b/client/src/main/java/io/split/telemetry/domain/Config.java new file mode 100644 index 00000000..f5369ea7 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/Config.java @@ -0,0 +1,218 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class Config { + /* package private */ static final String FIELD_OPERATION_MODE = "oM"; + /* package private */ static final String FIELD_STREAMING_ENABLED = "sE"; + /* package private */ static final String FIELD_STORAGE = "st"; + /* package private */ static final String FIELD_RATES = "rR"; + /* package private */ static final String FIELD_URL_OVERRIDES = "uO"; + /* package private */ static final String FIELD_IMPRESSIONS_QUEUE = "iQ"; + /* package private */ static final String FIELD_EVENT_QUEUE = "eQ"; + /* package private */ static final String FIELD_IMPRESSIONS_MODE = "iM"; + /* package private */ static final String FIELD_IMPRESSIONS_LISTENER = "iL"; + /* package private */ static final String FIELD_HTTP_PROXY_DETECTED = "hP"; + /* package private */ static final String FIELD_ACTIVE_FACTORIES = "aF"; + /* package private */ static final String FIELD_REDUNDANT_FACTORIES = "rF"; + /* package private */ static final String FIELD_TIME_UNTIL_READY = "tR"; + /* package private */ static final String FIELD_BUR_TIMEOUTS = "bT"; + /* package private */ static final String FIELD_NON_READY_USAGES = "nR"; + /* package private */ static final String FIELD_INTEGRATIONS = "i"; + /* package private */ static final String FIELD_TAGS = "t"; + /* package private */ static final String FIELD_FLAG_SETS_TOTAL = "fsT"; + /* package private */ static final String FIELD_FLAG_SETS_INVALID = "fsI"; + + @SerializedName(FIELD_OPERATION_MODE) + private int _operationMode; + @SerializedName(FIELD_STREAMING_ENABLED) + private boolean _streamingEnabled; + @SerializedName(FIELD_STORAGE) + private String _storage; + @SerializedName(FIELD_RATES) + private Rates _rates; + @SerializedName(FIELD_URL_OVERRIDES) + private URLOverrides _urlOverrides; + @SerializedName(FIELD_IMPRESSIONS_QUEUE) + private long _impressionsQueueSize; + @SerializedName(FIELD_EVENT_QUEUE) + private long _eventsQueueSize; + @SerializedName(FIELD_IMPRESSIONS_MODE) + private int _impressionsMode; + @SerializedName(FIELD_IMPRESSIONS_LISTENER) + private boolean _impressionsListenerEnabled; + @SerializedName(FIELD_HTTP_PROXY_DETECTED) + private boolean _httpProxyDetected; + @SerializedName(FIELD_ACTIVE_FACTORIES) + private long _activeFactories; + @SerializedName(FIELD_REDUNDANT_FACTORIES) + private long _redundantFactories; + @SerializedName(FIELD_TIME_UNTIL_READY) + private long _timeUntilReady; + @SerializedName(FIELD_BUR_TIMEOUTS) + private long _burTimeouts; + @SerializedName(FIELD_NON_READY_USAGES) + private long _nonReadyUsages; + @SerializedName(FIELD_INTEGRATIONS) + private List _integrations; + @SerializedName(FIELD_TAGS) + private List _tags; + @SerializedName(FIELD_FLAG_SETS_TOTAL) + private int _flagSetsTotal; + @SerializedName(FIELD_FLAG_SETS_INVALID) + private int _flagSetsInvalid; + + public int getOperationMode() { + return _operationMode; + } + + public void setOperationMode(int operationMode) { + this._operationMode = operationMode; + } + + public boolean isStreamingEnabled() { + return _streamingEnabled; + } + + public void setStreamingEnabled(boolean streamingEnabled) { + this._streamingEnabled = streamingEnabled; + } + + public String getStorage() { + return _storage; + } + + public void setStorage(String storage) { + this._storage = storage; + } + + public Rates getRates() { + return _rates; + } + + public void setRates(Rates rates) { + this._rates = rates; + } + + public URLOverrides getUrlOverrides() { + return _urlOverrides; + } + + public void setUrlOverrides(URLOverrides urlOverrides) { + this._urlOverrides = urlOverrides; + } + + public long getImpressionsQueueSize() { + return _impressionsQueueSize; + } + + public void setImpressionsQueueSize(long impressionsQueueSize) { + this._impressionsQueueSize = impressionsQueueSize; + } + + public long getEventsQueueSize() { + return _eventsQueueSize; + } + + public void setEventsQueueSize(long eventsQueueSize) { + this._eventsQueueSize = eventsQueueSize; + } + + public int getImpressionsMode() { + return _impressionsMode; + } + + public void setImpressionsMode(int impressionsMode) { + this._impressionsMode = impressionsMode; + } + + public boolean isImpressionsListenerEnabled() { + return _impressionsListenerEnabled; + } + + public void setImpressionsListenerEnabled(boolean impressionsListenerEnabled) { + this._impressionsListenerEnabled = impressionsListenerEnabled; + } + + public boolean isHttpProxyDetected() { + return _httpProxyDetected; + } + + public void setHttpProxyDetected(boolean httpProxyDetected) { + this._httpProxyDetected = httpProxyDetected; + } + + public long getActiveFactories() { + return _activeFactories; + } + + public void setActiveFactories(long activeFactories) { + this._activeFactories = activeFactories; + } + + public long getRedundantFactories() { + return _redundantFactories; + } + + public void setRedundantFactories(long redundantFactories) { + this._redundantFactories = redundantFactories; + } + + public long getTimeUntilReady() { + return _timeUntilReady; + } + + public void setTimeUntilReady(long timeUntilReady) { + this._timeUntilReady = timeUntilReady; + } + + public long getBurTimeouts() { + return _burTimeouts; + } + + public void setBurTimeouts(long burTimeouts) { + this._burTimeouts = burTimeouts; + } + + public long getNonReadyUsages() { + return _nonReadyUsages; + } + + public void setNonReadyUsages(long nonReadyUsages) { + this._nonReadyUsages = nonReadyUsages; + } + + public List getIntegrations() { + return _integrations; + } + + public void setIntegrations(List integrations) { + this._integrations = integrations; + } + + public List getTags() { + return _tags; + } + + public void setTags(List tags) { + this._tags = tags; + } + + public int getFlagSetsTotal() { + return _flagSetsTotal; + } + + public int getFlagSetsInvalid() { + return _flagSetsInvalid; + } + + public void setFlagSetsTotal(int flagSetsTotal) { + this._flagSetsTotal = flagSetsTotal; + } + + public void setFlagSetsInvalid(int flagSetsInvalid) { + this._flagSetsInvalid = flagSetsInvalid; + } +} diff --git a/client/src/main/java/io/split/telemetry/domain/HTTPErrors.java b/client/src/main/java/io/split/telemetry/domain/HTTPErrors.java new file mode 100644 index 00000000..69c85ad8 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/HTTPErrors.java @@ -0,0 +1,97 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class HTTPErrors { + /* package private */ static final String FIELD_SPLIT = "sp"; + /* package private */ static final String FIELD_SEGMENTS = "se"; + /* package private */ static final String FIELD_IMPRESSIONS = "im"; + /* package private */ static final String FIELD_IMPRESSIONS_COUNT = "ic"; + /* package private */ static final String FIELD_EVENTS = "ev"; + /* package private */ static final String FIELD_TOKEN = "to"; + /* package private */ static final String FIELD_TELEMETRY = "te"; + + @SerializedName(FIELD_SPLIT) + private Map _splits; + @SerializedName(FIELD_SEGMENTS) + private Map _segments; + @SerializedName(FIELD_IMPRESSIONS) + private Map _impressions; + @SerializedName(FIELD_IMPRESSIONS_COUNT) + private Map _impressionsCount; + @SerializedName(FIELD_EVENTS) + private Map _events; + @SerializedName(FIELD_TOKEN) + private Map _token; + @SerializedName(FIELD_TELEMETRY) + private Map _telemetry; + + public HTTPErrors() { + _splits = new ConcurrentHashMap<>(); + _segments = new ConcurrentHashMap<>(); + _impressions = new ConcurrentHashMap<>(); + _impressionsCount = new ConcurrentHashMap<>(); + _events = new ConcurrentHashMap<>(); + _token = new ConcurrentHashMap<>(); + _telemetry = new ConcurrentHashMap<>(); + } + + public Map getSplits() { + return _splits; + } + + public void setSplits(Map _splits) { + this._splits = _splits; + } + + public Map getSegments() { + return _segments; + } + + public void setSegments(Map _segments) { + this._segments = _segments; + } + + public Map getImpressions() { + return _impressions; + } + + public void setImpressions(Map _impressions) { + this._impressions = _impressions; + } + + public Map getEvents() { + return _events; + } + + public void setEvents(Map _events) { + this._events = _events; + } + + public Map getToken() { + return _token; + } + + public void setToken(Map _token) { + this._token = _token; + } + + public Map getTelemetry() { + return _telemetry; + } + + public void setTelemetry(Map _telemetry) { + this._telemetry = _telemetry; + } + + public Map getImpressionsCount() { + return _impressionsCount; + } + + public void setImpressionsCount(Map _impressionsCount) { + this._impressionsCount = _impressionsCount; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/HTTPLatencies.java b/client/src/main/java/io/split/telemetry/domain/HTTPLatencies.java new file mode 100644 index 00000000..1b7ec2f8 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/HTTPLatencies.java @@ -0,0 +1,97 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.List; + +public class HTTPLatencies { + /* package private */ static final String FIELD_SPLIT = "sp"; + /* package private */ static final String FIELD_SEGMENTS = "se"; + /* package private */ static final String FIELD_IMPRESSIONS = "im"; + /* package private */ static final String FIELD_IMPRESSIONS_COUNT = "ic"; + /* package private */ static final String FIELD_EVENTS = "ev"; + /* package private */ static final String FIELD_TOKEN = "to"; + /* package private */ static final String FIELD_TELEMETRY = "te"; + + @SerializedName(FIELD_SPLIT) + private List _splits; + @SerializedName(FIELD_SEGMENTS) + private List_segments; + @SerializedName(FIELD_IMPRESSIONS) + private List _impressions; + @SerializedName(FIELD_IMPRESSIONS_COUNT) + private List _impressionsCount; + @SerializedName(FIELD_EVENTS) + private List _events; + @SerializedName(FIELD_TOKEN) + private List _token; + @SerializedName(FIELD_TELEMETRY) + private List _telemetry; + + public HTTPLatencies() { + _splits = new ArrayList<>(); + _segments = new ArrayList<>(); + _impressions = new ArrayList<>(); + _impressionsCount = new ArrayList<>(); + _events = new ArrayList<>(); + _token = new ArrayList<>(); + _telemetry = new ArrayList<>(); + } + + public List getSplits() { + return _splits; + } + + public void setSplits(List _splits) { + this._splits = _splits; + } + + public List getSegments() { + return _segments; + } + + public void setSegments(List _segments) { + this._segments = _segments; + } + + public List getImpressions() { + return _impressions; + } + + public void setImpressions(List _impressions) { + this._impressions = _impressions; + } + + public List getEvents() { + return _events; + } + + public void setEvents(List _events) { + this._events = _events; + } + + public List getToken() { + return _token; + } + + public void setToken(List _token) { + this._token = _token; + } + + public List getTelemetry() { + return _telemetry; + } + + public void setTelemetry(List _telemetry) { + this._telemetry = _telemetry; + } + + public List getImpressionsCount() { + return _impressionsCount; + } + + public void setImpressionsCount(List _impressionsCount) { + this._impressionsCount = _impressionsCount; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/LastSynchronization.java b/client/src/main/java/io/split/telemetry/domain/LastSynchronization.java new file mode 100644 index 00000000..74889e1f --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/LastSynchronization.java @@ -0,0 +1,84 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class LastSynchronization { + /* package private */ static final String FIELD_SPLIT = "sp"; + /* package private */ static final String FIELD_SEGMENTS = "se"; + /* package private */ static final String FIELD_IMPRESSIONS = "im"; + /* package private */ static final String FIELD_IMPRESSIONS_COUNT = "ic"; + /* package private */ static final String FIELD_EVENTS = "ev"; + /* package private */ static final String FIELD_TOKEN = "to"; + /* package private */ static final String FIELD_TELEMETRY = "te"; + + @SerializedName(FIELD_SPLIT) + private long _splits; + @SerializedName(FIELD_SEGMENTS) + private long _segments; + @SerializedName(FIELD_IMPRESSIONS) + private long _impressions; + @SerializedName(FIELD_IMPRESSIONS_COUNT) + private long _impressionsCount; + @SerializedName(FIELD_EVENTS) + private long _events; + @SerializedName(FIELD_TOKEN) + private long _token; + @SerializedName(FIELD_TELEMETRY) + private long _telemetry; + + public long getSplits() { + return _splits; + } + + public void setSplits(long _splits) { + this._splits = _splits; + } + + public long getSegments() { + return _segments; + } + + public void setSegments(long _segments) { + this._segments = _segments; + } + + public long getImpressions() { + return _impressions; + } + + public void setImpressions(long _impressions) { + this._impressions = _impressions; + } + + public long getEvents() { + return _events; + } + + public void setEvents(long _events) { + this._events = _events; + } + + public long getToken() { + return _token; + } + + public void setToken(long _token) { + this._token = _token; + } + + public long getTelemetry() { + return _telemetry; + } + + public void setTelemetry(long _telemetry) { + this._telemetry = _telemetry; + } + + public long getImpressionsCount() { + return _impressionsCount; + } + + public void setImpressionsCount(long _impressionsCount) { + this._impressionsCount = _impressionsCount; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/MethodExceptions.java b/client/src/main/java/io/split/telemetry/domain/MethodExceptions.java new file mode 100644 index 00000000..c5298e8d --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/MethodExceptions.java @@ -0,0 +1,106 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class MethodExceptions { + /* package private */ static final String FIELD_TREATMENT = "t"; + /* package private */ static final String FIELD_TREATMENTS = "ts"; + /* package private */ static final String FIELD_TREATMENT_WITH_CONFIG = "tc"; + /* package private */ static final String FIELD_TREATMENTS_WITH_CONFIG = "tcs"; + /* package private */ static final String FIELD_TREATMENT_BY_FLAG_SET = "tf"; + /* package private */static final String FIELD_TREATMENT_BY_FLAG_SETS = "tfs"; + /* package private */static final String FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SET = "tcf"; + /* package private */static final String FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SETS = "tcfs"; + /* package private */ static final String FIELD_TRACK = "tr"; + + @SerializedName(FIELD_TREATMENT) + private long _treatment; + @SerializedName(FIELD_TREATMENTS) + private long _treatments; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG) + private long _treatmentWithConfig; + @SerializedName(FIELD_TREATMENTS_WITH_CONFIG) + private long _treatmentsWithConfig; + @SerializedName(FIELD_TREATMENT_BY_FLAG_SET) + private Long _treatmentByFlagSet; + @SerializedName(FIELD_TREATMENT_BY_FLAG_SETS) + private Long _treatmentByFlagSets; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SET) + private Long _treatmentWithConfigByFlagSet; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SETS) + private Long _treatmentWithConfigByFlagSets; + @SerializedName(FIELD_TRACK) + private long _track; + + public long getTreatment() { + return _treatment; + } + + public void setTreatment(long treatment) { + this._treatment = treatment; + } + + public long getTreatments() { + return _treatments; + } + + public void setTreatments(long treatments) { + this._treatments = treatments; + } + + public long getTreatmentsWithConfig() { + return _treatmentsWithConfig; + } + + public void setTreatmentsWithConfig(long treatmentsWithConfig) { + this._treatmentsWithConfig = treatmentsWithConfig; + } + + public long getTreatmentWithConfig() { + return _treatmentWithConfig; + } + + public void setTreatmentWithConfig(long treatmentWithConfig) { + this._treatmentWithConfig = treatmentWithConfig; + } + + public long getTrack() { + return _track; + } + + public void setTrack(long track) { + this._track = track; + } + + public long getTreatmentByFlagSet() { + return _treatmentByFlagSet; + } + + public long getTreatmentByFlagSets() { + return _treatmentByFlagSets; + } + + public long getTreatmentWithConfigByFlagSet() { + return _treatmentWithConfigByFlagSet; + } + + public long getTreatmentWithConfigByFlagSets() { + return _treatmentWithConfigByFlagSets; + } + + public void setTreatmentByFlagSet(Long treatmentByFlagSet) { + this._treatmentByFlagSet = treatmentByFlagSet; + } + + public void setTreatmentByFlagSets(Long treatmentByFlagSets) { + this._treatmentByFlagSets = treatmentByFlagSets; + } + + public void setTreatmentWithConfigByFlagSet(Long treatmentWithConfigByFlagSet) { + this._treatmentWithConfigByFlagSet = treatmentWithConfigByFlagSet; + } + + public void setTreatmentWithConfigByFlagSets(Long treatmentWithConfigByFlagSets) { + this._treatmentWithConfigByFlagSets = treatmentWithConfigByFlagSets; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/MethodLatencies.java b/client/src/main/java/io/split/telemetry/domain/MethodLatencies.java new file mode 100644 index 00000000..bed8df30 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/MethodLatencies.java @@ -0,0 +1,121 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.List; + +public class MethodLatencies { + /* package private */ static final String FIELD_TREATMENT = "t"; + /* package private */ static final String FIELD_TREATMENTS = "ts"; + /* package private */ static final String FIELD_TREATMENT_WITH_CONFIG = "tc"; + /* package private */ static final String FIELD_TREATMENTS_WITH_CONFIG = "tcs"; + /* package private */ static final String FIELD_TREATMENT_BY_FLAG_SET = "tf"; + /* package private */static final String FIELD_TREATMENT_BY_FLAG_SETS = "tfs"; + /* package private */static final String FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SET = "tcf"; + /* package private */static final String FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SETS = "tcfs"; + /* package private */ static final String FIELD_TRACK = "tr"; + + @SerializedName(FIELD_TREATMENT) + private List _treatment; + @SerializedName(FIELD_TREATMENTS) + private List _treatments; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG) + private List _treatmentWithConfig; + @SerializedName(FIELD_TREATMENTS_WITH_CONFIG) + private List _treatmentsWithConfig; + @SerializedName(FIELD_TREATMENT_BY_FLAG_SET) + private List _treatmentByFlagSet; + @SerializedName(FIELD_TREATMENT_BY_FLAG_SETS) + private List _treatmentByFlagSets; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SET) + private List _treatmentWithConfigByFlagSet; + @SerializedName(FIELD_TREATMENT_WITH_CONFIG_BY_FLAG_SETS) + private List _treatmentWithConfigByFlagSets; + @SerializedName(FIELD_TRACK) + private List _track; + + public MethodLatencies() { + _treatment = new ArrayList<>(); + _treatments = new ArrayList<>(); + _treatmentWithConfig = new ArrayList<>(); + _treatmentsWithConfig = new ArrayList<>(); + _treatmentByFlagSet = new ArrayList<>(); + _treatmentByFlagSets = new ArrayList<>(); + _treatmentWithConfigByFlagSet = new ArrayList<>(); + _treatmentWithConfigByFlagSets = new ArrayList<>(); + _track = new ArrayList<>(); + } + + public List getTreatment() { + return _treatment; + } + + public void setTreatment(List treatment) { + this._treatment = treatment; + } + + public List getTreatments() { + return _treatments; + } + + public void setTreatments(List treatments) { + this._treatments = treatments; + } + + public List getTreatmentsWithConfig() { + return _treatmentsWithConfig; + } + + public void setTreatmentsWithConfig(List treatmentsWithConfig) { + this._treatmentsWithConfig = treatmentsWithConfig; + } + + public List getTreatmentWithConfig() { + return _treatmentWithConfig; + } + + public void setTreatmentWithConfig(List treatmentWithConfig) { + this._treatmentWithConfig = treatmentWithConfig; + } + + public List getTrack() { + return _track; + } + + public void setTrack(List track) { + this._track = track; + } + + public List getTreatmentByFlagSet() { + return _treatmentByFlagSet; + } + + public List getTreatmentByFlagSets() { + return _treatmentByFlagSets; + } + + public void setTreatmentByFlagSet(List treatmentByFlagSet) { + this._treatmentByFlagSet = treatmentByFlagSet; + } + + public void setTreatmentByFlagSets(List treatmentByFlagSets) { + this._treatmentByFlagSets = treatmentByFlagSets; + } + + public List getTreatmentWithConfigByFlagSet() { + return _treatmentWithConfigByFlagSet; + } + + public void setTreatmentWithConfigByFlagSet(List treatmentWithConfigByFlagSet) { + this._treatmentWithConfigByFlagSet = treatmentWithConfigByFlagSet; + } + + public void setTreatmentWithConfigByFlagSets(List treatmentWithConfigByFlagSets) { + this._treatmentWithConfigByFlagSets = treatmentWithConfigByFlagSets; + } + + public List getTreatmentWithConfigByFlagSets() { + return _treatmentWithConfigByFlagSets; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/Rates.java b/client/src/main/java/io/split/telemetry/domain/Rates.java new file mode 100644 index 00000000..0ae5efda --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/Rates.java @@ -0,0 +1,62 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class Rates { + /* package private */ static final String FIELD_SPLITS = "sp"; + /* package private */ static final String FIELD_SEGMENTS = "se"; + /* package private */ static final String FIELD_IMPRESSIONS = "im"; + /* package private */ static final String FIELD_EVENTS = "ev"; + /* package private */ static final String FIELD_TELEMETRY = "te"; + + @SerializedName(FIELD_SPLITS) + private long _splits; + @SerializedName(FIELD_SEGMENTS) + private long _segments; + @SerializedName(FIELD_IMPRESSIONS) + private long _impressions; + @SerializedName(FIELD_EVENTS) + private long _events; + @SerializedName(FIELD_TELEMETRY) + private long _telemetry; + + public long getSplits() { + return _splits; + } + + public void setSplits(long _splits) { + this._splits = _splits; + } + + public long getSegments() { + return _segments; + } + + public void setSegments(long _segments) { + this._segments = _segments; + } + + public long getImpressions() { + return _impressions; + } + + public void setImpressions(long _impressions) { + this._impressions = _impressions; + } + + public long getEvents() { + return _events; + } + + public void setEvents(long _events) { + this._events = _events; + } + + public long getTelemetry() { + return _telemetry; + } + + public void setTelemetry(long _telemetry) { + this._telemetry = _telemetry; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/Stats.java b/client/src/main/java/io/split/telemetry/domain/Stats.java new file mode 100644 index 00000000..55882cdc --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/Stats.java @@ -0,0 +1,218 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +public class Stats { + /* package private */ static final String FIELD_LAST_SYNCHRONIZATION = "lS"; + /* package private */ static final String FIELD_METHOD_LATENCIES = "ml"; + /* package private */ static final String FIELD_METHOD_EXCEPTIONS = "mE"; + /* package private */ static final String FIELD_HTTP_ERRORS = "hE"; + /* package private */ static final String FIELD_HTTP_LATENCIES = "hL"; + /* package private */ static final String FIELD_TOKEN_REFRESHES = "tR"; + /* package private */ static final String FIELD_AUTH_REJECTIONS = "aR"; + /* package private */ static final String FIELD_IMPRESSIONS_QUEUED = "iQ"; + /* package private */ static final String FIELD_IMPRESSIONS_DEDUPED = "iDe"; + /* package private */ static final String FIELD_IMPRESSIONS_DROPPED = "iDr"; + /* package private */ static final String FIELD_SPLITS = "spC"; + /* package private */ static final String FIELD_SEGMENTS = "seC"; + /* package private */ static final String FIELD_SEGMENTS_KEY = "skC"; + /* package private */ static final String FIELD_SESSION_LENGHT = "sL"; + /* package private */ static final String FIELD_EVENTS_QUEUED = "eQ"; + /* package private */ static final String FIELD_EVENTS_DROPPED = "eD"; + /* package private */ static final String FIELD_STREAMING_EVENT = "sE"; + /* package private */ static final String FIELD_TAGS = "t"; + /* package private */ static final String FIELD_UPDATES_FROM_SSE = "ufs"; + + @SerializedName(FIELD_LAST_SYNCHRONIZATION) + private LastSynchronization _lastSynchronization; + @SerializedName(FIELD_METHOD_LATENCIES) + private MethodLatencies _methodLatencies; + @SerializedName(FIELD_METHOD_EXCEPTIONS) + private MethodExceptions _methodExceptions; + @SerializedName(FIELD_HTTP_ERRORS) + private HTTPErrors _httpErrors; + @SerializedName(FIELD_HTTP_LATENCIES) + private HTTPLatencies _httpLatencies; + @SerializedName(FIELD_TOKEN_REFRESHES) + private long _tokenRefreshes; + @SerializedName(FIELD_AUTH_REJECTIONS) + private long _authRejections; + @SerializedName(FIELD_IMPRESSIONS_QUEUED) + private long _impressionsQueued; + @SerializedName(FIELD_IMPRESSIONS_DEDUPED) + private long _impressionsDeduped; + @SerializedName(FIELD_IMPRESSIONS_DROPPED) + private long _impressionsDropped; + @SerializedName(FIELD_SPLITS) + private long _splitCount; + @SerializedName(FIELD_SEGMENTS) + private long _segmentCount; + @SerializedName(FIELD_SEGMENTS_KEY) + private long _segmentKeyCount; + @SerializedName(FIELD_SESSION_LENGHT) + private long _sessionLengthMs; + @SerializedName(FIELD_EVENTS_QUEUED) + private long _eventsQueued; + @SerializedName(FIELD_EVENTS_DROPPED) + private long _eventsDropped; + @SerializedName(FIELD_STREAMING_EVENT) + private List _streamingEvents; + @SerializedName(FIELD_TAGS) + private List _tags; + @SerializedName(FIELD_UPDATES_FROM_SSE) + private UpdatesFromSSE _updatesFromSSE; + + public LastSynchronization getLastSynchronization() { + return _lastSynchronization; + } + + public void setLastSynchronization(LastSynchronization lastSynchronization) { + this._lastSynchronization = lastSynchronization; + } + + public MethodLatencies getMethodLatencies() { + return _methodLatencies; + } + + public void setMethodLatencies(MethodLatencies methodLatencies) { + this._methodLatencies = methodLatencies; + } + + public MethodExceptions getMethodExceptions() { + return _methodExceptions; + } + + public void setMethodExceptions(MethodExceptions methodExceptions) { + this._methodExceptions = methodExceptions; + } + + public HTTPErrors getHttpErrors() { + return _httpErrors; + } + + public void setHttpErrors(HTTPErrors httpErrors) { + this._httpErrors = httpErrors; + } + + public HTTPLatencies getHttpLatencies() { + return _httpLatencies; + } + + public void setHttpLatencies(HTTPLatencies httpLatencies) { + this._httpLatencies = httpLatencies; + } + + public long getTokenRefreshes() { + return _tokenRefreshes; + } + + public void setTokenRefreshes(long tokenRefreshes) { + this._tokenRefreshes = tokenRefreshes; + } + + public long getAuthRejections() { + return _authRejections; + } + + public void setAuthRejections(long authRejections) { + this._authRejections = authRejections; + } + + public long getImpressionsQueued() { + return _impressionsQueued; + } + + public void setImpressionsQueued(long impressionsQueued) { + this._impressionsQueued = impressionsQueued; + } + + public long getImpressionsDeduped() { + return _impressionsDeduped; + } + + public void setImpressionsDeduped(long impressionsDeduped) { + this._impressionsDeduped = impressionsDeduped; + } + + public long getImpressionsDropped() { + return _impressionsDropped; + } + + public void setImpressionsDropped(long impressionsDropped) { + this._impressionsDropped = impressionsDropped; + } + + public long getSplitCount() { + return _splitCount; + } + + public void setSplitCount(long splitCount) { + this._splitCount = splitCount; + } + + public long getSegmentCount() { + return _segmentCount; + } + + public void setSegmentCount(long segmentCount) { + this._segmentCount = segmentCount; + } + + public long getSegmentKeyCount() { + return _segmentKeyCount; + } + + public void setSegmentKeyCount(long segmentKeyCount) { + this._segmentKeyCount = segmentKeyCount; + } + + public long getSessionLengthMs() { + return _sessionLengthMs; + } + + public void setSessionLengthMs(long sessionLengthMs) { + this._sessionLengthMs = sessionLengthMs; + } + + public long getEventsQueued() { + return _eventsQueued; + } + + public void setEventsQueued(long eventsQueued) { + this._eventsQueued = eventsQueued; + } + + public long getEventsDropped() { + return _eventsDropped; + } + + public void setEventsDropped(long eventsDropped) { + this._eventsDropped = eventsDropped; + } + + public List getStreamingEvents() { + return _streamingEvents; + } + + public void setStreamingEvents(List streamingEvents) { + this._streamingEvents = streamingEvents; + } + + public List getTags() { + return _tags; + } + + public void setTags(List tags) { + this._tags = tags; + } + + public UpdatesFromSSE getUpdatesFromSSE() { + return _updatesFromSSE; + } + + public void setUpdatesFromSSE(UpdatesFromSSE updatesFromSSE) { + this._updatesFromSSE = updatesFromSSE; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/StreamingEvent.java b/client/src/main/java/io/split/telemetry/domain/StreamingEvent.java new file mode 100644 index 00000000..9fd3ae56 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/StreamingEvent.java @@ -0,0 +1,46 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class StreamingEvent { + /* package private */ static final String FIELD_TYPE = "e"; + /* package private */ static final String FIELD_DATA = "d"; + /* package private */ static final String FIELD_TIMESTAMP = "t"; + + @SerializedName(FIELD_TYPE) + private int _type; + @SerializedName(FIELD_DATA) + private long _data; + @SerializedName(FIELD_TIMESTAMP) + private long _timestamp; + + public StreamingEvent(int _type, long _data, long _timestamp) { + this._type = _type; + this._data = _data; + this._timestamp = _timestamp; + } + + public int getType() { + return _type; + } + + public void setType(int _type) { + this._type = _type; + } + + public long getData() { + return _data; + } + + public void setData(long _data) { + this._data = _data; + } + + public long getTimestamp() { + return _timestamp; + } + + public void setTimestamp(long timestamp) { + this._timestamp = timestamp; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/URLOverrides.java b/client/src/main/java/io/split/telemetry/domain/URLOverrides.java new file mode 100644 index 00000000..587fdf3d --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/URLOverrides.java @@ -0,0 +1,62 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class URLOverrides { + /* package private */ static final String FIELD_SDK = "s"; + /* package private */ static final String FIELD_EVENTS = "e"; + /* package private */ static final String FIELD_AUTH = "a"; + /* package private */ static final String FIELD_STREAM = "st"; + /* package private */ static final String FIELD_TELEMETRY = "t"; + + @SerializedName(FIELD_SDK) + private boolean _sdk; + @SerializedName(FIELD_EVENTS) + private boolean _events; + @SerializedName(FIELD_AUTH) + private boolean _auth; + @SerializedName(FIELD_STREAM) + private boolean _stream; + @SerializedName(FIELD_TELEMETRY) + private boolean _telemetry; + + public boolean isSdk() { + return _sdk; + } + + public void setSdk(boolean _sdk) { + this._sdk = _sdk; + } + + public boolean isEvents() { + return _events; + } + + public void setEvents(boolean _events) { + this._events = _events; + } + + public boolean isAuth() { + return _auth; + } + + public void setAuth(boolean _auth) { + this._auth = _auth; + } + + public boolean isStream() { + return _stream; + } + + public void setStream(boolean _stream) { + this._stream = _stream; + } + + public boolean isTelemetry() { + return _telemetry; + } + + public void setTelemetry(boolean _telemetry) { + this._telemetry = _telemetry; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/UpdatesFromSSE.java b/client/src/main/java/io/split/telemetry/domain/UpdatesFromSSE.java new file mode 100644 index 00000000..1f2238f9 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/UpdatesFromSSE.java @@ -0,0 +1,19 @@ +package io.split.telemetry.domain; + +import com.google.gson.annotations.SerializedName; + +public class UpdatesFromSSE { + + /* package private */ static final String FIELD_FEATURE_FLAGS = "sp"; + + @SerializedName(FIELD_FEATURE_FLAGS) + private long splits; + + public long getSplits() { + return splits; + } + + public void setSplits(long splits) { + this.splits = splits; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/domain/enums/EventsDataRecordsEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/EventsDataRecordsEnum.java new file mode 100644 index 00000000..8beb3ac4 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/EventsDataRecordsEnum.java @@ -0,0 +1,6 @@ +package io.split.telemetry.domain.enums; + +public enum EventsDataRecordsEnum { + EVENTS_QUEUED, + EVENTS_DROPPED +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/FactoryCountersEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/FactoryCountersEnum.java new file mode 100644 index 00000000..38708461 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/FactoryCountersEnum.java @@ -0,0 +1,6 @@ +package io.split.telemetry.domain.enums; + +public enum FactoryCountersEnum { + BUR_TIMEOUTS, + NON_READY_USAGES +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/HTTPLatenciesEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/HTTPLatenciesEnum.java new file mode 100644 index 00000000..6f858397 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/HTTPLatenciesEnum.java @@ -0,0 +1,11 @@ +package io.split.telemetry.domain.enums; + +public enum HTTPLatenciesEnum { + SPLITS, + SEGMENTS, + IMPRESSIONS, + IMPRESSIONS_COUNT, + EVENTS, + TELEMETRY, + TOKEN +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/HttpParamsWrapper.java b/client/src/main/java/io/split/telemetry/domain/enums/HttpParamsWrapper.java new file mode 100644 index 00000000..329f8bb9 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/HttpParamsWrapper.java @@ -0,0 +1,34 @@ +package io.split.telemetry.domain.enums; + +public enum HttpParamsWrapper { + EVENTS(HTTPLatenciesEnum.EVENTS, LastSynchronizationRecordsEnum.EVENTS, ResourceEnum.EVENT_SYNC), + TELEMETRY(HTTPLatenciesEnum.TELEMETRY, LastSynchronizationRecordsEnum.TELEMETRY, ResourceEnum.TELEMETRY_SYNC), + IMPRESSIONS(HTTPLatenciesEnum.IMPRESSIONS, LastSynchronizationRecordsEnum.IMPRESSIONS, ResourceEnum.IMPRESSION_SYNC), + IMPRESSIONS_COUNT(HTTPLatenciesEnum.IMPRESSIONS_COUNT, LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT, ResourceEnum.IMPRESSION_COUNT_SYNC), + SEGMENTS(HTTPLatenciesEnum.SEGMENTS, LastSynchronizationRecordsEnum.SEGMENTS, ResourceEnum.SEGMENT_SYNC), + SPLITS(HTTPLatenciesEnum.SPLITS, LastSynchronizationRecordsEnum.SPLITS, ResourceEnum.SPLIT_SYNC), + TOKEN(HTTPLatenciesEnum.TOKEN, LastSynchronizationRecordsEnum.TOKEN, ResourceEnum.TOKEN_SYNC); + + + private HTTPLatenciesEnum _httpLatenciesEnum; + private LastSynchronizationRecordsEnum _lastSynchronizationRecordsEnum; + private ResourceEnum _resourceEnum; + + HttpParamsWrapper(HTTPLatenciesEnum httpLatenciesEnum, LastSynchronizationRecordsEnum lastSynchronizationRecordsEnum, ResourceEnum resourceEnum) { + _httpLatenciesEnum = httpLatenciesEnum; + _lastSynchronizationRecordsEnum = lastSynchronizationRecordsEnum; + _resourceEnum = resourceEnum; + } + + public HTTPLatenciesEnum getHttpLatenciesEnum() { + return _httpLatenciesEnum; + } + + public LastSynchronizationRecordsEnum getLastSynchronizationRecordsEnum() { + return _lastSynchronizationRecordsEnum; + } + + public ResourceEnum getResourceEnum() { + return _resourceEnum; + } +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/ImpressionsDataTypeEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/ImpressionsDataTypeEnum.java new file mode 100644 index 00000000..3ca134c9 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/ImpressionsDataTypeEnum.java @@ -0,0 +1,7 @@ +package io.split.telemetry.domain.enums; + +public enum ImpressionsDataTypeEnum { + IMPRESSIONS_QUEUED, + IMPRESSIONS_DROPPED, + IMPRESSIONS_DEDUPED +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/LastSynchronizationRecordsEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/LastSynchronizationRecordsEnum.java new file mode 100644 index 00000000..d2439395 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/LastSynchronizationRecordsEnum.java @@ -0,0 +1,11 @@ +package io.split.telemetry.domain.enums; + +public enum LastSynchronizationRecordsEnum { + SPLITS, + SEGMENTS, + IMPRESSIONS, + IMPRESSIONS_COUNT, + EVENTS, + TOKEN, + TELEMETRY +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/MethodEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/MethodEnum.java new file mode 100644 index 00000000..27b72dbf --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/MethodEnum.java @@ -0,0 +1,23 @@ +package io.split.telemetry.domain.enums; + +public enum MethodEnum { + TREATMENT("getTreatment"), + TREATMENTS("getTreatments"), + TREATMENT_WITH_CONFIG("getTreatmentWithConfig"), + TREATMENTS_WITH_CONFIG("getTreatmentsWithConfig"), + TREATMENTS_BY_FLAG_SET("getTreatmentsByFlagSet"), + TREATMENTS_BY_FLAG_SETS("getTreatmentsByFlagSets"), + TREATMENTS_WITH_CONFIG_BY_FLAG_SET("getTreatmentsWithConfigByFlagSet"), + TREATMENTS_WITH_CONFIG_BY_FLAG_SETS("getTreatmentsWithConfigByFlagSets"), + TRACK("track"); + + private String _method; + + MethodEnum(String method) { + _method = method; + } + + public String getMethod() { + return _method; + } +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/PushCountersEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/PushCountersEnum.java new file mode 100644 index 00000000..53f023f7 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/PushCountersEnum.java @@ -0,0 +1,6 @@ +package io.split.telemetry.domain.enums; + +public enum PushCountersEnum { + AUTH_REJECTIONS, + TOKEN_REFRESHES +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/ResourceEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/ResourceEnum.java new file mode 100644 index 00000000..04af1116 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/ResourceEnum.java @@ -0,0 +1,11 @@ +package io.split.telemetry.domain.enums; + +public enum ResourceEnum { + SPLIT_SYNC, + SEGMENT_SYNC, + IMPRESSION_SYNC, + IMPRESSION_COUNT_SYNC, + EVENT_SYNC, + TELEMETRY_SYNC, + TOKEN_SYNC +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/SdkRecordsEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/SdkRecordsEnum.java new file mode 100644 index 00000000..6f878081 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/SdkRecordsEnum.java @@ -0,0 +1,5 @@ +package io.split.telemetry.domain.enums; + +public enum SdkRecordsEnum { + SESSION +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/StreamEventsEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/StreamEventsEnum.java new file mode 100644 index 00000000..30f977c6 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/StreamEventsEnum.java @@ -0,0 +1,69 @@ +package io.split.telemetry.domain.enums; + +public enum StreamEventsEnum { + CONNECTION_ESTABLISHED(0), + OCCUPANCY_PRI(10), + OCCUPANCY_SEC(20), + STREAMING_STATUS(30), + SSE_CONNECTION_ERROR(40), + TOKEN_REFRESH(50), + ABLY_ERROR(60), + SYNC_MODE_UPDATE(70); + + + private int _type; + + StreamEventsEnum(int type) { + _type = type; + } + + public int getType() { + return _type; + } + + public enum StreamingStatusValues { + STREAMING_DISABLED(0), + STREAMING_PAUSED(2), + STREAMING_ENABLED(1); + + private long _value; + + StreamingStatusValues(long value) { + _value = value; + } + + public long getValue() { + return _value; + } + } + + public enum SseConnectionErrorValues { + REQUESTED_CONNECTION_ERROR(0), + NON_REQUESTED_CONNECTION_ERROR (1); + + private long _value; + + SseConnectionErrorValues(long value) { + _value = value; + } + + public long getValue() { + return _value; + } + } + + public enum SyncModeUpdateValues { + STREAMING_EVENT(0), + POLLING_EVENT(1); + + private long _value; + + SyncModeUpdateValues(long value) { + _value = value; + } + + public long getValue() { + return _value; + } + } +} diff --git a/client/src/main/java/io/split/telemetry/domain/enums/UpdatesFromSSEEnum.java b/client/src/main/java/io/split/telemetry/domain/enums/UpdatesFromSSEEnum.java new file mode 100644 index 00000000..eceb872e --- /dev/null +++ b/client/src/main/java/io/split/telemetry/domain/enums/UpdatesFromSSEEnum.java @@ -0,0 +1,5 @@ +package io.split.telemetry.domain.enums; + +public enum UpdatesFromSSEEnum { + SPLITS +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/storage/InMemoryTelemetryStorage.java b/client/src/main/java/io/split/telemetry/storage/InMemoryTelemetryStorage.java new file mode 100644 index 00000000..6958f110 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/InMemoryTelemetryStorage.java @@ -0,0 +1,394 @@ +package io.split.telemetry.storage; + +import com.google.common.collect.Maps; + +import io.split.telemetry.domain.HTTPErrors; +import io.split.telemetry.domain.HTTPLatencies; +import io.split.telemetry.domain.LastSynchronization; +import io.split.telemetry.domain.MethodExceptions; +import io.split.telemetry.domain.MethodLatencies; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.UpdatesFromSSE; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.FactoryCountersEnum; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.domain.enums.PushCountersEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.domain.enums.SdkRecordsEnum; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; +import io.split.telemetry.utils.AtomicLongArray; +import io.split.telemetry.utils.BucketCalculator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; + +public class InMemoryTelemetryStorage implements TelemetryStorage{ + public static final int MAX_LATENCY_BUCKET_COUNT = 23; + public static final int MAX_STREAMING_EVENTS = 20; + public static final int MAX_TAGS = 10; + + //Latencies + private final ConcurrentMap _methodLatencies = Maps.newConcurrentMap(); + private final ConcurrentMap _httpLatencies = Maps.newConcurrentMap(); + + //Counters + private final ConcurrentMap _exceptionsCounters = Maps.newConcurrentMap(); + private final ConcurrentMap _pushCounters = Maps.newConcurrentMap(); + private final ConcurrentMap _factoryCounters = Maps.newConcurrentMap(); + + //Records + private final ConcurrentMap _impressionsDataRecords = Maps.newConcurrentMap(); + private final ConcurrentMap _eventsDataRecords = Maps.newConcurrentMap(); + private final ConcurrentMap _lastSynchronizationRecords = Maps.newConcurrentMap(); + private final ConcurrentMap _sdkRecords = Maps.newConcurrentMap(); + private final ConcurrentMap _updatesFromSSERecords = Maps.newConcurrentMap(); + + //HTTPErrors + private final ConcurrentMap> _httpErrors = Maps.newConcurrentMap(); + + //StreamingEvents + private final Object _streamingEventsLock = new Object(); + private List _streamingEvents = new ArrayList<>(); + + //Tags + private final Object _tagsLock = new Object(); + private Set _tags = new HashSet<>(); + + public InMemoryTelemetryStorage() { + initMethodLatencies(); + initHttpLatencies(); + initHttpErrors(); + initMethodExceptions(); + initFactoryCounters(); + initImpressionDataCounters(); + initPushCounters(); + initSdkRecords(); + initLastSynchronizationRecords(); + initEventDataRecords(); + initUpdatesFromSEE(); + } + + @Override + public long getBURTimeouts() { + return _factoryCounters.get(FactoryCountersEnum.BUR_TIMEOUTS).get(); + } + + @Override + public long getNonReadyUsages() { + return _factoryCounters.get(FactoryCountersEnum.NON_READY_USAGES).get(); + } + + @Override + public MethodExceptions popExceptions() { + MethodExceptions exceptions = new MethodExceptions(); + exceptions.setTreatment(_exceptionsCounters.get(MethodEnum.TREATMENT).getAndSet(0L)); + exceptions.setTreatments(_exceptionsCounters.get(MethodEnum.TREATMENTS).getAndSet(0L)); + exceptions.setTreatmentWithConfig(_exceptionsCounters.get(MethodEnum.TREATMENT_WITH_CONFIG).getAndSet(0L)); + exceptions.setTreatmentsWithConfig(_exceptionsCounters.get(MethodEnum.TREATMENTS_WITH_CONFIG).getAndSet(0L)); + exceptions.setTreatmentByFlagSet(_exceptionsCounters.get(MethodEnum.TREATMENTS_BY_FLAG_SET).getAndSet(0L)); + exceptions.setTreatmentByFlagSets(_exceptionsCounters.get(MethodEnum.TREATMENTS_BY_FLAG_SETS).getAndSet(0L)); + exceptions.setTreatmentWithConfigByFlagSet(_exceptionsCounters.get(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET).getAndSet(0L)); + exceptions.setTreatmentWithConfigByFlagSets(_exceptionsCounters.get(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS).getAndSet(0L)); + exceptions.setTrack(_exceptionsCounters.get(MethodEnum.TRACK).getAndSet(0L)); + + return exceptions; + } + + @Override + public MethodLatencies popLatencies() { + MethodLatencies latencies = new MethodLatencies(); + latencies.setTreatment(_methodLatencies.get(MethodEnum.TREATMENT).fetchAndClearAll()); + latencies.setTreatments(_methodLatencies.get(MethodEnum.TREATMENTS).fetchAndClearAll()); + latencies.setTreatmentWithConfig(_methodLatencies.get(MethodEnum.TREATMENT_WITH_CONFIG).fetchAndClearAll()); + latencies.setTreatmentsWithConfig(_methodLatencies.get(MethodEnum.TREATMENTS_WITH_CONFIG).fetchAndClearAll()); + latencies.setTreatmentByFlagSet(_methodLatencies.get(MethodEnum.TREATMENTS_BY_FLAG_SET).fetchAndClearAll()); + latencies.setTreatmentByFlagSets(_methodLatencies.get(MethodEnum.TREATMENTS_BY_FLAG_SETS).fetchAndClearAll()); + latencies.setTreatmentWithConfigByFlagSet(_methodLatencies.get(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET).fetchAndClearAll()); + latencies.setTreatmentWithConfigByFlagSets(_methodLatencies.get(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS).fetchAndClearAll()); + latencies.setTrack(_methodLatencies.get(MethodEnum.TRACK).fetchAndClearAll()); + + return latencies; + } + + @Override + public void recordNonReadyUsage() { + _factoryCounters.get(FactoryCountersEnum.NON_READY_USAGES).incrementAndGet(); + } + + @Override + public void recordBURTimeout() { + _factoryCounters.get(FactoryCountersEnum.BUR_TIMEOUTS).incrementAndGet(); + } + + + @Override + public void recordLatency(MethodEnum method, long latency) { + int bucket = BucketCalculator.getBucketForLatency(latency); + _methodLatencies.get(method).increment(bucket); + } + + @Override + public void recordException(MethodEnum method) { + _exceptionsCounters.get(method).incrementAndGet(); + } + + @Override + public long getImpressionsStats(ImpressionsDataTypeEnum dataType) { + return _impressionsDataRecords.get(dataType).get(); + } + + @Override + public long getEventStats(EventsDataRecordsEnum dataType) { + return _eventsDataRecords.get(dataType).get(); + } + + @Override + public LastSynchronization getLastSynchronization() { + LastSynchronization lastSynchronization = new LastSynchronization(); + lastSynchronization.setSplits(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.SPLITS).get()); + lastSynchronization.setSegments(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.SEGMENTS).get()); + lastSynchronization.setImpressions(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.IMPRESSIONS).get()); + lastSynchronization.setImpressionsCount(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT).get()); + lastSynchronization.setEvents(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.EVENTS).get()); + lastSynchronization.setTelemetry(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.TELEMETRY).get()); + lastSynchronization.setToken(_lastSynchronizationRecords.get(LastSynchronizationRecordsEnum.TOKEN).get()); + + return lastSynchronization; + } + + @Override + public HTTPErrors popHTTPErrors() { + HTTPErrors errors = new HTTPErrors(); + errors.setSplits(_httpErrors.get(ResourceEnum.SPLIT_SYNC)); + errors.setSegments(_httpErrors.get(ResourceEnum.SEGMENT_SYNC)); + errors.setImpressions(_httpErrors.get(ResourceEnum.IMPRESSION_SYNC)); + errors.setImpressionsCount(_httpErrors.get(ResourceEnum.IMPRESSION_COUNT_SYNC)); + errors.setEvents(_httpErrors.get(ResourceEnum.EVENT_SYNC)); + errors.setTelemetry(_httpErrors.get(ResourceEnum.TELEMETRY_SYNC)); + errors.setToken(_httpErrors.get(ResourceEnum.TOKEN_SYNC)); + + _httpErrors.clear(); + initHttpErrors(); + + return errors; + } + + @Override + public HTTPLatencies popHTTPLatencies(){ + HTTPLatencies latencies = new HTTPLatencies(); + latencies.setSplits(_httpLatencies.get(HTTPLatenciesEnum.SPLITS).fetchAndClearAll()); + latencies.setSegments(_httpLatencies.get(HTTPLatenciesEnum.SEGMENTS).fetchAndClearAll()); + latencies.setImpressions(_httpLatencies.get(HTTPLatenciesEnum.IMPRESSIONS).fetchAndClearAll()); + latencies.setImpressionsCount(_httpLatencies.get(HTTPLatenciesEnum.IMPRESSIONS_COUNT).fetchAndClearAll()); + latencies.setEvents(_httpLatencies.get(HTTPLatenciesEnum.EVENTS).fetchAndClearAll()); + latencies.setTelemetry(_httpLatencies.get(HTTPLatenciesEnum.TELEMETRY).fetchAndClearAll()); + latencies.setToken(_httpLatencies.get(HTTPLatenciesEnum.TOKEN).fetchAndClearAll()); + + return latencies; + } + + @Override + public long popAuthRejections() { + long authRejections = _pushCounters.get(PushCountersEnum.AUTH_REJECTIONS).get(); + + _pushCounters.replace(PushCountersEnum.AUTH_REJECTIONS, new AtomicLong()); + + return authRejections; + } + + @Override + public long popTokenRefreshes() { + long tokenRefreshes = _pushCounters.get(PushCountersEnum.TOKEN_REFRESHES).get(); + + _pushCounters.replace(PushCountersEnum.TOKEN_REFRESHES, new AtomicLong()); + + return tokenRefreshes; + } + + @Override + public List popStreamingEvents() { + synchronized (_streamingEventsLock) { + List streamingEvents = _streamingEvents; + _streamingEvents = new ArrayList<>(); + return streamingEvents; + } + } + + @Override + public List popTags() { + synchronized (_tagsLock) { + List tags = new ArrayList<>(_tags); + _tags = new HashSet<>(); + return tags; + } + } + + @Override + public long getSessionLength() { + return _sdkRecords.get(SdkRecordsEnum.SESSION).get(); + } + + @Override + public UpdatesFromSSE popUpdatesFromSSE() { + UpdatesFromSSE updatesFromSSE = new UpdatesFromSSE(); + updatesFromSSE.setSplits(_updatesFromSSERecords.get(UpdatesFromSSEEnum.SPLITS).getAndSet(0L)); + return updatesFromSSE; + } + + @Override + public void addTag(String tag) { + synchronized (_tagsLock) { + if(_tags.size() < MAX_TAGS) { + _tags.add(tag); + } + } + } + + @Override + public void recordImpressionStats(ImpressionsDataTypeEnum dataType, long count) { + _impressionsDataRecords.get(dataType).addAndGet(count); + } + + @Override + public void recordEventStats(EventsDataRecordsEnum dataType, long count) { + _eventsDataRecords.get(dataType).addAndGet(count); + } + + @Override + public void recordSuccessfulSync(LastSynchronizationRecordsEnum resource, long time) { + _lastSynchronizationRecords.replace(resource, new AtomicLong(time)); + } + + @Override + public void recordSyncError(ResourceEnum resource, int status) { + ConcurrentMap errors = _httpErrors.get(resource); + errors.putIfAbsent(Long.valueOf(status), 0l); + errors.replace(Long.valueOf(status), errors.get(Long.valueOf(status)) + 1); + } + + @Override + public void recordSyncLatency(HTTPLatenciesEnum resource, long latency) { + int bucket = BucketCalculator.getBucketForLatency(latency); + _httpLatencies.get(resource).increment(bucket); + + } + + @Override + public void recordAuthRejections() { + _pushCounters.get(PushCountersEnum.AUTH_REJECTIONS).incrementAndGet(); + } + + @Override + public void recordTokenRefreshes() { + _pushCounters.get(PushCountersEnum.TOKEN_REFRESHES).incrementAndGet(); + } + + @Override + public void recordStreamingEvents(StreamingEvent streamingEvent) { + synchronized (_streamingEventsLock) { + if(_streamingEvents.size() < MAX_STREAMING_EVENTS) { + _streamingEvents.add(streamingEvent); + } + } + } + + @Override + public void recordSessionLength(long sessionLength) { + _sdkRecords.replace(SdkRecordsEnum.SESSION, new AtomicLong(sessionLength)); + } + + @Override + public void recordUpdatesFromSSE(UpdatesFromSSEEnum updatesFromSSEEnum) { + _updatesFromSSERecords.get(UpdatesFromSSEEnum.SPLITS).incrementAndGet(); + } + + private void initMethodLatencies() { + _methodLatencies.put(MethodEnum.TREATMENT, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENT_WITH_CONFIG, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_WITH_CONFIG, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_BY_FLAG_SET, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_BY_FLAG_SETS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TRACK, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + } + + private void initHttpLatencies() { + _httpLatencies.put(HTTPLatenciesEnum.SPLITS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.SEGMENTS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.IMPRESSIONS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.IMPRESSIONS_COUNT, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.EVENTS, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.TELEMETRY, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _httpLatencies.put(HTTPLatenciesEnum.TOKEN, new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + } + + private void initHttpErrors() { + _httpErrors.put(ResourceEnum.SPLIT_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.SEGMENT_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.IMPRESSION_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.IMPRESSION_COUNT_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.EVENT_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.TELEMETRY_SYNC, Maps.newConcurrentMap()); + _httpErrors.put(ResourceEnum.TOKEN_SYNC, Maps.newConcurrentMap()); + } + + private void initMethodExceptions() { + _exceptionsCounters.put(MethodEnum.TREATMENT, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENT_WITH_CONFIG, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS_WITH_CONFIG, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS_BY_FLAG_SET, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS_BY_FLAG_SETS, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, new AtomicLong()); + _exceptionsCounters.put(MethodEnum.TRACK, new AtomicLong()); + } + + private void initFactoryCounters() { + _factoryCounters.put(FactoryCountersEnum.BUR_TIMEOUTS, new AtomicLong()); + _factoryCounters.put(FactoryCountersEnum.NON_READY_USAGES, new AtomicLong()); + } + + private void initImpressionDataCounters() { + _impressionsDataRecords.put(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, new AtomicLong()); + _impressionsDataRecords.put(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, new AtomicLong()); + _impressionsDataRecords.put(ImpressionsDataTypeEnum.IMPRESSIONS_QUEUED, new AtomicLong()); + } + + private void initPushCounters() { + _pushCounters.put(PushCountersEnum.AUTH_REJECTIONS, new AtomicLong()); + _pushCounters.put(PushCountersEnum.TOKEN_REFRESHES, new AtomicLong()); + } + + private void initSdkRecords() { + _sdkRecords.put(SdkRecordsEnum.SESSION, new AtomicLong()); + } + + private void initLastSynchronizationRecords() { + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.SPLITS, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.SEGMENTS, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.EVENTS, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.IMPRESSIONS, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.TOKEN, new AtomicLong()); + _lastSynchronizationRecords.put(LastSynchronizationRecordsEnum.TELEMETRY, new AtomicLong()); + } + + private void initEventDataRecords() { + _eventsDataRecords.put(EventsDataRecordsEnum.EVENTS_DROPPED, new AtomicLong()); + _eventsDataRecords.put(EventsDataRecordsEnum.EVENTS_QUEUED, new AtomicLong()); + } + + private void initUpdatesFromSEE() { + _updatesFromSSERecords.put(UpdatesFromSSEEnum.SPLITS, new AtomicLong()); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/storage/NoopTelemetryStorage.java b/client/src/main/java/io/split/telemetry/storage/NoopTelemetryStorage.java new file mode 100644 index 00000000..d18134a8 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/NoopTelemetryStorage.java @@ -0,0 +1,171 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.HTTPErrors; +import io.split.telemetry.domain.HTTPLatencies; +import io.split.telemetry.domain.LastSynchronization; +import io.split.telemetry.domain.MethodExceptions; +import io.split.telemetry.domain.MethodLatencies; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.UpdatesFromSSE; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; + +import java.util.List; + +public class NoopTelemetryStorage implements TelemetryStorage{ + + @Override + public void recordNonReadyUsage() { + + } + + @Override + public void recordBURTimeout() { + + } + + @Override + public void recordLatency(MethodEnum method, long latency) { + + } + + @Override + public void recordException(MethodEnum method) { + + } + + @Override + public void addTag(String tag) { + + } + + @Override + public void recordImpressionStats(ImpressionsDataTypeEnum dataType, long count) { + + } + + @Override + public void recordEventStats(EventsDataRecordsEnum dataType, long count) { + + } + + @Override + public void recordSuccessfulSync(LastSynchronizationRecordsEnum resource, long time) { + + } + + @Override + public void recordSyncError(ResourceEnum resource, int status) { + + } + + @Override + public void recordSyncLatency(HTTPLatenciesEnum resource, long latency) { + + } + + @Override + public void recordAuthRejections() { + + } + + @Override + public void recordTokenRefreshes() { + + } + + @Override + public void recordStreamingEvents(StreamingEvent streamingEvent) { + + } + + @Override + public void recordSessionLength(long sessionLength) { + + } + + @Override + public void recordUpdatesFromSSE(UpdatesFromSSEEnum updatesFromSSEEnum) { + + } + + @Override + public long getBURTimeouts() { + return 0; + } + + @Override + public long getNonReadyUsages() { + return 0; + } + + @Override + public MethodExceptions popExceptions() throws Exception { + return null; + } + + @Override + public MethodLatencies popLatencies() throws Exception { + return null; + } + + @Override + public long getImpressionsStats(ImpressionsDataTypeEnum data) { + return 0; + } + + @Override + public long getEventStats(EventsDataRecordsEnum type) { + return 0; + } + + @Override + public LastSynchronization getLastSynchronization() { + return null; + } + + @Override + public HTTPErrors popHTTPErrors() { + return null; + } + + @Override + public HTTPLatencies popHTTPLatencies(){ + return null; + } + + @Override + public long popAuthRejections() { + return 0; + } + + @Override + public long popTokenRefreshes() { + return 0; + } + + @Override + public List popStreamingEvents() { + return null; + } + + @Override + public List popTags() { + return null; + } + + @Override + public long getSessionLength() { + return 0; + } + + @Override + public UpdatesFromSSE popUpdatesFromSSE() { + return null; + } +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryConfigConsumer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryConfigConsumer.java new file mode 100644 index 00000000..680d128b --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryConfigConsumer.java @@ -0,0 +1,6 @@ +package io.split.telemetry.storage; + +public interface TelemetryConfigConsumer { + long getBURTimeouts(); + long getNonReadyUsages(); +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryConfigProducer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryConfigProducer.java new file mode 100644 index 00000000..64348c15 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryConfigProducer.java @@ -0,0 +1,6 @@ +package io.split.telemetry.storage; + +public interface TelemetryConfigProducer { + void recordNonReadyUsage(); + void recordBURTimeout(); +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationConsumer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationConsumer.java new file mode 100644 index 00000000..ca63112a --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationConsumer.java @@ -0,0 +1,9 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.MethodExceptions; +import io.split.telemetry.domain.MethodLatencies; + +public interface TelemetryEvaluationConsumer { + MethodExceptions popExceptions() throws Exception; + MethodLatencies popLatencies() throws Exception; +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationProducer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationProducer.java new file mode 100644 index 00000000..005106c3 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryEvaluationProducer.java @@ -0,0 +1,8 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.enums.MethodEnum; + +public interface TelemetryEvaluationProducer { + void recordLatency(MethodEnum method, long latency); + void recordException(MethodEnum method); +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeConsumer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeConsumer.java new file mode 100644 index 00000000..8be68998 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeConsumer.java @@ -0,0 +1,25 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.HTTPErrors; +import io.split.telemetry.domain.HTTPLatencies; +import io.split.telemetry.domain.LastSynchronization; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.UpdatesFromSSE; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; + +import java.util.List; + +public interface TelemetryRuntimeConsumer { + long getImpressionsStats(ImpressionsDataTypeEnum data); + long getEventStats(EventsDataRecordsEnum type); + LastSynchronization getLastSynchronization(); + HTTPErrors popHTTPErrors(); + HTTPLatencies popHTTPLatencies(); + long popAuthRejections(); + long popTokenRefreshes(); + List popStreamingEvents(); + List popTags(); + long getSessionLength(); + UpdatesFromSSE popUpdatesFromSSE(); +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeProducer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeProducer.java new file mode 100644 index 00000000..78956287 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryRuntimeProducer.java @@ -0,0 +1,19 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.UpdatesFromSSE; +import io.split.telemetry.domain.enums.*; + +public interface TelemetryRuntimeProducer { + void addTag(String tag); + void recordImpressionStats(ImpressionsDataTypeEnum dataType, long count); + void recordEventStats(EventsDataRecordsEnum dataType, long count); + void recordSuccessfulSync(LastSynchronizationRecordsEnum resource, long time); + void recordSyncError(ResourceEnum resource, int status); + void recordSyncLatency(HTTPLatenciesEnum resource, long latency); + void recordAuthRejections(); + void recordTokenRefreshes(); + void recordStreamingEvents(StreamingEvent streamingEvent); + void recordSessionLength(long sessionLength); + void recordUpdatesFromSSE(UpdatesFromSSEEnum updatesFromSSEEnum); +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryStorage.java b/client/src/main/java/io/split/telemetry/storage/TelemetryStorage.java new file mode 100644 index 00000000..47796509 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryStorage.java @@ -0,0 +1,4 @@ +package io.split.telemetry.storage; + +public interface TelemetryStorage extends TelemetryStorageConsumer, TelemetryStorageProducer{ +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryStorageConsumer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryStorageConsumer.java new file mode 100644 index 00000000..7efd537c --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryStorageConsumer.java @@ -0,0 +1,4 @@ +package io.split.telemetry.storage; + +public interface TelemetryStorageConsumer extends TelemetryConfigConsumer, TelemetryRuntimeConsumer, TelemetryEvaluationConsumer{ +} diff --git a/client/src/main/java/io/split/telemetry/storage/TelemetryStorageProducer.java b/client/src/main/java/io/split/telemetry/storage/TelemetryStorageProducer.java new file mode 100644 index 00000000..c3ef2303 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/storage/TelemetryStorageProducer.java @@ -0,0 +1,4 @@ +package io.split.telemetry.storage; + +public interface TelemetryStorageProducer extends TelemetryEvaluationProducer, TelemetryConfigProducer, TelemetryRuntimeProducer{ +} diff --git a/client/src/main/java/io/split/telemetry/synchronizer/HttpTelemetryMemorySender.java b/client/src/main/java/io/split/telemetry/synchronizer/HttpTelemetryMemorySender.java new file mode 100644 index 00000000..4388eeca --- /dev/null +++ b/client/src/main/java/io/split/telemetry/synchronizer/HttpTelemetryMemorySender.java @@ -0,0 +1,67 @@ +package io.split.telemetry.synchronizer; + +import com.google.common.annotations.VisibleForTesting; +import io.split.client.dtos.UniqueKeys; +import io.split.client.utils.Utils; +import io.split.service.HttpPostImp; +import io.split.service.SplitHttpClient; +import io.split.telemetry.domain.Config; +import io.split.telemetry.domain.Stats; +import io.split.telemetry.domain.enums.HttpParamsWrapper; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.net.URISyntaxException; + +public class HttpTelemetryMemorySender{ + + private static final Logger _log = LoggerFactory.getLogger(HttpTelemetryMemorySender.class); + + private static final String CONFIG_ENDPOINT_PATH = "metrics/config"; + private static final String STATS_ENDPOINT_PATH = "metrics/usage"; + private static final String UNIQUE_KEYS_ENDPOINT_PATH = "keys/ss"; + private static final String CONFIG_METRICS = "Config metrics "; + private static final String STATS_METRICS = "Stats metrics "; + private static final String UNIQUE_KEYS_METRICS = "Unique keys metrics "; + + private final URI _impressionConfigTarget; + private final URI _impressionStatsTarget; + private final URI _uniqueKeysTarget; + private final HttpPostImp _httpPost; + + public static HttpTelemetryMemorySender create(SplitHttpClient client, URI telemetryRootEndpoint, + TelemetryRuntimeProducer telemetryRuntimeProducer) throws URISyntaxException { + return new HttpTelemetryMemorySender(client, + Utils.appendPath(telemetryRootEndpoint,CONFIG_ENDPOINT_PATH), + Utils.appendPath(telemetryRootEndpoint, STATS_ENDPOINT_PATH), + Utils.appendPath(telemetryRootEndpoint, UNIQUE_KEYS_ENDPOINT_PATH), + telemetryRuntimeProducer + ); + } + + @VisibleForTesting + HttpTelemetryMemorySender(SplitHttpClient client, URI impressionConfigTarget, URI impressionStatsTarget, + URI uniqueKeysTarget,TelemetryRuntimeProducer telemetryRuntimeProducer) { + _httpPost = new HttpPostImp(client, telemetryRuntimeProducer); + _impressionConfigTarget = impressionConfigTarget; + _impressionStatsTarget = impressionStatsTarget; + _uniqueKeysTarget = uniqueKeysTarget; + } + + public void postConfig(Config config) { + if (_log.isDebugEnabled()) { + _log.debug("Sending init telemetry"); + } + _httpPost.post(_impressionConfigTarget, config, CONFIG_METRICS, HttpParamsWrapper.TELEMETRY); + } + + public void postStats(Stats stats) { + _httpPost.post(_impressionStatsTarget, stats, STATS_METRICS, HttpParamsWrapper.TELEMETRY); + } + + public void postUniqueKeys(UniqueKeys uniqueKeys) { + _httpPost.post(_uniqueKeysTarget, uniqueKeys, UNIQUE_KEYS_METRICS, HttpParamsWrapper.TELEMETRY); + } +} diff --git a/client/src/main/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitter.java b/client/src/main/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitter.java new file mode 100644 index 00000000..5559332a --- /dev/null +++ b/client/src/main/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitter.java @@ -0,0 +1,173 @@ +package io.split.telemetry.synchronizer; + +import com.google.common.annotations.VisibleForTesting; +import io.split.client.SplitClientConfig; +import io.split.client.dtos.UniqueKeys; +import io.split.client.impressions.ImpressionListener; +import io.split.client.impressions.ImpressionsManager; +import io.split.integrations.IntegrationsConfig; +import io.split.integrations.NewRelicListener; +import io.split.service.SplitHttpClient; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.domain.Config; +import io.split.telemetry.domain.Rates; +import io.split.telemetry.domain.Stats; +import io.split.telemetry.domain.URLOverrides; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.storage.TelemetryStorageConsumer; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +public class TelemetryInMemorySubmitter implements TelemetrySynchronizer{ + + private static final int OPERATION_MODE = 0; + private static final String STORAGE = "memory"; + + private HttpTelemetryMemorySender _httpHttpTelemetryMemorySender; + private TelemetryStorageConsumer _telemetryStorageConsumer; + private SplitCacheConsumer _splitCacheConsumer; + private SegmentCacheConsumer _segmentCacheConsumer; + private final long _initStartTime; + + public TelemetryInMemorySubmitter(SplitHttpClient client, URI telemetryRootEndpoint, TelemetryStorageConsumer telemetryStorageConsumer, + SplitCacheConsumer splitCacheConsumer, SegmentCacheConsumer segmentCacheConsumer, + TelemetryRuntimeProducer telemetryRuntimeProducer, long initStartTime) throws URISyntaxException { + _httpHttpTelemetryMemorySender = HttpTelemetryMemorySender.create(client, telemetryRootEndpoint, telemetryRuntimeProducer); + _telemetryStorageConsumer = checkNotNull(telemetryStorageConsumer); + _splitCacheConsumer = checkNotNull(splitCacheConsumer); + _segmentCacheConsumer = checkNotNull(segmentCacheConsumer); + _initStartTime = initStartTime; + } + + @Override + public void synchronizeConfig(SplitClientConfig config, long readyTimeStamp, Map factoryInstances, List tags) { + _httpHttpTelemetryMemorySender.postConfig(generateConfig(config, readyTimeStamp, factoryInstances, tags)); + } + + @Override + public void synchronizeStats() throws Exception { + _httpHttpTelemetryMemorySender.postStats(generateStats()); + } + + @Override + public void synchronizeUniqueKeys(UniqueKeys uniqueKeys){ + _httpHttpTelemetryMemorySender.postUniqueKeys(uniqueKeys); + } + + @Override + public void finalSynchronization() throws Exception { + Stats stats = generateStats(); + stats.setSplitCount(_splitCacheConsumer.getAll().stream().count()); + stats.setSegmentCount(_segmentCacheConsumer.getSegmentCount()); + stats.setSegmentKeyCount(_segmentCacheConsumer.getKeyCount()); + _httpHttpTelemetryMemorySender.postStats(stats); + } + + @VisibleForTesting + Stats generateStats() throws Exception { + Stats stats = new Stats(); + stats.setLastSynchronization(_telemetryStorageConsumer.getLastSynchronization()); + stats.setMethodLatencies(_telemetryStorageConsumer.popLatencies()); + stats.setMethodExceptions(_telemetryStorageConsumer.popExceptions()); + stats.setHttpErrors(_telemetryStorageConsumer.popHTTPErrors()); + stats.setHttpLatencies(_telemetryStorageConsumer.popHTTPLatencies()); + stats.setTokenRefreshes(_telemetryStorageConsumer.popTokenRefreshes()); + stats.setAuthRejections(_telemetryStorageConsumer.popAuthRejections()); + stats.setImpressionsQueued(_telemetryStorageConsumer.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_QUEUED)); + stats.setImpressionsDeduped(_telemetryStorageConsumer.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED)); + stats.setImpressionsDropped(_telemetryStorageConsumer.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED)); + stats.setSplitCount(_splitCacheConsumer.getAll().stream().count()); + stats.setSegmentCount(_segmentCacheConsumer.getSegmentCount()); + stats.setSegmentKeyCount(_segmentCacheConsumer.getKeyCount()); + stats.setSessionLengthMs(_telemetryStorageConsumer.getSessionLength()); + stats.setEventsQueued(_telemetryStorageConsumer.getEventStats(EventsDataRecordsEnum.EVENTS_QUEUED)); + stats.setEventsDropped(_telemetryStorageConsumer.getEventStats(EventsDataRecordsEnum.EVENTS_DROPPED)); + stats.setStreamingEvents(_telemetryStorageConsumer.popStreamingEvents()); + stats.setTags(_telemetryStorageConsumer.popTags()); + stats.setUpdatesFromSSE(_telemetryStorageConsumer.popUpdatesFromSSE()); + return stats; + } + + @VisibleForTesting + Config generateConfig(SplitClientConfig splitClientConfig, long readyTimestamp, Map factoryInstances, List tags) { + Config config = new Config(); + Rates rates = new Rates(); + URLOverrides urlOverrides = new URLOverrides(); + List impressionsListeners = new ArrayList<>(); + if(splitClientConfig.integrationsConfig() != null) { + impressionsListeners.addAll(splitClientConfig.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC)); + impressionsListeners.addAll(splitClientConfig.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.SYNC)); + } + List impressions = getImpressions(impressionsListeners); + + rates.setTelemetry(splitClientConfig.getTelemetryRefreshRate()); + rates.setEvents(splitClientConfig.eventSendIntervalInMillis()); + rates.setImpressions(splitClientConfig.impressionsRefreshRate()); + rates.setSegments(splitClientConfig.segmentsRefreshRate()); + rates.setSplits(splitClientConfig.featuresRefreshRate()); + + urlOverrides.setAuth(!SplitClientConfig.AUTH_ENDPOINT.equals(splitClientConfig.authServiceURL())); + urlOverrides.setStream(!SplitClientConfig.STREAMING_ENDPOINT.equals(splitClientConfig.streamingServiceURL())); + urlOverrides.setSdk(!SplitClientConfig.SDK_ENDPOINT.equals(splitClientConfig.endpoint())); + urlOverrides.setEvents(!SplitClientConfig.EVENTS_ENDPOINT.equals(splitClientConfig.eventsEndpoint())); + urlOverrides.setTelemetry(!SplitClientConfig.TELEMETRY_ENDPOINT.equals(splitClientConfig.telemetryURL())); + + config.setBurTimeouts(_telemetryStorageConsumer.getBURTimeouts()); + config.setNonReadyUsages(_telemetryStorageConsumer.getNonReadyUsages()); + config.setHttpProxyDetected(splitClientConfig.proxy() != null); + config.setImpressionsMode(getImpressionsMode(splitClientConfig)); + config.setIntegrations(impressions); + config.setImpressionsListenerEnabled((impressionsListeners.size()-impressions.size()) > 0); + config.setOperationMode(OPERATION_MODE); + config.setStorage(STORAGE); + config.setImpressionsQueueSize(splitClientConfig.impressionsQueueSize()); + config.setRedundantFactories(getRedundantFactories(factoryInstances)); + config.setEventsQueueSize(splitClientConfig.eventsQueueSize()); + config.setTags(getListMaxSize(tags)); + config.setActiveFactories(factoryInstances.size()); + config.setTimeUntilReady(readyTimestamp - _initStartTime); + config.setRates(rates); + config.setUrlOverrides(urlOverrides); + config.setStreamingEnabled(splitClientConfig.streamingEnabled()); + int invalidSets = splitClientConfig.getInvalidSets(); + config.setFlagSetsTotal(splitClientConfig.getSetsFilter().size() + invalidSets); + config.setFlagSetsInvalid(invalidSets); + return config; + } + + private long getRedundantFactories(Map factoryInstances) { + long count = 0; + for(Long l :factoryInstances.values()) { + count = count + l - 1l; + } + return count; + } + + private int getImpressionsMode(SplitClientConfig config) { + return ImpressionsManager.Mode.OPTIMIZED.equals(config.impressionsMode()) ? 0 : 1; + } + + private List getListMaxSize(List list) { + return list.size()> 10 ? list.subList(0, 10) : list; + } + + private List getImpressions(List impressionsListeners) { + List impressions = new ArrayList<>(); + for(IntegrationsConfig.ImpressionListenerWithMeta il: impressionsListeners) { + ImpressionListener listener = il.listener(); + if(listener instanceof NewRelicListener) { + impressions.add(NewRelicListener.class.getName()); + } + } + return impressions; + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySyncTask.java b/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySyncTask.java new file mode 100644 index 00000000..a3d10bd9 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySyncTask.java @@ -0,0 +1,44 @@ +package io.split.telemetry.synchronizer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import static com.google.common.base.Preconditions.checkNotNull; +import static io.split.client.utils.SplitExecutorFactory.buildSingleThreadScheduledExecutor; + +public class TelemetrySyncTask { + + private static final Logger _log = LoggerFactory.getLogger(TelemetrySyncTask.class); + private final ScheduledExecutorService _telemetrySyncScheduledExecutorService; + private final TelemetrySynchronizer _telemetrySynchronizer; + private final int _telemetryRefreshRate; + + public TelemetrySyncTask(int telemetryRefreshRate, TelemetrySynchronizer telemetrySynchronizer, ThreadFactory threadFactory) { + _telemetrySynchronizer = checkNotNull(telemetrySynchronizer); + _telemetryRefreshRate = telemetryRefreshRate; + _telemetrySyncScheduledExecutorService = buildSingleThreadScheduledExecutor(threadFactory, "Telemetry-sync-%d"); + } + + public void startScheduledTask() { + _telemetrySyncScheduledExecutorService.scheduleWithFixedDelay(() -> { + try { + _telemetrySynchronizer.synchronizeStats(); + } catch (Exception e) { + _log.warn("Error sending telemetry stats."); + } + },_telemetryRefreshRate, _telemetryRefreshRate, TimeUnit.SECONDS); + } + + public void stopScheduledTask() { + try { + _telemetrySynchronizer.finalSynchronization(); + } catch (Exception e) { + _log.warn("Error trying to send telemetry stats."); + } + _telemetrySyncScheduledExecutorService.shutdown(); + } +} \ No newline at end of file diff --git a/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySynchronizer.java b/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySynchronizer.java new file mode 100644 index 00000000..54ccff68 --- /dev/null +++ b/client/src/main/java/io/split/telemetry/synchronizer/TelemetrySynchronizer.java @@ -0,0 +1,14 @@ +package io.split.telemetry.synchronizer; + +import io.split.client.SplitClientConfig; +import io.split.client.dtos.UniqueKeys; + +import java.util.List; +import java.util.Map; + +public interface TelemetrySynchronizer { + void synchronizeConfig(SplitClientConfig config, long timeUntilReady, Map factoryInstances, List tags); + void synchronizeStats() throws Exception; + void synchronizeUniqueKeys(UniqueKeys uniqueKeys); + void finalSynchronization() throws Exception; +} diff --git a/client/src/main/java/io/split/telemetry/utils/AtomicLongArray.java b/client/src/main/java/io/split/telemetry/utils/AtomicLongArray.java new file mode 100644 index 00000000..eaad8b6d --- /dev/null +++ b/client/src/main/java/io/split/telemetry/utils/AtomicLongArray.java @@ -0,0 +1,44 @@ +package io.split.telemetry.utils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.IntStream; + +public class AtomicLongArray { + private AtomicLong[] array; + private static final int MAX_LENGTH = 23; + + private static final Logger _log = LoggerFactory.getLogger(AtomicLongArray.class); + + public AtomicLongArray(int size) { + if(size <= 0) { + _log.error("Invalid array size. Using default size: " + MAX_LENGTH); + size = MAX_LENGTH; + } + array = new AtomicLong[size]; + IntStream.range(0, array.length).forEach(x -> array[x] = new AtomicLong()); + } + + public void increment(int index) { + if (index < 0 || index >= array.length) { + _log.error("Index is out of bounds. Did not incremented."); + return; + } + array[index].getAndIncrement(); + } + + public List fetchAndClearAll() { + List listValues = new ArrayList<>(); + for (AtomicLong a: array) { + listValues.add(a.longValue()); + } + + IntStream.range(0, array.length).forEach(x -> array[x] = new AtomicLong()); + + return listValues; + } +} diff --git a/client/src/main/java/io/split/telemetry/utils/BucketCalculator.java b/client/src/main/java/io/split/telemetry/utils/BucketCalculator.java new file mode 100644 index 00000000..7c5ce3ce --- /dev/null +++ b/client/src/main/java/io/split/telemetry/utils/BucketCalculator.java @@ -0,0 +1,64 @@ +package io.split.telemetry.utils; + +import java.util.Arrays; + +/** + * Calculates buckets from latency + *

+ * (1) 1.00 + * (2) 1.50 + * (3) 2.25 + * (4) 3.38 + * (5) 5.06 + * (6) 7.59 + * (7) 11.39 + * (8) 17.09 + * (9) 25.63 + * (10) 38.44 + * (11) 57.67 + * (12) 86.50 + * (13) 129.75 + * (14) 194.62 + * (15) 291.93 + * (16) 437.89 + * (17) 656.84 + * (18) 985.26 + * (19) 1,477.89 + * (20) 2,216.84 + * (21) 3,325.26 + * (22) 4,987.89 + * (23) 7,481.83 + *

+ */ +public class BucketCalculator { + + static final long[] BUCKETS = { + 1000, 1500, 2250, 3375, 5063, + 7594, 11391, 17086, 25629, 38443, + 57665, 86498, 129746, 194620, 291929, + 437894, 656841, 985261, 1477892, 2216838, + 3325257, 4987885, 7481828 + }; + + static final long MAX_LATENCY = 7481828; + + public static int getBucketForLatency(long latency) { + long micros = latency / 1000; //Convert to milliseconds + if (micros > MAX_LATENCY) { + return BUCKETS.length - 1; + } + + int index = Arrays.binarySearch(BUCKETS, micros); + + if (index < 0) { + + // Adjust the index based on Java Array javadocs. <0 means the value wasn't found and it's module value + // is where it should be inserted (in this case, it means the counter it applies - unless it's equals to the + // length of the array). + + index = -(index + 1); + } + return index; + } + +} diff --git a/client/src/test/java/io/split/SplitMockServer.java b/client/src/test/java/io/split/SplitMockServer.java index 209f26e0..1b7790d1 100644 --- a/client/src/test/java/io/split/SplitMockServer.java +++ b/client/src/test/java/io/split/SplitMockServer.java @@ -1,20 +1,19 @@ package io.split; import okhttp3.mockwebserver.Dispatcher; -import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; import java.io.IOException; -import java.io.InputStream; -import java.util.Scanner; + +import static org.glassfish.jersey.internal.guava.Preconditions.checkNotNull; public class SplitMockServer { private final MockWebServer _server; - public SplitMockServer() { + public SplitMockServer(Dispatcher dispatcher) { + checkNotNull(dispatcher); _server = new MockWebServer(); - _server.setDispatcher(buildDispatcher()); + _server.setDispatcher(dispatcher); } public void start() throws IOException { @@ -28,51 +27,4 @@ public void stop() throws IOException { public String getUrl() { return String.format("http://%s:%s", _server.getHostName(), _server.getPort()); } - - private Dispatcher buildDispatcher() { - return new Dispatcher() { - @Override - public MockResponse dispatch(RecordedRequest request) { - switch (request.getPath()) { - case "/api/splitChanges?since=-1": - return new MockResponse().setBody(inputStreamToString("splits.json")); - case "/api/auth/enabled": - return new MockResponse().setBody(inputStreamToString("streaming-auth-push-enabled.json")); - case "/api/auth/disabled": - return new MockResponse().setBody(inputStreamToString("streaming-auth-push-disabled.json")); - case "/api/splitChanges?since=1585948850109": - return new MockResponse().setBody("{\"splits\": [], \"since\":1585948850109, \"till\":1585948850110}"); - case "/api/splitChanges?since=1585948850110": - return new MockResponse().setBody(inputStreamToString("splits2.json")); - case "/api/splitChanges?since=1585948850111": - return new MockResponse().setBody(inputStreamToString("splits_killed.json")); - case "/api/splitChanges?since=1585948850112": - return new MockResponse().setBody("{\"splits\": [], \"since\":1585948850112, \"till\":1585948850112}"); - case "/api/segmentChanges/segment-test?since=-1": - return new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": -1,\"till\": -1}"); - case "/api/segmentChanges/segment3?since=-1": - return new MockResponse().setBody(inputStreamToString("segment3.json")); - case "/api/segmentChanges/segment3?since=1585948850110": - return new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": 1585948850110,\"till\": 1585948850110}"); - case "/api/segmentChanges/segment3?since=1585948850111": - return new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": 1585948850111,\"till\": 1585948850111}"); - case "/api/metrics/time": - case "api/metrics/counter": - return new MockResponse().setResponseCode(200); - } - return new MockResponse().setResponseCode(404); - } - }; - } - - private String inputStreamToString(String fileName) { - InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName); - Scanner sc = new Scanner(inputStream); - StringBuffer sb = new StringBuffer(); - while(sc.hasNext()){ - sb.append(sc.nextLine()); - } - - return sb.toString(); - } } diff --git a/client/src/test/java/io/split/TestHelper.java b/client/src/test/java/io/split/TestHelper.java index b0e424a5..577a1d00 100644 --- a/client/src/test/java/io/split/TestHelper.java +++ b/client/src/test/java/io/split/TestHelper.java @@ -1,14 +1,23 @@ package io.split; +import io.split.client.dtos.Condition; +import io.split.client.dtos.Excluded; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Status; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.Header; import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.message.BasicHeader; import org.mockito.Mockito; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; public class TestHelper { public static CloseableHttpClient mockHttpClient(String jsonName, int httpStatus) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { @@ -18,16 +27,35 @@ public static CloseableHttpClient mockHttpClient(String jsonName, int httpStatus ClassicHttpResponse httpResponseMock = Mockito.mock(ClassicHttpResponse.class); Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock); Mockito.when(httpResponseMock.getCode()).thenReturn(httpStatus); - + Header[] headers = new Header[2]; + headers[0] = new BasicHeader(HttpHeaders.VIA, "HTTP/1.1 m_proxy_rio1"); + headers[1] = new BasicHeader(HttpHeaders.VIA, "HTTP/1.1 s_proxy_rio1"); + Mockito.when(httpResponseMock.getHeaders()).thenReturn(headers); CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); Mockito.when(httpClientMock.execute(Mockito.anyObject())).thenReturn(classicResponseToCloseableMock(httpResponseMock)); return httpClientMock; } - private static CloseableHttpResponse classicResponseToCloseableMock(ClassicHttpResponse mocked) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + public static CloseableHttpResponse classicResponseToCloseableMock(ClassicHttpResponse mocked) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { Method adaptMethod = CloseableHttpResponse.class.getDeclaredMethod("adapt", ClassicHttpResponse.class); adaptMethod.setAccessible(true); return (CloseableHttpResponse) adaptMethod.invoke(null, mocked); } + + public static RuleBasedSegment makeRuleBasedSegment(String name, List conditions, long changeNumber) { + Excluded excluded = new Excluded(); + excluded.segments = new ArrayList<>(); + excluded.keys = new ArrayList<>(); + + RuleBasedSegment ruleBasedSegment = new RuleBasedSegment(); + ruleBasedSegment.name = name; + ruleBasedSegment.status = Status.ACTIVE; + ruleBasedSegment.conditions = conditions; + ruleBasedSegment.trafficTypeName = "user"; + ruleBasedSegment.changeNumber = changeNumber; + ruleBasedSegment.excluded = excluded; + return ruleBasedSegment; + } + } diff --git a/client/src/test/java/io/split/cache/InMemoryCacheTest.java b/client/src/test/java/io/split/cache/InMemoryCacheTest.java deleted file mode 100644 index 23ea022f..00000000 --- a/client/src/test/java/io/split/cache/InMemoryCacheTest.java +++ /dev/null @@ -1,121 +0,0 @@ -package io.split.cache; - -import io.split.engine.experiments.ParsedSplit; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.*; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class InMemoryCacheTest { - private SplitCache _cache; - - @Before - public void before() { - _cache = new InMemoryCacheImp(); - } - - @Test - public void putAndGetSplit() { - ParsedSplit split = getParsedSplit("split_name"); - _cache.put(split); - - ParsedSplit result = _cache.get("split_name"); - Assert.assertNotNull(result); - Assert.assertEquals(split.changeNumber(), result.changeNumber()); - Assert.assertEquals(split.trafficTypeName(), result.trafficTypeName()); - Assert.assertEquals(split.defaultTreatment(), result.defaultTreatment()); - } - - @Test - public void putDuplicateSplit() { - ParsedSplit split = getParsedSplit("split_name"); - ParsedSplit split2 = getParsedSplit("split_name"); - _cache.put(split); - _cache.put(split2); - - int result = _cache.getAll().size(); - - Assert.assertEquals(1, result); - } - - @Test - public void getInExistentSplit() { - ParsedSplit split = getParsedSplit("split_name"); - _cache.put(split); - - ParsedSplit result = _cache.get("split_name_2"); - Assert.assertNull(result); - } - - @Test - public void removeSplit() { - ParsedSplit split = getParsedSplit("split_name"); - ParsedSplit split2 = getParsedSplit("split_name_2"); - _cache.put(split); - _cache.put(split2); - - int result = _cache.getAll().size(); - Assert.assertEquals(2, result); - - _cache.remove("split_name"); - result = _cache.getAll().size(); - Assert.assertEquals(1, result); - - Assert.assertNull(_cache.get("split_name")); - } - - @Test - public void setAndGetChangeNumber() { - _cache.setChangeNumber(223); - - long changeNumber = _cache.getChangeNumber(); - Assert.assertEquals(223, changeNumber); - - _cache.setChangeNumber(539); - changeNumber = _cache.getChangeNumber(); - Assert.assertEquals(539, changeNumber); - } - - @Test - public void getMany() { - _cache.put(getParsedSplit("split_name_1")); - _cache.put(getParsedSplit("split_name_2")); - _cache.put(getParsedSplit("split_name_3")); - _cache.put(getParsedSplit("split_name_4")); - - List names = new ArrayList<>(); - names.add("split_name_2"); - names.add("split_name_3"); - - Collection result = _cache.getMany(names); - Assert.assertEquals(2, result.size()); - } - - @Test - public void trafficTypesExist() { - SplitCache cache = new InMemoryCacheImp(-1); - - cache.put(ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2)); - cache.put(ParsedSplit.createParsedSplitForTests("splitName_2", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2)); - cache.put(ParsedSplit.createParsedSplitForTests("splitName_3", 0, false, "default_treatment", new ArrayList<>(), "tt_2", 123, 2)); - cache.put(ParsedSplit.createParsedSplitForTests("splitName_4", 0, false, "default_treatment", new ArrayList<>(), "tt_3", 123, 2)); - - assertTrue(cache.trafficTypeExists("tt_2")); - assertTrue(cache.trafficTypeExists("tt")); - assertFalse(cache.trafficTypeExists("tt_5")); - - cache.remove("splitName_2"); - assertTrue(cache.trafficTypeExists("tt")); - - cache.remove("splitName_1"); - assertFalse(cache.trafficTypeExists("tt")); - } - - private ParsedSplit getParsedSplit(String splitName) { - return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2); - } -} diff --git a/client/src/test/java/io/split/client/ApiKeyCounterTest.java b/client/src/test/java/io/split/client/ApiKeyCounterTest.java index c017127c..1513313b 100644 --- a/client/src/test/java/io/split/client/ApiKeyCounterTest.java +++ b/client/src/test/java/io/split/client/ApiKeyCounterTest.java @@ -1,50 +1,107 @@ package io.split.client; import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; import org.junit.Test; +import java.util.Map; + public class ApiKeyCounterTest extends TestCase { private static final String FIRST_KEY = "KEYNUMBER1"; private static final String SECOND_KEY = "KEYNUMBER2"; + @After + public synchronized void clearApiKeys() { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } + + @Test + public synchronized void testAddingNewToken() { + try { + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } + } + @Test - public void testAddingNewToken() { - ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); + public synchronized void testAddingExistingToken() { + try { + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); + assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); + assertEquals(2, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } } @Test - public void testAddingExistingToken() { - ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - - assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); - assertEquals(2, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); - ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); + public synchronized void testRemovingToken() { + try { + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); + + assertFalse(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); + assertEquals(0, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } + } + + @Test + public synchronized void testAddingNonExistingToken() { + try { + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + + assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); + assertEquals(1, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); + assertEquals(1, ApiKeyCounter.getApiKeyCounterInstance().getCount(SECOND_KEY)); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } } @Test - public void testRemovingToken() { - ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); + public synchronized void testFactoryInstances() { + try { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); - assertFalse(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); - assertEquals(0, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); + Map factoryInstances = ApiKeyCounter.getApiKeyCounterInstance().getFactoryInstances(); + Assert.assertEquals(2, factoryInstances.size()); + Assert.assertEquals(3, factoryInstances.get(FIRST_KEY).intValue()); + Assert.assertEquals(2, factoryInstances.get(SECOND_KEY).intValue()); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } } @Test - public void testAddingNonExistingToken() { - ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); - - assertTrue(ApiKeyCounter.getApiKeyCounterInstance().isApiKeyPresent(FIRST_KEY)); - assertEquals(1, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); - assertEquals(1, ApiKeyCounter.getApiKeyCounterInstance().getCount(SECOND_KEY)); - ApiKeyCounter.getApiKeyCounterInstance().remove(FIRST_KEY); - ApiKeyCounter.getApiKeyCounterInstance().remove(SECOND_KEY); + public synchronized void testClearApiKey() { + try { + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + Assert.assertEquals(0, ApiKeyCounter.getApiKeyCounterInstance().getCount(FIRST_KEY)); + } + finally { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + } } } diff --git a/client/src/test/java/io/split/client/CacheUpdaterServiceTest.java b/client/src/test/java/io/split/client/CacheUpdaterServiceTest.java index 9d6d3014..5f8084f4 100644 --- a/client/src/test/java/io/split/client/CacheUpdaterServiceTest.java +++ b/client/src/test/java/io/split/client/CacheUpdaterServiceTest.java @@ -1,11 +1,14 @@ package io.split.client; -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SplitCache; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.SplitCache; import org.junit.Assert; import org.junit.Test; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; public class CacheUpdaterServiceTest { @@ -17,7 +20,8 @@ public class CacheUpdaterServiceTest { @Test public void testCacheUpdate() { - SplitCache splitCache = new InMemoryCacheImp(); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter); CacheUpdaterService cacheUpdaterService = new CacheUpdaterService(splitCache); cacheUpdaterService.updateCache(getMap()); Assert.assertNotNull(splitCache.get(MY_FEATURE)); diff --git a/client/src/test/java/io/split/client/EventsClientImplTest.java b/client/src/test/java/io/split/client/EventsClientImplTest.java deleted file mode 100644 index 1b2706ac..00000000 --- a/client/src/test/java/io/split/client/EventsClientImplTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package io.split.client; - -import io.split.client.dtos.Event; -import org.apache.hc.client5.http.classic.methods.HttpUriRequest; -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; - -import java.util.concurrent.LinkedBlockingQueue; - -public class EventsClientImplTest { - @Test - public void testDefaultURL() throws URISyntaxException { - URI rootTarget = URI.create("https://api.split.io"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - EventClientImpl fetcher = EventClientImpl.create(httpClient, rootTarget, 5, 5, 5); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/events/bulk"))); - } - - @Test - public void testCustomURLNoPathNoBackslash() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - EventClientImpl fetcher = EventClientImpl.create(httpClient, rootTarget, 5, 5, 5); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/api/events/bulk"))); - } - - @Test - public void testCustomURLAppendingPath() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com/split/"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - EventClientImpl fetcher = EventClientImpl.create(httpClient, rootTarget, 5, 5, 5); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/events/bulk"))); - } - - @Test - public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com/split"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - EventClientImpl fetcher = EventClientImpl.create(httpClient, rootTarget, 5, 5, 5); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/events/bulk"))); - } - - @Test - public void testEventsFlushedWhenSizeLimitReached() throws URISyntaxException, InterruptedException, IOException { - CloseableHttpClient client = Mockito.mock(CloseableHttpClient.class); - EventClientImpl eventClient = new EventClientImpl(new LinkedBlockingQueue(), - client, - URI.create("https://kubernetesturl.com/split"), - 10000, // Long queue so it doesn't flush by # of events - 100000, // Long period so it doesn't flush by timeout expiration. - 0); - - for (int i = 0; i < 159; ++i) { - Event event = new Event(); - eventClient.track(event, 1024 * 32); // 159 32kb events should be about to flush - } - - Thread.sleep(2000); - Mockito.verifyZeroInteractions(client); - - Event event = new Event(); - eventClient.track(event, 1024 * 32); // 159 32kb events should be about to flush - Thread.sleep(2000); - Mockito.verify(client, Mockito.times(1)).execute((HttpUriRequest) Mockito.any()); - } -} diff --git a/client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java b/client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java index afb23855..a8113d8e 100644 --- a/client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java @@ -2,66 +2,102 @@ import io.split.TestHelper; import io.split.client.dtos.SegmentChange; +import io.split.client.utils.SDKMetadata; +import io.split.engine.common.FetchOptions; import io.split.engine.metrics.Metrics; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.*; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; import java.io.IOException; +import java.io.StringBufferInputStream; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.net.URISyntaxException; +import java.util.List; + +import static org.mockito.Mockito.when; public class HttpSegmentChangeFetcherTest { + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + @Test public void testDefaultURL() throws URISyntaxException { URI rootTarget = URI.create("https://api.split.io"); CloseableHttpClient httpClient = HttpClients.custom().build(); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/segmentChanges"))); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://api.split.io/api/segmentChanges"))); } @Test public void testCustomURLNoPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); CloseableHttpClient httpClient = HttpClients.custom().build(); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); } @Test public void testCustomURLAppendingPath() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split/"); CloseableHttpClient httpClient = HttpClients.custom().build(); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); } @Test public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); CloseableHttpClient httpClient = HttpClients.custom().build(); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/segmentChanges"))); } @Test - public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { URI rootTarget = URI.create("https://api.split.io/api/segmentChanges"); - CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("segment-change-special-chatacters.json", HttpStatus.SC_OK); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("segment-change-special-chatacters.json", + HttpStatus.SC_OK); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", + metadata()); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(httpClientMock, rootTarget, metrics); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + TELEMETRY_STORAGE); - SegmentChange change = fetcher.fetch("some_segment", 1234567, true); + SegmentChange change = fetcher.fetch("some_segment", 1234567, new FetchOptions.Builder().build()); Assert.assertNotNull(change); Assert.assertEquals(1, change.added.size()); @@ -69,4 +105,67 @@ public void testFetcherWithSpecialCharacters() throws URISyntaxException, IOExce Assert.assertEquals(1, change.removed.size()); Assert.assertEquals("other_user", change.removed.get(0)); } + + @Test + public void testFetcherWithCDNBypassOption() throws IOException, URISyntaxException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { + URI rootTarget = URI.create("https://api.split.io"); + + HttpEntity entityMock = Mockito.mock(HttpEntity.class); + when(entityMock.getContent()).thenReturn(new StringBufferInputStream("{\"till\": 1}")); + ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); + when(response.getCode()).thenReturn(200); + when(response.getEntity()).thenReturn(entityMock); + when(response.getHeaders()).thenReturn(new Header[0]); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + when(httpClientMock.execute(requestCaptor.capture())) + .thenReturn(TestHelper.classicResponseToCloseableMock(response)); + + Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryStorage.class)); + + fetcher.fetch("someSegment", -1, new FetchOptions.Builder().targetChangeNumber(123).build()); + fetcher.fetch("someSegment2", -1, new FetchOptions.Builder().build()); + List captured = requestCaptor.getAllValues(); + Assert.assertEquals(captured.size(), 2); + Assert.assertTrue(captured.get(0).getUri().toString().contains("till=123")); + Assert.assertFalse(captured.get(1).getUri().toString().contains("till=")); + } + + @Test(expected = IllegalStateException.class) + public void testFetcherWithError() throws IOException, URISyntaxException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { + URI rootTarget = URI.create("https://api.split.io"); + + HttpEntity entityMock = Mockito.mock(HttpEntity.class); + when(entityMock.getContent()).thenReturn(new StringBufferInputStream("{\"till\": 1}")); + ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); + when(response.getCode()).thenReturn(400); + when(response.getEntity()).thenReturn(entityMock); + when(response.getHeaders()).thenReturn(new Header[0]); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + when(httpClientMock.execute(requestCaptor.capture())) + .thenReturn(TestHelper.classicResponseToCloseableMock(response)); + + Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); + HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryStorage.class)); + + fetcher.fetch("someSegment", -1, new FetchOptions.Builder().build()); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } } diff --git a/client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java index 564339db..e1198cd0 100644 --- a/client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java +++ b/client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java @@ -1,78 +1,300 @@ package io.split.client; +import io.split.Spec; import io.split.TestHelper; import io.split.client.dtos.Split; import io.split.client.dtos.SplitChange; +import io.split.client.utils.Json; +import io.split.client.utils.SDKMetadata; +import io.split.engine.common.FetchOptions; import io.split.engine.metrics.Metrics; +import io.split.engine.sse.client.SSEClient; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.storage.TelemetryStorage; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpStatus; -import org.hamcrest.Matchers; +import org.awaitility.Awaitility; import org.junit.Assert; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.net.URISyntaxException; -import java.util.Map; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.mockito.Mockito.when; public class HttpSplitChangeFetcherTest { + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + @Test public void testDefaultURL() throws URISyntaxException { URI rootTarget = URI.create("https://api.split.io"); CloseableHttpClient httpClient = HttpClients.custom().build(); Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/splitChanges"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, TELEMETRY_STORAGE, false); + Assert.assertEquals("https://api.split.io/api/splitChanges", fetcher.getTarget().toString()); } @Test public void testCustomURLNoPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); CloseableHttpClient httpClient = HttpClients.custom().build(); - Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/splitChanges"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, TELEMETRY_STORAGE, false); + Assert.assertEquals("https://kubernetesturl.com/split/api/splitChanges", fetcher.getTarget().toString()); } @Test public void testCustomURLAppendingPath() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split/"); CloseableHttpClient httpClient = HttpClients.custom().build(); - Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/splitChanges"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, TELEMETRY_STORAGE, false); + Assert.assertEquals("https://kubernetesturl.com/split/api/splitChanges", fetcher.getTarget().toString()); } @Test public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); CloseableHttpClient httpClient = HttpClients.custom().build(); - Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClient, rootTarget, metrics); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/splitChanges"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, TELEMETRY_STORAGE, false); + Assert.assertEquals("https://kubernetesturl.com/split/api/splitChanges", fetcher.getTarget().toString()); } @Test - public void testFetcherWithSpecialCharacters() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { + public void testFetcherWithSpecialCharacters() throws URISyntaxException, InvocationTargetException, + NoSuchMethodException, IllegalAccessException, IOException { URI rootTarget = URI.create("https://api.split.io"); - CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", HttpStatus.SC_OK); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_OK); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", + metadata()); - Metrics.NoopMetrics metrics = new Metrics.NoopMetrics(); - HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(httpClientMock, rootTarget, metrics); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, TELEMETRY_STORAGE, false); - SplitChange change = fetcher.fetch(1234567, true); + SplitChange change = fetcher.fetch(1234567, -1, new FetchOptions.Builder().cacheControlHeaders(true).build()); Assert.assertNotNull(change); - Assert.assertEquals(1, change.splits.size()); - Assert.assertNotNull(change.splits.get(0)); + Assert.assertEquals(1, change.featureFlags.d.size()); + Assert.assertNotNull(change.featureFlags.d.get(0)); - Split split = change.splits.get(0); + Split split = change.featureFlags.d.get(0); Map configs = split.configurations; Assert.assertEquals(2, configs.size()); Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on")); Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off")); + Assert.assertEquals(2, split.sets.size()); + } + + @Test + public void testFetcherWithCDNBypassOption() throws IOException, URISyntaxException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { + URI rootTarget = URI.create("https://api.split.io"); + + HttpEntity entityMock = Mockito.mock(HttpEntity.class); + when(entityMock.getContent()) + .thenReturn(new ByteArrayInputStream("{\"ff\":{\"t\": 1,\"s\": -1,\"d\": []},\"rbs\":{\"t\": -1,\"s\": -1,\"d\": []}}". + getBytes(StandardCharsets.UTF_8))); + ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); + when(response.getCode()).thenReturn(200); + when(response.getEntity()).thenReturn(entityMock); + when(response.getHeaders()).thenReturn(new Header[0]); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + when(httpClientMock.execute(requestCaptor.capture())) + .thenReturn(TestHelper.classicResponseToCloseableMock(response)); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryRuntimeProducer.class), false); + + fetcher.fetch(-1, -1, new FetchOptions.Builder().targetChangeNumber(123).build()); + // TODO: Fix the test with integration tests update + List captured = requestCaptor.getAllValues(); + Assert.assertEquals(1, captured.size()); + Assert.assertTrue(captured.get(0).getUri().toString().contains("till=123")); + } + + @Test + public void testRandomNumberGeneration() throws URISyntaxException { + URI rootTarget = URI.create("https://api.split.io"); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryRuntimeProducer.class), false); + + Set seen = new HashSet<>(); + long min = (long) Math.pow(2, 63) * (-1); + final long total = 10000000; + for (long x = 0; x < total; x++) { + long r = fetcher.makeRandomTill(); + Assert.assertTrue(r < 0 && r > min); + seen.add(r); + } + + Assert.assertTrue(seen.size() >= (total * 0.9999)); + } + + @Test(expected = IllegalStateException.class) + public void testURLTooLong() throws IOException, URISyntaxException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException { + URI rootTarget = URI.create("https://api.split.io"); + + HttpEntity entityMock = Mockito.mock(HttpEntity.class); + when(entityMock.getContent()) + .thenReturn(new ByteArrayInputStream("{\"till\": 1}".getBytes(StandardCharsets.UTF_8))); + ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class); + when(response.getCode()).thenReturn(414); + when(response.getEntity()).thenReturn(entityMock); + when(response.getHeaders()).thenReturn(new Header[0]); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class); + when(httpClientMock.execute(requestCaptor.capture())) + .thenReturn(TestHelper.classicResponseToCloseableMock(response)); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryRuntimeProducer.class), false); + List sets = new ArrayList(); + for (Integer i = 0; i < 100; i++) { + sets.add("set" + i.toString()); + } + String result = sets.stream() + .map(n -> String.valueOf(n)) + .collect(Collectors.joining(",", "", "")); + fetcher.fetch(-1, -1, new FetchOptions.Builder().flagSetsFilter(result).cacheControlHeaders(false).build()); } + + @Test + public void testSwitchingToOldSpec() throws URISyntaxException, InvocationTargetException, + NoSuchMethodException, IllegalAccessException, IOException, NoSuchFieldException, InterruptedException { + URI rootTarget = URI.create("https://api.split.io"); + CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class); + HttpEntity entityMock = Mockito.mock(HttpEntity.class); + when(entityMock.getContent()) + .thenReturn(new ByteArrayInputStream("{\"till\": -1, \"since\": -1, \"splits\": []}".getBytes(StandardCharsets.UTF_8))); + HttpEntity entityMock2 = Mockito.mock(HttpEntity.class); + when(entityMock2.getContent()) + .thenReturn(new ByteArrayInputStream("{\"till\": 123, \"since\": 122, \"splits\": [{\"name\":\"some\"}, {\"name\":\"some2\"}]}".getBytes(StandardCharsets.UTF_8))); + HttpEntity entityMock3 = Mockito.mock(HttpEntity.class); + when(entityMock3.getContent()) + .thenReturn(new ByteArrayInputStream("{\"till\": 123, \"since\": 122, \"splits\": [{\"name\":\"some\"}, {\"name\":\"some2\"}]}".getBytes(StandardCharsets.UTF_8))); + HttpEntity entityMock4 = Mockito.mock(HttpEntity.class); + when(entityMock4.getContent()) + .thenReturn(new ByteArrayInputStream("{\"ff\":{\"t\": 123, \"s\": 122, \"d\": [{\"name\":\"some\"}, {\"name\":\"some2\"}]}, \"rbs\":{\"t\": -1, \"s\": -1, \"d\": []}}".getBytes(StandardCharsets.UTF_8))); + ClassicHttpResponse response1 = Mockito.mock(ClassicHttpResponse.class); + when(response1.getCode()).thenReturn(HttpStatus.SC_BAD_REQUEST); + when(response1.getEntity()).thenReturn(entityMock); + when(response1.getHeaders()).thenReturn(new Header[0]); + + ClassicHttpResponse response2 = Mockito.mock(ClassicHttpResponse.class); + when(response2.getCode()).thenReturn(HttpStatus.SC_OK); + when(response2.getEntity()).thenReturn(entityMock2); + when(response2.getHeaders()).thenReturn(new Header[0]); + + ClassicHttpResponse response3 = Mockito.mock(ClassicHttpResponse.class); + when(response3.getCode()).thenReturn(HttpStatus.SC_OK); + when(response3.getEntity()).thenReturn(entityMock3); + when(response3.getHeaders()).thenReturn(new Header[0]); + + ClassicHttpResponse response4 = Mockito.mock(ClassicHttpResponse.class); + when(response4.getCode()).thenReturn(HttpStatus.SC_OK); + when(response4.getEntity()).thenReturn(entityMock4); + when(response4.getHeaders()).thenReturn(new Header[0]); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class); + + when(httpClientMock.execute(requestCaptor.capture())) + .thenReturn(TestHelper.classicResponseToCloseableMock(response1)) + .thenReturn(TestHelper.classicResponseToCloseableMock(response2)) + .thenReturn(TestHelper.classicResponseToCloseableMock(response1)) + .thenReturn(TestHelper.classicResponseToCloseableMock(response3)) + .thenReturn(TestHelper.classicResponseToCloseableMock(response4)); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, + Mockito.mock(TelemetryRuntimeProducer.class), true); + + SplitChange change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build()); + + List captured = requestCaptor.getAllValues(); + Assert.assertEquals(2, captured.size()); + Assert.assertTrue(captured.get(0).getUri().toString().contains("s=1.3")); + Assert.assertTrue(captured.get(1).getUri().toString().contains("s=1.1")); + Assert.assertEquals(122, change.featureFlags.s); + Assert.assertEquals(123, change.featureFlags.t); + Assert.assertEquals(2, change.featureFlags.d.size()); + Assert.assertEquals(Json.fromJson("{\"name\":\"some\"}", Split.class).name, change.featureFlags.d.get(0).name); + Assert.assertEquals(Json.fromJson("{\"name\":\"some2\"}", Split.class).name, change.featureFlags.d.get(1).name); + Assert.assertEquals(0, change.ruleBasedSegments.d.size()); + Assert.assertEquals(-1, change.ruleBasedSegments.s); + Assert.assertEquals(-1, change.ruleBasedSegments.t); + + // Set proxy interval to low number to force check for spec 1.3 + Field proxyInterval = fetcher.getClass().getDeclaredField("PROXY_CHECK_INTERVAL_MILLISECONDS_SS"); + proxyInterval.setAccessible(true); + proxyInterval.set(fetcher, 5); + Awaitility.await() + .atMost(1L, TimeUnit.SECONDS) + .untilAsserted(() -> Assert.assertTrue(proxyInterval.get(fetcher).equals(5))); + + change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build()); + + Assert.assertTrue(captured.get(2).getUri().toString().contains("s=1.3")); + Assert.assertTrue(captured.get(3).getUri().toString().contains("s=1.1")); + Assert.assertEquals(122, change.featureFlags.s); + Assert.assertEquals(123, change.featureFlags.t); + Assert.assertEquals(2, change.featureFlags.d.size()); + Assert.assertEquals(Json.fromJson("{\"name\":\"some\"}", Split.class).name, change.featureFlags.d.get(0).name); + Assert.assertEquals(Json.fromJson("{\"name\":\"some2\"}", Split.class).name, change.featureFlags.d.get(1).name); + + // test if proxy is upgraded and spec 1.3 now works. + Awaitility.await() + .atMost(5L, TimeUnit.SECONDS) + .untilAsserted(() -> Assert.assertTrue(captured.size() >= 4)); + change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build()); + Assert.assertTrue(captured.get(4).getUri().toString().contains("s=1.3")); + Assert.assertEquals(122, change.featureFlags.s); + Assert.assertEquals(123, change.featureFlags.t); + Assert.assertEquals(2, change.featureFlags.d.size()); + Assert.assertEquals(Json.fromJson("{\"name\":\"some\"}", Split.class).name, change.featureFlags.d.get(0).name); + Assert.assertEquals(Json.fromJson("{\"name\":\"some2\"}", Split.class).name, change.featureFlags.d.get(1).name); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + } diff --git a/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java new file mode 100644 index 00000000..583dddab --- /dev/null +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitChangeFetcherTest.java @@ -0,0 +1,223 @@ +package io.split.client; + +import io.split.client.dtos.*; +import io.split.client.utils.FileInputStreamProvider; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.StaticContentInputStreamProvider; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class JsonLocalhostSplitChangeFetcherTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private String TEST_0 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":-1},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":-1}}"; + private String TEST_1 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_2\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":-1},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":-1}}"; + private String TEST_2 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_2\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":2323},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":-1}}"; + private String TEST_3 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":2323},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\",\"gaston@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":1122}}"; + private String TEST_4 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":445345},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\",\"gaston@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":5566}}"; + private String TEST_5 = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_2\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":-1},\"rbs\":{\"d\":[{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}],\"s\":-1,\"t\":-1}}"; + + @Test + public void testParseSplitChange() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + List split = splitChange.featureFlags.d; + Assert.assertEquals(7, split.size()); + Assert.assertEquals(1660326991072L, splitChange.featureFlags.t); + Assert.assertEquals(-1L, splitChange.featureFlags.s); + } + + @Test + public void testSinceAndTillSanitization() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeTillSanitization.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(-1L, splitChange.featureFlags.t); + Assert.assertEquals(-1L, splitChange.featureFlags.s); + + Assert.assertEquals(-1L, splitChange.ruleBasedSegments.t); + Assert.assertEquals(-1L, splitChange.ruleBasedSegments.s); + + } + + @Test + public void testSplitChangeWithoutSplits() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeWithoutSplits.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(0, splitChange.featureFlags.d.size()); + Assert.assertEquals(0, splitChange.ruleBasedSegments.d.size()); + } + + @Test + public void testSplitChangeSplitsToSanitize() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(1, splitChange.featureFlags.d.size()); + Split split = splitChange.featureFlags.d.get(0); + Assert.assertEquals(Optional.of(100), Optional.of(split.trafficAllocation)); + Assert.assertEquals(Status.ACTIVE, split.status); + Assert.assertEquals("control", split.defaultTreatment); + Assert.assertEquals(ConditionType.ROLLOUT, split.conditions.get(split.conditions.size() - 1).conditionType); + + Assert.assertEquals(1, splitChange.ruleBasedSegments.d.size()); + RuleBasedSegment ruleBasedSegment = splitChange.ruleBasedSegments.d.get(0); + Assert.assertEquals(Status.ACTIVE, split.status); + Assert.assertEquals(ConditionType.ROLLOUT, ruleBasedSegment.conditions.get(ruleBasedSegment.conditions.size() - 1).conditionType); + Assert.assertEquals(new ArrayList<>(), ruleBasedSegment.excluded.segments); + } + + @Test + public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangerMatchersNull.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(1, splitChange.featureFlags.d.size()); + Split split = splitChange.featureFlags.d.get(0); + Assert.assertEquals(Optional.of(100), Optional.of(split.trafficAllocation)); + Assert.assertEquals(Status.ACTIVE, split.status); + Assert.assertEquals("off", split.defaultTreatment); + Assert.assertEquals(ConditionType.ROLLOUT, split.conditions.get(split.conditions.size() - 1).conditionType); + } + + @Test + public void testSplitChangeSplitsDifferentScenarios() throws IOException { + + File file = folder.newFile("test_0.json"); + + byte[] test = TEST_0.getBytes(); + com.google.common.io.Files.write(test, file); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider(file.getAbsolutePath()); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + // 0) The CN from storage is -1, till and since are -1, and sha doesn't exist in the hash. It's going to return a split change with updates. + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + Assert.assertEquals(1, splitChange.featureFlags.d.size()); + Assert.assertEquals(-1, splitChange.featureFlags.t); + Assert.assertEquals(-1, splitChange.featureFlags.s); + Assert.assertEquals(1, splitChange.ruleBasedSegments.d.size()); + Assert.assertEquals(-1, splitChange.ruleBasedSegments.t); + Assert.assertEquals(-1, splitChange.ruleBasedSegments.s); + + test = TEST_1.getBytes(); + com.google.common.io.Files.write(test, file); + + // 1) The CN from storage is -1, till and since are -1, and sha is different than before. It's going to return a split change with updates. + splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + Assert.assertEquals(2, splitChange.featureFlags.d.size()); + Assert.assertEquals(-1, splitChange.featureFlags.t); + Assert.assertEquals(-1, splitChange.featureFlags.s); + + test = TEST_2.getBytes(); + com.google.common.io.Files.write(test, file); + + // 2) The CN from storage is -1, till is 2323, and since is -1, and sha is the same as before. It's going to return a split change with the same data. + splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + Assert.assertEquals(2, splitChange.featureFlags.d.size()); + Assert.assertEquals(-1, splitChange.featureFlags.t); + Assert.assertEquals(-1, splitChange.featureFlags.s); + + test = TEST_3.getBytes(); + com.google.common.io.Files.write(test, file); + + // 3) The CN from storage is -1, till is 2323, and since is -1, sha is different than before. It's going to return a split change with updates. + splitChange = localhostSplitChangeFetcher.fetch(-1L, -1L, fetchOptions); + Assert.assertEquals(1, splitChange.featureFlags.d.size()); + Assert.assertEquals(2323, splitChange.featureFlags.t); + Assert.assertEquals(-1, splitChange.featureFlags.s); + Assert.assertEquals(1, splitChange.ruleBasedSegments.d.size()); + Assert.assertEquals(1122, splitChange.ruleBasedSegments.t); + Assert.assertEquals(-1, splitChange.ruleBasedSegments.s); + + test = TEST_4.getBytes(); + com.google.common.io.Files.write(test, file); + + // 4) The CN from storage is 2323, till is 445345, and since is -1, and sha is the same as before. It's going to return a split change with same data. + splitChange = localhostSplitChangeFetcher.fetch(2323, 1122, fetchOptions); + Assert.assertEquals(1, splitChange.featureFlags.d.size()); + Assert.assertEquals(2323, splitChange.featureFlags.t); + Assert.assertEquals(2323, splitChange.featureFlags.s); + Assert.assertEquals(1, splitChange.ruleBasedSegments.d.size()); + Assert.assertEquals(1122, splitChange.ruleBasedSegments.t); + Assert.assertEquals(1122, splitChange.ruleBasedSegments.s); + + test = TEST_5.getBytes(); + com.google.common.io.Files.write(test, file); + + // 5) The CN from storage is 2323, till and since are -1, and sha is different than before. It's going to return a split change with updates. + splitChange = localhostSplitChangeFetcher.fetch(2323, 1122, fetchOptions); + Assert.assertEquals(2, splitChange.featureFlags.d.size()); + Assert.assertEquals(2323, splitChange.featureFlags.t); + Assert.assertEquals(2323, splitChange.featureFlags.s); + Assert.assertEquals(1, splitChange.ruleBasedSegments.d.size()); + Assert.assertEquals(1122, splitChange.ruleBasedSegments.t); + Assert.assertEquals(1122, splitChange.ruleBasedSegments.s); + } + + @Test(expected = IllegalStateException.class) + public void processTestForException() { + InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json"); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + } + + @Test + public void testParseOldSpec() throws FileNotFoundException { + InputStream inputStream = new FileInputStream("src/test/resources/split_old_spec.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + List split = splitChange.featureFlags.d; + Assert.assertEquals(7, split.size()); + Assert.assertEquals(1660326991072L, splitChange.featureFlags.t); + Assert.assertEquals(-1L, splitChange.featureFlags.s); + + Assert.assertEquals(new ArrayList<>(), splitChange.ruleBasedSegments.d); + Assert.assertEquals(-1L, splitChange.ruleBasedSegments.t); + Assert.assertEquals(-1L, splitChange.ruleBasedSegments.s); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/JsonLocalhostSplitFactoryTest.java b/client/src/test/java/io/split/client/JsonLocalhostSplitFactoryTest.java new file mode 100644 index 00000000..2df64c08 --- /dev/null +++ b/client/src/test/java/io/split/client/JsonLocalhostSplitFactoryTest.java @@ -0,0 +1,61 @@ +package io.split.client; + +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.concurrent.TimeoutException; + +public class JsonLocalhostSplitFactoryTest { + + @Test + public void works() throws IOException, URISyntaxException, InterruptedException, TimeoutException { + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global"), + new HashMap() {{ put("feature", new FallbackTreatment("off-local", "{\"prop2\", \"val2\"}")); }}); + + SplitClientConfig config = SplitClientConfig.builder() + .splitFile("src/test/resources/splits_localhost.json") + .segmentDirectory("src/test/resources") + .setBlockUntilReadyTimeout(10000) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); + client.blockUntilReady(); + + Assert.assertEquals("on", client.getTreatment("bilal@@split.io", "rbs_flag", new HashMap() {{ + put("email", "bilal@@split.io"); + }})); + Assert.assertEquals("off", client.getTreatment("mauro@split.io", "rbs_flag", new HashMap() {{ + put("email", "mauro@split.io"); + }})); + Assert.assertEquals("off", client.getTreatment("bilal", "test_split")); + Assert.assertEquals("on", client.getTreatment("bilal", "push_test")); + Assert.assertEquals("on_whitelist", client.getTreatment("admin", "push_test")); + Assert.assertEquals("off-local", client.getTreatment("bilal", "feature")); + Assert.assertEquals("on-global", client.getTreatment("bilal", "feature2")); + + client.destroy(); + } + + @Test + public void testOldSpec() throws IOException, URISyntaxException, InterruptedException, TimeoutException { + SplitClientConfig config = SplitClientConfig.builder() + .splitFile("src/test/resources/split_old_spec.json") + .segmentDirectory("src/test/resources") + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); + client.blockUntilReady(); + + Assert.assertEquals("on", client.getTreatment("bilal", "split_1")); + Assert.assertEquals("off", client.getTreatment("bilal", "split_2")); + Assert.assertEquals("v5", client.getTreatment("admin", "split_2")); + client.destroy(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/LegacyLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/LegacyLocalhostSplitChangeFetcherTest.java new file mode 100644 index 00000000..affee801 --- /dev/null +++ b/client/src/test/java/io/split/client/LegacyLocalhostSplitChangeFetcherTest.java @@ -0,0 +1,41 @@ +package io.split.client; + +import com.google.common.collect.Maps; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.LocalhostUtils; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import java.io.File; +import java.io.IOException; +import java.util.Map; + +public class LegacyLocalhostSplitChangeFetcherTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testParseSplitChange() throws IOException { + File file = folder.newFile(LegacyLocalhostSplitChangeFetcher.FILENAME); + + Map map = Maps.newHashMap(); + map.put(SplitAndKey.of("onboarding"), LocalhostSplit.of("on")); + map.put(SplitAndKey.of("onboarding", "user1"), LocalhostSplit.of("off")); + map.put(SplitAndKey.of("onboarding", "user2"), LocalhostSplit.of("off")); + map.put(SplitAndKey.of("test"), LocalhostSplit.of("a")); + + LocalhostUtils.writeFile(file, map); + + LegacyLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(folder.getRoot().getAbsolutePath()); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(2, splitChange.featureFlags.d.size()); + Assert.assertEquals(-1, splitChange.featureFlags.s); + Assert.assertEquals(-1, splitChange.featureFlags.t); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/LocalhostSplitFactoryTest.java b/client/src/test/java/io/split/client/LocalhostSplitFactoryTest.java index 2728c736..983bf4cc 100644 --- a/client/src/test/java/io/split/client/LocalhostSplitFactoryTest.java +++ b/client/src/test/java/io/split/client/LocalhostSplitFactoryTest.java @@ -1,21 +1,22 @@ package io.split.client; import com.google.common.collect.Maps; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.utils.LocalhostUtils; import io.split.grammar.Treatments; +import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.net.URISyntaxException; +import java.util.HashMap; import java.util.Map; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; /** * Tests for LocalhostSplitFactory @@ -23,13 +24,12 @@ * @author adil */ public class LocalhostSplitFactoryTest { - @Rule public TemporaryFolder folder = new TemporaryFolder(); @Test - public void works() throws IOException, URISyntaxException { - File file = folder.newFile(LocalhostSplitFactory.FILENAME); + public void works() throws IOException, URISyntaxException, InterruptedException { + File file = folder.newFile(LegacyLocalhostSplitChangeFetcher.FILENAME); Map map = Maps.newHashMap(); map.put(SplitAndKey.of("onboarding"), LocalhostSplit.of("on")); @@ -37,56 +37,47 @@ public void works() throws IOException, URISyntaxException { map.put(SplitAndKey.of("onboarding", "user2"), LocalhostSplit.of("off")); map.put(SplitAndKey.of("test"), LocalhostSplit.of("a")); - writeFile(file, map); - - LocalhostSplitFactory factory = new LocalhostSplitFactory(folder.getRoot().getAbsolutePath(), LocalhostSplitFactory.FILENAME); - SplitClient client = factory.client(); - - assertThat(client.getTreatment(null, "foo"), is(equalTo(Treatments.CONTROL))); - assertThat(client.getTreatment("user1", "foo"), is(equalTo(Treatments.CONTROL))); - assertThat(client.getTreatment("user1", "onboarding"), is(equalTo("off"))); - assertThat(client.getTreatment("user2", "onboarding"), is(equalTo("off"))); - assertThat(client.getTreatment("user3", "onboarding"), is(equalTo("on"))); - assertThat(client.getTreatment("user1", "test"), is(equalTo("a"))); - assertThat(client.getTreatment("user2", "test"), is(equalTo("a"))); + LocalhostUtils.writeFile(file, map); + + SplitClientConfig config = SplitClientConfig.builder() + .splitFile(folder.getRoot().getAbsolutePath()) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); + + assertEquals(Treatments.CONTROL, client.getTreatment(null, "foo")); + assertEquals(Treatments.CONTROL, client.getTreatment("user1", "foo")); + assertEquals("off", client.getTreatment("user1", "onboarding")); + assertEquals("off", client.getTreatment("user1", "onboarding")); + assertEquals("off", client.getTreatment("user2", "onboarding")); + assertEquals("on", client.getTreatment("user3", "onboarding")); + assertEquals("a", client.getTreatment("user1", "test")); + assertEquals("a", client.getTreatment("user2", "test")); + } - // now update it. - map.clear(); + @Test + public void testFallbackTreatments() throws IOException, URISyntaxException, InterruptedException { + File file = folder.newFile(LegacyLocalhostSplitChangeFetcher.FILENAME); + Map map = Maps.newHashMap(); map.put(SplitAndKey.of("onboarding"), LocalhostSplit.of("on")); + map.put(SplitAndKey.of("onboarding", "user1"), LocalhostSplit.of("off")); + map.put(SplitAndKey.of("onboarding", "user2"), LocalhostSplit.of("off")); + map.put(SplitAndKey.of("test"), LocalhostSplit.of("a")); - factory.updateFeatureToTreatmentMap(map); - - assertThat(client.getTreatment("user1", "onboarding"), is(equalTo("on"))); - assertThat(client.getTreatment("user2", "onboarding"), is(equalTo("on"))); - assertThat(client.getTreatment("user3", "onboarding"), is(equalTo("on"))); - assertThat(client.getTreatment("user1", "test"), is(equalTo(Treatments.CONTROL))); - } - - private void writeFile(File f, Map map) throws IOException { - BufferedWriter writer = new BufferedWriter(new FileWriter(f)); + LocalhostUtils.writeFile(file, map); - for (Map.Entry entry : map.entrySet()) { - String line = toString(entry); - writer.write(line); - } + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global"), + new HashMap() {{ put("feature", new FallbackTreatment("off-local", "{\"prop2\", \"val2\"}")); }}); - writer.flush(); - writer.close(); - } + SplitClientConfig config = SplitClientConfig.builder() + .splitFile(folder.getRoot().getAbsolutePath()) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); - private String toString(Map.Entry entry) { - StringBuilder bldr = new StringBuilder(); - bldr.append(entry.getKey().split()); - bldr.append(' '); - bldr.append(entry.getValue().treatment); - if (entry.getKey().key() != null) { - bldr.append(' '); - bldr.append(entry.getKey().key()); - } - bldr.append('\n'); - return bldr.toString(); + assertEquals("off-local", client.getTreatment("user1", "feature")); + assertEquals("on-global", client.getTreatment("user1", "feature2")); } - - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlCompactSampleTest.java b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlCompactSampleTest.java index b54e7f48..97110cc8 100644 --- a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlCompactSampleTest.java +++ b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlCompactSampleTest.java @@ -1,12 +1,10 @@ package io.split.client; -import com.google.common.collect.Maps; import io.split.grammar.Treatments; import org.junit.Test; import java.io.IOException; import java.net.URISyntaxException; -import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -25,8 +23,9 @@ public void works() throws IOException, URISyntaxException { String file = getClass().getClassLoader().getResource("split_compact.yaml").getFile(); - LocalhostSplitFactory factory = new LocalhostSplitFactory("", file); - SplitClient client = factory.client(); + SplitClientConfig config = SplitClientConfig.builder().splitFile(file).build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); assertThat(client.getTreatment(null, "foo"), is(equalTo(Treatments.CONTROL))); assertThat(client.getTreatment("user_c", "foo"), is(equalTo(Treatments.CONTROL))); @@ -42,15 +41,30 @@ public void works() throws IOException, URISyntaxException { assertThat(client.getTreatment("user_e", "split_2"), is(equalTo("off"))); assertThat(client.getTreatmentWithConfig("user_e", "split_2").treatment(), is(equalTo("off"))); assertThat(client.getTreatmentWithConfig("user_e", "split_2").config(), is(equalTo("{ \"size\" : 55 }"))); + } - // Update + @Test + public void worksYML() throws IOException, URISyntaxException { - Map update = Maps.newHashMap(); - update.put(SplitAndKey.of("split_2", "user_a"), LocalhostSplit.of("on")); + String file = getClass().getClassLoader().getResource("split_compact.yml").getFile(); - factory.updateFeatureToTreatmentMap(update); + SplitClientConfig config = SplitClientConfig.builder().splitFile(file).build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); - assertThat(client.getTreatment("user_a", "split_2"), is(equalTo("on"))); - } + assertThat(client.getTreatment(null, "foo"), is(equalTo(Treatments.CONTROL))); + assertThat(client.getTreatment("user_c", "foo"), is(equalTo(Treatments.CONTROL))); + + assertThat(client.getTreatment("user_c", "split_1"), is(equalTo("off"))); + assertThat(client.getTreatmentWithConfig("user_c", "split_1").treatment(), is(equalTo("off"))); + assertThat(client.getTreatmentWithConfig("user_c", "split_1").config(), is(equalTo("{ \"size\" : 10 }"))); -} + assertThat(client.getTreatment("user_d", "split_1"), is(equalTo("on"))); + assertThat(client.getTreatmentWithConfig("user_d", "split_1").treatment(), is(equalTo("on"))); + assertThat(client.getTreatmentWithConfig("user_d", "split_1").config(), is(nullValue())); + + assertThat(client.getTreatment("user_e", "split_2"), is(equalTo("off"))); + assertThat(client.getTreatmentWithConfig("user_e", "split_2").treatment(), is(equalTo("off"))); + assertThat(client.getTreatmentWithConfig("user_e", "split_2").config(), is(equalTo("{ \"size\" : 55 }"))); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlSampleTest.java b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlSampleTest.java index 933d1903..c77aef3a 100644 --- a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlSampleTest.java +++ b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlSampleTest.java @@ -1,12 +1,10 @@ package io.split.client; -import com.google.common.collect.Maps; import io.split.grammar.Treatments; import org.junit.Test; import java.io.IOException; import java.net.URISyntaxException; -import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -25,8 +23,9 @@ public void works() throws IOException, URISyntaxException { String file = getClass().getClassLoader().getResource(SplitClientConfig.LOCALHOST_DEFAULT_FILE).getFile(); - LocalhostSplitFactory factory = new LocalhostSplitFactory("", file); - SplitClient client = factory.client(); + SplitClientConfig config = SplitClientConfig.builder().splitFile(file).build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); assertThat(client.getTreatment(null, "foo"), is(equalTo(Treatments.CONTROL))); assertThat(client.getTreatment("user_a", "foo"), is(equalTo(Treatments.CONTROL))); @@ -58,15 +57,5 @@ public void works() throws IOException, URISyntaxException { assertThat(client.getTreatment("user_random", "splitWithNoKeys"), is(equalTo("v2"))); assertThat(client.getTreatmentWithConfig("user_random", "splitWithNoKeys").treatment(), is(equalTo("v2"))); assertThat(client.getTreatmentWithConfig("user_random", "splitWithNoKeys").config(), is(equalTo("{ \"size\" : 999 }"))); - - // Update - - Map update = Maps.newHashMap(); - update.put(SplitAndKey.of("split_2", "user_a"), LocalhostSplit.of("on")); - - factory.updateFeatureToTreatmentMap(update); - - assertThat(client.getTreatment("user_a", "split_2"), is(equalTo("on"))); } - } diff --git a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlTest.java b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlTest.java index c0be1583..989da1a1 100644 --- a/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlTest.java +++ b/client/src/test/java/io/split/client/LocalhostSplitFactoryYamlTest.java @@ -1,22 +1,19 @@ package io.split.client; -import com.google.common.collect.Maps; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.utils.LocalhostUtils; import io.split.grammar.Treatments; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.yaml.snakeyaml.Yaml; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -39,7 +36,6 @@ * @author patricioe */ public class LocalhostSplitFactoryYamlTest { - @Rule public TemporaryFolder folder = new TemporaryFolder(); @@ -83,10 +79,11 @@ public void works() throws IOException, URISyntaxException { assertEquals(expectedYaml, writer.toString()); - writeFile(file, writer); + LocalhostUtils.writeFile(file, writer); - LocalhostSplitFactory factory = new LocalhostSplitFactory("", file.getAbsolutePath()); - SplitClient client = factory.client(); + SplitClientConfig config = SplitClientConfig.builder().splitFile(file.getAbsolutePath()).build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); assertThat(client.getTreatment(null, "foo"), is(equalTo(Treatments.CONTROL))); assertThat(client.getTreatment("user_a", "foo"), is(equalTo(Treatments.CONTROL))); @@ -103,50 +100,64 @@ public void works() throws IOException, URISyntaxException { assertThat(client.getTreatmentWithConfig("user_a", "split_2").treatment(), is(equalTo("off"))); assertThat(client.getTreatmentWithConfig("user_a", "split_2").config(), is(equalTo("{ \"size\" : 20 }"))); - // Update - Map update = Maps.newHashMap(); - update.put(SplitAndKey.of("split_2", "user_a"), LocalhostSplit.of("on")); - factory.updateFeatureToTreatmentMap(update); - - assertThat(client.getTreatment("user_a", "split_2"), is(equalTo("on"))); - - // Make split_1 "legacy" treatment for all keys mines the whitelisted ones. - update = Maps.newHashMap(); - update.put(SplitAndKey.of("split_1", "user_a"), LocalhostSplit.of("off")); - update.put(SplitAndKey.of("split_1", "user_b"), LocalhostSplit.of("on")); - update.put(SplitAndKey.of("split_1"), LocalhostSplit.of("legacy")); - factory.updateFeatureToTreatmentMap(update); - // unchanged assertThat(client.getTreatment("user_a", "split_1"), is(equalTo("off"))); // unchanged assertThat(client.getTreatment("user_b", "split_1"), is(equalTo("on"))); + } - // "legacy" for any other user - assertThat(client.getTreatment("user_blah", "split_1"), is(equalTo("legacy"))); + @Test + public void testFallbackTreatment() throws IOException, URISyntaxException { + File file = folder.newFile(SplitClientConfig.LOCALHOST_DEFAULT_FILE); - factory.updateFeatureToTreatmentMap(update); - } + List> allSplits = new ArrayList(); - private void writeFile(File f, StringWriter content) throws IOException { - BufferedWriter writer = new BufferedWriter(new FileWriter(f)); - writer.write(content.toString()); - writer.flush(); - writer.close(); - } + Map split1_user_a = new LinkedHashMap<>(); + Map split1_user_a_data = new LinkedHashMap<>(); + split1_user_a_data.put("keys", "user_a"); + split1_user_a_data.put("treatment", "off"); + split1_user_a_data.put("config", "{ \"size\" : 20 }"); + split1_user_a.put("split_1", split1_user_a_data); + allSplits.add(split1_user_a); - private String toString(Map.Entry entry) { - StringBuilder bldr = new StringBuilder(); - bldr.append(entry.getKey().split()); - bldr.append(' '); - bldr.append(entry.getValue()); - if (entry.getKey().key() != null) { - bldr.append(' '); - bldr.append(entry.getKey().key()); - } - bldr.append('\n'); - return bldr.toString(); - } + Map split1_user_b = new LinkedHashMap<>(); + Map split1_user_b_data = new LinkedHashMap<>(); + split1_user_b_data.put("keys", "user_b"); + split1_user_b_data.put("treatment", "on"); + split1_user_b.put("split_1", split1_user_b_data); + allSplits.add(split1_user_b); + + Map split2_user_a = new LinkedHashMap<>(); + Map split2_user_a_data = new LinkedHashMap<>(); + split2_user_a_data.put("keys", "user_a"); + split2_user_a_data.put("treatment", "off"); + split2_user_a_data.put("config", "{ \"size\" : 20 }"); + split2_user_a.put("split_2", split2_user_a_data); + allSplits.add(split2_user_a); + + + Yaml yaml = new Yaml(); + StringWriter writer = new StringWriter(); + yaml.dump(allSplits, writer); + String expectedYaml = "- split_1: {keys: user_a, treatment: 'off', config: '{ \"size\" : 20 }'}\n" + + "- split_1: {keys: user_b, treatment: 'on'}\n" + + "- split_2: {keys: user_a, treatment: 'off', config: '{ \"size\" : 20 }'}\n"; -} + assertEquals(expectedYaml, writer.toString()); + + LocalhostUtils.writeFile(file, writer); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-global"), + new HashMap() {{ put("feature", new FallbackTreatment("off-local", "{\"prop2\", \"val2\"}")); }}); + + SplitClientConfig config = SplitClientConfig.builder() + .splitFile(file.getAbsolutePath()) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("localhost", config); + SplitClient client = splitFactory.client(); + + assertEquals("off-local", client.getTreatment("user1", "feature")); + assertEquals("on-global", client.getTreatment("user1", "feature2")); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SdkLifecycleEventsModesIntegrationTest.java b/client/src/test/java/io/split/client/SdkLifecycleEventsModesIntegrationTest.java new file mode 100644 index 00000000..498b75ae --- /dev/null +++ b/client/src/test/java/io/split/client/SdkLifecycleEventsModesIntegrationTest.java @@ -0,0 +1,114 @@ +package io.split.client; + +import io.split.client.lifecycle.EventThreads; +import io.split.client.lifecycle.SdkLifecycleEventsIntegrationTest; +import io.split.client.api.SdkEvent; +import io.split.storages.enums.OperationMode; +import org.junit.After; +import org.junit.Test; +import pluggable.CustomStorageWrapper; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Lifecycle events in the modes that have no sync engine producing updates: localhost and consumer. Both emit + * SDK_READY (with the metadata of their mode) and neither ever emits SDK_UPDATE. + */ +public class SdkLifecycleEventsModesIntegrationTest { + + private static final long WAIT_SECONDS = 20L; + private static final Duration QUIET_WINDOW = Duration.ofMillis(1500); + + private SplitFactoryImpl factory; + + @After + public void destroyFactory() { + if (factory != null) { + factory.destroy(); + } + EventThreads.awaitRecordedThreadsTerminated(); + } + + // Scenario: sdkReady fires once and replays to late subscribers (localhost mode). + @Test + public void localhostFiresSdkReadyOnceReplaysAndNeverRaisesAnUpdate() throws Exception { + SplitClientConfig config = SplitClientConfig.builder() + .splitFile("src/test/resources/splits_localhost.json") + .setBlockUntilReadyTimeout(10000) + .build(); + factory = new SplitFactoryImpl(config); + SplitClient client = factory.client(); + SdkLifecycleEventsIntegrationTest.Recorder ready = new SdkLifecycleEventsIntegrationTest.Recorder(); + SdkLifecycleEventsIntegrationTest.Recorder updates = new SdkLifecycleEventsIntegrationTest.Recorder(); + client.on(SdkEvent.SDK_READY, ready); + client.on(SdkEvent.SDK_UPDATE, updates); + + client.blockUntilReady(); + // localhost loads its file while the factory is constructed, so this subscriber may be a replay (no metadata) + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> ready.size() == 1); + + SdkLifecycleEventsIntegrationTest.Recorder late = new SdkLifecycleEventsIntegrationTest.Recorder(); + client.on(SdkEvent.SDK_READY, late); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> late.size() == 1); + assertNull(late.get(0)); + + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS) + .until(() -> ready.size() == 1 && updates.size() == 0); + } + + // Scenario: destroy stops events and clears handlers (localhost mode). + @Test + public void localhostDestroyDropsHandlers() throws Exception { + SplitClientConfig config = SplitClientConfig.builder() + .splitFile("src/test/resources/splits_localhost.json") + .setBlockUntilReadyTimeout(10000) + .build(); + factory = new SplitFactoryImpl(config); + SplitClient client = factory.client(); + client.blockUntilReady(); + factory.destroy(); + + SdkLifecycleEventsIntegrationTest.Recorder afterDestroy = new SdkLifecycleEventsIntegrationTest.Recorder(); + client.on(SdkEvent.SDK_READY, afterDestroy); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> afterDestroy.size() == 0); + assertTrue(factory.isDestroyed()); + } + + // Consumer mode emits SDK_READY (not from cache) but never SDK_UPDATE. + @Test + public void consumerFiresSdkReadyOnceWithoutCacheMetadataAndNeverRaisesAnUpdate() throws Exception { + CustomStorageWrapper wrapper = mock(CustomStorageWrapper.class); + when(wrapper.connect()).thenReturn(true); + SplitClientConfig config = SplitClientConfig.builder() + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(wrapper) + .setBlockUntilReadyTimeout(10000) + .build(); + factory = new SplitFactoryImpl("fake-api-token", config, wrapper); + SplitClient client = factory.client(); + SdkLifecycleEventsIntegrationTest.Recorder ready = new SdkLifecycleEventsIntegrationTest.Recorder(); + SdkLifecycleEventsIntegrationTest.Recorder updates = new SdkLifecycleEventsIntegrationTest.Recorder(); + client.on(SdkEvent.SDK_READY, ready); + client.on(SdkEvent.SDK_UPDATE, updates); + + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> ready.size() == 1); + assertFalse(ready.get(0).isInitialCacheLoad()); + + SdkLifecycleEventsIntegrationTest.Recorder late = new SdkLifecycleEventsIntegrationTest.Recorder(); + client.on(SdkEvent.SDK_READY, late); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> late.size() == 1); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS) + .until(() -> ready.size() == 1 && updates.size() == 0); + assertEquals(1, late.size()); + } +} diff --git a/client/src/test/java/io/split/client/SplitClientConfigTest.java b/client/src/test/java/io/split/client/SplitClientConfigTest.java index 1fa3e6ab..69c95d03 100644 --- a/client/src/test/java/io/split/client/SplitClientConfigTest.java +++ b/client/src/test/java/io/split/client/SplitClientConfigTest.java @@ -1,12 +1,27 @@ package io.split.client; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import io.split.client.dtos.RequestContext; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.ProxyConfiguration; import io.split.client.impressions.Impression; import io.split.client.impressions.ImpressionListener; import io.split.client.impressions.ImpressionsManager; import io.split.integrations.IntegrationsConfig; -import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -15,14 +30,14 @@ public class SplitClientConfigTest { @Test(expected = IllegalArgumentException.class) - public void cannot_set_feature_refresh_rate_to_less_than_5() { + public void cannotSetFeatureRefreshRateToLessThan5() { SplitClientConfig.builder() .featuresRefreshRate(4) .build(); } @Test(expected = IllegalArgumentException.class) - public void cannot_set_segment_refresh_rate_to_less_than_30() { + public void cannotSetSegmentRefreshRateToLessThan30() { SplitClientConfig.builder() .segmentsRefreshRate(29) .build(); @@ -77,67 +92,62 @@ public void testImpressionRefreshRateConstraints() { } @Test - public void set_impression_refresh_rate_works() { - SplitClientConfig.builder() - .impressionsRefreshRate(1) + public void setImpressionRefreshRateWorks() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsRefreshRate(65) .build(); + Assert.assertEquals(65, config.impressionsRefreshRate()); } @Test(expected = IllegalArgumentException.class) - public void cannot_set_events_flush_rate_to_equal_to_1000() { + public void cannotSetEventsFlushRateToEqualTo1000() { SplitClientConfig.builder() .eventFlushIntervalInMillis(999) .build(); } @Test - public void events_flush_rate_works() { - SplitClientConfig.builder() + public void eventsFlushRateWorks() { + SplitClientConfig config = SplitClientConfig.builder() .eventFlushIntervalInMillis(1000) .build(); + Assert.assertEquals(1000, config.eventSendIntervalInMillis()); } @Test(expected = IllegalArgumentException.class) - public void cannot_set_metrics_refresh_rate_to_less_than_30() { + public void cannotSetMetricsRefreshRateToLessThan30() { SplitClientConfig.builder() .metricsRefreshRate(29) .build(); } @Test - public void can_set_refresh_rates_to__30() { - SplitClientConfig.builder() + public void canSetRefreshRatesTo30() { + SplitClientConfig cfg = SplitClientConfig.builder() .featuresRefreshRate(30) .segmentsRefreshRate(30) - .impressionsRefreshRate(30) - .metricsRefreshRate(30) - .build(); - } - - @Test(expected = IllegalArgumentException.class) - public void cannot_set_zero_capacity_on_impression_listener() throws InterruptedException { - SplitClientConfig.builder() - .integrations(IntegrationsConfig.builder() - .impressionsListener(new ImpressionListener.NoopImpressionListener(), 0) - .build()) + .impressionsRefreshRate(65) + .metricsRefreshRate(65) .build(); + Assert.assertEquals(30, cfg.featuresRefreshRate()); + Assert.assertEquals(30, cfg.segmentsRefreshRate()); + Assert.assertEquals(65, cfg.impressionsRefreshRate()); + Assert.assertEquals(65, cfg.metricsRefreshRate()); } @Test - public void config_does_not_crash_if_new_relic_class_not_present() { + public void configDoesNotCrashIfNewRelicClassNotPresent() { SplitClientConfig cfg = SplitClientConfig.builder() .integrations(IntegrationsConfig.builder() .newRelicImpressionListener() .build()) .build(); - Assert.assertThat( - cfg.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.SYNC).size(), - is(equalTo(0))); + Assert.assertEquals(0, cfg.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.SYNC).size()); } @Test - public void old_impression_listener_config_still_works() { + public void oldImpressionListenerConfigStillWorks() { SplitClientConfig cfg = SplitClientConfig.builder() .impressionListener(new ImpressionListener() { @Override @@ -148,9 +158,7 @@ public void close() { /* noop */ } }, 1000) .build(); - Assert.assertThat( - cfg.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC).size(), - is(equalTo(1))); + Assert.assertEquals(1, cfg.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC).size()); } @Test @@ -158,8 +166,8 @@ public void testVersion() { SplitClientConfig config = SplitClientConfig.builder() .build(); - Assert.assertThat(config.splitSdkVersion, Matchers.not(Matchers.equalTo("undefined"))); - Assert.assertThat(config.splitSdkVersion, Matchers.startsWith("java-")); + Assert.assertNotEquals("undefined", config.splitSdkVersion); + Assert.assertTrue(config.splitSdkVersion.startsWith("java-")); } @Test(expected = IllegalArgumentException.class) @@ -188,5 +196,192 @@ public void streamingReconnectBackoffBaseAllowed() { SplitClientConfig cfg = SplitClientConfig.builder() .streamingReconnectBackoffBase(1) .build(); + Assert.assertEquals(1, cfg.streamingReconnectBackoffBase()); + } + + @Test + public void checkDefaultRateForFeatureAndSegment() { + SplitClientConfig config = SplitClientConfig.builder().build(); + Assert.assertEquals(60, config.featuresRefreshRate()); + Assert.assertEquals(60, config.segmentsRefreshRate()); + } + + @Test + public void checkSetFlagSetsFilter() { + List sets = Stream.of("test1", "test2", "TEST3", "test-4").collect(Collectors.toList()); + SplitClientConfig config = SplitClientConfig.builder().flagSetsFilter(sets).build(); + Assert.assertNotNull(config.getSetsFilter()); + Assert.assertEquals(3, config.getSetsFilter().size()); + } + + @Test + public void threadFactoryNull() { + SplitClientConfig config = SplitClientConfig.builder().build(); + Assert.assertNull(config.getThreadFactory()); + } + + @Test + public void threadFactoryNotNull() { + SplitClientConfig config = SplitClientConfig.builder().threadFactory(new ThreadFactoryBuilder().build()).build(); + Assert.assertNotNull(config.getThreadFactory()); + } + + @Test + public void IntegrationConfigSyncNotNull() { + SplitClientConfig config = SplitClientConfig.builder().integrations(IntegrationsConfig.builder() + .impressionsListener(Mockito.mock(ImpressionListener.class), 500, IntegrationsConfig.Execution.SYNC) + .build()).build(); + Assert.assertNotNull(config.integrationsConfig()); + Assert.assertEquals(1, config.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.SYNC).size()); + Assert.assertEquals(0, config.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC).size()); + } + + @Test + public void IntegrationConfigAsyncNotNull() { + SplitClientConfig config = SplitClientConfig.builder().integrations(IntegrationsConfig.builder() + .impressionsListener(Mockito.mock(ImpressionListener.class), 500, IntegrationsConfig.Execution.ASYNC) + .build()).build(); + Assert.assertNotNull(config.integrationsConfig()); + Assert.assertEquals(0, config.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.SYNC).size()); + Assert.assertEquals(1, config.integrationsConfig().getImpressionsListeners(IntegrationsConfig.Execution.ASYNC).size()); + } + + @Test + public void checkUserCustomdHeaderDecorator() { + CustomHeaderDecorator ucd = new CustomHeaderDecorator() { + @Override + public Map> getHeaderOverrides(RequestContext context) { + return null; + } + }; + SplitClientConfig config = SplitClientConfig.builder().customHeaderDecorator(ucd).build(); + Assert.assertNotNull(config.customHeaderDecorator()); + Assert.assertEquals(ucd, config.customHeaderDecorator()); + + SplitClientConfig config2 = SplitClientConfig.builder().build(); + Assert.assertNull(config2.customHeaderDecorator()); + } + + @Test + public void checkProxyParams() throws MalformedURLException, FileNotFoundException { + SplitClientConfig config = SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:8888")) + .build()) + .build(); + Assert.assertEquals("proxy-host", config.proxyConfiguration().getHost().getHostName()); + Assert.assertEquals(8888, config.proxyConfiguration().getHost().getPort()); + Assert.assertEquals("https", config.proxyConfiguration().getHost().getSchemeName()); + + config = SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:888")) + .credentialsProvider(new io.split.client.dtos.BasicCredentialsProvider() { + @Override + public String getUsername() { + return "user"; + } + + @Override + public String getPassword() { + return "pass"; + } + }) + .build()) + .build(); + io.split.client.dtos.BasicCredentialsProvider basicAuth = (io.split.client.dtos.BasicCredentialsProvider) config.proxyConfiguration().getProxyCredentialsProvider(); + Assert.assertEquals("user", basicAuth.getUsername()); + Assert.assertEquals("pass", basicAuth.getPassword()); + + io.split.client.dtos.BearerCredentialsProvider bearerCredentialsProvider = new io.split.client.dtos.BearerCredentialsProvider() { + @Override + public String getToken() { + return "my-token"; + } + }; + + config = SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:888")) + .credentialsProvider(bearerCredentialsProvider) + .build()) + .build(); + Assert.assertEquals(bearerCredentialsProvider, config.proxyConfiguration().getProxyCredentialsProvider()); + FileInputStream p12File = new FileInputStream("src/test/resources/keyStore.p12"); + config = SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:888")) + .mtls(p12File, "pass-key") + .build()) + .build(); + Assert.assertEquals(p12File, config.proxyConfiguration().getP12File()); + Assert.assertEquals("pass-key", config.proxyConfiguration().getPassKey()); + } + + @Test(expected = IllegalArgumentException.class) + public void cannotUseInvalidHttpScheme() throws MalformedURLException { + SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("ftp://proxy-host:888")) + .build()) + .build(); + } + + @Test(expected = MalformedURLException.class) + public void cannotUseInvalidUrl() throws MalformedURLException { + SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("")) + .build()) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void mustUseUrl() throws MalformedURLException { + SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .build()) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void mustUseP12FileWithProxyMtls() throws MalformedURLException { + SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:888")) + .mtls(null, "pass-key") + .build()) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void mustUseP12PassKeyWithProxyMtls() throws MalformedURLException, FileNotFoundException { + SplitClientConfig.builder() + .proxyConfiguration(new ProxyConfiguration.Builder() + .url(new URL("https://proxy-host:888")) + .mtls(new FileInputStream("src/test/resources/keyStore.p12"), null) + .build()) + .build(); + } + + @Test + public void fallbackTreatmentCheckRegex() { + SplitClientConfig config = SplitClientConfig.builder() + .fallbackTreatments(new FallbackTreatmentsConfiguration(new FallbackTreatment("12#2"))) + .build(); + Assert.assertEquals(null, config.fallbackTreatments().getGlobalFallbackTreatment().getTreatment()); + + config = SplitClientConfig.builder() + .fallbackTreatments(new FallbackTreatmentsConfiguration(new HashMap() {{ put("flag", new FallbackTreatment("12#2")); }} )) + .build(); + Assert.assertEquals(0, config.fallbackTreatments().getByFlagFallbackTreatment().size()); + + config = SplitClientConfig.builder() + .fallbackTreatments(new FallbackTreatmentsConfiguration( + "on", + new HashMap() {{ put("flag", new FallbackTreatment("off")); }} )) + .build(); + Assert.assertEquals("on", config.fallbackTreatments().getGlobalFallbackTreatment().getTreatment()); + Assert.assertEquals("off", config.fallbackTreatments().getByFlagFallbackTreatment().get("flag").getTreatment()); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SplitClientImplTest.java b/client/src/test/java/io/split/client/SplitClientImplTest.java index 4e1370ad..26a85057 100644 --- a/client/src/test/java/io/split/client/SplitClientImplTest.java +++ b/client/src/test/java/io/split/client/SplitClientImplTest.java @@ -4,36 +4,41 @@ import com.google.common.collect.Lists; import io.split.client.api.Key; import io.split.client.api.SplitResult; -import io.split.client.dtos.ConditionType; -import io.split.client.dtos.DataType; -import io.split.client.dtos.Event; -import io.split.client.dtos.Partition; +import io.split.client.dtos.*; +import io.split.client.events.EventsStorageProducer; +import io.split.client.events.NoopEventsStorageImp; import io.split.client.impressions.Impression; import io.split.client.impressions.ImpressionsManager; -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SplitCache; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.engine.matchers.PrerequisitesMatcher; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.EqualToMatcher; +import io.split.engine.matchers.GreaterThanOrEqualToMatcher; +import io.split.engine.matchers.AllKeysMatcher; +import io.split.engine.matchers.DependencyMatcher; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; import io.split.engine.evaluator.EvaluatorImp; import io.split.engine.SDKReadinessGates; import io.split.engine.experiments.ParsedCondition; import io.split.engine.experiments.ParsedSplit; -import io.split.engine.matchers.AllKeysMatcher; -import io.split.engine.matchers.CombiningMatcher; -import io.split.engine.matchers.DependencyMatcher; -import io.split.engine.matchers.EqualToMatcher; -import io.split.engine.matchers.GreaterThanOrEqualToMatcher; import io.split.engine.matchers.collections.ContainsAnyOfSetMatcher; import io.split.engine.matchers.strings.WhitelistMatcher; -import io.split.engine.metrics.Metrics; import io.split.grammar.Treatments; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; import org.apache.commons.lang3.RandomStringUtils; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -43,10 +48,11 @@ import java.util.Set; import java.util.concurrent.TimeoutException; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyList; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -61,148 +67,174 @@ */ public class SplitClientImplTest { - private SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(100).build(); + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + private SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(100).flagSetsFilter(new ArrayList<>( + Arrays.asList("set1", "set2", "set3"))).build(); + private FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set1", "set2", "set3"))); + + @Before + public void updateTelemetryStorage() { + TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + } @Test - public void null_key_results_in_control() { + public void nullKeyResultsInControl() { String test = "test1"; - ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); + assertEquals(Treatments.CONTROL, client.getTreatment(null, "test1")); - assertThat(client.getTreatment(null, "test1"), is(equalTo(Treatments.CONTROL))); - - verifyZeroInteractions(splitCache); + verifyZeroInteractions(splitCacheConsumer); } @Test - public void null_test_results_in_control() { + public void nullTestResultsInControl() { String test = "test1"; - ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, null); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); + assertEquals(Treatments.CONTROL, client.getTreatment("adil@relateiq.com", null)); - assertThat(client.getTreatment("adil@relateiq.com", null), is(equalTo(Treatments.CONTROL))); - - verifyZeroInteractions(splitCache); + verifyZeroInteractions(splitCacheConsumer); } @Test - public void exceptions_result_in_control() { + public void exceptionsResultInControl() { SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(anyString())).thenThrow(RuntimeException.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(anyString())).thenThrow(RuntimeException.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@relateiq.com", "test1"), is(equalTo(Treatments.CONTROL))); + assertEquals(Treatments.CONTROL, client.getTreatment("adil@relateiq.com", "test1")); - verify(splitCache).get("test1"); + verify(splitCacheConsumer).get("test1"); } @Test public void works() { String test = "test1"; - ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); + when(gates.isSDKReady()).thenReturn(true); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); int numKeys = 5; for (int i = 0; i < numKeys; i++) { String randomKey = RandomStringUtils.random(10); - assertThat(client.getTreatment(randomKey, test), is(equalTo("on"))); + Assert.assertEquals("on", client.getTreatment(randomKey, test)); } - verify(splitCache, times(numKeys)).get(test); + verify(splitCacheConsumer, times(numKeys)).get(test); + verify(TELEMETRY_STORAGE, times(5)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); } /** * There is no config for this treatment */ @Test - public void works_null_config() { + public void worksNullConfig() { String test = "test1"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - - String randomKey = RandomStringUtils.random(10); SplitResult result = client.getTreatmentWithConfig(randomKey, test); - assertThat(result.treatment(), is(equalTo(Treatments.ON))); - assertThat(result.config(), is(nullValue())); - - verify(splitCache).get(test); + assertEquals(Treatments.ON, result.treatment()); + assertNull(result.config()); + verify(splitCacheConsumer).get(test); } @Test @@ -216,103 +248,114 @@ public void worksAndHasConfig() { Map configurations = new HashMap<>(); configurations.put(Treatments.ON, "{\"size\" : 30}"); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, configurations); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); int numKeys = 5; for (int i = 0; i < numKeys; i++) { Map attributes = new HashMap<>(); String randomKey = RandomStringUtils.random(10); - assertThat(client.getTreatment(randomKey, test), is(equalTo("on"))); - assertThat(client.getTreatmentWithConfig(randomKey, test, attributes).config(), is(equalTo(configurations.get("on")))); + assertEquals("on", client.getTreatment(randomKey, test)); + assertEquals(configurations.get("on"), client.getTreatmentWithConfig(randomKey, test, attributes).config()); } // Times 2 because we are calling getTreatment twice. Once for getTreatment and one for getTreatmentWithConfig - verify(splitCache, times(numKeys * 2)).get(test); + verify(splitCacheConsumer, times(numKeys * 2)).get(test); } @Test - public void last_condition_is_always_default() { + public void lastConditionIsAlwaysDefault() { String test = "test1"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("pato@codigo.com", test), is(equalTo(Treatments.OFF))); + assertEquals(Treatments.OFF, client.getTreatment("pato@codigo.com", test)); - verify(splitCache).get(test); + verify(splitCacheConsumer).get(test); } /** * Tests that we retrieve configs from the default treatment */ @Test - public void last_condition_is_always_default_but_with_treatment() { + public void lastConditionIsAlwaysDefaultButWithTreatment() { String test = "test1"; - ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher( + Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); // Add config for only one treatment(default) Map configurations = new HashMap<>(); configurations.put(Treatments.OFF, "{\"size\" : 30}"); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, configurations); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + "user", 1, 1, configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); SplitResult result = client.getTreatmentWithConfig("pato@codigo.com", test); - assertThat(result.treatment(), is(equalTo(Treatments.OFF))); - assertThat(result.config(), is(equalTo("{\"size\" : 30}"))); + assertEquals(Treatments.OFF, result.treatment()); + assertEquals("{\"size\" : 30}", result.config()); - verify(splitCache).get(test); + verify(splitCacheConsumer).get(test); } @Test - public void multiple_conditions_work() { + public void multipleConditionsWork() { String test = "test1"; ParsedCondition adil_is_always_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); @@ -320,335 +363,368 @@ public void multiple_conditions_work() { ParsedCondition trevor_is_always_shown = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("trevor@codigo.com"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(adil_is_always_on, pato_is_never_shown, trevor_is_always_shown); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); + when(gates.isSDKReady()).thenReturn(false); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo("on"))); - assertThat(client.getTreatment("pato@codigo.com", test), is(equalTo("off"))); - assertThat(client.getTreatment("trevor@codigo.com", test), is(equalTo("on"))); + assertEquals("on", client.getTreatment("adil@codigo.com", test)); + assertEquals("off", client.getTreatment("pato@codigo.com", test)); + assertEquals("on", client.getTreatment("adil@codigo.com", test)); - verify(splitCache, times(3)).get(test); + verify(splitCacheConsumer, times(3)).get(test); + verify(TELEMETRY_STORAGE, times(3)).recordNonReadyUsage(); } @Test - public void killed_test_always_goes_to_default() { + public void killedTestAlwaysGoesToDefault() { String test = "test1"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, true, Treatments.OFF, conditions, "user", 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, true, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo(Treatments.OFF))); + assertEquals(Treatments.OFF, client.getTreatment("adil@codigo.com", test)); - verify(splitCache).get(test); + verify(splitCacheConsumer).get(test); } /** * when killed, the evaluator follows a slightly different path. So testing that when there is a config. */ @Test - public void killed_test_always_goes_to_default_has_config() { + public void killedTestAlwaysGoesToDefaultHasConfig() { String test = "test1"; - ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher( + Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); // Add config for only one treatment(default) Map configurations = new HashMap<>(); configurations.put(Treatments.OFF, "{\"size\" : 30}"); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, true, Treatments.OFF, conditions, "user", 1, 1, configurations); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, true, Treatments.OFF, conditions, + "user", 1, 1, configurations, new HashSet<>(), true, null); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); SplitResult result = client.getTreatmentWithConfig("adil@codigo.com", test); - assertThat(result.treatment(), is(equalTo(Treatments.OFF))); - assertThat(result.config(), is(equalTo("{\"size\" : 30}"))); + assertEquals(Treatments.OFF, result.treatment()); + assertEquals("{\"size\" : 30}", result.config()); - verify(splitCache).get(test); + verify(splitCacheConsumer).get(test); } @Test - public void dependency_matcher_on() { + public void dependencyMatcherOn() { String parent = "parent"; String dependent = "dependent"; ParsedCondition parent_is_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition(Treatments.ON, 100))); List parent_conditions = Lists.newArrayList(parent_is_on); - ParsedSplit parentSplit = ParsedSplit.createParsedSplitForTests(parent, 123, false, Treatments.OFF, parent_conditions, null, 1, 1); + ParsedSplit parentSplit = ParsedSplit.createParsedSplitForTests(parent, 123, false, Treatments.OFF, parent_conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); ParsedCondition dependent_needs_parent = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new DependencyMatcher(parent, Lists.newArrayList(Treatments.ON))), Lists.newArrayList(partition(Treatments.ON, 100))); List dependent_conditions = Lists.newArrayList(dependent_needs_parent); - ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.OFF, dependent_conditions, null, 1, 1); + ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.OFF, dependent_conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(parent)).thenReturn(parentSplit); - when(splitCache.get(dependent)).thenReturn(dependentSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(parent)).thenReturn(parentSplit); + when(splitCacheConsumer.get(dependent)).thenReturn(dependentSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("key", parent), is(equalTo(Treatments.ON))); - assertThat(client.getTreatment("key", dependent), is(equalTo(Treatments.ON))); + assertEquals(Treatments.ON, client.getTreatment("key", parent)); + assertEquals(Treatments.ON, client.getTreatment("key", dependent)); } @Test - public void dependency_matcher_off() { + public void dependencyMatcherOff() { String parent = "parent"; String dependent = "dependent"; ParsedCondition parent_is_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition(Treatments.ON, 100))); List parent_conditions = Lists.newArrayList(parent_is_on); - ParsedSplit parentSplit = ParsedSplit.createParsedSplitForTests(parent, 123, false, Treatments.OFF, parent_conditions, null, 1, 1); + ParsedSplit parentSplit = ParsedSplit.createParsedSplitForTests(parent, 123, false, Treatments.OFF, parent_conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); ParsedCondition dependent_needs_parent = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new DependencyMatcher(parent, Lists.newArrayList(Treatments.OFF))), Lists.newArrayList(partition(Treatments.ON, 100))); List dependent_conditions = Lists.newArrayList(dependent_needs_parent); - ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.OFF, dependent_conditions, null, 1, 1); + ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.OFF, dependent_conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(parent)).thenReturn(parentSplit); - when(splitCache.get(dependent)).thenReturn(dependentSplit); - + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(parent)).thenReturn(parentSplit); + when(splitCacheConsumer.get(dependent)).thenReturn(dependentSplit); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp ); - assertThat(client.getTreatment("key", parent), is(equalTo(Treatments.ON))); - assertThat(client.getTreatment("key", dependent), is(equalTo(Treatments.OFF))); + assertEquals(Treatments.ON, client.getTreatment("key", parent)); + assertEquals(Treatments.OFF, client.getTreatment("key", dependent)); } @Test - public void dependency_matcher_control() { + public void dependencyMatcherControl() { String dependent = "dependent"; ParsedCondition dependent_needs_parent = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new DependencyMatcher("not-exists", Lists.newArrayList(Treatments.OFF))), Lists.newArrayList(partition(Treatments.OFF, 100))); List dependent_conditions = Lists.newArrayList(dependent_needs_parent); - ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.ON, dependent_conditions, null, 1, 1); + ParsedSplit dependentSplit = ParsedSplit.createParsedSplitForTests(dependent, 123, false, Treatments.ON, dependent_conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(dependent)).thenReturn(dependentSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(dependent)).thenReturn(dependentSplit); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp ); - assertThat(client.getTreatment("key", dependent), is(equalTo(Treatments.ON))); + assertEquals(Treatments.ON, client.getTreatment("key", dependent)); } @Test - public void attributes_work() { + public void attributesWork() { String test = "test1"; ParsedCondition adil_is_always_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition(Treatments.ON, 100))); ParsedCondition users_with_age_greater_than_10_are_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of("age", new GreaterThanOrEqualToMatcher(10, DataType.NUMBER)), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(adil_is_always_on, users_with_age_greater_than_10_are_on); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo("on"))); - assertThat(client.getTreatment("adil@codigo.com", test, null), is(equalTo("on"))); - assertThat(client.getTreatment("adil@codigo.com", test, ImmutableMap.of()), is(equalTo("on"))); - - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10)), is(equalTo("on"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 9)), is(equalTo("off"))); + assertEquals("on", client.getTreatment("adil@codigo.com", test)); + assertEquals("on", client.getTreatment("adil@codigo.com", test, new HashMap<>())); + assertEquals("on", client.getTreatment("adil@codigo.com", test, ImmutableMap.of())); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 9))); - verify(splitCache, times(5)).get(test); + verify(splitCacheConsumer, times(5)).get(test); } @Test - public void attributes_work_2() { + public void attributesWork2() { String test = "test1"; ParsedCondition age_equal_to_0_should_be_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of("age", new EqualToMatcher(0, DataType.NUMBER)), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(age_equal_to_0_should_be_on); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, null), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, ImmutableMap.of()), is(equalTo("off"))); + assertEquals("off", client.getTreatment("adil@codigo.com", test)); + assertEquals("off", client.getTreatment("adil@codigo.com", test, new HashMap<>())); + assertEquals("off", client.getTreatment("adil@codigo.com", test, ImmutableMap.of())); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10)), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 0)), is(equalTo("on"))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 0))); - verify(splitCache, times(5)).get(test); + verify(splitCacheConsumer, times(5)).get(test); } @Test - public void attributes_greater_than_negative_number() { + public void attributesGreaterThanNegativeNumber() { String test = "test1"; ParsedCondition age_equal_to_0_should_be_on = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of("age", new EqualToMatcher(-20, DataType.NUMBER)), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(age_equal_to_0_should_be_on); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, null), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, ImmutableMap.of()), is(equalTo("off"))); + assertEquals("off", client.getTreatment("adil@codigo.com", test)); + assertEquals("off", client.getTreatment("adil@codigo.com", test, new HashMap<>())); + assertEquals("off", client.getTreatment("adil@codigo.com", test, ImmutableMap.of())); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", -20))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 20))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", -21))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 10)), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", -20)), is(equalTo("on"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", 20)), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("age", -21)), is(equalTo("off"))); - - verify(splitCache, times(7)).get(test); + verify(splitCacheConsumer, times(7)).get(test); } @Test - public void attributes_for_sets() { + public void attributesForSets() { String test = "test1"; ParsedCondition any_of_set = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of("products", new ContainsAnyOfSetMatcher(Lists.newArrayList("sms", "video"))), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(any_of_set); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer ,segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("adil@codigo.com", test), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, null), is(equalTo("off"))); - assertThat(client.getTreatment("adil@codigo.com", test, ImmutableMap.of()), is(equalTo("off"))); + assertEquals("off", client.getTreatment("adil@codigo.com", test)); + assertEquals("off", client.getTreatment("adil@codigo.com", test, new HashMap<>())); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList())), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList(""))), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("talk"))), is(equalTo("off"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("sms"))), is(equalTo("on"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("sms", "video"))), is(equalTo("on"))); - assertThat(client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("video"))), is(equalTo("on"))); + assertEquals("off", client.getTreatment("adil@codigo.com", test, ImmutableMap.of())); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList()))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("")))); + assertEquals("off", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("talk")))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("sms")))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("sms", "video")))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, ImmutableMap.of("products", Lists.newArrayList("video")))); - verify(splitCache, times(9)).get(test); + verify(splitCacheConsumer, times(9)).get(test); } @Test - public void labels_are_populated() { + public void labelsArePopulated() { String test = "test1"; ParsedCondition age_equal_to_0_should_be_on = new ParsedCondition(ConditionType.ROLLOUT, @@ -658,39 +734,47 @@ public void labels_are_populated() { ); List conditions = Lists.newArrayList(age_equal_to_0_should_be_on); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SDKReadinessGates gates = mock(SDKReadinessGates.class); ImpressionsManager impressionsManager = mock(ImpressionsManager.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, impressionsManager, - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); Map attributes = ImmutableMap.of("age", -20, "acv", "1000000"); - assertThat(client.getTreatment("pato@codigo.com", test, attributes), is(equalTo("on"))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, attributes)); - ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(Impression.class); + ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(List.class); verify(impressionsManager).track(impressionCaptor.capture()); - assertThat(impressionCaptor.getValue().appliedRule(), is(equalTo("foolabel"))); + List impressions = impressionCaptor.getValue(); + assertNotNull(impressions); + assertEquals(1, impressions.size()); + Impression impression = impressions.get(0).impression(); + + assertEquals("foolabel", impression.appliedRule()); - assertThat(impressionCaptor.getValue().attributes(), is(attributes)); + assertEquals(attributes, impression.attributes()); } @Test - public void not_in_split_if_no_allocation() { - traffic_allocation("pato@split.io", 0, 123, "off", "not in split"); + public void notInSplitIfNoAllocation() { + trafficAllocation("pato@split.io", 0, 123, "off", "not in split"); } /** @@ -704,77 +788,86 @@ public void not_in_split_if_no_allocation() { * @author adil */ @Test - public void not_in_split_if_10_percent_allocation() { + public void notInSplitIf10PercentAllocation() { String key = "pato@split.io"; int i = 0; for (; i <= 9; i++) { - traffic_allocation(key, i, 123, "off", "not in split"); + trafficAllocation(key, i, 123, "off", "not in split"); } for (; i <= 100; i++) { - traffic_allocation(key, i, 123, "on", "in segment all"); + trafficAllocation(key, i, 123, "on", "in segment all"); } } @Test - public void traffic_allocation_one_percent() { + public void trafficAllocationOnePercent() { //This key, with this seed it should fall in the 1% String fallsInOnePercent = "pato193"; - traffic_allocation(fallsInOnePercent, 1, 123, "on", "in segment all"); + trafficAllocation(fallsInOnePercent, 1, 123, "on", "in segment all"); //All these others should not be in split for (int offset = 0; offset <= 100; offset++) { - traffic_allocation("pato" + String.valueOf(offset), 1, 123, "off", "not in split"); + trafficAllocation("pato" + String.valueOf(offset), 1, 123, "off", "not in split"); } } @Test - public void in_split_if_100_percent_allocation() { - traffic_allocation("pato@split.io", 100, 123, "on", "in segment all"); + public void inSplitIf100PercentAllocation() { + trafficAllocation("pato@split.io", 100, 123, "on", "in segment all"); } @Test - public void whitelist_overrides_traffic_allocation() { - traffic_allocation("adil@split.io", 0, 123, "on", "whitelisted user"); + public void whitelistOverridesTrafficAllocation() { + trafficAllocation("adil@split.io", 0, 123, "on", "whitelisted user"); } - private void traffic_allocation(String key, int trafficAllocation, int trafficAllocationSeed, String expected_treatment_on_or_off, String label) { + private void trafficAllocation(String key, int trafficAllocation, int trafficAllocationSeed, String expected_treatment_on_or_off, String label) { String test = "test1"; - ParsedCondition whitelistCondition = new ParsedCondition(ConditionType.WHITELIST, CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@split.io"))), Lists.newArrayList(partition("on", 100), partition("off", 0)), "whitelisted user"); - ParsedCondition rollOutToEveryone = new ParsedCondition(ConditionType.ROLLOUT, CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100), partition("off", 0)), "in segment all"); + ParsedCondition whitelistCondition = new ParsedCondition(ConditionType.WHITELIST, CombiningMatcher.of(new WhitelistMatcher( + Lists.newArrayList("adil@split.io"))), Lists.newArrayList(partition("on", 100), partition( + "off", 0)), "whitelisted user"); + ParsedCondition rollOutToEveryone = new ParsedCondition(ConditionType.ROLLOUT, CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100), partition("off", 0)), "in segment all"); List conditions = Lists.newArrayList(whitelistCondition, rollOutToEveryone); - ParsedSplit parsedSplit = new ParsedSplit(test, 123, false, Treatments.OFF, conditions, null, 1, trafficAllocation, trafficAllocationSeed, 1, null); + ParsedSplit parsedSplit = new ParsedSplit(test, 123, false, Treatments.OFF, conditions, null, 1, + trafficAllocation, trafficAllocationSeed, 1, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); ImpressionsManager impressionsManager = mock(ImpressionsManager.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, impressionsManager, - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment(key, test), is(equalTo(expected_treatment_on_or_off))); + assertEquals(expected_treatment_on_or_off, client.getTreatment(key, test)); - ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(Impression.class); + ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(List.class); verify(impressionsManager).track(impressionCaptor.capture()); - - assertThat(impressionCaptor.getValue().appliedRule(), is(equalTo(label))); + assertNotNull(impressionCaptor.getValue()); + assertEquals(1, impressionCaptor.getValue().size()); + DecoratedImpression impression = (DecoratedImpression) impressionCaptor.getValue().get(0); + assertEquals(label, impression.impression().appliedRule()); } /** @@ -792,45 +885,52 @@ public void notInTrafficAllocationDefaultConfig() { configurations.put(Treatments.ON, "{\"size\" : 30}"); configurations.put(Treatments.OFF, "{\"size\" : 30}"); // OFF is default treatment - ParsedCondition rollOutToEveryone = new ParsedCondition(ConditionType.ROLLOUT, CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100), partition("off", 0)), "in segment all"); + ParsedCondition rollOutToEveryone = new ParsedCondition(ConditionType.ROLLOUT, CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100), partition("off", 0)), "in segment all"); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = new ParsedSplit(test, 123, false, Treatments.OFF, conditions, null, 1, trafficAllocation, trafficAllocationSeed, 1, configurations); + ParsedSplit parsedSplit = new ParsedSplit(test, 123, false, Treatments.OFF, conditions, null, + 1, trafficAllocation, trafficAllocationSeed, 1, configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); ImpressionsManager impressionsManager = mock(ImpressionsManager.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, impressionsManager, - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - assertThat(client.getTreatment("pato@split.io", test), is(equalTo(Treatments.OFF))); - + assertEquals(Treatments.OFF, client.getTreatment("pato@split.io", test)); SplitResult result = client.getTreatmentWithConfig("pato@split.io", test); - assertThat(result.treatment(), is(equalTo(Treatments.OFF))); - assertThat(result.config(), is(equalTo("{\"size\" : 30}"))); + assertEquals(Treatments.OFF, result.treatment()); + assertEquals("{\"size\" : 30}", result.config()); - ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(Impression.class); + ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(List.class); verify(impressionsManager, times(2)).track(impressionCaptor.capture()); - assertThat(impressionCaptor.getValue().appliedRule(), is(equalTo("not in split"))); + assertNotNull(impressionCaptor.getValue()); + assertEquals(1, impressionCaptor.getValue().size()); + DecoratedImpression impression = (DecoratedImpression) impressionCaptor.getValue().get(0); + assertEquals("not in split", impression.impression().appliedRule()); } @Test - public void matching_bucketing_keys_work() { + public void matchingBucketingKeysWork() { String test = "test1"; @@ -839,34 +939,121 @@ public void matching_bucketing_keys_work() { ParsedCondition aijaz_should_match = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(whitelist)), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(aijaz_should_match); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); Key bad_key = new Key("adil", "aijaz"); Key good_key = new Key("aijaz", "adil"); - assertThat(client.getTreatment(bad_key, test, Collections.emptyMap()), is(equalTo("off"))); - assertThat(client.getTreatment(good_key, test, Collections.emptyMap()), is(equalTo("on"))); + assertEquals("off", client.getTreatment(bad_key, test, Collections.emptyMap())); + assertEquals("on", client.getTreatment(good_key, test, Collections.emptyMap())); + + verify(splitCacheConsumer, times(2)).get(test); + } + + @Test + public void matchingBucketingKeysByFlagSetWork() { + String test = "test1"; + + Set whitelist = new HashSet<>(); + whitelist.add("aijaz"); + ParsedCondition aijaz_should_match = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(whitelist)), Lists.newArrayList(partition("on", 100))); + + List conditions = Lists.newArrayList(aijaz_should_match); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + HashMap> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(Arrays.asList("set1"))).thenReturn(flagsBySets); + + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + when(splitCacheConsumer.fetchMany(Arrays.asList(test))).thenReturn(fetchManyResult); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + + Key bad_key = new Key("adil", "aijaz"); + Key good_key = new Key("aijaz", "adil"); - verify(splitCache, times(2)).get(test); + assertEquals("off", client.getTreatmentsByFlagSet(bad_key, "set1", Collections.emptyMap()).get(test)); + assertEquals("on", client.getTreatmentsByFlagSet(good_key, "set1", Collections.emptyMap()).get(test)); } @Test - public void impression_metadata_is_propagated() { + public void matchingBucketingKeysByFlagSetsWork() { + String test = "test1"; + + Set whitelist = new HashSet<>(); + whitelist.add("aijaz"); + ParsedCondition aijaz_should_match = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(whitelist)), Lists.newArrayList(partition("on", 100))); + + List conditions = Lists.newArrayList(aijaz_should_match); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, "user", 1, 1, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + HashMap> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(Arrays.asList("set1"))).thenReturn(flagsBySets); + + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + when(splitCacheConsumer.fetchMany(Arrays.asList(test))).thenReturn(fetchManyResult); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + + Key bad_key = new Key("adil", "aijaz"); + Key good_key = new Key("aijaz", "adil"); + + assertEquals("off", client.getTreatmentsByFlagSets(bad_key, Arrays.asList("set1"), Collections.emptyMap()).get(test)); + assertEquals("on", client.getTreatmentsByFlagSets(good_key, Arrays.asList("set1"), Collections.emptyMap()).get(test)); + } + + @Test + public void impressionMetadataIsPropagated() { String test = "test1"; ParsedCondition age_equal_to_0_should_be_on = new ParsedCondition(ConditionType.ROLLOUT, @@ -876,34 +1063,42 @@ public void impression_metadata_is_propagated() { ); List conditions = Lists.newArrayList(age_equal_to_0_should_be_on); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); ImpressionsManager impressionsManager = mock(ImpressionsManager.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, impressionsManager, - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); Map attributes = ImmutableMap.of("age", -20, "acv", "1000000"); - assertThat(client.getTreatment("pato@codigo.com", test, attributes), is(equalTo("on"))); + assertEquals("on", client.getTreatment("pato@codigo.com", test, attributes)); - ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(Impression.class); + ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(List.class); verify(impressionsManager).track(impressionCaptor.capture()); - assertThat(impressionCaptor.getValue().appliedRule(), is(equalTo("foolabel"))); - assertThat(impressionCaptor.getValue().attributes(), is(equalTo(attributes))); + + assertNotNull(impressionCaptor.getValue()); + assertEquals(1, impressionCaptor.getValue().size()); + DecoratedImpression impression = (DecoratedImpression) impressionCaptor.getValue().get(0); + + assertEquals("foolabel", impression.impression().appliedRule()); + assertEquals(attributes, impression.impression().attributes()); } private Partition partition(String treatment, int size) { @@ -914,168 +1109,229 @@ private Partition partition(String treatment, int size) { } @Test - public void block_until_ready_does_not_time_when_sdk_is_ready() throws TimeoutException, InterruptedException { - SplitCache splitCache = mock(InMemoryCacheImp.class); + public void blockUntilReadyDoesNotTimeWhenSdkIsReady() throws TimeoutException, InterruptedException { + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); SDKReadinessGates ready = mock(SDKReadinessGates.class); - when(ready.isSDKReady(100)).thenReturn(true); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(ready.waitUntilInternalReady(100)).thenReturn(true); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, mock(ImpressionsManager.class), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, ready, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); client.blockUntilReady(); } @Test(expected = TimeoutException.class) - public void block_until_ready_times_when_sdk_is_not_ready() throws TimeoutException, InterruptedException { - SplitCache splitCache = mock(InMemoryCacheImp.class); + public void blockUntilReadyTimesWhenSdkIsNotReady() throws TimeoutException, InterruptedException { + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); SDKReadinessGates ready = mock(SDKReadinessGates.class); - when(ready.isSDKReady(100)).thenReturn(false); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(ready.waitUntilInternalReady(100)).thenReturn(false); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, mock(ImpressionsManager.class), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, ready, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); client.blockUntilReady(); } @Test - public void track_with_valid_parameters() { + public void trackWithValidParameters() { SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(gates.isSDKReady()).thenReturn(false); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - Assert.assertThat(client.track("validKey", "valid_traffic_type", "valid_event"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(true))); + assertTrue(client.track("validKey", "valid_traffic_type", "valid_event")); String validEventSize = new String(new char[80]).replace('\0', 'a'); String validKeySize = new String(new char[250]).replace('\0', 'a'); - Assert.assertThat(client.track(validKeySize, "valid_traffic_type", validEventSize, 10), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(true))); - + assertTrue(client.track(validKeySize, "valid_traffic_type", validEventSize, 10)); + verify(TELEMETRY_STORAGE, times(2)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); } @Test - public void track_with_invalid_event_type_ids() { + public void trackWithInvalidEventTypeIds() { SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); + Assert.assertFalse(client.track("validKey", "valid_traffic_type", "")); + Assert.assertFalse(client.track("validKey", "valid_traffic_type", null)); + Assert.assertFalse(client.track("validKey", "valid_traffic_type", "invalid#char")); - Assert.assertThat(client.track("validKey", "valid_traffic_type", ""), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); - - Assert.assertThat(client.track("validKey", "valid_traffic_type", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + String invalidEventSize = new String(new char[81]).replace('\0', 'a'); + Assert.assertFalse(client.track("validKey", "valid_traffic_type", invalidEventSize)); + } - Assert.assertThat(client.track("validKey", "valid_traffic_type", "invalid#char"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + @Test + public void trackWithInvalidTrafficTypeNames() { + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); - String invalidEventSize = new String(new char[81]).replace('\0', 'a'); - Assert.assertThat(client.track("validKey", "valid_traffic_type", invalidEventSize), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + Assert.assertFalse(client.track("validKey", "", "valid")); + Assert.assertFalse(client.track("validKey", null, "valid")); } @Test - public void track_with_invalid_traffic_type_names() { + public void trackWithInvalidKeys() { SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - Assert.assertThat(client.track("validKey", "", "valid"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + Assert.assertFalse(client.track("", "valid_traffic_type", "valid")); + Assert.assertFalse(client.track(null, "valid_traffic_type", "valid")); - Assert.assertThat(client.track("validKey", null, "valid"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + String invalidKeySize = new String(new char[251]).replace('\0', 'a'); + Assert.assertFalse(client.track(invalidKeySize, "valid_traffic_type", "valid")); } @Test - public void track_with_invalid_keys() { + public void getTreatmentWithInvalidKeys() { + String test = "split"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); + Assert.assertNotEquals(Treatments.CONTROL, client.getTreatment("valid", "split")); + assertEquals(Treatments.CONTROL, client.getTreatment("", "split")); + assertEquals(Treatments.CONTROL, client.getTreatment(null, "split")); - Assert.assertThat(client.track("", "valid_traffic_type", "valid"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + String invalidKeySize = new String(new char[251]).replace('\0', 'a'); + assertEquals(Treatments.CONTROL, client.getTreatment(invalidKeySize, "split")); - Assert.assertThat(client.track(null, "valid_traffic_type", "valid"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + assertEquals(Treatments.CONTROL, client.getTreatment("valid", "")); + assertEquals(Treatments.CONTROL, client.getTreatment("valid", null)); - String invalidKeySize = new String(new char[251]).replace('\0', 'a'); - Assert.assertThat(client.track(invalidKeySize, "valid_traffic_type", "valid"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + String matchingKey = new String(new char[250]).replace('\0', 'a'); + String bucketingKey = new String(new char[250]).replace('\0', 'a'); + Key key = new Key(matchingKey, bucketingKey); + Assert.assertNotEquals(Treatments.CONTROL, client.getTreatment(key, "split", null)); + + key = new Key("valid", ""); + assertEquals(Treatments.CONTROL, client.getTreatment(key, "split", null)); + + key = new Key("", "valid"); + assertEquals(Treatments.CONTROL, client.getTreatment(key, "split", null)); + + matchingKey = new String(new char[251]).replace('\0', 'a'); + bucketingKey = new String(new char[250]).replace('\0', 'a'); + key = new Key(matchingKey, bucketingKey); + assertEquals(Treatments.CONTROL, client.getTreatment(key, "split", null)); + + matchingKey = new String(new char[250]).replace('\0', 'a'); + bucketingKey = new String(new char[251]).replace('\0', 'a'); + key = new Key(matchingKey, bucketingKey); + assertEquals(Treatments.CONTROL, client.getTreatment(key, "split", null)); } @Test - public void track_with_properties() { + public void trackWithProperties() { SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - EventClient eventClientMock = Mockito.mock(EventClient.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + EventsStorageProducer eventClientMock = Mockito.mock(EventsStorageProducer.class); Mockito.when(eventClientMock.track((Event) Mockito.any(), Mockito.anyInt())).thenReturn(true); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), eventClientMock, config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); HashMap properties = new HashMap<>(); @@ -1083,57 +1339,53 @@ public void track_with_properties() { properties.put("ok_property", 123); properties.put("some_property", new Object()); - Assert.assertThat(client.track("key1", "user", "purchase", properties), - org.hamcrest.Matchers.is(true)); + assertTrue(client.track("key1", "user", "purchase", properties)); verify(eventClientMock).track(eventArgumentCaptor.capture(), Mockito.anyInt()); Event captured = eventArgumentCaptor.getValue(); - Assert.assertThat(captured.properties.size(), org.hamcrest.Matchers.is(2)); - Assert.assertThat((Integer) captured.properties.get("ok_property"), org.hamcrest.Matchers.is(123)); - Assert.assertThat(captured.properties.get("some_property"), org.hamcrest.Matchers.nullValue()); + assertEquals(2, captured.properties.size()); + assertEquals(123, captured.properties.get("ok_property")); + assertNull(captured.properties.get("some_property")); properties.clear(); Mockito.reset(eventClientMock); Mockito.when(eventClientMock.track((Event) Mockito.any(), Mockito.anyInt())).thenReturn(true); properties.put("ok_property", 123); properties.put("some_property", Arrays.asList(1, 2, 3)); - Assert.assertThat(client.track("key1", "user", "purchase", properties), - org.hamcrest.Matchers.is(true)); + assertTrue(client.track("key1", "user", "purchase", properties)); eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); verify(eventClientMock).track(eventArgumentCaptor.capture(), Mockito.anyInt()); captured = eventArgumentCaptor.getValue(); - Assert.assertThat(captured.properties.size(), org.hamcrest.Matchers.is(2)); - Assert.assertThat((Integer) captured.properties.get("ok_property"), org.hamcrest.Matchers.is(123)); - Assert.assertThat(captured.properties.get("some_property"), org.hamcrest.Matchers.nullValue()); + assertEquals(2, captured.properties.size()); + assertEquals(123, captured.properties.get("ok_property")); + assertNull(captured.properties.get("some_property")); properties.clear(); Mockito.reset(eventClientMock); Mockito.when(eventClientMock.track((Event) Mockito.any(), Mockito.anyInt())).thenReturn(true); properties.put("ok_property", 123); properties.put("some_property", new HashMap()); - Assert.assertThat(client.track("key1", "user", "purchase", properties), - org.hamcrest.Matchers.is(true)); + assertTrue(client.track("key1", "user", "purchase", properties)); eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); verify(eventClientMock).track(eventArgumentCaptor.capture(), Mockito.anyInt()); captured = eventArgumentCaptor.getValue(); - Assert.assertThat(captured.properties.size(), org.hamcrest.Matchers.is(2)); - Assert.assertThat((Integer) captured.properties.get("ok_property"), org.hamcrest.Matchers.is(123)); - Assert.assertThat(captured.properties.get("some_property"), org.hamcrest.Matchers.nullValue()); + assertEquals(2, captured.properties.size()); + assertEquals(123, captured.properties.get("ok_property")); + assertNull(captured.properties.get("some_property")); properties.clear(); Mockito.reset(eventClientMock); Mockito.when(eventClientMock.track((Event) Mockito.any(), Mockito.anyInt())).thenReturn(true); properties.put("ok_property", 123); - Assert.assertThat(client.track("key1", "user", "purchase", 123, properties), - org.hamcrest.Matchers.is(true)); + assertTrue(client.track("key1", "user", "purchase", 123, properties)); eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); verify(eventClientMock).track(eventArgumentCaptor.capture(), Mockito.anyInt()); captured = eventArgumentCaptor.getValue(); - Assert.assertThat(captured.value, org.hamcrest.Matchers.is(123.0)); - Assert.assertThat(captured.trafficTypeName,org.hamcrest.Matchers.is("user")); - Assert.assertThat(captured.eventTypeId,org.hamcrest.Matchers.is("purchase")); - Assert.assertThat(captured.key,org.hamcrest.Matchers.is("key1")); - Assert.assertThat(captured.properties.size(), org.hamcrest.Matchers.is(1)); - Assert.assertThat((Integer) captured.properties.get("ok_property"), org.hamcrest.Matchers.is(123)); + assertEquals(123.0, captured.value, 0); + assertEquals("user", captured.trafficTypeName); + assertEquals("purchase", captured.eventTypeId); + assertEquals("key1", captured.key); + assertEquals(1, captured.properties.size()); + assertEquals(123, captured.properties.get("ok_property")); properties.clear(); Mockito.reset(eventClientMock); @@ -1144,18 +1396,17 @@ public void track_with_properties() { properties.put("prop4", "something"); properties.put("prop5", true); properties.put("prop6", null); - Assert.assertThat(client.track("key1", "user", "purchase", properties), - org.hamcrest.Matchers.is(true)); + assertTrue(client.track("key1", "user", "purchase", properties)); eventArgumentCaptor = ArgumentCaptor.forClass(Event.class); verify(eventClientMock).track(eventArgumentCaptor.capture(), Mockito.anyInt()); captured = eventArgumentCaptor.getValue(); - Assert.assertThat(captured.properties.size(), org.hamcrest.Matchers.is(6)); - Assert.assertThat((Integer) captured.properties.get("prop1"), org.hamcrest.Matchers.is(1)); - Assert.assertThat((Long) captured.properties.get("prop2"), org.hamcrest.Matchers.is(2L)); - Assert.assertThat((Double) captured.properties.get("prop3"), org.hamcrest.Matchers.is(7.56)); - Assert.assertThat((String) captured.properties.get("prop4"), org.hamcrest.Matchers.is("something")); - Assert.assertThat((Boolean) captured.properties.get("prop5"), org.hamcrest.Matchers.is(true)); - Assert.assertThat(captured.properties.get("prop6"), org.hamcrest.Matchers.nullValue()); + assertEquals(6, captured.properties.size()); + assertEquals(1, captured.properties.get("prop1")); + assertEquals(2L, captured.properties.get("prop2")); + assertEquals(7.56, captured.properties.get("prop3")); + assertEquals("something", captured.properties.get("prop4")); + assertTrue((Boolean) captured.properties.get("prop5")); + assertNull(captured.properties.get("prop6")); // 110 props of 300 bytes should be enough to make the event fail. properties.clear(); @@ -1163,135 +1414,508 @@ public void track_with_properties() { properties.put(new String(new char[300]).replace('\0', 'a') + i , new String(new char[300]).replace('\0', 'a') + i); } - Assert.assertThat(client.track("key1", "user", "purchase", properties), org.hamcrest.Matchers.is(false)); + Assert.assertFalse(client.track("key1", "user", "purchase", properties)); } @Test - public void getTreatment_with_invalid_keys() { + public void clientCannotPerformActionsWhenDestroyed() throws InterruptedException, URISyntaxException, TimeoutException, IOException { String test = "split"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); + + SplitFactory mockFactory = new SplitFactory() { + private boolean destroyed = false; + + @Override + public SplitClient client() { return null; } + + @Override + public SplitManager manager() { return null; } + + @Override + public void destroy() { destroyed = true; } + + @Override + public boolean isDestroyed() { return destroyed; } + }; + + SplitClientImpl client = new SplitClientImpl( + mockFactory, + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + + assertEquals(Treatments.ON, client.getTreatment("valid", "split")); + assertTrue(client.track("validKey", "valid_traffic_type", "valid_event")); + + client.destroy(); + + assertEquals(Treatments.CONTROL, client.getTreatment("valid", "split")); + Assert.assertFalse(client.track("validKey", "valid_traffic_type", "valid_event")); + } + + @Test + public void worksAndHasConfigTryKetTreatmentWithKey() { + String test = "test1"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + + // Add config for only one treatment + Map configurations = new HashMap<>(); + configurations.put(Treatments.ON, "{\"size\" : 30}"); + + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); - Assert.assertThat(client.getTreatment("valid", "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.not(Treatments.CONTROL))); + int numKeys = 5; + for (int i = 0; i < numKeys; i++) { + Map attributes = new HashMap<>(); + String randomKey = RandomStringUtils.random(10); + Key key = new Key(randomKey, "BucketingKey"); + assertEquals("on", client.getTreatment(randomKey, test)); + assertEquals("{\"size\" : 30}", client.getTreatmentWithConfig(key, test, attributes).config()); + } - Assert.assertThat(client.getTreatment("", "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + // Times 2 because we are calling getTreatment twice. Once for getTreatment and one for getTreatmentWithConfig + verify(splitCacheConsumer, times(numKeys * 2)).get(test); + } - Assert.assertThat(client.getTreatment(null, "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + @Test + public void worksAndHasConfigByFlagSetTryKetTreatmentWithKey() { + String test = "test1"; - String invalidKeySize = new String(new char[251]).replace('\0', 'a'); - Assert.assertThat(client.getTreatment(invalidKeySize, "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); - Assert.assertThat(client.getTreatment("valid", ""), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + // Add config for only one treatment + Map configurations = new HashMap<>(); + configurations.put(Treatments.ON, "{\"size\" : 30}"); - Assert.assertThat(client.getTreatment("valid", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); - String matchingKey = new String(new char[250]).replace('\0', 'a'); - String bucketingKey = new String(new char[250]).replace('\0', 'a'); - Key key = new Key(matchingKey, bucketingKey); - Assert.assertThat(client.getTreatment(key, "split", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.not(Treatments.CONTROL))); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + HashMap> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(Arrays.asList("set1"))).thenReturn(flagsBySets); - key = new Key("valid", ""); - Assert.assertThat(client.getTreatment(key, "split", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + when(splitCacheConsumer.fetchMany(Arrays.asList(test))).thenReturn(fetchManyResult); - key = new Key("", "valid"); - Assert.assertThat(client.getTreatment(key, "split", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); - matchingKey = new String(new char[251]).replace('\0', 'a'); - bucketingKey = new String(new char[250]).replace('\0', 'a'); - key = new Key(matchingKey, bucketingKey); - Assert.assertThat(client.getTreatment(key, "split", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.is(Treatments.CONTROL))); + int numKeys = 5; + for (int i = 0; i < numKeys; i++) { + Map attributes = new HashMap<>(); + String randomKey = RandomStringUtils.random(10); + Key key = new Key(randomKey, "BucketingKey"); + assertEquals("on", client.getTreatmentsByFlagSet(randomKey, "set1", new HashMap<>()).get(test)); + assertEquals("{\"size\" : 30}", client.getTreatmentsWithConfigByFlagSet(key, "set1", attributes).get(test).config()); + } + assertEquals("on", client.getTreatmentsByFlagSet("randomKey", "set1").get(test)); + } - matchingKey = new String(new char[250]).replace('\0', 'a'); - bucketingKey = new String(new char[251]).replace('\0', 'a'); - key = new Key(matchingKey, bucketingKey); - Assert.assertThat(client.getTreatment(key, "split", null), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.is(Treatments.CONTROL))); + @Test + public void worksAndHasConfigByFlagSetsTryKetTreatmentWithKey() { + String test = "test1"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + + // Add config for only one treatment + Map configurations = new HashMap<>(); + configurations.put(Treatments.ON, "{\"size\" : 30}"); + + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + HashMap> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(Arrays.asList("set1"))).thenReturn(flagsBySets); + + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + when(splitCacheConsumer.fetchMany(Arrays.asList(test))).thenReturn(fetchManyResult); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + + int numKeys = 5; + for (int i = 0; i < numKeys; i++) { + Map attributes = new HashMap<>(); + String randomKey = RandomStringUtils.random(10); + Key key = new Key(randomKey, "BucketingKey"); + assertEquals("on", client.getTreatmentsByFlagSets(randomKey, Arrays.asList("set1"), new HashMap<>()).get(test)); + assertEquals("{\"size\" : 30}", client.getTreatmentsWithConfigByFlagSets(key, Arrays.asList("set1"), attributes).get(test).config()); + } + } + + @Test(expected = IllegalArgumentException.class) + public void blockUntilReadyException() throws TimeoutException, InterruptedException { + String test = "test1"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); + + SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(-100).build(); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + + client.blockUntilReady(); } @Test - public void client_cannot_perform_actions_when_destroyed() throws InterruptedException, URISyntaxException, TimeoutException, IOException { - String test = "split"; + public void nullKeyResultsInControlGetTreatments() { + String test = "test1"; + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map splits = new HashMap<>(); + splits.put(test, parsedSplit); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(Collections.singletonList(test))).thenReturn(splits); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + assertEquals(Treatments.CONTROL, client.getTreatments(null, Collections.singletonList("test1")).get("test1")); + + verifyZeroInteractions(splitCacheConsumer); + } + + @Test + public void nullSplitsResultsInEmptyGetTreatments() { + String test = "test1"; + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map splits = new HashMap<>(); + splits.put(test, parsedSplit); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(Collections.singletonList(test))).thenReturn(splits); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + assertEquals(0, client.getTreatments("key", null).size()); + + verifyZeroInteractions(splitCacheConsumer); + } + + @Test + public void exceptionsResultInControlGetTreatments() { + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenThrow(RuntimeException.class); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + Map result = client.getTreatments("adil@relateiq.com", Arrays.asList("test1", "test2")); + assertEquals(2, result.values().size()); + assertEquals(Treatments.CONTROL, result.get("test1")); + assertEquals(Treatments.CONTROL, result.get("test2")); + + verify(splitCacheConsumer).fetchMany(anyList()); + } + + @Test + public void getTreatmentsWorks() { + String test = "test1"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map splits = new HashMap<>(); + splits.put(test, parsedSplit); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(splits); + when(gates.isSDKReady()).thenReturn(true); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp + ); + Map result = client.getTreatments("randomKey", Arrays.asList(test, "test2")); + assertEquals("on", result.get(test)); + assertEquals(Treatments.CONTROL, result.get("test2")); + + verify(TELEMETRY_STORAGE, times(1)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); + } + + @Test + public void emptySplitsResultsInNullGetTreatments() { + String test = "test1"; + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map splits = new HashMap<>(); + splits.put(test, parsedSplit); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(Collections.singletonList(test))).thenReturn(splits); - SplitFactory mockFactory = new SplitFactory() { - private boolean destroyed = false; + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + Map result = client.getTreatments("key", new ArrayList<>()); + assertNotNull(result); + assertTrue(result.isEmpty()); - @Override - public SplitClient client() { return null; } + verifyZeroInteractions(splitCacheConsumer); + } - @Override - public SplitManager manager() { return null; } + @Test + public void exceptionsResultInControlTreatments() { + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(anyString())).thenThrow(RuntimeException.class); - @Override - public void destroy() { destroyed = true; } + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + Map result = client.getTreatments("adil@relateiq.com", Arrays.asList("test1")); + assertEquals(1, result.size()); + assertEquals(Treatments.CONTROL, result.get("test1")); + } - @Override - public boolean isDestroyed() { return destroyed; } - }; + @Test + public void worksTreatments() { + String test = "test1"; + String test2 = "test2"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit2 = ParsedSplit.createParsedSplitForTests(test2, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); + parsedSplits.put(test2, parsedSplit2); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(parsedSplits); + when(gates.isSDKReady()).thenReturn(true); SplitClientImpl client = new SplitClientImpl( - mockFactory, - splitCache, + mock(SplitFactory.class), + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); + Map result = client.getTreatments("anyKey", Arrays.asList(test, test2)); + assertNotNull(result); + assertEquals(2, result.size()); + assertEquals("on", result.get(test)); + assertEquals("on", result.get(test2)); + + verify(splitCacheConsumer, times(1)).fetchMany(anyList()); + verify(TELEMETRY_STORAGE, times(1)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); + } + + @Test + public void worksOneControlTreatments() { + String test = "test1"; + String test2 = "test2"; - Assert.assertThat(client.getTreatment("valid", "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.not(Treatments.CONTROL))); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); - Assert.assertThat(client.track("validKey", "valid_traffic_type", "valid_event"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(true))); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(parsedSplits); + when(gates.isSDKReady()).thenReturn(true); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); - client.destroy(); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp + ); - Assert.assertThat(client.getTreatment("valid", "split"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(Treatments.CONTROL))); + Map result = client.getTreatments("anyKey", Arrays.asList(test, test2)); + assertNotNull(result); + assertEquals(2, result.size()); + assertEquals("on", result.get(test)); + assertEquals("control", result.get(test2)); - Assert.assertThat(client.track("validKey", "valid_traffic_type", "valid_event"), - org.hamcrest.Matchers.is(org.hamcrest.Matchers.equalTo(false))); + verify(splitCacheConsumer, times(1)).fetchMany(anyList()); + verify(TELEMETRY_STORAGE, times(1)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); } @Test - public void worksAndHasConfigTryKetTreatmentWithKey() { + public void treatmentsWorksAndHasConfig() { String test = "test1"; + String test2 = "test2"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); @@ -1299,61 +1923,718 @@ public void worksAndHasConfigTryKetTreatmentWithKey() { // Add config for only one treatment Map configurations = new HashMap<>(); configurations.put(Treatments.ON, "{\"size\" : 30}"); + configurations.put(Treatments.CONTROL, "{\"size\" : 30}"); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, configurations); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(parsedSplits); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp + ); + Map attributes = new HashMap<>(); + Map result = client.getTreatmentsWithConfig("randomKey", Arrays.asList(test, test2, "", null), attributes); + assertEquals(2, result.size()); + assertEquals(configurations.get("on"), result.get(test).config()); + assertNull(result.get(test2).config()); + assertEquals("control", result.get(test2).treatment()); + + verify(splitCacheConsumer, times(1)).fetchMany(anyList()); + } + + @Test + public void testTreatmentsByFlagSet() { + String test = "test1"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(Arrays.asList("set1", "set2")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + when(splitCacheConsumer.fetchMany(new ArrayList<>(Arrays.asList(test)))).thenReturn(fetchManyResult); + List sets = new ArrayList<>(Arrays.asList("set1")); + Map> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagsBySets); + when(gates.isSDKReady()).thenReturn(true); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) ); int numKeys = 5; + Map getTreatmentResult; for (int i = 0; i < numKeys; i++) { - Map attributes = new HashMap<>(); String randomKey = RandomStringUtils.random(10); - Key key = new Key(randomKey, "BucketingKey"); - assertThat(client.getTreatment(randomKey, test), is(equalTo("on"))); - assertThat(client.getTreatmentWithConfig(key, test, attributes).config(), is(equalTo(configurations.get("on")))); + getTreatmentResult = client.getTreatmentsByFlagSet(randomKey, "set1", new HashMap<>()); + assertEquals("on", getTreatmentResult.get(test)); } + verify(splitCacheConsumer, times(numKeys)).fetchMany(new ArrayList<>(Arrays.asList(test))); + verify(TELEMETRY_STORAGE, times(5)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); + getTreatmentResult = client.getTreatmentsByFlagSet("randomKey", "set1"); + assertEquals("on", getTreatmentResult.get(test)); + } - // Times 2 because we are calling getTreatment twice. Once for getTreatment and one for getTreatmentWithConfig - verify(splitCache, times(numKeys * 2)).get(test); + @Test + public void testTreatmentsByFlagSetInvalid() { + String test = "test1"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(Arrays.asList("set1", "set2")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + List sets = new ArrayList<>(); + when(gates.isSDKReady()).thenReturn(true); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + assertTrue(client.getTreatmentsByFlagSet(RandomStringUtils.random(10), "", new HashMap<>()).isEmpty()); } - @Test(expected = IllegalArgumentException.class) - public void blockUntilReadyException() throws TimeoutException, InterruptedException { + @Test + public void testTreatmentsByFlagSets() { + String test = "test1"; + String test2 = "test2"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), + Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(Arrays.asList("set1", "set2")), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit2 = ParsedSplit.createParsedSplitForTests(test2, 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(Arrays.asList("set3", "set4")), true, new PrerequisitesMatcher(null)); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + + Map fetchManyResult = new HashMap<>(); + fetchManyResult.put(test, parsedSplit); + fetchManyResult.put(test2, parsedSplit2); + when(splitCacheConsumer.fetchMany(new ArrayList<>(Arrays.asList(test2, test)))).thenReturn(fetchManyResult); + + List sets = new ArrayList<>(Arrays.asList("set3", "set1")); + Map> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test))); + flagsBySets.put("set3", new HashSet<>(Arrays.asList(test2))); + + when(splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagsBySets); + when(gates.isSDKReady()).thenReturn(true); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + new FallbackTreatmentCalculatorImp(null) + ); + int numKeys = 5; + Map getTreatmentResult; + for (int i = 0; i < numKeys; i++) { + String randomKey = RandomStringUtils.random(10); + getTreatmentResult = client.getTreatmentsByFlagSets(randomKey, Arrays.asList("set1", "set3"), new HashMap<>()); + assertEquals("on", getTreatmentResult.get(test)); + assertEquals("on", getTreatmentResult.get(test2)); + } + verify(splitCacheConsumer, times(numKeys)).fetchMany(new ArrayList<>(Arrays.asList(test2, test))); + verify(TELEMETRY_STORAGE, times(5)).recordLatency(Mockito.anyObject(), Mockito.anyLong()); + getTreatmentResult = client.getTreatmentsByFlagSets("key", Arrays.asList("set1", "set3")); + assertEquals("on", getTreatmentResult.get(test)); + assertEquals("on", getTreatmentResult.get(test2)); + } + + @Test + public void treatmentsWorksAndHasConfigFlagSet() { String test = "test1"; + String test2 = "test2"; ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); List conditions = Lists.newArrayList(rollOutToEveryone); - ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1); + + // Add config for only one treatment + Map configurations = new HashMap<>(); + configurations.put(Treatments.ON, "{\"size\" : 30}"); + configurations.put(Treatments.CONTROL, "{\"size\" : 30}"); + + + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); + + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(parsedSplits); + + List sets = new ArrayList<>(Arrays.asList("set1")); + Map> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test, test2))); + when(splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagsBySets); SDKReadinessGates gates = mock(SDKReadinessGates.class); - SplitCache splitCache = mock(InMemoryCacheImp.class); - when(splitCache.get(test)).thenReturn(parsedSplit); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); - SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(-100).build(); SplitClientImpl client = new SplitClientImpl( mock(SplitFactory.class), - splitCache, + splitCacheConsumer, new ImpressionsManager.NoOpImpressionsManager(), - new Metrics.NoopMetrics(), - NoopEventClient.create(), + NoopEventsStorageImp.create(), config, gates, - new EvaluatorImp(splitCache) + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp ); + Map attributes = new HashMap<>(); + Map result = client.getTreatmentsWithConfigByFlagSet("randomKey", "set1", attributes); + assertEquals(2, result.size()); + assertEquals(configurations.get("on"), result.get(test).config()); + assertNull(result.get(test2).config()); + assertEquals("control", result.get(test2).treatment()); + + verify(splitCacheConsumer, times(1)).fetchMany(anyList()); + + result = client.getTreatmentsWithConfigByFlagSet("randomKey", "set1"); + assertEquals(2, result.size()); + assertEquals(configurations.get("on"), result.get(test).config()); + assertNull(result.get(test2).config()); + assertEquals("control", result.get(test2).treatment()); + } - client.blockUntilReady(); + @Test + public void treatmentsWorksAndHasConfigFlagSets() { + String test = "test1"; + String test2 = "test2"; + + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + + // Add config for only one treatment + Map configurations = new HashMap<>(); + configurations.put(Treatments.ON, "{\"size\" : 30}"); + configurations.put(Treatments.CONTROL, "{\"size\" : 30}"); + + + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, + null, 1, 1, configurations, new HashSet<>(Arrays.asList("set1")), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); + + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(parsedSplits); + + List sets = new ArrayList<>(Arrays.asList("set1")); + Map> flagsBySets = new HashMap<>(); + flagsBySets.put("set1", new HashSet<>(Arrays.asList(test, test2))); + when(splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagsBySets); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculatorImp), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + flagSetsFilter, + fallbackTreatmentCalculatorImp + ); + Map attributes = new HashMap<>(); + Map result = client.getTreatmentsWithConfigByFlagSets("randomKey", new ArrayList<>(Arrays.asList("set1")), attributes); + assertEquals(2, result.size()); + assertEquals(configurations.get("on"), result.get(test).config()); + assertNull(result.get(test2).config()); + assertEquals("control", result.get(test2).treatment()); + + verify(splitCacheConsumer, times(1)).fetchMany(anyList()); + } + + @Test + public void impressionPropertiesTest() { + String test = "test1"; + + ParsedCondition age_equal_to_0_should_be_on = new ParsedCondition(ConditionType.ROLLOUT, + CombiningMatcher.of("age", new EqualToMatcher(-20, DataType.NUMBER)), + Lists.newArrayList(partition("on", 100)), + "foolabel" + ); + + List conditions = Lists.newArrayList(age_equal_to_0_should_be_on); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests(test, 123, false, Treatments.OFF, conditions, null, 1, 1, new HashSet<>(Arrays.asList("set")), true, new PrerequisitesMatcher(null)); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(test, parsedSplit); + + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(test)).thenReturn(parsedSplit); + when(splitCacheConsumer.fetchMany(Arrays.asList(test))).thenReturn(parsedSplits); + Map> splits = new HashMap<>(); + splits.put("set", new HashSet<>(Arrays.asList(test))); + when(splitCacheConsumer.getNamesByFlagSets(Arrays.asList("set"))).thenReturn(splits); + + SDKReadinessGates gates = mock(SDKReadinessGates.class); + ImpressionsManager impressionsManager = mock(ImpressionsManager.class); + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + impressionsManager, + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, null), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + new FallbackTreatmentCalculatorImp(null) + + ); + Map attributes = ImmutableMap.of("age", -20, "acv", "1000000"); + EvaluationOptions properties = new EvaluationOptions(new HashMap() + {{ + put("prop2", "val2"); + put("prop1", "val1"); + }}); + Map result = new HashMap<>(); + result.put(test, Treatments.ON); + List split_names = Arrays.asList(test); + + assertEquals("on", client.getTreatment("pato@codigo.com", test, attributes, properties)); + assertEquals("on", client.getTreatmentWithConfig("bilal1@codigo.com", test, attributes, properties).treatment()); + assertEquals("on", client.getTreatments("bilal2@codigo.com", Arrays.asList(test), attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsWithConfig("bilal3@codigo.com", Arrays.asList(test), attributes, properties).get(test).treatment()); + assertEquals("on", client.getTreatmentsByFlagSet("bilal4@codigo.com", "set", attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsByFlagSets("bilal5@codigo.com", Arrays.asList("set"), attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsWithConfigByFlagSet("bilal6@codigo.com", "set", attributes, properties).get(test).treatment()); + assertEquals("on", client.getTreatmentsWithConfigByFlagSets("bilal7@codigo.com", Arrays.asList("set"), attributes, properties).get(test).treatment()); + assertEquals("on", client.getTreatment(new Key("bilal8@codigo.com", "bilal8@codigo.com"), test, attributes, properties)); + assertEquals("on", client.getTreatmentWithConfig(new Key("bilal9@codigo.com", "bilal9@codigo.com"), test, attributes, properties).treatment()); + assertEquals("on", client.getTreatments(new Key("bilal10@codigo.com", "bilal10@codigo.com"), Arrays.asList(test), attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsWithConfig(new Key("bilal11@codigo.com", "bilal11@codigo.com"), Arrays.asList(test), attributes, properties).get(test).treatment()); + assertEquals("on", client.getTreatmentsByFlagSet(new Key("bilal12@codigo.com", "bilal12@codigo.com"), "set", attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsByFlagSets(new Key("bilal13@codigo.com", "bilal13@codigo.com"), Arrays.asList("set"), attributes, properties).get(test)); + assertEquals("on", client.getTreatmentsWithConfigByFlagSet(new Key("bilal14@codigo.com", "bilal14@codigo.com"), "set", attributes, properties).get(test).treatment()); + assertEquals("on", client.getTreatmentsWithConfigByFlagSets(new Key("bilal15@codigo.com", "bilal15@codigo.com"), Arrays.asList("set"), attributes, properties).get(test).treatment()); + assertEquals("off", client.getTreatment("bilal16@codigo.com", test, properties)); + assertEquals("off", client.getTreatmentWithConfig("bilal17@codigo.com", test, properties).treatment()); + assertEquals("off", client.getTreatments("bilal18@codigo.com", Arrays.asList(test), properties).get(test)); + assertEquals("off", client.getTreatmentsWithConfig("bilal19@codigo.com", Arrays.asList(test), properties).get(test).treatment()); + assertEquals("off", client.getTreatmentsByFlagSet("bilal20@codigo.com", "set", properties).get(test)); + assertEquals("off", client.getTreatmentsByFlagSets("bilal21@codigo.com", Arrays.asList("set"), properties).get(test)); + assertEquals("off", client.getTreatmentsWithConfigByFlagSet("bilal22@codigo.com", "set", properties).get(test).treatment()); + assertEquals("off", client.getTreatmentsWithConfigByFlagSets("bilal23@codigo.com", Arrays.asList("set"), properties).get(test).treatment()); + + ArgumentCaptor impressionCaptor = ArgumentCaptor.forClass(List.class); + verify(impressionsManager, times(24)).track(impressionCaptor.capture()); + assertNotNull(impressionCaptor.getValue()); + + DecoratedImpression impression = (DecoratedImpression) impressionCaptor.getAllValues().get(0).get(0); + assertEquals("pato@codigo.com", impression.impression().key()); + assertEquals("{\"prop2\":\"val2\",\"prop1\":\"val1\"}", impression.impression().properties()); + + for (int i=1; i<=23; i++) { + impression = (DecoratedImpression) impressionCaptor.getAllValues().get(i).get(0); + assertEquals("bilal" + i + "@codigo.com", impression.impression().key()); + assertEquals("{\"prop2\":\"val2\",\"prop1\":\"val1\"}", impression.impression().properties()); + } + } + + @Test + public void fallbackTreatmentWithExceptionsResult() { + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + when(splitCacheConsumer.get(anyString())).thenThrow(RuntimeException.class); + when(splitCacheConsumer.fetchMany(anyList())).thenThrow(RuntimeException.class); + HashMap> features = new HashMap<>(); + features.put("flag", new HashSet<>(Arrays.asList("test1"))); + when(splitCacheConsumer.getNamesByFlagSets(anyList())).thenReturn(features); + + String fallbcakConfigGlobal = "{\"prop1\", \"val1\"}"; + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new FallbackTreatment("on", fallbcakConfigGlobal)); + FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), + TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals("on", client.getTreatment("adil@relateiq.com", "test1")); + assertEquals("on", client.getTreatmentWithConfig("adil@relateiq.com", "test1").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentWithConfig("adil@relateiq.com", "test1").config()); + assertEquals("on", client.getTreatments("adil@relateiq.com", Arrays.asList("test1")).get("test1")); + assertEquals("on", client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("test1")).get("test1").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("test1")).get("test1").config()); + + assertEquals("on", client.getTreatmentsByFlagSet("adil@relateiq.com", "flag").get("test1")); + assertEquals("on", client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test1")); + assertEquals("on", client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test1").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test1").config()); + assertEquals("on", client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test1").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test1").config()); + + String fallbcakConfigByFlag = "{\"prop2\", \"val2\"}"; + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on", fallbcakConfigGlobal), + new HashMap() {{ put("feature", new FallbackTreatment("off", fallbcakConfigByFlag)); }}); + + features = new HashMap<>(); + features.put("flag", new HashSet<>(Arrays.asList("test", "feature"))); + when(splitCacheConsumer.getNamesByFlagSets(anyList())).thenReturn(features); + + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), + TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals("on", client.getTreatment("adil@relateiq.com", "test")); + assertEquals("off", client.getTreatment("adil@relateiq.com", "feature")); + assertEquals("on", client.getTreatmentWithConfig("adil@relateiq.com", "test1").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentWithConfig("adil@relateiq.com", "test1").config()); + assertEquals("off", client.getTreatmentWithConfig("adil@relateiq.com", "feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentWithConfig("adil@relateiq.com", "feature").config()); + Map result = client.getTreatments("adil@relateiq.com", Arrays.asList("feature", "test")); + assertEquals("off", result.get("feature")); + assertEquals("on", result.get("test")); + Map results = client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("feature", "test")); + assertEquals("off", results.get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("feature").config()); + assertEquals("on", results.get("test").treatment()); + assertEquals(fallbcakConfigGlobal, results.get("test").config()); + + assertEquals("on", client.getTreatmentsByFlagSet("adil@relateiq.com", "flag").get("test")); + assertEquals("off", client.getTreatmentsByFlagSet("adil@relateiq.com", "flag").get("feature")); + assertEquals("on", client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test")); + assertEquals("off", client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature")); + assertEquals("on", client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test").config()); + assertEquals("off", client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("feature").config()); + assertEquals("on", client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test").config()); + assertEquals("off", client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature").config()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new HashMap() {{ put("feature", new FallbackTreatment("off", fallbcakConfigByFlag)); }}); + + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals(Treatments.CONTROL, client.getTreatment("adil@relateiq.com", "test")); + assertEquals("off", client.getTreatment("adil@relateiq.com", "feature")); + assertEquals(Treatments.CONTROL, client.getTreatmentWithConfig("adil@relateiq.com", "test1").treatment()); + assertEquals(null, client.getTreatmentWithConfig("adil@relateiq.com", "test1").config()); + assertEquals("off", client.getTreatmentWithConfig("adil@relateiq.com", "feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentWithConfig("adil@relateiq.com", "feature").config()); + result = client.getTreatments("adil@relateiq.com", Arrays.asList("feature", "test")); + assertEquals("off", result.get("feature")); + assertEquals(Treatments.CONTROL, result.get("test")); + results = client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("feature", "test")); + assertEquals("off", results.get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("feature").config()); + assertEquals(Treatments.CONTROL, results.get("test").treatment()); + assertEquals(null, results.get("test").config()); + + assertEquals(Treatments.CONTROL, client.getTreatmentsByFlagSet("adil@relateiq.com", "flag").get("test")); + assertEquals("off", client.getTreatmentsByFlagSet("adil@relateiq.com", "flag").get("feature")); + assertEquals(Treatments.CONTROL, client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test")); + assertEquals("off", client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature")); + assertEquals(Treatments.CONTROL, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test").treatment()); + assertEquals(null, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("test").config()); + assertEquals("off", client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag").get("feature").config()); + assertEquals(Treatments.CONTROL, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test").treatment()); + assertEquals(null, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("test").config()); + assertEquals("off", client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")).get("feature").config()); + } + + @Test + public void fallbackTreatmentWithSplitNotFoundResult() { + SDKReadinessGates gates = mock(SDKReadinessGates.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = mock(SegmentCacheConsumer.class); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = mock(RuleBasedSegmentCacheConsumer.class); + ParsedCondition rollOutToEveryone = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new WhitelistMatcher(Lists.newArrayList("adil@codigo.com"))), Lists.newArrayList(partition("on", 100))); + List conditions = Lists.newArrayList(rollOutToEveryone); + ParsedSplit parsedSplit = ParsedSplit.createParsedSplitForTests("test", 123, false, Treatments.OFF, conditions, + null, 1, 1, new HashSet<>(), false, new PrerequisitesMatcher(null)); + + when(splitCacheConsumer.get("test1")).thenReturn(parsedSplit); + when(splitCacheConsumer.get("test2")).thenReturn(null); + when(splitCacheConsumer.get("test3")).thenReturn(null); + HashMap features = new HashMap<>(); + features.put("test1", parsedSplit); + features.put("test2", null); + features.put("test3", null); + when(splitCacheConsumer.fetchMany(anyList())).thenReturn(features); + HashMap> flagFeatures = new HashMap<>(); + flagFeatures.put("flag", new HashSet<>(Arrays.asList("test1", "test2", "test3"))); + when(splitCacheConsumer.getNamesByFlagSets(anyList())).thenReturn(flagFeatures); + + String fallbcakConfigGlobal = "{\"prop1\", \"val1\"}"; + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new FallbackTreatment("on", fallbcakConfigGlobal)); + FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + SplitClientImpl client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), + TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals("off", client.getTreatment("adil@relateiq.com", "test1")); + assertEquals("on", client.getTreatment("adil@relateiq.com", "test2")); + assertEquals("on", client.getTreatmentWithConfig("adil@relateiq.com", "test2").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentWithConfig("adil@relateiq.com", "test2").config()); + + Map result = client.getTreatments("adil@relateiq.com", Arrays.asList("test1", "test2")); + assertEquals("off", result.get("test1")); + assertEquals("on", result.get("test2")); + Map resultWithConfig = client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("test1", "test2")); + assertEquals("off", resultWithConfig.get("test1").treatment()); + assertEquals(null, resultWithConfig.get("test1").config()); + assertEquals("on", resultWithConfig.get("test2").treatment()); + assertEquals(fallbcakConfigGlobal, resultWithConfig.get("test2").config()); + + result = client.getTreatmentsByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", result.get("test1")); + assertEquals("on", result.get("test2")); + result = client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", result.get("test1")); + assertEquals("on", result.get("test2")); + resultWithConfig = client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", resultWithConfig.get("test1").treatment()); + assertEquals(null, resultWithConfig.get("test1").config()); + assertEquals("on", resultWithConfig.get("test2").treatment()); + assertEquals(fallbcakConfigGlobal, resultWithConfig.get("test2").config()); + resultWithConfig = client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", resultWithConfig.get("test1").treatment()); + assertEquals(null, resultWithConfig.get("test1").config()); + assertEquals("on", resultWithConfig.get("test2").treatment()); + assertEquals(fallbcakConfigGlobal, resultWithConfig.get("test2").config()); + + String fallbcakConfigByFlag = "{\"prop2\", \"val2\"}"; + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on", fallbcakConfigGlobal), + new HashMap() {{ put("test2", new FallbackTreatment("off-fallback", fallbcakConfigByFlag)); }}); + + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), + TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals("off", client.getTreatment("adil@relateiq.com", "test1")); + assertEquals("off-fallback", client.getTreatment("adil@relateiq.com", "test2")); + assertEquals("on", client.getTreatment("adil@relateiq.com", "test3")); + + assertEquals("off", client.getTreatmentWithConfig("adil@relateiq.com", "test1").treatment()); + assertEquals(null, client.getTreatmentWithConfig("adil@relateiq.com", "test1").config()); + assertEquals("off-fallback", client.getTreatmentWithConfig("adil@relateiq.com", "test2").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentWithConfig("adil@relateiq.com", "test2").config()); + assertEquals("on", client.getTreatmentWithConfig("adil@relateiq.com", "test3").treatment()); + assertEquals(fallbcakConfigGlobal, client.getTreatmentWithConfig("adil@relateiq.com", "test3").config()); + + result = client.getTreatments("adil@relateiq.com", Arrays.asList("test1", "test2", "test3")); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals("on", result.get("test3")); + + Map results = client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("test1", "test2", "test3")); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals("on", results.get("test3").treatment()); + assertEquals(fallbcakConfigGlobal, results.get("test3").config()); + + result = client.getTreatmentsByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals("on", result.get("test3")); + + result = client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals("on", result.get("test3")); + + results = client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals("on", results.get("test3").treatment()); + assertEquals(fallbcakConfigGlobal, results.get("test3").config()); + + results = client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals("on", results.get("test3").treatment()); + assertEquals(fallbcakConfigGlobal, results.get("test3").config()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new HashMap() {{ put("test2", new FallbackTreatment("off-fallback", fallbcakConfigByFlag)); }}); + + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + + client = new SplitClientImpl( + mock(SplitFactory.class), + splitCacheConsumer, + new ImpressionsManager.NoOpImpressionsManager(), + NoopEventsStorageImp.create(), + config, + gates, + new EvaluatorImp(splitCacheConsumer, segmentCacheConsumer, ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator), TELEMETRY_STORAGE, TELEMETRY_STORAGE, + new FlagSetsFilterImpl(new HashSet<>()), + fallbackTreatmentCalculator + ); + assertEquals("off", client.getTreatment("adil@relateiq.com", "test1")); + assertEquals("off-fallback", client.getTreatment("adil@relateiq.com", "test2")); + assertEquals(Treatments.CONTROL, client.getTreatment("adil@relateiq.com", "test3")); + + assertEquals("off", client.getTreatmentWithConfig("adil@relateiq.com", "test1").treatment()); + assertEquals(null, client.getTreatmentWithConfig("adil@relateiq.com", "test1").config()); + assertEquals("off-fallback", client.getTreatmentWithConfig("adil@relateiq.com", "test2").treatment()); + assertEquals(fallbcakConfigByFlag, client.getTreatmentWithConfig("adil@relateiq.com", "test2").config()); + assertEquals(Treatments.CONTROL, client.getTreatmentWithConfig("adil@relateiq.com", "test3").treatment()); + assertEquals(null, client.getTreatmentWithConfig("adil@relateiq.com", "test3").config()); + + result = client.getTreatments("adil@relateiq.com", Arrays.asList("test1", "test2", "test3")); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals(Treatments.CONTROL, result.get("test3")); + + results = client.getTreatmentsWithConfig("adil@relateiq.com", Arrays.asList("test1", "test2", "test3")); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals(Treatments.CONTROL, results.get("test3").treatment()); + assertEquals(null, results.get("test3").config()); + + result = client.getTreatmentsByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals(Treatments.CONTROL, result.get("test3")); + + result = client.getTreatmentsByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", result.get("test1")); + assertEquals("off-fallback", result.get("test2")); + assertEquals(Treatments.CONTROL, result.get("test3")); + + results = client.getTreatmentsWithConfigByFlagSet("adil@relateiq.com", "flag"); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals(Treatments.CONTROL, results.get("test3").treatment()); + assertEquals(null, results.get("test3").config()); + + results = client.getTreatmentsWithConfigByFlagSets("adil@relateiq.com", Arrays.asList("flag")); + assertEquals("off", results.get("test1").treatment()); + assertEquals(null, results.get("test1").config()); + assertEquals("off-fallback", results.get("test2").treatment()); + assertEquals(fallbcakConfigByFlag, results.get("test2").config()); + assertEquals(Treatments.CONTROL, results.get("test3").treatment()); + assertEquals(null, results.get("test3").config()); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SplitClientIntegrationTest.java b/client/src/test/java/io/split/client/SplitClientIntegrationTest.java index d7ab9ef6..69b1b366 100644 --- a/client/src/test/java/io/split/client/SplitClientIntegrationTest.java +++ b/client/src/test/java/io/split/client/SplitClientIntegrationTest.java @@ -3,27 +3,63 @@ import io.split.SSEMockServer; import io.split.SplitMockServer; import io.split.client.api.SplitView; +import io.split.client.dtos.EvaluationOptions; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.utils.CustomDispatcher; +import io.split.storages.enums.OperationMode; +import io.split.storages.enums.StorageMode; +import io.split.storages.pluggable.CustomStorageWrapperImp; +import io.split.storages.pluggable.domain.EventConsumer; +import io.split.storages.pluggable.domain.ImpressionConsumer; + +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; import org.awaitility.Awaitility; import org.glassfish.grizzly.utils.Pair; import org.glassfish.jersey.media.sse.OutboundEvent; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import javax.ws.rs.sse.OutboundSseEvent; import java.io.IOException; import java.net.URISyntaxException; -import java.util.Date; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Queue; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; public class SplitClientIntegrationTest { - // TODO: review this test. + @Test - @Ignore - public void getTreatmentWithStreamingEnabled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void getTreatmentWithStreamingEnabled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850110, \"t\":1585948850110}, \"rbs\":{\"d\":[],\"s\":1585948850110,\"t\":1585948850110}}"); + MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850111, \"t\":1585948850111}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + Queue responses2 = new LinkedList<>(); + responses2.add(response2); + Queue responses3 = new LinkedList<>(); + responses3.add(response3); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .path(CustomDispatcher.SINCE_1585948850110, responses2) + .path(CustomDispatcher.SINCE_1585948850111, responses3) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -35,6 +71,8 @@ public void getTreatmentWithStreamingEnabled() throws IOException, TimeoutExcept .endpoint(splitServer.getUrl(), splitServer.getUrl()) .authServiceURL(String.format("%s/api/auth/enabled", splitServer.getUrl())) .streamingServiceURL("http://localhost:" + sseServer.getPort()) + .featuresRefreshRate(20) + .segmentsRefreshRate(30) .streamingEnabled(true) .build(); @@ -46,6 +84,21 @@ public void getTreatmentWithStreamingEnabled() throws IOException, TimeoutExcept Assert.assertEquals("on_whitelist", result); // SPLIT_UPDATED should fetch -> changeNumber > since + + OutboundSseEvent sseEventWithPublishers = new OutboundEvent + .Builder() + .name("message") + .data("{\"id\":\"22\",\"timestamp\":1588254668328,\"encoding\":\"json\",\"channel\":\"[?occupancy=metrics.publishers]control_pri\",\"data\":\"{\\\"metrics\\\":{\\\"publishers\\\":2}}\",\"name\":\"[meta]occupancy\"}") + .build(); + eventQueue.push(sseEventWithPublishers); + + OutboundSseEvent sseEventWithoutPublishers = new OutboundEvent + .Builder() + .name("message") + .data("{\"id\":\"22\",\"timestamp\":1588254668328,\"encoding\":\"json\",\"channel\":\"[?occupancy=metrics.publishers]control_pri\",\"data\":\"{\\\"metrics\\\":{\\\"publishers\\\":0}}\",\"name\":\"[meta]occupancy\"}") + .build(); + eventQueue.push(sseEventWithoutPublishers); + OutboundSseEvent sseEvent1 = new OutboundEvent .Builder() .name("message") @@ -65,33 +118,14 @@ public void getTreatmentWithStreamingEnabled() throws IOException, TimeoutExcept .build(); eventQueue.push(sseEvent4); + Awaitility.await() .atMost(50L, TimeUnit.SECONDS) .until(() -> "after_notification_received".equals(client.getTreatment("admin", "push_test")) && "on_rollout".equals(client.getTreatment("test_in_segment", "push_test"))); - OutboundSseEvent sseEvent2 = new OutboundEvent - .Builder() - .name("message") - .data("{\"id\":\"22\",\"clientId\":\"22\",\"timestamp\":1592591696052,\"encoding\":\"json\",\"channel\":\"xxxx_xxxx_segments\",\"data\":\"{\\\"type\\\":\\\"SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1585948850111,\\\"segmentName\\\":\\\"segment3\\\"}\"}") - .build(); - eventQueue.push(sseEvent2); - - Awaitility.await() - .atMost(50L, TimeUnit.SECONDS) - .until(() -> "in_segment_match".equals(client.getTreatment("test_in_segment", "push_test"))); - - // SEGMENT_UPDATE should not fetch -> changeNumber < since - OutboundSseEvent sseEvent5 = new OutboundEvent - .Builder() - .name("message") - .data("{\"id\":\"22\",\"clientId\":\"22\",\"timestamp\":1592591696052,\"encoding\":\"json\",\"channel\":\"xxxx_xxxx_segments\",\"data\":\"{\\\"type\\\":\\\"SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1585948850109,\\\"segmentName\\\":\\\"segment3\\\"}\"}") - .build(); - eventQueue.push(sseEvent5); - - Awaitility.await() - .atMost(50L, TimeUnit.SECONDS) - .until(() -> "in_segment_match".equals(client.getTreatment("test_in_segment", "push_test"))); + eventQueue.push(sseEventWithPublishers); + eventQueue.push(sseEventWithoutPublishers); // SPLIT_KILL should fetch. OutboundSseEvent sseEvent3 = new OutboundEvent @@ -111,8 +145,13 @@ public void getTreatmentWithStreamingEnabled() throws IOException, TimeoutExcept } @Test - public void getTreatmentWithStreamingEnabledAndAuthDisabled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void getTreatmentWithStreamingEnabledAndAuthDisabled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850109,\"d\":[]}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); splitServer.start(); SplitClientConfig config = SplitClientConfig.builder() @@ -128,14 +167,24 @@ public void getTreatmentWithStreamingEnabledAndAuthDisabled() throws IOException String result = client.getTreatment("admin", "push_test"); Assert.assertEquals("on_whitelist", result); - + Assert.assertEquals("on", client.getTreatment("bilal@@split.io", "rbs_flag", new HashMap() {{ + put("email", "bilal@@split.io"); + }})); + Assert.assertEquals("off", client.getTreatment("mauro@split.io", "rbs_flag", new HashMap() {{ + put("email", "mauro@split.io"); + }})); client.destroy(); splitServer.stop(); } @Test - public void getTreatmentWithStreamingDisabled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void getTreatmentWithStreamingDisabled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); splitServer.start(); SplitClientConfig config = SplitClientConfig.builder() @@ -162,8 +211,13 @@ public void getTreatmentWithStreamingDisabled() throws IOException, TimeoutExcep } @Test - public void managerSplitsWithStreamingEnabled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void managerSplitsWithStreamingEnabled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -177,8 +231,8 @@ public void managerSplitsWithStreamingEnabled() throws IOException, TimeoutExcep manager.blockUntilReady(); List results = manager.splits(); - Assert.assertEquals(4, results.size()); - Assert.assertEquals(3, results.stream().filter(r -> !r.killed).toArray().length); + Assert.assertEquals(5, results.size()); + Assert.assertEquals(4, results.stream().filter(r -> !r.killed).toArray().length); // SPLIT_KILL should fetch. OutboundSseEvent sseEventSplitKill = new OutboundEvent @@ -190,15 +244,28 @@ public void managerSplitsWithStreamingEnabled() throws IOException, TimeoutExcep Awaitility.await() .atMost(2L, TimeUnit.MINUTES) - .until(() -> 2 == manager.splits().stream().filter(r -> !r.killed).toArray().length); + .until(() -> 3 == manager.splits().stream().filter(r -> !r.killed).toArray().length); splitServer.stop(); sseServer.stop(); } @Test - public void splitClientOccupancyNotifications() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void splitClientOccupancyNotifications() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850110, \"t\":1585948850110}, \"rbs\":{\"d\":[],\"s\":1585948850110,\"t\":1585948850110}}"); + MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850111, \"t\":1585948850111}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + Queue responses2 = new LinkedList<>(); + responses2.add(response2); + Queue responses3 = new LinkedList<>(); + responses3.add(response3); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .path(CustomDispatcher.SINCE_1585948850110, responses2) + .path(CustomDispatcher.SINCE_1585948850111, responses3) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -259,8 +326,21 @@ public void splitClientOccupancyNotifications() throws IOException, TimeoutExcep } @Test - public void splitClientControlNotifications() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void splitClientControlNotifications() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850110, \"t\":1585948850110}, \"rbs\":{\"d\":[],\"s\":1585948850110,\"t\":1585948850110}}"); + MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850111, \"t\":1585948850111}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + Queue responses2 = new LinkedList<>(); + responses2.add(response2); + Queue responses3 = new LinkedList<>(); + responses3.add(response3); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .path(CustomDispatcher.SINCE_1585948850110, responses2) + .path(CustomDispatcher.SINCE_1585948850111, responses3) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -341,8 +421,30 @@ public void splitClientControlNotifications() throws IOException, TimeoutExcepti } @Test - public void splitClientMultiFactory() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void splitClientMultiFactory() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + responses.add(response); + responses.add(response); + responses.add(response); + responses.add(response); + responses.add(response); + responses.add(response); + responses.add(response); + + SplitMockServer splitServer1 = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); + SplitMockServer splitServer2 = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); + SplitMockServer splitServer3 = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); + SplitMockServer splitServer4 = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); SSEMockServer.SseEventQueue eventQueue1 = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer1 = buildSSEMockServer(eventQueue1); @@ -356,28 +458,31 @@ public void splitClientMultiFactory() throws IOException, TimeoutException, Inte SSEMockServer.SseEventQueue eventQueue4 = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer4 = buildSSEMockServer(eventQueue4); - splitServer.start(); + splitServer1.start(); + splitServer2.start(); + splitServer3.start(); + splitServer4.start(); sseServer1.start(); sseServer2.start(); sseServer3.start(); sseServer4.start(); - SplitClientConfig config1 = buildSplitClientConfig("enabled", splitServer.getUrl(), sseServer1.getPort(), true, 20); + SplitClientConfig config1 = buildSplitClientConfig("enabled", splitServer1.getUrl(), sseServer1.getPort(), true, 20); SplitFactory factory1 = SplitFactoryBuilder.build("fake-api-token-1", config1); SplitClient client1 = factory1.client(); client1.blockUntilReady(); - SplitClientConfig config2 = buildSplitClientConfig("enabled", splitServer.getUrl(), sseServer2.getPort(), true, 20); + SplitClientConfig config2 = buildSplitClientConfig("enabled", splitServer2.getUrl(), sseServer2.getPort(), true, 20); SplitFactory factory2 = SplitFactoryBuilder.build("fake-api-token-2", config2); SplitClient client2 = factory2.client(); client2.blockUntilReady(); - SplitClientConfig config3 = buildSplitClientConfig("enabled", splitServer.getUrl(), sseServer3.getPort(), true, 20); + SplitClientConfig config3 = buildSplitClientConfig("enabled", splitServer3.getUrl(), sseServer3.getPort(), true, 20); SplitFactory factory3 = SplitFactoryBuilder.build("fake-api-token-3", config3); SplitClient client3 = factory3.client(); client3.blockUntilReady(); - SplitClientConfig config4 = buildSplitClientConfig("disabled", splitServer.getUrl(), sseServer4.getPort(), true, 100); + SplitClientConfig config4 = buildSplitClientConfig("disabled", splitServer4.getUrl(), sseServer4.getPort(), true, 100); SplitFactory factory4 = SplitFactoryBuilder.build("fake-api-token-4", config4); SplitClient client4 = factory4.client(); client4.blockUntilReady(); @@ -412,11 +517,11 @@ public void splitClientMultiFactory() throws IOException, TimeoutException, Inte eventQueue3.push(sseEventInitial); eventQueue4.push(sseEventInitial); - Thread.sleep(1000); + Thread.sleep(10000); eventQueue1.push(sseEventSplitUpdate); Awaitility.await() - .atMost(50L, TimeUnit.SECONDS) + .atMost(100L, TimeUnit.SECONDS) .until(() -> "split_killed".equals(client1.getTreatment("admin", "push_test"))); @@ -425,7 +530,7 @@ public void splitClientMultiFactory() throws IOException, TimeoutException, Inte .until(() -> "on_whitelist".equals(client2.getTreatment("admin", "push_test"))); Awaitility.await() - .atMost(50L, TimeUnit.SECONDS) + .atMost(100L, TimeUnit.SECONDS) .until(() -> "on_whitelist".equals(client3.getTreatment("admin", "push_test"))); Awaitility.await() @@ -447,26 +552,34 @@ public void splitClientMultiFactory() throws IOException, TimeoutException, Inte .until(() -> "split_killed".equals(client3.getTreatment("admin", "push_test"))); Awaitility.await() - .atMost(50L, TimeUnit.SECONDS) + .atMost(100L, TimeUnit.SECONDS) .until(() -> "on_whitelist".equals(client4.getTreatment("admin", "push_test"))); - client1.destroy(); client2.destroy(); client3.destroy(); client4.destroy(); - splitServer.stop(); + splitServer1.stop(); + splitServer2.stop(); + splitServer3.stop(); + splitServer4.stop(); sseServer1.stop(); sseServer2.stop(); sseServer3.stop(); sseServer4.stop(); } - // TODO: review this test. @Test - @Ignore - public void keepAlive() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void keepAlive() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); + + //plitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder().build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -482,7 +595,7 @@ public void keepAlive() throws IOException, TimeoutException, InterruptedExcepti Assert.assertEquals("on_whitelist", result); // wait to check keep alive notification. - Thread.sleep(80000); + Thread.sleep(50000); // must reconnect and after the second syncAll the result must be different Awaitility.await() @@ -495,8 +608,13 @@ public void keepAlive() throws IOException, TimeoutException, InterruptedExcepti } @Test - public void testConnectionClosedByRemoteHostIsProperlyHandled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void testConnectionClosedByRemoteHostIsProperlyHandled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -528,8 +646,13 @@ public void testConnectionClosedByRemoteHostIsProperlyHandled() throws IOExcepti } @Test - public void testConnectionClosedIsProperlyHandled() throws IOException, TimeoutException, InterruptedException, URISyntaxException { - SplitMockServer splitServer = new SplitMockServer(); + public void testConnectionClosedIsProperlyHandled() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.SINCE_1585948850109, responses) + .build()); SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); @@ -558,6 +681,793 @@ public void testConnectionClosedIsProperlyHandled() throws IOException, TimeoutE Assert.assertNotEquals("on_whitelist", result); } + @Test + public void testPluggableMode() throws IOException, URISyntaxException { + CustomStorageWrapperImp customStorageWrapper = new CustomStorageWrapperImp(); + SplitClientConfig config = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .setBlockUntilReadyTimeout(10000) + .streamingEnabled(true) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(customStorageWrapper) + .build(); + SplitFactory splitFactory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = splitFactory.client(); + try { + client.blockUntilReady(); + SplitManager splitManager = splitFactory.manager(); + HashMap properties = new HashMap<>(); + properties.put("number_property", 123); + properties.put("object_property", new Object()); + + client.track("key", "tt", "importantEventType"); + client.track("keyValue", "tt", "importantEventType", 12L); + client.track("keyProperties", "tt", "importantEventType", 12L, properties); + List events = customStorageWrapper.getEvents(); + List splits = splitManager.splits(); + + Assert.assertEquals(3, events.size()); + Assert.assertTrue(events.stream().anyMatch(e -> "key".equals(e.getEventDto().key) && "tt".equals(e.getEventDto().trafficTypeName))); + Assert.assertTrue(events.stream().anyMatch(e -> "keyValue".equals(e.getEventDto().key) && e.getEventDto().value == 12L)); + Assert.assertTrue(events.stream().anyMatch(e -> "keyProperties".equals(e.getEventDto().key) && e.getEventDto().properties != null)); + + Assert.assertEquals(3, splits.size()); + Assert.assertTrue(splits.stream().anyMatch(sw -> "first-name".equals(sw.name))); + Assert.assertTrue(splits.stream().anyMatch(sw -> "second-name".equals(sw.name))); + Assert.assertEquals("on", client.getTreatment("key", "first-name")); + Assert.assertEquals("off", client.getTreatmentWithConfig("FakeKey", "second-name").treatment()); + Assert.assertEquals("control", client.getTreatment("FakeKey", "noSplit")); + Assert.assertEquals("on", client.getTreatment("bilal@@split.io", "rbs_flag", new HashMap() {{ + put("email", "bilal@@split.io"); + }})); + Assert.assertEquals("off", client.getTreatment("mauro@split.io", "rbs_flag", new HashMap() {{ + put("email", "mauro@split.io"); + }})); + + List impressions = customStorageWrapper.getImps(); + Assert.assertEquals(4, impressions.size()); + Assert.assertTrue(impressions.stream().anyMatch(imp -> "first-name".equals(imp.getKeyImpression().feature) && "on".equals(imp.getKeyImpression().treatment))); + Assert.assertTrue(impressions.stream().anyMatch(imp -> "second-name".equals(imp.getKeyImpression().feature) && "off".equals(imp.getKeyImpression().treatment))); + + Map latencies = customStorageWrapper.getLatencies(); + + List keys = new ArrayList<>(latencies.keySet()); + + String key1 = keys.stream().filter(key -> key.contains("track/")).collect(Collectors.toList()).get(0); + String key2 = keys.stream().filter(key -> key.contains("getTreatment/")).collect(Collectors.toList()).get(0); + String key3 = keys.stream().filter(key -> key.contains("getTreatmentWithConfig/")).collect(Collectors.toList()).get(0); + + Assert.assertEquals(Optional.of(3L), Optional.ofNullable(latencies.get(key1))); + Assert.assertEquals(Optional.of(3L), Optional.of(latencies.get(key2))); + Assert.assertEquals(Optional.of(1L), Optional.of(latencies.get(key3))); + + Thread.sleep(500); + Assert.assertNotNull(customStorageWrapper.getConfig()); + String key = customStorageWrapper.getConfig().keySet().stream().collect(Collectors.toList()).get(0); + Assert.assertTrue(customStorageWrapper.getConfig().get(key).contains(StorageMode.PLUGGABLE.name())); + + } catch (TimeoutException | InterruptedException e) { + } + } + + @Test + public void getTreatmentFlagSetWithPolling() throws Exception { + MockResponse response = new MockResponse().setBody("{\"ff\":{\"d\":[{\"trafficTypeName\":\"client\",\"name\":\"workm\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set1\",\"set2\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"client\",\"name\":\"workm_set_3\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set3\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":1602796638344},\"rbs\":{\"d\":[],\"t\":-1,\"s\":-1}}"); + MockResponse responseFlag = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1602796638344, \"t\":1602796638344},\"rbs\":{\"d\":[],\"t\":-1,\"s\":-1}}"); + MockResponse segmentResponse = new MockResponse().setBody("{\"name\":\"new_segment\",\"added\":[\"user-1\"],\"removed\":[\"user-2\",\"user-3\"],\"since\":-1,\"till\":-1}"); + Queue responses = new LinkedList<>(); + responses.add(response); + Queue responsesFlags = new LinkedList<>(); + responsesFlags.add(responseFlag); + Queue segmentResponses = new LinkedList<>(); + segmentResponses.add(segmentResponse); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder() + .path(CustomDispatcher.INITIAL_FLAGS_BY_SETS, responses) + .path(CustomDispatcher.SINCE_1602796638344, responsesFlags) + .path(CustomDispatcher.SEGMENT_BY_FLAG_SET, segmentResponses) + .build()); + splitServer.start(); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(splitServer.getUrl(), splitServer.getUrl()) + .authServiceURL(String.format("%s/api/auth/enabled", splitServer.getUrl())) + .streamingEnabled(false) + .flagSetsFilter(Arrays.asList("set2", "set1")) + .featuresRefreshRate(5) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + String result = client.getTreatment("admin", "workm"); + Assert.assertEquals("on", result); + Assert.assertEquals("on", client.getTreatmentsByFlagSet("admin", "set1", new HashMap<>()).get("workm")); + + client.destroy(); + splitServer.stop(); + } + + @Test + public void ImpressionToggleOptimizedModeTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .telemetryURL(serverURL + "/v1") + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", new HashMap<>())); + client.destroy(); + boolean check1 = false, check2 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + check1 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertFalse(body.contains("impression_toggle_off")); + } + if (allRequests.get(i).getPath().equals("/v1/keys/ss")) { + check2 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertFalse(body.contains("without_impression_toggle")); + Assert.assertFalse(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("impression_toggle_off")); + } + } + server.shutdown(); + Assert.assertTrue(check1); + Assert.assertTrue(check2); + } + + @Test + public void ImpressionToggleDebugModeTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", new HashMap<>())); + client.destroy(); + boolean check1 = false, check2 = false, check3 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + check1 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertFalse(body.contains("impression_toggle_off")); + } + if (allRequests.get(i).getPath().equals("/v1/keys/ss")) { + check2 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertFalse(body.contains("without_impression_toggle")); + Assert.assertFalse(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("impression_toggle_off")); + } + if (allRequests.get(i).getPath().equals("/api/testImpressions/count")) { + check3 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertFalse(body.contains("without_impression_toggle")); + Assert.assertFalse(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("impression_toggle_off")); + } + } + server.shutdown(); + Assert.assertTrue(check1); + Assert.assertTrue(check2); + Assert.assertTrue(check3); + } + + @Test + public void ImpressionToggleNoneModeTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.NONE) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", new HashMap<>())); + Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", new HashMap<>())); + client.destroy(); + boolean check1 = false, check2 = false, check3 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + check1 = true; + } + if (allRequests.get(i).getPath().equals("/v1/keys/ss")) { + check2 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("impression_toggle_off")); + } + if (allRequests.get(i).getPath().equals("/api/testImpressions/count")) { + check3 = true; + String body = allRequests.get(i).getBody().readUtf8(); + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("impression_toggle_off")); + } + } + server.shutdown(); + Assert.assertFalse(check1); + Assert.assertTrue(check2); + Assert.assertTrue(check3); + } + + @Test + public void ImpressionPropertiesTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", new HashMap<>(), new EvaluationOptions(new HashMap() {{ put("prop1", "val1"); }}))); + Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", new HashMap<>(), new EvaluationOptions(new HashMap() + {{ + put("prop1", "val1"); + put("prop2", "val2"); + }}))); + Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_on", new EvaluationOptions(null))); + client.destroy(); + boolean check1 = false, check2 = false, check3 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + String body = allRequests.get(i).getBody().readUtf8(); + if (body.contains("user1")) { + check1 = true; + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("\"properties\":\"{\\\"prop1\\\":\\\"val1\\\"}\"")); + } + if (body.contains("user2")) { + check2 = true; + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("\"properties\":\"{\\\"prop2\\\":\\\"val2\\\",\\\"prop1\\\":\\\"val1\\\"}\"")); + } + if (body.contains("user3")) { + check3 = true; + Assert.assertTrue(body.contains("impression_toggle_on")); + Assert.assertTrue(body.contains("\"properties\":null")); + } + } + } + server.shutdown(); + Assert.assertTrue(check1); + Assert.assertTrue(check2); + } + + @Test + public void getTreatmentWithPrerequisites() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_prereq.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1585948850109&rbSince=1585948850109": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109},\"rbs\":{\"d\":[],\"t\":1585948850109,\"s\":1585948850109}}"); + case "/api/segmentChanges/segment-test?since=-1": + return new MockResponse().setResponseCode(200).setBody("{\"name\":\"segment-test\",\"added\":[\"user-1\"],\"removed\":[],\"since\":-1,\"till\":-1}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer splitServer = new MockWebServer(); + splitServer.setDispatcher(dispatcher); + splitServer.start(); + String serverURL = String.format("http://%s:%s", splitServer.getHostName(), splitServer.getPort()); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("on", client.getTreatment("bilal@split.io", "test_prereq", new HashMap() {{ + put("email", "bilal@@split.io"); + }})); + Assert.assertEquals("def_treatment", client.getTreatment("bilal@split.io", "test_prereq")); + Assert.assertEquals("def_treatment", client.getTreatment("mauro@split.io", "test_prereq", new HashMap() {{ + put("email", "mauro@@split.io"); + }})); + Assert.assertEquals("on", client.getTreatment("pato@split.io", "test_prereq", new HashMap() {{ + put("email", "pato@@split.io"); + }})); + + Assert.assertEquals("on_whitelist", client.getTreatment("bilal@split.io", "prereq_chain", new HashMap() {{ + put("email", "bilal@@split.io"); + }})); + Assert.assertEquals("on", client.getTreatment("pato@split.io", "prereq_chain", new HashMap() {{ + put("email", "pato@@split.io"); + }})); + Assert.assertEquals("on_default", client.getTreatment("mauro@split.io", "prereq_chain", new HashMap() {{ + put("email", "mauro@@split.io"); + }})); + + client.destroy(); + splitServer.shutdown(); + } + + @Test + public void FallbackTreatmentGlobalAndByFlagTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-fallback", "{\"prop1\", \"val1\"}"), + new HashMap() {{ put("feature", new FallbackTreatment("off-fallback", "{\"prop2\", \"val2\"}")); }}); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle")); + Assert.assertEquals("off-fallback", client.getTreatmentWithConfig("user2", "feature").treatment()); + Assert.assertEquals("{\"prop2\", \"val2\"}", client.getTreatmentWithConfig("user2", "feature").config()); + Assert.assertEquals("on-fallback", client.getTreatmentWithConfig("user2", "feature2").treatment()); + Assert.assertEquals("{\"prop1\", \"val1\"}", client.getTreatmentWithConfig("user2", "feature2").config()); + + client.destroy(); + boolean check1 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + String body = allRequests.get(i).getBody().readUtf8(); + if (body.contains("user1")) { + check1 = true; + Assert.assertTrue(body.contains("without_impression_toggle")); + } + } + } + server.shutdown(); + Assert.assertTrue(check1); + } + + @Test + public void FallbackTreatmentGlobalTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on-fallback", "{\"prop1\", \"val1\"}")); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle")); + Assert.assertEquals("on-fallback", client.getTreatmentWithConfig("user2", "feature").treatment()); + Assert.assertEquals("{\"prop1\", \"val1\"}", client.getTreatmentWithConfig("user2", "feature").config()); + Assert.assertEquals("on-fallback", client.getTreatmentWithConfig("user2", "feature2").treatment()); + Assert.assertEquals("{\"prop1\", \"val1\"}", client.getTreatmentWithConfig("user2", "feature2").config()); + + client.destroy(); + boolean check1 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + String body = allRequests.get(i).getBody().readUtf8(); + if (body.contains("user1")) { + check1 = true; + Assert.assertTrue(body.contains("without_impression_toggle")); + } + } + } + server.shutdown(); + Assert.assertTrue(check1); + } + + @Test + public void FallbackTreatmentByFlagTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new HashMap() {{ put("feature", new FallbackTreatment("off-fallback", "{\"prop2\", \"val2\"}")); }}); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + client.blockUntilReady(); + + Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle")); + Assert.assertEquals("off-fallback", client.getTreatmentWithConfig("user2", "feature").treatment()); + Assert.assertEquals("{\"prop2\", \"val2\"}", client.getTreatmentWithConfig("user2", "feature").config()); + Assert.assertEquals("control", client.getTreatmentWithConfig("user2", "feature2").treatment()); + Assert.assertEquals(null, client.getTreatmentWithConfig("user2", "feature2").config()); + + client.destroy(); + boolean check1 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + String body = allRequests.get(i).getBody().readUtf8(); + if (body.contains("user1")) { + check1 = true; + Assert.assertTrue(body.contains("without_impression_toggle")); + } + } + } + server.shutdown(); + Assert.assertTrue(check1); + } + + @Test + public void FallbackTreatmentNotReadyTest() throws Exception { + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + List allRequests = new ArrayList<>(); + Dispatcher dispatcher = new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { + allRequests.add(request); + switch (request.getPath()) { + case "/api/splitChanges?s=1.3&since=-1&rbSince=-1": + Thread.sleep(1000); + return new MockResponse().setResponseCode(200).setBody(splits); + case "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1": + return new MockResponse().setResponseCode(200).setBody("{\"ff\":{\"d\":[], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"d\":[],\"s\":-1,\"t\":-1}}"); + case "/api/testImpressions/bulk": + return new MockResponse().setResponseCode(200); + case "/api/testImpressions/count": + return new MockResponse().setResponseCode(200); + case "/v1/keys/ss": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/usage": + return new MockResponse().setResponseCode(200); + case "/v1/metrics/config": + return new MockResponse().setResponseCode(200); + } + return new MockResponse().setResponseCode(404); + } + }; + + MockWebServer server = new MockWebServer(); + server.setDispatcher(dispatcher); + + server.start(); + String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort()); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration("on-fallback"); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(serverURL, serverURL) + .telemetryURL(serverURL + "/v1") + .authServiceURL(String.format("%s/api/auth/enabled", serverURL)) + .streamingEnabled(false) + .featuresRefreshRate(5) + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .fallbackTreatments(fallbackTreatmentsConfiguration) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config); + SplitClient client = factory.client(); + + Assert.assertEquals("on-fallback", client.getTreatment("user1", "without_impression_toggle")); + Assert.assertEquals("on-fallback", client.getTreatment("user2", "feature")); + client.blockUntilReady(); + + client.destroy(); + boolean check1 = false, check2 = false; + for (int i=0; i < allRequests.size(); i++ ) { + if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) { + String body = allRequests.get(i).getBody().readUtf8(); + if (body.contains("user2")) { + check1 = true; + Assert.assertTrue(body.contains("feature")); + Assert.assertTrue(body.contains("fallback - not ready")); + } + if (body.contains("user1")) { + check2 = true; + Assert.assertTrue(body.contains("without_impression_toggle")); + Assert.assertTrue(body.contains("fallback - not ready")); + } + } + } + server.shutdown(); + Assert.assertTrue(check1); + Assert.assertTrue(check2); + } + private SSEMockServer buildSSEMockServer(SSEMockServer.SseEventQueue eventQueue) { return new SSEMockServer(eventQueue, (token, version, channel) -> { if (!"1.1".equals(version)) { @@ -577,4 +1487,4 @@ private SplitClientConfig buildSplitClientConfig(String authUrl, String splitSer .streamingEnabled(streamingEnabled) .build(); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SplitClientOnOffTest.java b/client/src/test/java/io/split/client/SplitClientOnOffTest.java new file mode 100644 index 00000000..d7cc0862 --- /dev/null +++ b/client/src/test/java/io/split/client/SplitClientOnOffTest.java @@ -0,0 +1,181 @@ +package io.split.client; + +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventListener; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.dtos.FallbackTreatmentCalculator; +import io.split.client.events.EventsStorageProducer; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.lifecycle.SdkEventsManager; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.SDKReadinessGates; +import io.split.engine.evaluator.Evaluator; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; + +public class SplitClientOnOffTest { + + private SdkEventsManager _manager; + private SplitClientImpl _client; + + @Before + public void setUp() { + _manager = SdkEventsManager.create(null); + TelemetryStorage telemetry = mock(TelemetryStorage.class); + _client = new SplitClientImpl(mock(SplitFactory.class), mock(SplitCacheConsumer.class), mock(ImpressionsManager.class), + mock(EventsStorageProducer.class), SplitClientConfig.builder().build(), new SDKReadinessGates(), + mock(Evaluator.class), telemetry, telemetry, mock(FlagSetsFilter.class), + mock(FallbackTreatmentCalculator.class), _manager); + } + + @After + public void tearDown() { + _manager.destroy(); + } + + private void becomeReady() { + _manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + } + + private void flagUpdate(String name) { + _manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, + SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Collections.singleton(name))); + } + + @Test + public void multipleListenersAndDuplicatesAreAllInvokedInOrder() { + becomeReady(); + List calls = new CopyOnWriteArrayList<>(); + SdkEventListener shared = metadata -> calls.add("shared"); + _client.on(SdkEvent.SDK_UPDATE, shared); + _client.on(SdkEvent.SDK_UPDATE, shared); + _client.on(SdkEvent.SDK_UPDATE, metadata -> calls.add("other")); + + flagUpdate("f1"); + + await().atMost(2, TimeUnit.SECONDS).until(() -> calls.size() == 3); + assertEquals(Arrays.asList("shared", "shared", "other"), calls); + } + + @Test + public void runnableOverloadRunsWithoutMetadata() { + becomeReady(); + AtomicInteger calls = new AtomicInteger(); + _client.on(SdkEvent.SDK_UPDATE, calls::incrementAndGet); + + flagUpdate("f1"); + + await().atMost(2, TimeUnit.SECONDS).until(() -> calls.get() == 1); + } + + @Test + public void listenerReceivesTheUpdateMetadata() { + becomeReady(); + List received = new CopyOnWriteArrayList<>(); + _client.on(SdkEvent.SDK_UPDATE, received::add); + + flagUpdate("f1"); + + await().atMost(2, TimeUnit.SECONDS).until(() -> received.size() == 1); + assertEquals(SdkEventType.FLAGS_UPDATE, received.get(0).getType()); + assertEquals(Collections.singleton("f1"), received.get(0).getNames()); + } + + @Test + public void lateSdkReadySubscriberIsReplayedWithNullMetadata() { + becomeReady(); + await().atMost(2, TimeUnit.SECONDS).until(() -> _manager.eventAlreadyTriggered(SdkEvent.SDK_READY)); + List received = new CopyOnWriteArrayList<>(); + _client.on(SdkEvent.SDK_READY, received::add); + + await().atMost(2, TimeUnit.SECONDS).until(() -> received.size() == 1); + assertNull(received.get(0)); + } + + @Test + public void offRemovesOnlyTheGivenEvent() throws InterruptedException { + AtomicInteger updates = new AtomicInteger(); + AtomicInteger ready = new AtomicInteger(); + _client.on(SdkEvent.SDK_UPDATE, updates::incrementAndGet); + _client.on(SdkEvent.SDK_READY, ready::incrementAndGet); + + _client.off(SdkEvent.SDK_UPDATE); + becomeReady(); + await().atMost(2, TimeUnit.SECONDS).until(() -> ready.get() == 1); + flagUpdate("f1"); + + Thread.sleep(200); + assertEquals(0, updates.get()); + } + + @Test + public void registeringAfterDestroyIsASilentNoOp() throws InterruptedException { + _manager.destroy(); + AtomicInteger calls = new AtomicInteger(); + + _client.on(SdkEvent.SDK_READY, calls::incrementAndGet); + _client.on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + _client.off(SdkEvent.SDK_READY); + becomeReady(); + + Thread.sleep(200); + assertEquals(0, calls.get()); + } + + @Test + public void nullArgumentsAreIgnoredWithoutThrowing() throws InterruptedException { + becomeReady(); + AtomicInteger calls = new AtomicInteger(); + + _client.on(null, calls::incrementAndGet); + _client.on(null, (SdkEventListener) metadata -> calls.incrementAndGet()); + _client.on(SdkEvent.SDK_UPDATE, (Runnable) null); + _client.on(SdkEvent.SDK_UPDATE, (SdkEventListener) null); + _client.off(null); + flagUpdate("f1"); + + Thread.sleep(200); + assertEquals(0, calls.get()); + } + + @Test + public void clientWithoutAnEventsManagerIgnoresRegistrations() { + TelemetryStorage telemetry = mock(TelemetryStorage.class); + SplitClientImpl client = new SplitClientImpl(mock(SplitFactory.class), mock(SplitCacheConsumer.class), + mock(ImpressionsManager.class), mock(EventsStorageProducer.class), SplitClientConfig.builder().build(), + new SDKReadinessGates(), mock(Evaluator.class), telemetry, telemetry, mock(FlagSetsFilter.class), + mock(FallbackTreatmentCalculator.class)); + + client.on(SdkEvent.SDK_READY, () -> { }); + client.off(SdkEvent.SDK_READY); + } + + @Test + public void noCommonsTypeIsReachableFromThePublicApi() { + for (Method method : SplitClient.class.getMethods()) { + assertFalse(method.getName(), method.getReturnType().getName().startsWith("io.harness")); + for (Class parameter : method.getParameterTypes()) { + assertFalse(method.getName(), parameter.getName().startsWith("io.harness")); + } + } + } +} diff --git a/client/src/test/java/io/split/client/SplitFactoryImplEventsTest.java b/client/src/test/java/io/split/client/SplitFactoryImplEventsTest.java new file mode 100644 index 00000000..11843cea --- /dev/null +++ b/client/src/test/java/io/split/client/SplitFactoryImplEventsTest.java @@ -0,0 +1,123 @@ +package io.split.client; + +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsManager; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.storages.enums.OperationMode; +import org.junit.Test; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +public class SplitFactoryImplEventsTest { + + private static SdkEventsManager eventsManagerOf(Object target) throws Exception { + Field field = target.getClass().getDeclaredField("_eventsManager"); + field.setAccessible(true); + return (SdkEventsManager) field.get(target); + } + + private static SplitFactoryImpl localhostFactory() throws Exception { + return new SplitFactoryImpl("localhost", SplitClientConfig.builder().setBlockUntilReadyTimeout(10000).build()); + } + + @Test + public void localhostFactoryBuildsOneManagerSharedWithTheClient() throws Exception { + SplitFactoryImpl factory = localhostFactory(); + try { + SdkEventsManager factoryManager = eventsManagerOf(factory); + assertNotNull(factoryManager); + assertSame(factoryManager, eventsManagerOf(factory.client())); + } finally { + factory.destroy(); + } + } + + @Test + public void destroyStopsDeliveryAndRegistrationAfterwardsIsNoOp() throws Exception { + SplitFactoryImpl factory = localhostFactory(); + SdkEventsManager manager = eventsManagerOf(factory); + AtomicInteger calls = new AtomicInteger(); + manager.on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + + factory.destroy(); + + manager.on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + Thread.sleep(200); + assertTrue(factory.isDestroyed()); + assertFalse(manager.eventAlreadyTriggered(SdkEvent.SDK_READY)); + org.junit.Assert.assertEquals(0, calls.get()); + } + + @Test + public void destroyTwiceDoesNotThrow() throws Exception { + SplitFactoryImpl factory = localhostFactory(); + factory.destroy(); + factory.destroy(); + assertTrue(factory.isDestroyed()); + } + + @Test + public void localhostFactoryDeliversSdkReadyExactlyOnce() throws Exception { + SplitFactoryImpl factory = new SplitFactoryImpl(SplitClientConfig.builder() + .splitFile("src/test/resources/splits_localhost.json") + .setBlockUntilReadyTimeout(10000) + .build()); + try { + AtomicInteger calls = new AtomicInteger(); + eventsManagerOf(factory).on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + org.awaitility.Awaitility.await().atMost(5, java.util.concurrent.TimeUnit.SECONDS).until(() -> calls.get() == 1); + Thread.sleep(300); + org.junit.Assert.assertEquals(1, calls.get()); + } finally { + factory.destroy(); + } + } + + @Test + public void consumerFactoryDeliversSdkReadyExactlyOnce() throws Exception { + CustomStorageWrapper wrapper = mock(CustomStorageWrapper.class); + org.mockito.Mockito.when(wrapper.connect()).thenReturn(true); + SplitClientConfig config = SplitClientConfig.builder() + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(wrapper) + .build(); + SplitFactoryImpl factory = new SplitFactoryImpl("token", config, wrapper); + try { + AtomicInteger calls = new AtomicInteger(); + eventsManagerOf(factory).on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + org.awaitility.Awaitility.await().atMost(5, java.util.concurrent.TimeUnit.SECONDS).until(() -> calls.get() == 1); + Thread.sleep(300); + org.junit.Assert.assertEquals(1, calls.get()); + } finally { + factory.destroy(); + } + } + + @Test + public void consumerFactoryBuildsAManagerAndDestroysIt() throws Exception { + CustomStorageWrapper wrapper = mock(CustomStorageWrapper.class); + SplitClientConfig config = SplitClientConfig.builder() + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(wrapper) + .build(); + SplitFactoryImpl factory = new SplitFactoryImpl("token", config, wrapper); + SdkEventsManager manager = eventsManagerOf(factory); + assertNotNull(manager); + assertSame(manager, eventsManagerOf(factory.client())); + + factory.destroy(); + factory.destroy(); + assertTrue(factory.isDestroyed()); + assertFalse(manager.eventAlreadyTriggered(SdkEvent.SDK_READY)); + } +} diff --git a/client/src/test/java/io/split/client/SplitFactoryImplTest.java b/client/src/test/java/io/split/client/SplitFactoryImplTest.java index 451bf7dc..82f3ea8c 100644 --- a/client/src/test/java/io/split/client/SplitFactoryImplTest.java +++ b/client/src/test/java/io/split/client/SplitFactoryImplTest.java @@ -1,11 +1,47 @@ package io.split.client; +import io.split.client.dtos.FallbackTreatment; +import io.split.client.dtos.FallbackTreatmentCalculatorImp; +import io.split.client.dtos.FallbackTreatmentsConfiguration; +import io.split.client.dtos.ProxyConfiguration; import io.split.client.impressions.ImpressionsManager; +import io.split.client.utils.FileTypeEnum; +import io.split.engine.evaluator.EvaluatorImp; import io.split.integrations.IntegrationsConfig; +import io.split.service.SplitHttpClientImpl; +import io.split.storages.enums.OperationMode; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; import junit.framework.TestCase; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.config.Registry; +import org.awaitility.Awaitility; +import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; +import static org.mockito.Mockito.when; +import pluggable.CustomStorageWrapper; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; + public class SplitFactoryImplTest extends TestCase { public static final String API_KEY ="29013ionasdasd09u"; @@ -15,7 +51,7 @@ public class SplitFactoryImplTest extends TestCase { @Test - public void testFactoryInstantiation() throws URISyntaxException { + public void testFactoryInstantiation() throws Exception { SplitClientConfig splitClientConfig = SplitClientConfig.builder() .enableDebug() .impressionsMode(ImpressionsManager.Mode.DEBUG) @@ -23,20 +59,37 @@ public void testFactoryInstantiation() throws URISyntaxException { .endpoint(ENDPOINT,EVENTS_ENDPOINT) .authServiceURL(AUTH_SERVICE) .setBlockUntilReadyTimeout(10000) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .fallbackTreatments(new FallbackTreatmentsConfiguration(new FallbackTreatment("on"))) .build(); SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig); assertNotNull(splitFactory.client()); assertNotNull(splitFactory.manager()); + + Field fallbackField = SplitClientImpl.class.getDeclaredField("_fallbackTreatmentCalculator"); + fallbackField.setAccessible(true); + FallbackTreatmentCalculatorImp fallbackCalc = (FallbackTreatmentCalculatorImp) fallbackField.get(splitFactory.client()); + assertNotNull(fallbackCalc); + + Field evalField = SplitClientImpl.class.getDeclaredField("_evaluator"); + evalField.setAccessible(true); + EvaluatorImp evaluatorImp = (EvaluatorImp) evalField.get(splitFactory.client()); + assertNotNull(fallbackCalc); + fallbackField = EvaluatorImp.class.getDeclaredField("_fallbackTreatmentCalculator"); + fallbackField.setAccessible(true); + fallbackCalc = (FallbackTreatmentCalculatorImp) fallbackField.get(evaluatorImp); + assertNotNull(fallbackCalc); } @Test - public void testFactoryInstantiationWithoutBlockUntilReady() throws URISyntaxException { + public void testFactoryInstantiationWithoutBlockUntilReady() throws Exception { SplitClientConfig splitClientConfig = SplitClientConfig.builder() .enableDebug() .impressionsMode(ImpressionsManager.Mode.DEBUG) .impressionsRefreshRate(1) .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) .authServiceURL(AUTH_SERVICE) .build(); SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig); @@ -46,13 +99,14 @@ public void testFactoryInstantiationWithoutBlockUntilReady() throws URISyntaxExc } @Test - public void testFactoryInstantiationIntegrationsConfig() throws URISyntaxException { + public void testFactoryInstantiationIntegrationsConfig() throws Exception { IntegrationsConfig integrationsConfig = new IntegrationsConfig.Builder().build(); SplitClientConfig splitClientConfig = SplitClientConfig.builder() .enableDebug() .impressionsMode(ImpressionsManager.Mode.DEBUG) .impressionsRefreshRate(1) .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) .authServiceURL(AUTH_SERVICE) .setBlockUntilReadyTimeout(1000) .integrations(integrationsConfig) @@ -64,39 +118,464 @@ public void testFactoryInstantiationIntegrationsConfig() throws URISyntaxExcepti } @Test - public void testFactoryInstantiationWithProxy() throws URISyntaxException { + public void testFactoryInstantiationWithLegacyProxy() throws Exception { SplitClientConfig splitClientConfig = SplitClientConfig.builder() .enableDebug() .impressionsMode(ImpressionsManager.Mode.DEBUG) .impressionsRefreshRate(1) - .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .endpoint(ENDPOINT, EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) .authServiceURL(AUTH_SERVICE) .setBlockUntilReadyTimeout(1000) - .proxyPort(6060) - .proxyUsername("test") - .proxyPassword("password") - .proxyHost(ENDPOINT) + .proxyPort(8888) + .proxyHost("proxy-host") + .proxyUsername("user") + .proxyPassword("pass") .build(); SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig); + assertNotNull(splitFactory.client()); + splitFactory.destroy(); + } + + @Test + public void testFactoryInstantiationWithProxyCredentials() throws Exception { + class MyBearerCredentialsProvider implements io.split.client.dtos.BasicCredentialsProvider { + @Override + public String getUsername() { + return "test"; + } + @Override + public String getPassword() { + return "password"; + } + }; + + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT, EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(1000) + .proxyConfiguration(ProxyConfiguration.builder() + .url(new URL("http://proxy-name:6060")) + .credentialsProvider(new MyBearerCredentialsProvider()) + .build()) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig); assertNotNull(splitFactory.client()); assertNotNull(splitFactory.manager()); + + Field splitHttpClientField = SplitFactoryImpl.class.getDeclaredField("_splitHttpClient"); + splitHttpClientField.setAccessible(true); + SplitHttpClientImpl client = (SplitHttpClientImpl) splitHttpClientField.get(splitFactory); + + Field httpClientField = SplitHttpClientImpl.class.getDeclaredField("_client"); + httpClientField.setAccessible(true); + Class InternalHttp = Class.forName("org.apache.hc.client5.http.impl.classic.InternalHttpClient"); + + Field routePlannerField = InternalHttp.getDeclaredField("routePlanner"); + routePlannerField.setAccessible(true); + DefaultProxyRoutePlanner routePlanner = (DefaultProxyRoutePlanner) routePlannerField.get(InternalHttp.cast(httpClientField.get(client))); + + Field proxyField = DefaultProxyRoutePlanner.class.getDeclaredField("proxy"); + proxyField.setAccessible(true); + HttpHost proxy = (HttpHost) proxyField.get(routePlanner); + + Assert.assertEquals("http", proxy.getSchemeName()); + Assert.assertEquals("proxy-name", proxy.getHostName()); + Assert.assertEquals(6060, proxy.getPort()); + + Field credentialsProviderField = InternalHttp.getDeclaredField("credentialsProvider"); + credentialsProviderField.setAccessible(true); + BasicCredentialsProvider credentialsProvider = (BasicCredentialsProvider) credentialsProviderField.get(InternalHttp.cast(httpClientField.get(client))); + + Field credMapField = BasicCredentialsProvider.class.getDeclaredField("credMap"); + credMapField.setAccessible(true); + ConcurrentHashMap credMap = (ConcurrentHashMap) credMapField.get(credentialsProvider); + + Assert.assertEquals("test", credMap.entrySet().stream().iterator().next().getValue().getUserName()); + assertNotNull(credMap.entrySet().stream().iterator().next().getValue().getUserPassword()); + + splitFactory.destroy(); + } + + @Test + public void testFactoryInstantiationWithProxyToken() throws Exception { + class MyBearerCredentialsProvider implements io.split.client.dtos.BearerCredentialsProvider { + @Override + public String getToken() { + return "123456789"; + } + }; + + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT, EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(1000) + .proxyConfiguration(ProxyConfiguration.builder() + .url(new URL("http://proxy-name:6060")) + .credentialsProvider(new MyBearerCredentialsProvider()) + .build()) + .build(); + SplitFactoryImpl splitFactory2 = new SplitFactoryImpl(API_KEY, splitClientConfig); + assertNotNull(splitFactory2.client()); + assertNotNull(splitFactory2.manager()); + + Field splitHttpClientField2 = SplitFactoryImpl.class.getDeclaredField("_splitHttpClient"); + splitHttpClientField2.setAccessible(true); + SplitHttpClientImpl client2 = (SplitHttpClientImpl) splitHttpClientField2.get(splitFactory2); + + Field httpClientField2 = SplitHttpClientImpl.class.getDeclaredField("_client"); + httpClientField2.setAccessible(true); + Class InternalHttp2 = Class.forName("org.apache.hc.client5.http.impl.classic.InternalHttpClient"); + + Field credentialsProviderField2 = InternalHttp2.getDeclaredField("credentialsProvider"); + credentialsProviderField2.setAccessible(true); + HttpClientDynamicCredentials credentialsProvider2 = (HttpClientDynamicCredentials) credentialsProviderField2.get(InternalHttp2.cast(httpClientField2.get(client2))); + + Field proxyRuntimeField = HttpClientDynamicCredentials.class.getDeclaredField("_bearerCredentialsProvider"); + proxyRuntimeField.setAccessible(true); + MyBearerCredentialsProvider proxyRuntime = (MyBearerCredentialsProvider) proxyRuntimeField.get(credentialsProvider2); + + assertNotNull("123456789", proxyRuntime.getToken()); + + splitFactory2.destroy(); } @Test - public void testFactoryDestroy() throws URISyntaxException { + public void testFactoryInstantiationWithProxyMtls() throws Exception { SplitClientConfig splitClientConfig = SplitClientConfig.builder() .enableDebug() .impressionsMode(ImpressionsManager.Mode.DEBUG) .impressionsRefreshRate(1) .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(1000) + .proxyConfiguration(ProxyConfiguration.builder() + .url(new URL("http://proxy-name:6060")) + .mtls(new FileInputStream("src/test/resources/keyStore.p12"), "split") + .build()) + .build(); + SplitFactoryImpl splitFactory3 = new SplitFactoryImpl(API_KEY, splitClientConfig); + assertNotNull(splitFactory3.client()); + assertNotNull(splitFactory3.manager()); + + Field splitHttpClientField3 = SplitFactoryImpl.class.getDeclaredField("_splitHttpClient"); + splitHttpClientField3.setAccessible(true); + SplitHttpClientImpl client3 = (SplitHttpClientImpl) splitHttpClientField3.get(splitFactory3); + + Field httpClientField3 = SplitHttpClientImpl.class.getDeclaredField("_client"); + httpClientField3.setAccessible(true); + Class InternalHttp3 = Class.forName("org.apache.hc.client5.http.impl.classic.InternalHttpClient"); + + Field connManagerField = InternalHttp3.getDeclaredField("connManager"); + connManagerField.setAccessible(true); + PoolingHttpClientConnectionManager connManager = (PoolingHttpClientConnectionManager) connManagerField.get(InternalHttp3.cast(httpClientField3.get(client3))); + + Field connectionOperatorField = PoolingHttpClientConnectionManager.class.getDeclaredField("connectionOperator"); + connectionOperatorField.setAccessible(true); + DefaultHttpClientConnectionOperator connectionOperator = (DefaultHttpClientConnectionOperator) connectionOperatorField.get(connManager); + + Field tlsSocketStrategyLookupField = DefaultHttpClientConnectionOperator.class.getDeclaredField("tlsSocketStrategyLookup"); + tlsSocketStrategyLookupField.setAccessible(true); + Registry tlsSocketStrategyLookup = (Registry) tlsSocketStrategyLookupField.get(connectionOperator); + + Field mapField = Registry.class.getDeclaredField("map"); + mapField.setAccessible(true); + Class map = mapField.get(tlsSocketStrategyLookup).getClass(); + + Class value = ((ConcurrentHashMap) map.cast(mapField.get(tlsSocketStrategyLookup))).get("https").getClass(); + + Field arg1Field = value.getDeclaredField("arg$1"); + arg1Field.setAccessible(true); + Class sslConnectionSocketFactory = arg1Field.get(((ConcurrentHashMap) map.cast(mapField.get(tlsSocketStrategyLookup))).get("https")).getClass(); + + Field socketFactoryField = sslConnectionSocketFactory.getDeclaredField("socketFactory"); + socketFactoryField.setAccessible(true); + Class socketFactory = socketFactoryField.get(arg1Field.get(((ConcurrentHashMap) map.cast(mapField.get(tlsSocketStrategyLookup))).get("https"))).getClass(); + + Field contextField = socketFactory.getDeclaredField("context"); + contextField.setAccessible(true); + Class context = Class.forName("sun.security.ssl.SSLContextImpl"); + + Field keyManagerField = context.getDeclaredField("keyManager"); + keyManagerField.setAccessible(true); + Class keyManager = keyManagerField.get(contextField.get(socketFactoryField.get(arg1Field.get(((ConcurrentHashMap) map.cast(mapField.get(tlsSocketStrategyLookup))).get("https"))))).getClass(); + + Field credentialsMapField = keyManager.getDeclaredField("credentialsMap"); + credentialsMapField.setAccessible(true); + HashMap credentialsMap = (HashMap) credentialsMapField.get(keyManagerField.get(contextField.get(socketFactoryField.get(arg1Field.get(((ConcurrentHashMap) map.cast(mapField.get(tlsSocketStrategyLookup))).get("https")))))); + + assertNotNull(credentialsMap.get("1")); + + splitFactory3.destroy(); + } + + @Test + public void testFactoryDestroy() throws Exception { + TelemetryStorage telemetryStorage = Mockito.mock(TelemetryStorage.class); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) .authServiceURL(AUTH_SERVICE) .setBlockUntilReadyTimeout(10000) .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig); + //Before destroy we replace telemetryStorage via reflection. + Field factoryDestroy = SplitFactoryImpl.class.getDeclaredField("_telemetryStorageProducer"); + factoryDestroy.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(factoryDestroy, factoryDestroy.getModifiers() & ~Modifier.FINAL); + + factoryDestroy.set(splitFactory, telemetryStorage); + splitFactory.destroy(); + + assertTrue(splitFactory.isDestroyed()); + Mockito.verify(telemetryStorage, Mockito.times(1)).recordSessionLength(Mockito.anyLong()); + } + + @Test + public void testFactoryConsumerInstantiation() throws Exception { + CustomStorageWrapper customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + UserStorageWrapper userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetrySynchronizer.class); + when(userStorageWrapper.connect()).thenReturn(true); + + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(10000) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(customStorageWrapper) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig, customStorageWrapper); + Field splitFactoryImpl = SplitFactoryImpl.class.getDeclaredField("_userStorageWrapper"); + splitFactoryImpl.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(splitFactoryImpl, splitFactoryImpl.getModifiers() & ~Modifier.FINAL); + splitFactoryImpl.set(splitFactory, userStorageWrapper); + + Field telemetryStorageProducer = SplitFactoryImpl.class.getDeclaredField("_telemetrySynchronizer"); + telemetryStorageProducer.setAccessible(true); + modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(telemetryStorageProducer, telemetryStorageProducer.getModifiers() & ~Modifier.FINAL); + telemetryStorageProducer.set(splitFactory, telemetrySynchronizer); + assertNotNull(splitFactory.client()); + assertNotNull(splitFactory.manager()); + Thread.sleep(1500); + Mockito.verify(userStorageWrapper, Mockito.times(1)).connect(); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeConfig(Mockito.anyObject(), Mockito.anyLong(), Mockito.anyObject(), Mockito.anyObject()); + + Field fallbackField = SplitClientImpl.class.getDeclaredField("_fallbackTreatmentCalculator"); + fallbackField.setAccessible(true); + FallbackTreatmentCalculatorImp fallbackCalc = (FallbackTreatmentCalculatorImp) fallbackField.get(splitFactory.client()); + assertNotNull(fallbackCalc); + + Field evalField = SplitClientImpl.class.getDeclaredField("_evaluator"); + evalField.setAccessible(true); + EvaluatorImp evaluatorImp = (EvaluatorImp) evalField.get(splitFactory.client()); + assertNotNull(fallbackCalc); + fallbackField = EvaluatorImp.class.getDeclaredField("_fallbackTreatmentCalculator"); + fallbackField.setAccessible(true); + fallbackCalc = (FallbackTreatmentCalculatorImp) fallbackField.get(evaluatorImp); + assertNotNull(fallbackCalc); + } + + @Test + public void testFactoryConsumerInstantiationRetryReadiness() throws Exception { + CustomStorageWrapper customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + UserStorageWrapper userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + when(userStorageWrapper.connect()).thenReturn(false).thenReturn(true); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(10000) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(customStorageWrapper) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig, customStorageWrapper); + Field splitFactoryImpl = SplitFactoryImpl.class.getDeclaredField("_userStorageWrapper"); + splitFactoryImpl.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(splitFactoryImpl, splitFactoryImpl.getModifiers() & ~Modifier.FINAL); + splitFactoryImpl.set(splitFactory, userStorageWrapper); + assertNotNull(splitFactory.client()); + assertNotNull(splitFactory.manager()); + Awaitility.await() + .atMost(5L, TimeUnit.SECONDS) + .untilAsserted(() -> Assert.assertTrue(userStorageWrapper.connect())); + + Mockito.verify(userStorageWrapper, Mockito.times(2)).connect(); + } + + @Test + public void testFactoryConsumerDestroy() throws NoSuchFieldException, URISyntaxException, IllegalAccessException { + CustomStorageWrapper customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + UserStorageWrapper userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + when(userStorageWrapper.connect()).thenReturn(false).thenReturn(true); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .enableDebug() + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .impressionsRefreshRate(1) + .endpoint(ENDPOINT,EVENTS_ENDPOINT) + .authServiceURL(AUTH_SERVICE) + .setBlockUntilReadyTimeout(10000) + .telemetryURL(SplitClientConfig.TELEMETRY_ENDPOINT) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(customStorageWrapper) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl(API_KEY, splitClientConfig, customStorageWrapper); + Field splitFactoryImpl = SplitFactoryImpl.class.getDeclaredField("_userStorageWrapper"); + splitFactoryImpl.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(splitFactoryImpl, splitFactoryImpl.getModifiers() & ~Modifier.FINAL); + splitFactoryImpl.set(splitFactory, userStorageWrapper); splitFactory.destroy(); assertTrue(splitFactory.isDestroyed()); + Mockito.verify(userStorageWrapper, Mockito.times(1)).disconnect(); + } + + @Test + public void testLocalhostLegacy() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhostYaml() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile("src/test/resources/split.yaml") + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof YamlLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJson() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile("src/test/resources/split_init.json") + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof JsonLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhostYamlInputStream() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, IOException { + InputStream inputStream = new FileInputStream("src/test/resources/split.yaml"); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, FileTypeEnum.YAML) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof YamlLocalhostSplitChangeFetcher); } + @Test + public void testLocalhosJsonInputStream() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, IOException { + InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, FileTypeEnum.JSON) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof JsonLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStreamNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(null, FileTypeEnum.JSON) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStreamAndFileTypeNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, IOException { + InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(inputStream, null) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } + + @Test + public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxException, NoSuchMethodException, InvocationTargetException, + IllegalAccessException, IOException { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .splitFile(null, null) + .setBlockUntilReadyTimeout(10000) + .build(); + SplitFactoryImpl splitFactory = new SplitFactoryImpl("localhost", splitClientConfig); + + Method method = SplitFactoryImpl.class.getDeclaredMethod("createSplitChangeFetcher", SplitClientConfig.class); + method.setAccessible(true); + Object splitChangeFetcher = method.invoke(splitFactory, splitClientConfig); + Assert.assertTrue(splitChangeFetcher instanceof LegacyLocalhostSplitChangeFetcher); + } } \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SplitManagerImplTest.java b/client/src/test/java/io/split/client/SplitManagerImplTest.java index 7ef068ac..f3c04454 100644 --- a/client/src/test/java/io/split/client/SplitManagerImplTest.java +++ b/client/src/test/java/io/split/client/SplitManagerImplTest.java @@ -1,175 +1,263 @@ package io.split.client; import com.google.common.collect.Lists; + import io.split.client.api.SplitView; +import io.split.client.dtos.Prerequisites; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.Json; import io.split.engine.ConditionsTestUtil; import io.split.engine.SDKReadinessGates; -import io.split.cache.SplitCache; import io.split.engine.experiments.ParsedCondition; import io.split.engine.experiments.ParsedSplit; +import io.split.engine.experiments.SplitParser; import io.split.engine.matchers.AllKeysMatcher; import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.PrerequisitesMatcher; import io.split.grammar.Treatments; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class SplitManagerImplTest { private SplitClientConfig config = SplitClientConfig.builder().setBlockUntilReadyTimeout(100).build(); + private static TelemetryStorage TELEMETRY_STORAGE = mock(InMemoryTelemetryStorage.class); + @Before + public void updateTelemetryStorage() { + TELEMETRY_STORAGE = mock(InMemoryTelemetryStorage.class); + } @Test public void splitCallWithNonExistentSplit() { String nonExistent = "nonExistent"; - SplitCache splitCache = Mockito.mock(SplitCache.class); - Mockito.when(splitCache.get(nonExistent)).thenReturn(null); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + when(splitCacheConsumer.get(nonExistent)).thenReturn(null); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); - assertThat(splitManager.split("nonExistent"), is(nullValue())); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); + Assert.assertNull(splitManager.split("nonExistent")); } @Test public void splitCallWithExistentSplit() { String existent = "existent"; - SplitCache splitCache = Mockito.mock(SplitCache.class); - ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1); - Mockito.when(splitCache.get(existent)).thenReturn(response); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + Prerequisites prereq = new Prerequisites(); + prereq.featureFlagName = "feature1"; + prereq.treatments = Lists.newArrayList("on"); + ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(), false, + new PrerequisitesMatcher(Lists.newArrayList(prereq))); + when(splitCacheConsumer.get(existent)).thenReturn(response); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); SplitView theOne = splitManager.split(existent); - assertThat(theOne.name, is(equalTo(response.feature()))); - assertThat(theOne.changeNumber, is(equalTo(response.changeNumber()))); - assertThat(theOne.killed, is(equalTo(response.killed()))); - assertThat(theOne.trafficType, is(equalTo(response.trafficTypeName()))); - assertThat(theOne.treatments.size(), is(equalTo(1))); - assertThat(theOne.treatments.get(0), is(equalTo("off"))); - assertThat(theOne.configs.size(), is(0)); + Assert.assertEquals(response.feature(), theOne.name); + Assert.assertEquals(response.changeNumber(), theOne.changeNumber); + Assert.assertEquals(response.killed(), theOne.killed); + Assert.assertEquals(response.trafficTypeName(), theOne.trafficType); + Assert.assertEquals(1, theOne.treatments.size()); + Assert.assertEquals("off", theOne.treatments.get(0)); + Assert.assertEquals(0, theOne.configs.size()); + Assert.assertEquals("off", theOne.defaultTreatment); + Assert.assertEquals(Lists.newArrayList(prereq), theOne.prerequisites); } @Test public void splitCallWithExistentSplitAndConfigs() { String existent = "existent"; - SplitCache splitCache = Mockito.mock(SplitCache.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); // Add config for only one treatment(default) Map configurations = new HashMap<>(); configurations.put(Treatments.OFF, "{\"size\" : 30}"); - ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, configurations); - Mockito.when(splitCache.get(existent)).thenReturn(response); + ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, configurations, new HashSet<>(), false, null); + when(splitCacheConsumer.get(existent)).thenReturn(response); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); SplitView theOne = splitManager.split(existent); - assertThat(theOne.name, is(equalTo(response.feature()))); - assertThat(theOne.changeNumber, is(equalTo(response.changeNumber()))); - assertThat(theOne.killed, is(equalTo(response.killed()))); - assertThat(theOne.trafficType, is(equalTo(response.trafficTypeName()))); - assertThat(theOne.treatments.size(), is(equalTo(1))); - assertThat(theOne.treatments.get(0), is(equalTo("off"))); - assertThat(theOne.configs.get("off"), is(equalTo("{\"size\" : 30}"))); + + Assert.assertEquals(response.feature(), theOne.name); + Assert.assertEquals(response.changeNumber(), theOne.changeNumber); + Assert.assertEquals(response.killed(), theOne.killed); + Assert.assertEquals(response.trafficTypeName(), theOne.trafficType); + Assert.assertEquals(1, theOne.treatments.size()); + Assert.assertEquals("off", theOne.treatments.get(0)); + Assert.assertEquals("{\"size\" : 30}", theOne.configs.get("off")); } @Test public void splitsCallWithNoSplit() { - SplitCache splitCache = Mockito.mock(SplitCache.class); - Mockito.when(splitCache.getAll()).thenReturn(Lists.newArrayList()); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); - assertThat(splitManager.splits(), is(empty())); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + when(splitCacheConsumer.getAll()).thenReturn(Lists.newArrayList()); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + when(gates.isSDKReady()).thenReturn(false); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + gates, TELEMETRY_STORAGE); + Assert.assertTrue(splitManager.splits().isEmpty()); + verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage(); } @Test public void splitsCallWithSplit() { - SplitCache splitCache = Mockito.mock(SplitCache.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); List parsedSplits = Lists.newArrayList(); - ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + when(gates.isSDKReady()).thenReturn(false); + ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(), false, null); parsedSplits.add(response); - Mockito.when(splitCache.getAll()).thenReturn(parsedSplits); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); + when(splitCacheConsumer.getAll()).thenReturn(parsedSplits); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + gates, TELEMETRY_STORAGE); List splits = splitManager.splits(); - assertThat(splits.size(), is(equalTo(1))); - assertThat(splits.get(0).name, is(equalTo(response.feature()))); - assertThat(splits.get(0).changeNumber, is(equalTo(response.changeNumber()))); - assertThat(splits.get(0).killed, is(equalTo(response.killed()))); - assertThat(splits.get(0).trafficType, is(equalTo(response.trafficTypeName()))); - assertThat(splits.get(0).treatments.size(), is(equalTo(1))); - assertThat(splits.get(0).treatments.get(0), is(equalTo("off"))); + Assert.assertEquals(1, splits.size()); + Assert.assertEquals(response.feature(), splits.get(0).name); + Assert.assertEquals(response.changeNumber(), response.changeNumber()); + Assert.assertEquals(response.killed(), splits.get(0).killed); + Assert.assertEquals(response.trafficTypeName(), splits.get(0).trafficType); + Assert.assertEquals(1, splits.get(0).treatments.size()); + Assert.assertEquals("off", splits.get(0).treatments.get(0)); + verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage(); } @Test public void splitNamesCallWithNoSplit() { - SplitCache splitCache = Mockito.mock(SplitCache.class); - Mockito.when(splitCache.getAll()).thenReturn(Lists.newArrayList()); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); - assertThat(splitManager.splitNames(), is(empty())); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + when(splitCacheConsumer.getAll()).thenReturn(Lists.newArrayList()); + SDKReadinessGates gates = mock(SDKReadinessGates.class); + when(gates.isSDKReady()).thenReturn(false); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + gates, TELEMETRY_STORAGE); + Assert.assertTrue(splitManager.splitNames().isEmpty()); + verify(TELEMETRY_STORAGE, times(1)).recordNonReadyUsage(); } @Test public void splitNamesCallWithSplit() { - SplitCache splitCache = Mockito.mock(SplitCache.class); - List parsedSplits = Lists.newArrayList(); - ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1); - parsedSplits.add(response); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + List parsedSplits = new ArrayList<>(); + parsedSplits.add("FeatureName"); - Mockito.when(splitCache.getAll()).thenReturn(parsedSplits); - SplitManagerImpl splitManager = new SplitManagerImpl(splitCache, - Mockito.mock(SplitClientConfig.class), - Mockito.mock(SDKReadinessGates.class)); + when(splitCacheConsumer.splitNames()).thenReturn(parsedSplits); + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); List splitNames = splitManager.splitNames(); - assertThat(splitNames.size(), is(equalTo(1))); - assertThat(splitNames.get(0), is(equalTo(response.feature()))); + Assert.assertEquals(1, splitNames.size()); + Assert.assertEquals("FeatureName",splitNames.get(0)); } @Test - public void block_until_ready_does_not_time_when_sdk_is_ready() throws TimeoutException, InterruptedException { + public void blockUntilReadyDoesNotTimeWhenSdkIsReady() throws TimeoutException, InterruptedException { SDKReadinessGates ready = mock(SDKReadinessGates.class); - when(ready.isSDKReady(100)).thenReturn(true); - SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCache.class), + when(ready.waitUntilInternalReady(100)).thenReturn(true); + SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCacheConsumer.class), config, - ready); + ready, TELEMETRY_STORAGE); splitManager.blockUntilReady(); } @Test(expected = TimeoutException.class) - public void block_until_ready_times_when_sdk_is_not_ready() throws TimeoutException, InterruptedException { + public void blockUntilReadyTimesWhenSdkIsNotReady() throws TimeoutException, InterruptedException { SDKReadinessGates ready = mock(SDKReadinessGates.class); - when(ready.isSDKReady(100)).thenReturn(false); + when(ready.waitUntilInternalReady(100)).thenReturn(false); - SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCache.class), + SplitManagerImpl splitManager = new SplitManagerImpl(mock(SplitCacheConsumer.class), config, - ready); + ready, TELEMETRY_STORAGE); splitManager.blockUntilReady(); + verify(TELEMETRY_STORAGE, times(1)).recordBURTimeout(); + } + + @Test + public void splitCallWithExistentSets() { + String existent = "existent"; + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", + Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, new HashSet<>(Arrays.asList("set1", "set2", "set3")), false, null); + when(splitCacheConsumer.get(existent)).thenReturn(response); + + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); + SplitView theOne = splitManager.split(existent); + Assert.assertEquals(response.flagSets().size(), theOne.sets.size()); + } + + @Test + public void splitCallWithEmptySets() { + String existent = "existent"; + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + ParsedSplit response = ParsedSplit.createParsedSplitForTests("FeatureName", 123, true, "off", + Lists.newArrayList(getTestCondition("off")), "traffic", 456L, 1, null, false, null); + when(splitCacheConsumer.get(existent)).thenReturn(response); + + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); + SplitView theOne = splitManager.split(existent); + Assert.assertEquals(0, theOne.sets.size()); } private ParsedCondition getTestCondition(String treatment) { return ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(ConditionsTestUtil.partition(treatment, 10))); } -} + @Test + public void ImpressionToggleParseTest() throws IOException { + SplitParser parser = new SplitParser(); + String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(splits, SplitChange.class); + SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class); + for (Split split : change.featureFlags.d) { + ParsedSplit parsedSplit = parser.parse(split); + when(splitCacheConsumer.get(split.name)).thenReturn(parsedSplit); + } + SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer, + mock(SplitClientConfig.class), + mock(SDKReadinessGates.class), TELEMETRY_STORAGE); + + SplitView splitView = splitManager.split("without_impression_toggle"); + assertFalse(splitView.impressionsDisabled); + splitView = splitManager.split("impression_toggle_on"); + assertFalse(splitView.impressionsDisabled); + splitView = splitManager.split("impression_toggle_off"); + assertTrue(splitView.impressionsDisabled); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/SplitTimeoutEventsTest.java b/client/src/test/java/io/split/client/SplitTimeoutEventsTest.java new file mode 100644 index 00000000..a0b4174b --- /dev/null +++ b/client/src/test/java/io/split/client/SplitTimeoutEventsTest.java @@ -0,0 +1,124 @@ +package io.split.client; + +import io.split.client.api.SdkEvent; +import io.split.client.lifecycle.SdkEventsManager; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.SDKReadinessGates; +import io.split.engine.evaluator.Evaluator; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.dtos.FallbackTreatmentCalculator; +import io.split.client.events.EventsStorageProducer; +import io.split.storages.SplitCacheConsumer; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +public class SplitTimeoutEventsTest { + + private static final SplitClientConfig CONFIG = SplitClientConfig.builder().setBlockUntilReadyTimeout(50).build(); + + private static SplitClientImpl client(SDKReadinessGates gates, SdkEventsManager manager) { + TelemetryStorage telemetry = mock(TelemetryStorage.class); + return new SplitClientImpl(mock(SplitFactory.class), mock(SplitCacheConsumer.class), mock(ImpressionsManager.class), + mock(EventsStorageProducer.class), CONFIG, gates, mock(Evaluator.class), telemetry, telemetry, + mock(FlagSetsFilter.class), mock(FallbackTreatmentCalculator.class), manager); + } + + @Test + public void clientBlockUntilReadyTimeoutEmitsTimedOutOnceWithNullMetadata() throws Exception { + SdkEventsManager manager = SdkEventsManager.create(null); + AtomicInteger timedOut = new AtomicInteger(); + AtomicInteger ready = new AtomicInteger(); + manager.on(SdkEvent.SDK_READY_TIMED_OUT, metadata -> { + assertNull(metadata); + timedOut.incrementAndGet(); + }); + manager.on(SdkEvent.SDK_READY, metadata -> ready.incrementAndGet()); + SplitClientImpl client = client(new SDKReadinessGates(), manager); + + for (int i = 0; i < 2; i++) { + try { + client.blockUntilReady(); + org.junit.Assert.fail("expected TimeoutException"); + } catch (TimeoutException expected) { + // behaviour unchanged + } + } + + await().atMost(2, TimeUnit.SECONDS).until(() -> timedOut.get() == 1); + Thread.sleep(200); + assertEquals(1, timedOut.get()); + assertEquals(0, ready.get()); + manager.destroy(); + } + + @Test + public void readinessWinningTheRaceSuppressesTimedOut() throws Exception { + SdkEventsManager manager = SdkEventsManager.create(null); + AtomicInteger timedOut = new AtomicInteger(); + AtomicInteger ready = new AtomicInteger(); + manager.on(SdkEvent.SDK_READY_TIMED_OUT, metadata -> timedOut.incrementAndGet()); + manager.on(SdkEvent.SDK_READY, metadata -> ready.incrementAndGet()); + + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, io.split.client.api.SdkEventMetadata.ready(true, null)); + await().atMost(2, TimeUnit.SECONDS).until(() -> ready.get() == 1); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED, null); + + Thread.sleep(300); + assertEquals(1, ready.get()); + assertEquals(0, timedOut.get()); + manager.destroy(); + } + + @Test + public void managerBlockUntilReadyTimeoutNotifiesTimeoutReached() throws Exception { + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + SplitManagerImpl manager = new SplitManagerImpl(mock(SplitCacheConsumer.class), CONFIG, new SDKReadinessGates(), + mock(TelemetryStorage.class), notifier); + + try { + manager.blockUntilReady(); + org.junit.Assert.fail("expected TimeoutException"); + } catch (TimeoutException expected) { + // behaviour unchanged + } + + verify(notifier).notify(eq(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED), eq(null)); + } + + @Test + public void blockUntilReadyThatSucceedsNotifiesNothing() throws Exception { + SDKReadinessGates gates = new SDKReadinessGates(); + gates.sdkInternalReady(); + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + new SplitManagerImpl(mock(SplitCacheConsumer.class), CONFIG, gates, mock(TelemetryStorage.class), notifier) + .blockUntilReady(); + verifyZeroInteractions(notifier); + } + + @Test(expected = IllegalArgumentException.class) + public void nonPositiveTimeoutStillThrowsIllegalArgument() throws Exception { + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + try { + new SplitManagerImpl(mock(SplitCacheConsumer.class), SplitClientConfig.builder().build(), new SDKReadinessGates(), + mock(TelemetryStorage.class), notifier).blockUntilReady(); + } finally { + verify(notifier, never()).notify(any(SdkInternalEvent.class), any(io.split.client.api.SdkEventMetadata.class)); + } + } +} diff --git a/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java new file mode 100644 index 00000000..f37367ae --- /dev/null +++ b/client/src/test/java/io/split/client/YamlLocalhostSplitChangeFetcherTest.java @@ -0,0 +1,86 @@ +package io.split.client; + +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.FileInputStreamProvider; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.LocalhostUtils; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class YamlLocalhostSplitChangeFetcherTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testParseSplitChange() throws IOException { + File file = folder.newFile(SplitClientConfig.LOCALHOST_DEFAULT_FILE); + + List> allSplits = new ArrayList(); + + Map split1_user_a = new LinkedHashMap<>(); + Map split1_user_a_data = new LinkedHashMap<>(); + split1_user_a_data.put("keys", "user_a"); + split1_user_a_data.put("treatment", "off"); + split1_user_a_data.put("config", "{ \"size\" : 20 }"); + split1_user_a.put("split_1", split1_user_a_data); + allSplits.add(split1_user_a); + + Map split1_user_b = new LinkedHashMap<>(); + Map split1_user_b_data = new LinkedHashMap<>(); + split1_user_b_data.put("keys", "user_b"); + split1_user_b_data.put("treatment", "on"); + split1_user_b.put("split_1", split1_user_b_data); + allSplits.add(split1_user_b); + + Map split2_user_a = new LinkedHashMap<>(); + Map split2_user_a_data = new LinkedHashMap<>(); + split2_user_a_data.put("keys", "user_a"); + split2_user_a_data.put("treatment", "off"); + split2_user_a_data.put("config", "{ \"size\" : 20 }"); + split2_user_a.put("split_2", split2_user_a_data); + allSplits.add(split2_user_a); + + + Yaml yaml = new Yaml(); + StringWriter writer = new StringWriter(); + yaml.dump(allSplits, writer); + LocalhostUtils.writeFile(file, writer); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider(file.getAbsolutePath()); + YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + + Assert.assertEquals(2, splitChange.featureFlags.d.size()); + Assert.assertEquals(-1, splitChange.featureFlags.s); + Assert.assertEquals(-1, splitChange.featureFlags.t); + + + for (Split split: splitChange.featureFlags.d) { + Assert.assertEquals("control", split.defaultTreatment); + } + } + + @Test(expected = IllegalStateException.class) + public void processTestForException() { + InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.yaml"); + YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SplitChange splitChange = localhostSplitChangeFetcher.fetch(-1L, -1, fetchOptions); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/api/SdkEventMetadataTest.java b/client/src/test/java/io/split/client/api/SdkEventMetadataTest.java new file mode 100644 index 00000000..52ce6293 --- /dev/null +++ b/client/src/test/java/io/split/client/api/SdkEventMetadataTest.java @@ -0,0 +1,84 @@ +package io.split.client.api; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class SdkEventMetadataTest { + + @Test + public void updateRetainsNames() { + SdkEventMetadata m = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a", "b")); + assertEquals(SdkEventType.FLAGS_UPDATE, m.getType()); + assertEquals(2, m.getNames().size()); + assertTrue(m.getNames().contains("a")); + assertTrue(m.getNames().contains("b")); + } + + @Test + public void updateDropsNullAndBlankNames() { + SdkEventMetadata m = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a", null, "", " ")); + assertEquals(Collections.singleton("a"), m.getNames()); + } + + @Test + public void updateWithNullCollectionYieldsEmptyNames() { + assertTrue(SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, null).getNames().isEmpty()); + } + + @Test(expected = UnsupportedOperationException.class) + public void namesAreUnmodifiable() { + SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a")).getNames().add("b"); + } + + @Test + public void namesAreDefensivelyCopied() { + List source = new ArrayList<>(Arrays.asList("a")); + SdkEventMetadata m = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, source); + source.add("b"); + assertEquals(Collections.singleton("a"), m.getNames()); + } + + @Test + public void updateHasNoReadyFields() { + SdkEventMetadata m = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a")); + assertFalse(m.isInitialCacheLoad()); + assertNull(m.getLastUpdateTimestamp()); + } + + @Test + public void readyStandaloneShape() { + SdkEventMetadata m = SdkEventMetadata.ready(true, null); + assertTrue(m.isInitialCacheLoad()); + assertNull(m.getLastUpdateTimestamp()); + assertNull(m.getType()); + assertTrue(m.getNames().isEmpty()); + } + + @Test + public void readyConsumerShape() { + SdkEventMetadata m = SdkEventMetadata.ready(false, null); + assertFalse(m.isInitialCacheLoad()); + assertNull(m.getLastUpdateTimestamp()); + } + + @Test + public void equalsAndHashCode() { + SdkEventMetadata a = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("x")); + SdkEventMetadata b = SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("x")); + SdkEventMetadata c = SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, Arrays.asList("x")); + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + assertNotEquals(a, c); + assertNotEquals(SdkEventMetadata.ready(true, 1L), SdkEventMetadata.ready(true, 2L)); + } +} diff --git a/client/src/test/java/io/split/client/dtos/FallbackTreatmentCalculationImpTest.java b/client/src/test/java/io/split/client/dtos/FallbackTreatmentCalculationImpTest.java new file mode 100644 index 00000000..854a1a0e --- /dev/null +++ b/client/src/test/java/io/split/client/dtos/FallbackTreatmentCalculationImpTest.java @@ -0,0 +1,40 @@ +package io.split.client.dtos; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class FallbackTreatmentCalculationImpTest { + + @Test + public void TestWorks() { + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on")); + FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + assertEquals("on", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment()); + assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"), + new HashMap() {{ put("flag", "off"); }} ); + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + assertEquals("on", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment()); + assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel()); + assertEquals("off", fallbackTreatmentCalculator.resolve("flag", "exception").getTreatment()); + assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("flag", "exception").getLabel()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration( + new HashMap() {{ put("flag", new FallbackTreatment("off")); }} ); + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + assertEquals("control", fallbackTreatmentCalculator.resolve("anyflag", "exception").getTreatment()); + assertEquals("exception", fallbackTreatmentCalculator.resolve("anyflag", "exception").getLabel()); + assertEquals("off", fallbackTreatmentCalculator.resolve("flag", "exception").getTreatment()); + assertEquals("fallback - exception", fallbackTreatmentCalculator.resolve("flag", "exception").getLabel()); + } +} diff --git a/client/src/test/java/io/split/client/dtos/FallbackTreatmentConfigurationTest.java b/client/src/test/java/io/split/client/dtos/FallbackTreatmentConfigurationTest.java new file mode 100644 index 00000000..a09cf599 --- /dev/null +++ b/client/src/test/java/io/split/client/dtos/FallbackTreatmentConfigurationTest.java @@ -0,0 +1,32 @@ +package io.split.client.dtos; + +import org.junit.Test; + +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +public class FallbackTreatmentConfigurationTest { + + @Test + public void TestWorks() { + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on")); + assertEquals("on", fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getTreatment()); + assertEquals(null, fallbackTreatmentsConfiguration.getByFlagFallbackTreatment()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on", "{\"prop\":\"val\"}"), + new HashMap() {{ put("flag", new FallbackTreatment("off", "{\"prop2\":\"val2\"}")); }} ); + assertEquals("on", fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getTreatment()); + assertEquals("{\"prop\":\"val\"}", fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getConfig()); + assertEquals(null, fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getLabel()); + assertEquals("off", fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get("flag").getTreatment()); + assertEquals("{\"prop2\":\"val2\"}", fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get("flag").getConfig()); + assertEquals(null, fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get("flag").getLabel()); + + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration("on", + new HashMap() {{ put("flag", "off"); }} ); + assertEquals("on", fallbackTreatmentsConfiguration.getGlobalFallbackTreatment().getTreatment()); + assertEquals("off", fallbackTreatmentsConfiguration.getByFlagFallbackTreatment().get("flag").getTreatment()); + + } +} diff --git a/client/src/test/java/io/split/client/dtos/ImpressionCountTest.java b/client/src/test/java/io/split/client/dtos/ImpressionCountTest.java index ee154c29..140a4b30 100644 --- a/client/src/test/java/io/split/client/dtos/ImpressionCountTest.java +++ b/client/src/test/java/io/split/client/dtos/ImpressionCountTest.java @@ -9,7 +9,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; diff --git a/client/src/test/java/io/split/client/dtos/KeyImpressionTest.java b/client/src/test/java/io/split/client/dtos/KeyImpressionTest.java index c4ac5d12..11ff40b3 100644 --- a/client/src/test/java/io/split/client/dtos/KeyImpressionTest.java +++ b/client/src/test/java/io/split/client/dtos/KeyImpressionTest.java @@ -25,6 +25,7 @@ public void TestShrinkedPropertyNames() { imp.changeNumber = 123L; imp.time = 456L; imp.previousTime = 789L; + imp.properties = "{\"name\": \"value\"}"; String serialized = gson.toJson(imp); Map deSerialized = gson.fromJson(serialized, new TypeToken>() { }.getType()); @@ -65,5 +66,11 @@ public void TestShrinkedPropertyNames() { assertThat(previousTime, is(notNullValue())); assertThat(previousTime, instanceOf(Double.class)); assertThat(previousTime, is(789.0)); + + Object properties = deSerialized.get(KeyImpression.FIELD_PROPERTIES); + assertThat(properties, is(notNullValue())); + assertThat(properties, instanceOf(String.class)); + assertThat(properties, is("{\"name\": \"value\"}")); + } } diff --git a/client/src/test/java/io/split/client/dtos/UniqueKeysTest.java b/client/src/test/java/io/split/client/dtos/UniqueKeysTest.java new file mode 100644 index 00000000..cd078efa --- /dev/null +++ b/client/src/test/java/io/split/client/dtos/UniqueKeysTest.java @@ -0,0 +1,42 @@ +package io.split.client.dtos; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsInstanceOf.instanceOf; + +public class UniqueKeysTest { + + @Test + public void TestShrinkedPropertyNames() { + Gson gson = new Gson(); + List keys = new ArrayList<>(); + keys.add("key-1"); + keys.add("key-2"); + List uniqueKeys = new ArrayList<>(); + uniqueKeys.add(new UniqueKeys.UniqueKey("feature-1", keys)); + UniqueKeys imp = new UniqueKeys(uniqueKeys); + String serialized = gson.toJson(imp); + + HashMap parsedRaw = gson.fromJson(serialized, new TypeToken>(){}.getType()); + assertThat(parsedRaw.get("keys"), instanceOf(List.class)); + List asList = (ArrayList) parsedRaw.get("keys"); + assertThat(asList.size(), is(equalTo(1))); + + Map item0 = (Map) asList.get(0); + assertThat(item0.get("f"), is(equalTo("feature-1"))); + + List ks = (List) item0.get("ks"); + assertThat(ks.get(0), is(equalTo("key-1"))); + assertThat(ks.get(1), is(equalTo("key-2"))); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/events/EventsSenderTest.java b/client/src/test/java/io/split/client/events/EventsSenderTest.java new file mode 100644 index 00000000..c9ddb754 --- /dev/null +++ b/client/src/test/java/io/split/client/events/EventsSenderTest.java @@ -0,0 +1,79 @@ +package io.split.client.events; + +import io.split.TestHelper; +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.*; + +public class EventsSenderTest { + + private static final TelemetryRuntimeProducer TELEMETRY_RUNTIME_CONSUMER = Mockito + .mock(TelemetryRuntimeProducer.class); + private static final CloseableHttpClient CLOSEABLE_HTTP_CLIENT = Mockito.mock(CloseableHttpClient.class); + + @Test + public void testDefaultURL() throws URISyntaxException { + SplitHttpClient SPLIT_HTTP_CLIENT = SplitHttpClientImpl.create(CLOSEABLE_HTTP_CLIENT, + new RequestDecorator(null), "qwerty", metadata()); + URI rootTarget = URI.create("https://api.split.io"); + EventsSender fetcher = EventsSender.create(SPLIT_HTTP_CLIENT, rootTarget, TELEMETRY_RUNTIME_CONSUMER); + Assert.assertEquals("https://api.split.io/api/events/bulk", fetcher.getBulkEndpoint().toString()); + } + + @Test + public void testCustomURLNoPathNoBackslash() throws URISyntaxException { + SplitHttpClient SPLIT_HTTP_CLIENT = SplitHttpClientImpl.create(CLOSEABLE_HTTP_CLIENT, + new RequestDecorator(null), "qwerty", metadata()); + URI rootTarget = URI.create("https://kubernetesturl.com"); + EventsSender fetcher = EventsSender.create(SPLIT_HTTP_CLIENT, rootTarget, TELEMETRY_RUNTIME_CONSUMER); + Assert.assertEquals("https://kubernetesturl.com/api/events/bulk", fetcher.getBulkEndpoint().toString()); + } + + @Test + public void testCustomURLAppendingPath() throws URISyntaxException { + SplitHttpClient SPLIT_HTTP_CLIENT = SplitHttpClientImpl.create(CLOSEABLE_HTTP_CLIENT, + new RequestDecorator(null), "qwerty", metadata()); + URI rootTarget = URI.create("https://kubernetesturl.com/split/"); + EventsSender fetcher = EventsSender.create(SPLIT_HTTP_CLIENT, rootTarget, TELEMETRY_RUNTIME_CONSUMER); + Assert.assertEquals("https://kubernetesturl.com/split/api/events/bulk", fetcher.getBulkEndpoint().toString()); + } + + @Test + public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { + SplitHttpClient SPLIT_HTTP_CLIENT = SplitHttpClientImpl.create(CLOSEABLE_HTTP_CLIENT, + new RequestDecorator(null), "qwerty", metadata()); + URI rootTarget = URI.create("https://kubernetesturl.com/split"); + EventsSender fetcher = EventsSender.create(SPLIT_HTTP_CLIENT, rootTarget, TELEMETRY_RUNTIME_CONSUMER); + Assert.assertEquals("https://kubernetesturl.com/split/api/events/bulk", fetcher.getBulkEndpoint().toString()); + } + + @Test + public void testHttpError() throws URISyntaxException, IOException, InvocationTargetException, + IllegalAccessException, NoSuchMethodException { + URI rootTarget = URI.create("https://kubernetesturl.com/split"); + CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_BAD_REQUEST); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + EventsSender sender = EventsSender.create(splitHtpClient, rootTarget, TELEMETRY_RUNTIME_CONSUMER); + // should not raise exception + sender.sendEvents(new ArrayList<>()); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + +} diff --git a/client/src/test/java/io/split/client/events/EventsTaskTest.java b/client/src/test/java/io/split/client/events/EventsTaskTest.java new file mode 100644 index 00000000..93d5d0d5 --- /dev/null +++ b/client/src/test/java/io/split/client/events/EventsTaskTest.java @@ -0,0 +1,102 @@ +package io.split.client.events; + +import io.split.client.dtos.Event; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class EventsTaskTest { + private static final EventsSender EVENTS_SENDER = Mockito.mock(EventsSender.class); + + @Test + public void testEventsAreSending() throws InterruptedException { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(10000, telemetryRuntimeProducer); + EventsSender eventsSender = Mockito.mock(EventsSender.class); + EventsTask eventClient = new EventsTask(eventsStorage, + 2000, + eventsSender, + null); + eventClient.start(); + + for (int i = 0; i < 159; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + } + + Thread.sleep(1000); + + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + Thread.sleep(2000); + Mockito.verify(eventsSender, Mockito.times(1)).sendEvents(Mockito.anyObject()); + } + + @Test + public void testEventsWhenCloseTask() throws InterruptedException { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + EventsSender eventsSender = Mockito.mock(EventsSender.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(10000, telemetryRuntimeProducer); + EventsTask eventClient = new EventsTask(eventsStorage, + 2000, + eventsSender, + null); + + for (int i = 0; i < 159; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + } + + eventClient.close(); + Thread.sleep(2000); + Mockito.verify(eventsSender, Mockito.times(1)).sendEvents(Mockito.anyObject()); + } + + @Test + public void testCheckQueFull() { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(10, telemetryRuntimeProducer); + EventsTask eventClient = new EventsTask(eventsStorage, + 2000, + EVENTS_SENDER, + null); + + for (int i = 0; i < 10; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + } + Assert.assertTrue(eventsStorage.isFull()); + } + + @Test + public void testTimesSendingEvents() throws InterruptedException { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + EventsSender eventsSender = Mockito.mock(EventsSender.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(100, telemetryRuntimeProducer); + EventsTask eventClient = new EventsTask(eventsStorage, + 2000, + eventsSender, + null); + eventClient.start(); + + for (int i = 0; i < 10; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + } + + Thread.sleep(3000); + Mockito.verify(eventsSender, Mockito.times(1)).sendEvents(Mockito.anyObject()); + + for (int i = 0; i < 10; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1024 * 32); + } + + Thread.sleep(3000); + Mockito.verify(eventsSender, Mockito.times(2)).sendEvents(Mockito.anyObject()); + eventClient.close(); + Thread.sleep(1000); + Mockito.verify(eventsSender, Mockito.times(2)).sendEvents(Mockito.anyObject()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/events/InMemoryEventsStorageTest.java b/client/src/test/java/io/split/client/events/InMemoryEventsStorageTest.java new file mode 100644 index 00000000..3fe8e41c --- /dev/null +++ b/client/src/test/java/io/split/client/events/InMemoryEventsStorageTest.java @@ -0,0 +1,85 @@ +package io.split.client.events; + +import io.split.client.dtos.Event; +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.concurrent.BlockingQueue; + +public class InMemoryEventsStorageTest{ + + @Test + public void testDropEvent() { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(2, telemetryRuntimeProducer); + + for (int i = 0; i < 3; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1); + } + + Mockito.verify(telemetryRuntimeProducer, Mockito.times(2)).recordEventStats(EventsDataRecordsEnum.EVENTS_QUEUED, 1); + Mockito.verify(telemetryRuntimeProducer, Mockito.times(1)).recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 1); + } + + @Test + public void testTrackAndPop() { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + InMemoryEventsStorage eventsStorage = new InMemoryEventsStorage(10, telemetryRuntimeProducer); + + for (int i = 0; i < 5; ++i) { + Event event = new Event(); + eventsStorage.track(event, 1); + } + + Assert.assertEquals(5, eventsStorage.queueSize()); + Assert.assertNotNull(eventsStorage.pop()); + } + + @Test + public void testPopFailed() throws NoSuchFieldException, IllegalAccessException, InterruptedException { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue blockingQueue = Mockito.mock(BlockingQueue.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(2, telemetryRuntimeProducer); + Field eventsQueue = InMemoryEventsStorage.class.getDeclaredField("_eventQueue"); + eventsQueue.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(eventsQueue, eventsQueue.getModifiers() & ~Modifier.FINAL); + eventsQueue.set(eventsStorage, blockingQueue); + Mockito.when(blockingQueue.take()).thenThrow(new InterruptedException()); + Assert.assertNull(eventsStorage.pop()); + } + + @Test + public void testTrackException() throws NoSuchFieldException, IllegalAccessException { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue blockingQueue = Mockito.mock(BlockingQueue.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(2, telemetryRuntimeProducer); + + Field eventsQueue = InMemoryEventsStorage.class.getDeclaredField("_eventQueue"); + eventsQueue.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(eventsQueue, eventsQueue.getModifiers() & ~Modifier.FINAL); + eventsQueue.set(eventsStorage, blockingQueue); + Mockito.when(blockingQueue.offer(Mockito.anyObject())).thenThrow(new ClassCastException()); + + + Assert.assertEquals(false, eventsStorage.track(new Event(), 1)); + Mockito.verify(telemetryRuntimeProducer, Mockito.times(1)).recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 1); + } + + @Test + public void testEventNullThenFalse() { + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue blockingQueue = Mockito.mock(BlockingQueue.class); + EventsStorage eventsStorage = new InMemoryEventsStorage(2, telemetryRuntimeProducer); + Assert.assertEquals(false, eventsStorage.track(null, 1)); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/BloomFilterImpTest.java b/client/src/test/java/io/split/client/impressions/BloomFilterImpTest.java new file mode 100644 index 00000000..de0c8a3d --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/BloomFilterImpTest.java @@ -0,0 +1,42 @@ +package io.split.client.impressions; + +import io.split.client.impressions.filters.BloomFilterImp; +import org.junit.Assert; +import org.junit.Test; + +public class BloomFilterImpTest { + + @Test + public void addSomeElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + Assert.assertEquals(true, bloomFilterImp.add("feature key-1")); + Assert.assertEquals(true, bloomFilterImp.add("feature key-2")); + Assert.assertEquals(true, bloomFilterImp.add("feature key-3")); + } + + @Test + public void checkContainSomeElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + Assert.assertTrue(bloomFilterImp.add("feature key-1")); + Assert.assertTrue(bloomFilterImp.add("feature key-2")); + Assert.assertTrue(bloomFilterImp.add("feature key-3")); + + Assert.assertEquals(true, bloomFilterImp.contains("feature key-1")); + Assert.assertEquals(true, bloomFilterImp.contains("feature key-2")); + Assert.assertEquals(true, bloomFilterImp.contains("feature key-3")); + } + + @Test + public void removedElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + bloomFilterImp.add("feature key-1"); + bloomFilterImp.add("feature key-2"); + bloomFilterImp.add("feature key-3"); + + bloomFilterImp.clear(); + + Assert.assertEquals(false, bloomFilterImp.contains("feature key-1")); + Assert.assertEquals(false, bloomFilterImp.contains("feature key-2")); + Assert.assertEquals(false, bloomFilterImp.contains("feature key-3")); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/FilterAdapterImplTest.java b/client/src/test/java/io/split/client/impressions/FilterAdapterImplTest.java new file mode 100644 index 00000000..0c0875d6 --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/FilterAdapterImplTest.java @@ -0,0 +1,48 @@ +package io.split.client.impressions; + +import io.split.client.impressions.filters.BloomFilterImp; +import io.split.client.impressions.filters.FilterAdapterImpl; +import org.junit.Assert; +import org.junit.Test; + +public class FilterAdapterImplTest { + + @Test + public void addSomeElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + FilterAdapterImpl filterAdapter = new FilterAdapterImpl(bloomFilterImp); + + Assert.assertEquals(true, filterAdapter.add("feature", "key-1")); + Assert.assertEquals(true, filterAdapter.add("feature", "key-2")); + Assert.assertEquals(true, filterAdapter.add("feature", "key-3")); + } + + @Test + public void checkContainSomeElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + FilterAdapterImpl filterAdapter = new FilterAdapterImpl(bloomFilterImp); + + Assert.assertTrue(filterAdapter.add("feature","key-1")); + Assert.assertTrue(filterAdapter.add("feature","key-2")); + Assert.assertTrue(filterAdapter.add("feature","key-3")); + + Assert.assertEquals(true, filterAdapter.contains("feature","key-1")); + Assert.assertEquals(true, filterAdapter.contains("feature","key-2")); + Assert.assertEquals(true, filterAdapter.contains("feature","key-3")); + } + + @Test + public void removedElements(){ + BloomFilterImp bloomFilterImp = new BloomFilterImp(5,0.01); + FilterAdapterImpl filterAdapter = new FilterAdapterImpl(bloomFilterImp); + filterAdapter.add("feature","key-1"); + filterAdapter.add("feature","key-2"); + filterAdapter.add("feature"," key-3"); + + filterAdapter.clear(); + + Assert.assertEquals(false, bloomFilterImp.contains("feature key-1")); + Assert.assertEquals(false, bloomFilterImp.contains("feature key-2")); + Assert.assertEquals(false, bloomFilterImp.contains("feature key-3")); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java b/client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java index 876e2bf3..00471171 100644 --- a/client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java +++ b/client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java @@ -3,9 +3,15 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import io.split.TestHelper; +import io.split.client.RequestDecorator; import io.split.client.dtos.ImpressionCount; import io.split.client.dtos.KeyImpression; import io.split.client.dtos.TestImpressions; +import io.split.client.utils.SDKMetadata; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.classic.methods.HttpUriRequest; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; @@ -22,6 +28,7 @@ import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -34,47 +41,69 @@ import static org.mockito.Mockito.verify; public class HttpImpressionsSenderTest { + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + @Test public void testDefaultURL() throws URISyntaxException { URI rootTarget = URI.create("https://api.split.io"); CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpImpressionsSender fetcher = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/testImpressions/bulk"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpImpressionsSender fetcher = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.DEBUG, TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://api.split.io/api/testImpressions/bulk"))); } @Test public void testCustomURLNoPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com"); CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpImpressionsSender fetcher = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/api/testImpressions/bulk"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpImpressionsSender fetcher = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.DEBUG, TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/api/testImpressions/bulk"))); } @Test public void testCustomURLAppendingPath() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split/"); CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpImpressionsSender fetcher = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/testImpressions/bulk"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpImpressionsSender fetcher = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.DEBUG, TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/testImpressions/bulk"))); } @Test public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpImpressionsSender fetcher = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG); - Assert.assertThat(fetcher.getTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/testImpressions/bulk"))); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpImpressionsSender fetcher = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.DEBUG, TELEMETRY_STORAGE); + Assert.assertThat(fetcher.getTarget().toString(), + Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/testImpressions/bulk"))); } @Test - public void testImpressionCountsEndpointOptimized() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void testImpressionCountsEndpointOptimized() throws URISyntaxException, IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); // Setup response mock CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_OK); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); // Send counters - HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.OPTIMIZED); + HttpImpressionsSender sender = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.OPTIMIZED, TELEMETRY_STORAGE); HashMap toSend = new HashMap<>(); toSend.put(new ImpressionCounter.Key("test1", 0), 4); toSend.put(new ImpressionCounter.Key("test2", 0), 5); @@ -84,8 +113,8 @@ public void testImpressionCountsEndpointOptimized() throws URISyntaxException, I ArgumentCaptor captor = ArgumentCaptor.forClass(HttpUriRequest.class); verify(httpClient).execute(captor.capture()); HttpUriRequest request = captor.getValue(); - assertThat(request.getUri(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/count")))); - assertThat(request.getHeaders().length, is(0)); + assertThat(request.getUri(), + is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/count")))); assertThat(request, instanceOf(HttpPost.class)); HttpPost asPostRequest = (HttpPost) request; InputStreamReader reader = new InputStreamReader(asPostRequest.getEntity().getContent()); @@ -97,67 +126,74 @@ public void testImpressionCountsEndpointOptimized() throws URISyntaxException, I } @Test - public void testImpressionCountsEndpointDebug() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { - URI rootTarget = URI.create("https://kubernetesturl.com/split"); - - // Setup response mock - CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_OK); - - // Send counters - HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.DEBUG); - HashMap toSend = new HashMap<>(); - toSend.put(new ImpressionCounter.Key("test1", 0), 4); - toSend.put(new ImpressionCounter.Key("test2", 0), 5); - sender.postCounters(toSend); - - // Assert that the HTTP client was not called - verify(httpClient, Mockito.never()).execute(Mockito.any()); - } - - @Test - public void testImpressionBulksEndpoint() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void testImpressionBulksEndpoint() throws URISyntaxException, IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { URI rootTarget = URI.create("https://kubernetesturl.com/split"); // Setup response mock CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_OK); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); - HttpImpressionsSender sender = HttpImpressionsSender.create(httpClient, rootTarget, ImpressionsManager.Mode.OPTIMIZED); + HttpImpressionsSender sender = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.OPTIMIZED, TELEMETRY_STORAGE); // Send impressions List toSend = Arrays.asList(new TestImpressions("t1", Arrays.asList( - KeyImpression.fromImpression(new Impression("k1", null, "t1", "on", 123L, "r1", 456L, null)), - KeyImpression.fromImpression(new Impression("k2", null, "t1", "on", 123L, "r1", 456L, null)), - KeyImpression.fromImpression(new Impression("k3", null, "t1", "on", 123L, "r1", 456L, null)) - )), new TestImpressions("t2", Arrays.asList( - KeyImpression.fromImpression(new Impression("k1", null, "t2", "on", 123L, "r1", 456L, null)), - KeyImpression.fromImpression(new Impression("k2", null, "t2", "on", 123L, "r1", 456L, null)), - KeyImpression.fromImpression(new Impression("k3", null, "t2", "on", 123L, "r1", 456L, null)) - ))); + KeyImpression.fromImpression(new Impression("k1", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t1", "on", 123L, "r1", 456L, null, null)))), + new TestImpressions("t2", Arrays.asList( + KeyImpression.fromImpression(new Impression("k1", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t2", "on", 123L, "r1", 456L, null, null))))); sender.postImpressionsBulk(toSend); // Capture outgoing request and validate it ArgumentCaptor captor = ArgumentCaptor.forClass(HttpUriRequest.class); verify(httpClient).execute(captor.capture()); HttpUriRequest request = captor.getValue(); - assertThat(request.getUri(), is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk")))); - assertThat(request.getHeaders().length, is(1)); + assertThat(request.getUri(), + is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk")))); assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("OPTIMIZED"))); assertThat(request, instanceOf(HttpPost.class)); HttpPost asPostRequest = (HttpPost) request; InputStreamReader reader = new InputStreamReader(asPostRequest.getEntity().getContent()); Gson gson = new Gson(); - List payload = gson.fromJson(reader, new TypeToken>() { }.getType()); + List payload = gson.fromJson(reader, new TypeToken>() { + }.getType()); assertThat(payload.size(), is(equalTo(2))); // Do the same flow for imrpessionsMode = debug CloseableHttpClient httpClientDebugMode = TestHelper.mockHttpClient("", HttpStatus.SC_OK); + SplitHttpClient splitHtpClient2 = SplitHttpClientImpl.create(httpClientDebugMode, new RequestDecorator(null), + "qwerty", metadata()); - sender = HttpImpressionsSender.create(httpClientDebugMode, rootTarget, ImpressionsManager.Mode.DEBUG); + sender = HttpImpressionsSender.create(splitHtpClient2, rootTarget, ImpressionsManager.Mode.DEBUG, + TELEMETRY_STORAGE); sender.postImpressionsBulk(toSend); captor = ArgumentCaptor.forClass(HttpUriRequest.class); verify(httpClientDebugMode).execute(captor.capture()); request = captor.getValue(); - assertThat(request.getHeaders().length, is(1)); assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("DEBUG"))); } + + @Test + public void testHttpError() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException { + URI rootTarget = URI.create("https://kubernetesturl.com/split"); + CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_BAD_REQUEST); + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + HttpImpressionsSender sender = HttpImpressionsSender.create(splitHtpClient, rootTarget, + ImpressionsManager.Mode.OPTIMIZED, TELEMETRY_STORAGE); + // Should not raise exception + sender.postImpressionsBulk(new ArrayList<>()); + sender.postCounters(new HashMap<>()); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + } diff --git a/client/src/test/java/io/split/client/impressions/ImpressionCounterTest.java b/client/src/test/java/io/split/client/impressions/ImpressionCounterTest.java index 4d8737dd..b04c93eb 100644 --- a/client/src/test/java/io/split/client/impressions/ImpressionCounterTest.java +++ b/client/src/test/java/io/split/client/impressions/ImpressionCounterTest.java @@ -4,10 +4,15 @@ import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import static org.hamcrest.CoreMatchers.both; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.core.IsEqual.equalTo; public class ImpressionCounterTest { @@ -101,4 +106,83 @@ public void manyConcurrentCalls() throws InterruptedException { assertThat(counted.get(new ImpressionCounter.Key("feature1", ImpressionUtils.truncateTimeframe(nextHourTimestamp))), is(equalTo(iterations * 3))); assertThat(counted.get(new ImpressionCounter.Key("feature2", ImpressionUtils.truncateTimeframe(nextHourTimestamp))), is(equalTo(iterations * 3))); } + + @Test + public void manyConcurrentCallsWithConcurrentPops() throws InterruptedException { + final int iterations = 10000000; + final long timestamp = makeTimestamp(2020, 9, 2, 10, 10, 12); + final long nextHourTimestamp = makeTimestamp(2020, 9, 2, 11, 10, 12); + ImpressionCounter counter = new ImpressionCounter(); + Thread t1 = new Thread(() -> { + int times = iterations; + while (times-- > 0) { + counter.inc("feature1", timestamp, 1); + counter.inc("feature2", timestamp, 1); + counter.inc("feature1", nextHourTimestamp, 2); + counter.inc("feature2", nextHourTimestamp, 2); + } + }); + Thread t2 = new Thread(() -> { + int times = iterations; + while (times-- > 0) { + counter.inc("feature1", timestamp, 2); + counter.inc("feature2", timestamp, 2); + counter.inc("feature1", nextHourTimestamp, 1); + counter.inc("feature2", nextHourTimestamp, 1); + } + }); + + // Pushing to this list will be done from a single thread. And querying will be done from the main one + // after all other threads have ended. No need for extra sync logic. + List> pops = new ArrayList<>(); + Thread t3 = new Thread(() -> { + try { + for (int i=10; i > 0; --i){ + Thread.sleep(1); + pops.add(counter.popAll()); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + + t1.setDaemon(true); t2.setDaemon(true); t2.setDaemon(true); + t1.start(); t2.start(); t3.start(); + t1.join(); t2.join(); t3.join(); + + // --- No other thread is running at this point. + + // Do an extra pop in case there's still some data in the counter + pops.add(counter.popAll()); + + Long feature1TSCount = pops.stream() + .reduce(0L, + (accum, next) -> accum + next.getOrDefault(new ImpressionCounter.Key("feature1", ImpressionUtils.truncateTimeframe(timestamp)), 0), + (x, y) -> x + y); + + Long feature1NextTSCount = pops.stream() + .reduce(0L, + (accum, next) -> accum + next.getOrDefault(new ImpressionCounter.Key("feature1", ImpressionUtils.truncateTimeframe(nextHourTimestamp)), 0), + (x, y) -> x + y); + + Long feature2TSCount = pops.stream() + .reduce(0L, + (accum, next) -> accum + next.getOrDefault(new ImpressionCounter.Key("feature2", ImpressionUtils.truncateTimeframe(timestamp)), 0), + (x, y) -> x + y); + + Long feature2NextTSCount = pops.stream() + .reduce(0L, + (accum, next) -> accum + next.getOrDefault(new ImpressionCounter.Key("feature2", ImpressionUtils.truncateTimeframe(nextHourTimestamp)), 0), + (x, y) -> x + y); + + + // Using lockless/atomic structures for higher performance at the cost of 0.001% margin error accepted in very high concurrency + Long lowerBound = (long) ((iterations * 3) * 0.99999); + Long upperBound = (long) ((iterations * 3) * 1.00001); + + assertThat(feature1TSCount, is(both(greaterThan(lowerBound)).and(lessThan(upperBound)))); + assertThat(feature1NextTSCount, is(both(greaterThan(lowerBound)).and(lessThan(upperBound)))); + assertThat(feature2TSCount, is(both(greaterThan(lowerBound)).and(lessThan(upperBound)))); + assertThat(feature2NextTSCount, is(both(greaterThan(lowerBound)).and(lessThan(upperBound)))); + } } diff --git a/client/src/test/java/io/split/client/impressions/ImpressionHasherTest.java b/client/src/test/java/io/split/client/impressions/ImpressionHasherTest.java index 86214d77..031c1aa8 100644 --- a/client/src/test/java/io/split/client/impressions/ImpressionHasherTest.java +++ b/client/src/test/java/io/split/client/impressions/ImpressionHasherTest.java @@ -18,7 +18,7 @@ public void works() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); // Different feature Impression imp2 = new Impression("someKey", @@ -28,7 +28,7 @@ public void works() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); @@ -40,7 +40,7 @@ public void works() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); // different changeNumber @@ -51,7 +51,7 @@ public void works() { System.currentTimeMillis(), "someLabel", 456L, - null); + null, null); assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); // different label @@ -62,7 +62,7 @@ public void works() { System.currentTimeMillis(), "someOtherLabel", 123L, - null); + null, null); assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); // different treatment @@ -73,7 +73,7 @@ public void works() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); assertThat(ImpressionHasher.process(imp1), not(equalTo(ImpressionHasher.process(imp2)))); } @@ -87,7 +87,7 @@ public void doesNotCrash() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); assertNotNull(ImpressionHasher.process(imp1)); imp1 = new Impression(null, @@ -97,7 +97,7 @@ public void doesNotCrash() { System.currentTimeMillis(), "someLabel", 123L, - null); + null, null); assertNotNull(ImpressionHasher.process(imp1)); imp1 = new Impression(null, @@ -107,7 +107,7 @@ public void doesNotCrash() { System.currentTimeMillis(), "someLabel", null, - null); + null, null); assertNotNull(ImpressionHasher.process(imp1)); imp1 = new Impression(null, @@ -117,7 +117,7 @@ public void doesNotCrash() { System.currentTimeMillis(), null, null, - null); + null, null); assertNotNull(ImpressionHasher.process(imp1)); imp1 = new Impression(null, @@ -127,7 +127,7 @@ public void doesNotCrash() { System.currentTimeMillis(), "someLabel", null, - null); + null, null); assertNotNull(ImpressionHasher.process(imp1)); assertNull(ImpressionHasher.process(null)); } diff --git a/client/src/test/java/io/split/client/impressions/ImpressionObserverTest.java b/client/src/test/java/io/split/client/impressions/ImpressionObserverTest.java index 564ce9e3..57fc258a 100644 --- a/client/src/test/java/io/split/client/impressions/ImpressionObserverTest.java +++ b/client/src/test/java/io/split/client/impressions/ImpressionObserverTest.java @@ -1,6 +1,7 @@ package io.split.client.impressions; import com.google.common.base.Strings; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,7 @@ private List generateImpressions(long count) { System.currentTimeMillis(), (i % 2 == 0) ? "in segment all" : "whitelisted", i * i, + null, null); imps.add(imp); } @@ -53,6 +55,7 @@ public void testBasicFunctionality() { "on", System.currentTimeMillis(), "in segment all", 1234L, + null, null); // Add 5 new impressions so that the old one is evicted and re-try the test. @@ -74,10 +77,7 @@ public void testMemoryUsageStopsWhenCacheIsFull() throws Exception { getObjectSize = objectSizeCalculatorClass.getMethod("getObjectSize", Object.class); //getObjectSize(observer); } catch (ClassNotFoundException | NoSuchMethodException e) { _log.error("This test only runs with the hotspot JVM. It's ignored locally, but mandatory on CI"); - if (!Strings.isNullOrEmpty(System.getenv("CI"))) { // If the CI environment variable is present - throw new Exception("Setup CI to run with a hotspot JVM"); - } - // Otherwise just ignore this test. + // TODO: Fix this test for JDK > 8 return; } @@ -110,6 +110,7 @@ private void caller(ImpressionObserver o, int count, ConcurrentLinkedQueue imps = new ConcurrentLinkedQueue<>(); diff --git a/client/src/test/java/io/split/client/impressions/ImpressionTestUtils.java b/client/src/test/java/io/split/client/impressions/ImpressionTestUtils.java new file mode 100644 index 00000000..db161204 --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/ImpressionTestUtils.java @@ -0,0 +1,17 @@ +package io.split.client.impressions; + +import io.split.client.dtos.KeyImpression; + +public class ImpressionTestUtils { + + public static KeyImpression keyImpression(String feature, String key, String treatment, long time, Long changeNumber, String properties) { + KeyImpression result = new KeyImpression(); + result.feature = feature; + result.keyName = key; + result.treatment = treatment; + result.time = time; + result.changeNumber = changeNumber; + result.properties = properties; + return result; + } +} diff --git a/client/src/test/java/io/split/client/impressions/ImpressionsManagerImplTest.java b/client/src/test/java/io/split/client/impressions/ImpressionsManagerImplTest.java index 4e812254..6dbf5061 100644 --- a/client/src/test/java/io/split/client/impressions/ImpressionsManagerImplTest.java +++ b/client/src/test/java/io/split/client/impressions/ImpressionsManagerImplTest.java @@ -1,61 +1,95 @@ package io.split.client.impressions; import io.split.client.SplitClientConfig; +import io.split.client.dtos.DecoratedImpression; import io.split.client.dtos.KeyImpression; import io.split.client.dtos.TestImpressions; -import org.junit.Ignore; +import io.split.client.dtos.UniqueKeys; +import io.split.client.impressions.strategy.ProcessImpressionDebug; +import io.split.client.impressions.strategy.ProcessImpressionNone; +import io.split.client.impressions.strategy.ProcessImpressionOptimized; +import io.split.client.impressions.strategy.ProcessImpressionStrategy; +import io.split.storages.enums.OperationMode; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.storage.TelemetryStorageProducer; +import io.split.telemetry.synchronizer.TelemetryInMemorySubmitter; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import pluggable.CustomStorageWrapper; import java.net.URISyntaxException; -import java.util.AbstractMap; -import java.util.HashMap; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static io.split.client.impressions.ImpressionTestUtils.keyImpression; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.junit.Assert.assertEquals; /** * Created by patricioe on 6/20/16. */ @RunWith(MockitoJUnitRunner.class) public class ImpressionsManagerImplTest { + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + + @Before + public void setUp() { + TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + } @Captor private ArgumentCaptor> impressionsCaptor; + @Captor + private ArgumentCaptor> impressionKeyList; + + @Captor + private ArgumentCaptor uniqueKeysCaptor; + @Captor private ArgumentCaptor> impressionCountCaptor; @Test public void works() throws URISyntaxException { - SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(4) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.DEBUG) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); - KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null); - KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L); - KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 2L); - KeyImpression ki4 = keyImpression("test2", "pato", "on", 4L, 3L); + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 2L, null); + KeyImpression ki4 = keyImpression("test2", "pato", "on", 4L, 3L, null); - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, ki1.changeNumber, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, ki2.changeNumber, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, ki3.changeNumber, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, ki4.changeNumber, null)); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, ki1.changeNumber, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, ki2.changeNumber, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, ki3.changeNumber, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, ki4.changeNumber, null, null), false)).collect(Collectors.toList())); // Do what the scheduler would do. treatmentLog.sendImpressions(); @@ -64,32 +98,168 @@ public void works() throws URISyntaxException { List captured = impressionsCaptor.getValue(); - assertThat(captured.size(), is(equalTo(2))); + Assert.assertEquals(2, captured.size()); + } + + @Test + public void testImpressionListenerOptimize() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .build(); + ImpressionsStorage storage = Mockito.mock(InMemoryImpressionsStorage.class); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(true, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionListener impressionListener = Mockito.mock(AsynchronousImpressionListener.class); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, impressionListener); + treatmentLog.start(); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 2L, null); + KeyImpression ki4 = keyImpression("test2", "pato", "on", 4L, 3L, null); + + List impressionList = new ArrayList<>(); + impressionList.add(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, ki1.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, ki2.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, ki3.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, ki4.changeNumber, null, null), false)); + + treatmentLog.track(impressionList); + verify(impressionListener, times(4)).log(Mockito.anyObject()); + + verify(storage).put(impressionKeyList.capture()); + + List captured = impressionKeyList.getValue(); + + Assert.assertEquals(3, captured.size()); } @Test - public void worksButDropsImpressions() throws URISyntaxException { + public void testImpressionListenerDebug() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(6) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + ImpressionsStorage storage = Mockito.mock(InMemoryImpressionsStorage.class); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(true, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionListener impressionListener = Mockito.mock(AsynchronousImpressionListener.class); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, impressionListener); + treatmentLog.start(); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 2L, null); + KeyImpression ki4 = keyImpression("test2", "pato", "on", 4L, 3L, null); + + List impressionList = new ArrayList<>(); + impressionList.add(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, ki1.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, ki2.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, ki3.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, ki4.changeNumber, null, null), false)); + + treatmentLog.track(impressionList); + verify(impressionListener, times(4)).log(Mockito.anyObject()); + + verify(storage).put(impressionKeyList.capture()); + + List captured = impressionKeyList.getValue(); + + Assert.assertEquals(4, captured.size()); + } + + @Test + public void testImpressionListenerNone() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .build(); + ImpressionsStorage storage = Mockito.mock(InMemoryImpressionsStorage.class); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionNone(true, uniqueKeysTracker, impressionCounter); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionListener impressionListener = Mockito.mock(AsynchronousImpressionListener.class); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, impressionListener); + treatmentLog.start(); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 2L, null); + KeyImpression ki4 = keyImpression("test2", "pato", "on", 4L, 3L, null); + + List impressionList = new ArrayList<>(); + impressionList.add(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, ki1.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, ki2.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, ki3.changeNumber, null, null), false)); + impressionList.add(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, ki4.changeNumber, null, null), false)); + + treatmentLog.track(impressionList); + verify(impressionListener, times(4)).log(Mockito.anyObject()); + + verify(storage).put(impressionKeyList.capture()); + + List captured = impressionKeyList.getValue(); + + Assert.assertEquals(0, captured.size()); + } + + @Test + public void worksButDropsImpressions() { SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(3) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.DEBUG) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); // These 4 unique test name will cause 4 entries but we are caping at the first 3. - KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null); - KeyImpression ki2 = keyImpression("test2", "adil", "on", 2L, null); - KeyImpression ki3 = keyImpression("test3", "pato", "on", 3L, null); - KeyImpression ki4 = keyImpression("test4", "pato", "on", 4L, null); + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 2L, null, null); + KeyImpression ki3 = keyImpression("test3", "pato", "on", 3L, null, null); + KeyImpression ki4 = keyImpression("test4", "pato", "on", 4L, null, null); - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, null, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, null, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, null, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, null, null)); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, null, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, null, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, null, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, null, null, null), false)).collect(Collectors.toList())); // Do what the scheduler would do. treatmentLog.sendImpressions(); @@ -98,32 +268,40 @@ public void worksButDropsImpressions() throws URISyntaxException { List captured = impressionsCaptor.getValue(); - assertThat(captured.size(), is(equalTo(3))); + Assert.assertEquals(3, captured.size()); + verify(TELEMETRY_STORAGE, times(1)).recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 1); } @Test - public void works4ImpressionsInOneTest() throws URISyntaxException { + public void works4ImpressionsInOneTest() { SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(10) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.DEBUG) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); // These 4 unique test name will cause 4 entries but we are caping at the first 3. - KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L); - KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L); - KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L); - KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L); + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null)); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); // Do what the scheduler would do. treatmentLog.sendImpressions(); @@ -132,22 +310,30 @@ public void works4ImpressionsInOneTest() throws URISyntaxException { List captured = impressionsCaptor.getValue(); - assertThat(captured.size(), is(equalTo(1))); - assertThat(captured.get(0).keyImpressions.size(), is(equalTo(4))); - assertThat(captured.get(0).keyImpressions.get(0), is(equalTo(ki1))); + Assert.assertEquals(1, captured.size()); + Assert.assertEquals(4, captured.get(0).keyImpressions.size()); + Assert.assertEquals(ki1, captured.get(0).keyImpressions.get(0)); + verify(TELEMETRY_STORAGE, times(4)).recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_QUEUED, 1); } @Test - public void worksNoImpressions() throws URISyntaxException { + public void worksNoImpressions() { SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(10) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.DEBUG) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); // There are no impressions to post. @@ -158,28 +344,33 @@ public void worksNoImpressions() throws URISyntaxException { } @Test - @Ignore // TODO: This test needs to be updated - public void alreadySeenImpressionsAreMarked() throws URISyntaxException { + public void alreadySeenImpressionsAreMarked() { SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(10) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.DEBUG) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); // These 4 unique test name will cause 4 entries but we are caping at the first 3. - KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L); - KeyImpression ki2 = keyImpression("test1", "adil2", "on", 2L, 1L); - KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L); - KeyImpression ki4 = keyImpression("test1", "pato2", "on", 4L, 1L); - - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null)); + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil2", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato2", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); treatmentLog.sendImpressions(); verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); @@ -187,87 +378,760 @@ public void alreadySeenImpressionsAreMarked() throws URISyntaxException { List captured = impressionsCaptor.getValue(); for (TestImpressions testImpressions : captured) { for (KeyImpression keyImpression : testImpressions.keyImpressions) { - assertThat(keyImpression.previousTime, is(equalTo(null))); + Assert.assertEquals(null, keyImpression.previousTime); } } // Do it again. Now they should all have a `seenAt` value Mockito.reset(senderMock); - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null)); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); treatmentLog.sendImpressions(); verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); - captured = impressionsCaptor.getValue(); + captured = impressionsCaptor.getAllValues().get(1); for (TestImpressions testImpressions : captured) { for (KeyImpression keyImpression : testImpressions.keyImpressions) { - assertThat(keyImpression.previousTime, is(equalTo(keyImpression.time))); + assertEquals(Optional.of(keyImpression.previousTime), Optional.of(keyImpression.time)); } } } - private KeyImpression keyImpression(String feature, String key, String treatment, long time, Long changeNumber) { - KeyImpression result = new KeyImpression(); - result.feature = feature; - result.keyName = key; - result.treatment = treatment; - result.time = time; - result.changeNumber = changeNumber; - return result; + @Test + public void testImpressionsStandaloneModeOptimizedMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(false, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(2, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + for (KeyImpression keyImpression : testImpressions.keyImpressions) { + Assert.assertEquals(null, keyImpression.previousTime); + } + } + // Only the first 2 impressions make it to the server + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + + treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 2))); + + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsStandaloneModeDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(4, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + KeyImpression keyImpression1 = testImpressions.keyImpressions.get(0); + KeyImpression keyImpression2 = testImpressions.keyImpressions.get(1); + KeyImpression keyImpression3 = testImpressions.keyImpressions.get(2); + KeyImpression keyImpression4 = testImpressions.keyImpressions.get(3); + Assert.assertEquals(null, keyImpression1.previousTime); + Assert.assertEquals(Optional.of(1L), Optional.of(keyImpression2.previousTime)); + Assert.assertEquals(null, keyImpression3.previousTime); + Assert.assertEquals(Optional.of(3L), Optional.of(keyImpression4.previousTime)); + } + // Only the first 2 impressions make it to the server + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + } + + @Test + public void testImpressionsStandaloneModeNoneMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionNone(false, uniqueKeysTracker, impressionCounter); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.close(); + uniqueKeysTracker.stop(); + + verify(telemetrySynchronizer).synchronizeUniqueKeys(uniqueKeysCaptor.capture()); + + List uniqueKeysList = uniqueKeysCaptor.getAllValues(); + UniqueKeys uniqueKeys = uniqueKeysList.get(0); + UniqueKeys.UniqueKey uniqueKey = uniqueKeys.uniqueKeys.get(0); + Assert.assertEquals("test1", uniqueKey.featureName); + + List keysDto = uniqueKey.keysDto; + Assert.assertEquals("pato", keysDto.get(0)); + Assert.assertEquals("adil", keysDto.get(1)); + + //treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 4))); + + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsConsumerModeOptimizedMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(false, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(2, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + for (KeyImpression keyImpression : testImpressions.keyImpressions) { + Assert.assertEquals(null, keyImpression.previousTime); + } + } + // Only the first 2 impressions make it to the server + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + + treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 2))); + + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsConsumerModeNoneMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionNone(false, uniqueKeysTracker, impressionCounter); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + uniqueKeysTracker.stop(); + treatmentLog.close(); + + verify(telemetrySynchronizer).synchronizeUniqueKeys(uniqueKeysCaptor.capture()); + + List uniqueKeysList = uniqueKeysCaptor.getAllValues(); + UniqueKeys uniqueKeys = uniqueKeysList.get(0); + UniqueKeys.UniqueKey uniqueKey = uniqueKeys.uniqueKeys.get(0); + Assert.assertEquals("test1", uniqueKey.featureName); + + List keysDto = uniqueKey.keysDto; + Assert.assertEquals("pato", keysDto.get(0)); + Assert.assertEquals("adil", keysDto.get(1)); + + //treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 4))); + + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsConsumerModeDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(4, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + KeyImpression keyImpression1 = testImpressions.keyImpressions.get(0); + KeyImpression keyImpression2 = testImpressions.keyImpressions.get(1); + KeyImpression keyImpression3 = testImpressions.keyImpressions.get(2); + KeyImpression keyImpression4 = testImpressions.keyImpressions.get(3); + Assert.assertEquals(null, keyImpression1.previousTime); + Assert.assertEquals(Optional.of(1L), Optional.of(keyImpression2.previousTime)); + Assert.assertEquals(null, keyImpression3.previousTime); + Assert.assertEquals(Optional.of(3L), Optional.of(keyImpression4.previousTime)); + } + // Only the first 2 impressions make it to the server + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + } + + @Test + public void testCounterStandaloneModeOptimizedMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(false, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + manager.start(); + Assert.assertNotNull(manager.getCounter()); + } + @Test + public void testCounterStandaloneModeDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, null, null); + manager.start(); + Assert.assertNull(manager.getCounter()); } @Test - public void testImpressionsOptimizedMode() throws URISyntaxException { + public void testCounterStandaloneModeNoneMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ProcessImpressionStrategy processImpressionStrategy = Mockito.mock(ProcessImpressionNone.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, Mockito.mock(ProcessImpressionNone.class), processImpressionStrategy, impressionCounter, null); + manager.start(); + Assert.assertNotNull(manager.getCounter()); + } + + @Test + public void testCounterConsumerModeOptimizedMode() { SplitClientConfig config = SplitClientConfig.builder() .impressionsQueueSize(10) .endpoint("nowhere.com", "nowhere.com") .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ProcessImpressionStrategy processImpressionStrategy = Mockito.mock(ProcessImpressionOptimized.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); - ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(null, config, senderMock, null); + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, Mockito.mock(ProcessImpressionNone.class), processImpressionStrategy, impressionCounter, null); + manager.start(); + Assert.assertNotNull(manager.getCounter()); + } + + @Test + public void testCounterConsumerModeDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ProcessImpressionStrategy processImpressionStrategy = Mockito.mock(ProcessImpressionDebug.class); + + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, Mockito.mock(ProcessImpressionNone.class), processImpressionStrategy, null, null); + manager.start(); + Assert.assertNull(manager.getCounter()); + } + + @Test + public void testCounterConsumerModeNoneMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ProcessImpressionStrategy processImpressionStrategy = Mockito.mock(ProcessImpressionNone.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + + ImpressionsManagerImpl manager = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, Mockito.mock(ProcessImpressionNone.class), processImpressionStrategy, impressionCounter, null); + manager.start(); + Assert.assertNotNull(manager.getCounter()); + } + + @Test + public void testImpressionToggleStandaloneOptimizedMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(false, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, uniqueKeysTracker, impressionCounter); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); // These 4 unique test name will cause 4 entries but we are caping at the first 3. - KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L); - KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L); - KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L); - KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L); - - treatmentLog.track(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null)); - treatmentLog.track(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null)); - treatmentLog.track(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null)); - treatmentLog.track(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null)); + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "mati", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "bilal", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), true)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), true)).collect(Collectors.toList())); treatmentLog.sendImpressions(); verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); List captured = impressionsCaptor.getValue(); - assertThat(captured.get(0).keyImpressions.size(), is(equalTo(2))); + Assert.assertEquals(2, captured.get(0).keyImpressions.size()); for (TestImpressions testImpressions : captured) { for (KeyImpression keyImpression : testImpressions.keyImpressions) { - assertThat(keyImpression.previousTime, is(equalTo(null))); + Assert.assertEquals(null, keyImpression.previousTime); } } // Only the first 2 impressions make it to the server - assertThat(captured.get(0).keyImpressions, - contains(keyImpression("test1", "adil", "on", 1L, 1L), - keyImpression("test1", "pato", "on", 3L, 1L))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + + HashMap> trackedKeys = ((UniqueKeysTrackerImp) uniqueKeysTracker).popAll(); + HashSet keys = new HashSet<>(); + keys.add("mati"); + keys.add("bilal"); + Assert.assertEquals(1, trackedKeys.size()); + Assert.assertEquals(keys, trackedKeys.get("test1")); treatmentLog.sendImpressionCounters(); verify(senderMock).postCounters(impressionCountCaptor.capture()); HashMap capturedCounts = impressionCountCaptor.getValue(); - assertThat(capturedCounts.size(), is(equalTo(1))); - assertThat(capturedCounts.entrySet(), - contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 4))); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 2))); + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionToggleStandaloneModeDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, uniqueKeysTracker, impressionCounter); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "mati", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "bilal", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), true)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), true)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + HashMap> trackedKeys = ((UniqueKeysTrackerImp) uniqueKeysTracker).popAll(); + HashSet keys = new HashSet<>(); + keys.add("mati"); + keys.add("bilal"); + Assert.assertEquals(1, trackedKeys.size()); + Assert.assertEquals(keys, trackedKeys.get("test1")); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(2, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + KeyImpression keyImpression1 = testImpressions.keyImpressions.get(0); + KeyImpression keyImpression3 = testImpressions.keyImpressions.get(1); + Assert.assertEquals(null, keyImpression1.previousTime); + Assert.assertEquals(null, keyImpression3.previousTime); + } + // Only the first 2 impressions make it to the server + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + } + + @Test + public void testImpressionToggleStandaloneModeNoneMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.NONE) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + UniqueKeysTracker uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 1000, 1000, null); + uniqueKeysTracker.start(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionNone(false, uniqueKeysTracker, impressionCounter); + ProcessImpressionNone processImpressionNone = (ProcessImpressionNone) processImpressionStrategy; + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, null); + KeyImpression ki2 = keyImpression("test1", "mati", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "bilal", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), true)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), true)).collect(Collectors.toList())); + treatmentLog.close(); + HashMap> trackedKeys = ((UniqueKeysTrackerImp) uniqueKeysTracker).popAll(); + uniqueKeysTracker.stop(); + + HashSet keys = new HashSet<>(); + keys.add("adil"); + keys.add("mati"); + keys.add("pato"); + keys.add("bilal"); + Assert.assertEquals(1, trackedKeys.size()); + Assert.assertEquals(keys, trackedKeys.get("test1")); + + //treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 4))); + + // Assert that the sender is never called if the counters are empty. + Mockito.reset(senderMock); + treatmentLog.sendImpressionCounters(); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsPropertiesOptimizedMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.OPTIMIZED) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = new ImpressionCounter(); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + TelemetryStorageProducer telemetryStorageProducer = new InMemoryTelemetryStorage(); + + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionOptimized(false, impressionObserver, impressionCounter, telemetryStorageProducer); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, "{\"prop\":\"val\"}"); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, ki1.properties), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(3, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + for (KeyImpression keyImpression : testImpressions.keyImpressions) { + Assert.assertEquals(null, keyImpression.previousTime); + } + } + // impression with properties is not deduped + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, "{\"prop\":\"val\"}"))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 2L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); + + treatmentLog.sendImpressionCounters(); + verify(senderMock).postCounters(impressionCountCaptor.capture()); + HashMap capturedCounts = impressionCountCaptor.getValue(); + Assert.assertEquals(1, capturedCounts.size()); + Assert.assertTrue(capturedCounts.entrySet().contains(new AbstractMap.SimpleEntry<>(new ImpressionCounter.Key("test1", 0), 1))); // Assert that the sender is never called if the counters are empty. Mockito.reset(senderMock); treatmentLog.sendImpressionCounters(); - verify(senderMock, Mockito.times(0)).postCounters(Mockito.any()); + verify(senderMock, times(0)).postCounters(Mockito.any()); + } + + @Test + public void testImpressionsPropertiesDebugMode() { + SplitClientConfig config = SplitClientConfig.builder() + .impressionsQueueSize(10) + .endpoint("nowhere.com", "nowhere.com") + .impressionsMode(ImpressionsManager.Mode.DEBUG) + .operationMode(OperationMode.CONSUMER) + .customStorageWrapper(Mockito.mock(CustomStorageWrapper.class)) + .build(); + ImpressionsStorage storage = new InMemoryImpressionsStorage(config.impressionsQueueSize()); + + ImpressionsSender senderMock = Mockito.mock(ImpressionsSender.class); + ImpressionCounter impressionCounter = Mockito.mock(ImpressionCounter.class); + ImpressionObserver impressionObserver = new ImpressionObserver(200); + ProcessImpressionStrategy processImpressionStrategy = new ProcessImpressionDebug(false, impressionObserver); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(false, null, null); + + ImpressionsManagerImpl treatmentLog = ImpressionsManagerImpl.instanceForTest(config, senderMock, TELEMETRY_STORAGE, storage, storage, processImpressionNone, processImpressionStrategy, impressionCounter, null); + treatmentLog.start(); + + // These 4 unique test name will cause 4 entries but we are caping at the first 3. + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, 1L, "{\"prop\":\"val\"}"); + KeyImpression ki2 = keyImpression("test1", "adil", "on", 2L, 1L, null); + KeyImpression ki3 = keyImpression("test1", "pato", "on", 3L, 1L, null); + KeyImpression ki4 = keyImpression("test1", "pato", "on", 4L, 1L, null); + + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, "{\"prop\":\"val\"}"), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.track(Stream.of(new DecoratedImpression(new Impression(ki4.keyName, null, ki4.feature, ki4.treatment, ki4.time, null, 1L, null, null), false)).collect(Collectors.toList())); + treatmentLog.sendImpressions(); + + verify(senderMock).postImpressionsBulk(impressionsCaptor.capture()); + + List captured = impressionsCaptor.getValue(); + Assert.assertEquals(4, captured.get(0).keyImpressions.size()); + for (TestImpressions testImpressions : captured) { + KeyImpression keyImpression1 = testImpressions.keyImpressions.get(0); + KeyImpression keyImpression2 = testImpressions.keyImpressions.get(1); + KeyImpression keyImpression3 = testImpressions.keyImpressions.get(2); + KeyImpression keyImpression4 = testImpressions.keyImpressions.get(3); + Assert.assertEquals(null, keyImpression1.previousTime); + Assert.assertEquals(null, keyImpression2.previousTime); + Assert.assertEquals(null, keyImpression3.previousTime); + Assert.assertEquals(Optional.of(3L), Optional.of(keyImpression4.previousTime)); + } + // impression with properties is not deduped + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, "{\"prop\":\"val\"}"))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "adil", "on", 1L, 1L, null))); + Assert.assertTrue(captured.get(0).keyImpressions.contains(keyImpression("test1", "pato", "on", 3L, 1L, null))); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/InMemoryImpressionsStorageTest.java b/client/src/test/java/io/split/client/impressions/InMemoryImpressionsStorageTest.java index 41692cda..f86cd3f0 100644 --- a/client/src/test/java/io/split/client/impressions/InMemoryImpressionsStorageTest.java +++ b/client/src/test/java/io/split/client/impressions/InMemoryImpressionsStorageTest.java @@ -4,6 +4,8 @@ import org.junit.Test; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; @@ -16,9 +18,9 @@ public void testBasicUsage() { InMemoryImpressionsStorage storage = new InMemoryImpressionsStorage(10); for (int i = 0; i < 15; i++) { if (i < 10) { - assertThat(storage.put(new KeyImpression()), is(true)); + assertThat(storage.put(Stream.of(new KeyImpression()).collect(Collectors.toList())), is(1L)); } else { - assertThat(storage.put(new KeyImpression()), is(false)); + assertThat(storage.put(Stream.of(new KeyImpression()).collect(Collectors.toList())), is(0L)); } } diff --git a/client/src/test/java/io/split/client/impressions/PluggableImpressionSenderTest.java b/client/src/test/java/io/split/client/impressions/PluggableImpressionSenderTest.java new file mode 100644 index 00000000..a7be40cb --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/PluggableImpressionSenderTest.java @@ -0,0 +1,44 @@ +package io.split.client.impressions; + +import io.split.storages.pluggable.CustomStorageWrapperHasPipeline; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; +import pluggable.HasPipelineSupport; + +import java.util.HashMap; +import java.util.Optional; +import java.util.concurrent.ConcurrentMap; + +public class PluggableImpressionSenderTest { + + @Test + public void testPostCounters() throws Exception { + CustomStorageWrapper customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + PluggableImpressionSender redisImpressionSender = PluggableImpressionSender.create(customStorageWrapper); + + HashMap counters = new HashMap<>(); + ImpressionCounter.Key counterKey1 = new ImpressionCounter.Key("feature1", 100); + counters.put(counterKey1, 2); + redisImpressionSender.postCounters(counters); + Mockito.verify(customStorageWrapper, Mockito.times(1)).hIncrement(Mockito.eq("SPLITIO.impressions.count"), Mockito.eq("feature1::100"), Mockito.eq(2L)); + } + + @Test + public void testPostCountersHasPipeline() throws Exception { + CustomStorageWrapperHasPipeline customStorageWrapper = new CustomStorageWrapperHasPipeline(); + PluggableImpressionSender redisImpressionSender = PluggableImpressionSender.create(customStorageWrapper); + + HashMap counters = new HashMap<>(); + ImpressionCounter.Key counterKey1 = new ImpressionCounter.Key("feature1", 100); + counters.put(counterKey1, 2); + redisImpressionSender.postCounters(counters); + + Assert.assertTrue(customStorageWrapper instanceof HasPipelineSupport); + ConcurrentMap impressionsCount = customStorageWrapper.getImpressionsCount(); + Assert.assertTrue(impressionsCount.containsKey("feature1::100")); + String key = "feature1::100"; + Assert.assertEquals(Optional.of(2L), Optional.of(impressionsCount.get(key))); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/UniqueKeysTrackerImpTest.java b/client/src/test/java/io/split/client/impressions/UniqueKeysTrackerImpTest.java new file mode 100644 index 00000000..e758369e --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/UniqueKeysTrackerImpTest.java @@ -0,0 +1,178 @@ +package io.split.client.impressions; + +import io.split.client.dtos.UniqueKeys; +import io.split.telemetry.synchronizer.TelemetryInMemorySubmitter; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicInteger; + +public class UniqueKeysTrackerImpTest { + private static TelemetrySynchronizer _telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + + @Test + public void addSomeElements(){ + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(_telemetrySynchronizer, 10000, 10000, null); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key3")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2","key4")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2","key5")); + + HashMap> result = uniqueKeysTrackerImp.popAll(); + Assert.assertEquals(2,result.size()); + + HashSet value1 = result.get("feature1"); + Assert.assertEquals(3,value1.size()); + Assert.assertTrue(value1.contains("key1")); + Assert.assertTrue(value1.contains("key2")); + Assert.assertTrue(value1.contains("key3")); + + HashSet value2 = result.get("feature2"); + Assert.assertEquals(2,value2.size()); + Assert.assertTrue(value2.contains("key4")); + Assert.assertTrue(value2.contains("key5")); + } + + @Test + public void addTheSameElements(){ + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(_telemetrySynchronizer, 10000, 10000, null); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key3")); + + Assert.assertFalse(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertFalse(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertFalse(uniqueKeysTrackerImp.track("feature1","key3")); + + HashMap> result = uniqueKeysTrackerImp.popAll(); + Assert.assertEquals(1,result.size()); + + HashSet value1 = result.get("feature1"); + Assert.assertEquals(3,value1.size()); + Assert.assertTrue(value1.contains("key1")); + Assert.assertTrue(value1.contains("key2")); + Assert.assertTrue(value1.contains("key3")); + } + + @Test + public void popAllUniqueKeys(){ + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(_telemetrySynchronizer, 10000, 10000, null); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2","key3")); + + HashMap> result = uniqueKeysTrackerImp.popAll(); + Assert.assertEquals(2,result.size()); + HashMap> resultAfterPopAll = uniqueKeysTrackerImp.popAll(); + Assert.assertEquals(0,resultAfterPopAll.size()); + } + + @Test + public void testSynchronization() throws Exception { + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(telemetrySynchronizer, 1, 3, null); + uniqueKeysTrackerImp.start(); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2","key3")); + + Thread.sleep(2900); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeUniqueKeys(Mockito.anyObject()); + Thread.sleep(2900); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeUniqueKeys(Mockito.anyObject()); + } + + @Test + public void testStopSynchronization() throws Exception { + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(telemetrySynchronizer, 1, 2, null); + uniqueKeysTrackerImp.start(); + Assert.assertFalse(uniqueKeysTrackerImp.getSendGuard().get()); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key1")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1","key2")); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2","key3")); + + Thread.sleep(2100); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeUniqueKeys(Mockito.anyObject()); + uniqueKeysTrackerImp.stop(); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).synchronizeUniqueKeys(Mockito.anyObject()); + } + + @Test + public void testUniqueKeysChunks() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(_telemetrySynchronizer, 10000, 10000, null); + HashMap> uniqueKeysHashMap = new HashMap<>(); + HashSet feature1 = new HashSet<>(); + HashSet feature2 = new HashSet<>(); + HashSet feature3 = new HashSet<>(); + HashSet feature4 = new HashSet<>(); + HashSet feature5 = new HashSet<>(); + for (Integer i=1; i<6000; i++) { + if (i <= 1000) { + feature1.add("key" + i); + } + if (i <= 2000) { + feature2.add("key" + i); + } + if (i <= 3000) { + feature3.add("key" + i); + } + if (i <= 4000) { + feature4.add("key" + i); + } + feature5.add("key" + i); + } + uniqueKeysHashMap.put("feature1", feature1); + uniqueKeysHashMap.put("feature2", feature2); + uniqueKeysHashMap.put("feature3", feature3); + uniqueKeysHashMap.put("feature4", feature4); + uniqueKeysHashMap.put("feature5", feature5); + + List uniqueKeysFromPopAll = new ArrayList<>(); + for (Map.Entry> uniqueKeyEntry : uniqueKeysHashMap.entrySet()) { + UniqueKeys.UniqueKey uniqueKey = new UniqueKeys.UniqueKey(uniqueKeyEntry.getKey(), new ArrayList<>(uniqueKeyEntry.getValue())); + uniqueKeysFromPopAll.add(uniqueKey); + } + Method methodCapChunks = uniqueKeysTrackerImp.getClass().getDeclaredMethod("capChunksToMaxSize", List.class); + methodCapChunks.setAccessible(true); + uniqueKeysFromPopAll = (List)methodCapChunks.invoke(uniqueKeysTrackerImp, uniqueKeysFromPopAll); + + Method methodGetChunks = uniqueKeysTrackerImp.getClass().getDeclaredMethod("getChunks", List.class); + methodGetChunks.setAccessible(true); + List> keysChunks = (List>) methodGetChunks.invoke(uniqueKeysTrackerImp, uniqueKeysFromPopAll); + for (List chunk : keysChunks) { + int chunkSize = 0; + for (UniqueKeys.UniqueKey keys : chunk) { + chunkSize += keys.keysDto.size(); + } + Assert.assertTrue(chunkSize <= 5000); + } + } + + @Test + public void testTrackReachMaxKeys() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTrackerImp uniqueKeysTrackerImp = new UniqueKeysTrackerImp(telemetrySynchronizer, 10000, 10000, null); + for (int i=1; i<6000; i++) { + Assert.assertTrue(uniqueKeysTrackerImp.track("feature1", "key" + i)); + Assert.assertTrue(uniqueKeysTrackerImp.track("feature2", "key" + i)); + } + Mockito.verify(telemetrySynchronizer, Mockito.times(2)).synchronizeUniqueKeys(Mockito.anyObject()); + + Field getTrackerSize = uniqueKeysTrackerImp.getClass().getDeclaredField("trackerKeysSize"); + getTrackerSize.setAccessible(true); + AtomicInteger trackerSize = (AtomicInteger) getTrackerSize.get(uniqueKeysTrackerImp); + Assert.assertTrue(trackerSize.intValue() == 1998); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionDebugTest.java b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionDebugTest.java new file mode 100644 index 00000000..68be97c5 --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionDebugTest.java @@ -0,0 +1,70 @@ +package io.split.client.impressions.strategy; + +import static io.split.client.impressions.ImpressionTestUtils.keyImpression; + +import io.split.client.dtos.KeyImpression; +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionObserver; +import io.split.client.impressions.ImpressionsResult; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +public class ProcessImpressionDebugTest { + + private static final long LAST_SEEN_CACHE_SIZE = 500000; + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + + @Test + public void processImpressionsWithListener(){ + boolean listenerEnable = true; + ImpressionObserver impressionObserver = new ImpressionObserver(LAST_SEEN_CACHE_SIZE); + ProcessImpressionDebug processImpressionDebug = new ProcessImpressionDebug(listenerEnable, impressionObserver); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionDebug.process(impressions); + + long pt3 = impressionsResult1.getImpressionsToQueue().get(2).pt(); + Assert.assertEquals(1, pt3); + + Assert.assertEquals(3,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertEquals(3,impressionsResult1.getImpressionsToListener().size()); + } + + @Test + public void processImpressionsWithoutListener(){ + boolean listenerEnable = false; + ImpressionObserver impressionObserver = new ImpressionObserver(LAST_SEEN_CACHE_SIZE); + ProcessImpressionDebug processImpressionDebug = new ProcessImpressionDebug(listenerEnable, impressionObserver); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionDebug.process(impressions); + + long pt3 = impressionsResult1.getImpressionsToQueue().get(2).pt(); + Assert.assertEquals(1, pt3); + + Assert.assertEquals(3,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertNull(impressionsResult1.getImpressionsToListener()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionNoneTest.java b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionNoneTest.java new file mode 100644 index 00000000..1debedd1 --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionNoneTest.java @@ -0,0 +1,73 @@ +package io.split.client.impressions.strategy; + +import io.split.client.dtos.KeyImpression; +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionsResult; +import io.split.client.impressions.UniqueKeysTrackerImp; +import io.split.client.impressions.ImpressionCounter; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.synchronizer.TelemetryInMemorySubmitter; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.*; + +import static io.split.client.impressions.ImpressionTestUtils.keyImpression; + +public class ProcessImpressionNoneTest { + + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + + @Test + public void processImpressionsWithListener(){ + boolean listenerEnable = true; + ImpressionCounter counter = new ImpressionCounter(); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTrackerImp uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 10000, 10000, null); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(listenerEnable, uniqueKeysTracker, counter); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionNone.process(impressions); + Assert.assertEquals(0,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertEquals(3,impressionsResult1.getImpressionsToListener().size()); + Assert.assertEquals(2, uniqueKeysTracker.popAll().size()); + + HashMap counters = counter.popAll(); + Assert.assertEquals(2, counters.size()); + } + + @Test + public void processImpressionsWithoutListener(){ + boolean listenerEnable = false; + ImpressionCounter counter = new ImpressionCounter(); + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + UniqueKeysTrackerImp uniqueKeysTracker = new UniqueKeysTrackerImp(telemetrySynchronizer, 10000, 10000, null); + ProcessImpressionNone processImpressionNone = new ProcessImpressionNone(listenerEnable, uniqueKeysTracker, counter); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionNone.process(impressions); + Assert.assertEquals(0,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertNull(impressionsResult1.getImpressionsToListener()); + Assert.assertEquals(2, uniqueKeysTracker.popAll().size()); + Assert.assertEquals(2, counter.popAll().size()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionOptimizedTest.java b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionOptimizedTest.java new file mode 100644 index 00000000..f7cecebe --- /dev/null +++ b/client/src/test/java/io/split/client/impressions/strategy/ProcessImpressionOptimizedTest.java @@ -0,0 +1,69 @@ +package io.split.client.impressions.strategy; + +import static io.split.client.impressions.ImpressionTestUtils.keyImpression; + +import io.split.client.dtos.KeyImpression; + +import io.split.client.impressions.Impression; +import io.split.client.impressions.ImpressionObserver; +import io.split.client.impressions.ImpressionsResult; +import io.split.client.impressions.ImpressionCounter; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +public class ProcessImpressionOptimizedTest { + + private static final long LAST_SEEN_CACHE_SIZE = 500000; + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + + @Test + public void processImpressionsWithListener(){ + boolean listenerEnable = true; + ImpressionObserver impressionObserver = new ImpressionObserver(LAST_SEEN_CACHE_SIZE); + ImpressionCounter counter = new ImpressionCounter(); + ProcessImpressionOptimized processImpressionOptimized = new ProcessImpressionOptimized(listenerEnable, impressionObserver, counter, TELEMETRY_STORAGE); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionOptimized.process(impressions); + + Assert.assertEquals(2,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertEquals(3,impressionsResult1.getImpressionsToListener().size()); + Assert.assertEquals(1, counter.popAll().size()); + } + + @Test + public void processImpressionsWithoutListener(){ + boolean listenerEnable = false; + ImpressionObserver impressionObserver = new ImpressionObserver(LAST_SEEN_CACHE_SIZE); + ImpressionCounter counter = new ImpressionCounter(); + ProcessImpressionOptimized processImpressionOptimized = new ProcessImpressionOptimized(listenerEnable, impressionObserver, counter, TELEMETRY_STORAGE); + + KeyImpression ki1 = keyImpression("test1", "adil", "on", 1L, null, null); + KeyImpression ki2 = keyImpression("test2", "adil", "on", 1L, null, null); + KeyImpression ki3 = keyImpression("test1", "adil", "on", 1L, null, null); + + List impressions = new ArrayList<>(); + impressions.add(new Impression(ki1.keyName, null, ki1.feature, ki1.treatment, ki1.time, null, 1L, null, null)); + impressions.add(new Impression(ki2.keyName, null, ki2.feature, ki2.treatment, ki2.time, null, 1L, null, null)); + impressions.add(new Impression(ki3.keyName, null, ki3.feature, ki3.treatment, ki3.time, null, 1L, null, null)); + + ImpressionsResult impressionsResult1 = processImpressionOptimized.process(impressions); + Assert.assertEquals(2,impressionsResult1.getImpressionsToQueue().size()); + Assert.assertNull(impressionsResult1.getImpressionsToListener()); + Assert.assertEquals(1, counter.popAll().size()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/interceptors/AddSplitHeadersFilterTest.java b/client/src/test/java/io/split/client/interceptors/AddSplitHeadersFilterTest.java deleted file mode 100644 index 2c68b033..00000000 --- a/client/src/test/java/io/split/client/interceptors/AddSplitHeadersFilterTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.split.client.interceptors; - -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.HttpRequest; -import org.apache.hc.core5.http.protocol.HttpContext; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mockito; - -import org.mockito.runners.MockitoJUnitRunner; - -import java.io.IOException; -import java.util.List; - -import static org.hamcrest.Matchers.*; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; - - -@RunWith(MockitoJUnitRunner.class) -public class AddSplitHeadersFilterTest { - - @Captor - private ArgumentCaptor headerNameCaptor; - - @Captor - private ArgumentCaptor headerValueCaptor; - - @Test - public void testHeadersWithIpAndHostname() throws IOException, HttpException { - AddSplitHeadersFilter filter = AddSplitHeadersFilter.instance("abc", true); - HttpRequest req = Mockito.mock(HttpRequest.class); - HttpContext ctx = Mockito.mock(HttpContext.class); - - filter.process(req, null, ctx); - Mockito.verify(req, Mockito.times(4)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); - - List headerNames = headerNameCaptor.getAllValues(); - List headerValues = headerValueCaptor.getAllValues(); - - assertThat(headerNames.size(), is(equalTo(4))); - assertThat(headerValues.size(), is(equalTo(4))); - - assertThat(headerNames, contains(AddSplitHeadersFilter.AUTHORIZATION_HEADER, - AddSplitHeadersFilter.CLIENT_VERSION, - AddSplitHeadersFilter.CLIENT_MACHINE_NAME_HEADER, - AddSplitHeadersFilter.CLIENT_MACHINE_IP_HEADER)); - } - - @Test - public void testHeadersWithoutIpAndHostname() throws IOException, HttpException { - AddSplitHeadersFilter filter = AddSplitHeadersFilter.instance("abc", false); - HttpRequest req = Mockito.mock(HttpRequest.class); - HttpContext ctx = Mockito.mock(HttpContext.class); - - filter.process(req, null, ctx); - Mockito.verify(req, Mockito.times(2)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); - - List headerNames = headerNameCaptor.getAllValues(); - List headerValues = headerValueCaptor.getAllValues(); - - assertThat(headerNames.size(), is(equalTo(2))); - assertThat(headerValues.size(), is(equalTo(2))); - - assertThat(headerNames, contains(AddSplitHeadersFilter.AUTHORIZATION_HEADER, - AddSplitHeadersFilter.CLIENT_VERSION)); - - assertThat(headerNames, not(contains(AddSplitHeadersFilter.CLIENT_MACHINE_NAME_HEADER, - AddSplitHeadersFilter.CLIENT_MACHINE_IP_HEADER))); - } -} diff --git a/client/src/test/java/io/split/client/interceptors/AuthorizationInterceptorFilterTest.java b/client/src/test/java/io/split/client/interceptors/AuthorizationInterceptorFilterTest.java new file mode 100644 index 00000000..1f10a90d --- /dev/null +++ b/client/src/test/java/io/split/client/interceptors/AuthorizationInterceptorFilterTest.java @@ -0,0 +1,51 @@ +package io.split.client.interceptors; + +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import java.io.IOException; +import java.util.List; + +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.*; + +@RunWith(MockitoJUnitRunner.class) +public class AuthorizationInterceptorFilterTest { + + @Captor + private ArgumentCaptor headerNameCaptor; + + @Captor + private ArgumentCaptor headerValueCaptor; + + @Test + public void authorizationInterceptorWithValue() throws IOException, HttpException { + AuthorizationInterceptorFilter filter = AuthorizationInterceptorFilter.instance("api-token-test"); + HttpRequest req = Mockito.mock(HttpRequest.class); + HttpContext ctx = Mockito.mock(HttpContext.class); + + filter.process(req, null, ctx); + Mockito.verify(req, Mockito.times(1)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); + + List headerNames = headerNameCaptor.getAllValues(); + List headerValues = headerValueCaptor.getAllValues(); + + assertEquals(1, headerNames.size()); + assertEquals(1, headerValues.size()); + + assertThat(headerNames, contains(AuthorizationInterceptorFilter.AUTHORIZATION_HEADER)); + assertThat(headerValues, contains("Bearer api-token-test")); + } + + @Test(expected = Exception.class) + public void authorizationInterceptorWithoutValue() { + AuthorizationInterceptorFilter filter = AuthorizationInterceptorFilter.instance(null); + } +} diff --git a/client/src/test/java/io/split/client/interceptors/ClientKeyInterceptorFilterTest.java b/client/src/test/java/io/split/client/interceptors/ClientKeyInterceptorFilterTest.java new file mode 100644 index 00000000..56121882 --- /dev/null +++ b/client/src/test/java/io/split/client/interceptors/ClientKeyInterceptorFilterTest.java @@ -0,0 +1,46 @@ +package io.split.client.interceptors; + +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import java.io.IOException; +import java.util.List; + +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +@RunWith(MockitoJUnitRunner.class) +public class ClientKeyInterceptorFilterTest { + @Captor + private ArgumentCaptor headerNameCaptor; + + @Captor + private ArgumentCaptor headerValueCaptor; + + @Test + public void ClientKeyInterceptorFilter() throws IOException, HttpException { + ClientKeyInterceptorFilter filter = ClientKeyInterceptorFilter.instance("api-token-test-1234"); + HttpRequest req = Mockito.mock(HttpRequest.class); + HttpContext ctx = Mockito.mock(HttpContext.class); + + filter.process(req, null, ctx); + Mockito.verify(req, Mockito.times(1)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); + + List headerNames = headerNameCaptor.getAllValues(); + List headerValues = headerValueCaptor.getAllValues(); + + assertEquals(1, headerNames.size()); + assertEquals(1, headerValues.size()); + + assertThat(headerNames, contains(ClientKeyInterceptorFilter.CLIENT_KEY)); + assertThat(headerValues, contains("1234")); + } +} diff --git a/client/src/test/java/io/split/client/interceptors/FlagSetsFilterImplTest.java b/client/src/test/java/io/split/client/interceptors/FlagSetsFilterImplTest.java new file mode 100644 index 00000000..c0467e29 --- /dev/null +++ b/client/src/test/java/io/split/client/interceptors/FlagSetsFilterImplTest.java @@ -0,0 +1,26 @@ +package io.split.client.interceptors; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.HashSet; + +public class FlagSetsFilterImplTest { + + @Test + public void testIntersectSetsWithShouldFilter() { + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("a", "b"))); + Assert.assertTrue(flagSetsFilter.intersect("a")); + Assert.assertTrue(flagSetsFilter.intersect(new HashSet<>(Arrays.asList("a", "c")))); + Assert.assertFalse(flagSetsFilter.intersect("c")); + Assert.assertFalse(flagSetsFilter.intersect(new HashSet<>(Arrays.asList("d", "c")))); + } + + @Test + public void testIntersectSetsWithShouldNotFilter() { + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + Assert.assertTrue(flagSetsFilter.intersect("a")); + Assert.assertTrue(flagSetsFilter.intersect(new HashSet<>(Arrays.asList("a", "c")))); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/interceptors/SdkMetadataInterceptorFilterTest.java b/client/src/test/java/io/split/client/interceptors/SdkMetadataInterceptorFilterTest.java new file mode 100644 index 00000000..ad38e612 --- /dev/null +++ b/client/src/test/java/io/split/client/interceptors/SdkMetadataInterceptorFilterTest.java @@ -0,0 +1,66 @@ +package io.split.client.interceptors; + +import io.split.client.utils.SDKMetadata; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import java.io.IOException; +import java.util.List; + +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +@RunWith(MockitoJUnitRunner.class) +public class SdkMetadataInterceptorFilterTest { + @Captor + private ArgumentCaptor headerNameCaptor; + + @Captor + private ArgumentCaptor headerValueCaptor; + + @Test + public void sdkMetadataWithIpEnabled() throws IOException, HttpException { + SdkMetadataInterceptorFilter filter = SdkMetadataInterceptorFilter.instance(new SDKMetadata( "sdk-version-1.2.3", "156256325", "testName")); + HttpRequest req = Mockito.mock(HttpRequest.class); + HttpContext ctx = Mockito.mock(HttpContext.class); + + filter.process(req, null, ctx); + Mockito.verify(req, Mockito.times(3)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); + + List headerNames = headerNameCaptor.getAllValues(); + List headerValues = headerValueCaptor.getAllValues(); + + assertEquals(3, headerNames.size()); + assertEquals(3, headerValues.size()); + + assertThat(headerNames, contains(SdkMetadataInterceptorFilter.CLIENT_VERSION, + SdkMetadataInterceptorFilter.CLIENT_MACHINE_NAME_HEADER, + SdkMetadataInterceptorFilter.CLIENT_MACHINE_IP_HEADER)); + } + + @Test + public void sdkMetadataWithIpDisabled() throws IOException, HttpException { + SdkMetadataInterceptorFilter filter = SdkMetadataInterceptorFilter.instance(new SDKMetadata( "sdk-version-1.2.3", "", "")); + HttpRequest req = Mockito.mock(HttpRequest.class); + HttpContext ctx = Mockito.mock(HttpContext.class); + + filter.process(req, null, ctx); + Mockito.verify(req, Mockito.times(1)).addHeader(headerNameCaptor.capture(), headerValueCaptor.capture()); + + List headerNames = headerNameCaptor.getAllValues(); + List headerValues = headerValueCaptor.getAllValues(); + + assertEquals(1, headerNames.size()); + assertEquals(1, headerValues.size()); + + assertThat(headerNames, contains(SdkMetadataInterceptorFilter.CLIENT_VERSION)); + } +} diff --git a/client/src/test/java/io/split/client/lifecycle/EventThreads.java b/client/src/test/java/io/split/client/lifecycle/EventThreads.java new file mode 100644 index 00000000..c8e23f4f --- /dev/null +++ b/client/src/test/java/io/split/client/lifecycle/EventThreads.java @@ -0,0 +1,43 @@ +package io.split.client.lifecycle; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; + +/** + * Leak check for the event-delivery thread. Other tests sharing the JVM may leave their own event threads running (or + * start one lazily at any moment), so instead of scanning all threads a test records the threads that delivered events + * to its own listeners and afterwards asserts that exactly those have terminated. + */ +public final class EventThreads { + + private static final Set DELIVERED_ON = Collections.synchronizedSet(new HashSet()); + + private EventThreads() { + } + + /** Call from a listener: remembers the thread the event was delivered on. */ + public static void record() { + DELIVERED_ON.add(Thread.currentThread()); + } + + public static void awaitRecordedThreadsTerminated() { + try { + await().atMost(10, TimeUnit.SECONDS).until(() -> { + synchronized (DELIVERED_ON) { + for (Thread t : DELIVERED_ON) { + if (t.isAlive()) { + return false; + } + } + return true; + } + }); + } finally { + DELIVERED_ON.clear(); + } + } +} diff --git a/client/src/test/java/io/split/client/lifecycle/LifecycleScriptDispatcher.java b/client/src/test/java/io/split/client/lifecycle/LifecycleScriptDispatcher.java new file mode 100644 index 00000000..fab8630b --- /dev/null +++ b/client/src/test/java/io/split/client/lifecycle/LifecycleScriptDispatcher.java @@ -0,0 +1,136 @@ +package io.split.client.lifecycle; + +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.RecordedRequest; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Stateful mock Split backend for the lifecycle event tests. Every endpoint answers "nothing changed" unless a + * response was scripted for the change number being requested, which lets a test publish exactly one change at + * the moment it chooses. + */ +final class LifecycleScriptDispatcher extends Dispatcher { + + static final String SEGMENT = "seg1"; + + private static final String AUTH_BODY = + "{\"pushEnabled\":true,\"token\":\"khdkjdahs987498217.eyJ4LWFibHktY2FwYWJpbGl0eSI6IntcInh4eHhfeHh4eF9zZWdtZW50c1wiOltcInN1YnNjcmliZVwiXSxcInh4eHhfeHh4eF9zcGxpdHNcIjpbXCJzdWJzY3JpYmVcIl0sXCJjb250cm9sXCI6W1wic3Vic2NyaWJlXCJdfSIsImV4cCI6MTU5MzAyNzQxMCwiaWF0IjoxNTkzMDIzODEwfQ\"}"; + + private final String initialFlags; + private final String initialSegment; + private final long headersDelayMillis; + private final Map flagScripts = new ConcurrentHashMap<>(); + private final Map segmentScripts = new ConcurrentHashMap<>(); + private final AtomicLong flagRequests = new AtomicLong(); + + LifecycleScriptDispatcher(String initialFlags, String initialSegment, long headersDelayMillis) { + this.initialFlags = initialFlags; + this.initialSegment = initialSegment; + this.headersDelayMillis = headersDelayMillis; + } + + /** Body served the next time the SDK asks for flag changes since {@code since}. */ + void scriptFlags(long since, String body) { + flagScripts.put(since, body); + } + + /** Body served the next time the SDK asks for changes of {@link #SEGMENT} since {@code since}. */ + void scriptSegment(long since, String body) { + segmentScripts.put(since, body); + } + + long flagRequests() { + return flagRequests.get(); + } + + @Override + public MockResponse dispatch(RecordedRequest request) { + String path = request.getPath(); + if (path.startsWith("/api/auth")) { + return new MockResponse().setBody(AUTH_BODY); + } + if (path.startsWith("/api/splitChanges")) { + flagRequests.incrementAndGet(); + long since = Long.parseLong(request.getRequestUrl().queryParameter("since")); + long rbSince = Long.parseLong(request.getRequestUrl().queryParameter("rbSince")); + MockResponse response = new MockResponse(); + if (headersDelayMillis > 0) { + response.setHeadersDelay(headersDelayMillis, java.util.concurrent.TimeUnit.MILLISECONDS); + } + if (since == -1) { + return response.setBody(initialFlags); + } + String scripted = flagScripts.get(since); + return response.setBody(scripted != null ? scripted : noFlagChanges(since, rbSince)); + } + if (path.startsWith("/api/segmentChanges/")) { + long since = Long.parseLong(request.getRequestUrl().queryParameter("since")); + if (since == -1) { + return new MockResponse().setBody(initialSegment); + } + String scripted = segmentScripts.get(since); + return new MockResponse().setBody(scripted != null ? scripted : noSegmentChanges(since)); + } + // telemetry, impressions, events + return new MockResponse().setResponseCode(200); + } + + static String noFlagChanges(long since, long rbSince) { + return "{\"ff\":{\"d\":[],\"s\":" + since + ",\"t\":" + since + "},\"rbs\":{\"d\":[],\"s\":" + rbSince + + ",\"t\":" + rbSince + "}}"; + } + + static String noSegmentChanges(long since) { + return segmentChange(since, since, "", ""); + } + + static String segmentChange(long since, long till, String addedCsv, String removedCsv) { + return "{\"name\":\"" + SEGMENT + "\",\"added\":[" + quoted(addedCsv) + "],\"removed\":[" + quoted(removedCsv) + + "],\"since\":" + since + ",\"till\":" + till + "}"; + } + + /** Flags body for a first fetch (since=-1) or a scripted change; {@code rbs} may be empty. */ + static String flagsBody(long since, long till, String flagsJson, long rbSince, long rbTill, String rbsJson) { + return "{\"ff\":{\"d\":[" + flagsJson + "],\"s\":" + since + ",\"t\":" + till + "},\"rbs\":{\"d\":[" + + rbsJson + "],\"s\":" + rbSince + ",\"t\":" + rbTill + "}}"; + } + + static String flag(String name, long changeNumber, boolean inSegment) { + String matcher = inSegment + ? "{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\"," + + "\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"" + SEGMENT + "\"}}" + : "{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\"," + + "\"negate\":false}"; + return "{\"trafficTypeName\":\"user\",\"name\":\"" + name + "\",\"trafficAllocation\":100," + + "\"trafficAllocationSeed\":-2092979940,\"seed\":105482719,\"status\":\"ACTIVE\",\"killed\":false," + + "\"defaultTreatment\":\"off\",\"changeNumber\":" + changeNumber + ",\"algo\":2,\"configurations\":{}," + + "\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[" + + matcher + "]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0}]," + + "\"label\":\"default rule\"}]}"; + } + + static String ruleBasedSegment(String name, long changeNumber) { + return "{\"changeNumber\":" + changeNumber + ",\"name\":\"" + name + "\",\"status\":\"ACTIVE\"," + + "\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[],\"segments\":[]},\"conditions\":[" + + "{\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"," + + "\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]}}]}"; + } + + private static String quoted(String csv) { + if (csv.isEmpty()) { + return ""; + } + StringBuilder out = new StringBuilder(); + for (String item : csv.split(",")) { + if (out.length() > 0) { + out.append(','); + } + out.append('"').append(item).append('"'); + } + return out.toString(); + } +} diff --git a/client/src/test/java/io/split/client/lifecycle/SdkEventsConfigTest.java b/client/src/test/java/io/split/client/lifecycle/SdkEventsConfigTest.java new file mode 100644 index 00000000..be93ad9c --- /dev/null +++ b/client/src/test/java/io/split/client/lifecycle/SdkEventsConfigTest.java @@ -0,0 +1,52 @@ +package io.split.client.lifecycle; + +import io.harness.events.EventsManagerConfig; +import io.split.client.api.SdkEvent; +import org.junit.Test; + +import java.util.Collections; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsCollectionContaining.hasItem; +import static org.junit.Assert.assertTrue; + +public class SdkEventsConfigTest { + + @Test + public void executionLimitsMatchOnceOnlyAndUnlimitedEvents() { + EventsManagerConfig config = SdkEventsConfig.build(); + assertThat(config.getExecutionLimits().get(SdkEvent.SDK_READY), is(1)); + assertThat(config.getExecutionLimits().get(SdkEvent.SDK_READY_TIMED_OUT), is(1)); + assertThat(config.getExecutionLimits().get(SdkEvent.SDK_UPDATE), is(-1)); + } + + @Test + public void sdkUpdateRequiresSdkReadyAsPrerequisite() { + EventsManagerConfig config = SdkEventsConfig.build(); + assertThat(config.getPrerequisites().get(SdkEvent.SDK_UPDATE), is(Collections.singleton(SdkEvent.SDK_READY))); + } + + @Test + public void evaluationOrderPlacesSdkReadyBeforeSdkUpdate() { + EventsManagerConfig config = SdkEventsConfig.build(); + java.util.List order = config.getEvaluationOrder(); + assertTrue(order.indexOf(SdkEvent.SDK_READY) < order.indexOf(SdkEvent.SDK_UPDATE)); + } + + @Test + public void sdkUpdateRequiresAnyOfTheFourUpdateInternalEvents() { + EventsManagerConfig config = SdkEventsConfig.build(); + java.util.Set> groups = config.getRequireAny().get(SdkEvent.SDK_UPDATE); + assertThat(groups, hasItem(Collections.singleton(SdkInternalEvent.FLAGS_UPDATED))); + assertThat(groups, hasItem(Collections.singleton(SdkInternalEvent.FLAG_KILLED_NOTIFICATION))); + assertThat(groups, hasItem(Collections.singleton(SdkInternalEvent.SEGMENTS_UPDATED))); + assertThat(groups, hasItem(Collections.singleton(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED))); + } + + @Test + public void buildDoesNotThrow() { + // Cycle detection runs inside build(); a circular prerequisite/suppressedBy graph would throw here. + SdkEventsConfig.build(); + } +} diff --git a/client/src/test/java/io/split/client/lifecycle/SdkEventsManagerTest.java b/client/src/test/java/io/split/client/lifecycle/SdkEventsManagerTest.java new file mode 100644 index 00000000..8731164e --- /dev/null +++ b/client/src/test/java/io/split/client/lifecycle/SdkEventsManagerTest.java @@ -0,0 +1,253 @@ +package io.split.client.lifecycle; + +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.awaitility.Awaitility.await; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class SdkEventsManagerTest { + + @Test + public void listenersOnSameEventFireInRegistrationOrder() { + SdkEventsManager manager = SdkEventsManager.create(null); + List order = new CopyOnWriteArrayList<>(); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + manager.on(SdkEvent.SDK_UPDATE, metadata -> order.add(1)); + manager.on(SdkEvent.SDK_UPDATE, metadata -> order.add(2)); + manager.on(SdkEvent.SDK_UPDATE, metadata -> order.add(3)); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + + await().atMost(2, TimeUnit.SECONDS).until(() -> order.size() == 3); + assertEquals(Arrays.asList(1, 2, 3), order); + manager.destroy(); + } + + @Test + public void aThrowingListenerDoesNotBlockOthers() { + SdkEventsManager manager = SdkEventsManager.create(null); + AtomicInteger secondCalls = new AtomicInteger(); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + manager.on(SdkEvent.SDK_UPDATE, metadata -> { + throw new RuntimeException("boom"); + }); + manager.on(SdkEvent.SDK_UPDATE, metadata -> secondCalls.incrementAndGet()); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + + await().atMost(2, TimeUnit.SECONDS).until(() -> secondCalls.get() == 1); + manager.destroy(); + } + + @Test + public void sdkReadyFiresOnceAndReplaysNullToLateSubscriber() { + SdkEventsManager manager = SdkEventsManager.create(null); + List earlyReceived = new CopyOnWriteArrayList<>(); + manager.on(SdkEvent.SDK_READY, earlyReceived::add); + + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, 123L)); + await().atMost(2, TimeUnit.SECONDS).until(() -> earlyReceived.size() == 1); + assertThat(earlyReceived.get(0).isInitialCacheLoad(), is(true)); + + List lateReceived = new CopyOnWriteArrayList<>(); + manager.on(SdkEvent.SDK_READY, lateReceived::add); + await().atMost(2, TimeUnit.SECONDS).until(() -> lateReceived.size() == 1); + assertNull(lateReceived.get(0)); + manager.destroy(); + } + + @Test + public void sdkUpdateNeverReplaysToLateSubscriber() throws InterruptedException { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + // give the first update time to be fully delivered before the late listener registers + Thread.sleep(200); + + AtomicInteger lateCalls = new AtomicInteger(); + manager.on(SdkEvent.SDK_UPDATE, metadata -> lateCalls.incrementAndGet()); + Thread.sleep(200); + assertEquals(0, lateCalls.get()); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("b"))); + await().atMost(2, TimeUnit.SECONDS).until(() -> lateCalls.get() == 1); + manager.destroy(); + } + + @Test + public void sdkUpdateRequiresSdkReadyFirst() throws InterruptedException { + SdkEventsManager manager = SdkEventsManager.create(null); + AtomicInteger updateCalls = new AtomicInteger(); + manager.on(SdkEvent.SDK_UPDATE, metadata -> updateCalls.incrementAndGet()); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + Thread.sleep(200); + assertEquals(0, updateCalls.get()); + + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + Thread.sleep(200); + assertEquals(0, updateCalls.get()); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + await().atMost(2, TimeUnit.SECONDS).until(() -> updateCalls.get() == 1); + manager.destroy(); + } + + @Test + public void sdkUpdateMetadataReflectsWhicheverInternalEventFired() { + SdkEventsManager flagsManager = SdkEventsManager.create(null); + List flagsReceived = new CopyOnWriteArrayList<>(); + flagsManager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + flagsManager.on(SdkEvent.SDK_UPDATE, flagsReceived::add); + flagsManager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + await().atMost(2, TimeUnit.SECONDS).until(() -> flagsReceived.size() == 1); + assertEquals(SdkEventType.FLAGS_UPDATE, flagsReceived.get(0).getType()); + assertEquals(Collections.singleton("a"), flagsReceived.get(0).getNames()); + flagsManager.destroy(); + + SdkEventsManager segmentsManager = SdkEventsManager.create(null); + List segmentsReceived = new CopyOnWriteArrayList<>(); + segmentsManager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + segmentsManager.on(SdkEvent.SDK_UPDATE, segmentsReceived::add); + segmentsManager.notifyInternalEvent(SdkInternalEvent.SEGMENTS_UPDATED, SdkEventMetadata.update(SdkEventType.SEGMENTS_UPDATE, Collections.emptyList())); + await().atMost(2, TimeUnit.SECONDS).until(() -> segmentsReceived.size() == 1); + assertEquals(SdkEventType.SEGMENTS_UPDATE, segmentsReceived.get(0).getType()); + segmentsManager.destroy(); + } + + @Test + public void offClearsListenersButDispatcherKeepsWorking() { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + + AtomicInteger firstListenerCalls = new AtomicInteger(); + manager.on(SdkEvent.SDK_UPDATE, metadata -> firstListenerCalls.incrementAndGet()); + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("a"))); + await().atMost(2, TimeUnit.SECONDS).until(() -> firstListenerCalls.get() == 1); + + manager.off(SdkEvent.SDK_UPDATE); + AtomicInteger secondListenerCalls = new AtomicInteger(); + manager.on(SdkEvent.SDK_UPDATE, metadata -> secondListenerCalls.incrementAndGet()); + + manager.notifyInternalEvent(SdkInternalEvent.FLAGS_UPDATED, SdkEventMetadata.update(SdkEventType.FLAGS_UPDATE, Arrays.asList("b"))); + await().atMost(2, TimeUnit.SECONDS).until(() -> secondListenerCalls.get() == 1); + assertEquals(1, firstListenerCalls.get()); + manager.destroy(); + } + + @Test + public void registrationIsNoOpAfterDestroy() throws InterruptedException { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.destroy(); + + AtomicInteger calls = new AtomicInteger(); + manager.on(SdkEvent.SDK_READY, metadata -> calls.incrementAndGet()); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + Thread.sleep(200); + assertEquals(0, calls.get()); + } + + @Test + public void notifyAndTriggeredQueryAfterDestroyAreNoOps() { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.destroy(); + + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + assertEquals(false, manager.eventAlreadyTriggered(SdkEvent.SDK_READY)); + } + + @Test + public void offAfterDestroyIsNoOp() { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.destroy(); + + manager.off(SdkEvent.SDK_UPDATE); + } + + @Test + public void inFlightDispatchAfterDestroyIsDroppedSilently() throws Exception { + SdkEventsManager manager = SdkEventsManager.create(null); + manager.destroy(); + + java.lang.reflect.Method dispatch = SdkEventsManager.class.getDeclaredMethod("dispatch", SdkEvent.class, SdkEventMetadata.class); + dispatch.setAccessible(true); + dispatch.invoke(manager, SdkEvent.SDK_UPDATE, null); + } + + @Test + public void deliveryAfterExecutorShutdownIsDroppedWithoutThrowing() throws Exception { + SdkEventsManager manager = SdkEventsManager.create(null); + AtomicInteger readyCalls = new AtomicInteger(); + manager.on(SdkEvent.SDK_READY, metadata -> readyCalls.incrementAndGet()); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + await().atMost(2, TimeUnit.SECONDS).until(() -> readyCalls.get() == 1); + + java.lang.reflect.Field executorField = SdkEventsManager.class.getDeclaredField("deliveryExecutor"); + executorField.setAccessible(true); + ((java.util.concurrent.ExecutorService) executorField.get(manager)).shutdown(); + + // replay path: delivered already contains SDK_READY, so on() submits to the shut-down executor + manager.on(SdkEvent.SDK_READY, metadata -> { }); + // trigger path: commons calls deliverAsync, which submits to the shut-down executor + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED, null); + Thread.sleep(200); + manager.destroy(); + } + + @Test + public void concurrentRegistrationRacingATriggerDeliversExactlyOnceEach() throws InterruptedException { + SdkEventsManager manager = SdkEventsManager.create(null); + int listenerCount = 50; + AtomicInteger totalCalls = new AtomicInteger(); + CountDownLatch startLatch = new CountDownLatch(1); + CountDownLatch doneLatch = new CountDownLatch(listenerCount); + + for (int i = 0; i < listenerCount; i++) { + Thread t = new Thread(() -> { + try { + startLatch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + manager.on(SdkEvent.SDK_READY, metadata -> totalCalls.incrementAndGet()); + doneLatch.countDown(); + }); + t.start(); + } + + startLatch.countDown(); + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY, SdkEventMetadata.ready(true, null)); + doneLatch.await(5, TimeUnit.SECONDS); + + await().atMost(3, TimeUnit.SECONDS).until(() -> totalCalls.get() == listenerCount); + assertEquals(listenerCount, totalCalls.get()); + manager.destroy(); + } + + @Test + public void sdkReadyTimedOutDeliversNullMetadata() { + SdkEventsManager manager = SdkEventsManager.create(null); + List received = new CopyOnWriteArrayList<>(); + manager.on(SdkEvent.SDK_READY_TIMED_OUT, received::add); + + manager.notifyInternalEvent(SdkInternalEvent.SDK_READY_TIMEOUT_REACHED, null); + + await().atMost(2, TimeUnit.SECONDS).until(() -> received.size() == 1); + assertNull(received.get(0)); + manager.destroy(); + } +} diff --git a/client/src/test/java/io/split/client/lifecycle/SdkLifecycleEventsIntegrationTest.java b/client/src/test/java/io/split/client/lifecycle/SdkLifecycleEventsIntegrationTest.java new file mode 100644 index 00000000..868cf851 --- /dev/null +++ b/client/src/test/java/io/split/client/lifecycle/SdkLifecycleEventsIntegrationTest.java @@ -0,0 +1,412 @@ +package io.split.client.lifecycle; + +import io.split.SSEMockServer; +import io.split.SplitMockServer; +import io.split.client.SplitClient; +import io.split.client.SplitClientConfig; +import io.split.client.SplitFactory; +import io.split.client.SplitFactoryBuilder; +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import org.glassfish.grizzly.utils.Pair; +import org.glassfish.jersey.media.sse.OutboundEvent; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.time.Duration; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static io.split.client.lifecycle.LifecycleScriptDispatcher.SEGMENT; +import static io.split.client.lifecycle.LifecycleScriptDispatcher.flag; +import static io.split.client.lifecycle.LifecycleScriptDispatcher.flagsBody; +import static io.split.client.lifecycle.LifecycleScriptDispatcher.ruleBasedSegment; +import static io.split.client.lifecycle.LifecycleScriptDispatcher.segmentChange; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * End-to-end tests for the SDK lifecycle events (SDK_READY, SDK_UPDATE, SDK_READY_TIMED_OUT) driving a real standalone + * {@link SplitFactory} against a scripted mock backend, over both polling and streaming. Localhost and consumer modes + * are covered by {@link SdkLifecycleEventsModesIntegrationTest}. + * + *

Out of scope here because they only exist in the client-side SDKs: the "sdkReadyFromCache" scenario and the + * per-client fan-out scenario (EventsManagerCoordinator). + */ +@RunWith(Parameterized.class) +public class SdkLifecycleEventsIntegrationTest { + + private static final long INITIAL_CN = 100L; + private static final long UPDATED_CN = 200L; + private static final long SEGMENT_CN = 10L; + private static final long SEGMENT_UPDATED_CN = 20L; + private static final long WAIT_SECONDS = 20L; + // The segment refresh rate has a 30 second floor, so a polled segment change takes up to that long to arrive. + private static final long SEGMENT_POLL_WAIT_SECONDS = 70L; + private static final Duration QUIET_WINDOW = Duration.ofMillis(1500); + + @Parameterized.Parameters(name = "streaming={0}") + public static Collection modes() { + return Arrays.asList(new Object[][] {{false}, {true}}); + } + + private final boolean streaming; + private LifecycleScriptDispatcher backend; + private SplitMockServer splitServer; + private SSEMockServer sseServer; + private SSEMockServer.SseEventQueue sseQueue; + private SplitFactory factory; + + public SdkLifecycleEventsIntegrationTest(boolean streaming) { + this.streaming = streaming; + } + + @Before + public void startBackend() throws Exception { + newBackend(0); + } + + @After + public void stopEverything() throws Exception { + if (factory != null) { + factory.destroy(); + } + if (sseServer != null) { + sseServer.stop(); + } + if (splitServer != null) { + splitServer.stop(); + } + // No leaked event-delivery thread once the factory is destroyed (threads that delivered to this test's listeners). + EventThreads.awaitRecordedThreadsTerminated(); + } + + // Scenario: sdkReady fires once the SDK is ready, and replays to a subscriber that registers after the fact. + @Test + public void sdkReadyFiresOnceAndReplaysToLateSubscribers() throws Exception { + SplitClient client = buildClient(10000); + Recorder early = new Recorder(); + client.on(SdkEvent.SDK_READY, early); + + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> early.size() == 1); + + Recorder late = new Recorder(); + client.on(SdkEvent.SDK_READY, late); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> late.size() == 1); + assertNull("a replayed SDK_READY carries no metadata", late.get(0)); + + // later syncs (and later registrations) never fire it again + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> early.size() == 1); + assertEquals(1, late.size()); + } + + // Scenario: sdkUpdate fires only after sdkReady (the initial load is not an update). + @Test + public void sdkUpdateIsNotRaisedByTheInitialLoadOnlyByLaterChanges() throws Exception { + SplitClient client = buildClient(10000); + Recorder ready = new Recorder(); + Recorder updates = new Recorder(); + client.on(SdkEvent.SDK_READY, ready); + client.on(SdkEvent.SDK_UPDATE, updates); + + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> ready.size() == 1); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() == 0); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() == 1); + } + + // Scenario: sdkUpdate does not replay to a subscriber that registers after an update already happened. + @Test + public void sdkUpdateDoesNotReplayToLateSubscribers() throws Exception { + SplitClient client = buildClient(10000); + Recorder first = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, first); + client.blockUntilReady(); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> first.size() == 1); + + Recorder late = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, late); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> late.size() == 0); + assertEquals(1, first.size()); + } + + // Scenario: sdkReadyTimedOut fires when the SDK does not become ready within the timeout. + @Test + public void sdkReadyTimedOutFiresWhenTheSdkIsNotReadyInTime() throws Exception { + stopBackend(); + newBackend(4000); + SplitClient client = buildClient(300); + Recorder timedOut = new Recorder(); + Recorder ready = new Recorder(); + client.on(SdkEvent.SDK_READY_TIMED_OUT, timedOut); + client.on(SdkEvent.SDK_READY, ready); + + try { + client.blockUntilReady(); + fail("the backend is slower than the timeout, blockUntilReady must time out"); + } catch (TimeoutException expected) { + // expected + } + + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> timedOut.size() == 1); + assertNull(timedOut.get(0)); + assertEquals(0, ready.size()); + + Recorder lateTimedOut = new Recorder(); + client.on(SdkEvent.SDK_READY_TIMED_OUT, lateTimedOut); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> lateTimedOut.size() == 1); + } + + // Scenario: sdkReadyTimedOut is suppressed when sdkReady wins. + @Test + public void sdkReadyTimedOutIsSuppressedWhenTheSdkIsReady() throws Exception { + SplitClient client = buildClient(10000); + Recorder ready = new Recorder(); + Recorder timedOut = new Recorder(); + client.on(SdkEvent.SDK_READY, ready); + client.on(SdkEvent.SDK_READY_TIMED_OUT, timedOut); + + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> ready.size() == 1); + + // already ready: another wait neither times out nor raises the event + client.blockUntilReady(); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> timedOut.size() == 0); + } + + // Scenario: listeners on one event are invoked sequentially, in registration order, with error isolation. + @Test + public void listenersRunSequentiallyInRegistrationOrderAndAThrowingOneDoesNotBlockTheRest() throws Exception { + SplitClient client = buildClient(10000); + List order = new CopyOnWriteArrayList<>(); + List threads = new CopyOnWriteArrayList<>(); + client.on(SdkEvent.SDK_UPDATE, metadata -> { + order.add("first"); + threads.add(Thread.currentThread().getName()); + EventThreads.record(); + }); + client.on(SdkEvent.SDK_UPDATE, metadata -> { + order.add("throws"); + throw new IllegalStateException("listener failure under test"); + }); + client.on(SdkEvent.SDK_UPDATE, metadata -> { + order.add("third"); + threads.add(Thread.currentThread().getName()); + EventThreads.record(); + }); + client.blockUntilReady(); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> order.size() == 3); + assertEquals(Arrays.asList("first", "throws", "third"), order); + assertEquals("listeners share the single delivery thread", threads.get(0), threads.get(1)); + assertFalse("not delivered on the test thread", threads.get(0).equals(Thread.currentThread().getName())); + } + + // Scenario: the metadata is exposed to handlers. + @Test + public void metadataIsExposedToHandlers() throws Exception { + SplitClient client = buildClient(10000); + Recorder ready = new Recorder(); + Recorder updates = new Recorder(); + client.on(SdkEvent.SDK_READY, ready); + client.on(SdkEvent.SDK_UPDATE, updates); + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> ready.size() == 1); + assertTrue(ready.get(0).isInitialCacheLoad()); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() == 1); + + assertEquals(SdkEventType.FLAGS_UPDATE, updates.get(0).getType()); + assertEquals(Collections.singleton("f1"), updates.get(0).getNames()); + } + + // Scenario: destroy stops events and clears the handlers. + @Test + public void destroyStopsEventsAndClearsHandlers() throws Exception { + SplitClient client = buildClient(10000); + Recorder updates = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, updates); + client.blockUntilReady(); + + factory.destroy(); + + // events published after destroy never reach the dropped handlers + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + Recorder afterDestroy = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, afterDestroy); + client.on(SdkEvent.SDK_READY, afterDestroy); + client.off(SdkEvent.SDK_UPDATE); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS) + .until(() -> updates.size() == 0 && afterDestroy.size() == 0); + assertTrue(factory.isDestroyed()); + } + + // Appendix 4, case D: a polled response carrying flags and rule-based segments raises exactly one SDK_UPDATE, + // typed FLAGS_UPDATE. + @Test + public void flagsAndRuleBasedSegmentsInOneResponseRaiseExactlyOneFlagsUpdate() throws Exception { + SplitClient client = buildClient(10000); + Recorder updates = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, updates); + client.blockUntilReady(); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f2", UPDATED_CN, false), + -1, UPDATED_CN, ruleBasedSegment("rbs1", UPDATED_CN))); + + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() >= 1); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() == 1); + assertEquals(SdkEventType.FLAGS_UPDATE, updates.get(0).getType()); + assertEquals(Collections.singleton("f2"), updates.get(0).getNames()); + } + + // Appendix 4, case E: a polled segment change raises exactly one SDK_UPDATE, typed SEGMENTS_UPDATE. + @Test + public void aSegmentChangeRaisesExactlyOneSegmentsUpdate() throws Exception { + SplitClient client = buildClient(10000); + Recorder updates = new Recorder(); + client.on(SdkEvent.SDK_UPDATE, updates); + client.blockUntilReady(); + + backend.scriptSegment(SEGMENT_CN, segmentChange(SEGMENT_CN, SEGMENT_UPDATED_CN, "b", "")); + if (streaming) { + pushSse("xxxx_xxxx_segments", "{\\\"type\\\":\\\"SEGMENT_UPDATE\\\",\\\"changeNumber\\\":" + SEGMENT_UPDATED_CN + + ",\\\"segmentName\\\":\\\"" + SEGMENT + "\\\"}"); + } + + await().atMost(SEGMENT_POLL_WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() >= 1); + await().during(QUIET_WINDOW).atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> updates.size() == 1); + assertEquals(SdkEventType.SEGMENTS_UPDATE, updates.get(0).getType()); + assertTrue(updates.get(0).getNames().isEmpty()); + } + + // A throwing listener does not affect other listeners or destabilise the SDK. + @Test + public void aThrowingListenerDoesNotDestabiliseTheSdk() throws Exception { + SplitClient client = buildClient(10000); + Recorder healthyReady = new Recorder(); + Recorder healthyUpdate = new Recorder(); + client.on(SdkEvent.SDK_READY, metadata -> { + throw new IllegalStateException("ready listener failure under test"); + }); + client.on(SdkEvent.SDK_READY, healthyReady); + client.on(SdkEvent.SDK_UPDATE, metadata -> { + throw new IllegalStateException("update listener failure under test"); + }); + client.on(SdkEvent.SDK_UPDATE, healthyUpdate); + + client.blockUntilReady(); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> healthyReady.size() == 1); + assertEquals("on", client.getTreatment("a", "f1")); + + publishFlagChange(flagsBody(INITIAL_CN, UPDATED_CN, flag("f1", UPDATED_CN, true), -1, -1, "")); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> healthyUpdate.size() == 1); + + // delivery keeps working for subsequent events, and evaluation is unaffected + publishFlagChange(UPDATED_CN, 300L, flagsBody(UPDATED_CN, 300L, flag("f1", 300L, true), -1, -1, "")); + await().atMost(WAIT_SECONDS, TimeUnit.SECONDS).until(() -> healthyUpdate.size() == 2); + assertEquals("on", client.getTreatment("a", "f1")); + } + + private void newBackend(long headersDelayMillis) throws Exception { + backend = new LifecycleScriptDispatcher( + flagsBody(-1, INITIAL_CN, flag("f1", INITIAL_CN, true), -1, -1, ""), + segmentChange(-1, SEGMENT_CN, "a", ""), + headersDelayMillis); + splitServer = new SplitMockServer(backend); + splitServer.start(); + if (streaming) { + sseQueue = new SSEMockServer.SseEventQueue(); + sseServer = new SSEMockServer(sseQueue, (token, version, channel) -> { + if (!"1.1".equals(version)) { + return new Pair<>(new OutboundEvent.Builder().data("wrong version").build(), false); + } + return new Pair<>(null, true); + }); + sseServer.start(); + } + } + + private void stopBackend() throws Exception { + if (sseServer != null) { + sseServer.stop(); + sseServer = null; + } + splitServer.stop(); + } + + private SplitClient buildClient(int blockUntilReadyTimeoutMillis) throws Exception { + SplitClientConfig.Builder builder = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(blockUntilReadyTimeoutMillis) + .endpoint(splitServer.getUrl(), splitServer.getUrl()) + .authServiceURL(String.format("%s/api/auth/enabled", splitServer.getUrl())) + .featuresRefreshRate(5) + .segmentsRefreshRate(30) + .streamingEnabled(streaming); + if (streaming) { + builder.streamingServiceURL("http://localhost:" + sseServer.getPort()); + } + factory = SplitFactoryBuilder.build("fake-api-token", builder.build()); + return factory.client(); + } + + private void publishFlagChange(String body) throws Exception { + publishFlagChange(INITIAL_CN, UPDATED_CN, body); + } + + /** Makes {@code body} the answer to "changes since {@code since}" and, when streaming, announces it. */ + private void publishFlagChange(long since, long till, String body) throws Exception { + backend.scriptFlags(since, body); + if (streaming) { + pushSse("xxxx_xxxx_splits", "{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":" + till + "}"); + } + } + + private void pushSse(String channel, String innerJson) { + sseQueue.push(new OutboundEvent.Builder() + .name("message") + .data("{\"id\":\"22\",\"clientId\":\"22\",\"timestamp\":1592590436082,\"encoding\":\"json\",\"channel\":\"" + + channel + "\",\"data\":\"" + innerJson + "\"}") + .build()); + } + + /** Collects every invocation; a replayed SDK_READY / SDK_READY_TIMED_OUT records {@code null}. */ + public static final class Recorder implements io.split.client.api.SdkEventListener { + private final List received = new CopyOnWriteArrayList<>(); + + @Override + public void handle(SdkEventMetadata metadata) { + EventThreads.record(); + received.add(metadata); + } + + public int size() { + return received.size(); + } + + public SdkEventMetadata get(int index) { + return received.get(index); + } + } +} diff --git a/client/src/test/java/io/split/client/metrics/BinarySearchLatencyTrackerTest.java b/client/src/test/java/io/split/client/metrics/BinarySearchLatencyTrackerTest.java deleted file mode 100644 index c04fa5b4..00000000 --- a/client/src/test/java/io/split/client/metrics/BinarySearchLatencyTrackerTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package io.split.client.metrics; - -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; - -public class BinarySearchLatencyTrackerTest { - - BinarySearchLatencyTracker tracker; - - @Before - public void before() { - tracker = new BinarySearchLatencyTracker(); - } - - /** - * Latencies of <=1 millis or <= 1000 micros correspond to the first bucket (index 0) - */ - @Test - public void testLessThanFirstBucket() { - - tracker.addLatencyMicros(750); - tracker.addLatencyMicros(450); - assertThat(tracker.getLatency(0), is(equalTo(2L))); - - tracker.addLatencyMillis(0); - assertThat(tracker.getLatency(0), is(equalTo(3L))); - } - - /** - * Latencies of 1 millis or <= 1000 micros correspond to the first bucket (index 0) - */ - @Test - public void testFirstBucket() { - - tracker.addLatencyMicros(1000); - assertThat(tracker.getLatency(0), is(equalTo(1L))); - - tracker.addLatencyMillis(1); - assertThat(tracker.getLatency(0), is(equalTo(2L))); - } - - /** - * Latencies of 7481 millis or 7481828 micros correspond to the last bucket (index 22) - */ - @Test - public void testLastBucket() { - - tracker.addLatencyMicros(7481828); - assertThat(tracker.getLatency(22), is(equalTo(1L))); - - tracker.addLatencyMillis(7481); - assertThat(tracker.getLatency(22), is(equalTo(2L))); - } - - /** - * Latencies of more than 7481 millis or 7481828 micros correspond to the last bucket (index 22) - */ - @Test - public void testGreaterThanLastBucket() { - - tracker.addLatencyMicros(7481830); - assertThat(tracker.getLatency(22), is(equalTo(1L))); - - tracker.addLatencyMicros(7999999); - assertThat(tracker.getLatency(22), is(equalTo(2L))); - - tracker.addLatencyMillis(7482); - assertThat(tracker.getLatency(22), is(equalTo(3L))); - - tracker.addLatencyMillis(8000); - assertThat(tracker.getLatency(22), is(equalTo(4L))); - } - - /** - * Latencies between 11,392 and 17,086 are in the 8th bucket. - */ - @Test - public void test8ThBucket() { - - tracker.addLatencyMicros(11392); - assertThat(tracker.getLatency(7), is(equalTo(1L))); - - tracker.addLatencyMicros(17086); - assertThat(tracker.getLatency(7), is(equalTo(2L))); - - } - -} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/metrics/CachedMetricsTest.java b/client/src/test/java/io/split/client/metrics/CachedMetricsTest.java deleted file mode 100644 index 57dd8a18..00000000 --- a/client/src/test/java/io/split/client/metrics/CachedMetricsTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package io.split.client.metrics; - -import com.google.common.collect.Lists; -import io.split.client.dtos.Counter; -import io.split.client.dtos.Latency; -import org.junit.Test; - -import java.util.List; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -/** - * Created by adilaijaz on 9/23/15. - */ -public class CachedMetricsTest { - - private static final class MyDTOMetrics implements DTOMetrics { - - private List latencies = Lists.newArrayList(); - private List counters = Lists.newArrayList(); - - @Override - public void time(Latency dto) { - latencies.add(dto); - } - - @Override - public void count(Counter dto) { - counters.add(dto); - } - } - - @Test - public void count() { - MyDTOMetrics metrics = new MyDTOMetrics(); - - CachedMetrics cachedMetrics = new CachedMetrics(metrics, 2); - - cachedMetrics.count("foo", 4); - cachedMetrics.count("foo", 5); - cachedMetrics.count("foo", 6); - cachedMetrics.count("foo", 7); - cachedMetrics.count("foo", 8); - - Counter counter = new Counter(); - counter.name = "foo"; - counter.delta = 9L; - - assertThat(metrics.counters.size(), is(equalTo(2))); - assertThat(metrics.counters.get(0).name, is(equalTo("foo"))); - assertThat(metrics.counters.get(0).delta, is(equalTo(9L))); - - assertThat(metrics.counters.get(1).name, is(equalTo("foo"))); - assertThat(metrics.counters.get(1).delta, is(equalTo(13L))); - } - - @Test - public void latency() throws Exception { - MyDTOMetrics delegate = new MyDTOMetrics(); - CachedMetrics cachedMetrics = new CachedMetrics(delegate, 15L); - - cachedMetrics.time("foo", 4); - cachedMetrics.time("foo", 5); - cachedMetrics.time("foo", 6); - cachedMetrics.time("foo", 7); - Thread.sleep(30); - cachedMetrics.time("foo", 8); - - List latencies = Lists.newArrayList(0L, 0L, 0L, 0L, 2L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L); - - assertThat(delegate.latencies.get(0).name, is(equalTo("foo"))); - assertThat(delegate.latencies.get(0).latencies, is(equalTo(latencies))); - } - -} diff --git a/client/src/test/java/io/split/client/metrics/HttpMetricsTest.java b/client/src/test/java/io/split/client/metrics/HttpMetricsTest.java deleted file mode 100644 index 71ade9db..00000000 --- a/client/src/test/java/io/split/client/metrics/HttpMetricsTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.split.client.metrics; - -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; - -import java.net.URI; -import java.net.URISyntaxException; - -public class HttpMetricsTest { - @Test - public void testDefaultURL() throws URISyntaxException { - URI rootTarget = URI.create("https://api.split.io"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpMetrics fetcher = HttpMetrics.create(httpClient, rootTarget); - Assert.assertThat(fetcher.getTimeTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/metrics/time"))); - Assert.assertThat(fetcher.getCounterTarget().toString(), Matchers.is(Matchers.equalTo("https://api.split.io/api/metrics/counter"))); - } - - @Test - public void testCustomURLNoPathNoBackslash() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpMetrics fetcher = HttpMetrics.create(httpClient, rootTarget); - Assert.assertThat(fetcher.getTimeTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/api/metrics/time"))); - Assert.assertThat(fetcher.getCounterTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/api/metrics/counter"))); - } - - @Test - public void testCustomURLAppendingPath() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com/split/"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpMetrics fetcher = HttpMetrics.create(httpClient, rootTarget); - Assert.assertThat(fetcher.getTimeTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/metrics/time"))); - Assert.assertThat(fetcher.getCounterTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/metrics/counter"))); - } - - @Test - public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException { - URI rootTarget = URI.create("https://kubernetesturl.com/split"); - CloseableHttpClient httpClient = HttpClients.custom().build(); - HttpMetrics fetcher = HttpMetrics.create(httpClient, rootTarget); - Assert.assertThat(fetcher.getTimeTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/metrics/time"))); - Assert.assertThat(fetcher.getCounterTarget().toString(), Matchers.is(Matchers.equalTo("https://kubernetesturl.com/split/api/metrics/counter"))); - } -} diff --git a/client/src/test/java/io/split/client/metrics/LogarithmicSearchLatencyTrackerTest.java b/client/src/test/java/io/split/client/metrics/LogarithmicSearchLatencyTrackerTest.java deleted file mode 100644 index fedcec0a..00000000 --- a/client/src/test/java/io/split/client/metrics/LogarithmicSearchLatencyTrackerTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package io.split.client.metrics; - -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -@Ignore -public class LogarithmicSearchLatencyTrackerTest { - - LogarithmicSearchLatencyTracker tracker; - - @Before - public void before() { - tracker = new LogarithmicSearchLatencyTracker(); - } - - /** - * Latencies of <=1 millis or <= 1000 micros correspond to the first bucket (index 0) - */ - @Test - public void testLessThanFirstBucket() { - - tracker.addLatencyMicros(750); - tracker.addLatencyMicros(450); - assertThat(tracker.getLatency(0), is(equalTo(2L))); - - tracker.addLatencyMillis(0); - assertThat(tracker.getLatency(0), is(equalTo(3L))); - } - - /** - * Latencies of 1 millis or <= 1000 micros correspond to the first bucket (index 0) - */ - @Test - public void testFirstBucket() { - - tracker.addLatencyMicros(1000); - assertThat(tracker.getLatency(0), is(equalTo(1L))); - - tracker.addLatencyMillis(1); - assertThat(tracker.getLatency(0), is(equalTo(2L))); - } - - /** - * Latencies of 7481 millis or 7481828 micros correspond to the last bucket (index 22) - */ - @Test - public void testLastBucket() { - - tracker.addLatencyMicros(7481828); - assertThat(tracker.getLatency(22), is(equalTo(1L))); - - tracker.addLatencyMillis(7481); - assertThat(tracker.getLatency(22), is(equalTo(2L))); - } - - /** - * Latencies of more than 7481 millis or 7481828 micros correspond to the last bucket (index 22) - */ - @Test - public void testGreaterThanLastBucket() { - - tracker.addLatencyMicros(7481830); - assertThat(tracker.getLatency(22), is(equalTo(1L))); - - tracker.addLatencyMicros(7999999); - assertThat(tracker.getLatency(22), is(equalTo(2L))); - - tracker.addLatencyMillis(7482); - assertThat(tracker.getLatency(22), is(equalTo(3L))); - - tracker.addLatencyMillis(8000); - assertThat(tracker.getLatency(22), is(equalTo(4L))); - } - - /** - * Latencies between 11,392 and 17,086 are in the 8th bucket. - */ - @Test - public void test8ThBucket() { - - tracker.addLatencyMicros(11392); - assertThat(tracker.getLatency(7), is(equalTo(1L))); - - tracker.addLatencyMicros(17086); - assertThat(tracker.getLatency(7), is(equalTo(2L))); - - tracker.addLatencyMillis(18); - assertThat(tracker.getLatency(7), is(equalTo(3L))); - } - - /** - * Latencies between 656,842 and 985,261 are in the 18th bucket. - */ - @Test - public void test17ThBucket() { - - tracker.addLatencyMicros(656842); - assertThat(tracker.getLatency(17), is(equalTo(1L))); - - tracker.addLatencyMicros(985261); - assertThat(tracker.getLatency(17), is(equalTo(2L))); - - tracker.addLatencyMillis(985); - assertThat(tracker.getLatency(17), is(equalTo(3L))); - } - -} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/ApacheRequestDecoratorTest.java b/client/src/test/java/io/split/client/utils/ApacheRequestDecoratorTest.java new file mode 100644 index 00000000..5d5971bb --- /dev/null +++ b/client/src/test/java/io/split/client/utils/ApacheRequestDecoratorTest.java @@ -0,0 +1,114 @@ +package io.split.client.utils; + +import io.split.client.CustomHeaderDecorator; +import io.split.client.RequestDecorator; +import io.split.client.dtos.RequestContext; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.ProtocolException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; +import java.util.Arrays; +import java.util.Map; + +public class ApacheRequestDecoratorTest { + + @Test + public void testNoOp() { + ApacheRequestDecorator apacheRequestDecorator = new ApacheRequestDecorator(); + RequestDecorator requestDecorator = new RequestDecorator(null); + HttpGet request = new HttpGet("http://anyhost"); + + request = (HttpGet) apacheRequestDecorator.decorate(request, requestDecorator); + Assert.assertEquals(0, request.getHeaders().length); + request.addHeader("myheader", "value"); + request = (HttpGet) apacheRequestDecorator.decorate(request, requestDecorator); + Assert.assertEquals(1, request.getHeaders().length); + } + + @Test + public void testAddCustomHeaders() throws ProtocolException { + class MyCustomHeaders implements CustomHeaderDecorator { + public MyCustomHeaders() {} + @Override + public Map> getHeaderOverrides(RequestContext context) { + Map> additionalHeaders = context.headers(); + additionalHeaders.put("first", Arrays.asList("1")); + additionalHeaders.put("second", Arrays.asList("2.1", "2.2")); + additionalHeaders.put("third", Arrays.asList("3")); + return additionalHeaders; + } + } + MyCustomHeaders myHeaders = new MyCustomHeaders(); + RequestDecorator decorator = new RequestDecorator(myHeaders); + ApacheRequestDecorator apacheRequestDecorator = new ApacheRequestDecorator(); + + HttpGet request = new HttpGet("http://anyhost"); + request.addHeader("first", "myfirstheader"); + request = (HttpGet) apacheRequestDecorator.decorate(request, decorator); + + Assert.assertEquals(4, request.getHeaders().length); + Assert.assertEquals("1", request.getHeader("first").getValue()); + + Header[] second = request.getHeaders("second"); + Assert.assertEquals("2.1", second[0].getValue()); + Assert.assertEquals("2.2", second[1].getValue()); + Assert.assertEquals("3", request.getHeader("third").getValue()); + + HttpPost request2 = new HttpPost("http://anyhost"); + request2.addHeader("myheader", "value"); + request2 = (HttpPost) apacheRequestDecorator.decorate(request2, decorator); + Assert.assertEquals(5, request2.getHeaders().length); + } + + @Test + public void testAddBlockedHeaders() throws ProtocolException { + class MyCustomHeaders implements CustomHeaderDecorator { + public MyCustomHeaders() {} + @Override + public Map> getHeaderOverrides(RequestContext context) { + Map> additionalHeaders = context.headers(); + additionalHeaders.put("first", Arrays.asList("1")); + additionalHeaders.put("SplitSDKVersion", Arrays.asList("2.4")); + additionalHeaders.put("SplitMachineip", Arrays.asList("xx")); + additionalHeaders.put("splitMachineName", Arrays.asList("xx")); + additionalHeaders.put("splitimpressionsmode", Arrays.asList("xx")); + additionalHeaders.put("HOST", Arrays.asList("xx")); + additionalHeaders.put("referrer", Arrays.asList("xx")); + additionalHeaders.put("content-type", Arrays.asList("xx")); + additionalHeaders.put("content-length", Arrays.asList("xx")); + additionalHeaders.put("content-encoding", Arrays.asList("xx")); + additionalHeaders.put("ACCEPT", Arrays.asList("xx")); + additionalHeaders.put("keep-alive", Arrays.asList("xx")); + additionalHeaders.put("x-fastly-debug", Arrays.asList("xx")); + return additionalHeaders; + } + } + MyCustomHeaders myHeaders = new MyCustomHeaders(); + RequestDecorator decorator = new RequestDecorator(myHeaders); + ApacheRequestDecorator apacheRequestDecorator = new ApacheRequestDecorator(); + HttpGet request = new HttpGet("http://anyhost"); + request = (HttpGet) apacheRequestDecorator.decorate(request, decorator); + Assert.assertEquals(1, request.getHeaders().length); + Assert.assertEquals(null, request.getHeader("SplitSDKVersion")); + } + + @Test(expected = IllegalArgumentException.class) + public void customDecoratorError() { + class MyCustomHeaders implements CustomHeaderDecorator { + public MyCustomHeaders() {} + @Override + public Map> getHeaderOverrides(RequestContext context) { + throw new RuntimeException(); + } + } + MyCustomHeaders myHeaders = new MyCustomHeaders(); + RequestDecorator decorator = new RequestDecorator(myHeaders); + ApacheRequestDecorator apacheRequestDecorator = new ApacheRequestDecorator(); + HttpGet request = new HttpGet("http://anyhost"); + request = (HttpGet) apacheRequestDecorator.decorate(request, decorator); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/CustomDispatcher.java b/client/src/test/java/io/split/client/utils/CustomDispatcher.java new file mode 100644 index 00000000..ea2a2235 --- /dev/null +++ b/client/src/test/java/io/split/client/utils/CustomDispatcher.java @@ -0,0 +1,122 @@ +package io.split.client.utils; + +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.RecordedRequest; +import org.jetbrains.annotations.NotNull; + +import java.io.InputStream; +import java.util.*; + +public class CustomDispatcher extends Dispatcher { + public static final String INITIAL_SPLIT_CHANGES = "/api/splitChanges?s=1.3&since=-1&rbSince=-1"; + public static final String INITIAL_FLAGS_BY_SETS = "/api/splitChanges?s=1.3&since=-1&rbSince=-1&sets=set1%2Cset2"; + public static final String SINCE_1602796638344 = "/api/splitChanges?s=1.3&since=1602796638344&rbSince=-1&sets=set1%2Cset2"; + public static final String AUTH_ENABLED = "/api/auth/enabled?s=1.3"; + public static final String AUTH_DISABLED = "/api/auth/disabled?s=1.3"; + public static final String SINCE_1585948850109 = "/api/splitChanges?s=1.3&since=1585948850109&rbSince=1585948850109"; + public static final String SINCE_1585948850109_FLAG_SET = "/api/splitChanges?s=1.3&since=-1&rbSince=-1&sets=set_1%2Cset_2"; + public static final String SINCE_1585948850110 = "/api/splitChanges?s=1.3&since=1585948850110&rbSince=1585948850110"; + public static final String SINCE_1585948850111 = "/api/splitChanges?s=1.3&since=1585948850111&rbSince=1585948850111"; + public static final String SINCE_1585948850112 = "/api/splitChanges?s=1.3&since=1585948850112&rbSince=1585948850112"; + public static final String SEGMENT_TEST_INITIAL = "/api/segmentChanges/segment-test?since=-1"; + public static final String SEGMENT3_INITIAL = "/api/segmentChanges/segment3?since=-1"; + public static final String SEGMENT3_SINCE_1585948850110 = "/api/segmentChanges/segment3?since=1585948850110"; + public static final String SEGMENT3_SINCE_1585948850111 = "/api/segmentChanges/segment3?since=1585948850111"; + public static final String SEGMENT_BY_FLAG_SET = "/api/segmentChanges/new_segment?since=-1"; + public static final String METRICS_TIME = "/api/metrics/time"; + public static final String METRICS_COUNTER = "api/metrics/counter"; + + private final Map>_responses; + + public CustomDispatcher(Map> responses){ + _responses = responses; + } + + public static CustomDispatcher.Builder builder() { + return new CustomDispatcher.Builder(); + } + + @NotNull + @Override + public MockResponse dispatch(@NotNull RecordedRequest request) { + switch (request.getPath()) { + case CustomDispatcher.INITIAL_SPLIT_CHANGES: + return getResponse(CustomDispatcher.INITIAL_SPLIT_CHANGES, new MockResponse().setBody(inputStreamToString("splits.json"))); + case CustomDispatcher.INITIAL_FLAGS_BY_SETS: + return getResponse(CustomDispatcher.INITIAL_FLAGS_BY_SETS, new MockResponse().setBody("{\"ff\":{\"d\":[{\"trafficTypeName\":\"client\",\"name\":\"workm\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set1\",\"set2\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"client\",\"name\":\"workm_set_3\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set_3\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":1602796638344},\"rbs\":{\"s\":-1,\"t\":-1,\"d\":[]}}")); + case CustomDispatcher.AUTH_ENABLED: + return getResponse(CustomDispatcher.AUTH_ENABLED,new MockResponse().setBody(inputStreamToString("streaming-auth-push-enabled.json"))); + case CustomDispatcher.AUTH_DISABLED: + return getResponse(CustomDispatcher.AUTH_DISABLED,new MockResponse().setBody(inputStreamToString("streaming-auth-push-disabled.json"))); + case CustomDispatcher.SINCE_1585948850109: + return getResponse(CustomDispatcher.SINCE_1585948850109, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}")); + case SINCE_1585948850109_FLAG_SET: + return getResponse(SINCE_1585948850109_FLAG_SET, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}")); + case CustomDispatcher.SINCE_1585948850110: + return getResponse(CustomDispatcher.SINCE_1585948850110, new MockResponse().setBody(inputStreamToString("splits2.json"))); + case CustomDispatcher.SINCE_1585948850111: + return getResponse(CustomDispatcher.SINCE_1585948850111, new MockResponse().setBody(inputStreamToString("splits_killed.json"))); + case CustomDispatcher.SINCE_1585948850112: + return getResponse(CustomDispatcher.SINCE_1585948850112, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850112, \"t\":1585948850112}, \"rbs\":{\"s\":1585948850112,\"t\":1585948850112,\"d\":[]}}")); + case CustomDispatcher.SINCE_1602796638344: + return getResponse(CustomDispatcher.SINCE_1602796638344, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1602796638344, \"t\":1602796638344}, \"rbs\":{\"s\":1602796638344,\"t\":1602796638344,\"d\":[]}}")); + case CustomDispatcher.SEGMENT_TEST_INITIAL: + return getResponse(CustomDispatcher.SEGMENT_TEST_INITIAL, new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": -1,\"till\": -1}")); + case CustomDispatcher.SEGMENT3_INITIAL: + return getResponse(CustomDispatcher.SEGMENT3_INITIAL, new MockResponse().setBody(inputStreamToString("segment3.json"))); + case CustomDispatcher.SEGMENT3_SINCE_1585948850110: + return getResponse(CustomDispatcher.SEGMENT3_SINCE_1585948850110, new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": 1585948850110,\"till\": 1585948850110}")); + case CustomDispatcher.SEGMENT3_SINCE_1585948850111: + return getResponse(CustomDispatcher.SEGMENT3_SINCE_1585948850111, new MockResponse().setBody("{\"name\": \"segment3\",\"added\": [],\"removed\": [],\"since\": 1585948850111,\"till\": 1585948850111}")); + case CustomDispatcher.SEGMENT_BY_FLAG_SET: + return getResponse(CustomDispatcher.SEGMENT3_SINCE_1585948850111, new MockResponse().setBody("{\"name\":\"new_segment\",\"added\":[\"user-1\"],\"removed\":[\"user-2\",\"user-3\"],\"since\":-1,\"till\":-1}")); + case CustomDispatcher.METRICS_TIME: + case CustomDispatcher.METRICS_COUNTER: + return getResponse(CustomDispatcher.METRICS_COUNTER, new MockResponse().setResponseCode(200)); + } + return new MockResponse().setResponseCode(404); + } + + private String inputStreamToString(String fileName) { + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName); + Scanner sc = new Scanner(inputStream); + StringBuffer sb = new StringBuffer(); + while(sc.hasNext()){ + sb.append(sc.nextLine()); + } + + return sb.toString(); + } + + private MockResponse getResponse(String target, MockResponse mockedResponse) { + Queue responses = _responses.get(target); + if(responses != null) { + MockResponse finalResponse = responses.poll(); + return finalResponse == null ? mockedResponse : finalResponse; + } + return mockedResponse; + } + + + + public static final class Builder { + private Map> _responses = new HashMap<>(); + public Builder(){}; + + /** + * Add responses to an specific path + * @param path + * @param responses + * @return + */ + public Builder path(String path, Queue responses) { + _responses.put(path, responses); + return this; + } + + public CustomDispatcher build() { + return new CustomDispatcher(_responses); + } + } +} diff --git a/client/src/test/java/io/split/client/utils/CustomDispatcher2.java b/client/src/test/java/io/split/client/utils/CustomDispatcher2.java new file mode 100644 index 00000000..15979ffc --- /dev/null +++ b/client/src/test/java/io/split/client/utils/CustomDispatcher2.java @@ -0,0 +1,181 @@ +package io.split.client.utils; + +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.RecordedRequest; +import org.jetbrains.annotations.NotNull; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Queue; +import java.util.Scanner; + +public class CustomDispatcher2 extends Dispatcher { + public static final String SPLIT_FETCHER_1 = "/api/splitChanges?s=1.3&since=-1&rbSince=-1"; + public static final String SPLIT_FETCHER_2 = "/api/splitChanges?s=1.3&since=1675095324253&rbSince=1585948850111"; + public static final String SPLIT_FETCHER_3 = "/api/splitChanges?s=1.3&since=1685095324253&rbSince=1585948850111"; + public static final String SPLIT_FETCHER_4 = "/api/splitChanges?s=1.3&since=1695095324253&rbSince=1585948850111"; + public static final String SPLIT_FETCHER_5 = "/api/splitChanges?s=1.3&since=1775095324253&rbSince=1585948850111"; + + private final Map>_responses; + + public CustomDispatcher2(Map> responses){ + _responses = responses; + } + + public static CustomDispatcher2.Builder builder() { + return new CustomDispatcher2.Builder(); + } + + MockResponse response = new MockResponse().setBody("{" + + "\"ff\":{" + + "\"t\":1675095324253," + + "\"s\":-1," + + "\"d\": [{" + + "\"changeNumber\": 123," + + "\"trafficTypeName\": \"user\"," + + "\"name\": \"some_name\"," + + "\"trafficAllocation\": 100," + + "\"trafficAllocationSeed\": 123456," + + "\"seed\": 321654," + + "\"status\": \"ACTIVE\"," + + "\"killed\": false," + + "\"defaultTreatment\": \"off\"," + + "\"algo\": 2," + + "\"conditions\": [" + + "{" + + "\"partitions\": [" + + "{\"treatment\": \"on\", \"size\": 50}," + + "{\"treatment\": \"off\", \"size\": 50}" + + "]," + + "\"contitionType\": \"WHITELIST\"," + + "\"label\": \"some_label\"," + + "\"matcherGroup\": {" + + "\"matchers\": [" + + "{" + + "\"matcherType\": \"WHITELIST\"," + + "\"whitelistMatcherData\": {" + + "\"whitelist\": [\"k1\", \"k2\", \"k3\"]" + + "}," + + "\"negate\": false" + + "}" + + "]," + + "\"combiner\": \"AND\"" + + "}" + + "}," + + "{" + + "\"conditionType\": \"ROLLOUT\"," + + "\"matcherGroup\": {" + + "\"combiner\": \"AND\"," + + "\"matchers\": [" + + "{" + + "\"keySelector\": {" + + "\"trafficType\": \"user\"" + + "}," + + "\"matcherType\": \"IN_RULE_BASED_SEGMENT\"," + + "\"negate\": false," + + "\"userDefinedSegmentMatcherData\": {" + + "\"segmentName\": \"sample_rule_based_segment\"" + + "}" + + "}" + + "]" + + "}," + + "\"partitions\": [" + + "{" + + "\"treatment\": \"on\"," + + "\"size\": 100" + + "}," + + "{" + + "\"treatment\": \"off\"," + + "\"size\": 0" + + "}" + + "]," + + "\"label\": \"in rule based segment sample_rule_based_segment\"" + + "}" + + "]," + + "\"sets\": [\"set1\", \"set2\"]}]" + + "}," + + "\"rbs\": {" + + "\"t\": 1585948850111," + + "\"s\": -1," + + "\"d\": [" + + "{" + + "\"changeNumber\": 5," + + "\"name\": \"sample_rule_based_segment\"," + + "\"status\": \"ACTIVE\"," + + "\"trafficTypeName\": \"user\"," + + "\"excluded\":{" + + "\"keys\":[\"mauro@split.io\",\"gaston@split.io\"]," + + "\"segments\":[]" + + "}," + + "\"conditions\": [" + + "{" + + "\"matcherGroup\": {" + + "\"combiner\": \"AND\"," + + "\"matchers\": [" + + "{" + + "\"keySelector\": {" + + "\"trafficType\": \"user\"," + + "\"attribute\": \"email\"" + + "}," + + "\"matcherType\": \"ENDS_WITH\"," + + "\"negate\": false," + + "\"whitelistMatcherData\": {" + + "\"whitelist\": [" + + "\"@split.io\"" + + "]}}]}}]}]}}"); + MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1675095324253, \"t\":1675095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1685095324253, \"t\":1695095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response4 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1695095324253, \"t\":1775095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response5 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1775095324253, \"t\":1775095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + + @NotNull + @Override + public MockResponse dispatch(@NotNull RecordedRequest request) { + switch (request.getPath()) { + case CustomDispatcher2.SPLIT_FETCHER_1: + return getResponse(CustomDispatcher2.SPLIT_FETCHER_1, response); + case CustomDispatcher2.SPLIT_FETCHER_2: + return getResponse(CustomDispatcher2.SPLIT_FETCHER_2, response2); + case CustomDispatcher2.SPLIT_FETCHER_3: + return getResponse(CustomDispatcher2.SPLIT_FETCHER_3, response3); + case CustomDispatcher2.SPLIT_FETCHER_4: + return getResponse(CustomDispatcher2.SPLIT_FETCHER_4, response4); + case CustomDispatcher2.SPLIT_FETCHER_5: + return getResponse(CustomDispatcher2.SPLIT_FETCHER_5, response5); + } + return new MockResponse().setResponseCode(404); + } + + private MockResponse getResponse(String target, MockResponse mockedResponse) { + Queue responses = _responses.get(target); + if(responses != null) { + MockResponse finalResponse = responses.poll(); + return finalResponse == null ? mockedResponse : finalResponse; + } + return mockedResponse; + } + + + + public static final class Builder { + private Map> _responses = new HashMap<>(); + public Builder(){}; + + /** + * Add responses to an specific path + * @param path + * @param responses + * @return + */ + public Builder path(String path, Queue responses) { + _responses.put(path, responses); + return this; + } + + public CustomDispatcher2 build() { + return new CustomDispatcher2(_responses); + } + } +} diff --git a/client/src/test/java/io/split/client/utils/FeatureFlagProcessorTest.java b/client/src/test/java/io/split/client/utils/FeatureFlagProcessorTest.java new file mode 100644 index 00000000..7a1b774a --- /dev/null +++ b/client/src/test/java/io/split/client/utils/FeatureFlagProcessorTest.java @@ -0,0 +1,81 @@ +package io.split.client.utils; + +import io.split.client.dtos.Split; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.engine.experiments.SplitParser; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import static io.split.client.utils.FeatureFlagProcessor.processFeatureFlagChanges; + +public class FeatureFlagProcessorTest { + + @Test + public void testProcessFeatureFlagChanges() { + SplitParser splitParser = new SplitParser(); + List featureFlags = new ArrayList<>(); + + String definition1 = "{\"trafficTypeName\":\"user\",\"id\":\"d431cdd0-b0be-11ea-8a80-1660ada9ce39\",\"name\":\"mauro_java\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-92391491,\"seed\":-1769377604,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1684329854385,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"WHITELIST\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"matcherType\":\"WHITELIST\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"admin\",\"mauro\",\"nico\"]}}]},\"partitions\":[{\"treatment\":\"off\",\"size\":100}],\"label\":\"whitelisted\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"maur-2\"}}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"in segment maur-2\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"default rule\"}]}"; + Split featureFlagTest1 = Json.fromJson(definition1, Split.class); + + String definition2 = "{\"trafficTypeName\":\"user\",\"id\":\"d704f220-0567-11ee-80ee-fa3c6460cd13\",\"name\":\"NET_CORE_getTreatmentWithConfigAfterArchive\",\"trafficAllocation\":100,\"trafficAllocationSeed\":179018541,\"seed\":272707374,\"status\":\"ARCHIVED\",\"killed\":false,\"defaultTreatment\":\"V-FGyN\",\"changeNumber\":1686165617166,\"algo\":2,\"configurations\":{\"V-FGyN\":\"{\\\"color\\\":\\\"blue\\\"}\",\"V-YrWB\":\"{\\\"color\\\":\\\"red\\\"}\"},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"test\"},\"matcherType\":\"LESS_THAN_OR_EQUAL_TO\",\"negate\":false,\"unaryNumericMatcherData\":{\"dataType\":\"NUMBER\",\"value\":20}}]},\"partitions\":[{\"treatment\":\"V-FGyN\",\"size\":0},{\"treatment\":\"V-YrWB\",\"size\":100}],\"label\":\"test \\u003c\\u003d 20\"}]}"; + Split featureFlagTest2 = Json.fromJson(definition2, Split.class); + + featureFlags.add(featureFlagTest1); + featureFlags.add(featureFlagTest2); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(splitParser, featureFlags, flagSetsFilter); + + Assert.assertEquals(1, featureFlagsToUpdate.toAdd.size()); + Assert.assertEquals(1, featureFlagsToUpdate.toRemove.size()); + Assert.assertEquals(1, featureFlagsToUpdate.segments.size()); + } + + @Test + public void testProcessFeatureFlagChangesWithSetsToAdd() { + SplitParser splitParser = new SplitParser(); + List featureFlags = new ArrayList<>(); + + String definition1 = "{\"trafficTypeName\":\"user\",\"id\":\"d431cdd0-b0be-11ea-8a80-1660ada9ce39\",\"name\":\"mauro_java\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-92391491,\"seed\":-1769377604,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1684329854385,\"algo\":2,\"configurations\":{},\"sets\":[\"set_1\",\"set_2\"],\"conditions\":[{\"conditionType\":\"WHITELIST\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"matcherType\":\"WHITELIST\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"admin\",\"mauro\",\"nico\"]}}]},\"partitions\":[{\"treatment\":\"off\",\"size\":100}],\"label\":\"whitelisted\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"maur-2\"}}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"in segment maur-2\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"default rule\"}]}"; + Split featureFlagTest1 = Json.fromJson(definition1, Split.class); + + String definition2 = "{\"trafficTypeName\":\"user\",\"id\":\"d704f220-0567-11ee-80ee-fa3c6460cd13\",\"name\":\"NET_CORE_getTreatmentWithConfigAfterArchive\",\"trafficAllocation\":100,\"trafficAllocationSeed\":179018541,\"seed\":272707374,\"status\":\"ARCHIVED\",\"killed\":false,\"defaultTreatment\":\"V-FGyN\",\"changeNumber\":1686165617166,\"algo\":2,\"configurations\":{\"V-FGyN\":\"{\\\"color\\\":\\\"blue\\\"}\",\"V-YrWB\":\"{\\\"color\\\":\\\"red\\\"}\"},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"test\"},\"matcherType\":\"LESS_THAN_OR_EQUAL_TO\",\"negate\":false,\"unaryNumericMatcherData\":{\"dataType\":\"NUMBER\",\"value\":20}}]},\"partitions\":[{\"treatment\":\"V-FGyN\",\"size\":0},{\"treatment\":\"V-YrWB\",\"size\":100}],\"label\":\"test \\u003c\\u003d 20\"}]}"; + Split featureFlagTest2 = Json.fromJson(definition2, Split.class); + + featureFlags.add(featureFlagTest1); + featureFlags.add(featureFlagTest2); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_1"))); + FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(splitParser, featureFlags, flagSetsFilter); + + Assert.assertEquals(1, featureFlagsToUpdate.toAdd.size()); + Assert.assertEquals(1, featureFlagsToUpdate.toRemove.size()); + Assert.assertEquals(1, featureFlagsToUpdate.segments.size()); + } + + @Test + public void testProcessFeatureFlagChangesWithSetsToRemove() { + SplitParser splitParser = new SplitParser(); + List featureFlags = new ArrayList<>(); + + String definition1 = "{\"trafficTypeName\":\"user\",\"id\":\"d431cdd0-b0be-11ea-8a80-1660ada9ce39\",\"name\":\"mauro_java\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-92391491,\"seed\":-1769377604,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1684329854385,\"algo\":2,\"configurations\":{},\"sets\":[\"set_1\",\"set_2\"],\"conditions\":[{\"conditionType\":\"WHITELIST\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"matcherType\":\"WHITELIST\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"admin\",\"mauro\",\"nico\"]}}]},\"partitions\":[{\"treatment\":\"off\",\"size\":100}],\"label\":\"whitelisted\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"maur-2\"}}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"in segment maur-2\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"default rule\"}]}"; + Split featureFlagTest1 = Json.fromJson(definition1, Split.class); + + String definition2 = "{\"trafficTypeName\":\"user\",\"id\":\"d704f220-0567-11ee-80ee-fa3c6460cd13\",\"name\":\"NET_CORE_getTreatmentWithConfigAfterArchive\",\"trafficAllocation\":100,\"trafficAllocationSeed\":179018541,\"seed\":272707374,\"status\":\"ARCHIVED\",\"killed\":false,\"defaultTreatment\":\"V-FGyN\",\"changeNumber\":1686165617166,\"algo\":2,\"configurations\":{\"V-FGyN\":\"{\\\"color\\\":\\\"blue\\\"}\",\"V-YrWB\":\"{\\\"color\\\":\\\"red\\\"}\"},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"test\"},\"matcherType\":\"LESS_THAN_OR_EQUAL_TO\",\"negate\":false,\"unaryNumericMatcherData\":{\"dataType\":\"NUMBER\",\"value\":20}}]},\"partitions\":[{\"treatment\":\"V-FGyN\",\"size\":0},{\"treatment\":\"V-YrWB\",\"size\":100}],\"label\":\"test \\u003c\\u003d 20\"}]}"; + Split featureFlagTest2 = Json.fromJson(definition2, Split.class); + + featureFlags.add(featureFlagTest1); + featureFlags.add(featureFlagTest2); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_3"))); + FeatureFlagsToUpdate featureFlagsToUpdate = processFeatureFlagChanges(splitParser, featureFlags, flagSetsFilter); + + Assert.assertEquals(0, featureFlagsToUpdate.toAdd.size()); + Assert.assertEquals(2, featureFlagsToUpdate.toRemove.size()); + Assert.assertEquals(0, featureFlagsToUpdate.segments.size()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/FileInputStreamProviderTest.java b/client/src/test/java/io/split/client/utils/FileInputStreamProviderTest.java new file mode 100644 index 00000000..5c9e34d2 --- /dev/null +++ b/client/src/test/java/io/split/client/utils/FileInputStreamProviderTest.java @@ -0,0 +1,13 @@ +package io.split.client.utils; + +import io.split.client.exceptions.InputStreamProviderException; +import org.junit.Test; + +public class FileInputStreamProviderTest { + + @Test(expected = InputStreamProviderException.class) + public void processTestForException() throws InputStreamProviderException { + FileInputStreamProvider fileInputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json"); + fileInputStreamProvider.get(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/FlagSetsValidatorTest.java b/client/src/test/java/io/split/client/utils/FlagSetsValidatorTest.java new file mode 100644 index 00000000..444de28c --- /dev/null +++ b/client/src/test/java/io/split/client/utils/FlagSetsValidatorTest.java @@ -0,0 +1,78 @@ +package io.split.client.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static io.split.inputValidation.FlagSetsValidator.cleanup; + +public class FlagSetsValidatorTest { + + @Test + public void testEmptyFlagSets() { + List flagSets = new ArrayList<>(); + Assert.assertTrue(cleanup(flagSets).isEmpty()); + } + + @Test + public void testUpperFlagSets() { + List flagSets = new ArrayList<>(); + flagSets.add("Test1"); + flagSets.add("TEST2"); + Set cleanFlagSets = cleanup(flagSets); + Assert.assertTrue(cleanFlagSets.contains("test1")); + Assert.assertTrue(cleanFlagSets.contains("test2")); + } + + @Test + public void testTrimFlagSets() { + List flagSets = new ArrayList<>(); + flagSets.add(" test1"); + flagSets.add(" test2 "); + Set cleanFlagSets = cleanup(flagSets); + Assert.assertTrue(cleanFlagSets.contains("test1")); + Assert.assertTrue(cleanFlagSets.contains("test2")); + } + + @Test + public void testRegexFlagSets() { + List flagSets = new ArrayList<>(); + flagSets.add(" test1"); + flagSets.add(" test-2 "); + Set cleanFlagSets = cleanup(flagSets); + Assert.assertEquals(1, cleanFlagSets.size()); + Assert.assertTrue(cleanFlagSets.contains("test1")); + Assert.assertFalse(cleanFlagSets.contains("test-2")); + } + + @Test + public void testDuplicateFlagSets() { + List flagSets = new ArrayList<>(); + flagSets.add(" test1"); + flagSets.add(" test1 "); + Set cleanFlagSets = cleanup(flagSets); + Assert.assertEquals(1, cleanFlagSets.size()); + Assert.assertTrue(cleanFlagSets.contains("test1")); + } + + @Test + public void testFlagSetsInOrder() { + List flagSets = new ArrayList<>(); + flagSets.add(" test3"); + flagSets.add(" test2"); + flagSets.add(" test1 "); + flagSets.add(" 1test "); + flagSets.add(" 2test "); + Set cleanFlagSets = cleanup(flagSets); + Assert.assertEquals(5, cleanFlagSets.size()); + List sets = new ArrayList<>(cleanFlagSets); + Assert.assertEquals("1test", sets.get(0)); + Assert.assertEquals("2test", sets.get(1)); + Assert.assertEquals("test1", sets.get(2)); + Assert.assertEquals("test2", sets.get(3)); + Assert.assertEquals("test3", sets.get(4)); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/LocalhostSegmentChangeFetcherTest.java b/client/src/test/java/io/split/client/utils/LocalhostSegmentChangeFetcherTest.java new file mode 100644 index 00000000..b43388fc --- /dev/null +++ b/client/src/test/java/io/split/client/utils/LocalhostSegmentChangeFetcherTest.java @@ -0,0 +1,150 @@ +package io.split.client.utils; + +import io.split.client.LocalhostSegmentChangeFetcher; +import io.split.client.dtos.SegmentChange; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import java.io.File; +import java.io.IOException; + +public class LocalhostSegmentChangeFetcherTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private String TEST_0 = "{\"name\":\"segment_test\",\"added\":[\"user-1\"],\"removed\":[],\"since\":-1,\"till\":-1}"; + private String TEST_1 = "{\"name\":\"segment_test\",\"added\":[\"user-1\"],\"removed\":[\"user-2\"],\"since\":-1,\"till\":-1}"; + private String TEST_2 = "{\"name\":\"segment_test\",\"added\":[\"user-1\"],\"removed\":[\"user-2\"],\"since\":-1,\"till\":2323}"; + private String TEST_3 = "{\"name\":\"segment_test\",\"added\":[\"user-1\",\"user-3\"],\"removed\":[\"user-2\"],\"since\":-1,\"till\":2323}"; + private String TEST_4 = "{\"name\":\"segment_test\",\"added\":[\"user-1\",\"user-3\"],\"removed\":[\"user-2\"],\"since\":-1,\"till\":445345}"; + private String TEST_5 = "{\"name\":\"segment_test\",\"added\":[\"user-1\"],\"removed\":[\"user-2\",\"user-3\"],\"since\":-1,\"till\":-1}"; + + @Test + public void testSegmentFetch() { + LocalhostSegmentChangeFetcher localhostSegmentChangeFetcher = new LocalhostSegmentChangeFetcher("src/test/resources/"); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SegmentChange segmentChange = localhostSegmentChangeFetcher.fetch("segment_1", -1L, fetchOptions); + + Assert.assertEquals("segment_1", segmentChange.name); + Assert.assertEquals(4, segmentChange.added.size()); + } + + @Test + public void testSegmentNameNull() { + LocalhostSegmentChangeFetcher localhostSegmentChangeFetcher = new LocalhostSegmentChangeFetcher("src/test/resources/sanitizer/"); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SegmentChange segmentChange = localhostSegmentChangeFetcher.fetch("segmentNameNull", -1L, fetchOptions); + + Assert.assertNull(segmentChange); + } + + @Test + public void sameInAddedAndRemoved() { + LocalhostSegmentChangeFetcher localhostSegmentChangeFetcher = new LocalhostSegmentChangeFetcher("src/test/resources/sanitizer/"); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SegmentChange segmentChange = localhostSegmentChangeFetcher.fetch("sameInAddedAndRemoved", -1L, fetchOptions); + + Assert.assertEquals(0, segmentChange.removed.size()); + Assert.assertEquals(4, segmentChange.added.size()); + } + + @Test + public void checkTillAndSince() { + LocalhostSegmentChangeFetcher localhostSegmentChangeFetcher = new LocalhostSegmentChangeFetcher("src/test/resources/sanitizer/"); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + SegmentChange segmentChange = localhostSegmentChangeFetcher.fetch("segmentChangeSinceTill", -1L, fetchOptions); + + Assert.assertEquals(-1L, segmentChange.till); + Assert.assertEquals(-1L, segmentChange.since); + } + + @Test + public void testProcessSegmentFetch() throws IOException { + File file = folder.newFile("segment_test.json"); + + byte[] test = TEST_0.getBytes(); + com.google.common.io.Files.write(test, file); + + LocalhostSegmentChangeFetcher localhostSplitChangeFetcher = new LocalhostSegmentChangeFetcher(folder.getRoot().getAbsolutePath()); + FetchOptions fetchOptions = Mockito.mock(FetchOptions.class); + + // 0) The CN from storage is -1, till and since are -1, and sha doesn't exist in the hash. It's going to return a segment change with updates. + SegmentChange segmentChange = localhostSplitChangeFetcher.fetch("segment_test",-1L, fetchOptions); + + Assert.assertEquals(1, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertEquals(0, segmentChange.removed.size()); + Assert.assertEquals(-1, segmentChange.till); + Assert.assertEquals(-1, segmentChange.since); + + test = TEST_1.getBytes(); + com.google.common.io.Files.write(test, file); + + // 1) The CN from storage is -1, till and since are -1, and sha is different than before. It's going to return a segment change with updates. + segmentChange = localhostSplitChangeFetcher.fetch("segment_test",-1L, fetchOptions); + Assert.assertEquals(1, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertEquals(1, segmentChange.removed.size()); + Assert.assertTrue(segmentChange.removed.contains("user-2")); + Assert.assertEquals(-1, segmentChange.till); + Assert.assertEquals(-1, segmentChange.since); + + test = TEST_2.getBytes(); + com.google.common.io.Files.write(test, file); + + // 2) The CN from storage is -1, till is 2323, and since is -1, and sha is the same as before. It's going to return a segment change with the same data. + segmentChange = localhostSplitChangeFetcher.fetch("segment_test",-1L, fetchOptions); + Assert.assertEquals(1, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertEquals(1, segmentChange.removed.size()); + Assert.assertTrue(segmentChange.removed.contains("user-2")); + Assert.assertEquals(-1, segmentChange.till); + Assert.assertEquals(-1, segmentChange.since); + + test = TEST_3.getBytes(); + com.google.common.io.Files.write(test, file); + + // 3) The CN from storage is -1, till is 2323, and since is -1, sha is different than before. It's going to return a segment change with updates. + segmentChange = localhostSplitChangeFetcher.fetch("segment_test",-1L, fetchOptions); + Assert.assertEquals(2, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertEquals(1, segmentChange.removed.size()); + Assert.assertTrue(segmentChange.removed.contains("user-2")); + Assert.assertEquals(2323, segmentChange.till); + Assert.assertEquals(-1, segmentChange.since); + + test = TEST_4.getBytes(); + com.google.common.io.Files.write(test, file); + + // 4) The CN from storage is 2323, till is 445345, and since is -1, and sha is the same as before. It's going to return a segment change with same data. + segmentChange = localhostSplitChangeFetcher.fetch("segment_test",2323, fetchOptions); + Assert.assertEquals(2, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertTrue(segmentChange.added.contains("user-3")); + Assert.assertEquals(1, segmentChange.removed.size()); + Assert.assertTrue(segmentChange.removed.contains("user-2")); + Assert.assertEquals(2323, segmentChange.till); + Assert.assertEquals(2323, segmentChange.since); + + test = TEST_5.getBytes(); + com.google.common.io.Files.write(test, file); + + // 5) The CN from storage is 2323, till and since are -1, and sha is different than before. It's going to return a segment change with updates. + segmentChange = localhostSplitChangeFetcher.fetch("segment_test",2323, fetchOptions); + Assert.assertEquals(1, segmentChange.added.size()); + Assert.assertTrue(segmentChange.added.contains("user-1")); + Assert.assertFalse(segmentChange.added.contains("user-3")); + Assert.assertEquals(2, segmentChange.removed.size()); + Assert.assertTrue(segmentChange.removed.contains("user-2")); + Assert.assertTrue(segmentChange.removed.contains("user-3")); + Assert.assertEquals(2323, segmentChange.till); + Assert.assertEquals(2323, segmentChange.since); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/client/utils/LocalhostUtils.java b/client/src/test/java/io/split/client/utils/LocalhostUtils.java new file mode 100644 index 00000000..de507bd4 --- /dev/null +++ b/client/src/test/java/io/split/client/utils/LocalhostUtils.java @@ -0,0 +1,46 @@ +package io.split.client.utils; + +import io.split.client.LocalhostSplit; +import io.split.client.SplitAndKey; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.util.Map; + +public class LocalhostUtils { + + public static void writeFile(File f, StringWriter content) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(f)); + writer.write(content.toString()); + writer.flush(); + writer.close(); + } + + public static void writeFile(File f, Map map) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(f)); + + for (Map.Entry entry : map.entrySet()) { + String line = toString(entry); + writer.write(line); + } + + writer.flush(); + writer.close(); + } + + private static String toString(Map.Entry entry) { + StringBuilder bldr = new StringBuilder(); + bldr.append(entry.getKey().split()); + bldr.append(' '); + bldr.append(entry.getValue().treatment); + if (entry.getKey().key() != null) { + bldr.append(' '); + bldr.append(entry.getKey().key()); + } + bldr.append('\n'); + return bldr.toString(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/ConsumerSyncManagerTest.java b/client/src/test/java/io/split/engine/common/ConsumerSyncManagerTest.java new file mode 100644 index 00000000..d01184f8 --- /dev/null +++ b/client/src/test/java/io/split/engine/common/ConsumerSyncManagerTest.java @@ -0,0 +1,18 @@ +package io.split.engine.common; + +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; + +public class ConsumerSyncManagerTest { + @Test + public void testStartAndShutdown() throws IOException { + Synchronizer redisSynchronizer = Mockito.mock(ConsumerSynchronizer.class); + ConsumerSyncManager imp = new ConsumerSyncManager(redisSynchronizer); + imp.start(); + Mockito.verify(redisSynchronizer, Mockito.times(1)).startPeriodicDataRecording(); + imp.shutdown(); + Mockito.verify(redisSynchronizer, Mockito.times(1)).stopPeriodicDataRecording(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/ConsumerSynchronizerTest.java b/client/src/test/java/io/split/engine/common/ConsumerSynchronizerTest.java new file mode 100644 index 00000000..3c488fab --- /dev/null +++ b/client/src/test/java/io/split/engine/common/ConsumerSynchronizerTest.java @@ -0,0 +1,30 @@ +package io.split.engine.common; + +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import org.junit.Test; +import org.mockito.Mockito; + +public class ConsumerSynchronizerTest { + + @Test + public void testDataRecording() { + ImpressionsManager impressionsManager = Mockito.mock(ImpressionsManager.class); + UniqueKeysTracker uniqueKeysTracker = Mockito.mock(UniqueKeysTracker.class); + TelemetrySyncTask telemetrySyncTask = Mockito.mock(TelemetrySyncTask.class); + SplitTasks splitTasks = SplitTasks.build(null, null, impressionsManager, null, telemetrySyncTask, uniqueKeysTracker); + Synchronizer imp = new ConsumerSynchronizer(splitTasks); + imp.startPeriodicDataRecording(); + + Mockito.verify(impressionsManager, Mockito.times(1)).start(); + Mockito.verify(uniqueKeysTracker, Mockito.times(1)).start(); + Mockito.verify(telemetrySyncTask, Mockito.times(1)).startScheduledTask(); + + imp.stopPeriodicDataRecording(); + + Mockito.verify(impressionsManager, Mockito.times(1)).close(); + Mockito.verify(uniqueKeysTracker, Mockito.times(1)).stop(); + Mockito.verify(telemetrySyncTask, Mockito.times(1)).stopScheduledTask(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/FastlyHeadersCaptorTest.java b/client/src/test/java/io/split/engine/common/FastlyHeadersCaptorTest.java new file mode 100644 index 00000000..7fd4a410 --- /dev/null +++ b/client/src/test/java/io/split/engine/common/FastlyHeadersCaptorTest.java @@ -0,0 +1,62 @@ +package io.split.engine.common; + +import org.junit.Test; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class FastlyHeadersCaptorTest { + + @Test + public void filterWorks() { + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); + captor.handle(Stream.of(new String[][] { + {"Fastly-Debug-Path", "something"}, + {"Fastly-Debug-TTL", "something"}, + {"Fastly-Debug-Digest", "something"}, + {"X-Served-By", "something"}, + {"X-Cache", "something"}, + {"X-Cache-Hits", "something"}, + {"X-Timer", "something"}, + {"Surrogate-Key", "something"}, + {"ETag", "something"}, + {"Cache-Control", "something"}, + {"X-Request-ID", "something"}, + {"Last-Modified", "something"}, + {"NON_IMPORTANT_1", "something"}, + {"ANOTHER_NON_IMPORTANT", "something"} + }).collect(Collectors.toMap(d -> d[0], d -> d[1]))); + + assertEquals(captor.get().size(), 1); + assertEquals(captor.get().get(0).size(), 12); + assertFalse(captor.get().get(0).containsKey("NON_IMPORTANT_1")); + assertFalse(captor.get().get(0).containsKey("ANOTHER_NON_IMPORTANT")); + } + + @Test + public void orderIsPreserved() { + FastlyHeadersCaptor captor = new FastlyHeadersCaptor(); + captor.handle(Stream.of(new String[][]{ + {"Fastly-Debug-Path", "first"}, + }).collect(Collectors.toMap(d -> d[0], d -> d[1]))); + + captor.handle(Stream.of(new String[][]{ + {"Fastly-Debug-Path", "second"}, + }).collect(Collectors.toMap(d -> d[0], d -> d[1]))); + + captor.handle(Stream.of(new String[][]{ + {"Fastly-Debug-Path", "third"}, + }).collect(Collectors.toMap(d -> d[0], d -> d[1]))); + + assertEquals(captor.get().size(), 3); + assertEquals(captor.get().get(0).size(), 1); + assertEquals(captor.get().get(1).size(), 1); + assertEquals(captor.get().get(2).size(), 1); + assertEquals(captor.get().get(0).get("Fastly-Debug-Path"), "first"); + assertEquals(captor.get().get(1).get("Fastly-Debug-Path"), "second"); + assertEquals(captor.get().get(2).get("Fastly-Debug-Path"), "third"); + } +} diff --git a/client/src/test/java/io/split/engine/common/FetcherOptionsTest.java b/client/src/test/java/io/split/engine/common/FetcherOptionsTest.java new file mode 100644 index 00000000..19a1d45e --- /dev/null +++ b/client/src/test/java/io/split/engine/common/FetcherOptionsTest.java @@ -0,0 +1,21 @@ +package io.split.engine.common; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FetcherOptionsTest { + + @Test + public void optionsPropagatedOk() { + FetchOptions options = new FetchOptions.Builder() + .cacheControlHeaders(true) + .targetChangeNumber(123) + .flagSetsFilter("set1,set2") + .build(); + + assertEquals(options.cacheControlHeadersEnabled(), true); + assertEquals(options.targetCN(), 123); + assertEquals("set1,set2", options.flagSetsFilter()); + } +} diff --git a/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java new file mode 100644 index 00000000..04163aed --- /dev/null +++ b/client/src/test/java/io/split/engine/common/LocalhostSynchronizerTest.java @@ -0,0 +1,105 @@ +package io.split.engine.common; + +import io.split.client.LocalhostSegmentChangeFetcher; +import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.utils.FileInputStreamProvider; +import io.split.client.utils.InputStreamProvider; +import io.split.engine.experiments.*; +import io.split.engine.segments.SegmentChangeFetcher; +import io.split.engine.segments.SegmentSynchronizationTaskImp; +import io.split.storages.*; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import io.split.telemetry.storage.NoopTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.HashSet; + +public class LocalhostSynchronizerTest { + + private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); + + @Test + public void testSyncAll(){ + SplitCache splitCacheProducer = new InMemoryCacheImp(FLAG_SETS_FILTER); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/split_init.json"); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCache); + + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000L, null); + + SegmentChangeFetcher segmentChangeFetcher = new LocalhostSegmentChangeFetcher("src/test/resources/"); + SegmentCacheProducer segmentCacheProducer = new SegmentCacheInMemoryImpl(); + + SegmentSynchronizationTaskImp segmentSynchronizationTaskImp = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1000, 1, segmentCacheProducer, + TELEMETRY_STORAGE_NOOP, splitCacheProducer, null, ruleBasedSegmentCache); + SplitTasks splitTasks = SplitTasks.build(splitSynchronizationTask, segmentSynchronizationTaskImp, null, null, null, null); + + LocalhostSynchronizer localhostSynchronizer = new LocalhostSynchronizer(splitTasks, splitFetcher, false); + + Assert.assertTrue(localhostSynchronizer.syncAll()); + } + + @Test + public void testPeriodicFetching() throws InterruptedException { + SplitCache splitCacheProducer = new InMemoryCacheImp(FLAG_SETS_FILTER); + + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCache); + + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000L, null); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(LocalhostSegmentChangeFetcher.class); + SegmentCacheProducer segmentCacheProducer = new SegmentCacheInMemoryImpl(); + + SegmentSynchronizationTaskImp segmentSynchronizationTaskImp = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1000, 1, segmentCacheProducer, + TELEMETRY_STORAGE_NOOP, splitCacheProducer, null, ruleBasedSegmentCache); + + SplitTasks splitTasks = SplitTasks.build(splitSynchronizationTask, segmentSynchronizationTaskImp, null, null, null, null); + LocalhostSynchronizer localhostSynchronizer = new LocalhostSynchronizer(splitTasks, splitFetcher, true); + + localhostSynchronizer.startPeriodicFetching(); + + Thread.sleep(2000); + + Mockito.verify(splitChangeFetcher, Mockito.times(1)).fetch(-1, -1, fetchOptions); + } + + @Test + public void testRefreshSplits() { + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(FLAG_SETS_FILTER); + SplitChangeFetcher splitChangeFetcher = Mockito.mock(SplitChangeFetcher.class); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000L, null); + SplitTasks splitTasks = SplitTasks.build(splitSynchronizationTask, null, null, null, null, null); + LocalhostSynchronizer localhostSynchronizer = new LocalhostSynchronizer(splitTasks, splitFetcher, false); + + localhostSynchronizer.refreshSplits(null, null); + + Mockito.verify(splitChangeFetcher, Mockito.times(1)).fetch(Mockito.anyLong(), Mockito.anyLong(), Mockito.anyObject()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/PushManagerTest.java b/client/src/test/java/io/split/engine/common/PushManagerTest.java index 4662ddab..33ce1341 100644 --- a/client/src/test/java/io/split/engine/common/PushManagerTest.java +++ b/client/src/test/java/io/split/engine/common/PushManagerTest.java @@ -7,31 +7,40 @@ import io.split.engine.sse.client.SSEClient; import io.split.engine.sse.dtos.AuthenticationResponse; import io.split.engine.sse.workers.SegmentsWorkerImp; -import io.split.engine.sse.workers.SplitsWorker; +import io.split.engine.sse.workers.FeatureFlagsWorker; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import java.util.concurrent.LinkedBlockingQueue; - public class PushManagerTest { private AuthApiClient _authApiClient; private EventSourceClient _eventSourceClient; private Backoff _backoff; private PushManager _pushManager; private PushStatusTracker _pushStatusTracker; + private TelemetryStorage _telemetryStorage; + private FeatureFlagsWorker _featureFlagsWorker; + private SegmentsWorkerImp _segmentsWorkerImp; @Before public void setUp() { + _featureFlagsWorker = Mockito.mock(FeatureFlagsWorker.class); + _segmentsWorkerImp = Mockito.mock(SegmentsWorkerImp.class); _authApiClient = Mockito.mock(AuthApiClient.class); _eventSourceClient = Mockito.mock(EventSourceClient.class); _backoff = Mockito.mock(Backoff.class); _pushStatusTracker = Mockito.mock(PushStatusTrackerImp.class); + _telemetryStorage = new InMemoryTelemetryStorage(); _pushManager = new PushManagerImp(_authApiClient, _eventSourceClient, - Mockito.mock(SplitsWorker.class), - Mockito.mock(SegmentsWorkerImp.class), - _pushStatusTracker); + _featureFlagsWorker, + _segmentsWorkerImp, + _pushStatusTracker, + _telemetryStorage, + null); } @Test @@ -58,6 +67,7 @@ public void startWithPushEnabledShouldConnect() throws InterruptedException { Mockito.verify(_pushStatusTracker, Mockito.times(0)).handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); Mockito.verify(_pushStatusTracker, Mockito.times(0)).forcePushDisable(); + Assert.assertEquals(1, _telemetryStorage.popStreamingEvents().size()); } @Test @@ -101,4 +111,60 @@ public void startWithPushDisabledAndRetryTrueShouldConnect() throws InterruptedE Thread.sleep(1500); Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); } -} + + + @Test + public void startAndStop() throws InterruptedException { + AuthenticationResponse response = new AuthenticationResponse(true, "token-test", "channels-test", 1, false); + + Mockito.when(_authApiClient.Authenticate()) + .thenReturn(response); + + Mockito.when(_eventSourceClient.start(response.getChannels(), response.getToken())) + .thenReturn(true); + + _pushManager.start(); + + Mockito.verify(_authApiClient, Mockito.times(1)).Authenticate(); + Mockito.verify(_eventSourceClient, Mockito.times(1)).start(response.getChannels(), response.getToken()); + + Thread.sleep(1500); + + Mockito.verify(_pushStatusTracker, Mockito.times(0)).handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); + Mockito.verify(_pushStatusTracker, Mockito.times(0)).forcePushDisable(); + Assert.assertEquals(1, _telemetryStorage.popStreamingEvents().size()); + + _pushManager.stop(); + + Mockito.verify(_eventSourceClient, Mockito.times(1)).stop(); + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).stop(); + Mockito.verify(_segmentsWorkerImp, Mockito.times(1)).stop(); + } + + @Test + public void validateStartWorkers() { + _pushManager.startWorkers(); + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).start(); + Mockito.verify(_segmentsWorkerImp, Mockito.times(1)).start(); + } + + @Test + public void validateScheduleConnectionReset() throws InterruptedException { + AuthenticationResponse response = new AuthenticationResponse(false, "token-test", "channels-test", 3, false); + + Mockito.when(_authApiClient.Authenticate()) + .thenReturn(response); + + Mockito.when(_eventSourceClient.start(response.getChannels(), response.getToken())) + .thenReturn(true); + + _pushManager.start(); + + _pushManager.scheduleConnectionReset(); + Thread.sleep(1000); + + Mockito.verify(_eventSourceClient, Mockito.times(3)).stop(); + Mockito.verify(_featureFlagsWorker, Mockito.times(3)).stop(); + Mockito.verify(_segmentsWorkerImp, Mockito.times(3)).stop(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/SyncManagerReadyEventsTest.java b/client/src/test/java/io/split/engine/common/SyncManagerReadyEventsTest.java new file mode 100644 index 00000000..afa52244 --- /dev/null +++ b/client/src/test/java/io/split/engine/common/SyncManagerReadyEventsTest.java @@ -0,0 +1,103 @@ +package io.split.engine.common; + +import io.split.client.SplitClientConfig; +import io.split.client.api.SdkEventMetadata; +import io.split.client.events.EventsTask; +import io.split.client.impressions.ImpressionsManagerImpl; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.SDKReadinessGates; +import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.segments.SegmentSynchronizationTaskImp; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.util.concurrent.LinkedBlockingQueue; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +public class SyncManagerReadyEventsTest { + + private static void assertReadyMetadata(SdkEventsNotifier notifier) { + ArgumentCaptor metadata = ArgumentCaptor.forClass(SdkEventMetadata.class); + verify(notifier, timeout(2000).times(1)).notify(eq(SdkInternalEvent.SDK_READY), metadata.capture()); + assertTrue(metadata.getValue().isInitialCacheLoad()); + assertNull(metadata.getValue().getLastUpdateTimestamp()); + } + + @Test + public void standaloneSyncManagerNotifiesSdkReadyOnceAfterSyncAll() throws Exception { + Synchronizer synchronizer = mock(Synchronizer.class); + when(synchronizer.syncAll()).thenReturn(true); + SDKReadinessGates gates = new SDKReadinessGates(); + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + SplitTasks tasks = SplitTasks.build(mock(SplitSynchronizationTask.class), mock(SegmentSynchronizationTaskImp.class), + mock(ImpressionsManagerImpl.class), mock(EventsTask.class), mock(TelemetrySyncTask.class), + mock(UniqueKeysTracker.class)); + SyncManagerImp syncManager = new SyncManagerImp(tasks, false, synchronizer, mock(PushManager.class), + new LinkedBlockingQueue<>(), gates, mock(TelemetryStorage.class), mock(TelemetrySynchronizer.class), + SplitClientConfig.builder().build(), mock(SplitAPI.class), notifier); + + syncManager.start(); + + assertReadyMetadata(notifier); + assertTrue(gates.isSDKReady()); + syncManager.shutdown(); + } + + @Test + public void standaloneSyncManagerDoesNotNotifyWhenSyncAllNeverSucceeds() throws Exception { + Synchronizer synchronizer = mock(Synchronizer.class); + when(synchronizer.syncAll()).thenReturn(false); + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + SplitTasks tasks = SplitTasks.build(mock(SplitSynchronizationTask.class), mock(SegmentSynchronizationTaskImp.class), + mock(ImpressionsManagerImpl.class), mock(EventsTask.class), mock(TelemetrySyncTask.class), + mock(UniqueKeysTracker.class)); + SyncManagerImp syncManager = new SyncManagerImp(tasks, false, synchronizer, mock(PushManager.class), + new LinkedBlockingQueue<>(), new SDKReadinessGates(), mock(TelemetryStorage.class), + mock(TelemetrySynchronizer.class), SplitClientConfig.builder().build(), mock(SplitAPI.class), notifier); + + syncManager.start(); + Thread.sleep(300); + + verify(notifier, never()).notify(any(SdkInternalEvent.class), any(SdkEventMetadata.class)); + syncManager.shutdown(); + } + + @Test + public void localhostSyncManagerNotifiesSdkReadyAfterGate() { + Synchronizer synchronizer = mock(Synchronizer.class); + when(synchronizer.syncAll()).thenReturn(true); + SDKReadinessGates gates = new SDKReadinessGates(); + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + + new LocalhostSyncManager(synchronizer, gates, notifier).start(); + + assertTrue(gates.isSDKReady()); + assertReadyMetadata(notifier); + } + + @Test + public void localhostSyncManagerDoesNotNotifyWhenSyncFails() { + Synchronizer synchronizer = mock(Synchronizer.class); + when(synchronizer.syncAll()).thenReturn(false); + SdkEventsNotifier notifier = mock(SdkEventsNotifier.class); + + new LocalhostSyncManager(synchronizer, new SDKReadinessGates(), notifier).start(); + + verifyZeroInteractions(notifier); + } +} diff --git a/client/src/test/java/io/split/engine/common/SyncManagerTest.java b/client/src/test/java/io/split/engine/common/SyncManagerTest.java index 551ebd7f..f1c1699c 100644 --- a/client/src/test/java/io/split/engine/common/SyncManagerTest.java +++ b/client/src/test/java/io/split/engine/common/SyncManagerTest.java @@ -1,58 +1,122 @@ package io.split.engine.common; +import io.split.client.SplitClientConfig; +import io.split.client.events.EventsTask; +import io.split.client.impressions.ImpressionsManagerImpl; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.engine.SDKReadinessGates; +import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.segments.SegmentSynchronizationTaskImp; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import io.split.telemetry.synchronizer.TelemetrySynchronizer; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.io.IOException; import java.util.concurrent.LinkedBlockingQueue; public class SyncManagerTest { - private static final int BACKOFF_BASE = 1; private Synchronizer _synchronizer; private PushManager _pushManager; + private SDKReadinessGates _gates; + private ImpressionsManagerImpl _impressionsManager; + private TelemetrySynchronizer _telemetrySynchronizer; + private TelemetrySyncTask _telemetrySyncTask; + private EventsTask _eventsTask; + private SplitClientConfig _config; + private SegmentSynchronizationTaskImp _segmentSynchronizationTaskImp; + private SplitSynchronizationTask _splitSynchronizationTask; + private UniqueKeysTracker _uniqueKeysTracker; @Before public void setUp() { _synchronizer = Mockito.mock(Synchronizer.class); _pushManager = Mockito.mock(PushManager.class); + _gates = Mockito.mock(SDKReadinessGates.class); + _impressionsManager = Mockito.mock(ImpressionsManagerImpl.class); + _telemetrySynchronizer = Mockito.mock(TelemetrySynchronizer.class); + _telemetrySyncTask = Mockito.mock(TelemetrySyncTask.class); + _eventsTask = Mockito.mock(EventsTask.class); + _config = Mockito.mock(SplitClientConfig.class); + _segmentSynchronizationTaskImp = Mockito.mock(SegmentSynchronizationTaskImp.class); + _splitSynchronizationTask = Mockito.mock(SplitSynchronizationTask.class); + _uniqueKeysTracker = Mockito.mock(UniqueKeysTracker.class); } @Test - public void startWithStreamingFalseShouldStartPolling() { - SyncManagerImp syncManager = new SyncManagerImp(false, _synchronizer, _pushManager, new LinkedBlockingQueue<>(), BACKOFF_BASE); + public void startWithStreamingFalseShouldStartPolling() throws InterruptedException, IOException { + TelemetryStorage telemetryStorage = Mockito.mock(TelemetryStorage.class); + _gates.sdkInternalReady(); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + + Mockito.when(_synchronizer.syncAll()).thenReturn(true); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + SyncManagerImp syncManager = new SyncManagerImp( splitTasks, false, _synchronizer, _pushManager, new LinkedBlockingQueue<>(), + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); syncManager.start(); + Thread.sleep(1000); Mockito.verify(_synchronizer, Mockito.times(1)).startPeriodicFetching(); - Mockito.verify(_synchronizer, Mockito.times(0)).syncAll(); + Mockito.verify(_synchronizer, Mockito.times(1)).syncAll(); Mockito.verify(_pushManager, Mockito.times(0)).start(); + + syncManager.shutdown(); + Mockito.verify(_pushManager, Mockito.times(0)).stop(); + Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicDataRecording(); } @Test - public void startWithStreamingTrueShouldStartSyncAll() { - SyncManager sm = new SyncManagerImp(true, _synchronizer, _pushManager, new LinkedBlockingQueue<>(), BACKOFF_BASE); + public void startWithStreamingTrueShouldStartSyncAll() throws InterruptedException { + TelemetryStorage telemetryStorage = Mockito.mock(TelemetryStorage.class); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + + Mockito.when(_synchronizer.syncAll()).thenReturn(true); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + SyncManager sm = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, new LinkedBlockingQueue<>(), + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); sm.start(); + Thread.sleep(1000); Mockito.verify(_synchronizer, Mockito.times(0)).startPeriodicFetching(); Mockito.verify(_synchronizer, Mockito.times(1)).syncAll(); Mockito.verify(_pushManager, Mockito.times(1)).start(); + Mockito.verify(telemetryStorage, Mockito.times(1)).recordStreamingEvents(Mockito.any()); } @Test public void onStreamingAvailable() throws InterruptedException { - LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + TelemetryStorage telemetryStorage = Mockito.mock(TelemetryStorage.class); + LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); Thread t = new Thread(syncManager::incomingPushStatusHandler); t.start(); - messsages.offer(PushManager.Status.STREAMING_READY); + messages.offer(PushManager.Status.STREAMING_READY); Thread.sleep(500); Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicFetching(); Mockito.verify(_synchronizer, Mockito.times(1)).syncAll(); Mockito.verify(_pushManager, Mockito.times(1)).startWorkers(); + Mockito.verify(telemetryStorage, Mockito.times(1)).recordStreamingEvents(Mockito.any()); t.interrupt(); } @Test public void onStreamingDisabled() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messsages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); Thread t = new Thread(syncManager::incomingPushStatusHandler); t.start(); messsages.offer(PushManager.Status.STREAMING_DOWN); @@ -65,8 +129,13 @@ public void onStreamingDisabled() throws InterruptedException { @Test public void onStreamingShutdown() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messsages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); Thread t = new Thread(syncManager::incomingPushStatusHandler); t.start(); messsages.offer(PushManager.Status.STREAMING_OFF); @@ -76,9 +145,15 @@ public void onStreamingShutdown() throws InterruptedException { } @Test - public void onConnected() throws InterruptedException { + public void onConnected() throws InterruptedException, IOException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messsages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); Thread t = new Thread(syncManager::incomingPushStatusHandler); t.start(); messsages.offer(PushManager.Status.STREAMING_READY); @@ -86,29 +161,73 @@ public void onConnected() throws InterruptedException { Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicFetching(); Mockito.verify(_synchronizer, Mockito.times(1)).syncAll(); t.interrupt(); + + syncManager.shutdown(); + Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicDataRecording(); } @Test - public void onDisconnect() throws InterruptedException { + public void onDisconnect() throws InterruptedException, IOException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messsages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); Thread t = new Thread(syncManager::incomingPushStatusHandler); t.start(); messsages.offer(PushManager.Status.STREAMING_OFF); Thread.sleep(500); Mockito.verify(_synchronizer, Mockito.times(1)).startPeriodicFetching(); t.interrupt(); + + syncManager.shutdown(); + Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicDataRecording(); } @Test - public void onDisconnectAndReconnect() throws InterruptedException { // Check with mauro. reconnect should call pushManager.start again, right? + public void onDisconnectAndReconnect() throws InterruptedException, IOException { // Check with mauro. reconnect should call pushManager.start again, right? + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messsages = new LinkedBlockingQueue<>(); - SyncManagerImp syncManager = new SyncManagerImp(true, _synchronizer, _pushManager, messsages, BACKOFF_BASE); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + Mockito.when(_synchronizer.syncAll()).thenReturn(true); + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, true, _synchronizer, _pushManager, messsages, + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); syncManager.start(); messsages.offer(PushManager.Status.STREAMING_BACKOFF); Thread.sleep(1200); Mockito.verify(_synchronizer, Mockito.times(1)).startPeriodicFetching(); Mockito.verify(_synchronizer, Mockito.times(1)).syncAll(); Mockito.verify(_pushManager, Mockito.times(2)).start(); + + syncManager.shutdown(); + Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicDataRecording(); + } + + @Test + public void syncAllRetryThenShouldStartPolling() throws InterruptedException, IOException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + SplitTasks splitTasks = SplitTasks.build(_splitSynchronizationTask, _segmentSynchronizationTaskImp, + _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); + SplitAPI splitAPI = Mockito.mock(SplitAPI.class); + + Mockito.when(_synchronizer.syncAll()).thenReturn(false).thenReturn(true); + SyncManagerImp syncManager = new SyncManagerImp(splitTasks, false, _synchronizer, _pushManager, new LinkedBlockingQueue<>(), + _gates, telemetryStorage, _telemetrySynchronizer, _config, splitAPI); + syncManager.start(); + Thread.sleep(2000); + Mockito.verify(_synchronizer, Mockito.times(1)).startPeriodicFetching(); + Mockito.verify(_synchronizer, Mockito.times(2)).syncAll(); + Mockito.verify(_pushManager, Mockito.times(0)).start(); + Mockito.verify(_gates, Mockito.times(1)).sdkInternalReady(); + Mockito.verify(_telemetrySynchronizer, Mockito.times(1)).synchronizeConfig(Mockito.anyObject(), Mockito.anyLong(), Mockito.anyObject(), Mockito.anyObject()); + + syncManager.shutdown(); + Mockito.verify(_synchronizer, Mockito.times(1)).stopPeriodicDataRecording(); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/common/SynchronizerImpKillEventsTest.java b/client/src/test/java/io/split/engine/common/SynchronizerImpKillEventsTest.java new file mode 100644 index 00000000..92990acd --- /dev/null +++ b/client/src/test/java/io/split/engine/common/SynchronizerImpKillEventsTest.java @@ -0,0 +1,77 @@ +package io.split.engine.common; + +import io.split.client.api.SdkEventType; +import io.split.client.events.EventsTask; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.experiments.FetchResult; +import io.split.engine.experiments.SplitFetcher; +import io.split.engine.experiments.SplitSynchronizationTask; +import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.engine.sse.dtos.SplitKillNotification; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCache; +import io.split.storages.SplitCache; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import java.util.Collections; +import java.util.HashSet; + +import static org.junit.Assert.assertEquals; + +public class SynchronizerImpKillEventsTest { + + private SplitCache _splitCache; + private SdkEventsNotifier _notifier; + private SynchronizerImp _synchronizer; + + @Before + public void setUp() { + SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); + Mockito.when(splitFetcher.forceRefresh(Mockito.any(FetchOptions.class))) + .thenReturn(new FetchResult(true, false, new HashSet())); + _splitCache = Mockito.mock(SplitCache.class); + // first read is localKillSplit's staleness check; afterwards the kill has "landed" so refreshSplits returns immediately + Mockito.when(_splitCache.getChangeNumber()).thenReturn(10L, 20L); + RuleBasedSegmentCache rbsCache = Mockito.mock(RuleBasedSegmentCache.class); + SplitTasks tasks = SplitTasks.build(Mockito.mock(SplitSynchronizationTask.class), + Mockito.mock(SegmentSynchronizationTask.class), Mockito.mock(ImpressionsManager.class), + Mockito.mock(EventsTask.class), Mockito.mock(TelemetrySyncTask.class), Mockito.mock(UniqueKeysTracker.class)); + _notifier = Mockito.mock(SdkEventsNotifier.class); + _synchronizer = new SynchronizerImp(tasks, splitFetcher, _splitCache, Mockito.mock(SegmentCache.class), rbsCache, + 50, 10, 5, new HashSet(), _notifier); + } + + @Test + public void killEmitsFlagKilledNotificationWithFlagName() { + _synchronizer.localKillSplit(kill("flag1", 20L)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(SdkEventMetadata.class); + Mockito.verify(_notifier).notify(Mockito.eq(SdkInternalEvent.FLAG_KILLED_NOTIFICATION), captor.capture()); + Mockito.verifyNoMoreInteractions(_notifier); + assertEquals(SdkEventType.FLAGS_UPDATE, captor.getValue().getType()); + assertEquals(Collections.singleton("flag1"), captor.getValue().getNames()); + } + + @Test + public void staleKillEmitsNothing() { + _synchronizer.localKillSplit(kill("flag1", 5L)); + + Mockito.verifyZeroInteractions(_notifier); + } + + private static SplitKillNotification kill(String name, long changeNumber) { + SplitKillNotification notification = Mockito.mock(SplitKillNotification.class); + Mockito.when(notification.getSplitName()).thenReturn(name); + Mockito.when(notification.getChangeNumber()).thenReturn(changeNumber); + Mockito.when(notification.getDefaultTreatment()).thenReturn("off"); + return notification; + } +} diff --git a/client/src/test/java/io/split/engine/common/SynchronizerTest.java b/client/src/test/java/io/split/engine/common/SynchronizerTest.java index 5bff9d00..0ce439c7 100644 --- a/client/src/test/java/io/split/engine/common/SynchronizerTest.java +++ b/client/src/test/java/io/split/engine/common/SynchronizerTest.java @@ -1,49 +1,111 @@ package io.split.engine.common; -import io.split.cache.SegmentCache; -import io.split.cache.SplitCache; +import io.split.client.events.EventsTask; +import io.split.client.impressions.ImpressionsManager; +import io.split.client.impressions.UniqueKeysTracker; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.engine.segments.SegmentChangeFetcher; +import io.split.engine.segments.SegmentSynchronizationTaskImp; +import io.split.storages.*; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.engine.experiments.FetchResult; import io.split.engine.experiments.SplitFetcherImp; import io.split.engine.experiments.SplitSynchronizationTask; import io.split.engine.segments.SegmentFetcher; import io.split.engine.segments.SegmentSynchronizationTask; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.synchronizer.TelemetrySyncTask; +import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import org.mockito.internal.matchers.Any; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.mockito.Mockito.when; public class SynchronizerTest { + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); private SplitSynchronizationTask _refreshableSplitFetcherTask; private SegmentSynchronizationTask _segmentFetcher; private SplitFetcherImp _splitFetcher; - private SplitCache _splitCache; + private SplitCacheProducer _splitCacheProducer; + private RuleBasedSegmentCacheProducer _ruleBasedSegmentCacheProducer; private Synchronizer _synchronizer; - private SegmentCache _segmentCache; + private SegmentCacheProducer _segmentCacheProducer; + private SplitTasks _splitTasks; + private TelemetrySyncTask _telemetrySyncTask; + private ImpressionsManager _impressionsManager; + private EventsTask _eventsTask; + private UniqueKeysTracker _uniqueKeysTracker; @Before public void beforeMethod() { _refreshableSplitFetcherTask = Mockito.mock(SplitSynchronizationTask.class); _segmentFetcher = Mockito.mock(SegmentSynchronizationTask.class); _splitFetcher = Mockito.mock(SplitFetcherImp.class); - _splitCache = Mockito.mock(SplitCache.class); - _segmentCache = Mockito.mock(SegmentCache.class); + _ruleBasedSegmentCacheProducer = Mockito.mock(RuleBasedSegmentCacheProducer.class); + _splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + _segmentCacheProducer = Mockito.mock(SegmentCache.class); + _telemetrySyncTask = Mockito.mock(TelemetrySyncTask.class); + _impressionsManager = Mockito.mock(ImpressionsManager.class); + _eventsTask = Mockito.mock(EventsTask.class); + _uniqueKeysTracker = Mockito.mock(UniqueKeysTracker.class); + + _splitTasks = SplitTasks.build(_refreshableSplitFetcherTask, _segmentFetcher, _impressionsManager, _eventsTask, _telemetrySyncTask, _uniqueKeysTracker); - _synchronizer = new SynchronizerImp(_refreshableSplitFetcherTask, _splitFetcher, _segmentFetcher, _splitCache, _segmentCache); + _synchronizer = new SynchronizerImp(_splitTasks, _splitFetcher, _splitCacheProducer, _segmentCacheProducer, _ruleBasedSegmentCacheProducer, 50, 10, 5, new HashSet<>()); } @Test public void syncAll() throws InterruptedException { + Mockito.when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>())); + Mockito.when(_segmentFetcher.fetchAllSynchronous()).thenReturn(true); + _synchronizer.syncAll(); + + Thread.sleep(1000); + Mockito.verify(_splitFetcher, Mockito.times(1)).forceRefresh(Mockito.anyObject()); + Mockito.verify(_segmentFetcher, Mockito.times(1)).fetchAllSynchronous(); + } + + @Test + public void testSyncAllSegments() throws InterruptedException, NoSuchFieldException, IllegalAccessException { + SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(Mockito.mock(SegmentChangeFetcher.class), + 20L, 1, _segmentCacheProducer, Mockito.mock(TelemetryRuntimeProducer.class), + Mockito.mock(SplitCacheConsumer.class), null, Mockito.mock(RuleBasedSegmentCache.class)); + Field synchronizerSegmentFetcher = SynchronizerImp.class.getDeclaredField("_segmentSynchronizationTaskImp"); + synchronizerSegmentFetcher.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(synchronizerSegmentFetcher, synchronizerSegmentFetcher.getModifiers() & ~Modifier.FINAL); + synchronizerSegmentFetcher.set(_synchronizer, segmentSynchronizationTask); + Mockito.when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, Stream.of("Segment1", "Segment2").collect(Collectors.toSet()))); + Mockito.when(_segmentFetcher.fetchAllSynchronous()).thenReturn(true); _synchronizer.syncAll(); - Thread.sleep(100); - Mockito.verify(_splitFetcher, Mockito.times(1)).fetchAll(true); - Mockito.verify(_segmentFetcher, Mockito.times(1)).fetchAll(true); + Thread.sleep(1000); + Mockito.verify(_splitFetcher, Mockito.times(1)).forceRefresh(Mockito.anyObject()); + Assert.assertNotNull(segmentSynchronizationTask.getFetcher("Segment1")); + Assert.assertNotNull(segmentSynchronizationTask.getFetcher("Segment2")); } @Test public void startPeriodicFetching() { _synchronizer.startPeriodicFetching(); - Mockito.verify(_refreshableSplitFetcherTask, Mockito.times(1)).startPeriodicFetching(); - Mockito.verify(_segmentFetcher, Mockito.times(1)).startPeriodicFetching(); + Mockito.verify(_refreshableSplitFetcherTask, Mockito.times(1)).start(); + Mockito.verify(_segmentFetcher, Mockito.times(1)).start(); } @Test @@ -56,20 +118,225 @@ public void stopPeriodicFetching() { @Test public void streamingRetryOnSplit() { - Mockito.when(_splitCache.getChangeNumber()).thenReturn(0l).thenReturn(0l).thenReturn(1l); - _synchronizer.refreshSplits(1l); + when(_splitCacheProducer.getChangeNumber()).thenReturn(0l).thenReturn(0l).thenReturn(1l); + when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>())); + _synchronizer.refreshSplits(1L, 0L); - Mockito.verify(_splitCache, Mockito.times(3)).getChangeNumber(); + Mockito.verify(_splitCacheProducer, Mockito.times(3)).getChangeNumber(); } @Test public void streamingRetryOnSegment() { SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class); - Mockito.when(_segmentFetcher.getFetcher(Mockito.anyString())).thenReturn(fetcher); - Mockito.when(_segmentCache.getChangeNumber(Mockito.anyString())).thenReturn(0l).thenReturn(0l).thenReturn(1l); + when(_segmentFetcher.getFetcher(Mockito.anyString())).thenReturn(fetcher); + when(_segmentCacheProducer.getChangeNumber(Mockito.anyString())).thenReturn(0l).thenReturn(0l).thenReturn(1l); _synchronizer.refreshSegment("Segment",1l); - Mockito.verify(_segmentCache, Mockito.times(3)).getChangeNumber(Mockito.anyString()); + Mockito.verify(_segmentCacheProducer, Mockito.times(3)).getChangeNumber(Mockito.anyString()); + } + + @Test + public void streamingRetryOnSplitAndSegment() { + when(_splitCacheProducer.getChangeNumber()).thenReturn(0l).thenReturn(0l).thenReturn(1l); + Set segments = new HashSet<>(); + segments.add("segment1"); + segments.add("segment2"); + when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, segments)); + SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class); + when(_segmentCacheProducer.getChangeNumber(Mockito.anyString())).thenReturn(0l).thenReturn(0l).thenReturn(1l); + when(_segmentFetcher.getFetcher(Mockito.anyString())).thenReturn(fetcher); + _synchronizer.refreshSplits(1L, 0L); + + Mockito.verify(_splitCacheProducer, Mockito.times(3)).getChangeNumber(); + Mockito.verify(_segmentFetcher, Mockito.times(2)).getFetcher(Mockito.anyString()); + } + + @Test + public void testCDNBypassIsRequestedAfterNFailures() { + SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER); + Synchronizer imp = new SynchronizerImp(_splitTasks, + _splitFetcher, + cache, + _segmentCacheProducer, + _ruleBasedSegmentCacheProducer, + 50, + 3, + 1, + new HashSet<>()); + + ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); + AtomicInteger calls = new AtomicInteger(); + Mockito.doAnswer(invocationOnMock -> { + calls.getAndIncrement(); + switch (calls.get()) { + case 4: cache.setChangeNumber(123); + } + return new FetchResult(true, false, new HashSet<>()); + }).when(_splitFetcher).forceRefresh(optionsCaptor.capture()); + + imp.refreshSplits(123L, 0L); + + List options = optionsCaptor.getAllValues(); + Assert.assertEquals(options.size(), 4); + Assert.assertFalse(options.get(0).hasCustomCN()); + Assert.assertFalse(options.get(1).hasCustomCN()); + Assert.assertFalse(options.get(2).hasCustomCN()); + Assert.assertTrue(options.get(3).hasCustomCN()); } -} + @Test + public void testCDNBypassRequestLimitAndBackoff() throws NoSuchFieldException, IllegalAccessException { + SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER); + Synchronizer imp = new SynchronizerImp(_splitTasks, + _splitFetcher, + cache, + _segmentCacheProducer, + _ruleBasedSegmentCacheProducer, + 50, + 3, + 1, + new HashSet<>()); + + ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); + AtomicInteger calls = new AtomicInteger(); + Mockito.doAnswer(invocationOnMock -> { + calls.getAndIncrement(); + switch (calls.get()) { + case 14: Assert.assertTrue(false); // should never get here + } + return null; + }).when(_splitFetcher).forceRefresh(optionsCaptor.capture()); + + // Before executing, we'll update the backoff via reflection, to avoid waiting minutes for the test to run. + Field backoffBase = SynchronizerImp.class.getDeclaredField("ON_DEMAND_FETCH_BACKOFF_BASE_MS"); + backoffBase.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL); + backoffBase.set(imp, 1); // 1ms + + long before = System.currentTimeMillis(); + imp.refreshSplits(1L, 0L); + long after = System.currentTimeMillis(); + + List options = optionsCaptor.getAllValues(); + Assert.assertEquals(options.size(), 13); + Assert.assertFalse(options.get(0).hasCustomCN()); + Assert.assertFalse(options.get(1).hasCustomCN()); + Assert.assertFalse(options.get(2).hasCustomCN()); + Assert.assertTrue(options.get(3).hasCustomCN()); + Assert.assertTrue(options.get(4).hasCustomCN()); + Assert.assertTrue(options.get(5).hasCustomCN()); + Assert.assertTrue(options.get(6).hasCustomCN()); + Assert.assertTrue(options.get(7).hasCustomCN()); + Assert.assertTrue(options.get(8).hasCustomCN()); + Assert.assertTrue(options.get(9).hasCustomCN()); + Assert.assertTrue(options.get(10).hasCustomCN()); + Assert.assertTrue(options.get(11).hasCustomCN()); + Assert.assertTrue(options.get(12).hasCustomCN()); + + Assert.assertEquals(calls.get(), 13); + long minDiffExpected = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256; + Assert.assertTrue((after - before) > minDiffExpected); + } + + @Test + public void testCDNBypassRequestLimitAndForSegmentsBackoff() throws NoSuchFieldException, IllegalAccessException { + SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER); + Synchronizer imp = new SynchronizerImp(_splitTasks, + _splitFetcher, + cache, + _segmentCacheProducer, + _ruleBasedSegmentCacheProducer, + 50, + 3, + 1, + new HashSet<>()); + + SegmentFetcher fetcher = Mockito.mock(SegmentFetcher.class); + when(_segmentFetcher.getFetcher("someSegment")).thenReturn(fetcher); + + ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); + AtomicInteger calls = new AtomicInteger(); + Mockito.doAnswer(invocationOnMock -> { + calls.getAndIncrement(); + switch (calls.get()) { + case 14: Assert.assertTrue(false); // should never get here + } + return null; + }).when(fetcher).fetch(optionsCaptor.capture()); + + // Before executing, we'll update the backoff via reflection, to avoid waiting minutes for the test to run. + Field backoffBase = SynchronizerImp.class.getDeclaredField("ON_DEMAND_FETCH_BACKOFF_BASE_MS"); + backoffBase.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(backoffBase, backoffBase.getModifiers() & ~Modifier.FINAL); + backoffBase.set(imp, 1); // 1ms + + long before = System.currentTimeMillis(); + imp.refreshSegment("someSegment",1L); + long after = System.currentTimeMillis(); + + List options = optionsCaptor.getAllValues(); + Assert.assertEquals(options.size(), 13); + Assert.assertFalse(options.get(0).hasCustomCN()); + Assert.assertFalse(options.get(1).hasCustomCN()); + Assert.assertFalse(options.get(2).hasCustomCN()); + Assert.assertTrue(options.get(3).hasCustomCN()); + Assert.assertTrue(options.get(4).hasCustomCN()); + Assert.assertTrue(options.get(5).hasCustomCN()); + Assert.assertTrue(options.get(6).hasCustomCN()); + Assert.assertTrue(options.get(7).hasCustomCN()); + Assert.assertTrue(options.get(8).hasCustomCN()); + Assert.assertTrue(options.get(9).hasCustomCN()); + Assert.assertTrue(options.get(10).hasCustomCN()); + Assert.assertTrue(options.get(11).hasCustomCN()); + Assert.assertTrue(options.get(12).hasCustomCN()); + + Assert.assertEquals(calls.get(), 13); + long minDiffExpected = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256; + Assert.assertTrue((after - before) > minDiffExpected); + } + + @Test + public void testDataRecording(){ + SplitCache cache = new InMemoryCacheImp(FLAG_SETS_FILTER); + Synchronizer imp = new SynchronizerImp(_splitTasks, + _splitFetcher, + cache, + _segmentCacheProducer, + _ruleBasedSegmentCacheProducer, + 50, + 3, + 1, + new HashSet<>()); + imp.startPeriodicDataRecording(); + + Mockito.verify(_eventsTask, Mockito.times(1)).start(); + Mockito.verify(_impressionsManager, Mockito.times(1)).start(); + Mockito.verify(_uniqueKeysTracker, Mockito.times(1)).start(); + Mockito.verify(_telemetrySyncTask, Mockito.times(1)).startScheduledTask(); + + imp.stopPeriodicDataRecording(); + + Mockito.verify(_eventsTask, Mockito.times(1)).close(); + Mockito.verify(_impressionsManager, Mockito.times(1)).close(); + Mockito.verify(_uniqueKeysTracker, Mockito.times(1)).stop(); + Mockito.verify(_telemetrySyncTask, Mockito.times(1)).stopScheduledTask(); + } + + @Test + public void skipSyncWhenChangeNumbersAreZero() { + _synchronizer.refreshSplits(0L, 0L); + Mockito.verify(_splitFetcher, Mockito.times(0)).forceRefresh(Mockito.anyObject()); + } + + @Test + public void testSyncRuleBasedSegment() { + when(_ruleBasedSegmentCacheProducer.getChangeNumber()).thenReturn(-1l).thenReturn(-1l).thenReturn(123l); + when(_splitFetcher.forceRefresh(Mockito.anyObject())).thenReturn(new FetchResult(true, false, new HashSet<>())); + _synchronizer.refreshSplits(0L, 123L); + Mockito.verify(_splitFetcher, Mockito.times(2)).forceRefresh(Mockito.anyObject()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/evaluator/EvaluatorIntegrationTest.java b/client/src/test/java/io/split/engine/evaluator/EvaluatorIntegrationTest.java index 14cc7eee..5b0a024a 100644 --- a/client/src/test/java/io/split/engine/evaluator/EvaluatorIntegrationTest.java +++ b/client/src/test/java/io/split/engine/evaluator/EvaluatorIntegrationTest.java @@ -1,21 +1,37 @@ package io.split.engine.evaluator; import com.google.common.collect.Lists; -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SplitCache; import io.split.client.dtos.ConditionType; +import io.split.client.dtos.FallbackTreatmentCalculatorImp; import io.split.client.dtos.MatcherCombiner; import io.split.client.dtos.Partition; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; import io.split.engine.experiments.ParsedCondition; +import io.split.engine.experiments.ParsedRuleBasedSegment; import io.split.engine.experiments.ParsedSplit; import io.split.engine.matchers.AttributeMatcher; import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.PrerequisitesMatcher; +import io.split.engine.matchers.RuleBasedSegmentMatcher; import io.split.engine.matchers.strings.EndsWithAnyOfMatcher; import io.split.engine.matchers.strings.WhitelistMatcher; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCache; +import io.split.storages.SplitCache; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class EvaluatorIntegrationTest { private static final String DEFAULT_TREATMENT_VALUE = "defaultTreatment"; @@ -23,6 +39,7 @@ public class EvaluatorIntegrationTest { private static final String TEST_LABEL_VALUE_WHITELIST = "test label whitelist"; private static final String TEST_LABEL_VALUE_ROLL_OUT = "test label roll out"; private static final String ON_TREATMENT = "on"; + private static final String OFF_TREATMENT = "off"; @Test public void evaluateFeatureWithWhitelistShouldReturnOn() { @@ -121,32 +138,83 @@ public void evaluateFeatureWhenSplitNotExistsShouldReturnControl() { Assert.assertEquals(Labels.DEFINITION_NOT_FOUND, result.label); } + @Test + public void evaluateMultipleFeatures() { + Evaluator evaluator = buildEvaluatorAndLoadCache(false, 100); + + Map result = evaluator.evaluateFeatures("test_1", null, new ArrayList<>(Arrays.asList("test", "split_3")), null); + Assert.assertNotNull(result); + Assert.assertEquals(2, result.keySet().size()); + Assert.assertEquals(Labels.DEFINITION_NOT_FOUND, result.get("test").label); + Assert.assertEquals(ON_TREATMENT, result.get("split_3").treatment); + Long changeNumberExpected = 223366554L; + Assert.assertEquals(changeNumberExpected, result.get("split_3").changeNumber); + Assert.assertEquals(TEST_LABEL_VALUE_WHITELIST, result.get("split_3").label); + } + + @Test(expected = NullPointerException.class) + public void evaluateFeaturesSplitsNull() { + Evaluator evaluator = buildEvaluatorAndLoadCache(false, 100); + + Map result = evaluator.evaluateFeatures("mauro@test.io", null, null, null); + } + + @Test + public void evaluateFeatureWithRuleBasedSegmentMatcher() { + Evaluator evaluator = buildEvaluatorAndLoadCache(false, 100); + + EvaluatorImp.TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature("mauro@test.io", null, "split_5", null); + Assert.assertEquals(ON_TREATMENT, result.treatment); + + result = evaluator.evaluateFeature("admin", null, "split_5", null); + Assert.assertEquals(OFF_TREATMENT, result.treatment); + } + private Evaluator buildEvaluatorAndLoadCache(boolean killed, int trafficAllocation) { - SplitCache splitCache = new InMemoryCacheImp(); - Evaluator evaluator = new EvaluatorImp(splitCache); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + FallbackTreatmentCalculatorImp fallbackTreatmentCalculatorImp = new FallbackTreatmentCalculatorImp(null); + Evaluator evaluator = new EvaluatorImp(splitCache, segmentCache, ruleBasedSegmentCache, fallbackTreatmentCalculatorImp); Partition partition = new Partition(); partition.treatment = ON_TREATMENT; partition.size = 100; + Partition partitionOff = new Partition(); + partitionOff.treatment = OFF_TREATMENT; + partitionOff.size = 100; + List partitions = Lists.newArrayList(partition); AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin"))); AttributeMatcher endsWithMatcher = AttributeMatcher.vanilla(new EndsWithAnyOfMatcher(Lists.newArrayList("@test.io", "@mail.io"))); + AttributeMatcher ruleBasedSegmentMatcher = AttributeMatcher.vanilla(new RuleBasedSegmentMatcher("sample_rule_based_segment")); CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher)); CombiningMatcher endsWithCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(endsWithMatcher)); + CombiningMatcher ruleBasedSegmentCombinerMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ruleBasedSegmentMatcher)); ParsedCondition whitelistCondition = new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, partitions, TEST_LABEL_VALUE_WHITELIST); ParsedCondition rollOutCondition = new ParsedCondition(ConditionType.ROLLOUT, endsWithCombiningMatcher, partitions, TEST_LABEL_VALUE_ROLL_OUT); + ParsedCondition ruleBasedSegmentCondition = new ParsedCondition(ConditionType.ROLLOUT, ruleBasedSegmentCombinerMatcher, Lists.newArrayList(partitionOff), TEST_LABEL_VALUE_ROLL_OUT); List conditions = Lists.newArrayList(whitelistCondition, rollOutCondition); + List conditionsForRBS = Lists.newArrayList(ruleBasedSegmentCondition, rollOutCondition); + + ParsedSplit parsedSplit1 = new ParsedSplit("split_1", 0, false, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366551, 100, 0, 2, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit2 = new ParsedSplit("split_2", 0, true, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366552, 100, 0, 2, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit3 = new ParsedSplit("split_3", 0, false, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366554, 100, 0, 2, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit4 = new ParsedSplit("split_test", 0, killed, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366555, trafficAllocation, 0, 2, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); + ParsedSplit parsedSplit5 = new ParsedSplit("split_5", 0, false, DEFAULT_TREATMENT_VALUE, conditionsForRBS, TRAFFIC_TYPE_VALUE, 223366554, 100, 0, 2, null, new HashSet<>(), true, new PrerequisitesMatcher(null)); + splitCache.putMany(Stream.of(parsedSplit1, parsedSplit2, parsedSplit3, parsedSplit4, parsedSplit5).collect(Collectors.toList())); - splitCache.put(new ParsedSplit("split_1", 0, false, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366551, 100, 0, 2, null)); - splitCache.put(new ParsedSplit("split_2", 0, true, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366552, 100, 0, 2, null)); - splitCache.put(new ParsedSplit("split_3", 0, false, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366554, 100, 0, 2, null)); - splitCache.put(new ParsedSplit("split_test", 0, killed, DEFAULT_TREATMENT_VALUE, conditions, TRAFFIC_TYPE_VALUE, 223366555, trafficAllocation, 0, 2, null)); + ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("sample_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, TEST_LABEL_VALUE_WHITELIST)),"user", + 123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList()); + ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment), null, 123); return evaluator; } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/evaluator/EvaluatorTest.java b/client/src/test/java/io/split/engine/evaluator/EvaluatorTest.java index 1a64cb34..33ebf6d6 100644 --- a/client/src/test/java/io/split/engine/evaluator/EvaluatorTest.java +++ b/client/src/test/java/io/split/engine/evaluator/EvaluatorTest.java @@ -1,17 +1,23 @@ package io.split.engine.evaluator; -import io.split.client.dtos.ConditionType; -import io.split.client.dtos.Partition; -import io.split.cache.SplitCache; +import io.split.client.dtos.*; +import io.split.client.utils.Json; import io.split.engine.experiments.ParsedCondition; import io.split.engine.experiments.ParsedSplit; import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.PrerequisitesMatcher; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -27,18 +33,24 @@ public class EvaluatorTest { private static final String TRAFFIC_TYPE_VALUE = "tt"; private static final String TREATMENT_VALUE = "treatment_test"; - private SplitCache _splitCache; + private SplitCacheConsumer _splitCacheConsumer; + private SegmentCacheConsumer _segmentCacheConsumer; + private RuleBasedSegmentCacheConsumer _ruleBasedSegmentCacheConsumer; private Evaluator _evaluator; private CombiningMatcher _matcher; private Map _configurations; private List _conditions; private List _partitions; + private EvaluationContext _evaluationContext; @Before public void before() { - _splitCache = Mockito.mock(SplitCache.class); - _evaluator = new EvaluatorImp(_splitCache); + _splitCacheConsumer = Mockito.mock(SplitCacheConsumer.class); + _segmentCacheConsumer = Mockito.mock(SegmentCacheConsumer.class); + _ruleBasedSegmentCacheConsumer = Mockito.mock(RuleBasedSegmentCacheConsumer.class); + _evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, new FallbackTreatmentCalculatorImp(null)); _matcher = Mockito.mock(CombiningMatcher.class); + _evaluationContext = Mockito.mock(EvaluationContext.class); _configurations = new HashMap<>(); _conditions = new ArrayList<>(); @@ -47,7 +59,7 @@ public void before() { @Test public void evaluateWhenSplitNameDoesNotExistReturnControl() { - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(null); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -57,8 +69,8 @@ public void evaluateWhenSplitNameDoesNotExistReturnControl() { @Test public void evaluateWhenSplitIsKilledReturnDefaultTreatment() { - ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, true, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2); - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(split); + ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, true, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -69,8 +81,8 @@ public void evaluateWhenSplitIsKilledReturnDefaultTreatment() { @Test public void evaluateWithoutConditionsReturnDefaultTreatment() { - ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2); - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(split); + ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2, new HashSet<>(), true, new PrerequisitesMatcher(null)); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -88,10 +100,10 @@ public void evaluateWithRollOutConditionBucketIsBiggerTrafficAllocationReturnDef ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, _matcher,_partitions, TEST_LABEL_VALUE); _conditions.add(condition); - ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 10, 12, 2, _configurations); + ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 10, 12, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(split); - Mockito.when(condition.matcher().match(MATCHING_KEY, BUCKETING_KEY, null, _evaluator)).thenReturn(true); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + Mockito.when(condition.matcher().match(MATCHING_KEY, BUCKETING_KEY, null, _evaluationContext)).thenReturn(true); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -109,10 +121,10 @@ public void evaluateWithRollOutConditionTrafficAllocationIsBiggerBucketReturnTre ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, _matcher, _partitions, TEST_LABEL_VALUE); _conditions.add(condition); - ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations); + ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(split); - Mockito.when(condition.matcher().match(MATCHING_KEY, BUCKETING_KEY, null, _evaluator)).thenReturn(true); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + Mockito.when(condition.matcher().match(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(true); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -130,10 +142,10 @@ public void evaluateWithWhitelistConditionReturnTreatment() { ParsedCondition condition = new ParsedCondition(ConditionType.WHITELIST, _matcher, _partitions, "test whitelist label"); _conditions.add(condition); - ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations); + ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); - Mockito.when(_splitCache.get(SPLIT_NAME)).thenReturn(split); - Mockito.when(condition.matcher().match(MATCHING_KEY, BUCKETING_KEY, null, _evaluator)).thenReturn(true); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + Mockito.when(condition.matcher().match(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(true); EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); @@ -141,4 +153,147 @@ public void evaluateWithWhitelistConditionReturnTreatment() { assertEquals("test whitelist label", result.label); assertEquals(CHANGE_NUMBER, result.changeNumber); } -} + + @Test + public void evaluateWithSets() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2, new HashSet<>(Arrays.asList("set1", "set2")), true, new PrerequisitesMatcher(null)); + List sets = new ArrayList<>(Arrays.asList("set1", "empty_set")); + Map> flagSets = new HashMap<>(); + flagSets.put("set1", new HashSet<>(Arrays.asList(SPLIT_NAME))); + flagSets.put("empty_set", null); + Mockito.when(_splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagSets); + Map parsedSplits = new HashMap<>(); + parsedSplits.put(SPLIT_NAME, split); + Mockito.when(_splitCacheConsumer.fetchMany(Arrays.asList(SPLIT_NAME))).thenReturn(parsedSplits); + + Map result = _evaluator.evaluateFeaturesByFlagSets(MATCHING_KEY, BUCKETING_KEY, sets, null); + + EvaluatorImp.TreatmentLabelAndChangeNumber treatmentLabelAndChangeNumber = result.get(SPLIT_NAME); + + assertEquals(DEFAULT_TREATMENT_VALUE, treatmentLabelAndChangeNumber.treatment); + assertEquals("default rule", treatmentLabelAndChangeNumber.label); + assertEquals(CHANGE_NUMBER, treatmentLabelAndChangeNumber.changeNumber); + } + + @Test + public void evaluateWithSetsNotHaveFlags() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 2, new HashSet<>(Arrays.asList("set1", "set2")), true, new PrerequisitesMatcher(null)); + List sets = new ArrayList<>(Arrays.asList("set2")); + Map> flagSets = new HashMap<>(); + Mockito.when(_splitCacheConsumer.getNamesByFlagSets(sets)).thenReturn(flagSets); + Map parsedSplits = new HashMap<>(); + Mockito.when(_splitCacheConsumer.fetchMany(Arrays.asList(SPLIT_NAME))).thenReturn(parsedSplits); + + Map result = _evaluator.evaluateFeaturesByFlagSets(MATCHING_KEY, BUCKETING_KEY, sets, null); + Assert.assertTrue(result.isEmpty()); + } + + @Test + public void evaluateWithPrerequisites() { + Partition partition = new Partition(); + partition.treatment = TREATMENT_VALUE; + partition.size = 100; + _partitions.add(partition); + ParsedCondition condition = new ParsedCondition(ConditionType.WHITELIST, _matcher, _partitions, "test whitelist label"); + _conditions.add(condition); + List prerequisites = Arrays.asList(Json.fromJson("{\"n\": \"split1\", \"ts\": [\"" + TREATMENT_VALUE + "\"]}", Prerequisites.class)); + + ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(prerequisites)); + ParsedSplit split1 = new ParsedSplit("split1", 0, false, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(null)); + + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + Mockito.when(_splitCacheConsumer.get("split1")).thenReturn(split1); + Mockito.when(condition.matcher().match(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(true); + + EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals(TREATMENT_VALUE, result.treatment); + assertEquals("test whitelist label", result.label); + assertEquals(CHANGE_NUMBER, result.changeNumber); + + Mockito.when(condition.matcher().match(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(false); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals(DEFAULT_TREATMENT_VALUE, result.treatment); + assertEquals(Labels.PREREQUISITES_NOT_MET, result.label); + assertEquals(CHANGE_NUMBER, result.changeNumber); + + // if split is killed, label should be killed. + split = new ParsedSplit(SPLIT_NAME, 0, true, DEFAULT_TREATMENT_VALUE, _conditions, TRAFFIC_TYPE_VALUE, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), true, new PrerequisitesMatcher(prerequisites)); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals(DEFAULT_TREATMENT_VALUE, result.treatment); + assertEquals(Labels.KILLED, result.label); + assertEquals(CHANGE_NUMBER, result.changeNumber); + } + + @Test + public void evaluateFallbackTreatmentWorks() { + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null); + FallbackTreatmentsConfiguration fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on")); + FallbackTreatmentCalculator fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + _evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator); + + EvaluatorImp.TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("on", result.treatment); + assertEquals("fallback - definition not found", result.label); + + ParsedSplit split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, null, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), false, null); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("on", result.treatment); + assertEquals("fallback - exception", result.label); + + // using byflag only + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null); + Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(null); + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new HashMap() {{ put(SPLIT_NAME, new FallbackTreatment("off")); }} ); + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + _evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator); + + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("off", result.treatment); + assertEquals("fallback - definition not found", result.label); + + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, "another_name", null); + assertEquals("control", result.treatment); + assertEquals("definition not found", result.label); + + split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, null, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), false, null); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("off", result.treatment); + assertEquals("fallback - exception", result.label); + + split = new ParsedSplit("another_name", 0, false, DEFAULT_TREATMENT_VALUE, _conditions, null, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), false, null); + Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, "another_name", null); + assertEquals("control", result.treatment); + assertEquals("exception", result.label); + + // with byflag + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(null); + Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(null); + fallbackTreatmentsConfiguration = new FallbackTreatmentsConfiguration(new FallbackTreatment("on"), new HashMap() {{ put(SPLIT_NAME, new FallbackTreatment("off")); }} ); + fallbackTreatmentCalculator = new FallbackTreatmentCalculatorImp(fallbackTreatmentsConfiguration); + _evaluator = new EvaluatorImp(_splitCacheConsumer, _segmentCacheConsumer, _ruleBasedSegmentCacheConsumer, fallbackTreatmentCalculator); + + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("off", result.treatment); + assertEquals("fallback - definition not found", result.label); + + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, "another_name", null); + assertEquals("on", result.treatment); + assertEquals("fallback - definition not found", result.label); + + split = new ParsedSplit(SPLIT_NAME, 0, false, DEFAULT_TREATMENT_VALUE, _conditions, null, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), false, null); + Mockito.when(_splitCacheConsumer.get(SPLIT_NAME)).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, SPLIT_NAME, null); + assertEquals("off", result.treatment); + assertEquals("fallback - exception", result.label); + + split = new ParsedSplit("another_name", 0, false, DEFAULT_TREATMENT_VALUE, _conditions, null, CHANGE_NUMBER, 60, 18, 2, _configurations, new HashSet<>(), false, null); + Mockito.when(_splitCacheConsumer.get("another_name")).thenReturn(split); + result = _evaluator.evaluateFeature(MATCHING_KEY, BUCKETING_KEY, "another_name", null); + assertEquals("on", result.treatment); + assertEquals("fallback - exception", result.label); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/AChangePerCallSplitChangeFetcher.java b/client/src/test/java/io/split/engine/experiments/AChangePerCallSplitChangeFetcher.java index 6b011456..0e0f6729 100644 --- a/client/src/test/java/io/split/engine/experiments/AChangePerCallSplitChangeFetcher.java +++ b/client/src/test/java/io/split/engine/experiments/AChangePerCallSplitChangeFetcher.java @@ -7,6 +7,7 @@ import io.split.client.dtos.SplitChange; import io.split.client.dtos.Status; import io.split.engine.ConditionsTestUtil; +import io.split.engine.common.FetchOptions; import io.split.grammar.Treatments; import java.util.concurrent.atomic.AtomicLong; @@ -31,12 +32,13 @@ public AChangePerCallSplitChangeFetcher(String segmentName) { @Override - public SplitChange fetch(long since, boolean addCacheHeader) { + public SplitChange fetch(long since, long rbSince, FetchOptions options) { long latestChangeNumber = since + 1; Condition condition = null; if (_segmentName != null) { - condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.ROLLOUT, _segmentName, Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); + condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.ROLLOUT, _segmentName, + Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); } else { condition = ConditionsTestUtil.makeAllKeysCondition(Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); } @@ -65,9 +67,9 @@ public SplitChange fetch(long since, boolean addCacheHeader) { SplitChange splitChange = new SplitChange(); - splitChange.splits = Lists.newArrayList(add, remove); - splitChange.since = since; - splitChange.till = latestChangeNumber; + splitChange.featureFlags.d = Lists.newArrayList(add, remove); + splitChange.featureFlags.s = since; + splitChange.featureFlags.t = latestChangeNumber; _lastAdded.set(latestChangeNumber); diff --git a/client/src/test/java/io/split/engine/experiments/ParsedRuleBasedSegmentTest.java b/client/src/test/java/io/split/engine/experiments/ParsedRuleBasedSegmentTest.java new file mode 100644 index 00000000..25363681 --- /dev/null +++ b/client/src/test/java/io/split/engine/experiments/ParsedRuleBasedSegmentTest.java @@ -0,0 +1,88 @@ +package io.split.engine.experiments; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.ExcludedSegments; +import io.split.client.dtos.MatcherCombiner; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.Json; +import io.split.client.utils.RuleBasedSegmentsToUpdate; +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; + +public class ParsedRuleBasedSegmentTest { + + @Test + public void works() { + List excludedSegments = new ArrayList<>(); + excludedSegments.add(new ExcludedSegments("standard","segment1")); + excludedSegments.add(new ExcludedSegments("standard","segment2")); + + AttributeMatcher segmentMatcher = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher("employees")); + CombiningMatcher segmentCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(segmentMatcher)); + ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("another_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")), "user", + 123, Lists.newArrayList("mauro@test.io", "gaston@test.io"), excludedSegments); + + Assert.assertEquals(Sets.newHashSet("segment2", "segment1", "employees"), parsedRuleBasedSegment.getSegmentsNames()); + Assert.assertEquals("another_rule_based_segment", parsedRuleBasedSegment.ruleBasedSegment()); + Assert.assertEquals(Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")), + parsedRuleBasedSegment.parsedConditions()); + Assert.assertEquals(123, parsedRuleBasedSegment.changeNumber()); + } + + @Test + public void worksWithoutExcluded() { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}]}]}}"; + SplitChange change = Json.fromJson(load, SplitChange.class); + RuleBasedSegmentsToUpdate toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedKeys().isEmpty()); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedSegments().isEmpty()); + + load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[{\"type\": \"standard\",\"name\":\"segment1\"}]},\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}]}]}}"; + change = Json.fromJson(load, SplitChange.class); + toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedKeys().isEmpty()); + + load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":[{\"type\": \"standard\",\"name\":\"segment1\"}], \"keys\":null},\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}]}]}}"; + change = Json.fromJson(load, SplitChange.class); + toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedKeys().isEmpty()); + + load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"excluded\":{\"keys\":[\"key1\"]},\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}]}]}}"; + change = Json.fromJson(load, SplitChange.class); + toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedSegments().isEmpty()); + + load = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"excluded\":{\"segments\":null, \"keys\":[\"key1\"]},\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"ALL_KEYS\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}]}]}}"; + change = Json.fromJson(load, SplitChange.class); + toUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + Assert.assertTrue(toUpdate.getToAdd().get(0).excludedSegments().isEmpty()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/RuleBasedSegmentParserTest.java b/client/src/test/java/io/split/engine/experiments/RuleBasedSegmentParserTest.java new file mode 100644 index 00000000..add3eb2a --- /dev/null +++ b/client/src/test/java/io/split/engine/experiments/RuleBasedSegmentParserTest.java @@ -0,0 +1,556 @@ +package io.split.engine.experiments; + +import com.google.common.collect.Lists; +import io.split.client.dtos.*; +import io.split.client.dtos.Matcher; +import io.split.client.utils.Json; +import io.split.client.utils.RuleBasedSegmentsToUpdate; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.evaluator.Labels; +import io.split.engine.matchers.*; +import io.split.engine.matchers.collections.ContainsAllOfSetMatcher; +import io.split.engine.matchers.collections.ContainsAnyOfSetMatcher; +import io.split.engine.matchers.collections.EqualToSetMatcher; +import io.split.engine.matchers.collections.PartOfSetMatcher; +import io.split.engine.matchers.strings.ContainsAnyOfMatcher; +import io.split.engine.matchers.strings.EndsWithAnyOfMatcher; +import io.split.engine.matchers.strings.StartsWithAnyOfMatcher; +import io.split.engine.segments.SegmentChangeFetcher; +import io.split.grammar.Treatments; +import io.split.storages.SegmentCache; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static io.split.TestHelper.makeRuleBasedSegment; +import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; + +import static org.junit.Assert.assertTrue; + +/** + * Tests for ExperimentParser + * + * @author adil + */ +public class RuleBasedSegmentParserTest { + + public static final String EMPLOYEES = "employees"; + public static final String SALES_PEOPLE = "salespeople"; + public static final int CONDITIONS_UPPER_LIMIT = 50; + + @Test + public void works() { + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); + Matcher notSalespeople = ConditionsTestUtil.userDefinedSegmentMatcher(SALES_PEOPLE, true); + Condition c = ConditionsTestUtil.and(employeesMatcher, notSalespeople, null); + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher employeesMatcherLogic = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher(EMPLOYEES)); + AttributeMatcher notSalesPeopleMatcherLogic = new AttributeMatcher(null, new UserDefinedSegmentMatcher(SALES_PEOPLE), true); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(employeesMatcherLogic, notSalesPeopleMatcherLogic)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + assertTrue(expected.hashCode() != 0); + assertTrue(expected.equals(expected)); + } + + @Test + public void worksForTwoConditions() { + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); + + Matcher salespeopleMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(SALES_PEOPLE, false); + + List fullyRollout = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); + List turnOff = Lists.newArrayList(ConditionsTestUtil.partition(Treatments.CONTROL, 100)); + + Condition c1 = ConditionsTestUtil.and(employeesMatcher, fullyRollout); + Condition c2 = ConditionsTestUtil.and(salespeopleMatcher, turnOff); + + List conditions = Lists.newArrayList(c1, c2); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + ParsedCondition parsedCondition1 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(EMPLOYEES)), fullyRollout); + ParsedCondition parsedCondition2 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(EMPLOYEES)), turnOff); + List listOfParsedConditions = Lists.newArrayList(parsedCondition1, parsedCondition2); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfParsedConditions, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void successForLongConditions() { + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee); + + Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); + + List conditions = Lists.newArrayList(); + List p1 = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); + for (int i = 0 ; i < CONDITIONS_UPPER_LIMIT+1 ; i++) { + Condition c = ConditionsTestUtil.and(employeesMatcher, p1); + conditions.add(c); + } + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + + Assert.assertNotNull(parser.parse(ruleBasedSegment)); + } + + @Test + public void worksWithAttributes() { + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher("user", "name", EMPLOYEES, false); + + Matcher creationDateNotOlderThanAPoint = ConditionsTestUtil.numericMatcher("user", "creation_date", + MatcherType.GREATER_THAN_OR_EQUAL_TO, + DataType.DATETIME, + 1457386741L, + true); + + Condition c = ConditionsTestUtil.and(employeesMatcher, creationDateNotOlderThanAPoint, null); + + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher employeesMatcherLogic = new AttributeMatcher("name", new UserDefinedSegmentMatcher(EMPLOYEES), false); + AttributeMatcher creationDateNotOlderThanAPointLogic = new AttributeMatcher("creation_date", new GreaterThanOrEqualToMatcher(1457386741L, DataType.DATETIME), true); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(employeesMatcherLogic, creationDateNotOlderThanAPointLogic)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void lessThanOrEqualTo() { + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher ageLessThan10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.LESS_THAN_OR_EQUAL_TO, DataType.NUMBER, 10L, false); + Condition c = ConditionsTestUtil.and(ageLessThan10, null); + + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher ageLessThan10Logic = new AttributeMatcher("age", new LessThanOrEqualToMatcher(10, DataType.NUMBER), false); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ageLessThan10Logic)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void equalTo() { + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher ageLessThan10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.EQUAL_TO, DataType.NUMBER, 10L, true); + Condition c = ConditionsTestUtil.and(ageLessThan10, null); + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher equalToMatcher = new AttributeMatcher("age", new EqualToMatcher(10, DataType.NUMBER), true); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(equalToMatcher)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void equalToNegativeNumber() { + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher equalToNegative10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.EQUAL_TO, DataType.NUMBER, -10L, false); + Condition c = ConditionsTestUtil.and(equalToNegative10, null); + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher ageEqualTo10Logic = new AttributeMatcher("age", new EqualToMatcher(-10, DataType.NUMBER), false); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ageEqualTo10Logic)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void between() { + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + Matcher ageBetween10And11 = ConditionsTestUtil.betweenMatcher("user", + "age", + DataType.NUMBER, + 10, + 12, + false); + + Condition c = ConditionsTestUtil.and(ageBetween10And11, null); + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher ageBetween10And11Logic = new AttributeMatcher("age", new BetweenMatcher(10, 12, DataType.NUMBER), false); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ageBetween10And11Logic)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + @Test + public void containsAnyOfSet() { + ArrayList set = Lists.newArrayList("sms", "voice"); + + Condition c = ConditionsTestUtil.containsAnyOfSet("user", + "products", + set, + false, + null + ); + + ContainsAnyOfSetMatcher m = new ContainsAnyOfSetMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void containsAllOfSet() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.containsAllOfSet("user", + "products", + set, + false, + null + ); + + ContainsAllOfSetMatcher m = new ContainsAllOfSetMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void equalToSet() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.equalToSet("user", + "products", + set, + false, + null + ); + + EqualToSetMatcher m = new EqualToSetMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void isPartOfSet() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.isPartOfSet("user", + "products", + set, + false, + null + ); + + PartOfSetMatcher m = new PartOfSetMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void startsWithString() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.startsWithString("user", + "products", + set, + false, + null + ); + + StartsWithAnyOfMatcher m = new StartsWithAnyOfMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void endsWithString() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.endsWithString("user", + "products", + set, + false, + null + ); + + EndsWithAnyOfMatcher m = new EndsWithAnyOfMatcher(set); + setMatcherTest(c, m); + } + + + @Test + public void containsString() { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.containsString("user", + "products", + set, + false, + null + ); + + ContainsAnyOfMatcher m = new ContainsAnyOfMatcher(set); + setMatcherTest(c, m); + } + + @Test + public void UnsupportedMatcher() { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String splitWithUndefinedMatcher = "{\"ff\":{\"s\":-1,\"t\":-1,\"d\":[]},\"rbs\":{\"s\":-1,\"t\":1457726098069,\"d\":[{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"status\": \"ACTIVE\",\"conditions\": [{\"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"UNKNOWN\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}],\"excluded\":{\"keys\":[],\"segments\":[]}}]}}"; + SplitChange change = Json.fromJson(splitWithUndefinedMatcher, SplitChange.class); + for (RuleBasedSegment ruleBasedSegment : change.ruleBasedSegments.d) { + // should not cause exception + ParsedRuleBasedSegment parsedRuleBasedSegment = parser.parse(ruleBasedSegment); + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label() == Labels.UNSUPPORTED_MATCHER); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" in segment all")); + } + } + } + } + + @Test + public void EqualToSemverMatcher() throws IOException { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (RuleBasedSegment ruleBasedSegment : change.ruleBasedSegments.d) { + // should not cause exception + ParsedRuleBasedSegment parsedRuleBasedSegment = parser.parse(ruleBasedSegment); + if (ruleBasedSegment.name.equals("rbs_semver_equalto")) { + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label().equals("equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" == semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void GreaterThanOrEqualSemverMatcher() throws IOException { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (RuleBasedSegment ruleBasedSegment : change.ruleBasedSegments.d) { + // should not cause exception + ParsedRuleBasedSegment parsedRuleBasedSegment = parser.parse(ruleBasedSegment); + if (ruleBasedSegment.name.equals("rbs_semver_greater_or_equalto")) { + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label().equals("greater than or equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" >= semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void LessThanOrEqualSemverMatcher() throws IOException { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (RuleBasedSegment ruleBasedSegment : change.ruleBasedSegments.d) { + // should not cause exception + ParsedRuleBasedSegment parsedRuleBasedSegment = parser.parse(ruleBasedSegment); + if (ruleBasedSegment.name.equals("rbs_semver_less_or_equalto")) { + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label().equals("less than or equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" <= semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void BetweenSemverMatcher() throws IOException { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(parser, change.ruleBasedSegments.d); + for (ParsedRuleBasedSegment parsedRuleBasedSegment : ruleBasedSegmentsToUpdate.getToAdd()) { + // should not cause exception + if (parsedRuleBasedSegment.ruleBasedSegment().equals("rbs_semver_between")) { + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label().equals("between semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" between semver 1\\.22\\.9 and 2\\.1\\.0")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void InListSemverMatcher() throws IOException { + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (RuleBasedSegment ruleBasedSegment : change.ruleBasedSegments.d) { + // should not cause exception + ParsedRuleBasedSegment parsedRuleBasedSegment = parser.parse(ruleBasedSegment); + if (ruleBasedSegment.name.equals("rbs_semver_inlist")) { + for (ParsedCondition parsedCondition : parsedRuleBasedSegment.parsedConditions()) { + assertTrue(parsedCondition.label().equals("in list semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().startsWith(" in semver list")); + return; + } + } + } + } + assertTrue(false); + } + + public void setMatcherTest(Condition c, io.split.engine.matchers.Matcher m) { + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); + SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + + ArrayList set = Lists.newArrayList("sms", "voice"); + List conditions = Lists.newArrayList(c); + + RuleBasedSegmentParser parser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("first-name", conditions, 1); + ParsedRuleBasedSegment actual = parser.parse(ruleBasedSegment); + + AttributeMatcher attrMatcher = new AttributeMatcher("products", m, false); + CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(attrMatcher)); + ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, null); + List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); + + ParsedRuleBasedSegment expected = ParsedRuleBasedSegment.createParsedRuleBasedSegmentForTests ("first-name", listOfMatcherAndSplits, "user", 1, + new ArrayList<>(), new ArrayList<>()); + + Assert.assertEquals(actual, expected); + } + + private SegmentChange getSegmentChange(long since, long till, String segmentName){ + SegmentChange segmentChange = new SegmentChange(); + segmentChange.name = segmentName; + segmentChange.since = since; + segmentChange.till = till; + segmentChange.added = new ArrayList<>(); + segmentChange.removed = new ArrayList<>(); + return segmentChange; + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpEventsTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpEventsTest.java new file mode 100644 index 00000000..2ad95840 --- /dev/null +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpEventsTest.java @@ -0,0 +1,174 @@ +package io.split.engine.experiments; + +import io.split.client.api.SdkEventType; +import io.split.client.dtos.ChangeDto; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.Status; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.common.FetchOptions; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SplitFetcherImpEventsTest { + + private SplitChangeFetcher _changeFetcher; + private SplitCacheProducer _splitCache; + private RuleBasedSegmentCacheProducer _rbsCache; + private SdkEventsNotifier _notifier; + private SplitFetcherImp _fetcher; + + @Before + public void setUp() throws Exception { + _changeFetcher = Mockito.mock(SplitChangeFetcher.class); + _splitCache = Mockito.mock(SplitCacheProducer.class); + _rbsCache = Mockito.mock(RuleBasedSegmentCacheProducer.class); + Mockito.when(_splitCache.getChangeNumber()).thenReturn(-1L); + Mockito.when(_rbsCache.getChangeNumber()).thenReturn(-1L); + _notifier = Mockito.mock(SdkEventsNotifier.class); + _fetcher = new SplitFetcherImp(_changeFetcher, new SplitParser(), _splitCache, + Mockito.mock(TelemetryRuntimeProducer.class), new FlagSetsFilterImpl(new HashSet()), + new RuleBasedSegmentParser(), _rbsCache, _notifier); + } + + @Test + public void flagChangeEmitsFlagsUpdatedWithExactNames() throws Exception { + givenChange(flags(activeFlag("f1"), archivedFlag("f2")), rbs()); + + _fetcher.forceRefresh(new FetchOptions.Builder().build()); + + SdkEventMetadata metadata = verifyEmitted(SdkInternalEvent.FLAGS_UPDATED); + assertEquals(SdkEventType.FLAGS_UPDATE, metadata.getType()); + assertEquals(new HashSet<>(Arrays.asList("f1", "f2")), metadata.getNames()); + } + + @Test + public void rbsOnlyChangeEmitsRuleBasedSegmentsUpdatedWithEmptyNames() throws Exception { + givenChange(flags(), rbs(archivedRbs("r1"))); + + _fetcher.forceRefresh(new FetchOptions.Builder().build()); + + SdkEventMetadata metadata = verifyEmitted(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED); + assertEquals(SdkEventType.SEGMENTS_UPDATE, metadata.getType()); + assertTrue(metadata.getNames().isEmpty()); + } + + @Test + public void flagAndRbsChangeEmitsOnlyFlagsUpdated() throws Exception { + givenChange(flags(activeFlag("f1")), rbs(archivedRbs("r1"))); + + _fetcher.forceRefresh(new FetchOptions.Builder().build()); + + SdkEventMetadata metadata = verifyEmitted(SdkInternalEvent.FLAGS_UPDATED); + assertEquals(SdkEventType.FLAGS_UPDATE, metadata.getType()); + assertEquals(Collections.singleton("f1"), metadata.getNames()); + } + + @Test + public void emptyPollEmitsNothing() throws Exception { + givenChange(flags(), rbs()); + + _fetcher.forceRefresh(new FetchOptions.Builder().build()); + + Mockito.verifyZeroInteractions(_notifier); + } + + @Test + public void staleChangeEmitsNothing() throws Exception { + Mockito.when(_splitCache.getChangeNumber()).thenReturn(500L); + givenChange(flags(activeFlag("f1")), rbs()); + + _fetcher.forceRefresh(new FetchOptions.Builder().build()); + + Mockito.verifyZeroInteractions(_notifier); + } + + @Test + public void defaultConstructorStillWorks() throws Exception { + givenChange(flags(activeFlag("f1")), rbs()); + SplitFetcherImp plain = new SplitFetcherImp(_changeFetcher, new SplitParser(), _splitCache, + Mockito.mock(TelemetryRuntimeProducer.class), new FlagSetsFilterImpl(new HashSet()), + new RuleBasedSegmentParser(), _rbsCache); + + plain.forceRefresh(new FetchOptions.Builder().build()); + + Mockito.verifyZeroInteractions(_notifier); + } + + private SdkEventMetadata verifyEmitted(SdkInternalEvent event) { + ArgumentCaptor captor = ArgumentCaptor.forClass(SdkEventMetadata.class); + Mockito.verify(_notifier).notify(Mockito.eq(event), captor.capture()); + Mockito.verifyNoMoreInteractions(_notifier); + return captor.getValue(); + } + + private void givenChange(ChangeDto ff, ChangeDto rbs) throws Exception { + SplitChange change = new SplitChange(); + change.featureFlags = ff; + change.ruleBasedSegments = rbs; + Mockito.when(_changeFetcher.fetch(Mockito.anyLong(), Mockito.anyLong(), Mockito.any(FetchOptions.class))) + .thenReturn(change); + } + + private static ChangeDto flags(Split... splits) { + ChangeDto dto = new ChangeDto<>(); + dto.s = -1; + dto.t = splits.length == 0 ? -1 : 100; + dto.d = new ArrayList<>(Arrays.asList(splits)); + return dto; + } + + private static ChangeDto rbs(RuleBasedSegment... segments) { + ChangeDto dto = new ChangeDto<>(); + dto.s = -1; + dto.t = segments.length == 0 ? -1 : 100; + dto.d = new ArrayList<>(Arrays.asList(segments)); + return dto; + } + + private static Split activeFlag(String name) { + Split split = new Split(); + split.name = name; + split.status = Status.ACTIVE; + split.defaultTreatment = "off"; + split.trafficTypeName = "user"; + split.trafficAllocation = 100; + split.trafficAllocationSeed = 1; + split.algo = 2; + split.conditions = new ArrayList<>(); + split.prerequisites = new ArrayList<>(); + return split; + } + + private static Split archivedFlag(String name) { + Split split = new Split(); + split.name = name; + split.status = Status.ARCHIVED; + return split; + } + + private static RuleBasedSegment archivedRbs(String name) { + RuleBasedSegment segment = new RuleBasedSegment(); + segment.name = name; + segment.status = Status.ARCHIVED; + return segment; + } +} diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java new file mode 100644 index 00000000..78b6eac6 --- /dev/null +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherImpTest.java @@ -0,0 +1,243 @@ +package io.split.engine.experiments; + +import io.split.SplitMockServer; +import io.split.client.*; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.interceptors.GzipDecoderResponseInterceptor; +import io.split.client.interceptors.GzipEncoderRequestInterceptor; +import io.split.client.utils.*; +import io.split.engine.common.FetchOptions; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SplitCache; +import io.split.storages.SplitCacheProducer; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.NoopTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.storage.TelemetryStorageProducer; +import okhttp3.mockwebserver.MockResponse; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.cookie.StandardCookieSpec; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.util.Timeout; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.*; + +public class SplitFetcherImpTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); + private static final String TEST_FLAG_SETS = "{\"ff\":{\"d\":[{\"trafficTypeName\":\"client\",\"name\":\"workm\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set_1\",\"set_2\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"client\",\"name\":\"workm_set_3\",\"trafficAllocation\":100,\"trafficAllocationSeed\":147392224,\"seed\":524417105,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"on\",\"changeNumber\":1602796638344,\"algo\":2,\"configurations\":{},\"sets\":[\"set_3\"],\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"new_segment\"},\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":100},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"in segment new_segment\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"client\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":100},{\"treatment\":\"off\",\"size\":0},{\"treatment\":\"free\",\"size\":0},{\"treatment\":\"conta\",\"size\":0}],\"label\":\"default rule\"}]}],\"s\":-1,\"t\":1602796638344},\"rbs\":{\"d\":[],\"t\":-1,\"s\":-1}}"; + + @Test + public void testFetchingSplitsAndRuleBasedSegments() throws Exception { + MockResponse response = new MockResponse().setBody("{" + + "\"ff\":{" + + "\"t\":1675095324253," + + "\"s\":-1," + + "\"d\": [{" + + "\"changeNumber\": 123," + + "\"trafficTypeName\": \"user\"," + + "\"name\": \"some_name\"," + + "\"trafficAllocation\": 100," + + "\"trafficAllocationSeed\": 123456," + + "\"seed\": 321654," + + "\"status\": \"ACTIVE\"," + + "\"killed\": false," + + "\"defaultTreatment\": \"off\"," + + "\"algo\": 2," + + "\"conditions\": [{" + + "\"partitions\": [{\"treatment\": \"on\", \"size\": 50},{\"treatment\": \"off\", \"size\": 50}]," + + "\"contitionType\": \"WHITELIST\"," + + "\"label\": \"some_label\"," + + "\"matcherGroup\": {" + + "\"matchers\": [{\"matcherType\": \"WHITELIST\",\"whitelistMatcherData\": {\"whitelist\": [\"k1\", \"k2\", \"k3\"]},\"negate\": false}]," + + "\"combiner\": \"AND\"}" + + "},{" + + "\"conditionType\": \"ROLLOUT\"," + + "\"matcherGroup\": {\"combiner\": \"AND\"," + + "\"matchers\": [{\"keySelector\": {\"trafficType\": \"user\"},\"matcherType\": \"IN_RULE_BASED_SEGMENT\",\"negate\": false,\"userDefinedSegmentMatcherData\": {\"segmentName\": \"sample_rule_based_segment\"}}]" + + "}," + + "\"partitions\": [{\"treatment\": \"on\",\"size\": 100},{\"treatment\": \"off\",\"size\": 0}]," + + "\"label\": \"in rule based segment sample_rule_based_segment\"" + + "}]," + + "\"sets\": [\"set1\", \"set2\"]}]" + + "}," + + "\"rbs\": {" + + "\"t\": 1585948850111," + + "\"s\": -1," + + "\"d\": [" + + "{" + + "\"changeNumber\": 5," + + "\"name\": \"sample_rule_based_segment\"," + + "\"status\": \"ACTIVE\"," + + "\"trafficTypeName\": \"user\"," + + "\"excluded\":{" + + "\"keys\":[\"mauro@split.io\",\"gaston@split.io\"]," + + "\"segments\":[]" + + "}," + + "\"conditions\": [" + + "{" + + "\"matcherGroup\": {" + + "\"combiner\": \"AND\"," + + "\"matchers\": [" + + "{" + + "\"keySelector\": {" + + "\"trafficType\": \"user\"," + + "\"attribute\": \"email\"" + + "}," + + "\"matcherType\": \"ENDS_WITH\"," + + "\"negate\": false," + + "\"whitelistMatcherData\": {" + + "\"whitelist\": [" + + "\"@split.io\"" + + "]}}]}}]}]}}"); + MockResponse response2 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1675095324253, \"t\":1685095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response3 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1685095324253, \"t\":1695095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response4 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1695095324253, \"t\":1775095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + MockResponse response5 = new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1775095324253, \"t\":1775095324253}, \"rbs\":{\"d\":[],\"s\":1585948850111,\"t\":1585948850111}}"); + Queue responses = new LinkedList<>(); + responses.add(response); + Queue responses2 = new LinkedList<>(); + responses2.add(response2); + Queue responses3 = new LinkedList<>(); + responses3.add(response3); + Queue responses4 = new LinkedList<>(); + responses4.add(response4); + Queue responses5 = new LinkedList<>(); + responses5.add(response5); + SplitMockServer splitServer = new SplitMockServer(CustomDispatcher2.builder() + .path(CustomDispatcher2.SPLIT_FETCHER_1, responses) + .path(CustomDispatcher2.SPLIT_FETCHER_2, responses2) + .path(CustomDispatcher2.SPLIT_FETCHER_3, responses3) + .path(CustomDispatcher2.SPLIT_FETCHER_4, responses4) + .path(CustomDispatcher2.SPLIT_FETCHER_5, responses5) + .build()); + splitServer.start(); + + SplitClientConfig config = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .endpoint(splitServer.getUrl(), splitServer.getUrl()) + .featuresRefreshRate(20) + .segmentsRefreshRate(30) + .streamingEnabled(false) + .build(); + + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(config.getSetsFilter()); + SplitCache splitCache = new InMemoryCacheImp(flagSetsFilter); + RequestDecorator _requestDecorator = new RequestDecorator(config.customHeaderDecorator()); + SDKMetadata _sdkMetadata = new SDKMetadata("1.1.1", "ip", "machineName"); + RequestConfig requestConfig = RequestConfig.custom() + .setConnectTimeout(Timeout.ofMilliseconds(config.connectionTimeout())) + .setCookieSpec(StandardCookieSpec.STRICT) + .build(); + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + TelemetryStorageProducer _telemetryStorageProducer = telemetryStorage; + + HttpClientBuilder httpClientbuilder = HttpClients.custom() + .setDefaultRequestConfig(requestConfig) + .addRequestInterceptorLast(new GzipEncoderRequestInterceptor()) + .addResponseInterceptorLast((new GzipDecoderResponseInterceptor())); + SplitHttpClient _splitHttpClient = SplitHttpClientImpl.create(httpClientbuilder.build(), + _requestDecorator, + "apiToken", + _sdkMetadata); + URI _rootTarget = URI.create(config.endpoint()); + SplitChangeFetcher splitChangeFetcher = HttpSplitChangeFetcher.create(_splitHttpClient, _rootTarget, + _telemetryStorageProducer, config.isSdkEndpointOverridden()); + SplitFetcherImp splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer, + flagSetsFilter, ruleBasedSegmentParser, ruleBasedSegmentCache); + + splitFetcher.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(false).build()); + splitServer.stop(); + Assert.assertEquals("some_name", splitCache.get("some_name").feature()); + Assert.assertEquals("sample_rule_based_segment", ruleBasedSegmentCache.get("sample_rule_based_segment").ruleBasedSegment()); + } + + @Test + public void testLocalHost() { + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(flagSetsFilter); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/split_init.json"); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + SplitParser splitParser = new SplitParser(); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + + FetchResult fetchResult = splitFetcher.forceRefresh(fetchOptions); + + Assert.assertEquals(2, fetchResult.getSegments().size()); + } + + @Test + public void testLocalHostFlagSets() throws IOException { + File file = folder.newFile("test_0.json"); + + byte[] test = TEST_FLAG_SETS.getBytes(); + com.google.common.io.Files.write(test, file); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider(file.getAbsolutePath()); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_1"))); + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(flagSetsFilter); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + SplitParser splitParser = new SplitParser(); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + + FetchResult fetchResult = splitFetcher.forceRefresh(fetchOptions); + + Assert.assertEquals(1, fetchResult.getSegments().size()); + } + + @Test + public void testLocalHostFlagSetsNotIntersect() throws IOException { + File file = folder.newFile("test_0.json"); + + byte[] test = TEST_FLAG_SETS.getBytes(); + com.google.common.io.Files.write(test, file); + + InputStreamProvider inputStreamProvider = new FileInputStreamProvider(file.getAbsolutePath()); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_4"))); + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(flagSetsFilter); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + SplitParser splitParser = new SplitParser(); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + + FetchResult fetchResult = splitFetcher.forceRefresh(fetchOptions); + + Assert.assertEquals(0, fetchResult.getSegments().size()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java b/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java index cb935299..a6c2468a 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitFetcherTest.java @@ -1,25 +1,35 @@ package io.split.engine.experiments; import com.google.common.collect.Lists; -import io.split.cache.InMemoryCacheImp; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; -import io.split.cache.SplitCache; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.storages.*; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; import io.split.client.dtos.*; import io.split.engine.ConditionsTestUtil; -import io.split.engine.SDKReadinessGates; +import io.split.engine.common.FetchOptions; import io.split.engine.matchers.AllKeysMatcher; import io.split.engine.matchers.CombiningMatcher; import io.split.engine.segments.SegmentChangeFetcher; import io.split.engine.segments.SegmentSynchronizationTask; import io.split.engine.segments.SegmentSynchronizationTaskImp; import io.split.grammar.Treatments; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -30,6 +40,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.*; import static org.mockito.Mockito.mock; @@ -40,31 +51,34 @@ */ public class SplitFetcherTest { private static final Logger _log = LoggerFactory.getLogger(SplitFetcherTest.class); + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); @Test - public void works_when_we_start_without_any_state() throws InterruptedException { + @Ignore //This test is ignore since is deprecated. We can review this in a future. + public void worksWhenWeStartWithoutAnyState() throws InterruptedException { works(0); } @Test - public void works_when_we_start_with_any_state() throws InterruptedException { + @Ignore //This test is ignore since is deprecated. We can review this in a future. + public void worksWhenWeStartWithAnyState() throws InterruptedException { works(11L); } private void works(long startingChangeNumber) throws InterruptedException { AChangePerCallSplitChangeFetcher splitChangeFetcher = new AChangePerCallSplitChangeFetcher(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); - SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1,10, gates, segmentCache); - SplitCache cache = new InMemoryCacheImp(startingChangeNumber); - SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(segmentSynchronizationTask, segmentCache), gates, cache); + SplitCache cache = new InMemoryCacheImp(startingChangeNumber, FLAG_SETS_FILTER); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(), cache, TELEMETRY_STORAGE, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); // execute the fetcher for a little bit. executeWaitAndTerminate(fetcher, 1, 3, TimeUnit.SECONDS); assertThat(splitChangeFetcher.lastAdded(), is(greaterThan(startingChangeNumber))); - assertThat(cache.getChangeNumber(), is(equalTo(splitChangeFetcher.lastAdded()))); // all previous splits have been removed since they are dead for (long i = startingChangeNumber; i < cache.getChangeNumber(); i++) { @@ -74,17 +88,15 @@ private void works(long startingChangeNumber) throws InterruptedException { ParsedCondition expectedParsedCondition = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); List expectedListOfMatcherAndSplits = Lists.newArrayList(expectedParsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("" + cache.getChangeNumber(), (int) cache.getChangeNumber(), false, Treatments.OFF, expectedListOfMatcherAndSplits, null, cache.getChangeNumber(), 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("" + cache.getChangeNumber(), (int) cache.getChangeNumber(), false, Treatments.OFF, expectedListOfMatcherAndSplits, null, cache.getChangeNumber(), 1, new HashSet<>(), true, null); ParsedSplit actual = cache.get("" + cache.getChangeNumber()); - + Thread.sleep(1000); assertThat(actual, is(equalTo(expected))); - assertThat(gates.areSplitsReady(0), is(equalTo(true))); } @Test - public void when_parser_fails_we_remove_the_experiment() throws InterruptedException { - SDKReadinessGates gates = new SDKReadinessGates(); + public void whenParserFailsWeRemoveTheExperiment() throws InterruptedException { Split validSplit = new Split(); validSplit.status = Status.ACTIVE; validSplit.seed = (int) -1; @@ -93,9 +105,14 @@ public void when_parser_fails_we_remove_the_experiment() throws InterruptedExcep validSplit.name = "-1"; SplitChange validReturn = new SplitChange(); - validReturn.splits = Lists.newArrayList(validSplit); - validReturn.since = -1L; - validReturn.till = 0L; + validReturn.featureFlags = new ChangeDto<>(); + validReturn.featureFlags.d = Lists.newArrayList(validSplit); + validReturn.featureFlags.s = -1L; + validReturn.featureFlags.t = 0L; + validReturn.ruleBasedSegments = new ChangeDto<>(); + validReturn.ruleBasedSegments.t = -1; + validReturn.ruleBasedSegments.s = -1; + validReturn.ruleBasedSegments.d = new ArrayList<>(); MatcherGroup invalidMatcherGroup = new MatcherGroup(); invalidMatcherGroup.matchers = Lists.newArrayList(); @@ -112,55 +129,128 @@ public void when_parser_fails_we_remove_the_experiment() throws InterruptedExcep invalidSplit.name = "-1"; SplitChange invalidReturn = new SplitChange(); - invalidReturn.splits = Lists.newArrayList(invalidSplit); - invalidReturn.since = 0L; - invalidReturn.till = 1L; + invalidReturn.featureFlags = new ChangeDto<>(); + invalidReturn.featureFlags.d = Lists.newArrayList(invalidSplit); + invalidReturn.featureFlags.s = 0L; + invalidReturn.featureFlags.t = 1L; + invalidReturn.ruleBasedSegments = new ChangeDto<>(); + invalidReturn.ruleBasedSegments.t = -1; + invalidReturn.ruleBasedSegments.s = -1; + invalidReturn.ruleBasedSegments.d = new ArrayList<>(); SplitChange noReturn = new SplitChange(); - noReturn.splits = Lists.newArrayList(); - noReturn.since = 1L; - noReturn.till = 1L; + noReturn.featureFlags = new ChangeDto<>(); + noReturn.featureFlags.d = Lists.newArrayList(); + noReturn.featureFlags.s = 1L; + noReturn.featureFlags.t = 1L; + noReturn.ruleBasedSegments = new ChangeDto<>(); + noReturn.ruleBasedSegments.t = -1; + noReturn.ruleBasedSegments.s = -1; + noReturn.ruleBasedSegments.d = new ArrayList<>(); SplitChangeFetcher splitChangeFetcher = mock(SplitChangeFetcher.class); - when(splitChangeFetcher.fetch(-1L, false)).thenReturn(validReturn); - when(splitChangeFetcher.fetch(0L, false)).thenReturn(invalidReturn); - when(splitChangeFetcher.fetch(1L, false)).thenReturn(noReturn); + when(splitChangeFetcher.fetch(Mockito.eq(-1L), Mockito.eq(-1L), Mockito.any())).thenReturn(validReturn); + when(splitChangeFetcher.fetch(Mockito.eq(0L), Mockito.eq(-1L), Mockito.any())).thenReturn(invalidReturn); + when(splitChangeFetcher.fetch(Mockito.eq(1L), Mockito.eq(-1L), Mockito.any())).thenReturn(noReturn); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + SplitCache cache = new InMemoryCacheImp(-1, FLAG_SETS_FILTER); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); SegmentChangeFetcher segmentChangeFetcher = mock(SegmentChangeFetcher.class); - SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, gates, segmentCache); - segmentSynchronizationTask.startPeriodicFetching(); - SplitCache cache = new InMemoryCacheImp(-1); - SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(segmentSynchronizationTask, segmentCache), new SDKReadinessGates(), cache); + SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, segmentCache, TELEMETRY_STORAGE, cache, null, ruleBasedSegmentCache); + segmentSynchronizationTask.start(); + SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(), cache, TELEMETRY_STORAGE, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCache); + // execute the fetcher for a little bit. executeWaitAndTerminate(fetcher, 1, 5, TimeUnit.SECONDS); - assertThat(cache.getChangeNumber(), is(equalTo(1L))); + assertEquals(1L, cache.getChangeNumber()); // verify that the fetcher return null - assertThat(cache.get("-1"), is(nullValue())); + Assert.assertNull(cache.get("-1")); } @Test - public void if_there_is_a_problem_talking_to_split_change_count_down_latch_is_not_decremented() throws Exception { - SDKReadinessGates gates = new SDKReadinessGates(); - SplitCache cache = new InMemoryCacheImp(-1); + public void ifThereIsAProblemTalkingToSplitChangeCountDownLatchIsNotDecremented() throws Exception { + SplitCache cache = new InMemoryCacheImp(-1, FLAG_SETS_FILTER); SplitChangeFetcher splitChangeFetcher = mock(SplitChangeFetcher.class); - when(splitChangeFetcher.fetch(-1L, false)).thenThrow(new RuntimeException()); + when(splitChangeFetcher.fetch(-1L, -1, new FetchOptions.Builder().build())).thenThrow(new RuntimeException()); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); SegmentChangeFetcher segmentChangeFetcher = mock(SegmentChangeFetcher.class); - SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, gates, segmentCache); - segmentSynchronizationTask.startPeriodicFetching(); - SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(segmentSynchronizationTask, segmentCache), gates, cache); + SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, segmentCache, TELEMETRY_STORAGE, cache, null, ruleBasedSegmentCache); + segmentSynchronizationTask.start(); + SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(), cache, TELEMETRY_STORAGE, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCache); // execute the fetcher for a little bit. executeWaitAndTerminate(fetcher, 1, 5, TimeUnit.SECONDS); - assertThat(cache.getChangeNumber(), is(equalTo(-1L))); - assertThat(gates.areSplitsReady(0), is(equalTo(false))); + Assert.assertEquals(-1L, cache.getChangeNumber()); + } + + @Test + public void addFeatureFlags() throws InterruptedException { + SplitCache cache = new InMemoryCacheImp(-1, new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_1", "set_2")))); + + Split featureFlag1 = new Split(); + featureFlag1.status = Status.ACTIVE; + featureFlag1.seed = (int) -1; + featureFlag1.conditions = Lists.newArrayList(ConditionsTestUtil.makeAllKeysCondition(Lists.newArrayList(ConditionsTestUtil.partition("on", 10)))); + featureFlag1.defaultTreatment = Treatments.OFF; + featureFlag1.name = "feature_flag"; + featureFlag1.sets = new HashSet<>(Arrays.asList("set_1", "set_2")); + featureFlag1.trafficAllocation = 100; + featureFlag1.trafficAllocationSeed = 147392224; + + SplitChange validReturn = new SplitChange(); + validReturn.featureFlags = new ChangeDto<>(); + validReturn.featureFlags.d = Lists.newArrayList(featureFlag1); + validReturn.featureFlags.s = -1L; + validReturn.featureFlags.t = 0L; + validReturn.ruleBasedSegments = new ChangeDto<>(); + validReturn.ruleBasedSegments.t = -1; + validReturn.ruleBasedSegments.s = -1; + validReturn.ruleBasedSegments.d = new ArrayList<>(); + + SplitChangeFetcher splitChangeFetcher = mock(SplitChangeFetcher.class); + when(splitChangeFetcher.fetch(Mockito.eq(-1L), Mockito.eq(-1L), Mockito.any())).thenReturn(validReturn); + + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set_1", "set_2"))); + SplitFetcherImp fetcher = new SplitFetcherImp(splitChangeFetcher, new SplitParser(), cache, TELEMETRY_STORAGE, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCache); + + executeWaitAndTerminate(fetcher, 1, 5, TimeUnit.SECONDS); + + Assert.assertTrue(cache.getNamesByFlagSets(Arrays.asList("set_1", "set_2")).get("set_1").contains("feature_flag")); + Assert.assertTrue(cache.getNamesByFlagSets(Arrays.asList("set_1", "set_2")).get("set_2").contains("feature_flag")); + + featureFlag1.sets.remove("set_2"); + + validReturn = new SplitChange(); + validReturn.featureFlags = new ChangeDto<>(); + validReturn.featureFlags.d = Lists.newArrayList(featureFlag1); + validReturn.featureFlags.s = 0L; + validReturn.featureFlags.t = 1L; + validReturn.ruleBasedSegments = new ChangeDto<>(); + validReturn.ruleBasedSegments.t = -1; + validReturn.ruleBasedSegments.s = -1; + validReturn.ruleBasedSegments.d = new ArrayList<>(); + + when(splitChangeFetcher.fetch(Mockito.eq(0L), Mockito.eq(-1L), Mockito.any())).thenReturn(validReturn); + + executeWaitAndTerminate(fetcher, 1, 5, TimeUnit.SECONDS); + + Assert.assertTrue(cache.getNamesByFlagSets(Arrays.asList("set_1", "set_2")).get("set_1").contains("feature_flag")); + Assert.assertFalse(cache.getNamesByFlagSets(Arrays.asList("set_1", "set_2")).get("set_2").contains("feature_flag")); } private void executeWaitAndTerminate(Runnable runnable, long frequency, long waitInBetween, TimeUnit unit) throws InterruptedException { @@ -183,20 +273,23 @@ private void executeWaitAndTerminate(Runnable runnable, long frequency, long wai } @Test - public void works_with_user_defined_segments() throws Exception { + @Ignore //This test is ignore since is deprecated. We can review this in a future. + public void worksWithUserDefinedSegments() throws Exception { long startingChangeNumber = -1; String segmentName = "foosegment"; AChangePerCallSplitChangeFetcher experimentChangeFetcher = new AChangePerCallSplitChangeFetcher(segmentName); - SDKReadinessGates gates = new SDKReadinessGates(); - SplitCache cache = new InMemoryCacheImp(startingChangeNumber); + SplitCache cache = new InMemoryCacheImp(startingChangeNumber, FLAG_SETS_FILTER); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); SegmentChangeFetcher segmentChangeFetcher = mock(SegmentChangeFetcher.class); SegmentChange segmentChange = getSegmentChange(0L, 0L, segmentName); - when(segmentChangeFetcher.fetch(anyString(), anyLong(), anyBoolean())).thenReturn(segmentChange); - SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, gates, segmentCache); - segmentSynchronizationTask.startPeriodicFetching(); - SplitFetcherImp fetcher = new SplitFetcherImp(experimentChangeFetcher, new SplitParser(segmentSynchronizationTask, segmentCache), gates, cache); + when(segmentChangeFetcher.fetch(anyString(), anyLong(), any())).thenReturn(segmentChange); + SegmentSynchronizationTask segmentSynchronizationTask = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1,10, segmentCache, Mockito.mock(TelemetryStorage.class), cache, null, ruleBasedSegmentCache); + segmentSynchronizationTask.start(); + SplitFetcherImp fetcher = new SplitFetcherImp(experimentChangeFetcher, new SplitParser(), cache, TELEMETRY_STORAGE, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCache); // execute the fetcher for a little bit. executeWaitAndTerminate(fetcher, 1, 5, TimeUnit.SECONDS); @@ -209,11 +302,59 @@ public void works_with_user_defined_segments() throws Exception { assertThat("Asking for " + i + " " + cache.getAll(), cache.get("" + i), is(not(nullValue()))); assertThat(cache.get("" + i).killed(), is(true)); } + } - assertThat(gates.areSplitsReady(0), is(equalTo(true))); - assertThat(gates.isSegmentRegistered(segmentName), is(equalTo(true))); - assertThat(gates.areSegmentsReady(100), is(equalTo(true))); - assertThat(gates.isSDKReady(0), is(equalTo(true))); + @Test + public void testBypassCdnClearedAfterFirstHit() { + SplitChangeFetcher mockFetcher = Mockito.mock(SplitChangeFetcher.class); + SplitParser mockParser = new SplitParser(); + SplitCache mockCache = new InMemoryCacheImp(FLAG_SETS_FILTER); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitFetcherImp fetcher = new SplitFetcherImp(mockFetcher, mockParser, mockCache, Mockito.mock(TelemetryRuntimeProducer.class), FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + + SplitChange response1 = new SplitChange(); + response1.featureFlags = new ChangeDto<>(); + response1.featureFlags.d = new ArrayList<>(); + response1.featureFlags.s = -1; + response1.featureFlags.t = 1; + response1.ruleBasedSegments = new ChangeDto<>(); + response1.ruleBasedSegments.t = -1; + response1.ruleBasedSegments.s = -1; + response1.ruleBasedSegments.d = new ArrayList<>(); + + SplitChange response2 = new SplitChange(); + response2.featureFlags = new ChangeDto<>(); + response2.featureFlags.d = new ArrayList<>(); + response2.featureFlags.s = 1; + response2.featureFlags.t = 1; + response2.ruleBasedSegments = new ChangeDto<>(); + response2.ruleBasedSegments.t = -1; + response2.ruleBasedSegments.s = -1; + response2.ruleBasedSegments.d = new ArrayList<>(); + + ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); + ArgumentCaptor cnCaptor = ArgumentCaptor.forClass(Long.class); + ArgumentCaptor rbsCnCaptor = ArgumentCaptor.forClass(Long.class); + when(mockFetcher.fetch(cnCaptor.capture(), rbsCnCaptor.capture(), optionsCaptor.capture())).thenReturn(response1, response2); + + FetchOptions originalOptions = new FetchOptions.Builder().targetChangeNumber(123).targetChangeNumberRBS(-1).build(); + fetcher.forceRefresh(originalOptions); + List capturedCNs = cnCaptor.getAllValues(); + List capturedOptions = optionsCaptor.getAllValues(); + + Assert.assertEquals(capturedOptions.size(), 2); + Assert.assertEquals(capturedCNs.size(), 2); + + Assert.assertEquals(capturedCNs.get(0), Long.valueOf(-1)); + Assert.assertEquals(capturedCNs.get(1), Long.valueOf(1)); + + Assert.assertEquals(capturedOptions.get(0).targetCN(), 123); + Assert.assertEquals(capturedOptions.get(1).targetCN(), -1); + + // Ensure that the original value hasn't been modified + Assert.assertEquals(originalOptions.targetCN(), 123); } private SegmentChange getSegmentChange(long since, long till, String segmentName){ @@ -225,4 +366,4 @@ private SegmentChange getSegmentChange(long since, long till, String segmentName segmentChange.removed = new ArrayList<>(); return segmentChange; } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/SplitParserTest.java b/client/src/test/java/io/split/engine/experiments/SplitParserTest.java index 86beed26..d9e945bf 100644 --- a/client/src/test/java/io/split/engine/experiments/SplitParserTest.java +++ b/client/src/test/java/io/split/engine/experiments/SplitParserTest.java @@ -1,11 +1,17 @@ package io.split.engine.experiments; import com.google.common.collect.Lists; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; -import io.split.client.dtos.*; -import io.split.engine.ConditionsTestUtil; -import io.split.engine.SDKReadinessGates; +import io.split.client.dtos.Condition; +import io.split.client.dtos.DataType; +import io.split.client.dtos.Matcher; +import io.split.client.dtos.MatcherCombiner; +import io.split.client.dtos.MatcherType; +import io.split.client.dtos.Partition; +import io.split.client.dtos.SegmentChange; +import io.split.client.dtos.Split; +import io.split.client.dtos.SplitChange; +import io.split.client.dtos.Status; +import io.split.engine.matchers.PrerequisitesMatcher; import io.split.engine.matchers.AttributeMatcher; import io.split.engine.matchers.BetweenMatcher; import io.split.engine.matchers.CombiningMatcher; @@ -13,6 +19,11 @@ import io.split.engine.matchers.GreaterThanOrEqualToMatcher; import io.split.engine.matchers.LessThanOrEqualToMatcher; import io.split.engine.matchers.UserDefinedSegmentMatcher; +import io.split.storages.SegmentCache; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import io.split.client.utils.Json; +import io.split.engine.evaluator.Labels; +import io.split.engine.ConditionsTestUtil; import io.split.engine.matchers.collections.ContainsAllOfSetMatcher; import io.split.engine.matchers.collections.ContainsAnyOfSetMatcher; import io.split.engine.matchers.collections.EqualToSetMatcher; @@ -21,23 +32,25 @@ import io.split.engine.matchers.strings.EndsWithAnyOfMatcher; import io.split.engine.matchers.strings.StartsWithAnyOfMatcher; import io.split.engine.segments.SegmentChangeFetcher; -import io.split.engine.segments.SegmentSynchronizationTask; -import io.split.engine.segments.SegmentSynchronizationTaskImp; import io.split.grammar.Treatments; +import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; /** * Tests for ExperimentParser @@ -48,20 +61,19 @@ public class SplitParserTest { public static final String EMPLOYEES = "employees"; public static final String SALES_PEOPLE = "salespeople"; + public static final int CONDITIONS_UPPER_LIMIT = 50; @Test public void works() { - SDKReadinessGates gates = new SDKReadinessGates(); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>()); - segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>()); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); @@ -73,35 +85,35 @@ public void works() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); - AttributeMatcher employeesMatcherLogic = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher(segmentCache, EMPLOYEES)); - AttributeMatcher notSalesPeopleMatcherLogic = new AttributeMatcher(null, new UserDefinedSegmentMatcher(segmentCache,SALES_PEOPLE), true); + AttributeMatcher employeesMatcherLogic = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher(EMPLOYEES)); + AttributeMatcher notSalesPeopleMatcherLogic = new AttributeMatcher(null, new UserDefinedSegmentMatcher(SALES_PEOPLE), true); CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(employeesMatcherLogic, notSalesPeopleMatcherLogic)); ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); + assertTrue(expected.hashCode() != 0); + assertTrue(expected.equals(expected)); + Assert.assertEquals(expected.toString(), actual.toString()); } @Test public void worksWithConfig() { - SDKReadinessGates gates = new SDKReadinessGates(); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>()); - segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>()); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); @@ -116,36 +128,49 @@ public void worksWithConfig() { Map configurations = new HashMap<>(); configurations.put("on", "{\"size\":15,\"test\":20}"); configurations.put("off", "{\"size\":10}"); - Split split = makeSplit("first.name", 123, conditions, 1, configurations); + Split split = makeSplit("first-name", 123, conditions, 1, configurations); ParsedSplit actual = parser.parse(split); - AttributeMatcher employeesMatcherLogic = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher(segmentCache, EMPLOYEES)); - AttributeMatcher notSalesPeopleMatcherLogic = new AttributeMatcher(null, new UserDefinedSegmentMatcher(segmentCache,SALES_PEOPLE), true); + AttributeMatcher employeesMatcherLogic = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher(EMPLOYEES)); + AttributeMatcher notSalesPeopleMatcherLogic = new AttributeMatcher(null, new UserDefinedSegmentMatcher(SALES_PEOPLE), true); CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(employeesMatcherLogic, notSalesPeopleMatcherLogic)); ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, configurations); - - assertThat(actual, is(equalTo(expected))); - assertThat(actual.configurations().get("on"), is(equalTo(configurations.get("on")))); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, + listOfMatcherAndSplits, "user", 1, 1, configurations, new HashSet<>(), false, new PrerequisitesMatcher(null)); + + Assert.assertEquals(actual.parsedConditions(), expected.parsedConditions()); + Assert.assertEquals(actual.feature(), expected.feature()); + Assert.assertEquals(actual.changeNumber(), expected.changeNumber()); + Assert.assertEquals(actual.defaultTreatment(), expected.defaultTreatment()); + Assert.assertEquals(actual.killed(), expected.killed()); + Assert.assertEquals(actual.impressionsDisabled(), expected.impressionsDisabled()); + Assert.assertEquals(null, actual.flagSets()); + Assert.assertEquals(actual.algo(), expected.algo()); + Assert.assertEquals(actual.seed(), expected.seed()); + Assert.assertEquals(actual.trafficAllocation(), expected.trafficAllocation()); + Assert.assertEquals(actual.trafficAllocationSeed(), expected.trafficAllocationSeed()); + Assert.assertEquals(actual.getSegmentsNames(), expected.getSegmentsNames()); + Assert.assertEquals(actual.getRuleBasedSegmentsNames(), expected.getRuleBasedSegmentsNames()); + Assert.assertEquals(actual.prerequisitesMatcher().toString(), expected.prerequisitesMatcher().toString()); + + Assert.assertEquals(actual.configurations().get("on"), configurations.get("on")); } @Test - public void works_for_two_conditions() { - SDKReadinessGates gates = new SDKReadinessGates(); + public void worksForTwoConditions() { SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>()); - segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>()); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); @@ -159,62 +184,58 @@ public void works_for_two_conditions() { List conditions = Lists.newArrayList(c1, c2); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); - ParsedCondition parsedCondition1 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(segmentCache, EMPLOYEES)), fullyRollout); - ParsedCondition parsedCondition2 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(segmentCache, EMPLOYEES)), turnOff); + ParsedCondition parsedCondition1 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(EMPLOYEES)), fullyRollout); + ParsedCondition parsedCondition2 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(SALES_PEOPLE)), turnOff); List listOfParsedConditions = Lists.newArrayList(parsedCondition1, parsedCondition2); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfParsedConditions, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfParsedConditions, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test - public void fails_for_long_conditions() { - SDKReadinessGates gates = new SDKReadinessGates(); + public void successForLongConditions() { SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>()); - segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>()); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher(EMPLOYEES, false); List conditions = Lists.newArrayList(); List p1 = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); - for (int i = 0 ; i < SplitParser.CONDITIONS_UPPER_LIMIT+1 ; i++) { + for (int i = 0 ; i < CONDITIONS_UPPER_LIMIT+1 ; i++) { Condition c = ConditionsTestUtil.and(employeesMatcher, p1); conditions.add(c); } - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); - assertThat(parser.parse(split), is(nullValue())); + Assert.assertNotNull(parser.parse(split)); } @Test - public void works_with_attributes() { - SDKReadinessGates gates = new SDKReadinessGates(); + public void worksWithAttributes() { SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>()); - segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>()); + segmentCache.updateSegment(EMPLOYEES, Stream.of("adil", "pato", "trevor").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCache.updateSegment(SALES_PEOPLE, Stream.of("kunal").collect(Collectors.toList()), new ArrayList<>(), 1L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher employeesMatcher = ConditionsTestUtil.userDefinedSegmentMatcher("user", "name", EMPLOYEES, false); @@ -230,37 +251,30 @@ public void works_with_attributes() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); - AttributeMatcher employeesMatcherLogic = new AttributeMatcher("name", new UserDefinedSegmentMatcher(segmentCache, EMPLOYEES), false); + AttributeMatcher employeesMatcherLogic = new AttributeMatcher("name", new UserDefinedSegmentMatcher(EMPLOYEES), false); AttributeMatcher creationDateNotOlderThanAPointLogic = new AttributeMatcher("creation_date", new GreaterThanOrEqualToMatcher(1457386741L, DataType.DATETIME), true); CombiningMatcher combiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(employeesMatcherLogic, creationDateNotOlderThanAPointLogic)); ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test - public void less_than_or_equal_to() { - - -// SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(fetcherMap); -// SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + public void lessThanOrEqualTo() { SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher ageLessThan10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.LESS_THAN_OR_EQUAL_TO, DataType.NUMBER, 10L, false); @@ -271,7 +285,7 @@ public void less_than_or_equal_to() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); @@ -280,27 +294,20 @@ public void less_than_or_equal_to() { ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test - public void equal_to() { - -// SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(fetcherMap); -// SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + public void equalTo() { SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher ageLessThan10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.EQUAL_TO, DataType.NUMBER, 10L, true); @@ -310,7 +317,7 @@ public void equal_to() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); @@ -319,27 +326,19 @@ public void equal_to() { ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test - public void equal_to_negative_number() { - -// SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(fetcherMap); -// SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + public void equalToNegativeNumber() { SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); - - - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher equalToNegative10 = ConditionsTestUtil.numericMatcher("user", "age", MatcherType.EQUAL_TO, DataType.NUMBER, -10L, false); @@ -349,7 +348,7 @@ public void equal_to_negative_number() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); @@ -358,27 +357,19 @@ public void equal_to_negative_number() { ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test public void between() { - -// SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(fetcherMap); -// SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); Matcher ageBetween10And11 = ConditionsTestUtil.betweenMatcher("user", "age", @@ -393,7 +384,7 @@ public void between() { List conditions = Lists.newArrayList(c); - Split split = makeSplit("first.name", 123, conditions, 1); + Split split = makeSplit("first-name", 123, conditions, 1); ParsedSplit actual = parser.parse(split); @@ -402,14 +393,13 @@ public void between() { ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first.name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("first-name", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); } @Test - public void contains_any_of_set() { - + public void containsAnyOfSet() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -423,12 +413,11 @@ public void contains_any_of_set() { ContainsAnyOfSetMatcher m = new ContainsAnyOfSetMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void contains_all_of_set() { - + public void containsAllOfSet() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -442,12 +431,11 @@ public void contains_all_of_set() { ContainsAllOfSetMatcher m = new ContainsAllOfSetMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void equal_to_set() { - + public void equalToSet() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -461,12 +449,11 @@ public void equal_to_set() { EqualToSetMatcher m = new EqualToSetMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void is_part_of_set() { - + public void isPartOfSet() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -480,12 +467,11 @@ public void is_part_of_set() { PartOfSetMatcher m = new PartOfSetMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void starts_with_string() { - + public void startsWithString() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -499,12 +485,11 @@ public void starts_with_string() { StartsWithAnyOfMatcher m = new StartsWithAnyOfMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void ends_with_string() { - + public void endsWithString() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -518,13 +503,12 @@ public void ends_with_string() { EndsWithAnyOfMatcher m = new EndsWithAnyOfMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } @Test - public void contains_string() { - + public void containsString() { ArrayList set = Lists.newArrayList("sms", "voice"); List partitions = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); @@ -538,23 +522,176 @@ public void contains_string() { ContainsAnyOfMatcher m = new ContainsAnyOfMatcher(set); - set_matcher_test(c, m); + setMatcherTest(c, m); } - public void set_matcher_test(Condition c, io.split.engine.matchers.Matcher m) { + @Test + public void UnsupportedMatcher() { + SplitParser parser = new SplitParser(); + String splitWithUndefinedMatcher = "{\"ff\":{\"s\":-1,\"t\": 1457726098069,\"d\": [{ \"changeNumber\": 123, \"trafficTypeName\": \"user\", \"name\": \"some_name\"," + + "\"trafficAllocation\": 100, \"trafficAllocationSeed\": 123456, \"seed\": 321654, \"status\": \"ACTIVE\"," + + "\"killed\": false, \"defaultTreatment\": \"off\", \"algo\": 2,\"conditions\": [{ \"partitions\": [" + + "{\"treatment\": \"on\", \"size\": 50}, {\"treatment\": \"off\", \"size\": 50}], \"contitionType\": \"ROLLOUT\"," + + "\"label\": \"some_label\", \"matcherGroup\": { \"matchers\": [{ \"matcherType\": \"UNKNOWN\", \"negate\": false}]," + + "\"combiner\": \"AND\"}}], \"sets\": [\"set1\"]}]}, \"rbs\":{\"s\":-1,\"t\":-1,\"d\":[]}}"; + SplitChange change = Json.fromJson(splitWithUndefinedMatcher, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label() == Labels.UNSUPPORTED_MATCHER); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" in segment all")); + } + } + } + } + + @Test + public void EqualToSemverMatcher() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("semver_equalto")) { + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label().equals("equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" == semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void GreaterThanOrEqualSemverMatcher() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("semver_greater_or_equalto")) { + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label().equals("greater than or equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" >= semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void LessThanOrEqualSemverMatcher() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("semver_less_or_equalto")) { + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label().equals("less than or equal to semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" <= semver 1\\.22\\.9")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void BetweenSemverMatcher() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("semver_between")) { + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label().equals("between semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().equals(" between semver 1\\.22\\.9 and 2\\.1\\.0")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void InListSemverMatcher() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/semver/semver-splits.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + for (Split split : change.featureFlags.d) { + // should not cause exception + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("semver_inlist")) { + for (ParsedCondition parsedCondition : parsedSplit.parsedConditions()) { + assertTrue(parsedCondition.label().equals("in list semver")); + for (AttributeMatcher matcher : parsedCondition.matcher().attributeMatchers()) { + // Check the matcher is ALL_KEYS + assertTrue(matcher.matcher().toString().startsWith(" in semver list")); + return; + } + } + } + } + assertTrue(false); + } + + @Test + public void ImpressionToggleParseTest() throws IOException { + SplitParser parser = new SplitParser(); + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8); + SplitChange change = Json.fromJson(load, SplitChange.class); + boolean check1 = false, check2 = false, check3 = false; + for (Split split : change.featureFlags.d) { + ParsedSplit parsedSplit = parser.parse(split); + if (split.name.equals("without_impression_toggle")) { + assertFalse(parsedSplit.impressionsDisabled()); + check1 = true; + } + if (split.name.equals("impression_toggle_on")) { + assertFalse(parsedSplit.impressionsDisabled()); + check2 = true; + } + if (split.name.equals("impression_toggle_off")) { + assertTrue(parsedSplit.impressionsDisabled()); + check3 = true; + } + } + assertTrue(check1); + assertTrue(check2); + assertTrue(check3); + } + + public void setMatcherTest(Condition c, io.split.engine.matchers.Matcher m) { -// SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(fetcherMap); -// SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChangeEmployee = getSegmentChange(-1L, -1L, EMPLOYEES); SegmentChange segmentChangeSalesPeople = getSegmentChange(-1L, -1L, SALES_PEOPLE); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - - SegmentSynchronizationTask segmentFetcher = new SegmentSynchronizationTaskImp(segmentChangeFetcher,1L, 1, gates, segmentCache); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChangeEmployee).thenReturn(segmentChangeSalesPeople); - SplitParser parser = new SplitParser(segmentFetcher, segmentCache); + SplitParser parser = new SplitParser(); ArrayList set = Lists.newArrayList("sms", "voice"); @@ -572,9 +709,27 @@ public void set_matcher_test(Condition c, io.split.engine.matchers.Matcher m) { ParsedCondition parsedCondition = ParsedCondition.createParsedConditionForTests(combiningMatcher, partitions); List listOfMatcherAndSplits = Lists.newArrayList(parsedCondition); - ParsedSplit expected = ParsedSplit.createParsedSplitForTests("splitName", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1); + ParsedSplit expected = ParsedSplit.createParsedSplitForTests("splitName", 123, false, Treatments.OFF, listOfMatcherAndSplits, "user", 1, 1, null, false, new PrerequisitesMatcher(null)); - assertThat(actual, is(equalTo(expected))); + compareParsed(actual, expected); + } + + private void compareParsed(ParsedSplit actual, ParsedSplit expected) { + Assert.assertEquals(actual.getRuleBasedSegmentsNames(), expected.getRuleBasedSegmentsNames()); + Assert.assertEquals(actual.seed(), expected.seed()); + Assert.assertEquals(actual.algo(), expected.algo()); + Assert.assertEquals(actual.trafficAllocationSeed(), expected.trafficAllocationSeed()); + Assert.assertEquals(actual.flagSets(), expected.flagSets()); + Assert.assertEquals(actual.parsedConditions(), expected.parsedConditions()); + Assert.assertEquals(actual.trafficAllocation(), expected.trafficAllocation()); + Assert.assertEquals(actual.getSegmentsNames(), expected.getSegmentsNames()); + Assert.assertEquals(actual.impressionsDisabled(), expected.impressionsDisabled()); + Assert.assertEquals(actual.killed(), expected.killed()); + Assert.assertEquals(actual.defaultTreatment(), expected.defaultTreatment()); + Assert.assertEquals(actual.changeNumber(), expected.changeNumber()); + Assert.assertEquals(actual.feature(), expected.feature()); + Assert.assertEquals(actual.configurations(), expected.configurations()); + Assert.assertEquals(actual.prerequisitesMatcher().toString(), expected.prerequisitesMatcher().toString()); } private Split makeSplit(String name, int seed, List conditions, long changeNumber) { @@ -594,6 +749,7 @@ private Split makeSplit(String name, int seed, List conditions, long split.changeNumber = changeNumber; split.algo = 1; split.configurations = configurations; + split.prerequisites = new ArrayList<>(); return split; } @@ -606,5 +762,4 @@ private SegmentChange getSegmentChange(long since, long till, String segmentName segmentChange.removed = new ArrayList<>(); return segmentChange; } - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java b/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java new file mode 100644 index 00000000..ce04294c --- /dev/null +++ b/client/src/test/java/io/split/engine/experiments/SplitSynchronizationTaskTest.java @@ -0,0 +1,55 @@ +package io.split.engine.experiments; + +import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.engine.common.FetchOptions; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SplitCacheProducer; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.telemetry.storage.NoopTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.HashSet; + +public class SplitSynchronizationTaskTest { + + private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); + + @Test + public void testLocalhost() throws InterruptedException { + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(FLAG_SETS_FILTER); + + SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class); + SplitParser splitParser = new SplitParser(); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + RuleBasedSegmentCacheProducer ruleBasedSegmentCacheProducer = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, FLAG_SETS_FILTER, + ruleBasedSegmentParser, ruleBasedSegmentCacheProducer); + + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000, null); + + splitSynchronizationTask.start(); + + Thread.sleep(2000); + + Mockito.verify(splitChangeFetcher, Mockito.times(1)).fetch(-1, -1, fetchOptions); + } + + @Test + public void testStartAndStop() throws InterruptedException { + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(FLAG_SETS_FILTER); + SplitFetcherImp splitFetcherImp = Mockito.mock(SplitFetcherImp.class); + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcherImp, splitCacheProducer, 1000, null); + splitSynchronizationTask.start(); + + Thread.sleep(2000); + + Mockito.verify(splitFetcherImp, Mockito.times(1)).run(); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/AttributeMatcherTest.java b/client/src/test/java/io/split/engine/matchers/AttributeMatcherTest.java index 7c723adb..9f535790 100644 --- a/client/src/test/java/io/split/engine/matchers/AttributeMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/AttributeMatcherTest.java @@ -5,6 +5,7 @@ import com.google.common.collect.Maps; import io.split.client.dtos.DataType; import io.split.engine.matchers.strings.WhitelistMatcher; +import org.junit.Assert; import org.junit.Test; import java.util.Calendar; @@ -12,9 +13,6 @@ import java.util.Date; import java.util.Map; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for AllKeysMatcher */ @@ -23,60 +21,62 @@ public class AttributeMatcherTest { @Test public void works() { AttributeMatcher matcher = new AttributeMatcher("creation_date", new GreaterThanOrEqualToMatcher(100L, DataType.NUMBER), false); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null), is(false)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null)); } @Test - public void works_negation() { + public void worksNegation() { AttributeMatcher matcher = new AttributeMatcher("creation_date", new GreaterThanOrEqualToMatcher(100L, DataType.NUMBER), true); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null), is(true)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null)); } @Test - public void works_less_than_or_equal_to() { + public void worksLessThanOrEqualTo() { AttributeMatcher matcher = new AttributeMatcher("creation_date", new LessThanOrEqualToMatcher(100L, DataType.NUMBER), false); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null), is(false)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 99L), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 100L), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 101.3), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", Calendar.getInstance()), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", new Date()), null)); } @Test - public void works_boolean() { + public void worksBoolean() { AttributeMatcher matcher = new AttributeMatcher("value", new BooleanMatcher(true), false); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", true), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "true"), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "True"), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "TrUe"), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "TRUE"), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", false), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "false"), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "False"), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "FALSE"), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", "faLSE"), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", ""), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", 0), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("value", 1), null), is(false)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("value", true), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("value", "true"), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("value", "True"), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("value", "TrUe"), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("value", "TRUE"), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", false), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", "false"), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", "False"), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", "FALSE"), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", "faLSE"), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", ""), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", 0), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("value", 1), null)); } @Test - public void error_conditions() { + public void errorConditions() { AttributeMatcher matcher = new AttributeMatcher("creation_date", new GreaterThanOrEqualToMatcher(100L, DataType.NUMBER), false); - assertThat(matcher.match("ignore", null, null, null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("foo", 101), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", "101"), null), is(false)); + Assert.assertFalse(matcher.match("ignore", null, null, null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("foo", 101), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", "101"), null)); } @Test @@ -85,38 +85,38 @@ public void dates() { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, -1); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", c.getTimeInMillis()), null), is(false)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", c.getTimeInMillis()), null)); c.add(Calendar.YEAR, 2); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", c.getTimeInMillis()), null), is(true)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", c.getTimeInMillis()), null)); } @Test public void between() { AttributeMatcher matcher = new AttributeMatcher("creation_date", new BetweenMatcher(10, 12, DataType.NUMBER), false); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 9), null), is(false)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 10), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 11), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 12), null), is(true)); - assertThat(matcher.match("ignore", null, ImmutableMap.of("creation_date", 13), null), is(false)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 9), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 10), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 11), null)); + Assert.assertTrue(matcher.match("ignore", null, ImmutableMap.of("creation_date", 12), null)); + Assert.assertFalse(matcher.match("ignore", null, ImmutableMap.of("creation_date", 13), null)); } @Test - public void when_no_attribute_we_use_the_key() { + public void whenNoAttributeWeUseTheKey() { AttributeMatcher matcher = new AttributeMatcher(null, new WhitelistMatcher(Lists.newArrayList("trial")), false); Map nullMap = Maps.newHashMap(); nullMap.put("planType", null); - assertThat(matcher.match("trial", null, ImmutableMap.of("planType", "trial"), null), is(true)); - assertThat(matcher.match("trial", null, ImmutableMap.of("planType", "Trial"), null), is(true)); - assertThat(matcher.match("trial", null, nullMap, null), is(true)); - assertThat(matcher.match("trial", null, ImmutableMap.of("planType", "premium"), null), is(true)); - assertThat(matcher.match("trial", null, ImmutableMap.of("planType", 10), null), is(true)); - assertThat(matcher.match("trial", null, Collections.emptyMap(), null), is(true)); - assertThat(matcher.match("trial", null, null, null), is(true)); - assertThat(matcher.match("premium", null, null, null), is(false)); + Assert.assertTrue(matcher.match("trial", null, ImmutableMap.of("planType", "trial"), null)); + Assert.assertTrue(matcher.match("trial", null, ImmutableMap.of("planType", "Trial"), null)); + Assert.assertTrue(matcher.match("trial", null, nullMap, null)); + Assert.assertTrue(matcher.match("trial", null, ImmutableMap.of("planType", "premium"), null)); + Assert.assertTrue(matcher.match("trial", null, ImmutableMap.of("planType", 10), null)); + Assert.assertTrue(matcher.match("trial", null, Collections.emptyMap(), null)); + Assert.assertTrue(matcher.match("trial", null, null, null)); + Assert.assertFalse(matcher.match("premium", null, null, null)); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/BetweenMatcherTest.java b/client/src/test/java/io/split/engine/matchers/BetweenMatcherTest.java index 41097769..22bc3b44 100644 --- a/client/src/test/java/io/split/engine/matchers/BetweenMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/BetweenMatcherTest.java @@ -1,11 +1,9 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; +import org.junit.Assert; import org.junit.Test; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for BetweenMatcherTest */ @@ -18,18 +16,18 @@ public void works() { BetweenMatcher matcher = new BetweenMatcher(start, end, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); for (int i = start; i <= end; i++) { - assertThat(matcher.match(i, null, null, null), is(true)); + Assert.assertTrue(matcher.match(i, null, null, null)); } - assertThat(matcher.match(new Long(start - 1), null, null, null), is(false)); - assertThat(matcher.match(end + 1, null, null, null), is(false)); + Assert.assertFalse(matcher.match(new Long(start - 1), null, null, null)); + Assert.assertFalse(matcher.match(end + 1, null, null, null)); } @Test - public void works_dates() { + public void worksDates() { long april11_2016_23_59 = 1460419199000L; long april12_2016_midnight_19 = 1460420360000L; long april12_2016_midnight_20 = 1460420421903L; @@ -39,14 +37,10 @@ public void works_dates() { BetweenMatcher matcher = new BetweenMatcher(april12_2016_midnight_19, april12_2016_midnight_20_59, DataType.DATETIME); - assertThat(matcher.match(april11_2016_23_59, null, null, null), is(false)); - assertThat(matcher.match(april12_2016_midnight_19, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20_59, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_1_20, null, null, null), is(false)); - - + Assert.assertFalse(matcher.match(april11_2016_23_59, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_19, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20_59, null, null, null)); + Assert.assertFalse(matcher.match(april12_2016_1_20, null, null, null)); } - - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/BetweenSemverMatcherTest.java b/client/src/test/java/io/split/engine/matchers/BetweenSemverMatcherTest.java new file mode 100644 index 00000000..41a4f76d --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/BetweenSemverMatcherTest.java @@ -0,0 +1,36 @@ +package io.split.engine.matchers; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +/** + * Tests for EqualToSemverMatcher + */ + +public class BetweenSemverMatcherTest { + + @Test + public void works() { + BetweenSemverMatcher betweenSemverMatcher = new BetweenSemverMatcher("2.1.8", "3.0.0"); + + assertTrue( betweenSemverMatcher.match("2.1.8", null, null, null)); + assertTrue( betweenSemverMatcher.match("2.1.9", null, null, null)); + assertFalse( betweenSemverMatcher.match("2.1.8-rc", null, null, null)); + assertTrue( betweenSemverMatcher.match("3.0.0+build", null, null, null)); + assertFalse( betweenSemverMatcher.match("4.5.8", null, null, null)); + assertFalse( betweenSemverMatcher.match("1.0.4", null, null, null)); + assertTrue(betweenSemverMatcher.equals(betweenSemverMatcher)); + assertTrue(betweenSemverMatcher.hashCode() != 0); + } + + @Test + public void testNull() { + BetweenSemverMatcher betweenSemverMatcher = new BetweenSemverMatcher("2.1.8", "3.0.0"); + assertFalse( betweenSemverMatcher.match(null, null, null, null)); + + betweenSemverMatcher = new BetweenSemverMatcher("2.www.8", "3.xx.0"); + assertFalse(betweenSemverMatcher.match("2.www.8", null, null, null)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/BooleanMatcherTest.java b/client/src/test/java/io/split/engine/matchers/BooleanMatcherTest.java index cb466bc0..14889af6 100644 --- a/client/src/test/java/io/split/engine/matchers/BooleanMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/BooleanMatcherTest.java @@ -8,7 +8,7 @@ public class BooleanMatcherTest { @Test - public void works_true() { + public void worksTrue() { BooleanMatcher matcher = new BooleanMatcher(true); assertThat(matcher.match(null, null, null, null), is(false)); assertThat(matcher.match(true, null, null, null), is(true)); @@ -23,7 +23,7 @@ public void works_true() { } @Test - public void works_false() { + public void worksFalse() { BooleanMatcher matcher = new BooleanMatcher(false); assertThat(matcher.match(null, null, null, null), is(false)); assertThat(matcher.match(true, null, null, null), is(false)); diff --git a/client/src/test/java/io/split/engine/matchers/CombiningMatcherTest.java b/client/src/test/java/io/split/engine/matchers/CombiningMatcherTest.java index ee58a18a..3946aed5 100644 --- a/client/src/test/java/io/split/engine/matchers/CombiningMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/CombiningMatcherTest.java @@ -3,13 +3,11 @@ import com.google.common.collect.Lists; import io.split.client.dtos.MatcherCombiner; import io.split.engine.matchers.strings.WhitelistMatcher; +import org.junit.Assert; import org.junit.Test; import java.util.Collections; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests CombiningMatcher * @@ -18,15 +16,14 @@ public class CombiningMatcherTest { @Test - public void works_and() { + public void worksAnd() { AttributeMatcher matcher1 = AttributeMatcher.vanilla(new AllKeysMatcher()); AttributeMatcher matcher2 = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("a", "b"))); CombiningMatcher combiner = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(matcher1, matcher2)); - assertThat(combiner.match("a", null, null, null), is(true)); - assertThat(combiner.match("b", null, Collections.emptyMap(), null), is(true)); - assertThat(combiner.match("c", null, null, null), is(false)); + Assert.assertTrue(combiner.match("a", null, null, null)); + Assert.assertTrue(combiner.match("b", null, Collections.emptyMap(), null)); + Assert.assertFalse(combiner.match("c", null, null, null)); } - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/EqualToMatcherTest.java b/client/src/test/java/io/split/engine/matchers/EqualToMatcherTest.java index aeb887f9..f8320e51 100644 --- a/client/src/test/java/io/split/engine/matchers/EqualToMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/EqualToMatcherTest.java @@ -1,11 +1,9 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; +import org.junit.Assert; import org.junit.Test; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for AllKeysMatcher */ @@ -14,30 +12,30 @@ public class EqualToMatcherTest { @Test public void works() { EqualToMatcher matcher = new EqualToMatcher(10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(false)); - assertThat(matcher.match(new Long(-1), null, null, null), is(false)); - assertThat(matcher.match(9, null, null, null), is(false)); - assertThat(matcher.match(new Long(10), null, null, null), is(true)); - assertThat(matcher.match(11, null, null, null), is(false)); - assertThat(matcher.match(100, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertFalse(matcher.match(1, null, null, null)); + Assert.assertFalse(matcher.match(new Long(-1), null, null, null)); + Assert.assertFalse(matcher.match(9, null, null, null)); + Assert.assertTrue(matcher.match(new Long(10), null, null, null)); + Assert.assertFalse(matcher.match(11, null, null, null)); + Assert.assertFalse(matcher.match(100, null, null, null)); } @Test - public void works_negative() { + public void worksNegative() { EqualToMatcher matcher = new EqualToMatcher(-10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(false)); - assertThat(matcher.match(new Long(-1), null, null, null), is(false)); - assertThat(matcher.match(9, null, null, null), is(false)); - assertThat(matcher.match(new Long(10), null, null, null), is(false)); - assertThat(matcher.match(11, null, null, null), is(false)); - assertThat(matcher.match(-10, null, null, null), is(true)); - assertThat(matcher.match(-11, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertFalse(matcher.match(1, null, null, null)); + Assert.assertFalse(matcher.match(new Long(-1), null, null, null)); + Assert.assertFalse(matcher.match(9, null, null, null)); + Assert.assertFalse(matcher.match(new Long(10), null, null, null)); + Assert.assertFalse(matcher.match(11, null, null, null)); + Assert.assertTrue(matcher.match(-10, null, null, null)); + Assert.assertFalse(matcher.match(-11, null, null, null)); } @Test - public void works_dates() { + public void worksDates() { long april11_2016_23_59_59 = 1460419199000L; long april12_2016_midnight_19 = 1460420360000L; long april12_2016_midnight_20 = 1460420421903L; @@ -45,12 +43,10 @@ public void works_dates() { long april13_2016_00_00_00 = 1460505600000L; EqualToMatcher matcher = new EqualToMatcher(april12_2016_midnight_20, DataType.DATETIME); - assertThat(matcher.match(april11_2016_23_59_59, null, null, null), is(false)); - assertThat(matcher.match(april12_2016_midnight_19, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_1_20, null, null, null), is(true)); - assertThat(matcher.match(april13_2016_00_00_00, null, null, null), is(false)); - + Assert.assertFalse(matcher.match(april11_2016_23_59_59, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_19, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_1_20, null, null, null)); + Assert.assertFalse(matcher.match(april13_2016_00_00_00, null, null, null)); } - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/EqualToSemverMatcherTest.java b/client/src/test/java/io/split/engine/matchers/EqualToSemverMatcherTest.java new file mode 100644 index 00000000..a5a41e2b --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/EqualToSemverMatcherTest.java @@ -0,0 +1,34 @@ +package io.split.engine.matchers; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +/** + * Tests for EqualToSemverMatcher + */ + +public class EqualToSemverMatcherTest { + + @Test + public void works() { + EqualToSemverMatcher equalToSemverMatcher = new EqualToSemverMatcher("2.1.8"); + + assertTrue( equalToSemverMatcher.match("2.1.8", null, null, null)); + assertFalse(equalToSemverMatcher.match("2.1.9", null, null, null)); + assertFalse(equalToSemverMatcher.match("2.1.8-rc", null, null, null)); + assertFalse( equalToSemverMatcher.match("2.1.8+build", null, null, null)); + assertTrue(equalToSemverMatcher.equals(equalToSemverMatcher)); + assertTrue(equalToSemverMatcher.hashCode() != 0); + } + + @Test + public void testNull() { + EqualToSemverMatcher equalToSemverMatcher = new EqualToSemverMatcher("2.1.8"); + assertFalse( equalToSemverMatcher.match(null, null, null, null)); + + equalToSemverMatcher = new EqualToSemverMatcher("2.ee.8"); + assertFalse(equalToSemverMatcher.match("2.ee.8", null, null, null)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToMatcherTest.java b/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToMatcherTest.java index 566f5362..bbe37fdd 100644 --- a/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToMatcherTest.java @@ -1,11 +1,9 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; +import org.junit.Assert; import org.junit.Test; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for AllKeysMatcher */ @@ -14,31 +12,31 @@ public class GreaterThanOrEqualToMatcherTest { @Test public void works() { GreaterThanOrEqualToMatcher matcher = new GreaterThanOrEqualToMatcher(10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(false)); - assertThat(matcher.match(new Long(-1), null, null, null), is(false)); - assertThat(matcher.match(9, null, null, null), is(false)); - assertThat(matcher.match(new Long(10), null, null, null), is(true)); - assertThat(matcher.match(11, null, null, null), is(true)); - assertThat(matcher.match(100, null, null, null), is(true)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertFalse(matcher.match(1, null, null, null)); + Assert.assertFalse(matcher.match(new Long(-1), null, null, null)); + Assert.assertFalse(matcher.match(9, null, null, null)); + Assert.assertTrue(matcher.match(new Long(10), null, null, null)); + Assert.assertTrue(matcher.match(11, null, null, null)); + Assert.assertTrue(matcher.match(100, null, null, null)); } @Test - public void works_negative() { + public void worksNegative() { GreaterThanOrEqualToMatcher matcher = new GreaterThanOrEqualToMatcher(-10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(true)); - assertThat(matcher.match(new Long(-1), null, null, null), is(true)); - assertThat(matcher.match(9, null, null, null), is(true)); - assertThat(matcher.match(new Long(10), null, null, null), is(true)); - assertThat(matcher.match(11, null, null, null), is(true)); - assertThat(matcher.match(100, null, null, null), is(true)); - assertThat(matcher.match(-10, null, null, null), is(true)); - assertThat(matcher.match(-11, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertTrue(matcher.match(1, null, null, null)); + Assert.assertTrue(matcher.match(new Long(-1), null, null, null)); + Assert.assertTrue(matcher.match(9, null, null, null)); + Assert.assertTrue(matcher.match(new Long(10), null, null, null)); + Assert.assertTrue(matcher.match(11, null, null, null)); + Assert.assertTrue(matcher.match(100, null, null, null)); + Assert.assertTrue(matcher.match(-10, null, null, null)); + Assert.assertFalse(matcher.match(-11, null, null, null)); } @Test - public void works_dates() { + public void worksDates() { long april12_2016_midnight_19 = 1460420360000L; long april12_2016_midnight_20 = 1460420421903L; long april12_2016_midnight_20_59 = 1460420459000L; @@ -46,11 +44,11 @@ public void works_dates() { long april12_2016_18_20 = 1460485239000L; GreaterThanOrEqualToMatcher matcher = new GreaterThanOrEqualToMatcher(april12_2016_midnight_20, DataType.DATETIME); - assertThat(matcher.match(april12_2016_midnight_19, null, null, null), is(false)); - assertThat(matcher.match(april12_2016_midnight_20_59, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_1_20, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_18_20, null, null, null), is(true)); + Assert.assertFalse(matcher.match(april12_2016_midnight_19, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20_59, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20_59, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_1_20, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_18_20, null, null, null)); } - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcherTest.java b/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcherTest.java new file mode 100644 index 00000000..753034c7 --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/GreaterThanOrEqualToSemverMatcherTest.java @@ -0,0 +1,34 @@ +package io.split.engine.matchers; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +/** + * Tests for EqualToSemverMatcher + */ + +public class GreaterThanOrEqualToSemverMatcherTest { + + @Test + public void works() { + GreaterThanOrEqualToSemverMatcher greaterThanOrEqualToSemverMatcher = new GreaterThanOrEqualToSemverMatcher("2.1.8"); + assertTrue( greaterThanOrEqualToSemverMatcher.match("2.1.8", null, null, null)); + assertTrue( greaterThanOrEqualToSemverMatcher.match("2.1.9", null, null, null)); + assertFalse( greaterThanOrEqualToSemverMatcher.match("2.1.8-rc", null, null, null)); + assertFalse( greaterThanOrEqualToSemverMatcher.match("2.0.10", null, null, null)); + assertTrue( greaterThanOrEqualToSemverMatcher.match("2.1.8+build", null, null, null)); + assertTrue(greaterThanOrEqualToSemverMatcher.equals(greaterThanOrEqualToSemverMatcher)); + assertTrue(greaterThanOrEqualToSemverMatcher.hashCode() != 0); + } + + @Test + public void testNull() { + GreaterThanOrEqualToSemverMatcher greaterThanOrEqualToSemverMatcher = new GreaterThanOrEqualToSemverMatcher("2.1.8"); + assertFalse( greaterThanOrEqualToSemverMatcher.match(null, null, null, null)); + + greaterThanOrEqualToSemverMatcher = new GreaterThanOrEqualToSemverMatcher("2.ee.8"); + assertFalse(greaterThanOrEqualToSemverMatcher.match("2.ee.8", null, null, null)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/InListSemverMatcherTest.java b/client/src/test/java/io/split/engine/matchers/InListSemverMatcherTest.java new file mode 100644 index 00000000..c0137125 --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/InListSemverMatcherTest.java @@ -0,0 +1,41 @@ +package io.split.engine.matchers; + +import com.google.common.collect.Lists; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Tests for EqualToSemverMatcher + */ + +public class InListSemverMatcherTest { + + @Test + public void works() { + List whitelist = Lists.newArrayList("2.1.8", "3.4.0"); + InListSemverMatcher inListSemverMatcher = new InListSemverMatcher(whitelist); + + assertTrue( inListSemverMatcher.match("2.1.8", null, null, null) == true); + assertTrue( inListSemverMatcher.match("2.1.9", null, null, null) == false); + assertTrue( inListSemverMatcher.match("2.1.8-rc", null, null, null) == false); + assertTrue( inListSemverMatcher.match("3.4.0", null, null, null) == true); + assertTrue( inListSemverMatcher.match("3.4.0+build", null, null, null) == false); + assertTrue(inListSemverMatcher.equals(inListSemverMatcher)); + assertTrue(inListSemverMatcher.hashCode() != 0); + } + + @Test + public void testNull() { + List whitelist = Lists.newArrayList("2.1.8", "3.4.0"); + InListSemverMatcher inListSemverMatcher = new InListSemverMatcher(whitelist); + assertFalse( inListSemverMatcher.match(null, null, null, null)); + + whitelist = Lists.newArrayList("2.1.eee", "3.xxx.0"); + inListSemverMatcher = new InListSemverMatcher(whitelist); + assertFalse(inListSemverMatcher.match("2.1.eee", null, null, null)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToMatcherTest.java b/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToMatcherTest.java index 1663a608..47853ed4 100644 --- a/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToMatcherTest.java @@ -1,11 +1,9 @@ package io.split.engine.matchers; import io.split.client.dtos.DataType; +import org.junit.Assert; import org.junit.Test; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for AllKeysMatcher */ @@ -14,31 +12,31 @@ public class LessThanOrEqualToMatcherTest { @Test public void works() { LessThanOrEqualToMatcher matcher = new LessThanOrEqualToMatcher(10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(true)); - assertThat(matcher.match(new Long(-1), null, null, null), is(true)); - assertThat(matcher.match(9, null, null, null), is(true)); - assertThat(matcher.match(new Long(10), null, null, null), is(true)); - assertThat(matcher.match(11, null, null, null), is(false)); - assertThat(matcher.match(100, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertTrue(matcher.match(1, null, null, null)); + Assert.assertTrue(matcher.match(new Long(-1), null, null, null)); + Assert.assertTrue(matcher.match(9, null, null, null)); + Assert.assertTrue(matcher.match(new Long(10), null, null, null)); + Assert.assertFalse(matcher.match(11, null, null, null)); + Assert.assertFalse(matcher.match(100, null, null, null)); } @Test - public void works_negative() { + public void worksNegative() { LessThanOrEqualToMatcher matcher = new LessThanOrEqualToMatcher(-10, DataType.NUMBER); - assertThat(matcher.match(null, null, null, null), is(false)); - assertThat(matcher.match(1, null, null, null), is(false)); - assertThat(matcher.match(new Long(-1), null, null, null), is(false)); - assertThat(matcher.match(9, null, null, null), is(false)); - assertThat(matcher.match(new Long(10), null, null, null), is(false)); - assertThat(matcher.match(11, null, null, null), is(false)); - assertThat(matcher.match(-9, null, null, null), is(false)); - assertThat(matcher.match(-10, null, null, null), is(true)); - assertThat(matcher.match(-11, null, null, null), is(true)); + Assert.assertFalse(matcher.match(null, null, null, null)); + Assert.assertFalse(matcher.match(1, null, null, null)); + Assert.assertFalse(matcher.match(new Long(-1), null, null, null)); + Assert.assertFalse(matcher.match(9, null, null, null)); + Assert.assertFalse(matcher.match(new Long(10), null, null, null)); + Assert.assertFalse(matcher.match(11, null, null, null)); + Assert.assertFalse(matcher.match(-9, null, null, null)); + Assert.assertTrue(matcher.match(-10, null, null, null)); + Assert.assertTrue(matcher.match(-11, null, null, null)); } @Test - public void works_dates() { + public void worksDates() { long april11_2016_23_59 = 1460419199000L; long april12_2016_midnight_19 = 1460420360000L; long april12_2016_midnight_20 = 1460420421903L; @@ -46,12 +44,10 @@ public void works_dates() { long april12_2016_1_20 = 1460424039000L; LessThanOrEqualToMatcher matcher = new LessThanOrEqualToMatcher(april12_2016_midnight_20, DataType.DATETIME); - assertThat(matcher.match(april11_2016_23_59, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_19, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_midnight_20_59, null, null, null), is(true)); - assertThat(matcher.match(april12_2016_1_20, null, null, null), is(false)); + Assert.assertTrue(matcher.match(april11_2016_23_59, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_19, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20, null, null, null)); + Assert.assertTrue(matcher.match(april12_2016_midnight_20_59, null, null, null)); + Assert.assertFalse(matcher.match(april12_2016_1_20, null, null, null)); } - - -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcherTest.java b/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcherTest.java new file mode 100644 index 00000000..349a608a --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/LessThanOrEqualToSemverMatcherTest.java @@ -0,0 +1,35 @@ +package io.split.engine.matchers; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +/** + * Tests for EqualToSemverMatcher + */ + +public class LessThanOrEqualToSemverMatcherTest { + + @Test + public void works() { + LessThanOrEqualToSemverMatcher lessThanOrEqualToSemverMatcher = new LessThanOrEqualToSemverMatcher("2.1.8"); + + assertTrue( lessThanOrEqualToSemverMatcher.match("2.1.8", null, null, null)); + assertFalse( lessThanOrEqualToSemverMatcher.match("2.1.9", null, null, null)); + assertTrue( lessThanOrEqualToSemverMatcher.match("2.1.8-rc", null, null, null)); + assertTrue( lessThanOrEqualToSemverMatcher.match("2.0.10", null, null, null)); + assertTrue( lessThanOrEqualToSemverMatcher.match("2.1.8+build", null, null, null)); + assertTrue(lessThanOrEqualToSemverMatcher.equals(lessThanOrEqualToSemverMatcher)); + assertTrue(lessThanOrEqualToSemverMatcher.hashCode() != 0); + } + + @Test + public void testNull() { + LessThanOrEqualToSemverMatcher lessThanOrEqualToSemverMatcher = new LessThanOrEqualToSemverMatcher("2.1.8"); + assertFalse( lessThanOrEqualToSemverMatcher.match(null, null, null, null)); + + lessThanOrEqualToSemverMatcher = new LessThanOrEqualToSemverMatcher("2.ee.8"); + assertFalse(lessThanOrEqualToSemverMatcher.match("2.ee.8", null, null, null)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/NegatableMatcherTest.java b/client/src/test/java/io/split/engine/matchers/NegatableMatcherTest.java index d3fe1b14..f80f3873 100644 --- a/client/src/test/java/io/split/engine/matchers/NegatableMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/NegatableMatcherTest.java @@ -1,18 +1,21 @@ package io.split.engine.matchers; import com.google.common.collect.Lists; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; +import io.split.engine.evaluator.EvaluationContext; +import io.split.engine.evaluator.Evaluator; import io.split.engine.matchers.strings.WhitelistMatcher; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCache; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; import java.util.ArrayList; import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Tests for NegatableMatcher. * @@ -21,39 +24,39 @@ public class NegatableMatcherTest { @Test - public void works_all_keys() { + public void worksAllKeys() { AllKeysMatcher delegate = new AllKeysMatcher(); AttributeMatcher.NegatableMatcher matcher = new AttributeMatcher.NegatableMatcher(delegate, true); - test(matcher, "foo", false); + test(matcher, "foo", false, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); } @Test - public void works_segment() { + public void worksSegment() { SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment("foo", Stream.of("a","b").collect(Collectors.toList()), new ArrayList<>()); - UserDefinedSegmentMatcher delegate = new UserDefinedSegmentMatcher(segmentCache, "foo"); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + segmentCache.updateSegment("foo", Stream.of("a","b").collect(Collectors.toList()), new ArrayList<>(), 1L); + UserDefinedSegmentMatcher delegate = new UserDefinedSegmentMatcher("foo"); AttributeMatcher.NegatableMatcher matcher = new AttributeMatcher.NegatableMatcher(delegate, true); - test(matcher, "a", false); - test(matcher, "b", false); - test(matcher, "c", true); + test(matcher, "a", false, segmentCache, ruleBasedSegmentCache); + test(matcher, "b", false, segmentCache, ruleBasedSegmentCache); + test(matcher, "c", true, segmentCache, ruleBasedSegmentCache); } @Test - public void works_whitelist() { + public void worksWhitelist() { WhitelistMatcher delegate = new WhitelistMatcher(Lists.newArrayList("a", "b")); AttributeMatcher.NegatableMatcher matcher = new AttributeMatcher.NegatableMatcher(delegate, true); - test(matcher, "a", false); - test(matcher, "b", false); - test(matcher, "c", true); + test(matcher, "a", false, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); + test(matcher, "b", false, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); + test(matcher, "c", true, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); } - private void test(AttributeMatcher.NegatableMatcher negationMatcher, String key, boolean expected) { - assertThat(negationMatcher.match(key, null, null, null), is(expected)); - assertThat(negationMatcher.delegate().match(key, null, null, null), is(!expected)); - + private void test(AttributeMatcher.NegatableMatcher negationMatcher, String key, boolean expected, SegmentCache segmentCache, RuleBasedSegmentCache ruleBasedSegmentCache) { + Assert.assertEquals(expected, negationMatcher.match(key, null, null, new EvaluationContext(Mockito.mock(Evaluator.class), segmentCache, ruleBasedSegmentCache))); + Assert.assertNotEquals(expected, negationMatcher.delegate().match(key, null, null, new EvaluationContext(Mockito.mock(Evaluator.class), segmentCache, ruleBasedSegmentCache))); } diff --git a/client/src/test/java/io/split/engine/matchers/PrerequisitesMatcherTest.java b/client/src/test/java/io/split/engine/matchers/PrerequisitesMatcherTest.java new file mode 100644 index 00000000..4fe92d04 --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/PrerequisitesMatcherTest.java @@ -0,0 +1,55 @@ +package io.split.engine.matchers; + +import io.split.client.dtos.Prerequisites; +import io.split.client.utils.Json; +import io.split.engine.evaluator.EvaluationContext; +import io.split.engine.evaluator.Evaluator; +import io.split.engine.evaluator.EvaluatorImp; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCache; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.Arrays; +import java.util.List; + +/** + * Tests for Prerequisites matcher + */ +public class PrerequisitesMatcherTest { + + @Test + public void works() { + Evaluator evaluator = Mockito.mock(Evaluator.class); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); + List prerequisites = Arrays.asList(Json.fromJson("{\"n\": \"split1\", \"ts\": [\"on\"]}", Prerequisites.class), Json.fromJson("{\"n\": \"split2\", \"ts\": [\"off\"]}", Prerequisites.class)); + PrerequisitesMatcher matcher = new PrerequisitesMatcher(prerequisites); + Assert.assertEquals("prerequisites: split1 [on], split2 [off]", matcher.toString()); + PrerequisitesMatcher matcher2 = new PrerequisitesMatcher(prerequisites); + Assert.assertTrue(matcher.equals(matcher2)); + Assert.assertTrue(matcher.hashCode() != 0); + + Mockito.when(evaluator.evaluateFeature("user", "user", "split1", null)).thenReturn(new EvaluatorImp.TreatmentLabelAndChangeNumber("on", "")); + Mockito.when(evaluator.evaluateFeature("user", "user", "split2", null)).thenReturn(new EvaluatorImp.TreatmentLabelAndChangeNumber("off", "")); + Assert.assertTrue(matcher.match("user", "user", null, evaluationContext)); + + Mockito.when(evaluator.evaluateFeature("user", "user", "split2", null)).thenReturn(new EvaluatorImp.TreatmentLabelAndChangeNumber("on", "")); + Assert.assertFalse(matcher.match("user", "user", null, evaluationContext)); + } + + @Test + public void invalidParams() { + Evaluator evaluator = Mockito.mock(Evaluator.class); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, Mockito.mock(SegmentCache.class), Mockito.mock(RuleBasedSegmentCache.class)); + + List prerequisites = Arrays.asList(Json.fromJson("{\"n\": \"split1\", \"ts\": [\"on\"]}", Prerequisites.class), Json.fromJson("{\"n\": \"split2\", \"ts\": [\"off\"]}", Prerequisites.class)); + PrerequisitesMatcher matcher = new PrerequisitesMatcher(prerequisites); + Mockito.when(evaluator.evaluateFeature("user", "user", "split1", null)).thenReturn(new EvaluatorImp.TreatmentLabelAndChangeNumber("on", "")); + Assert.assertFalse(matcher.match(null, null, null, evaluationContext)); + Assert.assertFalse(matcher.match(123, null, null, evaluationContext)); + + matcher = new PrerequisitesMatcher(null); + Assert.assertFalse(matcher.match(123, null, null, evaluationContext)); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java b/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java new file mode 100644 index 00000000..7d5d0c48 --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java @@ -0,0 +1,137 @@ +package io.split.engine.matchers; + +import com.google.common.collect.Lists; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.MatcherCombiner; +import io.split.client.dtos.SplitChange; +import io.split.client.utils.Json; +import io.split.client.utils.RuleBasedSegmentsToUpdate; +import io.split.engine.evaluator.EvaluationContext; +import io.split.engine.evaluator.Evaluator; +import io.split.engine.experiments.ParsedCondition; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.matchers.strings.WhitelistMatcher; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SegmentCache; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +public class RuleBasedSegmentMatcherTest { + @Test + public void works() { + Evaluator evaluator = Mockito.mock(Evaluator.class); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache); + AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin"))); + CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher)); + + AttributeMatcher ruleBasedSegmentMatcher = AttributeMatcher.vanilla(new RuleBasedSegmentMatcher("sample_rule_based_segment")); + CombiningMatcher ruleBasedSegmentCombinerMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(ruleBasedSegmentMatcher)); + ParsedCondition ruleBasedSegmentCondition = new ParsedCondition(ConditionType.ROLLOUT, ruleBasedSegmentCombinerMatcher, null, "test rbs rule"); + ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("sample_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, "whitelist label")),"user", + 123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList()); + ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment), null, 123); + + RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("sample_rule_based_segment"); + + assertThat(matcher.match("mauro@test.io", null, null, evaluationContext), is(false)); + assertThat(matcher.match("admin", null, null, evaluationContext), is(true)); + + assertThat(matcher.match("foo", null, null, evaluationContext), is(false)); + assertThat(matcher.match(null, null, null, evaluationContext), is(false)); + } + + @Test + public void usingRbsInConditionTest() throws IOException { + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/rule_base_segments.json")), StandardCharsets.UTF_8); + Evaluator evaluator = Mockito.mock(Evaluator.class); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache); + + SplitChange change = Json.fromJson(load, SplitChange.class); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(ruleBasedSegmentParser, + change.ruleBasedSegments.d); + ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), null, 123); + RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("dependent_rbs"); + HashMap attrib1 = new HashMap() {{ + put("email", "mauro@@split.io"); + }}; + HashMap attrib2 = new HashMap() {{ + put("email", "bilal@@split.io"); + }}; + assertThat(matcher.match("mauro@split.io", null, attrib1, evaluationContext), is(false)); + assertThat(matcher.match("bilal@split.io", null, attrib2, evaluationContext), is(true)); + } + + @Test + public void usingSegmentInExcludedTest() throws IOException { + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/rule_base_segments3.json")), StandardCharsets.UTF_8); + Evaluator evaluator = Mockito.mock(Evaluator.class); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + segmentCache.updateSegment("segment1", Arrays.asList("bilal@split.io"), new ArrayList<>(), 123); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache); + + SplitChange change = Json.fromJson(load, SplitChange.class); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(ruleBasedSegmentParser, + change.ruleBasedSegments.d); + ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), null, 123); + RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("sample_rule_based_segment"); + HashMap attrib1 = new HashMap() {{ + put("email", "mauro@split.io"); + }}; + HashMap attrib2 = new HashMap() {{ + put("email", "bilal@split.io"); + }}; + HashMap attrib3 = new HashMap() {{ + put("email", "pato@split.io"); + }}; + assertThat(matcher.match("mauro@split.io", null, attrib1, evaluationContext), is(false)); + assertThat(matcher.match("bilal@split.io", null, attrib2, evaluationContext), is(false)); + assertThat(matcher.match("pato@split.io", null, attrib3, evaluationContext), is(true)); + } + + @Test + public void usingRbsInExcludedTest() throws IOException { + String load = new String(Files.readAllBytes(Paths.get("src/test/resources/rule_base_segments2.json")), StandardCharsets.UTF_8); + Evaluator evaluator = Mockito.mock(Evaluator.class); + SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCache); + + SplitChange change = Json.fromJson(load, SplitChange.class); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(ruleBasedSegmentParser, + change.ruleBasedSegments.d); + ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), null, 123); + RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("sample_rule_based_segment"); + HashMap attrib1 = new HashMap() {{ + put("email", "mauro@split.io"); + }}; + HashMap attrib2 = new HashMap() {{ + put("email", "bilal@harness.io"); + }}; + assertThat(matcher.match("mauro", null, attrib1, evaluationContext), is(false)); + assertThat(matcher.match("bilal", null, attrib2, evaluationContext), is(true)); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/SemverTest.java b/client/src/test/java/io/split/engine/matchers/SemverTest.java new file mode 100644 index 00000000..40da8264 --- /dev/null +++ b/client/src/test/java/io/split/engine/matchers/SemverTest.java @@ -0,0 +1,112 @@ +package io.split.engine.matchers; + +import org.junit.Test; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertTrue; + +/** + * Tests for AllKeysMatcher + */ +public class SemverTest { + + @Test + public void testValidVersions() throws IOException { + List> versions = new ArrayList<>(); + BufferedReader br = new BufferedReader(new FileReader("src/test/resources/semver/valid-semantic-versions.csv")); + String line; + boolean firstLine = true; + while ((line = br.readLine()) != null) { + if (firstLine) {firstLine = false; continue; } + String[] values = line.split(","); + versions.add(Arrays.asList(values)); + } + for(List version : versions) { + assertTrue(Semver.build(version.get(0)) != null); + assertTrue(Semver.build(version.get(1)) != null); + } + } + + @Test + public void testInvalidVersions() throws IOException { + List> versions = new ArrayList<>(); + BufferedReader br = new BufferedReader(new FileReader("src/test/resources/semver/invalid-semantic-versions.csv")); + String line; + boolean firstLine = true; + while ((line = br.readLine()) != null) { + if (firstLine) {firstLine = false; continue; } + String[] values = line.split(","); + versions.add(Arrays.asList(values)); + } + for(List version : versions) { + assertTrue(Semver.build(version.get(0)) == null); + } + } + + @Test + public void testCompareVersions() throws IOException { + List> versions = new ArrayList<>(); + BufferedReader br = new BufferedReader(new FileReader("src/test/resources/semver/valid-semantic-versions.csv")); + String line; + boolean firstLine = true; + while ((line = br.readLine()) != null) { + if (firstLine) {firstLine = false; continue; } + String[] values = line.split(","); + versions.add(Arrays.asList(values)); + } + for(List version : versions) { + assertTrue(Semver.build(version.get(0)).compare(Semver.build(version.get(1))) == 1); + assertTrue(Semver.build(version.get(1)).compare(Semver.build(version.get(0))) == -1); + } + + versions.clear(); + br = new BufferedReader(new FileReader("src/test/resources/semver/equal-to-semver.csv")); + firstLine = true; + while ((line = br.readLine()) != null) { + if (firstLine) {firstLine = false; continue; } + String[] values = line.split(","); + versions.add(Arrays.asList(values)); + } + for(List version : versions) { + Semver version1 = Semver.build(version.get(0)); + Semver version2 = Semver.build(version.get(1)); + + if (version.get(2).equals("true")) { + assertTrue(version1.version().equals(version2.version())); + } else { + assertTrue(!version1.version().equals(version2.version())); + } + } + + versions.clear(); + br = new BufferedReader(new FileReader("src/test/resources/semver/between-semver.csv")); + firstLine = true; + while ((line = br.readLine()) != null) { + if (firstLine) {firstLine = false; continue; } + String[] values = line.split(","); + versions.add(Arrays.asList(values)); + } + for(List version : versions) { + Semver version1 = Semver.build(version.get(0)); + Semver version2 = Semver.build(version.get(1)); + Semver version3 = Semver.build(version.get(2)); + + if (version.get(3).equals("true")) { + assertTrue(version2.compare(version1) >= 0 && version3.compare(version2) >= 0); + } else { + assertTrue(version2.compare(version1) < 0 || version3.compare(version2) < 0); + } + } + + } + @Test + public void testLeadingZeros() { + assertTrue(Semver.build("1.01.2").version().equals("1\\.1\\.2")); + assertTrue(Semver.build("1.01.2-rc.01").version().equals("1\\.1\\.2-rc\\.1")); + } +} diff --git a/client/src/test/java/io/split/engine/matchers/UserDefinedSegmentMatcherTest.java b/client/src/test/java/io/split/engine/matchers/UserDefinedSegmentMatcherTest.java index 137e815a..b957f73d 100644 --- a/client/src/test/java/io/split/engine/matchers/UserDefinedSegmentMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/UserDefinedSegmentMatcherTest.java @@ -1,9 +1,15 @@ package io.split.engine.matchers; import com.google.common.collect.Sets; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; +import io.split.engine.evaluator.EvaluationContext; +import io.split.engine.evaluator.Evaluator; +import io.split.storages.RuleBasedSegmentCacheConsumer; +import io.split.storages.RuleBasedSegmentCacheProducer; +import io.split.storages.SegmentCache; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; import org.junit.Test; +import org.mockito.Mockito; import java.util.ArrayList; import java.util.Set; @@ -22,16 +28,19 @@ public class UserDefinedSegmentMatcherTest { @Test public void works() { Set keys = Sets.newHashSet("a", "b"); + Evaluator evaluator = Mockito.mock(Evaluator.class); SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); - segmentCache.updateSegment("foo", Stream.of("a","b").collect(Collectors.toList()), new ArrayList<>()); - UserDefinedSegmentMatcher matcher = new UserDefinedSegmentMatcher(segmentCache, "foo"); + RuleBasedSegmentCacheConsumer ruleBasedSegmentCacheConsumer = new RuleBasedSegmentCacheInMemoryImp(); + EvaluationContext evaluationContext = new EvaluationContext(evaluator, segmentCache, ruleBasedSegmentCacheConsumer); + segmentCache.updateSegment("foo", Stream.of("a","b").collect(Collectors.toList()), new ArrayList<>(), 1L); + UserDefinedSegmentMatcher matcher = new UserDefinedSegmentMatcher("foo"); for (String key : keys) { - assertThat(matcher.match(key, null, null, null), is(true)); + assertThat(matcher.match(key, null, null, evaluationContext), is(true)); } - assertThat(matcher.match("foo", null, null, null), is(false)); - assertThat(matcher.match(null, null, null, null), is(false)); + assertThat(matcher.match("foo", null, null, evaluationContext), is(false)); + assertThat(matcher.match(null, null, null, evaluationContext), is(false)); } diff --git a/client/src/test/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcherTest.java b/client/src/test/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcherTest.java index caa7b40d..1c84cf2e 100644 --- a/client/src/test/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/collections/ContainsAllOfSetMatcherTest.java @@ -1,5 +1,6 @@ package io.split.engine.matchers.collections; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -7,77 +8,75 @@ import java.util.List; import java.util.Set; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Created by adilaijaz on 4/18/17. */ public class ContainsAllOfSetMatcherTest { @Test - public void works_for_sets() { + public void worksForSets() { Set set = new HashSet<>(); set.add("first"); set.add("second"); ContainsAllOfSetMatcher matcher = new ContainsAllOfSetMatcher(set); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); Set argument = new HashSet<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); } @Test - public void works_for_lists() { + public void worksForLists() { List list = new ArrayList<>(); list.add("first"); list.add("second"); ContainsAllOfSetMatcher matcher = new ContainsAllOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); } @Test - public void works_for_empty_paramter() { + public void worksForEmptyParamter() { List list = new ArrayList<>(); ContainsAllOfSetMatcher matcher = new ContainsAllOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } } diff --git a/client/src/test/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcherTest.java b/client/src/test/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcherTest.java index 2b54dcba..520959ae 100644 --- a/client/src/test/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/collections/ContainsAnyOfSetMatcherTest.java @@ -1,5 +1,6 @@ package io.split.engine.matchers.collections; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -7,77 +8,74 @@ import java.util.List; import java.util.Set; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Created by adilaijaz on 4/18/17. */ public class ContainsAnyOfSetMatcherTest { @Test - public void works_for_sets() { + public void worksForSets() { Set set = new HashSet<>(); set.add("first"); set.add("second"); ContainsAnyOfSetMatcher matcher = new ContainsAnyOfSetMatcher(set); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); Set argument = new HashSet<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); } @Test - public void works_for_lists() { + public void worksForLists() { List list = new ArrayList<>(); list.add("first"); list.add("second"); ContainsAnyOfSetMatcher matcher = new ContainsAnyOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); } @Test - public void works_for_empty_paramter() { + public void worksForEmptyParamter() { List list = new ArrayList<>(); ContainsAnyOfSetMatcher matcher = new ContainsAnyOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/matchers/collections/EqualToSetMatcherTest.java b/client/src/test/java/io/split/engine/matchers/collections/EqualToSetMatcherTest.java index 24c783c5..ceb3b4b1 100644 --- a/client/src/test/java/io/split/engine/matchers/collections/EqualToSetMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/collections/EqualToSetMatcherTest.java @@ -1,5 +1,6 @@ package io.split.engine.matchers.collections; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -7,100 +8,97 @@ import java.util.List; import java.util.Set; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Created by adilaijaz on 4/18/17. */ public class EqualToSetMatcherTest { @Test - public void works_for_sets() { + public void worksForSets() { Set set = new HashSet<>(); set.add("first"); set.add("second"); EqualToSetMatcher matcher = new EqualToSetMatcher(set); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); Set argument = new HashSet<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } @Test - public void works_for_sets_same_order() { + public void worksForSetsSameOrder() { Set set = new HashSet<>(); set.add("first"); set.add("second"); EqualToSetMatcher matcher = new EqualToSetMatcher(set); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); Set argument = new HashSet<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } @Test - public void works_for_lists() { + public void worksForLists() { List list = new ArrayList<>(); list.add("first"); list.add("second"); EqualToSetMatcher matcher = new EqualToSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } @Test - public void works_for_empty_paramter() { + public void worksForEmptyParamter() { List list = new ArrayList<>(); EqualToSetMatcher matcher = new EqualToSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } } diff --git a/client/src/test/java/io/split/engine/matchers/collections/PartOfSetMatcherTest.java b/client/src/test/java/io/split/engine/matchers/collections/PartOfSetMatcherTest.java index ff75dc9f..0a734e88 100644 --- a/client/src/test/java/io/split/engine/matchers/collections/PartOfSetMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/collections/PartOfSetMatcherTest.java @@ -1,5 +1,6 @@ package io.split.engine.matchers.collections; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -7,77 +8,74 @@ import java.util.List; import java.util.Set; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - /** * Created by adilaijaz on 4/18/17. */ public class PartOfSetMatcherTest { @Test - public void works_for_sets() { + public void worksForSets() { Set set = new HashSet<>(); set.add("first"); set.add("second"); PartOfSetMatcher matcher = new PartOfSetMatcher(set); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); Set argument = new HashSet<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } @Test - public void works_for_lists() { + public void worksForLists() { List list = new ArrayList<>(); list.add("first"); list.add("second"); PartOfSetMatcher matcher = new PartOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(true)); + Assert.assertTrue(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } @Test - public void works_for_empty_paramter() { + public void worksForEmptyParamter() { List list = new ArrayList<>(); PartOfSetMatcher matcher = new PartOfSetMatcher(list); - assertThat(matcher.match(null, null, null, null), is(false)); + Assert.assertFalse(matcher.match(null, null, null, null)); List argument = new ArrayList<>(); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("second"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("first"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); argument.add("third"); - assertThat(matcher.match(argument, null, null, null), is(false)); + Assert.assertFalse(matcher.match(argument, null, null, null)); } } diff --git a/client/src/test/java/io/split/engine/segments/SegmentFetcherImpEventsTest.java b/client/src/test/java/io/split/engine/segments/SegmentFetcherImpEventsTest.java new file mode 100644 index 00000000..b05284fa --- /dev/null +++ b/client/src/test/java/io/split/engine/segments/SegmentFetcherImpEventsTest.java @@ -0,0 +1,74 @@ +package io.split.engine.segments; + +import io.split.client.api.SdkEventType; +import io.split.client.dtos.SegmentChange; +import io.split.client.api.SdkEventMetadata; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.engine.common.FetchOptions; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class SegmentFetcherImpEventsTest { + + private SegmentChangeFetcher _changeFetcher; + private SdkEventsNotifier _notifier; + private SegmentFetcherImp _fetcher; + + @Before + public void setUp() { + _changeFetcher = Mockito.mock(SegmentChangeFetcher.class); + _notifier = Mockito.mock(SdkEventsNotifier.class); + _fetcher = new SegmentFetcherImp("seg", _changeFetcher, new SegmentCacheInMemoryImpl(), + Mockito.mock(TelemetryRuntimeProducer.class), _notifier); + } + + @Test + public void constructionAloneEmitsNothing() { + Mockito.verifyZeroInteractions(_notifier); + } + + @Test + public void membershipChangeEmitsSegmentsUpdatedWithEmptyNames() { + Mockito.when(_changeFetcher.fetch(Mockito.eq("seg"), Mockito.anyLong(), Mockito.any(FetchOptions.class))) + .thenReturn(change(-1, 10, Arrays.asList("a", "b"), new ArrayList())); + + _fetcher.fetch(new FetchOptions.Builder().build()); + + ArgumentCaptor captor = ArgumentCaptor.forClass(SdkEventMetadata.class); + Mockito.verify(_notifier).notify(Mockito.eq(SdkInternalEvent.SEGMENTS_UPDATED), captor.capture()); + Mockito.verifyNoMoreInteractions(_notifier); + assertEquals(SdkEventType.SEGMENTS_UPDATE, captor.getValue().getType()); + assertTrue(captor.getValue().getNames().isEmpty()); + } + + @Test + public void noChangeEmitsNothing() { + Mockito.when(_changeFetcher.fetch(Mockito.eq("seg"), Mockito.anyLong(), Mockito.any(FetchOptions.class))) + .thenReturn(change(-1, 10, new ArrayList(), new ArrayList())); + + _fetcher.fetch(new FetchOptions.Builder().build()); + + Mockito.verifyZeroInteractions(_notifier); + } + + private static SegmentChange change(long since, long till, java.util.List added, java.util.List removed) { + SegmentChange change = new SegmentChange(); + change.name = "seg"; + change.since = since; + change.till = till; + change.added = added; + change.removed = removed; + return change; + } +} diff --git a/client/src/test/java/io/split/engine/segments/SegmentFetcherImpTest.java b/client/src/test/java/io/split/engine/segments/SegmentFetcherImpTest.java index 79786bdd..0872fb45 100644 --- a/client/src/test/java/io/split/engine/segments/SegmentFetcherImpTest.java +++ b/client/src/test/java/io/split/engine/segments/SegmentFetcherImpTest.java @@ -1,26 +1,29 @@ package io.split.engine.segments; -import com.google.common.collect.Sets; -import io.split.cache.SegmentCache; -import io.split.cache.SegmentCacheInMemoryImpl; +import io.split.storages.SegmentCache; +import io.split.storages.SegmentCacheProducer; +import io.split.storages.memory.SegmentCacheInMemoryImpl; import io.split.client.dtos.SegmentChange; -import io.split.engine.SDKReadinessGates; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.engine.common.FetchOptions; +import org.junit.Assert; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; /** * Tests for RefreshableSegmentFetcher. @@ -30,34 +33,31 @@ public class SegmentFetcherImpTest { private static final Logger _log = LoggerFactory.getLogger(SegmentFetcherImpTest.class); private static final String SEGMENT_NAME = "foo"; + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); @Test - public void works_when_we_start_without_state() throws InterruptedException { + public void worksWhenWeStartWithoutState() throws InterruptedException { works(-1L); } @Test - public void works_when_we_start_with_state() throws InterruptedException { + public void worksWhenWeStartWithState() throws InterruptedException { works(20L); - } @Test - public void works_when_there_are_no_changes() throws InterruptedException { - long startingChangeNumber = -1L; - SDKReadinessGates gates = new SDKReadinessGates(); - gates.registerSegment(SEGMENT_NAME); + public void worksWhenThereAreNoChanges() throws InterruptedException { SegmentCache segmentCache = new SegmentCacheInMemoryImpl(); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChange = getSegmentChange(-1L, 10L); - Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean())).thenReturn(segmentChange); + Mockito.when(segmentChangeFetcher.fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.any())).thenReturn(segmentChange); - SegmentFetcherImp fetcher = new SegmentFetcherImp(SEGMENT_NAME, segmentChangeFetcher, gates, segmentCache); + SegmentFetcherImp fetcher = new SegmentFetcherImp(SEGMENT_NAME, segmentChangeFetcher, segmentCache, TELEMETRY_STORAGE); // execute the fetcher for a little bit. ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - scheduledExecutorService.scheduleWithFixedDelay(fetcher::fetchAll, 0L, 100, TimeUnit.MICROSECONDS); + scheduledExecutorService.scheduleWithFixedDelay(() -> fetcher.fetch(new FetchOptions.Builder().build()), 0L, 100, TimeUnit.MICROSECONDS); Thread.currentThread().sleep(5 * 100); scheduledExecutorService.shutdown(); @@ -72,33 +72,27 @@ public void works_when_there_are_no_changes() throws InterruptedException { Thread.currentThread().interrupt(); } - Set expected = Sets.newHashSet("" + (startingChangeNumber + 1)); - assertNotNull(segmentCache.getChangeNumber(SEGMENT_NAME)); assertEquals(10L, segmentCache.getChangeNumber(SEGMENT_NAME)); - assertThat(gates.areSegmentsReady(10), is(true)); - } private void works(long startingChangeNumber) throws InterruptedException { - SDKReadinessGates gates = new SDKReadinessGates(); String segmentName = SEGMENT_NAME; - gates.registerSegment(segmentName); - SegmentCache segmentCache = Mockito.mock(SegmentCache.class); - Mockito.when(segmentCache.getChangeNumber(SEGMENT_NAME)).thenReturn(-1L).thenReturn(-1L) + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); + Mockito.when(segmentCacheProducer.getChangeNumber(SEGMENT_NAME)).thenReturn(-1L).thenReturn(-1L) .thenReturn(-1L) .thenReturn(0L); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); SegmentChange segmentChange = getSegmentChange(-1L, -1L); - Mockito.when(segmentChangeFetcher.fetch(SEGMENT_NAME, -1L, false)).thenReturn(segmentChange); - Mockito.when(segmentChangeFetcher.fetch(SEGMENT_NAME, 0L, false)).thenReturn(segmentChange); - SegmentFetcher fetcher = new SegmentFetcherImp(segmentName, segmentChangeFetcher, gates, segmentCache); + Mockito.when(segmentChangeFetcher.fetch(Mockito.eq(SEGMENT_NAME),Mockito.eq( -1L), Mockito.any())).thenReturn(segmentChange); + Mockito.when(segmentChangeFetcher.fetch(Mockito.eq(SEGMENT_NAME),Mockito.eq( 0L), Mockito.any())).thenReturn(segmentChange); + SegmentFetcher fetcher = new SegmentFetcherImp(segmentName, segmentChangeFetcher, segmentCacheProducer, Mockito.mock(TelemetryRuntimeProducer.class)); // execute the fetcher for a little bit. ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - scheduledExecutorService.scheduleWithFixedDelay(fetcher::fetchAll, 0L, Integer.MAX_VALUE, TimeUnit.SECONDS); + scheduledExecutorService.scheduleWithFixedDelay(() -> fetcher.fetch(new FetchOptions.Builder().build()), 0L, Integer.MAX_VALUE, TimeUnit.SECONDS); Thread.currentThread().sleep(5 * 100); scheduledExecutorService.shutdown(); @@ -112,30 +106,64 @@ private void works(long startingChangeNumber) throws InterruptedException { // reset the interrupt. Thread.currentThread().interrupt(); } - Mockito.verify(segmentChangeFetcher, Mockito.times(2)).fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean()); - assertThat(gates.areSegmentsReady(10), is(true)); - + Mockito.verify(segmentChangeFetcher, Mockito.times(2)).fetch(Mockito.anyString(), Mockito.anyLong(), Mockito.anyObject()); } @Test(expected = NullPointerException.class) - public void does_not_work_if_segment_change_fetcher_is_null() { - SegmentCache segmentCache = Mockito.mock(SegmentCache.class); - SegmentFetcher fetcher = new SegmentFetcherImp(SEGMENT_NAME, null, new SDKReadinessGates(), segmentCache); + public void doesNotWorkIfSegmentChangeFetcherIsNull() { + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); + SegmentFetcher fetcher = new SegmentFetcherImp(SEGMENT_NAME, null, segmentCacheProducer, TELEMETRY_STORAGE); } @Test(expected = NullPointerException.class) - public void does_not_work_if_segment_name_is_null() { - SegmentCache segmentCache = Mockito.mock(SegmentCache.class); + public void doesNotWorkIfSegmentNameIsNull() { + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); - SegmentFetcher fetcher = new SegmentFetcherImp(null, segmentChangeFetcher, new SDKReadinessGates(), segmentCache); + SegmentFetcher fetcher = new SegmentFetcherImp(null, segmentChangeFetcher, segmentCacheProducer, TELEMETRY_STORAGE); } - @Test(expected = NullPointerException.class) - public void does_not_work_if_sdk_readiness_gates_are_null() { - SegmentCache segmentCache = Mockito.mock(SegmentCache.class); - SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); - SegmentFetcher fetcher = new SegmentFetcherImp(SEGMENT_NAME, segmentChangeFetcher, null, segmentCache); + @Test + public void testBypassCdnClearedAfterFirstHit() { + SegmentChangeFetcher mockFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentCache segmentCacheMock = new SegmentCacheInMemoryImpl(); + SegmentFetcher fetcher = new SegmentFetcherImp("someSegment", mockFetcher, segmentCacheMock, Mockito.mock(TelemetryRuntimeProducer.class)); + + + SegmentChange response1 = new SegmentChange(); + response1.name = "someSegment"; + response1.added = new ArrayList<>(); + response1.removed = new ArrayList<>(); + response1.since = -1; + response1.till = 1; + + SegmentChange response2 = new SegmentChange(); + response2.name = "someSegment"; + response2.added = new ArrayList<>(); + response2.removed = new ArrayList<>(); + response2.since = 1; + response2.till = 1; + + ArgumentCaptor optionsCaptor = ArgumentCaptor.forClass(FetchOptions.class); + ArgumentCaptor cnCaptor = ArgumentCaptor.forClass(Long.class); + when(mockFetcher.fetch(Mockito.eq("someSegment"), cnCaptor.capture(), optionsCaptor.capture())).thenReturn(response1, response2); + + FetchOptions originalOptions = new FetchOptions.Builder().targetChangeNumber(123).build(); + fetcher.fetch(originalOptions); + List capturedCNs = cnCaptor.getAllValues(); + List capturedOptions = optionsCaptor.getAllValues(); + + Assert.assertEquals(capturedOptions.size(), 2); + Assert.assertEquals(capturedCNs.size(), 2); + + Assert.assertEquals(capturedCNs.get(0), Long.valueOf(-1)); + Assert.assertEquals(capturedCNs.get(1), Long.valueOf(1)); + + Assert.assertEquals(capturedOptions.get(0).targetCN(), 123); + Assert.assertEquals(capturedOptions.get(1).targetCN(), -1); + + // Ensure that the original value hasn't been modified + Assert.assertEquals(originalOptions.targetCN(), 123); } private SegmentChange getSegmentChange(long since, long till){ @@ -147,4 +175,4 @@ private SegmentChange getSegmentChange(long since, long till){ segmentChange.removed = new ArrayList<>(); return segmentChange; } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/segments/SegmentImpTest.java b/client/src/test/java/io/split/engine/segments/SegmentImpTest.java index e3b0f37b..5d042e1d 100644 --- a/client/src/test/java/io/split/engine/segments/SegmentImpTest.java +++ b/client/src/test/java/io/split/engine/segments/SegmentImpTest.java @@ -48,7 +48,7 @@ public void testSetChangeNumber(){ @Test public void testUpdateSegment(){ SegmentImp segmentImp = new SegmentImp(CHANGE_NUMBER, SEGMENT_NAME); - segmentImp.update(Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>()); + segmentImp.update(Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>(), 1L); assertTrue(segmentImp.contains(KEY)); } diff --git a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java index b856c1d7..f6f7f04f 100644 --- a/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java +++ b/client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java @@ -1,21 +1,44 @@ package io.split.engine.segments; -import io.split.engine.SDKReadinessGates; -import io.split.cache.SegmentCache; +import com.google.common.collect.Maps; +import io.split.Spec; +import io.split.client.LocalhostSegmentChangeFetcher; +import io.split.client.JsonLocalhostSplitChangeFetcher; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.utils.InputStreamProvider; +import io.split.client.utils.StaticContentInputStreamProvider; +import io.split.engine.common.FetchOptions; +import io.split.engine.experiments.*; +import io.split.storages.*; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; +import io.split.storages.memory.SegmentCacheInMemoryImpl; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.NoopTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashSet; import java.util.List; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; /** * Tests for SegmentSynchronizationTaskImp @@ -24,6 +47,8 @@ */ public class SegmentSynchronizationTaskImpTest { private static final Logger _log = LoggerFactory.getLogger(SegmentSynchronizationTaskImpTest.class); + private static final TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + private static final TelemetryStorage TELEMETRY_STORAGE_NOOP = Mockito.mock(NoopTelemetryStorage.class); private AtomicReference fetcher1 = null; private AtomicReference fetcher2 = null; @@ -36,11 +61,11 @@ public void beforeMethod() { @Test public void works() { - SDKReadinessGates gates = new SDKReadinessGates(); - SegmentCache segmentCache = Mockito.mock(SegmentCache.class); + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); - final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, gates, segmentCache); + final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, segmentCacheProducer, + TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), null, Mockito.mock(RuleBasedSegmentCache.class)); // create two tasks that will separately call segment and make sure @@ -72,11 +97,91 @@ public void run() { Thread.currentThread().interrupt(); } - gates.splitsAreReady(); + Assert.assertNotNull(fetcher1.get()); + assertEquals(fetcher1.get(), fetcher2.get()); + } + + @Test + public void testFetchAllAsynchronousAndGetFalse() throws NoSuchFieldException, IllegalAccessException { + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); + ConcurrentMap _segmentFetchers = Maps.newConcurrentMap(); + + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class); + _segmentFetchers.put("SF", segmentFetcher); + final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, + segmentCacheProducer, TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), null, Mockito.mock(RuleBasedSegmentCache.class)); + Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(false); + Mockito.when(segmentFetcher.fetch(Mockito.anyObject())).thenReturn(false); + + // Before executing, we'll update the map of segmentFecthers via reflection. + Field segmentFetchersForced = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers"); + segmentFetchersForced.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(segmentFetchersForced, segmentFetchersForced.getModifiers() & ~Modifier.FINAL); + + segmentFetchersForced.set(fetchers, _segmentFetchers); + boolean fetch = fetchers.fetchAllSynchronous(); + Assert.assertEquals(false, fetch); + } + + @Test + public void testFetchAllAsynchronousAndGetTrue() throws NoSuchFieldException, IllegalAccessException { + SegmentCacheProducer segmentCacheProducer = Mockito.mock(SegmentCacheProducer.class); - assertThat(fetcher1.get(), is(notNullValue())); - assertThat(fetcher1.get(), is(sameInstance(fetcher2.get()))); + ConcurrentMap _segmentFetchers = Maps.newConcurrentMap(); + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(SegmentChangeFetcher.class); + SegmentFetcherImp segmentFetcher = Mockito.mock(SegmentFetcherImp.class); + final SegmentSynchronizationTaskImp fetchers = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1L, 1, segmentCacheProducer, + TELEMETRY_STORAGE, Mockito.mock(SplitCacheConsumer.class), null, Mockito.mock(RuleBasedSegmentCache.class)); + + // Before executing, we'll update the map of segmentFecthers via reflection. + Field segmentFetchersForced = SegmentSynchronizationTaskImp.class.getDeclaredField("_segmentFetchers"); + segmentFetchersForced.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(segmentFetchersForced, segmentFetchersForced.getModifiers() & ~Modifier.FINAL); + segmentFetchersForced.set(fetchers, _segmentFetchers); + Mockito.when(segmentFetcher.runWhitCacheHeader()).thenReturn(true); + Mockito.when(segmentFetcher.fetch(Mockito.anyObject())).thenReturn(true); + boolean fetch = fetchers.fetchAllSynchronous(); + Assert.assertEquals(true, fetch); } + @Test + public void testLocalhostSegmentChangeFetcher() throws InterruptedException, FileNotFoundException { + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>()); + SplitCache splitCacheProducer = new InMemoryCacheImp(flagSetsFilter); + + InputStream inputStream = new FileInputStream("src/test/resources/split_init.json"); + InputStreamProvider inputStreamProvider = new StaticContentInputStreamProvider(inputStream); + SplitChangeFetcher splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider); + SplitParser splitParser = new SplitParser(); + FetchOptions fetchOptions = new FetchOptions.Builder().build(); + RuleBasedSegmentCache ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + + SplitFetcher splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCacheProducer, TELEMETRY_STORAGE_NOOP, flagSetsFilter, + ruleBasedSegmentParser, ruleBasedSegmentCache); + + SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000, null); + + splitSynchronizationTask.start(); + + Thread.sleep(2000); -} + SegmentChangeFetcher segmentChangeFetcher = Mockito.mock(LocalhostSegmentChangeFetcher.class); + SegmentCacheProducer segmentCacheProducer = new SegmentCacheInMemoryImpl(); + + SegmentSynchronizationTaskImp segmentSynchronizationTaskImp = new SegmentSynchronizationTaskImp(segmentChangeFetcher, 1000, 1, segmentCacheProducer, + TELEMETRY_STORAGE_NOOP, splitCacheProducer, null, ruleBasedSegmentCache); + + segmentSynchronizationTaskImp.start(); + + Thread.sleep(2000); + + Mockito.verify(segmentChangeFetcher, Mockito.times(1)).fetch("segment_1",-1, fetchOptions); + Mockito.verify(segmentChangeFetcher, Mockito.times(1)).fetch("segment_2",-1, fetchOptions); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/splitter/HashConsistencyTest.java b/client/src/test/java/io/split/engine/splitter/HashConsistencyTest.java index 34b0e557..e0de5ae2 100644 --- a/client/src/test/java/io/split/engine/splitter/HashConsistencyTest.java +++ b/client/src/test/java/io/split/engine/splitter/HashConsistencyTest.java @@ -1,8 +1,8 @@ package io.split.engine.splitter; -import com.google.common.base.Charsets; import com.google.common.hash.Hashing; import io.split.client.utils.MurmurHash3; +import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -12,7 +12,6 @@ import java.io.FileReader; import java.io.IOException; import java.net.URL; -import java.nio.charset.Charset; public class HashConsistencyTest { @Test @@ -77,7 +76,7 @@ private void validateFileLegacyHash(File file) throws IOException { int expected_hash = Integer.parseInt(parts[2]); int expected_bucket = Integer.parseInt(parts[3]); - int hash = Splitter.legacy_hash(key, seed); + int hash = Splitter.legacyHash(key, seed); int bucket = Splitter.bucket(hash); Assert.assertEquals(expected_hash, hash); @@ -98,7 +97,7 @@ private void validateFileMurmur3Hash(File file) throws IOException { long expected_hash = Long.parseLong(parts[2]); int expected_bucket = Integer.parseInt(parts[3]); - long hash = Splitter.murmur_hash(key, seed); + long hash = Splitter.murmurHash(key, seed); int bucket = Splitter.bucket(hash); Assert.assertEquals(expected_hash, hash); @@ -119,7 +118,7 @@ private void validateFileGuavaMurmur3Hash(File file) throws IOException { long expected_hash = Long.parseLong(parts[2]); int expected_bucket = Integer.parseInt(parts[3]); - int hash = Hashing.murmur3_32(seed).hashBytes(key.getBytes(Charsets.UTF_8)).asInt(); + int hash = Hashing.murmur3_32(seed).hashBytes(key.getBytes(StandardCharsets.UTF_8)).asInt(); int bucket = Splitter.bucket(hash); diff --git a/client/src/test/java/io/split/engine/splitter/HashingTest.java b/client/src/test/java/io/split/engine/splitter/HashingTest.java index aafad557..fac461bb 100644 --- a/client/src/test/java/io/split/engine/splitter/HashingTest.java +++ b/client/src/test/java/io/split/engine/splitter/HashingTest.java @@ -144,8 +144,6 @@ private void collisionTest(int seed, MyHash hash, List keys) { } } - System.out.println(hash + " collisions: " + collisions + " percentage: " + (100f * collisions / keys.size())); - System.out.println(hash + " time: " + durationSum / keys.size() + " ns"); } private void bucketTest(int seed, MyHash hash, List keys) { @@ -161,8 +159,6 @@ private void bucketTest(int seed, MyHash hash, List keys) { ranges[(bucket - 1) / 10]++; } - System.out.println(buckets); - System.out.println(Arrays.toString(ranges)); } private void spreadBySeed(int seed1, int seed2, MyHash hash, List keys) { @@ -182,6 +178,5 @@ private void spreadBySeed(int seed1, int seed2, MyHash hash, List keys) } int collisions = bitset.cardinality(); - System.out.println(hash + " collisions " + collisions + " percentage: " + (100f * collisions / keys.size())); } } diff --git a/client/src/test/java/io/split/engine/splitter/SplitterTest.java b/client/src/test/java/io/split/engine/splitter/SplitterTest.java index e888d2fd..5210b779 100644 --- a/client/src/test/java/io/split/engine/splitter/SplitterTest.java +++ b/client/src/test/java/io/split/engine/splitter/SplitterTest.java @@ -40,7 +40,6 @@ public void generateData() { String key = RandomStringUtils.randomAlphanumeric(keyLength); long hash = Splitter.hash(key, seed, 1); int bucket = Splitter.bucket(hash); - System.out.println(Joiner.on(',').join(Lists.newArrayList(seed, key, hash, bucket))); } } @@ -59,7 +58,6 @@ public void generateNonAlphaNumericData() { String key = RandomStringUtils.random(keyLength); long hash = Splitter.hash(key, seed, 1); int bucket = Splitter.bucket(hash); - System.out.println(Joiner.on(',').join(Lists.newArrayList(seed, key, hash, bucket))); } } diff --git a/client/src/test/java/io/split/engine/sse/AuthApiClientTest.java b/client/src/test/java/io/split/engine/sse/AuthApiClientTest.java index 2debfe68..b6f05e04 100644 --- a/client/src/test/java/io/split/engine/sse/AuthApiClientTest.java +++ b/client/src/test/java/io/split/engine/sse/AuthApiClientTest.java @@ -1,22 +1,41 @@ package io.split.engine.sse; import io.split.TestHelper; +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; import io.split.engine.sse.dtos.AuthenticationResponse; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.core5.http.HttpStatus; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import java.io.IOException; import java.lang.reflect.InvocationTargetException; +import java.net.URISyntaxException; public class AuthApiClientTest { - @Test - public void authenticateWithPushEnabledShouldReturnSuccess() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { - CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-enabled.json", HttpStatus.SC_OK); + private static TelemetryStorage TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + + @Before + public void setUp() { + TELEMETRY_STORAGE = Mockito.mock(InMemoryTelemetryStorage.class); + } - AuthApiClient authApiClient = new AuthApiClientImp( "www.split-test.io", httpClientMock); + @Test + public void authenticateWithPushEnabledShouldReturnSuccess() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-enabled.json", + HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertTrue(result.isPushEnabled()); @@ -24,14 +43,22 @@ public void authenticateWithPushEnabledShouldReturnSuccess() throws IOException, Assert.assertFalse(result.isRetry()); Assert.assertFalse(StringUtils.isEmpty(result.getToken())); Assert.assertTrue(result.getExpiration() > 0); + Mockito.verify(TELEMETRY_STORAGE, Mockito.times(1)).recordTokenRefreshes(); + Mockito.verify(TELEMETRY_STORAGE, Mockito.times(1)).recordSyncLatency(Mockito.anyObject(), Mockito.anyLong()); + Mockito.verify(TELEMETRY_STORAGE, Mockito.times(1)).recordSuccessfulSync(Mockito.anyObject(), + Mockito.anyLong()); } @Test - public void authenticateWithPushEnabledWithWrongTokenShouldReturnError() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { - CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-enabled-wrong-token.json", HttpStatus.SC_OK); - - AuthApiClient authApiClient = new AuthApiClientImp( "www.split-test.io", httpClientMock); + public void authenticateWithPushEnabledWithWrongTokenShouldReturnError() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-enabled-wrong-token.json", + HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertFalse(result.isPushEnabled()); @@ -42,10 +69,31 @@ public void authenticateWithPushEnabledWithWrongTokenShouldReturnError() throws } @Test - public void authenticateWithPushDisabledShouldReturnSuccess() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { - CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-disabled.json", HttpStatus.SC_OK); + public void authenticateWithPushDisabledShouldReturnSuccess() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-disabled.json", + HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); + AuthenticationResponse result = authApiClient.Authenticate(); + + Assert.assertFalse(result.isPushEnabled()); + Assert.assertTrue(StringUtils.isEmpty(result.getChannels())); + Assert.assertFalse(result.isRetry()); + Assert.assertTrue(StringUtils.isEmpty(result.getToken())); + } - AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", httpClientMock); + @Test + public void authenticateWithPushDisabledWithEmptyTokenShouldReturnSuccess() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("streaming-auth-push-disabled-empty-token.json", + HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertFalse(result.isPushEnabled()); @@ -55,10 +103,13 @@ public void authenticateWithPushDisabledShouldReturnSuccess() throws IOException } @Test - public void authenticateServerErrorShouldReturnErrorWithRetry() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void authenticateServerErrorShouldReturnErrorWithRetry() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("", HttpStatus.SC_INTERNAL_SERVER_ERROR); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); - AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", httpClientMock); + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertFalse(result.isPushEnabled()); @@ -68,10 +119,13 @@ public void authenticateServerErrorShouldReturnErrorWithRetry() throws IOExcepti } @Test - public void authenticateServerBadRequestShouldReturnErrorWithoutRetry() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void authenticateServerBadRequestShouldReturnErrorWithoutRetry() throws IOException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, URISyntaxException { CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("", HttpStatus.SC_BAD_REQUEST); - AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", httpClientMock); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertFalse(result.isPushEnabled()); @@ -81,15 +135,24 @@ public void authenticateServerBadRequestShouldReturnErrorWithoutRetry() throws I } @Test - public void authenticateServerUnauthorizedShouldReturnErrorWithoutRetry() throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void authenticateServerUnauthorizedShouldReturnErrorWithoutRetry() throws IOException, + IllegalAccessException, NoSuchMethodException, InvocationTargetException, URISyntaxException { CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("", HttpStatus.SC_UNAUTHORIZED); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null), + "qwerty", metadata()); - AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", httpClientMock); + AuthApiClient authApiClient = new AuthApiClientImp("www.split-test.io", splitHttpClient, TELEMETRY_STORAGE); AuthenticationResponse result = authApiClient.Authenticate(); Assert.assertFalse(result.isPushEnabled()); Assert.assertTrue(StringUtils.isEmpty(result.getChannels())); Assert.assertTrue(StringUtils.isEmpty(result.getToken())); Assert.assertFalse(result.isRetry()); + Mockito.verify(TELEMETRY_STORAGE, Mockito.times(1)).recordAuthRejections(); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); } + } diff --git a/client/src/test/java/io/split/engine/sse/CommonChangeNotificationTest.java b/client/src/test/java/io/split/engine/sse/CommonChangeNotificationTest.java new file mode 100644 index 00000000..f536870c --- /dev/null +++ b/client/src/test/java/io/split/engine/sse/CommonChangeNotificationTest.java @@ -0,0 +1,46 @@ +package io.split.engine.sse; + +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; +import io.split.client.dtos.Status; +import io.split.client.utils.Json; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.GenericNotificationData; +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.RawMessageNotification; +import io.split.engine.sse.enums.CompressType; +import org.junit.Assert; +import org.junit.Test; + +public class CommonChangeNotificationTest { + + @Test + public void testFeatureFlagNotification() { + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":2,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification featureFlagChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); + Assert.assertEquals(IncomingNotification.Type.SPLIT_UPDATE, featureFlagChangeNotification.getType()); + Assert.assertEquals(1684265694505L, featureFlagChangeNotification.getChangeNumber()); + Assert.assertEquals(CompressType.ZLIB, featureFlagChangeNotification.getCompressType()); + Assert.assertEquals(0L, featureFlagChangeNotification.getPreviousChangeNumber()); + Assert.assertEquals("mauro_java", featureFlagChangeNotification.getDefinition().name); + Assert.assertEquals(-1769377604, featureFlagChangeNotification.getDefinition().seed); + } + + @Test + public void testRuleBasedSegmentNotification() { + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"RB_SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":0,\\\"d\\\":\\\"eyJjaGFuZ2VOdW1iZXIiOiA1LCAibmFtZSI6ICJzYW1wbGVfcnVsZV9iYXNlZF9zZWdtZW50IiwgInN0YXR1cyI6ICJBQ1RJVkUiLCAidHJhZmZpY1R5cGVOYW1lIjogInVzZXIiLCAiZXhjbHVkZWQiOiB7ImtleXMiOiBbIm1hdXJvQHNwbGl0LmlvIiwgImdhc3RvbkBzcGxpdC5pbyJdLCAic2VnbWVudHMiOiBbXX0sICJjb25kaXRpb25zIjogW3sibWF0Y2hlckdyb3VwIjogeyJjb21iaW5lciI6ICJBTkQiLCAibWF0Y2hlcnMiOiBbeyJrZXlTZWxlY3RvciI6IHsidHJhZmZpY1R5cGUiOiAidXNlciIsICJhdHRyaWJ1dGUiOiAiZW1haWwifSwgIm1hdGNoZXJUeXBlIjogIkVORFNfV0lUSCIsICJuZWdhdGUiOiBmYWxzZSwgIndoaXRlbGlzdE1hdGNoZXJEYXRhIjogeyJ3aGl0ZWxpc3QiOiBbIkBzcGxpdC5pbyJdfX1dfX1dfQ==\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification ruleBasedSegmentCommonChangeNotification = new CommonChangeNotification(genericNotificationData, RuleBasedSegment.class); + Assert.assertEquals(IncomingNotification.Type.RB_SEGMENT_UPDATE, ruleBasedSegmentCommonChangeNotification.getType()); + Assert.assertEquals(1684265694505L, ruleBasedSegmentCommonChangeNotification.getChangeNumber()); + Assert.assertEquals(CompressType.NOT_COMPRESSED, ruleBasedSegmentCommonChangeNotification.getCompressType()); + Assert.assertEquals(0L, ruleBasedSegmentCommonChangeNotification.getPreviousChangeNumber()); + Assert.assertEquals("sample_rule_based_segment", ruleBasedSegmentCommonChangeNotification.getDefinition().name); + Assert.assertEquals(Status.ACTIVE, ruleBasedSegmentCommonChangeNotification.getDefinition().status); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/EventSourceClientTest.java b/client/src/test/java/io/split/engine/sse/EventSourceClientTest.java index 704e8dbb..e3a5050e 100644 --- a/client/src/test/java/io/split/engine/sse/EventSourceClientTest.java +++ b/client/src/test/java/io/split/engine/sse/EventSourceClientTest.java @@ -1,9 +1,12 @@ package io.split.engine.sse; import io.split.SSEMockServer; +import io.split.client.RequestDecorator; import io.split.engine.sse.client.SSEClient; +import io.split.engine.sse.dtos.CommonChangeNotification; import io.split.engine.sse.dtos.ErrorNotification; -import io.split.engine.sse.dtos.SplitChangeNotification; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; @@ -37,11 +40,12 @@ public void setUp() { public void startShouldConnect() throws IOException { SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); sseServer.start(); - EventSourceClient eventSourceClient = new EventSourceClientImp("http://localhost:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient()); + EventSourceClient eventSourceClient = new EventSourceClientImp("http://localhost:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient(), telemetryRuntimeProducer, null, new RequestDecorator(null)); - boolean result = eventSourceClient.start("channel-test","token-test"); + boolean result = eventSourceClient.start("channel-test", "token-test"); Assert.assertTrue(result); @@ -49,29 +53,31 @@ public void startShouldConnect() throws IOException { } @Test - public void startShouldNotConnect() throws IOException { + public void startShouldReconnect() throws IOException { SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); sseServer.start(); - EventSourceClient eventSourceClient = new EventSourceClientImp("http://fake:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient()); + EventSourceClient eventSourceClient = new EventSourceClientImp("http://fake:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient(), telemetryRuntimeProducer, null, new RequestDecorator(null)); - boolean result = eventSourceClient.start("channel-test","token-test"); + boolean result = eventSourceClient.start("channel-test", "token-test"); Assert.assertFalse(result); Awaitility.await() .atMost(50L, TimeUnit.SECONDS) - .untilAsserted(() -> Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleSseStatus(SSEClient.StatusMessage.NONRETRYABLE_ERROR)); + .untilAsserted(() -> Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR)); } @Test public void startAndReceiveNotification() throws IOException { SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue(); SSEMockServer sseServer = buildSSEMockServer(eventQueue); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); sseServer.start(); - EventSourceClient eventSourceClient = new EventSourceClientImp("http://localhost:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient()); + EventSourceClient eventSourceClient = new EventSourceClientImp("http://localhost:" + sseServer.getPort(), _notificationParser, _notificationProcessor, _pushStatusTracker, buildHttpClient(), telemetryRuntimeProducer, null, new RequestDecorator(null)); - boolean result = eventSourceClient.start("channel-test","token-test"); + boolean result = eventSourceClient.start("channel-test", "token-test"); Assert.assertTrue(result); @@ -90,7 +96,7 @@ public void startAndReceiveNotification() throws IOException { Awaitility.await() .atMost(50L, TimeUnit.SECONDS) - .untilAsserted(() -> Mockito.verify(_notificationProcessor, Mockito.times(1)).process(Mockito.any(SplitChangeNotification.class))); + .untilAsserted(() -> Mockito.verify(_notificationProcessor, Mockito.times(1)).process(Mockito.any(CommonChangeNotification.class))); OutboundSseEvent sseEventError = new OutboundEvent .Builder() @@ -106,6 +112,8 @@ public void startAndReceiveNotification() throws IOException { Awaitility.await() .atMost(50L, TimeUnit.SECONDS) .untilAsserted(() -> Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleIncomingAblyError(Mockito.any(ErrorNotification.class))); + + Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleSseStatus(SSEClient.StatusMessage.FIRST_EVENT); } private SSEMockServer buildSSEMockServer(SSEMockServer.SseEventQueue eventQueue) { @@ -131,4 +139,4 @@ private static CloseableHttpClient buildHttpClient() { .setDefaultRequestConfig(requestConfig) .build(); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/NotificationParserImpTest.java b/client/src/test/java/io/split/engine/sse/NotificationParserImpTest.java new file mode 100644 index 00000000..b8df6132 --- /dev/null +++ b/client/src/test/java/io/split/engine/sse/NotificationParserImpTest.java @@ -0,0 +1,71 @@ +package io.split.engine.sse; + +import io.split.client.dtos.Split; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.enums.CompressType; +import io.split.engine.sse.exceptions.EventParsingException; + +import org.junit.Assert; +import org.junit.Test; + +public class NotificationParserImpTest { + + @Test + public void validateZlibCompressType() throws EventParsingException { + String payload = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":2,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + NotificationParserImp notificationParserImp = new NotificationParserImp(); + + CommonChangeNotification incomingNotification = (CommonChangeNotification) notificationParserImp.parseMessage(payload); + Split split = (Split) incomingNotification.getDefinition(); + Assert.assertEquals("mauro_java", split.name); + Assert.assertEquals(1684265694505L, split.changeNumber); + Assert.assertEquals(CompressType.ZLIB, incomingNotification.getCompressType()); + Assert.assertEquals(0, incomingNotification.getPreviousChangeNumber()); + } + + @Test + public void validateGzipCompressType() throws EventParsingException { + String payload = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":1,\\\"d\\\":\\\"H4sIAAAAAAAA/8yT327aTBDFXyU612vJxoTgvUMfKB8qcaSapqoihAZ7DNusvWi9TpUiv3tl/pdQVb1qL+cwc3bOj/EGzlKeq3T6tuaYCoZEXbGFgMogkXXDIM0y31v4C/aCgMnrU9/3gl7Pp4yilMMIAuVusqDamvlXeiWIg/FAa5OSU6aEDHz/ip4wZ5Be1AmjoBsFAtVOCO56UXh31/O7ApUjV1eQGPw3HT+NIPCitG7bctIVC2ScU63d1DK5gksHCZPnEEhXVC45rosFW8ig1++GYej3g85tJEB6aSA7Aqkpc7Ws7XahCnLTbLVM7evnzalsUUHi8//j6WgyTqYQKMilK7b31tRryLa3WKiyfRCDeHhq2Dntiys+JS/J8THUt5VyrFXlHnYTQ3LU2h91yGdQVqhy+0RtTeuhUoNZ08wagTVZdxbBndF5vYVApb7z9m9pZgKaFqwhT+6coRHvg398nEweP/157Bd+S1hz6oxtm88O73B0jbhgM47nyej+YRRfgdNODDlXJWcJL9tUF5SqnRqfbtPr4LdcTHnk4rfp3buLOkG7+Pmp++vRM9w/wVblzX7Pm8OGfxf5YDKZfxh9SS6B/2Pc9t/7ja01o5k1PwIAAP//uTipVskEAAA=\\\"}\"}"; + NotificationParserImp notificationParserImp = new NotificationParserImp(); + + CommonChangeNotification incomingNotification = (CommonChangeNotification) notificationParserImp.parseMessage(payload); + Split split = (Split) incomingNotification.getDefinition(); + Assert.assertEquals("mauro_java", split.name); + Assert.assertEquals(1684333081259L, split.changeNumber); + Assert.assertEquals(CompressType.GZIP, incomingNotification.getCompressType()); + Assert.assertEquals(0, incomingNotification.getPreviousChangeNumber()); + } + + @Test + public void validateNotCompressType() throws EventParsingException { + String payload = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684329854385,\\\"pcn\\\":0,\\\"c\\\":0,\\\"d\\\":\\\"eyJ0cmFmZmljVHlwZU5hbWUiOiJ1c2VyIiwiaWQiOiJkNDMxY2RkMC1iMGJlLTExZWEtOGE4MC0xNjYwYWRhOWNlMzkiLCJuYW1lIjoibWF1cm9famF2YSIsInRyYWZmaWNBbGxvY2F0aW9uIjoxMDAsInRyYWZmaWNBbGxvY2F0aW9uU2VlZCI6LTkyMzkxNDkxLCJzZWVkIjotMTc2OTM3NzYwNCwic3RhdHVzIjoiQUNUSVZFIiwia2lsbGVkIjpmYWxzZSwiZGVmYXVsdFRyZWF0bWVudCI6Im9mZiIsImNoYW5nZU51bWJlciI6MTY4NDMyOTg1NDM4NSwiYWxnbyI6MiwiY29uZmlndXJhdGlvbnMiOnt9LCJjb25kaXRpb25zIjpbeyJjb25kaXRpb25UeXBlIjoiV0hJVEVMSVNUIiwibWF0Y2hlckdyb3VwIjp7ImNvbWJpbmVyIjoiQU5EIiwibWF0Y2hlcnMiOlt7Im1hdGNoZXJUeXBlIjoiV0hJVEVMSVNUIiwibmVnYXRlIjpmYWxzZSwid2hpdGVsaXN0TWF0Y2hlckRhdGEiOnsid2hpdGVsaXN0IjpbImFkbWluIiwibWF1cm8iLCJuaWNvIl19fV19LCJwYXJ0aXRpb25zIjpbeyJ0cmVhdG1lbnQiOiJvZmYiLCJzaXplIjoxMDB9XSwibGFiZWwiOiJ3aGl0ZWxpc3RlZCJ9LHsiY29uZGl0aW9uVHlwZSI6IlJPTExPVVQiLCJtYXRjaGVyR3JvdXAiOnsiY29tYmluZXIiOiJBTkQiLCJtYXRjaGVycyI6W3sia2V5U2VsZWN0b3IiOnsidHJhZmZpY1R5cGUiOiJ1c2VyIn0sIm1hdGNoZXJUeXBlIjoiSU5fU0VHTUVOVCIsIm5lZ2F0ZSI6ZmFsc2UsInVzZXJEZWZpbmVkU2VnbWVudE1hdGNoZXJEYXRhIjp7InNlZ21lbnROYW1lIjoibWF1ci0yIn19XX0sInBhcnRpdGlvbnMiOlt7InRyZWF0bWVudCI6Im9uIiwic2l6ZSI6MH0seyJ0cmVhdG1lbnQiOiJvZmYiLCJzaXplIjoxMDB9LHsidHJlYXRtZW50IjoiVjQiLCJzaXplIjowfSx7InRyZWF0bWVudCI6InY1Iiwic2l6ZSI6MH1dLCJsYWJlbCI6ImluIHNlZ21lbnQgbWF1ci0yIn0seyJjb25kaXRpb25UeXBlIjoiUk9MTE9VVCIsIm1hdGNoZXJHcm91cCI6eyJjb21iaW5lciI6IkFORCIsIm1hdGNoZXJzIjpbeyJrZXlTZWxlY3RvciI6eyJ0cmFmZmljVHlwZSI6InVzZXIifSwibWF0Y2hlclR5cGUiOiJBTExfS0VZUyIsIm5lZ2F0ZSI6ZmFsc2V9XX0sInBhcnRpdGlvbnMiOlt7InRyZWF0bWVudCI6Im9uIiwic2l6ZSI6MH0seyJ0cmVhdG1lbnQiOiJvZmYiLCJzaXplIjoxMDB9LHsidHJlYXRtZW50IjoiVjQiLCJzaXplIjowfSx7InRyZWF0bWVudCI6InY1Iiwic2l6ZSI6MH1dLCJsYWJlbCI6ImRlZmF1bHQgcnVsZSJ9XX0=\\\"}\"}"; + NotificationParserImp notificationParserImp = new NotificationParserImp(); + + CommonChangeNotification incomingNotification = (CommonChangeNotification) notificationParserImp.parseMessage(payload); + Split split = (Split) incomingNotification.getDefinition(); + Assert.assertEquals("mauro_java", split.name); + Assert.assertEquals(1684329854385L, split.changeNumber); + Assert.assertEquals(CompressType.NOT_COMPRESSED, incomingNotification.getCompressType()); + Assert.assertEquals(0, incomingNotification.getPreviousChangeNumber()); + } + + @Test + public void validateCompressTypeIncorrect() throws EventParsingException { + String payload = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":3,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + NotificationParserImp notificationParserImp = new NotificationParserImp(); + + CommonChangeNotification incomingNotification = (CommonChangeNotification) notificationParserImp.parseMessage(payload); + Assert.assertNull(incomingNotification.getCompressType()); + Assert.assertEquals(0, incomingNotification.getPreviousChangeNumber()); + } + + @Test + public void validateCompressTypeNull() throws EventParsingException { + String payload = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + NotificationParserImp notificationParserImp = new NotificationParserImp(); + + CommonChangeNotification incomingNotification = (CommonChangeNotification) notificationParserImp.parseMessage(payload); + Assert.assertNull(incomingNotification.getCompressType()); + Assert.assertEquals(0, incomingNotification.getPreviousChangeNumber()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/NotificationParserTest.java b/client/src/test/java/io/split/engine/sse/NotificationParserTest.java index 1bbf355a..ad0b4075 100644 --- a/client/src/test/java/io/split/engine/sse/NotificationParserTest.java +++ b/client/src/test/java/io/split/engine/sse/NotificationParserTest.java @@ -5,7 +5,7 @@ import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; public class NotificationParserTest { private NotificationParser notificationParser; @@ -22,7 +22,7 @@ public void parseSplitUpdateShouldReturnParsedEvent() throws EventParsingExcepti IncomingNotification result = notificationParser.parseMessage(payload); assertEquals(IncomingNotification.Type.SPLIT_UPDATE, result.getType()); assertEquals("xxxx_xxxx_splits", result.getChannel()); - assertEquals(1592590435115L, ((SplitChangeNotification) result).getChangeNumber()); + assertEquals(1592590435115L, ((CommonChangeNotification) result).getChangeNumber()); } @Test @@ -149,4 +149,4 @@ public void parseControlStreamingDisabledShouldReturnParsedEvent() throws EventP assertEquals("control_pri", result.getChannel()); assertEquals(ControlType.STREAMING_DISABLED, ((ControlNotification)result).getControlType()); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/NotificationProcessorTest.java b/client/src/test/java/io/split/engine/sse/NotificationProcessorTest.java index 4bbbaab7..ea75ecb1 100644 --- a/client/src/test/java/io/split/engine/sse/NotificationProcessorTest.java +++ b/client/src/test/java/io/split/engine/sse/NotificationProcessorTest.java @@ -1,38 +1,59 @@ package io.split.engine.sse; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; import io.split.engine.sse.dtos.*; import io.split.engine.sse.workers.SegmentsWorkerImp; -import io.split.engine.sse.workers.SplitsWorker; +import io.split.engine.sse.workers.FeatureFlagsWorker; import io.split.engine.sse.workers.Worker; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; public class NotificationProcessorTest { - private SplitsWorker _splitsWorker; + private FeatureFlagsWorker _featureFlagsWorker; private Worker _segmentWorker; private NotificationProcessor _notificationProcessor; private PushStatusTracker _pushStatusTracker; @Before public void setUp() { - _splitsWorker = Mockito.mock(SplitsWorker.class); + _featureFlagsWorker = Mockito.mock(FeatureFlagsWorker.class); _segmentWorker = Mockito.mock(SegmentsWorkerImp.class); _pushStatusTracker = Mockito.mock(PushStatusTracker.class); - _notificationProcessor = new NotificationProcessorImp(_splitsWorker, _segmentWorker, _pushStatusTracker); + _notificationProcessor = new NotificationProcessorImp(_featureFlagsWorker, _segmentWorker, _pushStatusTracker); } @Test public void processSplitUpdateAddToQueueInWorker() { long changeNumber = 1585867723838L; String channel = "splits"; - GenericNotificationData genericNotificationData = new GenericNotificationData(changeNumber, null, null, null, null, null, null, channel); - SplitChangeNotification splitChangeNotification = new SplitChangeNotification(genericNotificationData); + GenericNotificationData genericNotificationData = GenericNotificationData.builder() + .changeNumber(changeNumber) + .channel(channel) + .build(); + CommonChangeNotification splitChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); _notificationProcessor.process(splitChangeNotification); - Mockito.verify(_splitsWorker, Mockito.times(1)).addToQueue(splitChangeNotification.getChangeNumber()); + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).addToQueue(Mockito.anyObject()); + } + + @Test + public void processRuleBasedSegmentUpdateAddToQueueInWorker() { + long changeNumber = 1585867723838L; + String channel = "splits"; + GenericNotificationData genericNotificationData = GenericNotificationData.builder() + .changeNumber(changeNumber) + .channel(channel) + .type(IncomingNotification.Type.RB_SEGMENT_UPDATE) + .build(); + CommonChangeNotification ruleBasedSegmentChangeNotification = new CommonChangeNotification(genericNotificationData, RuleBasedSegment.class); + + _notificationProcessor.process(ruleBasedSegmentChangeNotification); + + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).addToQueue(Mockito.anyObject()); } @Test @@ -41,13 +62,18 @@ public void processSplitKillAndAddToQueueInWorker() { String defaultTreatment = "off"; String splitName = "test-split"; String channel = "splits"; - GenericNotificationData genericNotificationData = new GenericNotificationData(changeNumber, defaultTreatment, splitName, null, null, null, null, channel); + GenericNotificationData genericNotificationData = GenericNotificationData.builder() + .changeNumber(changeNumber) + .defaultTreatment(defaultTreatment) + .featureFlagName(splitName) + .channel(channel) + .build(); SplitKillNotification splitKillNotification = new SplitKillNotification(genericNotificationData); _notificationProcessor.process(splitKillNotification); - Mockito.verify(_splitsWorker, Mockito.times(1)).killSplit(splitKillNotification.getChangeNumber(), splitKillNotification.getSplitName(), splitKillNotification.getDefaultTreatment()); - Mockito.verify(_splitsWorker, Mockito.times(1)).addToQueue(splitKillNotification.getChangeNumber()); + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).kill(splitKillNotification); + Mockito.verify(_featureFlagsWorker, Mockito.times(1)).addToQueue(Mockito.anyObject()); } @Test @@ -55,7 +81,11 @@ public void processSegmentUpdateAddToQueueInWorker() { long changeNumber = 1585867723838L; String segmentName = "segment-test"; String channel = "segments"; - GenericNotificationData genericNotificationData = new GenericNotificationData(changeNumber, null, null, null, null, segmentName, null, channel); + GenericNotificationData genericNotificationData = GenericNotificationData.builder() + .changeNumber(changeNumber) + .segmentName(segmentName) + .channel(channel) + .build(); SegmentChangeNotification segmentChangeNotification = new SegmentChangeNotification(genericNotificationData); _notificationProcessor.process(segmentChangeNotification); @@ -75,11 +105,13 @@ public void processControlNotification() { @Test public void processOccupancyNotification() { - GenericNotificationData genericNotificationData = new GenericNotificationData(null, null, null, null, null, null, null, "control_pri"); + GenericNotificationData genericNotificationData = GenericNotificationData.builder() + .channel("control_pri") + .build(); OccupancyNotification occupancyNotification = new OccupancyNotification(genericNotificationData); _notificationProcessor.process(occupancyNotification); Mockito.verify(_pushStatusTracker, Mockito.times(1)).handleIncomingOccupancyEvent(Mockito.any(OccupancyNotification.class)); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/PushStatusTrackerTest.java b/client/src/test/java/io/split/engine/sse/PushStatusTrackerTest.java index 82d8fc55..2660f9e1 100644 --- a/client/src/test/java/io/split/engine/sse/PushStatusTrackerTest.java +++ b/client/src/test/java/io/split/engine/sse/PushStatusTrackerTest.java @@ -1,105 +1,156 @@ package io.split.engine.sse; import io.split.engine.common.PushManager; +import io.split.engine.sse.client.SSEClient; import io.split.engine.sse.dtos.*; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; import org.junit.Test; +import org.mockito.Mockito; import java.util.concurrent.LinkedBlockingQueue; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - public class PushStatusTrackerTest { + private static final String CONTROL_PRI = "control_pri"; + private static final String CONTROL_SEC = "control_sec"; @Test public void HandleControlEventStreamingPausedShouldNotifyEvent() { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); ControlNotification controlNotification = buildControlNotification(ControlType.STREAMING_PAUSED); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingControlEvent(controlNotification); - assertThat(messages.size(), is(equalTo(1))); - assertThat(messages.peek(), is(equalTo(PushManager.Status.STREAMING_DOWN))); + Assert.assertEquals(1, messages.size()); + Assert.assertEquals(PushManager.Status.STREAMING_DOWN, messages.peek()); } @Test public void HandleControlEventStreamingResumedShouldNotifyEvent() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingControlEvent(buildControlNotification(ControlType.STREAMING_PAUSED)); pushStatusTracker.handleIncomingControlEvent(buildControlNotification(ControlType.STREAMING_RESUMED)); - assertThat(messages.size(), is(equalTo(2))); - assertThat(messages.take(), is(equalTo(PushManager.Status.STREAMING_DOWN))); - assertThat(messages.take(), is(equalTo(PushManager.Status.STREAMING_READY))); + Assert.assertEquals(2, messages.size()); + Assert.assertEquals(PushManager.Status.STREAMING_DOWN, messages.take()); + Assert.assertEquals(PushManager.Status.STREAMING_READY, messages.take()); } @Test public void HandleControlEventStreamingResumedShouldNotNotifyEvent() { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); - OccupancyNotification occupancyNotification = buildOccupancyNotification(0, null); + OccupancyNotification occupancyNotification = buildOccupancyNotification(0, CONTROL_PRI); ControlNotification controlNotification = buildControlNotification(ControlType.STREAMING_RESUMED); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingOccupancyEvent(occupancyNotification); pushStatusTracker.handleIncomingControlEvent(controlNotification); pushStatusTracker.handleIncomingControlEvent(controlNotification); - assertThat(messages.size(), is(equalTo(1))); - assertThat(messages.peek(), is(equalTo(PushManager.Status.STREAMING_DOWN))); + Assert.assertEquals(1, messages.size()); + Assert.assertEquals(PushManager.Status.STREAMING_DOWN, messages.peek()); + Assert.assertEquals(1, telemetryStorage.popStreamingEvents().size()); } @Test public void HandleControlEventStreamingDisabledShouldNotifyShutdownEvent() { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); ControlNotification controlNotification = buildControlNotification(ControlType.STREAMING_DISABLED); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingControlEvent(controlNotification); pushStatusTracker.handleIncomingControlEvent(controlNotification); - assertThat(messages.size(), is(equalTo(1))); - assertThat(messages.peek(), is(equalTo(PushManager.Status.STREAMING_OFF))); + Assert.assertEquals(1, messages.size()); + Assert.assertEquals(PushManager.Status.STREAMING_OFF, messages.peek()); } @Test public void HandleOccupancyEventWithPublishersFirstTimeShouldNotNotifyEvent() { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); - OccupancyNotification occupancyNotification = buildOccupancyNotification(2, null); + OccupancyNotification occupancyNotification = buildOccupancyNotification(2, CONTROL_SEC); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingOccupancyEvent(occupancyNotification); - assertThat(messages.size(), is(equalTo(0))); + Assert.assertEquals(0, messages.size()); + Assert.assertEquals(1, telemetryStorage.popStreamingEvents().size()); } @Test public void HandleOccupancyEventWithPublishersAndWithStreamingDisabledShouldNotifyEvent() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingOccupancyEvent(buildOccupancyNotification(0, null)); pushStatusTracker.handleIncomingOccupancyEvent(buildOccupancyNotification(2, null)); - assertThat(messages.size(), is(equalTo(2))); + Assert.assertEquals(2, messages.size()); PushManager.Status m1 = messages.take(); - assertThat(m1, is(equalTo(PushManager.Status.STREAMING_DOWN))); + Assert.assertEquals(PushManager.Status.STREAMING_DOWN, m1); PushManager.Status m2 = messages.take(); - assertThat(m2, is(equalTo(PushManager.Status.STREAMING_READY))); + Assert.assertEquals(PushManager.Status.STREAMING_READY, m2); } @Test public void HandleOccupancyEventWithDifferentChannelsPublishersShouldNotifyEvent() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); - PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); pushStatusTracker.handleIncomingOccupancyEvent(buildOccupancyNotification(0, "control_pri")); pushStatusTracker.handleIncomingOccupancyEvent(buildOccupancyNotification(2, "control_sec")); - assertThat(messages.size(), is(equalTo(2))); + Assert.assertEquals(2, messages.size()); + PushManager.Status m1 = messages.take(); + Assert.assertEquals(PushManager.Status.STREAMING_DOWN, m1); + + PushManager.Status m2 = messages.take(); + Assert.assertEquals(PushManager.Status.STREAMING_READY, m2); + } + + @Test + public void handleSSESTatusRecordTelemetryStreamingEvent() { + TelemetryStorage telemetryStorage = Mockito.mock(InMemoryTelemetryStorage.class); + LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); + pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.CONNECTED); + pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.FIRST_EVENT); + + Mockito.verify(telemetryStorage, Mockito.times(1)).recordStreamingEvents(Mockito.any()); + } + + @Test + public void handleAblyErrorRecordTelemetryStreamingEvent() { + TelemetryStorage telemetryStorage = Mockito.mock(InMemoryTelemetryStorage.class); + LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); + pushStatusTracker.handleIncomingAblyError(new ErrorNotification("Error", "Ably error", 401)); + + Mockito.verify(telemetryStorage, Mockito.times(1)).recordStreamingEvents(Mockito.any()); + + } + + @Test + public void HandleTwoRetryableErrorInARow() throws InterruptedException { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + LinkedBlockingQueue messages = new LinkedBlockingQueue<>(); + PushStatusTracker pushStatusTracker = new PushStatusTrackerImp(messages, telemetryStorage); + pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.CONNECTED); + pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); + pushStatusTracker.handleSseStatus(SSEClient.StatusMessage.RETRYABLE_ERROR); + + Assert.assertEquals(2, messages.size()); PushManager.Status m1 = messages.take(); - assertThat(m1, is(equalTo(PushManager.Status.STREAMING_DOWN))); + Assert.assertEquals(PushManager.Status.STREAMING_BACKOFF, m1); PushManager.Status m2 = messages.take(); - assertThat(m2, is(equalTo(PushManager.Status.STREAMING_READY))); + Assert.assertEquals(PushManager.Status.STREAMING_BACKOFF, m2); } private ControlNotification buildControlNotification(ControlType controlType) { @@ -111,14 +162,11 @@ private OccupancyNotification buildOccupancyNotification(int publishers, String } private GenericNotificationData buildGenericData(ControlType controlType, IncomingNotification.Type type, Integer publishers, String channel) { - return new GenericNotificationData( - null, - null, - null, - controlType, - publishers != null ? new OccupancyMetrics(publishers) : null, - null, - type, - channel == null ? "channel-test" : channel); + return GenericNotificationData.builder() + .controlType(controlType) + .metrics(publishers != null ? new OccupancyMetrics(publishers) : null) + .type(type) + .channel(channel == null ? "channel-test" : channel) + .build(); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/SSEClientTest.java b/client/src/test/java/io/split/engine/sse/SSEClientTest.java index 37cec722..0f6669cc 100644 --- a/client/src/test/java/io/split/engine/sse/SSEClientTest.java +++ b/client/src/test/java/io/split/engine/sse/SSEClientTest.java @@ -1,20 +1,185 @@ package io.split.engine.sse; +import io.split.client.RequestDecorator; +import io.split.engine.sse.client.RawEvent; import io.split.engine.sse.client.SSEClient; +import io.split.engine.sse.client.SSEClient.StatusMessage; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.enums.StreamEventsEnum; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; +import org.apache.hc.core5.http.message.BasicClassicHttpResponse; import org.apache.hc.core5.net.URIBuilder; import org.apache.hc.core5.util.Timeout; +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.net.URISyntaxException; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; public class SSEClientTest { + private static final long WAIT_MILLIS = 5000; + private static final String SSE_MESSAGE = "id: 1\nevent: message\ndata: {}\n\n"; + private static final URI STUB_URI = URI.create("http://localhost/sse"); + + @Test(timeout = 15000) + public void closeBeforeOpenThenDroppedConnectionReportsRetryableErrorTest() throws Exception { + TelemetryRuntimeProducer telemetry = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue statuses = new LinkedBlockingQueue<>(); + BlockingQueue events = new LinkedBlockingQueue<>(); + try (ServerSocket server = new ServerSocket(0); + CloseableHttpClient httpClient = HttpClients.custom().build()) { + startServer(server, SSEClientTest::respondThenDrop); + SSEClient sse = newClient(httpClient, telemetry, statuses, events); + try { + sse.close(); + sse.open(uri(server)); + + Assert.assertEquals(StatusMessage.INITIALIZATION_IN_PROGRESS, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.CONNECTED, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.RETRYABLE_ERROR, nextStatus(statuses)); + // The loop ends on its own here, so awaitClosed means it has returned and all telemetry is recorded. + awaitClosed(sse); + verifyConnectionErrorEvents(telemetry, + StreamEventsEnum.SseConnectionErrorValues.NON_REQUESTED_CONNECTION_ERROR); + } finally { + sse.close(); + } + } + } + + /** + * The stream blocks in read() after serving one complete message. The test only calls close() once the stream + * reports the reader is parked in that read(), so the connection loop has already passed its isOpen() check. + * close() then always takes the abort path (SocketException "Socket closed") and the forced stop is reported. + */ + @Test(timeout = 15000) + public void closeMidStreamReportsForcedStopWithRequestedTelemetryTest() throws Exception { + TelemetryRuntimeProducer telemetry = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue statuses = new LinkedBlockingQueue<>(); + BlockingQueue events = new LinkedBlockingQueue<>(); + ScriptedStream stream = new ScriptedStream(SSE_MESSAGE, true, true); + SSEClient sse = newClient(mockClient(streamingResponse(stream)), telemetry, statuses, events); + try { + Assert.assertTrue(sse.open(STUB_URI)); + Assert.assertEquals(StatusMessage.INITIALIZATION_IN_PROGRESS, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.CONNECTED, nextStatus(statuses)); + awaitStreamingEvent(events); + Assert.assertTrue("reader should be blocked in read() before closing", stream.awaitBlocked()); + + sse.close(); + + Assert.assertEquals(StatusMessage.FORCED_STOP, nextStatus(statuses)); + stream.awaitReaderFinished(); + verifyConnectionErrorEvents(telemetry, + StreamEventsEnum.SseConnectionErrorValues.REQUESTED_CONNECTION_ERROR); + } finally { + sse.close(); + stream.abort(); + } + } + + /** + * Same synchronisation as closeMidStreamReportsForcedStopWithRequestedTelemetryTest: the first connection is + * only closed once its reader is parked in read(). The second connection ends on the server side and must + * report a retryable error, not inherit the forced stop. + */ + @Test(timeout = 15000) + public void forcedStopIsResetBetweenConnectionsTest() throws Exception { + TelemetryRuntimeProducer telemetry = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue statuses = new LinkedBlockingQueue<>(); + BlockingQueue events = new LinkedBlockingQueue<>(); + ScriptedStream first = new ScriptedStream(SSE_MESSAGE, true, true); + ScriptedStream second = new ScriptedStream("", false, false); + SSEClient sse = newClient(mockClient(streamingResponse(first), streamingResponse(second)), + telemetry, statuses, events); + try { + // First connection is stopped by the client. + Assert.assertTrue(sse.open(STUB_URI)); + Assert.assertEquals(StatusMessage.INITIALIZATION_IN_PROGRESS, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.CONNECTED, nextStatus(statuses)); + awaitStreamingEvent(events); + Assert.assertTrue("reader should be blocked in read() before closing", first.awaitBlocked()); + sse.close(); + Assert.assertEquals(StatusMessage.FORCED_STOP, nextStatus(statuses)); + // Wait for the first connection's second response close; the reopen is safe because SSEClient runs + // connectAndLoop on a single-thread executor, so the second connection starts after the first loop returns. + first.awaitReaderFinished(); + + // Second connection is ended by the server: must not inherit the forced stop. + sse.open(STUB_URI); + Assert.assertEquals(StatusMessage.INITIALIZATION_IN_PROGRESS, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.CONNECTED, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.RETRYABLE_ERROR, nextStatus(statuses)); + awaitClosed(sse); + verifyConnectionErrorEvents(telemetry, + StreamEventsEnum.SseConnectionErrorValues.REQUESTED_CONNECTION_ERROR, + StreamEventsEnum.SseConnectionErrorValues.NON_REQUESTED_CONNECTION_ERROR); + } finally { + sse.close(); + first.abort(); + second.abort(); + } + } + + /** + * A socket closed by close() surfaces as SocketException("Socket closed"), which takes a different branch + * than a generic IOException. This test stubs the response so the stream fails with a plain IOException + * once the client is closed, covering the forced-stop IOException branch. + */ + @Test(timeout = 15000) + public void closeMidReadWithIOExceptionReportsForcedStopTest() throws Exception { + TelemetryRuntimeProducer telemetry = Mockito.mock(TelemetryRuntimeProducer.class); + BlockingQueue statuses = new LinkedBlockingQueue<>(); + BlockingQueue events = new LinkedBlockingQueue<>(); + ScriptedStream stream = new ScriptedStream("", true, false); + SSEClient sse = newClient(mockClient(streamingResponse(stream)), telemetry, statuses, events); + try { + Assert.assertTrue(sse.open(STUB_URI)); + Assert.assertEquals(StatusMessage.INITIALIZATION_IN_PROGRESS, nextStatus(statuses)); + Assert.assertEquals(StatusMessage.CONNECTED, nextStatus(statuses)); + Assert.assertTrue("reader should be blocked in read() before closing", stream.awaitBlocked()); + + sse.close(); + + Assert.assertEquals(StatusMessage.FORCED_STOP, nextStatus(statuses)); + stream.awaitReaderFinished(); + verifyConnectionErrorEvents(telemetry, + StreamEventsEnum.SseConnectionErrorValues.REQUESTED_CONNECTION_ERROR); + // The forced stop is reported exactly once: nothing follows the loop end. + Assert.assertNull("no status should follow FORCED_STOP", statuses.poll()); + } finally { + sse.close(); + stream.abort(); + } + } + @Test @Ignore public void basicUsageTest() throws URISyntaxException, InterruptedException { @@ -23,6 +188,7 @@ public void basicUsageTest() throws URISyntaxException, InterruptedException { .addParameter("v", "1,1") .addParameter("channels", "[?occupancy=metrics.publishers]control_pri") .build(); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(Timeout.ofMilliseconds(70000)) @@ -33,11 +199,196 @@ public void basicUsageTest() throws URISyntaxException, InterruptedException { CloseableHttpClient httpClient = httpClientbuilder.build(); - SSEClient sse = new SSEClient(e -> { System.out.println(e); return null; }, - s -> { System.out.println(s); return null; }, httpClient); + SSEClient sse = new SSEClient(e -> null, + s -> null, httpClient, telemetryRuntimeProducer, null, new RequestDecorator(null)); sse.open(uri); Thread.sleep(5000); sse.close(); Thread.sleep(100000); } + + /** + * Scripted response body. It serves {@code data}, then either ends the stream or blocks in read() until the + * response is closed. {@link #awaitBlocked()} returns only once the reader is parked in that blocking read, + * which means it has consumed the data and already passed the connection loop's isOpen() check. + */ + private static final class ScriptedStream extends InputStream { + private final byte[] _data; + private final boolean _holdOpen; + private final boolean _socketClosedOnAbort; + private final CountDownLatch _blocked = new CountDownLatch(1); + private final CountDownLatch _aborted = new CountDownLatch(1); + // close() closes the response once and the connection loop's finally closes it again. + private final CountDownLatch _closes = new CountDownLatch(2); + private int _pos; + + ScriptedStream(String data, boolean holdOpen, boolean socketClosedOnAbort) { + _data = data.getBytes(StandardCharsets.UTF_8); + _holdOpen = holdOpen; + _socketClosedOnAbort = socketClosedOnAbort; + } + + @Override + public int read() throws IOException { + byte[] one = new byte[1]; + int n = read(one, 0, 1); + return n < 0 ? -1 : one[0] & 0xFF; + } + + @Override + public int read(byte[] buffer, int off, int len) throws IOException { + if (_pos < _data.length) { + int n = Math.min(len, _data.length - _pos); + System.arraycopy(_data, _pos, buffer, off, n); + _pos += n; + return n; + } + if (!_holdOpen) { + return -1; + } + _blocked.countDown(); + try { + _aborted.await(WAIT_MILLIS, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + if (_socketClosedOnAbort) { + throw new SocketException("Socket closed"); + } + throw new IOException("Stream aborted"); + } + + boolean awaitBlocked() throws InterruptedException { + return _blocked.await(WAIT_MILLIS, TimeUnit.MILLISECONDS); + } + + /** + * Waits for the second response close, which is the first line of the finally block in + * SSEClient.connectAndLoop. That runs before _state is set to CLOSED and _forcedStop is reset, so it does not + * mean the loop has fully returned. Reopening after it is safe only because SSEClient runs connectAndLoop on a + * single-thread executor, which starts the next connection after the previous loop has returned. + */ + void awaitReaderFinished() throws InterruptedException { + Assert.assertTrue("connection loop should finish after the forced stop", + _closes.await(WAIT_MILLIS, TimeUnit.MILLISECONDS)); + } + + void onResponseClosed() { + _aborted.countDown(); + _closes.countDown(); + } + + void abort() { + _aborted.countDown(); + } + } + + private static CloseableHttpResponse streamingResponse(ScriptedStream stream) { + BasicClassicHttpResponse classicResponse = new BasicClassicHttpResponse(200); + classicResponse.setEntity(new InputStreamEntity(stream, -1, ContentType.TEXT_PLAIN)); + return CloseableHttpResponse.create(classicResponse, (closeable, closeMode) -> stream.onResponseClosed()); + } + + private static CloseableHttpClient mockClient(CloseableHttpResponse first, CloseableHttpResponse... next) + throws IOException { + CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); + Mockito.when(httpClient.execute(Mockito.any(ClassicHttpRequest.class))).thenReturn(first, next); + return httpClient; + } + + private static SSEClient newClient(CloseableHttpClient httpClient, TelemetryRuntimeProducer telemetry, + BlockingQueue statuses, BlockingQueue events) { + return new SSEClient(e -> { + events.add(e); + return null; + }, s -> { + statuses.add(s); + return null; + }, httpClient, telemetry, null, new RequestDecorator(null)); + } + + private static URI uri(ServerSocket server) { + return URI.create("http://localhost:" + server.getLocalPort() + "/sse"); + } + + private static StatusMessage nextStatus(BlockingQueue statuses) throws InterruptedException { + StatusMessage status = statuses.poll(WAIT_MILLIS, TimeUnit.MILLISECONDS); + Assert.assertNotNull("expected a status callback within " + WAIT_MILLIS + " ms", status); + return status; + } + + /** Waits until the reader has delivered the server message to the event callback. */ + private static void awaitStreamingEvent(BlockingQueue events) throws InterruptedException { + Assert.assertNotNull("expected the server event within " + WAIT_MILLIS + " ms", + events.poll(WAIT_MILLIS, TimeUnit.MILLISECONDS)); + } + + /** + * Exact check that the expected connection-error telemetry was recorded and nothing else was. Call it only after + * the loop has finished recording telemetry (awaitClosed for loops that end on their own, + * ScriptedStream.awaitReaderFinished when the test closed the client; the latter fires at the start of the + * finally block, after the last recordStreamingEvents call). A duplicate or late call then fails here instead + * of being missed by a timed wait. + */ + private static void verifyConnectionErrorEvents(TelemetryRuntimeProducer telemetry, + StreamEventsEnum.SseConnectionErrorValues... expected) { + ArgumentCaptor captor = ArgumentCaptor.forClass(StreamingEvent.class); + Mockito.verify(telemetry, Mockito.times(expected.length)).recordStreamingEvents(captor.capture()); + Mockito.verifyNoMoreInteractions(telemetry); + List recorded = captor.getAllValues(); + for (int i = 0; i < expected.length; i++) { + Assert.assertEquals(StreamEventsEnum.SSE_CONNECTION_ERROR.getType(), recorded.get(i).getType()); + Assert.assertEquals(expected[i].getValue(), recorded.get(i).getData()); + } + } + + /** + * Waits for isOpen() to become false. Only reliable when the connection loop ends by itself, since close() + * sets the state to closed before the loop has actually returned. + */ + private static void awaitClosed(SSEClient sse) throws InterruptedException { + long deadline = System.currentTimeMillis() + WAIT_MILLIS; + while (sse.isOpen() && System.currentTimeMillis() < deadline) { + Thread.sleep(10); + } + Assert.assertFalse("SSE client should be closed once the connection loop ends", sse.isOpen()); + } + + private interface SocketBehavior { + void run(Socket socket) throws Exception; + } + + private static void startServer(ServerSocket server, SocketBehavior... behaviors) { + Thread serverThread = new Thread(() -> { + for (SocketBehavior behavior : behaviors) { + try (Socket socket = server.accept()) { + behavior.run(socket); + } catch (Exception ignored) { + } + } + }); + serverThread.setDaemon(true); + serverThread.start(); + } + + private static void respondThenDrop(Socket socket) throws IOException { + readRequestHead(socket); + writeStreamHeaders(socket); + } + + private static void readRequestHead(Socket socket) throws IOException { + BufferedReader reader = new BufferedReader( + new InputStreamReader(socket.getInputStream(), StandardCharsets.US_ASCII)); + String line; + do { + line = reader.readLine(); + } while (line != null && !line.isEmpty()); + } + + private static void writeStreamHeaders(Socket socket) throws IOException { + OutputStream out = socket.getOutputStream(); + out.write(("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n" + + "Connection: close\r\n\r\n").getBytes(StandardCharsets.UTF_8)); + out.flush(); + } } diff --git a/client/src/test/java/io/split/engine/sse/utils/DecompressionUtilTest.java b/client/src/test/java/io/split/engine/sse/utils/DecompressionUtilTest.java new file mode 100644 index 00000000..bc62b431 --- /dev/null +++ b/client/src/test/java/io/split/engine/sse/utils/DecompressionUtilTest.java @@ -0,0 +1,36 @@ +package io.split.engine.sse.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Base64; +import java.util.zip.DataFormatException; + +import static io.split.engine.sse.utils.DecompressionUtil.gZipDecompress; +import static io.split.engine.sse.utils.DecompressionUtil.zLibDecompress; + +public class DecompressionUtilTest { + + + @Test + public void testZLibDecompress() throws UnsupportedEncodingException, DataFormatException { + String toDecode = "eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU="; + + byte[] decodedBytes = Base64.getDecoder().decode(toDecode); + byte[] decompressFeatureFlag = zLibDecompress(decodedBytes); + String featureFlag = new String(decompressFeatureFlag, 0, decompressFeatureFlag.length, "UTF-8"); + Assert.assertEquals("{\"trafficTypeName\":\"user\",\"id\":\"d431cdd0-b0be-11ea-8a80-1660ada9ce39\",\"name\":\"mauro_java\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-92391491,\"seed\":-1769377604,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1684265694505,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"WHITELIST\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"matcherType\":\"WHITELIST\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"admin\",\"mauro\",\"nico\"]}}]},\"partitions\":[{\"treatment\":\"v5\",\"size\":100}],\"label\":\"whitelisted\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"maur-2\"}}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"in segment maur-2\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"default rule\"}]}", featureFlag); + } + + @Test + public void testGZipDecompress() throws IOException { + String toDecode = "H4sIAAAAAAAA/8yT327aTBDFXyU612vJxoTgvUMfKB8qcaSapqoihAZ7DNusvWi9TpUiv3tl/pdQVb1qL+cwc3bOj/EGzlKeq3T6tuaYCoZEXbGFgMogkXXDIM0y31v4C/aCgMnrU9/3gl7Pp4yilMMIAuVusqDamvlXeiWIg/FAa5OSU6aEDHz/ip4wZ5Be1AmjoBsFAtVOCO56UXh31/O7ApUjV1eQGPw3HT+NIPCitG7bctIVC2ScU63d1DK5gksHCZPnEEhXVC45rosFW8ig1++GYej3g85tJEB6aSA7Aqkpc7Ws7XahCnLTbLVM7evnzalsUUHi8//j6WgyTqYQKMilK7b31tRryLa3WKiyfRCDeHhq2Dntiys+JS/J8THUt5VyrFXlHnYTQ3LU2h91yGdQVqhy+0RtTeuhUoNZ08wagTVZdxbBndF5vYVApb7z9m9pZgKaFqwhT+6coRHvg398nEweP/157Bd+S1hz6oxtm88O73B0jbhgM47nyej+YRRfgdNODDlXJWcJL9tUF5SqnRqfbtPr4LdcTHnk4rfp3buLOkG7+Pmp++vRM9w/wVblzX7Pm8OGfxf5YDKZfxh9SS6B/2Pc9t/7ja01o5k1PwIAAP//uTipVskEAAA="; + + byte[] decodedBytes = Base64.getDecoder().decode(toDecode); + byte[] decompressFeatureFlag = gZipDecompress(decodedBytes); + String featureFlag = new String(decompressFeatureFlag, 0, decompressFeatureFlag.length, "UTF-8"); + Assert.assertEquals("{\"trafficTypeName\":\"user\",\"id\":\"d431cdd0-b0be-11ea-8a80-1660ada9ce39\",\"name\":\"mauro_java\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-92391491,\"seed\":-1769377604,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1684333081259,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"WHITELIST\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"matcherType\":\"WHITELIST\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"admin\",\"mauro\",\"nico\"]}}]},\"partitions\":[{\"treatment\":\"v5\",\"size\":100}],\"label\":\"whitelisted\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"IN_SEGMENT\",\"negate\":false,\"userDefinedSegmentMatcherData\":{\"segmentName\":\"maur-2\"}}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"in segment maur-2\"},{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\"},\"matcherType\":\"ALL_KEYS\",\"negate\":false}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100},{\"treatment\":\"V4\",\"size\":0},{\"treatment\":\"v5\",\"size\":0}],\"label\":\"default rule\"}]}", featureFlag); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpEventsTest.java b/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpEventsTest.java new file mode 100644 index 00000000..5d214ff4 --- /dev/null +++ b/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpEventsTest.java @@ -0,0 +1,114 @@ +package io.split.engine.sse.workers; + +import io.split.client.api.SdkEventMetadata; +import io.split.client.api.SdkEventType; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.Split; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.lifecycle.SdkEventsNotifier; +import io.split.client.lifecycle.SdkInternalEvent; +import io.split.client.utils.Json; +import io.split.engine.common.Synchronizer; +import io.split.engine.common.SynchronizerImp; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.experiments.SplitParser; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.GenericNotificationData; +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.RawMessageNotification; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class FeatureFlagWorkerImpEventsTest { + + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); + + private static final String WITH_DEFINITION = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":2,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + + private static final String WITHOUT_DEFINITION = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505}\"}"; + + private static final String RBS_WITH_DEFINITION = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"RB_SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":0,\\\"d\\\":\\\"eyJjaGFuZ2VOdW1iZXIiOiA1LCAibmFtZSI6ICJzYW1wbGVfcnVsZV9iYXNlZF9zZWdtZW50IiwgInN0YXR1cyI6ICJBQ1RJVkUiLCAidHJhZmZpY1R5cGVOYW1lIjogInVzZXIiLCAiZXhjbHVkZWQiOiB7ImtleXMiOiBbIm1hdXJvQHNwbGl0LmlvIiwgImdhc3RvbkBzcGxpdC5pbyJdLCAic2VnbWVudHMiOiBbXX0sICJjb25kaXRpb25zIjogW3sibWF0Y2hlckdyb3VwIjogeyJjb21iaW5lciI6ICJBTkQiLCAibWF0Y2hlcnMiOiBbeyJrZXlTZWxlY3RvciI6IHsidHJhZmZpY1R5cGUiOiAidXNlciIsICJhdHRyaWJ1dGUiOiAiZW1haWwifSwgIm1hdGNoZXJUeXBlIjogIkVORFNfV0lUSCIsICJuZWdhdGUiOiBmYWxzZSwgIndoaXRlbGlzdE1hdGNoZXJEYXRhIjogeyJ3aGl0ZWxpc3QiOiBbIkBzcGxpdC5pbyJdfX1dfX1dfQ==\\\"}\"}"; + + private static final String RBS_WITHOUT_DEFINITION = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"RB_SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1684265694505}\"}"; + + private static final class Recorded { + final SdkInternalEvent event; + final SdkEventMetadata metadata; + + Recorded(SdkInternalEvent event, SdkEventMetadata metadata) { + this.event = event; + this.metadata = metadata; + } + } + + private static List refresh(String notification) { + return refresh(notification, 0L, Split.class); + } + + private static List refreshRuleBasedSegment(String notification, long cachedChangeNumber) { + return refresh(notification, cachedChangeNumber, RuleBasedSegment.class); + } + + private static List refresh(String notification, long rbsCachedChangeNumber, Class definitionType) { + List recorded = new ArrayList<>(); + SdkEventsNotifier notifier = (event, metadata) -> recorded.add(new Recorded(event, metadata)); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + Mockito.when(ruleBasedSegmentCache.getChangeNumber()).thenReturn(rbsCachedChangeNumber); + FeatureFlagWorkerImp worker = new FeatureFlagWorkerImp(synchronizer, new SplitParser(), new RuleBasedSegmentParser(), + splitCacheProducer, ruleBasedSegmentCache, new InMemoryTelemetryStorage(), FLAG_SETS_FILTER, notifier); + + RawMessageNotification raw = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData data = Json.fromJson(raw.getData(), GenericNotificationData.class); + worker.executeRefresh((IncomingNotification) new CommonChangeNotification(data, definitionType)); + return recorded; + } + + @Test + public void aFlagDefinitionPushedOverSseRaisesOneFlagsUpdated() { + List recorded = refresh(WITH_DEFINITION); + + assertEquals(1, recorded.size()); + assertEquals(SdkInternalEvent.FLAGS_UPDATED, recorded.get(0).event); + assertEquals(SdkEventType.FLAGS_UPDATE, recorded.get(0).metadata.getType()); + assertEquals(1, recorded.get(0).metadata.getNames().size()); + } + + @Test + public void aNotificationWithoutDefinitionRaisesNothingItself() { + // The worker falls back to refreshSplits(); the fetcher is then the one that emits. + assertTrue(refresh(WITHOUT_DEFINITION).isEmpty()); + } + + @Test + public void aRuleBasedSegmentDefinitionPushedOverSseRaisesOneRuleBasedSegmentsUpdated() { + List recorded = refreshRuleBasedSegment(RBS_WITH_DEFINITION, 0L); + + assertEquals(1, recorded.size()); + assertEquals(SdkInternalEvent.RULE_BASED_SEGMENTS_UPDATED, recorded.get(0).event); + assertEquals(SdkEventType.SEGMENTS_UPDATE, recorded.get(0).metadata.getType()); + assertTrue(recorded.get(0).metadata.getNames().isEmpty()); + } + + @Test + public void aRuleBasedSegmentNotificationWithoutDefinitionRaisesNothingItself() { + assertTrue(refreshRuleBasedSegment(RBS_WITHOUT_DEFINITION, 0L).isEmpty()); + } + + @Test + public void aStaleRuleBasedSegmentNotificationRaisesNothing() { + assertTrue(refreshRuleBasedSegment(RBS_WITH_DEFINITION, Long.MAX_VALUE).isEmpty()); + } +} diff --git a/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpTest.java b/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpTest.java new file mode 100644 index 00000000..1f7c9a8c --- /dev/null +++ b/client/src/test/java/io/split/engine/sse/workers/FeatureFlagWorkerImpTest.java @@ -0,0 +1,154 @@ +package io.split.engine.sse.workers; + +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.dtos.MatcherCombiner; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.client.utils.Json; +import io.split.engine.common.Synchronizer; +import io.split.engine.common.SynchronizerImp; +import io.split.engine.experiments.ParsedCondition; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.experiments.SplitParser; +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.RawMessageNotification; +import io.split.engine.sse.dtos.GenericNotificationData; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SplitCacheProducer; +import io.split.storages.memory.InMemoryCacheImp; +import io.split.telemetry.domain.UpdatesFromSSE; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; + +import static org.mockito.Mockito.when; + +public class FeatureFlagWorkerImpTest { + + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); + + @Test + public void testRefreshSplitsWithCorrectFF() { + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryStorage telemetryRuntimeProducer = new InMemoryTelemetryStorage(); + FeatureFlagWorkerImp featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":2,\\\"d\\\":\\\"eJzMk99u2kwQxV8lOtdryQZj8N6hD5QPlThSTVNVEUKDPYZt1jZar1OlyO9emf8lVFWv2ss5zJyd82O8hTWUZSqZvW04opwhUVdsIKBSSKR+10vS1HWW7pIdz2NyBjRwHS8IXEopTLgbQqDYT+ZUm3LxlV4J4mg81LpMyKqygPRc94YeM6eQTtjphp4fegLVXvD6Qdjt9wPXF6gs2bqCxPC/2eRpDIEXpXXblpGuWCDljGptZ4bJ5lxYSJRZBoFkTcWKozpfsoH0goHfCXpB6PfcngDpVQnZEUjKIlOr2uwWqiC3zU5L1aF+3p7LFhUkPv8/mY2nk3gGgZxssmZzb8p6A9n25ktVtA9iGI3ODXunQ3HDp+AVWT6F+rZWlrWq7MN+YkSWWvuTDvkMSnNV7J6oTdl6qKTEvGnmjcCGjL2IYC/ovPYgUKnvvPtbmrmApiVryLM7p2jE++AfH6fTx09/HvuF32LWnNjStM0Xh3c8ukZcsZlEi3h8/zCObsBpJ0acqYLTmFdtqitK1V6NzrfpdPBbLmVx4uK26e27izpDu/r5yf/16AXun2Cr4u6w591xw7+LfDidLj6Mv8TXwP8xbofv/c7UmtHMmx8BAAD//0fclvU=\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification featureFlagChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); + featureFlagsWorker.executeRefresh(featureFlagChangeNotification); + UpdatesFromSSE updatesFromSSE = telemetryRuntimeProducer.popUpdatesFromSSE(); + Assert.assertEquals(1, updatesFromSSE.getSplits()); + Mockito.verify(synchronizer, Mockito.times(0)).refreshSplits(1684265694505L, 0L); + Mockito.verify(synchronizer, Mockito.times(1)).forceRefreshSegment(Mockito.anyString()); + } + + @Test + public void testRefreshSplitsWithEmptyData() { + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryStorage telemetryRuntimeProducer = new InMemoryTelemetryStorage(); + FeatureFlagWorkerImp featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification featureFlagChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); + featureFlagsWorker.executeRefresh(featureFlagChangeNotification); + UpdatesFromSSE updatesFromSSE = telemetryRuntimeProducer.popUpdatesFromSSE(); + Assert.assertEquals(0, updatesFromSSE.getSplits()); + Mockito.verify(synchronizer, Mockito.times(1)).refreshSplits(1684265694505L, 0L); + Mockito.verify(synchronizer, Mockito.times(0)).forceRefreshSegment(Mockito.anyString()); + } + + @Test + public void testRefreshSplitsArchiveFF() { + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = new InMemoryCacheImp(1686165614090L, FLAG_SETS_FILTER); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryStorage telemetryRuntimeProducer = new InMemoryTelemetryStorage(); + FeatureFlagWorkerImp featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1686165617166,\\\"pcn\\\":1686165614090,\\\"c\\\":2,\\\"d\\\":\\\"eJxsUdFu4jAQ/JVqnx3JDjTh/JZCrj2JBh0EqtOBIuNswKqTIMeuxKH8+ykhiKrqiyXvzM7O7lzAGlEUSqbnEyaiRODgGjRAQOXAIQ/puPB96tHHIPQYQ/QmFNErxEgG44DKnI2AQHXtTOI0my6WcXZAmxoUtsTKvil7nNZVoQ5RYdFERh7VBwK5TY60rqWwqq6AM0q/qa8Qc+As/EHZ5HHMCDR9wQ/9kIajcEygscK6BjhEy+nLr008AwLvSuuOVgjdIIEcC+H03RZw2Hg/n88JEJBHUR0wceUeDXAWTAIWPAYsZEFAQOhDDdwnIPslnOk9NcAvNwEOly3IWtdmC3wLe+1wCy0Q2Hh/zNvTV9xg3sFtr5irQe3v5f7twgAOy8V8vlinQKAUVh7RPJvanbrBsi73qurMQpTM7oSrzjueV6hR2tp05E8J39MV1hq1d7YrWWxsZ2cQGYjzeLXK0pcoyRbLLP69juZZuuiyxoPo2oa7ukqYc+JKNEq+XgVmwopucC6sGMSS9etTvAQCH0I7BO7Ttt21BE7C2E8XsN+l06h/CJy25CveH/eGM0rbHQEt9qiHnR62jtKR7N/8wafQ7tr/AQAA//8S4fPB\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification featureFlagChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); + featureFlagsWorker.executeRefresh(featureFlagChangeNotification); + UpdatesFromSSE updatesFromSSE = telemetryRuntimeProducer.popUpdatesFromSSE(); + Assert.assertEquals(1, updatesFromSSE.getSplits()); + Mockito.verify(synchronizer, Mockito.times(0)).refreshSplits(1686165617166L, 0L); + Mockito.verify(synchronizer, Mockito.times(0)).forceRefreshSegment(Mockito.anyString()); + } + + @Test + public void testUpdateRuleBasedSegmentsWithCorrectFF() { + io.split.engine.matchers.Matcher matcher = (matchValue, bucketingKey, attributes, evaluationContext) -> false; + ParsedCondition parsedCondition = new ParsedCondition(ConditionType.ROLLOUT, + new CombiningMatcher(MatcherCombiner.AND, Arrays.asList(new AttributeMatcher("email", matcher, false))), + null, + "my label"); + ParsedRuleBasedSegment parsedRBS = new ParsedRuleBasedSegment("sample_rule_based_segment", + Arrays.asList(parsedCondition), + "user", + 5, + Arrays.asList("mauro@split.io","gaston@split.io"), + new ArrayList<>()); + + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryStorage telemetryRuntimeProducer = new InMemoryTelemetryStorage(); + FeatureFlagWorkerImp featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"RB_SEGMENT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":0,\\\"d\\\":\\\"eyJjaGFuZ2VOdW1iZXIiOiA1LCAibmFtZSI6ICJzYW1wbGVfcnVsZV9iYXNlZF9zZWdtZW50IiwgInN0YXR1cyI6ICJBQ1RJVkUiLCAidHJhZmZpY1R5cGVOYW1lIjogInVzZXIiLCAiZXhjbHVkZWQiOiB7ImtleXMiOiBbIm1hdXJvQHNwbGl0LmlvIiwgImdhc3RvbkBzcGxpdC5pbyJdLCAic2VnbWVudHMiOiBbXX0sICJjb25kaXRpb25zIjogW3sibWF0Y2hlckdyb3VwIjogeyJjb21iaW5lciI6ICJBTkQiLCAibWF0Y2hlcnMiOiBbeyJrZXlTZWxlY3RvciI6IHsidHJhZmZpY1R5cGUiOiAidXNlciIsICJhdHRyaWJ1dGUiOiAiZW1haWwifSwgIm1hdGNoZXJUeXBlIjogIkVORFNfV0lUSCIsICJuZWdhdGUiOiBmYWxzZSwgIndoaXRlbGlzdE1hdGNoZXJEYXRhIjogeyJ3aGl0ZWxpc3QiOiBbIkBzcGxpdC5pbyJdfX1dfX1dfQ==\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + + CommonChangeNotification ruleBasedSegmentChangeNotification = new CommonChangeNotification(genericNotificationData, RuleBasedSegment.class); + featureFlagsWorker.executeRefresh(ruleBasedSegmentChangeNotification); + Mockito.verify(ruleBasedSegmentCache, Mockito.times(1)).update(Arrays.asList(parsedRBS), new ArrayList<>(), 1684265694505L); + } + + @Test + public void testRefreshRuleBasedSegmentWithCorrectFF() { + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + Synchronizer synchronizer = Mockito.mock(SynchronizerImp.class); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + HashSet rbs = new HashSet<>(); + rbs.add("sample_rule_based_segment"); + when(ruleBasedSegmentCache.contains(rbs)).thenReturn(false); + TelemetryStorage telemetryRuntimeProducer = new InMemoryTelemetryStorage(); + FeatureFlagWorkerImp featureFlagsWorker = new FeatureFlagWorkerImp(synchronizer, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + String notification = "{\"id\":\"vQQ61wzBRO:0:0\",\"clientId\":\"pri:MTUxNzg3MDg1OQ==\",\"timestamp\":1684265694676,\"encoding\":\"json\",\"channel\":\"NzM2MDI5Mzc0_MjkyNTIzNjczMw==_splits\",\"data\":\"{\\\"type\\\":\\\"SPLIT_UPDATE\\\",\\\"changeNumber\\\":1684265694505,\\\"pcn\\\":0,\\\"c\\\":0,\\\"d\\\":\\\"eyJjaGFuZ2VOdW1iZXIiOiAxMCwgInRyYWZmaWNUeXBlTmFtZSI6ICJ1c2VyIiwgIm5hbWUiOiAicmJzX2ZsYWciLCAidHJhZmZpY0FsbG9jYXRpb24iOiAxMDAsICJ0cmFmZmljQWxsb2NhdGlvblNlZWQiOiAxODI4Mzc3MzgwLCAic2VlZCI6IC0yODY2MTc5MjEsICJzdGF0dXMiOiAiQUNUSVZFIiwgImtpbGxlZCI6IGZhbHNlLCAiZGVmYXVsdFRyZWF0bWVudCI6ICJvZmYiLCAiYWxnbyI6IDIsICJjb25kaXRpb25zIjogW3siY29uZGl0aW9uVHlwZSI6ICJST0xMT1VUIiwgIm1hdGNoZXJHcm91cCI6IHsiY29tYmluZXIiOiAiQU5EIiwgIm1hdGNoZXJzIjogW3sia2V5U2VsZWN0b3IiOiB7InRyYWZmaWNUeXBlIjogInVzZXIifSwgIm1hdGNoZXJUeXBlIjogIklOX1JVTEVfQkFTRURfU0VHTUVOVCIsICJuZWdhdGUiOiBmYWxzZSwgInVzZXJEZWZpbmVkU2VnbWVudE1hdGNoZXJEYXRhIjogeyJzZWdtZW50TmFtZSI6ICJzYW1wbGVfcnVsZV9iYXNlZF9zZWdtZW50In19XX0sICJwYXJ0aXRpb25zIjogW3sidHJlYXRtZW50IjogIm9uIiwgInNpemUiOiAxMDB9LCB7InRyZWF0bWVudCI6ICJvZmYiLCAic2l6ZSI6IDB9XSwgImxhYmVsIjogImluIHJ1bGUgYmFzZWQgc2VnbWVudCBzYW1wbGVfcnVsZV9iYXNlZF9zZWdtZW50In0sIHsiY29uZGl0aW9uVHlwZSI6ICJST0xMT1VUIiwgIm1hdGNoZXJHcm91cCI6IHsiY29tYmluZXIiOiAiQU5EIiwgIm1hdGNoZXJzIjogW3sia2V5U2VsZWN0b3IiOiB7InRyYWZmaWNUeXBlIjogInVzZXIifSwgIm1hdGNoZXJUeXBlIjogIkFMTF9LRVlTIiwgIm5lZ2F0ZSI6IGZhbHNlfV19LCAicGFydGl0aW9ucyI6IFt7InRyZWF0bWVudCI6ICJvbiIsICJzaXplIjogMH0sIHsidHJlYXRtZW50IjogIm9mZiIsICJzaXplIjogMTAwfV0sICJsYWJlbCI6ICJkZWZhdWx0IHJ1bGUifV0sICJjb25maWd1cmF0aW9ucyI6IHt9LCAic2V0cyI6IFtdLCAiaW1wcmVzc2lvbnNEaXNhYmxlZCI6IGZhbHNlfQ==\\\"}\"}"; + RawMessageNotification rawMessageNotification = Json.fromJson(notification, RawMessageNotification.class); + GenericNotificationData genericNotificationData = Json.fromJson(rawMessageNotification.getData(), GenericNotificationData.class); + CommonChangeNotification featureFlagChangeNotification = new CommonChangeNotification(genericNotificationData, Split.class); + + featureFlagsWorker.executeRefresh(featureFlagChangeNotification); + Mockito.verify(synchronizer, Mockito.times(0)).refreshSplits(0L, 1684265694505L); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/engine/sse/workers/SplitsWorkerTest.java b/client/src/test/java/io/split/engine/sse/workers/SplitsWorkerTest.java index 8db49054..7e63fa55 100644 --- a/client/src/test/java/io/split/engine/sse/workers/SplitsWorkerTest.java +++ b/client/src/test/java/io/split/engine/sse/workers/SplitsWorkerTest.java @@ -1,10 +1,24 @@ package io.split.engine.sse.workers; +import io.split.client.dtos.Split; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; import io.split.engine.common.Synchronizer; +import io.split.engine.experiments.RuleBasedSegmentParser; +import io.split.engine.experiments.SplitParser; +import io.split.engine.sse.dtos.CommonChangeNotification; +import io.split.engine.sse.dtos.GenericNotificationData; +import io.split.engine.sse.dtos.IncomingNotification; +import io.split.engine.sse.dtos.SplitKillNotification; +import io.split.storages.RuleBasedSegmentCache; +import io.split.storages.SplitCacheProducer; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import java.util.HashSet; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; @@ -12,73 +26,124 @@ public class SplitsWorkerTest { + private static final FlagSetsFilter FLAG_SETS_FILTER = new FlagSetsFilterImpl(new HashSet<>()); + @Test public void addToQueueWithoutElementsWShouldNotTriggerFetch() throws InterruptedException { Synchronizer splitFetcherMock = Mockito.mock(Synchronizer.class); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); - SplitsWorker splitsWorker = new SplitsWorkerImp(splitFetcherMock); - splitsWorker.start(); + FeatureFlagsWorker featureFlagsWorker = new FeatureFlagWorkerImp(splitFetcherMock, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + featureFlagsWorker.start(); Thread.sleep(500); - Mockito.verify(splitFetcherMock, Mockito.never()).refreshSplits(Mockito.anyLong()); - splitsWorker.stop(); + Mockito.verify(splitFetcherMock, Mockito.never()).refreshSplits(Mockito.anyObject(), Mockito.anyObject()); + featureFlagsWorker.stop(); } @Test public void addToQueueWithElementsWShouldTriggerFetch() throws InterruptedException { Synchronizer syncMock = Mockito.mock(Synchronizer.class); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); - SplitsWorker splitsWorker = new SplitsWorkerImp(syncMock); - splitsWorker.start(); + FeatureFlagsWorker featureFlagsWorker = new FeatureFlagWorkerImp(syncMock, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + featureFlagsWorker.start(); ArgumentCaptor cnCaptor = ArgumentCaptor.forClass(Long.class); - splitsWorker.addToQueue(1585956698457L); - splitsWorker.addToQueue(1585956698467L); - splitsWorker.addToQueue(1585956698477L); - splitsWorker.addToQueue(1585956698476L); + ArgumentCaptor cnCaptor2 = ArgumentCaptor.forClass(Long.class); + + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698457L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698467L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698477L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698476L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); Thread.sleep(1000); - Mockito.verify(syncMock, Mockito.times(4)).refreshSplits(cnCaptor.capture()); + Mockito.verify(syncMock, Mockito.times(4)).refreshSplits(cnCaptor.capture(), cnCaptor2.capture()); List captured = cnCaptor.getAllValues(); assertThat(captured, contains(1585956698457L, 1585956698467L, 1585956698477L, 1585956698476L)); - splitsWorker.stop(); + featureFlagsWorker.stop(); } @Test public void killShouldTriggerFetch() { long changeNumber = 1585956698457L; - String splitName = "split-test"; + String featureFlagName = "feature-flag-test"; String defaultTreatment = "off"; Synchronizer syncMock = Mockito.mock(Synchronizer.class); - SplitsWorker splitsWorker = new SplitsWorkerImp(syncMock); - splitsWorker.start(); - - splitsWorker.killSplit(changeNumber, splitName, defaultTreatment); - Mockito.verify(syncMock, Mockito.times(1)).localKillSplit(splitName, defaultTreatment, changeNumber); - splitsWorker.stop(); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); + FeatureFlagsWorker featureFlagsWorker = new FeatureFlagWorkerImp(syncMock, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER) { + }; + featureFlagsWorker.start(); + SplitKillNotification splitKillNotification = new SplitKillNotification(GenericNotificationData.builder() + .changeNumber(changeNumber) + .defaultTreatment(defaultTreatment) + .featureFlagName(featureFlagName) + .build()); + + featureFlagsWorker.kill(splitKillNotification); + Mockito.verify(syncMock, Mockito.times(1)).localKillSplit(splitKillNotification); + featureFlagsWorker.stop(); } @Test public void messagesNotProcessedWhenWorkerStopped() throws InterruptedException { Synchronizer syncMock = Mockito.mock(Synchronizer.class); - SplitsWorker splitsWorker = new SplitsWorkerImp(syncMock); - splitsWorker.start(); - splitsWorker.addToQueue(1585956698457L); + SplitParser splitParser = new SplitParser(); + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + SplitCacheProducer splitCacheProducer = Mockito.mock(SplitCacheProducer.class); + RuleBasedSegmentCache ruleBasedSegmentCache = Mockito.mock(RuleBasedSegmentCache.class); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(InMemoryTelemetryStorage.class); + FeatureFlagsWorker featureFlagsWorker = new FeatureFlagWorkerImp(syncMock, splitParser, ruleBasedSegmentParser, splitCacheProducer, ruleBasedSegmentCache, telemetryRuntimeProducer, FLAG_SETS_FILTER); + featureFlagsWorker.start(); + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698457L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); Thread.sleep(500); - splitsWorker.stop(); + featureFlagsWorker.stop(); Thread.sleep(500); - splitsWorker.addToQueue(1585956698467L); - Mockito.verify(syncMock, Mockito.times(1)).refreshSplits(1585956698457L); // Previous one! + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698467L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); + Mockito.verify(syncMock, Mockito.times(1)).refreshSplits(Mockito.anyObject(), Mockito.anyObject()); // Previous one! Mockito.reset(syncMock); - splitsWorker.start(); - splitsWorker.addToQueue(1585956698477L); + featureFlagsWorker.start(); + featureFlagsWorker.addToQueue(new CommonChangeNotification(GenericNotificationData.builder() + .changeNumber(1585956698477L) + .type(IncomingNotification.Type.SPLIT_UPDATE) + .build(), Split.class)); Thread.sleep(500); - Mockito.verify(syncMock, Mockito.times(1)).refreshSplits(1585956698477L); - splitsWorker.stop(); + Mockito.verify(syncMock, Mockito.times(1)).refreshSplits(Mockito.anyObject(), Mockito.anyObject()); + featureFlagsWorker.stop(); } -} +} \ No newline at end of file diff --git a/client/src/test/java/io/split/inputValidation/FallbackTreatmentValidatorTest.java b/client/src/test/java/io/split/inputValidation/FallbackTreatmentValidatorTest.java new file mode 100644 index 00000000..6bfc4bd9 --- /dev/null +++ b/client/src/test/java/io/split/inputValidation/FallbackTreatmentValidatorTest.java @@ -0,0 +1,50 @@ +package io.split.inputValidation; + +import io.split.client.dtos.FallbackTreatment; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; + +public class FallbackTreatmentValidatorTest { + + @Test + public void isValidTreatmentWorks() { + Assert.assertEquals("123asHs_-sdf", FallbackTreatmentValidator.isValidTreatment("123asHs_-sdf", "test")); + + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment(new String(new char[101]).replace('\0', 'w'), "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment("", "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment(null, "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment("12@3asHs_-sdf", "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment("12#3asHs_-sdf", "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment("12!3asHs_-sdf", "test")); + Assert.assertEquals(null, FallbackTreatmentValidator.isValidTreatment("12^3asHs_-sdf", "test")); + } + + @Test + public void isValidByFlagTreatmentWorks() { + HashMap byRef = new HashMap() {{ put("flag", new FallbackTreatment("12#2")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("flag", new FallbackTreatment("12%2")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("flag", new FallbackTreatment(new String(new char[101]).replace('\0', 'w'))); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("flag", new FallbackTreatment("12&2")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("", new FallbackTreatment("on")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("12#dd", new FallbackTreatment("on")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put(new String(new char[101]).replace('\0', 'w'), new FallbackTreatment("on")); }}; + Assert.assertEquals(new HashMap<>(), FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test")); + + byRef = new HashMap() {{ put("flag", new FallbackTreatment("123asHs_-sdf")); }}; + Assert.assertEquals("123asHs_-sdf", FallbackTreatmentValidator.isValidByFlagTreatment(byRef, "test").get("flag").getTreatment()); + } +} diff --git a/client/src/test/java/io/split/inputValidation/ImpressionPropertiesValidatorTest.java b/client/src/test/java/io/split/inputValidation/ImpressionPropertiesValidatorTest.java new file mode 100644 index 00000000..2ab6eb94 --- /dev/null +++ b/client/src/test/java/io/split/inputValidation/ImpressionPropertiesValidatorTest.java @@ -0,0 +1,42 @@ +package io.split.inputValidation; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.split.grammar.Treatments; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class ImpressionPropertiesValidatorTest { + + @Test(expected = IllegalStateException.class) + public void testConstructorException() { + ImpressionPropertiesValidator iv = new ImpressionPropertiesValidator(); + } + + @Test + public void propertiesAreValidWorks() { + Map properties = new HashMap() + {{ + put("prop1", 1); + put("prop2", 2L); + put("prop3", 7.56); + put("prop4", "something"); + put("prop5", true); + put("prop6", null); + }}; + ImpressionPropertiesValidator.ImpressionPropertiesValidatorResult result = ImpressionPropertiesValidator.propertiesAreValid(properties); + Assert.assertTrue(result.getSuccess()); + Assert.assertEquals(1063, result.getEventSize()); + Assert.assertEquals(6, result.getValue().size()); + + // when properties size is > Event.MAX_PROPERTIES_LENGTH_BYTES + for (int i = 7; i <= (32 * 1024); i++) { + properties.put("prop" + i, "something-" + i); + } + result = ImpressionPropertiesValidator.propertiesAreValid(properties); + Assert.assertFalse(result.getSuccess()); + } +} diff --git a/client/src/test/java/io/split/inputValidation/SplitNameValidatorTest.java b/client/src/test/java/io/split/inputValidation/SplitNameValidatorTest.java index d8db6567..cb325dfc 100644 --- a/client/src/test/java/io/split/inputValidation/SplitNameValidatorTest.java +++ b/client/src/test/java/io/split/inputValidation/SplitNameValidatorTest.java @@ -22,9 +22,29 @@ public void isValidWorks() { result = SplitNameValidator.isValid("", "test"); Assert.assertFalse(result.isPresent()); + // test regex + result = SplitNameValidator.isValid("te#fg", "test"); + Assert.assertFalse(result.isPresent()); + + // test regex + result = SplitNameValidator.isValid("te@fg", "test"); + Assert.assertFalse(result.isPresent()); + + // test regex + result = SplitNameValidator.isValid("te&fg", "test"); + Assert.assertFalse(result.isPresent()); + + // test regex + result = SplitNameValidator.isValid("te)fg", "test"); + Assert.assertFalse(result.isPresent()); + + // test length + result = SplitNameValidator.isValid(new String(new char[101]).replace('\0', 'w'), "test"); + Assert.assertFalse(result.isPresent()); + // when split name have empty spaces - result = SplitNameValidator.isValid(" split name test ", "test"); + result = SplitNameValidator.isValid(" split-name-test ", "test"); Assert.assertTrue(result.isPresent()); - Assert.assertEquals("split name test", result.get()); + Assert.assertEquals("split-name-test", result.get()); } } diff --git a/client/src/test/java/io/split/inputValidation/TrafficTypeValidatorTest.java b/client/src/test/java/io/split/inputValidation/TrafficTypeValidatorTest.java index 765e5666..261e63db 100644 --- a/client/src/test/java/io/split/inputValidation/TrafficTypeValidatorTest.java +++ b/client/src/test/java/io/split/inputValidation/TrafficTypeValidatorTest.java @@ -1,6 +1,6 @@ package io.split.inputValidation; -import io.split.cache.SplitCache; +import io.split.storages.SplitCacheConsumer; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -11,25 +11,25 @@ public class TrafficTypeValidatorTest { @Test public void isValidWorks() { - SplitCache splitCache = Mockito.mock(SplitCache.class); + SplitCacheConsumer splitCacheConsumer = Mockito.mock(SplitCacheConsumer.class); - Optional result = TrafficTypeValidator.isValid("traffic_type_test", splitCache, "test"); + Optional result = TrafficTypeValidator.isValid("traffic_type_test", splitCacheConsumer, "test"); Assert.assertTrue(result.isPresent()); Assert.assertEquals("traffic_type_test", result.get()); // when tt have upper case - result = TrafficTypeValidator.isValid("trafficTypeTest", splitCache, "test"); + result = TrafficTypeValidator.isValid("trafficTypeTest", splitCacheConsumer, "test"); Assert.assertTrue(result.isPresent()); Assert.assertEquals("traffictypetest", result.get()); // when tt is null - result = TrafficTypeValidator.isValid(null, splitCache, "test"); + result = TrafficTypeValidator.isValid(null, splitCacheConsumer, "test"); Assert.assertFalse(result.isPresent()); // when tt is empty - result = TrafficTypeValidator.isValid("", splitCache, "test"); + result = TrafficTypeValidator.isValid("", splitCacheConsumer, "test"); Assert.assertFalse(result.isPresent()); } diff --git a/client/src/test/java/io/split/service/HttpPostImpTest.java b/client/src/test/java/io/split/service/HttpPostImpTest.java new file mode 100644 index 00000000..9efdb21c --- /dev/null +++ b/client/src/test/java/io/split/service/HttpPostImpTest.java @@ -0,0 +1,55 @@ +package io.split.service; + +import io.split.TestHelper; +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; +import io.split.telemetry.domain.enums.HttpParamsWrapper; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryStorage; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; + +public class HttpPostImpTest { + + private static final String URL = "www.split.io"; + + @Test + public void testPostWith200() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, + IOException, URISyntaxException { + CloseableHttpClient client = TestHelper.mockHttpClient(URL, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(client, new RequestDecorator(null), "qwerty", + metadata()); + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + HttpPostImp httpPostImp = new HttpPostImp(splitHttpClient, telemetryStorage); + httpPostImp.post(URI.create(URL), new Object(), "Metrics", HttpParamsWrapper.TELEMETRY); + Mockito.verify(client, Mockito.times(1)).execute(Mockito.any()); + Assert.assertNotEquals(0, telemetryStorage.getLastSynchronization().getTelemetry()); + Assert.assertEquals(1, telemetryStorage.popHTTPLatencies().getTelemetry().stream().mapToInt(Long::intValue).sum()); + } + + @Test + public void testPostWith400() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, + IOException, URISyntaxException { + CloseableHttpClient client = TestHelper.mockHttpClient(URL, HttpStatus.SC_CLIENT_ERROR); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(client, new RequestDecorator(null), "qwerty", + metadata()); + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + HttpPostImp httpPostImp = new HttpPostImp(splitHttpClient, telemetryStorage); + httpPostImp.post(URI.create(URL), new Object(), "Metrics", HttpParamsWrapper.TELEMETRY); + Mockito.verify(client, Mockito.times(1)).execute(Mockito.any()); + Assert.assertEquals(1, telemetryStorage.popHTTPErrors().getTelemetry().get(Long.valueOf(HttpStatus.SC_CLIENT_ERROR)).intValue()); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + +} diff --git a/client/src/test/java/io/split/service/HttpSplitClientTest.java b/client/src/test/java/io/split/service/HttpSplitClientTest.java new file mode 100644 index 00000000..746ae5c0 --- /dev/null +++ b/client/src/test/java/io/split/service/HttpSplitClientTest.java @@ -0,0 +1,171 @@ +package io.split.service; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import io.split.TestHelper; +import io.split.client.RequestDecorator; +import io.split.client.dtos.*; +import io.split.client.impressions.Impression; +import io.split.client.utils.Json; +import io.split.client.utils.SDKMetadata; +import io.split.engine.common.FetchOptions; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpUriRequest; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.mockito.Mockito.verify; + +public class HttpSplitClientTest { + + @Test + public void testGetWithSpecialCharacters() throws URISyntaxException, InvocationTargetException, + NoSuchMethodException, IllegalAccessException, IOException { + URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567&rbSince=-1"); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_OK); + RequestDecorator decorator = new RequestDecorator(null); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, decorator, "qwerty", metadata()); + Map> additionalHeaders = Collections.singletonMap("AdditionalHeader", + Collections.singletonList("add")); + + SplitHttpResponse splitHttpResponse = splitHtpClient.get(rootTarget, + new FetchOptions.Builder().cacheControlHeaders(true).build(), additionalHeaders); + SplitChange change = Json.fromJson(splitHttpResponse.body(), SplitChange.class); + + ArgumentCaptor captor = ArgumentCaptor.forClass(HttpUriRequest.class); + verify(httpClientMock).execute(captor.capture()); + HttpUriRequest request = captor.getValue(); + assertThat(request.getFirstHeader("AdditionalHeader").getValue(), is(equalTo("add"))); + + SplitHttpResponse.Header[] headers = splitHttpResponse.responseHeaders(); + assertThat(headers[0].getName(), is(equalTo("Via"))); + assertThat(headers[0].getValues().get(0), is(equalTo("HTTP/1.1 m_proxy_rio1"))); + Assert.assertNotNull(change); + Assert.assertEquals(1, change.featureFlags.d.size()); + Assert.assertNotNull(change.featureFlags.d.get(0)); + + Split split = change.featureFlags.d.get(0); + Map configs = split.configurations; + Assert.assertEquals(2, configs.size()); + Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on")); + Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off")); + Assert.assertEquals(2, split.sets.size()); + } + + @Test + public void testGetError() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, + IllegalAccessException, IOException { + URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567"); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_INTERNAL_SERVER_ERROR); + RequestDecorator decorator = new RequestDecorator(null); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, decorator, "qwerty", metadata()); + SplitHttpResponse splitHttpResponse = splitHtpClient.get(rootTarget, + new FetchOptions.Builder().cacheControlHeaders(true).build(), null); + Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode()); + } + + @Test(expected = IllegalStateException.class) + public void testException() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, + IllegalAccessException, IOException { + URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567"); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_INTERNAL_SERVER_ERROR); + RequestDecorator decorator = null; + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, decorator, "qwerty", metadata()); + splitHtpClient.get(rootTarget, + new FetchOptions.Builder().cacheControlHeaders(true).build(), null); + } + + @Test + public void testPost() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException { + URI rootTarget = URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk"); + + // Setup response mock + CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_OK); + RequestDecorator decorator = new RequestDecorator(null); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, decorator, "qwerty", metadata()); + + // Send impressions + List toSend = Arrays.asList(new TestImpressions("t1", Arrays.asList( + KeyImpression.fromImpression(new Impression("k1", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t1", "on", 123L, "r1", 456L, null, null)))), + new TestImpressions("t2", Arrays.asList( + KeyImpression.fromImpression(new Impression("k1", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t2", "on", 123L, "r1", 456L, null, null))))); + + Map> additionalHeaders = Collections.singletonMap("SplitSDKImpressionsMode", + Collections.singletonList("OPTIMIZED")); + SplitHttpResponse splitHttpResponse = splitHtpClient.post(rootTarget, Json.toJson(toSend), + additionalHeaders); + + // Capture outgoing request and validate it + ArgumentCaptor captor = ArgumentCaptor.forClass(HttpUriRequest.class); + verify(httpClient).execute(captor.capture()); + HttpUriRequest request = captor.getValue(); + assertThat(request.getUri(), + is(equalTo(URI.create("https://kubernetesturl.com/split/api/testImpressions/bulk")))); + assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("OPTIMIZED"))); + assertThat(request, instanceOf(HttpPost.class)); + HttpPost asPostRequest = (HttpPost) request; + InputStreamReader reader = new InputStreamReader(asPostRequest.getEntity().getContent()); + Gson gson = new Gson(); + List payload = gson.fromJson(reader, new TypeToken>() { + }.getType()); + assertThat(payload.size(), is(equalTo(2))); + Assert.assertEquals(200, (long) splitHttpResponse.statusCode()); + } + + @Test + public void testPosttNoExceptionOnHttpErrorCode() throws URISyntaxException, InvocationTargetException, + NoSuchMethodException, IllegalAccessException, IOException { + URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567"); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_INTERNAL_SERVER_ERROR); + RequestDecorator decorator = new RequestDecorator(null); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, decorator, "qwerty", metadata()); + SplitHttpResponse splitHttpResponse = splitHtpClient.post(rootTarget, + Json.toJson(Arrays.asList(new String[] { "A", "B", "C", "D" })), null); + Assert.assertEquals(500, (long) splitHttpResponse.statusCode()); + + } + + @Test(expected = IOException.class) + public void testPosttException() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, + IllegalAccessException, IOException { + URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567"); + CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json", + HttpStatus.SC_INTERNAL_SERVER_ERROR); + + SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, null, "qwerty", metadata()); + splitHtpClient.post(rootTarget, Json.toJson(Arrays.asList(new String[] { "A", "B", "C", "D" })), null); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + +} diff --git a/client/src/test/java/io/split/storages/memory/InMemoryCacheTest.java b/client/src/test/java/io/split/storages/memory/InMemoryCacheTest.java new file mode 100644 index 00000000..5589d71d --- /dev/null +++ b/client/src/test/java/io/split/storages/memory/InMemoryCacheTest.java @@ -0,0 +1,245 @@ +package io.split.storages.memory; + +import com.google.common.collect.Lists; +import io.split.client.dtos.Partition; +import io.split.client.interceptors.FlagSetsFilter; +import io.split.client.interceptors.FlagSetsFilterImpl; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.experiments.ParsedCondition; +import io.split.engine.experiments.ParsedSplit; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; +import io.split.grammar.Treatments; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class InMemoryCacheTest { + private static final String EMPLOYEES = "Employees"; + private InMemoryCacheImp _cache; + + @Before + public void before() { + FlagSetsFilter flagSetsFilter = new FlagSetsFilterImpl(new HashSet<>(Arrays.asList("set1", "set2"))); + _cache = new InMemoryCacheImp(flagSetsFilter); + } + + @Test + public void putAndGetSplit() { + ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name"); + _cache.putMany(Stream.of(split).collect(Collectors.toList())); + + ParsedSplit result = _cache.get("split_name"); + Assert.assertNotNull(result); + Assert.assertEquals(split.changeNumber(), result.changeNumber()); + Assert.assertEquals(split.trafficTypeName(), result.trafficTypeName()); + Assert.assertEquals(split.defaultTreatment(), result.defaultTreatment()); + } + + @Test + public void putDuplicateSplit() { + ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name"); + ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name"); + _cache.putMany(Stream.of(split, split2).collect(Collectors.toList())); + + int result = _cache.getAll().size(); + + Assert.assertEquals(1, result); + } + + @Test + public void getInExistentSplit() { + ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name"); + _cache.putMany(Stream.of(split).collect(Collectors.toList())); + + ParsedSplit result = _cache.get("split_name_2"); + Assert.assertNull(result); + } + + @Test + public void removeSplit() { + ParsedSplit splitWithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name"); + ParsedSplit split2WithFlagSetsSameStorage = getParsedSplitWithFlagSetsSameStorage("split_name_2"); + ParsedSplit splitWithFlagSetsNotSameStorage = getParsedSplitWithFlagSetsNotSameStorage("split_name_3"); + ParsedSplit splitFlagSetsEmpty = getParsedSplitFlagSetsEmpty("split_name_4"); + ParsedSplit splitFlagSetsNull = getParsedSplitFlagSetsNull("split_name_5"); + _cache.putMany(Stream.of(splitWithFlagSetsSameStorage, split2WithFlagSetsSameStorage, splitWithFlagSetsNotSameStorage, + splitFlagSetsEmpty, splitFlagSetsNull).collect(Collectors.toList())); + + int result = _cache.getAll().size(); + Assert.assertEquals(5, result); + Map> namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2")); + Assert.assertTrue(namesByFlagSets.get("set1").contains("split_name")); + Assert.assertTrue(namesByFlagSets.get("set2").contains("split_name")); + + _cache.remove("split_name"); + result = _cache.getAll().size(); + Assert.assertEquals(4, result); + namesByFlagSets = _cache.getNamesByFlagSets(Arrays.asList("set1", "set2")); + Assert.assertFalse(namesByFlagSets.get("set1").contains("split_name")); + Assert.assertFalse(namesByFlagSets.get("set2").contains("split_name")); + + _cache.remove("split_name_3"); + result = _cache.getAll().size(); + Assert.assertEquals(3, result); + + _cache.remove("split_name_4"); + result = _cache.getAll().size(); + Assert.assertEquals(2, result); + + _cache.remove("split_name_5"); + result = _cache.getAll().size(); + Assert.assertEquals(1, result); + + Assert.assertNull(_cache.get("split_name")); + } + + @Test + public void setAndGetChangeNumber() { + _cache.setChangeNumber(223); + + long changeNumber = _cache.getChangeNumber(); + Assert.assertEquals(223, changeNumber); + + _cache.setChangeNumber(539); + changeNumber = _cache.getChangeNumber(); + Assert.assertEquals(539, changeNumber); + } + + @Test + public void getMany() { + ParsedSplit split = getParsedSplitWithFlagSetsSameStorage("split_name_1"); + ParsedSplit split2 = getParsedSplitWithFlagSetsSameStorage("split_name_2"); + ParsedSplit split3 = getParsedSplitWithFlagSetsSameStorage("split_name_3"); + ParsedSplit split4 = getParsedSplitWithFlagSetsSameStorage("split_name_4"); + _cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList())); + + List names = new ArrayList<>(); + names.add("split_name_2"); + names.add("split_name_3"); + + Map result = _cache.fetchMany(names); + Assert.assertEquals(2, result.keySet().size()); + Assert.assertNotNull(result.get("split_name_2")); + } + + @Test + public void trafficTypesExist() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, null, true, null); + ParsedSplit split2 = ParsedSplit.createParsedSplitForTests("splitName_2", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, null, true, null); + ParsedSplit split3 = ParsedSplit.createParsedSplitForTests("splitName_3", 0, false, "default_treatment", new ArrayList<>(), "tt_2", 123, 2, null, true, null); + ParsedSplit split4 = ParsedSplit.createParsedSplitForTests("splitName_4", 0, false, "default_treatment", new ArrayList<>(), "tt_3", 123, 2, null, true, null); + + _cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList())); + assertTrue(_cache.trafficTypeExists("tt_2")); + assertTrue(_cache.trafficTypeExists("tt")); + assertFalse(_cache.trafficTypeExists("tt_5")); + + _cache.remove("splitName_2"); + assertTrue(_cache.trafficTypeExists("tt")); + + _cache.remove("splitName_1"); + assertFalse(_cache.trafficTypeExists("tt")); + } + + @Test + public void testSegmentNames() { + List fullyRollout = Lists.newArrayList(ConditionsTestUtil.partition("on", 100)); + List turnOff = Lists.newArrayList(ConditionsTestUtil.partition(Treatments.CONTROL, 100)); + ParsedCondition parsedCondition1 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(EMPLOYEES)), fullyRollout); + ParsedCondition parsedCondition2 = ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new UserDefinedSegmentMatcher(EMPLOYEES+"2")), turnOff); + + ParsedSplit split = ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", Stream.of(parsedCondition1).collect(Collectors.toList()), "tt", 123, 2, null, true, null); + ParsedSplit split2 = ParsedSplit.createParsedSplitForTests("splitName_2", 0, false, "default_treatment", Stream.of(parsedCondition2).collect(Collectors.toList()), "tt", 123, 2, null, true, null); + ParsedSplit split3 = ParsedSplit.createParsedSplitForTests("splitName_3", 0, false, "default_treatment", Stream.of(parsedCondition1).collect(Collectors.toList()), "tt_2", 123, 2, null, true, null); + ParsedSplit split4 = ParsedSplit.createParsedSplitForTests("splitName_4", 0, false, "default_treatment", Stream.of(parsedCondition2).collect(Collectors.toList()), "tt_3", 123, 2, null, true, null); + + _cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList())); + + Set segments = _cache.getSegments(); + Assert.assertEquals(2, segments.size()); + Assert.assertTrue(segments.contains(EMPLOYEES)); + Assert.assertTrue(segments.contains(EMPLOYEES+"2")); + + } + + private ParsedSplit getParsedSplitWithFlagSetsSameStorage(String splitName) { + return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1", "set2")), true, null); + } + + private ParsedSplit getParsedSplitWithFlagSetsNotSameStorage(String splitName) { + return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set3")), true, null); + } + + private ParsedSplit getParsedSplitFlagSetsNull(String splitName) { + return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, null, true, null); + } + + private ParsedSplit getParsedSplitFlagSetsEmpty(String splitName) { + return ParsedSplit.createParsedSplitForTests(splitName, 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(), true, null); + } + + @Test + public void testPutMany() { + _cache.putMany(Stream.of(getParsedSplitWithFlagSetsSameStorage("split_name_1"), getParsedSplitWithFlagSetsSameStorage("split_name_2"), getParsedSplitWithFlagSetsSameStorage("split_name_3"), getParsedSplitWithFlagSetsSameStorage("split_name_4")).collect(Collectors.toList())); + List names = Stream.of("split_name_1","split_name_2","split_name_3","split_name_4").collect(Collectors.toList()); + + Map result = _cache.fetchMany(names); + Assert.assertEquals(4, result.keySet().size()); + } + + @Test + public void testIncreaseTrafficType() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(), true, null); + _cache.putMany(Stream.of(split).collect(Collectors.toList())); + _cache.increaseTrafficType("tt_2"); + assertTrue(_cache.trafficTypeExists("tt_2")); + } + + @Test + public void testDecreaseTrafficType() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(), true, null); + _cache.putMany(Stream.of(split).collect(Collectors.toList())); + _cache.decreaseTrafficType("tt"); + assertFalse(_cache.trafficTypeExists("tt_2")); + } + + @Test + public void testGetNamesByFlagSets() { + ParsedSplit split = ParsedSplit.createParsedSplitForTests("splitName_1", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1", "set2", "set3")), true, null); + ParsedSplit split2 = ParsedSplit.createParsedSplitForTests("splitName_2", 0, false, "default_treatment", new ArrayList<>(), "tt", 123, 2, new HashSet<>(Arrays.asList("set1")), true, null); + ParsedSplit split3 = ParsedSplit.createParsedSplitForTests("splitName_3", 0, false, "default_treatment", new ArrayList<>(), "tt_2", 123, 2, new HashSet<>(Arrays.asList("set4")), true, null); + ParsedSplit split4 = ParsedSplit.createParsedSplitForTests("splitName_4", 0, false, "default_treatment", new ArrayList<>(), "tt_3", 123, 2, new HashSet<>(Arrays.asList("set2")), true, null); + + _cache.putMany(Stream.of(split, split2, split3, split4).collect(Collectors.toList())); + Map> namesByFlagSets = _cache.getNamesByFlagSets(new ArrayList<>(Arrays.asList("set1", "set2", "set3"))); + assertTrue(namesByFlagSets.get("set1").contains("splitName_1")); + assertTrue(namesByFlagSets.get("set1").contains("splitName_2")); + assertFalse(namesByFlagSets.get("set1").contains("splitName_3")); + assertFalse(namesByFlagSets.get("set1").contains("splitName_4")); + assertTrue(namesByFlagSets.keySet().contains("set3")); + assertNull(namesByFlagSets.get("set3")); + + _cache.remove("splitName_2"); + namesByFlagSets = _cache.getNamesByFlagSets(new ArrayList<>(Arrays.asList("set1", "set2", "set3"))); + assertFalse(namesByFlagSets.get("set1").contains("splitName_2")); + _cache.remove("splitName_1"); + namesByFlagSets = _cache.getNamesByFlagSets(new ArrayList<>(Arrays.asList("set1", "set2", "set3"))); + assertFalse(namesByFlagSets.get("set1").contains("splitName_1")); + assertTrue(namesByFlagSets.get("set1").isEmpty()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImplTest.java b/client/src/test/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImplTest.java new file mode 100644 index 00000000..492cc8ae --- /dev/null +++ b/client/src/test/java/io/split/storages/memory/RuleBasedSegmentCacheInMemoryImplTest.java @@ -0,0 +1,72 @@ +package io.split.storages.memory; + +import com.google.common.collect.Sets; +import io.split.client.dtos.ExcludedSegments; +import io.split.client.dtos.MatcherCombiner; +import io.split.engine.experiments.ParsedRuleBasedSegment; +import io.split.engine.experiments.ParsedCondition; +import io.split.client.dtos.ConditionType; + +import io.split.engine.matchers.AttributeMatcher; +import io.split.engine.matchers.CombiningMatcher; +import io.split.engine.matchers.UserDefinedSegmentMatcher; +import io.split.engine.matchers.strings.WhitelistMatcher; +import junit.framework.TestCase; +import org.junit.Test; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +public class RuleBasedSegmentCacheInMemoryImplTest extends TestCase { + + @Test + public void testAddAndDeleteSegment(){ + RuleBasedSegmentCacheInMemoryImp ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin"))); + CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher)); + ParsedRuleBasedSegment parsedRuleBasedSegment = new ParsedRuleBasedSegment("sample_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, "label")),"user", + 123, Lists.newArrayList("mauro@test.io","gaston@test.io"), Lists.newArrayList()); + ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment), null, 123); + assertEquals(123, ruleBasedSegmentCache.getChangeNumber()); + assertEquals(parsedRuleBasedSegment, ruleBasedSegmentCache.get("sample_rule_based_segment")); + assertTrue(ruleBasedSegmentCache.contains(new HashSet<>(Arrays.asList("sample_rule_based_segment")))); + assertFalse(ruleBasedSegmentCache.contains(new HashSet<>(Arrays.asList("sample_rule_based_segment", "123")))); + + ruleBasedSegmentCache.update(null, Lists.newArrayList("sample_rule_based_segment"), 124); + assertEquals(124, ruleBasedSegmentCache.getChangeNumber()); + assertEquals(null, ruleBasedSegmentCache.get("sample_rule_based_segment")); + } + + @Test + public void testMultipleSegment(){ + List excludedSegments = new ArrayList<>(); + excludedSegments.add(new ExcludedSegments("standard","segment1")); + excludedSegments.add(new ExcludedSegments("standard","segment3")); + + RuleBasedSegmentCacheInMemoryImp ruleBasedSegmentCache = new RuleBasedSegmentCacheInMemoryImp(); + AttributeMatcher whiteListMatcher = AttributeMatcher.vanilla(new WhitelistMatcher(Lists.newArrayList("test_1", "admin"))); + CombiningMatcher whitelistCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(whiteListMatcher)); + ParsedRuleBasedSegment parsedRuleBasedSegment1 = new ParsedRuleBasedSegment("sample_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, whitelistCombiningMatcher, null, "label")),"user", + 123, Lists.newArrayList("mauro@test.io","gaston@test.io"), excludedSegments); + + excludedSegments.clear(); + excludedSegments.add(new ExcludedSegments("standard","segment1")); + excludedSegments.add(new ExcludedSegments("standard","segment2")); + AttributeMatcher segmentMatcher = AttributeMatcher.vanilla(new UserDefinedSegmentMatcher("employees")); + CombiningMatcher segmentCombiningMatcher = new CombiningMatcher(MatcherCombiner.AND, Lists.newArrayList(segmentMatcher)); + ParsedRuleBasedSegment parsedRuleBasedSegment2 = new ParsedRuleBasedSegment("another_rule_based_segment", + Lists.newArrayList(new ParsedCondition(ConditionType.WHITELIST, segmentCombiningMatcher, null, "label")),"user", + 123, Lists.newArrayList("mauro@test.io","gaston@test.io"), excludedSegments); + + ruleBasedSegmentCache.update(Lists.newArrayList(parsedRuleBasedSegment1, parsedRuleBasedSegment2), null, 123); + assertEquals(Lists.newArrayList("another_rule_based_segment", "sample_rule_based_segment"), ruleBasedSegmentCache.ruleBasedSegmentNames()); + assertEquals(Sets.newHashSet("segment2", "segment1", "employees"), ruleBasedSegmentCache.getSegments()); + assertTrue(ruleBasedSegmentCache.contains(new HashSet<>(Arrays.asList("sample_rule_based_segment", "another_rule_based_segment")))); + assertTrue(ruleBasedSegmentCache.contains(new HashSet<>(Arrays.asList("sample_rule_based_segment")))); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/cache/SegmentCacheInMemoryImplTest.java b/client/src/test/java/io/split/storages/memory/SegmentCacheInMemoryImplTest.java similarity index 64% rename from client/src/test/java/io/split/cache/SegmentCacheInMemoryImplTest.java rename to client/src/test/java/io/split/storages/memory/SegmentCacheInMemoryImplTest.java index 95f0b54e..be4d7172 100644 --- a/client/src/test/java/io/split/cache/SegmentCacheInMemoryImplTest.java +++ b/client/src/test/java/io/split/storages/memory/SegmentCacheInMemoryImplTest.java @@ -1,6 +1,7 @@ -package io.split.cache; +package io.split.storages.memory; import junit.framework.TestCase; +import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; @@ -17,28 +18,28 @@ public class SegmentCacheInMemoryImplTest extends TestCase { @Test public void testUpdateSegment(){ SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); - assertEquals(DEFAULT_CHANGE_NUMBER, segmentCacheInMemory.getChangeNumber(SEGMENT_NAME)); + assertEquals(1L, segmentCacheInMemory.getChangeNumber(SEGMENT_NAME)); } @Test public void testIsInSegment() { SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME, Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME, Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>(), 1L); assertTrue(segmentCacheInMemory.isInSegment(SEGMENT_NAME, KEY)); } @Test public void testIsInSegmentWithFakeSegment() { SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME, Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME, Stream.of(KEY).collect(Collectors.toList()), new ArrayList<>(), 1L); assertFalse(segmentCacheInMemory.isInSegment(FAKE_SEGMENT_NAME, KEY)); } @Test public void testSetChangeNumber() { SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); segmentCacheInMemory.setChangeNumber(SEGMENT_NAME, CHANGE_NUMBER); assertEquals(CHANGE_NUMBER, segmentCacheInMemory.getChangeNumber(SEGMENT_NAME)); } @@ -46,16 +47,32 @@ public void testSetChangeNumber() { @Test public void testGetChangeNumberWithFakeSegment() { SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); assertEquals(DEFAULT_CHANGE_NUMBER, segmentCacheInMemory.getChangeNumber(FAKE_SEGMENT_NAME)); } @Test public void testClear() { SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); - segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>()); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); segmentCacheInMemory.setChangeNumber(SEGMENT_NAME, CHANGE_NUMBER); segmentCacheInMemory.clear(); assertEquals(DEFAULT_CHANGE_NUMBER, segmentCacheInMemory.getChangeNumber(SEGMENT_NAME)); } + + @Test + public void testGetAll() { + SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); + segmentCacheInMemory.updateSegment(FAKE_SEGMENT_NAME,new ArrayList<>(), new ArrayList<>(), 1L); + Assert.assertEquals(2, segmentCacheInMemory.getSegmentCount()); + } + + @Test + public void testGetAllKeys() { + SegmentCacheInMemoryImpl segmentCacheInMemory = new SegmentCacheInMemoryImpl(); + segmentCacheInMemory.updateSegment(SEGMENT_NAME,Stream.of("KEY1", "KEY2").collect(Collectors.toList()), new ArrayList<>(), 1L); + segmentCacheInMemory.updateSegment(FAKE_SEGMENT_NAME,Stream.of("KEY3", "KEY2").collect(Collectors.toList()), new ArrayList<>(), 1L); + Assert.assertEquals(4, segmentCacheInMemory.getKeyCount()); + } } \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperHasPipeline.java b/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperHasPipeline.java new file mode 100644 index 00000000..4a40061b --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperHasPipeline.java @@ -0,0 +1,196 @@ +package io.split.storages.pluggable; + +import com.google.common.collect.Maps; +import pluggable.CustomStorageWrapper; +import pluggable.HasPipelineSupport; +import pluggable.Pipeline; +import pluggable.Result; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentMap; + +public class CustomStorageWrapperHasPipeline implements CustomStorageWrapper, HasPipelineSupport { + + private static final String COUNTS = "SPLITIO.impressions.count"; + private static final String FLAG_SET = "SPLITIO.flagSet"; + private final ConcurrentMap _impressionsCount = Maps.newConcurrentMap(); + private final ConcurrentMap> _flagSets = Maps.newConcurrentMap(); + + public CustomStorageWrapperHasPipeline() { + _flagSets.put("SPLITIO.flagSet.set1", new HashSet<>(new ArrayList<>(Arrays.asList("flag1", "flag2")))); + } + + @Override + public String get(String key) throws Exception { + return null; + } + + @Override + public List getMany(List keys) { + return null; + } + + @Override + public void set(String key, String item) throws Exception { + + } + + @Override + public void hSet(String key, String field, String item) { + + } + + @Override + public void delete(List keys) { + + } + + @Override + public String getAndSet(String key, String item) { + return null; + } + + @Override + public Set getKeysByPrefix(String prefix) { + return null; + } + + @Override + public long increment(String key, long value) { + return 0; + } + + @Override + public long decrement(String key, long value) { + return 0; + } + + @Override + public long hIncrement(String key, String field, long value) { + return 0; + } + + @Override + public long pushItems(String key, List items) { + return 0; + } + + @Override + public List popItems(String key, long count) { + return null; + } + + @Override + public long getItemsCount(String key) { + return 0; + } + + @Override + public boolean itemContains(String key, String item) { + return false; + } + + @Override + public void addItems(String key, List items) { + + } + + @Override + public void removeItems(String key, List items) { + + } + + @Override + public List getItems(List keys) { + return null; + } + + @Override + public Set getMembers(String key) { + return null; + } + + @Override + public boolean connect() { + return false; + } + + @Override + public boolean disconnect() { + return false; + } + + @Override + public Pipeline pipeline() { + return new CustomPipeline(); + } + + public ConcurrentMap getImpressionsCount(){ + return _impressionsCount; + } + private class CustomPipeline implements Pipeline { + + private List> methodsToExecute; + + public CustomPipeline() { + this.methodsToExecute = new ArrayList<>(); + } + + @Override + public List exec() throws Exception { + List result = new ArrayList<>(); + for (Callable method : methodsToExecute) { + result.add(new Result(method.call())); + } + return result; + } + + @Override + public void hIncrement(String key, String field, long value) { + methodsToExecute.add(() -> { return hIncrementToExecute(key, field, value);}); + } + + public long hIncrementToExecute(String key, String field, long value){ + String storageKey = getStorage(key); + Long count = 0L; + if (storageKey.equals(COUNTS)){ + if(_impressionsCount.containsKey(field)){ + count = _impressionsCount.get(field); + } + count += value; + _impressionsCount.put(field, count); + } + return count; + } + + @Override + public void getMembers(String key) { + methodsToExecute.add(() -> { return getMembersToExecute(key);}); + } + + private HashSet getMembersToExecute(String key) { + String storageKey = getStorage(key); + if(storageKey.equals(FLAG_SET)) { + return _flagSets.get(key); + } + return new HashSet<>(); + } + + private String getStorage(String key) { + if(key.startsWith(COUNTS)) + return COUNTS; + if(key.startsWith(FLAG_SET)) + return FLAG_SET; + return ""; + } + + public ConcurrentMap getImpressionsCount(){ + return _impressionsCount; + } + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperImp.java b/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperImp.java new file mode 100644 index 00000000..8733f3d1 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/CustomStorageWrapperImp.java @@ -0,0 +1,316 @@ +package io.split.storages.pluggable; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializer; +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.Status; +import io.split.client.dtos.RuleBasedSegment; +import io.split.client.utils.Json; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.segments.SegmentImp; +import io.split.grammar.Treatments; +import io.split.storages.pluggable.domain.EventConsumer; +import io.split.storages.pluggable.domain.ImpressionConsumer; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.utils.AtomicLongArray; +import pluggable.CustomStorageWrapper; +import pluggable.NotPipelinedImpl; +import pluggable.Pipeline; + +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; + +public class CustomStorageWrapperImp implements CustomStorageWrapper { + + public static final int MAX_LATENCY_BUCKET_COUNT = 23; + private static final String TELEMETRY = "SPLITIO.telemetry"; + + private static final String TELEMETRY_INIT = "SPLITIO.telemetry.init"; + private static final String LATENCIES = "SPLITIO.telemetry.latencies"; + private static final String SPLIT = "SPLITIO.split."; + private static final String RULE_BASED_SEGMENT = "SPLITIO.rbsegment"; + private static final String RULE_BASED_SEGMENTS = "SPLITIO.rbsegments"; + private static final String SPLITS = "SPLITIO.splits.*"; + private static final String SEGMENT = "SPLITIO.segment."; + private static final String IMPRESSIONS = "SPLITIO.impressions"; + private static final String EVENTS = "SPLITIO.events"; + private static final String COUNTS = "SPLITIO.impressions.counts"; + private static final String FLAG_SET = "SPLITIO.flagSet"; + private Map splitsStorage = new HashMap<>(); + private Map ruleBasedSegmentStorage = new HashMap<>(); + private Map segmentStorage = new HashMap<>(); + private final ConcurrentMap _methodLatencies = Maps.newConcurrentMap(); + private final ConcurrentMap _latencies = Maps.newConcurrentMap(); + private final ConcurrentMap _impressionsCount = Maps.newConcurrentMap(); + private final ConcurrentMap _config = Maps.newConcurrentMap(); + private final ConcurrentMap> _flagSets = Maps.newConcurrentMap(); + private List imps = new ArrayList<>(); + private List events = new ArrayList<>(); + private final Gson _json = new GsonBuilder() + .serializeNulls() // Send nulls + .excludeFieldsWithModifiers(Modifier.STATIC) + .registerTypeAdapter(Double.class, (JsonSerializer) (src, typeOfSrc, context) -> { + if (src == src.longValue()) + return new JsonPrimitive(src.longValue()); + return new JsonPrimitive(src); + }) + .create(); + + public CustomStorageWrapperImp() { + _methodLatencies.put(MethodEnum.TREATMENT.getMethod(), new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS.getMethod(), new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENT_WITH_CONFIG.getMethod(), new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TREATMENTS_WITH_CONFIG.getMethod(), new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + _methodLatencies.put(MethodEnum.TRACK.getMethod(), new AtomicLongArray(MAX_LATENCY_BUCKET_COUNT)); + updateCache(); + } + + @Override + public String get(String key) throws Exception { + String value = getStorage(key); + if(value.equals(SPLIT)){ + return _json.toJson(splitsStorage.get(key)); + } + if(value.equals(RULE_BASED_SEGMENT)){ + return _json.toJson(ruleBasedSegmentStorage.get(key)); + } + return ""; + } + + @Override + public List getMany(List keys) throws Exception { + if(keys == null || keys.size()==0){ + return null; + } + String value = getStorage(keys.get(0)); + if(value.equals(SPLIT)){ + List results = new ArrayList<>(); + keys.forEach(k -> results.add(Json.toJson(splitsStorage.get(k)))); + return results; + } + return null; + } + + @Override + public void set(String key, String item) throws Exception { + + } + + @Override + public void hSet(String key, String field, String item) { + String value = getStorage(key); + if (value.equals(TELEMETRY_INIT)){ + _config.put(field, item); + } + } + + @Override + public void delete(List keys) throws Exception { + + } + + @Override + public String getAndSet(String key, String item) throws Exception { + return null; + } + + @Override + public Set getKeysByPrefix(String prefix) throws Exception { + String value = getStorage(prefix); + if(value.equals(SPLIT)){ + return splitsStorage.keySet(); + } + return null; + } + + @Override + public long increment(String key, long value) throws Exception { + return 0; + } + + @Override + public long decrement(String key, long value) throws Exception { + return 0; + } + + @Override + public long hIncrement(String key, String field, long value) throws Exception { + String storageKey = getStorage(key); + Long count = 0L; + if (storageKey.equals(COUNTS)){ + if(_impressionsCount.containsKey(field)){ + count = _impressionsCount.get(field); + } + count += value; + _impressionsCount.put(field, count); + return count; + } + if(storageKey.equals(LATENCIES)){ + if(_latencies.containsKey(field)){ + count = _latencies.get(field); + } + count += value; + _latencies.put(field, count); + } + return count; + } + + @Override + public long pushItems(String key, List items) throws Exception { + String value = getStorage(key); + if(value.equals(IMPRESSIONS)){ + items.forEach(imp -> imps.add(_json.fromJson(imp, ImpressionConsumer.class))); + } + else if(value.equals(EVENTS)) { + items.forEach(ev -> events.add(_json.fromJson(ev, EventConsumer.class))); + } + return 0; + } + + @Override + public List popItems(String key, long count) throws Exception { + return null; + } + + @Override + public long getItemsCount(String key) throws Exception { + return 0; + } + + @Override + public boolean itemContains(String key, String item) throws Exception { + String value = getStorage(key); + if(value.equals(SEGMENT)){ + SegmentImp segmentImp = segmentStorage.get(key); + return segmentImp.contains(item); + } + return false; + } + + @Override + public void addItems(String key, List items) throws Exception { + + } + + @Override + public void removeItems(String key, List items) throws Exception { + + } + + @Override + public List getItems(List keys) throws Exception { + return null; + } + + @Override + public Set getMembers(String key) { + String storageKey = getStorage(key); + if(storageKey.equals(FLAG_SET)) { + return _flagSets.get(key); + } + return new HashSet<>(); + } + + @Override + public boolean connect() throws Exception { + return true; + } + + @Override + public boolean disconnect() throws Exception { + return false; + } + + @Override + public Pipeline pipeline() throws Exception { + return new NotPipelinedImpl(this); + } + + private String getStorage(String key) { + if(key.startsWith(SPLITS)) + return SPLITS; + else if(key.startsWith(SPLIT)) + return SPLIT; + else if(key.startsWith(RULE_BASED_SEGMENT)) + return RULE_BASED_SEGMENT; + else if(key.startsWith(RULE_BASED_SEGMENTS)) + return RULE_BASED_SEGMENTS; + else if (key.startsWith(LATENCIES)) + return LATENCIES; + else if (key.startsWith(TELEMETRY_INIT)) + return TELEMETRY_INIT; + else if(key.startsWith(TELEMETRY)) + return TELEMETRY; + else if(key.startsWith(SEGMENT)) + return SEGMENT; + else if(key.startsWith(COUNTS)) + return COUNTS; + else if(key.startsWith(IMPRESSIONS)) + return IMPRESSIONS; + else if(key.startsWith(EVENTS)) + return EVENTS; + else if(key.startsWith(FLAG_SET)) + return FLAG_SET; + return ""; + } + + private void updateCache(){ + Condition condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.WHITELIST,"segmentName" , Lists.newArrayList(ConditionsTestUtil.partition("on", 100))); + segmentStorage.put(PrefixAdapter.buildSegment("segmentName"), new SegmentImp(9874654L, "segmentName", Lists.newArrayList("key", "key2"))); + splitsStorage.put(PrefixAdapter.buildSplitKey("first-name"), makeSplit("first-name", 123, Lists.newArrayList(condition), 456478976L)); + splitsStorage.put(PrefixAdapter.buildSplitKey("second-name"), makeSplit("second-name", 321, Lists.newArrayList(), 568613L)); + splitsStorage.put(PrefixAdapter.buildSplitKey("rbs_flag"), Json.fromJson("{\"changeNumber\": 10, \"trafficTypeName\": \"user\", \"name\": \"rbs_flag\", \"trafficAllocation\": 100, \"trafficAllocationSeed\": 1828377380, \"seed\": -286617921, \"status\": \"ACTIVE\", \"killed\": false, \"defaultTreatment\": \"off\", \"algo\": 2, \"conditions\": [{\"conditionType\": \"ROLLOUT\", \"matcherGroup\": {\"combiner\": \"AND\", \"matchers\": [{\"keySelector\": {\"trafficType\": \"user\"},\"matcherType\": \"IN_RULE_BASED_SEGMENT\", \"negate\": false, \"userDefinedSegmentMatcherData\": {\"segmentName\": \"sample_rule_based_segment\"}}]},\"partitions\": [{\"treatment\": \"on\", \"size\": 100},{\"treatment\": \"off\", \"size\": 0}],\"label\": \"in rule based segment sample_rule_based_segment\"},{\"conditionType\": \"ROLLOUT\", \"matcherGroup\": {\"combiner\": \"AND\", \"matchers\": [{\"keySelector\": {\"trafficType\": \"user\"},\"matcherType\": \"ALL_KEYS\", \"negate\": false}]},\"partitions\": [{\"treatment\": \"on\", \"size\": 0},{\"treatment\": \"off\", \"size\": 100}],\"label\": \"default rule\"}],\"configurations\": {},\"sets\": [],\"impressionsDisabled\": false}", Split.class)); + ruleBasedSegmentStorage.put(PrefixAdapter.buildRuleBasedSegmentKey("sample_rule_based_segment"), Json.fromJson( "{\"changeNumber\":5,\"name\":\"sample_rule_based_segment\",\"status\":\"ACTIVE\",\"trafficTypeName\":\"user\",\"excluded\":{\"keys\":[\"mauro@split.io\"],\"segments\":[]},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":\"email\"},\"matcherType\":\"ENDS_WITH\",\"negate\":false,\"whitelistMatcherData\":{\"whitelist\":[\"@split.io\"]}}]}}]}", RuleBasedSegment.class)); + _flagSets.put("SPLITIO.flagSet.set1", new HashSet<>(new ArrayList<>(Arrays.asList("flag1", "flag2")))); + } + + private Split makeSplit(String name, int seed, List conditions, long changeNumber) { + Split split = new Split(); + split.name = name; + split.seed = seed; + split.trafficAllocation = 100; + split.trafficAllocationSeed = seed; + split.status = Status.ACTIVE; + split.conditions = conditions; + split.defaultTreatment = Treatments.OFF; + split.trafficTypeName = "user"; + split.changeNumber = changeNumber; + split.algo = 1; + split.configurations = null; + return split; + } + + public ConcurrentMap getLatencies() { + return _latencies; + } + + public ConcurrentMap getImpressionsCount() { + return _impressionsCount; + } + + public List getImps() { + return imps; + } + + public List getEvents() { + return events; + } + + public ConcurrentMap getConfig() { + return _config; + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducerTest.java new file mode 100644 index 00000000..bc22a42f --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomEventAdapterProducerTest.java @@ -0,0 +1,37 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.Event; +import io.split.client.dtos.Metadata; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class UserCustomEventAdapterProducerTest{ + private CustomStorageWrapper _customStorageWrapper; + private UserCustomEventAdapterProducer _eventAdapterProducer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _eventAdapterProducer = new UserCustomEventAdapterProducer(_customStorageWrapper, Mockito.mock(Metadata.class)); + Metadata metadata = new Metadata(true, "SDK-version"); + Field userCustomMetadata = UserCustomEventAdapterProducer.class.getDeclaredField("_metadata"); + userCustomMetadata.setAccessible(true); + Field modifiersFieldMetadata = Field.class.getDeclaredField("modifiers"); + modifiersFieldMetadata.setAccessible(true); + modifiersFieldMetadata.setInt(userCustomMetadata, userCustomMetadata.getModifiers() & ~Modifier.FINAL); + userCustomMetadata.set(_eventAdapterProducer, metadata); + } + + @Test + public void testTrack() { + Event event = new Event(); + Assert.assertTrue(_eventAdapterProducer.track(event,1)); + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducerTest.java new file mode 100644 index 00000000..2926d1ad --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomImpressionAdapterProducerTest.java @@ -0,0 +1,59 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.dtos.KeyImpression; +import io.split.client.dtos.Metadata; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class UserCustomImpressionAdapterProducerTest { + + private CustomStorageWrapper _customStorageWrapper; + private UserCustomImpressionAdapterProducer _impressionAdapterProducer; + private UserStorageWrapper _userStorageWrapper; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _impressionAdapterProducer = new UserCustomImpressionAdapterProducer(_customStorageWrapper, Mockito.mock(Metadata.class)); + Field userCustomImpressionAdapterProducer = UserCustomImpressionAdapterProducer.class.getDeclaredField("_userStorageWrapper"); + userCustomImpressionAdapterProducer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomImpressionAdapterProducer, userCustomImpressionAdapterProducer.getModifiers() & ~Modifier.FINAL); + userCustomImpressionAdapterProducer.set(_impressionAdapterProducer, _userStorageWrapper); + Metadata metadata = new Metadata(true, "SDK-version"); + Field userCustomMetadata = UserCustomImpressionAdapterProducer.class.getDeclaredField("_metadata"); + userCustomMetadata.setAccessible(true); + Field modifiersFieldMetadata = Field.class.getDeclaredField("modifiers"); + modifiersFieldMetadata.setAccessible(true); + modifiersFieldMetadata.setInt(userCustomMetadata, userCustomMetadata.getModifiers() & ~Modifier.FINAL); + userCustomMetadata.set(_impressionAdapterProducer, metadata); + } + + @Test + public void testPut() { + KeyImpression keyImpression = new KeyImpression(); + Mockito.when(_userStorageWrapper.pushItems(Mockito.anyString(), Mockito.anyObject())).thenReturn(1L); + Assert.assertEquals(1L, _impressionAdapterProducer.put(Stream.of(keyImpression).collect(Collectors.toList()))); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).pushItems(Mockito.anyString(), Mockito.anyObject()); + } + + @Test + public void testPutMany() { + KeyImpression keyImpression = new KeyImpression(); + Mockito.when(_userStorageWrapper.pushItems(Mockito.anyString(), Mockito.anyObject())).thenReturn(1L); + Assert.assertEquals(1L, _impressionAdapterProducer.put(Stream.of(keyImpression).collect(Collectors.toList()))); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).pushItems(Mockito.anyString(), Mockito.anyObject()); + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumerTest.java new file mode 100644 index 00000000..b9baee95 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomRuleBasedSegmentAdapterConsumerTest.java @@ -0,0 +1,188 @@ +package io.split.storages.pluggable.adapters; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import io.split.client.dtos.*; +import io.split.client.utils.Json; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.experiments.*; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.*; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static io.split.TestHelper.makeRuleBasedSegment; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class UserCustomRuleBasedSegmentAdapterConsumerTest { + + private static final String RULE_BASED_SEGMENT_NAME = "RuleBasedSegmentName"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomRuleBasedSegmentAdapterConsumer _userCustomRuleBasedSegmentAdapterConsumer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomRuleBasedSegmentAdapterConsumer = new UserCustomRuleBasedSegmentAdapterConsumer(_customStorageWrapper); + Field userCustomRuleBasedSegmentAdapterConsumer = UserCustomRuleBasedSegmentAdapterConsumer.class.getDeclaredField("_userStorageWrapper"); + userCustomRuleBasedSegmentAdapterConsumer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomRuleBasedSegmentAdapterConsumer, userCustomRuleBasedSegmentAdapterConsumer.getModifiers() & ~Modifier.FINAL); + userCustomRuleBasedSegmentAdapterConsumer.set(_userCustomRuleBasedSegmentAdapterConsumer, _userStorageWrapper); + } + + @Test + public void testGetChangeNumber() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentChangeNumber())).thenReturn(getLongAsJson(120L)); + Assert.assertEquals(120L, _userCustomRuleBasedSegmentAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentChangeNumber())).thenReturn(null); + Assert.assertEquals(-1L, _userCustomRuleBasedSegmentAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithGsonFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentChangeNumber())).thenReturn("a"); + Assert.assertEquals(-1L, _userCustomRuleBasedSegmentAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetRuleBasedSegment() { + RuleBasedSegmentParser ruleBasedSegmentParser = new RuleBasedSegmentParser(); + RuleBasedSegment ruleBasedSegment = getRuleBasedSegment(RULE_BASED_SEGMENT_NAME); + ParsedRuleBasedSegment expected = ruleBasedSegmentParser.parse(ruleBasedSegment); + ConcurrentMap rbsCollection = Maps.newConcurrentMap(); + rbsCollection.put(RULE_BASED_SEGMENT_NAME, expected); + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentKey(RULE_BASED_SEGMENT_NAME))).thenReturn(getRuleBasedSegmentAsJson(ruleBasedSegment)); + Mockito.when(_userStorageWrapper.getKeysByPrefix("SPLITIO.rbsegment*")).thenReturn(new HashSet<>(Arrays.asList(RULE_BASED_SEGMENT_NAME))); + ParsedRuleBasedSegment result = _userCustomRuleBasedSegmentAdapterConsumer.get(RULE_BASED_SEGMENT_NAME); + Assert.assertEquals(expected, result); + assertTrue(_userCustomRuleBasedSegmentAdapterConsumer.contains(new HashSet<>(Arrays.asList(RULE_BASED_SEGMENT_NAME)))); + assertFalse(_userCustomRuleBasedSegmentAdapterConsumer.contains(new HashSet<>(Arrays.asList(RULE_BASED_SEGMENT_NAME, "123")))); + + } + + @Test + public void testGetRuleBasedSegmentNotFound() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentKey(RULE_BASED_SEGMENT_NAME))).thenReturn(null); + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildRuleBasedSegmentKey(RULE_BASED_SEGMENT_NAME))).thenReturn(null); + ParsedRuleBasedSegment result = _userCustomRuleBasedSegmentAdapterConsumer.get(RULE_BASED_SEGMENT_NAME); + Assert.assertNull(result); + } + + @Test + public void testGetAll() { + RuleBasedSegment ruleBasedSegment = getRuleBasedSegment(RULE_BASED_SEGMENT_NAME); + RuleBasedSegment ruleBasedSegment2 = getRuleBasedSegment(RULE_BASED_SEGMENT_NAME+"2"); + List listResultExpected = Stream.of(ruleBasedSegment, ruleBasedSegment2).collect(Collectors.toList()); + Set keysResult = Stream.of(RULE_BASED_SEGMENT_NAME, RULE_BASED_SEGMENT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + List getManyExpected = Stream.of(Json.toJson(ruleBasedSegment), Json.toJson(ruleBasedSegment2)).collect(Collectors.toList()); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(getManyExpected); + List ruleBasedSegmentsResult = (List) _userCustomRuleBasedSegmentAdapterConsumer.getAll(); + Assert.assertNotNull(ruleBasedSegmentsResult); + Assert.assertEquals(listResultExpected.size(), ruleBasedSegmentsResult.size()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getMany(Mockito.anyObject()); + } + + @Test + public void testGetAllWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildGetAllSplit())). + thenReturn(null); + List ruleBasedSegmentsResult = (List) _userCustomRuleBasedSegmentAdapterConsumer.getAll(); + Assert.assertNotNull(ruleBasedSegmentsResult); + Assert.assertEquals(0, ruleBasedSegmentsResult.size()); + } + + @Test + public void testGetAllNullOnWrappers() { + Mockito.when(_userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllRuleBasedSegment())). + thenReturn(null); + List ruleBasedSegmentsResult = (List) _userCustomRuleBasedSegmentAdapterConsumer.getAll(); + Assert.assertEquals(0, ruleBasedSegmentsResult.size()); + } + + @Test + public void testGetAllNullOnGetMany() { + Set keysResult = Stream.of(RULE_BASED_SEGMENT_NAME, RULE_BASED_SEGMENT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(null); + List ruleBasedSegmentsResult = (List) _userCustomRuleBasedSegmentAdapterConsumer.getAll(); + Assert.assertEquals(0, ruleBasedSegmentsResult.size()); + } + + @Test + public void testGetSegments() { + Condition condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.WHITELIST, "employee", + null, false); + RuleBasedSegment ruleBasedSegment = makeRuleBasedSegment("rbs", Arrays.asList(condition), 1); + List getManyExpected = Stream.of(Json.toJson(ruleBasedSegment)).collect(Collectors.toList()); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(getManyExpected); + HashSet segmentResult = (HashSet) _userCustomRuleBasedSegmentAdapterConsumer.getSegments(); + assertTrue(segmentResult.contains("employee")); + } + + @Test + public void testGetruleBasedSegmentNames() { + RuleBasedSegment ruleBasedSegment = getRuleBasedSegment(RULE_BASED_SEGMENT_NAME); + RuleBasedSegment ruleBasedSegment2 = getRuleBasedSegment(RULE_BASED_SEGMENT_NAME+"2"); + Set keysResult = Stream.of(RULE_BASED_SEGMENT_NAME, RULE_BASED_SEGMENT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + List getManyExpected = Stream.of(Json.toJson(ruleBasedSegment), Json.toJson(ruleBasedSegment2)).collect(Collectors.toList()); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(getManyExpected); + List ruleBasedSegmentsResult = _userCustomRuleBasedSegmentAdapterConsumer.ruleBasedSegmentNames(); + Assert.assertNotNull(ruleBasedSegmentsResult); + Assert.assertEquals(keysResult.size(), ruleBasedSegmentsResult.size()); + Assert.assertEquals(keysResult, new HashSet<>(ruleBasedSegmentsResult)); + } + + public static String getLongAsJson(long value) { + return Json.toJson(value); + } + + public static String getRuleBasedSegmentAsJson(RuleBasedSegment ruleBasedSegment) { + return Json.toJson(ruleBasedSegment); + } + + private RuleBasedSegment getRuleBasedSegment(String name) { + ArrayList set = Lists.newArrayList("sms", "voice"); + Condition c = ConditionsTestUtil.containsString("user", + "products", + set, + false, + null + ); + + List conditions = Lists.newArrayList(c); + return makeRuleBasedSegment(name, conditions, 1); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumerTest.java new file mode 100644 index 00000000..a6ae6451 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterConsumerTest.java @@ -0,0 +1,76 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.utils.Json; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Collections; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class UserCustomSegmentAdapterConsumerTest { + + private static final String SEGMENT_NAME = "SegmentName"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomSegmentAdapterConsumer _userCustomSegmentAdapterConsumer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomSegmentAdapterConsumer = new UserCustomSegmentAdapterConsumer(_customStorageWrapper); + Field userCustomSegmentAdapterConsumer = UserCustomSegmentAdapterConsumer.class.getDeclaredField("_userStorageWrapper"); + userCustomSegmentAdapterConsumer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomSegmentAdapterConsumer, userCustomSegmentAdapterConsumer.getModifiers() & ~Modifier.FINAL); + userCustomSegmentAdapterConsumer.set(_userCustomSegmentAdapterConsumer, _userStorageWrapper); + } + + @Test + public void testGetChangeNumber() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSegment(SEGMENT_NAME))).thenReturn(Json.toJson(120L)); + Assert.assertEquals(120L, _userCustomSegmentAdapterConsumer.getChangeNumber(SEGMENT_NAME)); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testIsInSegment() { + Mockito.when(_userStorageWrapper.itemContains(Mockito.anyString(), Mockito.anyString())).thenReturn(true); + Assert.assertTrue(_userCustomSegmentAdapterConsumer.isInSegment(SEGMENT_NAME, "item")); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).itemContains(Mockito.anyString(), Mockito.anyString()); + } + + @Test + public void testGetSegmentCount() { + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyString())).thenReturn(Collections.singleton(SEGMENT_NAME)); + Assert.assertEquals(1, _userCustomSegmentAdapterConsumer.getSegmentCount()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + } + + @Test + public void testGetKeyCount() { + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyString())).thenReturn(Stream.of(SEGMENT_NAME, SEGMENT_NAME+"2").collect(Collectors.toSet())); + Mockito.when(_userStorageWrapper.getItemsCount(Mockito.anyString())).thenReturn(1L).thenReturn(3L); + Assert.assertEquals(4, _userCustomSegmentAdapterConsumer.getKeyCount()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(2)).getItemsCount(Mockito.anyString()); + } + + @Test + public void testGetKeyCountNullResponse() { + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyString())).thenReturn(null); + Mockito.when(_userStorageWrapper.getItemsCount(Mockito.anyString())).thenReturn(1L).thenReturn(3L); + Assert.assertEquals(0, _userCustomSegmentAdapterConsumer.getKeyCount()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(0)).getItemsCount(Mockito.anyString()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducerTest.java new file mode 100644 index 00000000..3adcf9b3 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSegmentAdapterProducerTest.java @@ -0,0 +1,56 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.utils.Json; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; + +public class UserCustomSegmentAdapterProducerTest { + + private static final String SEGMENT_NAME = "SegmentName"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomSegmentAdapterProducer _userCustomSegmentAdapterProducer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomSegmentAdapterProducer = new UserCustomSegmentAdapterProducer(_customStorageWrapper); + Field userCustomSegmentAdapterProducer = UserCustomSegmentAdapterProducer.class.getDeclaredField("_userStorageWrapper"); + userCustomSegmentAdapterProducer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomSegmentAdapterProducer, userCustomSegmentAdapterProducer.getModifiers() & ~Modifier.FINAL); + userCustomSegmentAdapterProducer.set(_userCustomSegmentAdapterProducer, _userStorageWrapper); + } + + @Test + public void testGetChangeNumber() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSegment(SEGMENT_NAME))).thenReturn(Json.toJson(120L)); + Assert.assertEquals(120L, _userCustomSegmentAdapterProducer.getChangeNumber(SEGMENT_NAME)); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testUpdateSegment() { + _userCustomSegmentAdapterProducer.updateSegment(SEGMENT_NAME, new ArrayList<>(), new ArrayList<>(), 12L); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).addItems(Mockito.anyString(), Mockito.anyObject()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).removeItems(Mockito.anyString(), Mockito.anyObject()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).set(Mockito.anyString(), Mockito.anyString()); + } + + @Test + public void testSetChangeNumber() { + _userCustomSegmentAdapterProducer.setChangeNumber(SEGMENT_NAME, 1L); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).set(Mockito.anyString(), Mockito.anyString()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumerTest.java new file mode 100644 index 00000000..befe9645 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterConsumerTest.java @@ -0,0 +1,280 @@ +package io.split.storages.pluggable.adapters; + +import com.google.common.collect.Lists; +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.Status; +import io.split.client.utils.Json; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.experiments.ParsedSplit; +import io.split.engine.experiments.SplitParser; +import io.split.grammar.Treatments; +import io.split.storages.pluggable.CustomStorageWrapperHasPipeline; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class UserCustomSplitAdapterConsumerTest { + + private static final String SPLIT_NAME = "SplitName"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomSplitAdapterConsumer _userCustomSplitAdapterConsumer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomSplitAdapterConsumer = new UserCustomSplitAdapterConsumer(_customStorageWrapper); + Field userCustomSplitAdapterConsumer = UserCustomSplitAdapterConsumer.class.getDeclaredField("_userStorageWrapper"); + userCustomSplitAdapterConsumer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomSplitAdapterConsumer, userCustomSplitAdapterConsumer.getModifiers() & ~Modifier.FINAL); + userCustomSplitAdapterConsumer.set(_userCustomSplitAdapterConsumer, _userStorageWrapper); + } + + @Test + public void testGetChangeNumber() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn(getLongAsJson(120L)); + Assert.assertEquals(120L, _userCustomSplitAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn(null); + Assert.assertEquals(-1L, _userCustomSplitAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithGsonFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn("a"); + Assert.assertEquals(-1L, _userCustomSplitAdapterConsumer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetSplit() { + SplitParser splitParser = new SplitParser(); + Split split = getSplit(SPLIT_NAME); + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))).thenReturn(getSplitAsJson(split)); + ParsedSplit actual = _userCustomSplitAdapterConsumer.get(SPLIT_NAME); + ParsedSplit expected = splitParser.parse(split); + Assert.assertEquals(actual.getRuleBasedSegmentsNames(), expected.getRuleBasedSegmentsNames()); + Assert.assertEquals(actual.seed(), expected.seed()); + Assert.assertEquals(actual.algo(), expected.algo()); + Assert.assertEquals(actual.trafficAllocationSeed(), expected.trafficAllocationSeed()); + Assert.assertEquals(actual.flagSets(), expected.flagSets()); + Assert.assertEquals(actual.parsedConditions(), expected.parsedConditions()); + Assert.assertEquals(actual.trafficAllocation(), expected.trafficAllocation()); + Assert.assertEquals(actual.getSegmentsNames(), expected.getSegmentsNames()); + Assert.assertEquals(actual.impressionsDisabled(), expected.impressionsDisabled()); + Assert.assertEquals(actual.killed(), expected.killed()); + Assert.assertEquals(actual.defaultTreatment(), expected.defaultTreatment()); + Assert.assertEquals(actual.changeNumber(), expected.changeNumber()); + Assert.assertEquals(actual.feature(), expected.feature()); + Assert.assertEquals(actual.configurations(), expected.configurations()); + Assert.assertEquals(actual.prerequisitesMatcher().toString(), expected.prerequisitesMatcher().toString()); + } + + @Test + public void testGetSplitNotFound() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))).thenReturn(null); + ParsedSplit result = _userCustomSplitAdapterConsumer.get(SPLIT_NAME); + Assert.assertNull(result); + } + + @Test + public void testGetAll() { + Split split = getSplit(SPLIT_NAME); + Split split2 = getSplit(SPLIT_NAME+"2"); + List listResultExpected = Stream.of(split, split2).collect(Collectors.toList()); + Set keysResult = Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + List getManyExpected = Stream.of(Json.toJson(split), Json.toJson(split2)).collect(Collectors.toList()); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(getManyExpected); + List splitsResult = (List) _userCustomSplitAdapterConsumer.getAll(); + Assert.assertNotNull(splitsResult); + Assert.assertEquals(listResultExpected.size(), splitsResult.size()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getMany(Mockito.anyObject()); + } + + @Test + public void testGetSplitNames() { + Split split = getSplit(SPLIT_NAME); + Split split2 = getSplit(SPLIT_NAME+"2"); + List listResultExpected = Stream.of(split, split2).collect(Collectors.toList()); + Set keysResult = Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + List splitsResult = _userCustomSplitAdapterConsumer.splitNames(); + Assert.assertNotNull(splitsResult); + Assert.assertEquals(listResultExpected.size(), splitsResult.size()); + Assert.assertEquals(SPLIT_NAME, splitsResult.get(1)); + Assert.assertEquals(SPLIT_NAME+"2", splitsResult.get(0)); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).getKeysByPrefix(Mockito.anyString()); + + // default prefix + listResultExpected = Stream.of(split, split2).collect(Collectors.toList()); + keysResult = Stream.of("SPLITIO.split." + SPLIT_NAME, "SPLITIO.split." + SPLIT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + + splitsResult = _userCustomSplitAdapterConsumer.splitNames(); + Assert.assertNotNull(splitsResult); + Assert.assertEquals(listResultExpected.size(), splitsResult.size()); + Assert.assertEquals(SPLIT_NAME, splitsResult.get(1)); + Assert.assertEquals(SPLIT_NAME+"2", splitsResult.get(0)); + } + + @Test + public void testGetAllWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildGetAllSplit())). + thenReturn(null); + List splitsResult = (List) _userCustomSplitAdapterConsumer.getAll(); + Assert.assertNotNull(splitsResult); + Assert.assertEquals(0, splitsResult.size()); + } + + @Test + public void testGetAllNullOnWrappers() { + Mockito.when(_userStorageWrapper.getKeysByPrefix(PrefixAdapter.buildGetAllSplit())). + thenReturn(null); + List splitsResult = (List) _userCustomSplitAdapterConsumer.getAll(); + Assert.assertEquals(0, splitsResult.size()); + } + + @Test + public void testGetAllNullOnGetMany() { + Set keysResult = Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toSet()); + Mockito.when(_userStorageWrapper.getKeysByPrefix(Mockito.anyObject())). + thenReturn(keysResult); + Mockito.when(_userStorageWrapper.getMany(Mockito.anyObject())). + thenReturn(null); + List splitsResult = (List) _userCustomSplitAdapterConsumer.getAll(); + Assert.assertEquals(0, splitsResult.size()); + } + + + @Test + public void testTrafficTypeExists() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists("TrafficType"))). + thenReturn(getLongAsJson(2)); + boolean result = _userCustomSplitAdapterConsumer.trafficTypeExists("TrafficType"); + Assert.assertTrue(result); + } + + @Test + public void testTrafficTypeExistsWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists("TrafficType"))). + thenReturn(null); + boolean result = _userCustomSplitAdapterConsumer.trafficTypeExists("TrafficType"); + Assert.assertFalse(result); + } + + @Test + public void testTrafficTypeExistsWithGsonFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildTrafficTypeExists("TrafficType"))). + thenReturn("true"); + boolean result = _userCustomSplitAdapterConsumer.trafficTypeExists("TrafficType"); + Assert.assertFalse(result); + } + + @Test + public void testFetchMany() { + Split split = getSplit(SPLIT_NAME); + Split split2 = getSplit(SPLIT_NAME+"2"); + List listResultExpected = Stream.of(Json.toJson(split), Json.toJson(split2)).collect(Collectors.toList()); + Mockito.when(_userStorageWrapper.getItems(PrefixAdapter.buildFetchManySplits(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())))). + thenReturn(listResultExpected); + Map splitsResult = _userCustomSplitAdapterConsumer.fetchMany(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())); + Assert.assertNotNull(splitsResult); + Assert.assertEquals(2, splitsResult.keySet().size()); + } + + @Test + public void testFetchManyWithWrapperFailing() { + Mockito.when(_userStorageWrapper.getItems(PrefixAdapter.buildFetchManySplits(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())))). + thenReturn(null); + Map splitsResult = _userCustomSplitAdapterConsumer.fetchMany(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())); + Assert.assertNotNull(splitsResult); + Assert.assertNull(splitsResult.get(SPLIT_NAME)); + Assert.assertNull(splitsResult.get(SPLIT_NAME+"2")); + } + + @Test + public void testFetchManyNotFound() { + Mockito.when(_userStorageWrapper.getItems(PrefixAdapter.buildFetchManySplits(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())))). + thenReturn(null); + Map splitsResult = _userCustomSplitAdapterConsumer.fetchMany(Stream.of(SPLIT_NAME, SPLIT_NAME+"2").collect(Collectors.toList())); + Assert.assertNotNull(splitsResult); + Assert.assertNull(splitsResult.get(SPLIT_NAME)); + Assert.assertNull(splitsResult.get(SPLIT_NAME+"2")); + } + + @Test + public void testGetNamesByFlagSets() { + CustomStorageWrapper customStorageWrapper = new CustomStorageWrapperHasPipeline(); + UserCustomSplitAdapterConsumer userCustomSplitAdapterConsumer = new UserCustomSplitAdapterConsumer(customStorageWrapper); + Map> flagSets = userCustomSplitAdapterConsumer.getNamesByFlagSets(new ArrayList<>(Arrays.asList("set1"))); + Assert.assertEquals(2, flagSets.get("set1").size()); + } + + @Test + public void testGetSegments() { + //NoOp + UserCustomSplitAdapterConsumer userCustomSplitAdapterConsumer = new UserCustomSplitAdapterConsumer(_customStorageWrapper); + Assert.assertEquals(0, userCustomSplitAdapterConsumer.getSegments().size()); + } + + public static String getLongAsJson(long value) { + return Json.toJson(value); + } + + public static String getSplitAsJson(Split split) { + return Json.toJson(split); + } + + public static String getListOfSplitsAsJson(List splitList) { + return Json.toJson(splitList); + } + + public static String getBooleanAsJson(boolean result) { + return Json.toJson(result); + } + + private Split getSplit(String name) { + Condition condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.ROLLOUT, "Segment", Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); + Split split = new Split(); + split.status = Status.ACTIVE; + split.trafficAllocation = 100; + split.trafficAllocationSeed = (int) 12L; + split.seed = (int) 12L; + split.conditions = Lists.newArrayList(condition); + split.name = name; + split.defaultTreatment = Treatments.OFF; + split.changeNumber = 12L; + return split; + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducerTest.java new file mode 100644 index 00000000..ba80577d --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomSplitAdapterProducerTest.java @@ -0,0 +1,180 @@ +package io.split.storages.pluggable.adapters; + +import com.google.common.collect.Lists; +import io.split.client.dtos.Condition; +import io.split.client.dtos.ConditionType; +import io.split.client.dtos.Split; +import io.split.client.dtos.Status; +import io.split.engine.ConditionsTestUtil; +import io.split.engine.experiments.ParsedSplit; +import io.split.engine.experiments.SplitParser; +import io.split.grammar.Treatments; +import io.split.storages.pluggable.domain.PrefixAdapter; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class UserCustomSplitAdapterProducerTest{ + + private static final String SPLIT_NAME = "SplitName"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomSplitAdapterProducer _userCustomSplitAdapterProducer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomSplitAdapterProducer = new UserCustomSplitAdapterProducer(_customStorageWrapper); + Field userCustomSplitAdapterProducer = UserCustomSplitAdapterProducer.class.getDeclaredField("_userStorageWrapper"); + userCustomSplitAdapterProducer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomSplitAdapterProducer, userCustomSplitAdapterProducer.getModifiers() & ~Modifier.FINAL); + userCustomSplitAdapterProducer.set(_userCustomSplitAdapterProducer, _userStorageWrapper); + } + + @Test + public void testGetChangeNumber() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn(UserCustomSplitAdapterConsumerTest.getLongAsJson(120L)); + Assert.assertEquals(120L, _userCustomSplitAdapterProducer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn(null); + Assert.assertEquals(-1L, _userCustomSplitAdapterProducer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testGetChangeNumberWithGsonFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitChangeNumber())).thenReturn("a"); + Assert.assertEquals(-1L, _userCustomSplitAdapterProducer.getChangeNumber()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testRemove() { + Split split = getSplit(SPLIT_NAME); + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))) + .thenReturn(UserCustomSplitAdapterConsumerTest.getSplitAsJson(split)); + Mockito.when(_userStorageWrapper.decrement(Mockito.anyString(), Mockito.anyLong())) + .thenReturn(0L); + _userCustomSplitAdapterProducer.remove(SPLIT_NAME); + Mockito.verify(_userStorageWrapper, Mockito.times(2)).delete(Mockito.anyObject()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).decrement(Mockito.anyObject(), Mockito.anyLong()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testRemoveWithNoDelete() { + Split split = getSplit(SPLIT_NAME); + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))) + .thenReturn(UserCustomSplitAdapterConsumerTest.getSplitAsJson(split)); + Mockito.when(_userStorageWrapper.decrement(Mockito.anyString(), Mockito.anyLong())) + .thenReturn(1L); + _userCustomSplitAdapterProducer.remove(SPLIT_NAME); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).delete(Mockito.anyObject()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).decrement(Mockito.anyObject(), Mockito.anyLong()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testRemoveWithWrapperFailing() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))) + .thenReturn(null); + Assert.assertFalse(_userCustomSplitAdapterProducer.remove(SPLIT_NAME)); + Mockito.verify(_userStorageWrapper, Mockito.times(0)).delete(Mockito.anyObject()); + Mockito.verify(_userStorageWrapper, Mockito.times(0)).decrement(Mockito.anyObject(), Mockito.anyLong()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + } + + @Test + public void testSetChangeNumber() { + _userCustomSplitAdapterProducer.setChangeNumber(1L); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).set(Mockito.anyString(), Mockito.anyString()); + } + + @Test + public void testKill() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))).thenReturn(UserCustomSplitAdapterConsumerTest.getSplitAsJson(getSplit(SPLIT_NAME))); + _userCustomSplitAdapterProducer.kill(SPLIT_NAME, "DefaultTreatment", 2L); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).set(Mockito.anyString(), Mockito.anyString()); + } + + @Test + public void testKillSplitNotFound() { + Mockito.when(_userStorageWrapper.get(PrefixAdapter.buildSplitKey(SPLIT_NAME))).thenReturn(null); + _userCustomSplitAdapterProducer.kill(SPLIT_NAME, "DefaultTreatment", 2L); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).get(Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(0)).set(Mockito.anyString(), Mockito.anyString()); + } + + @Test + public void testPutMany() { + SplitParser splitParser = new SplitParser(); + ParsedSplit parsedSplit = splitParser.parse(getSplit(SPLIT_NAME)); + ParsedSplit parsedSplit2 = splitParser.parse(getSplit(SPLIT_NAME+"2")); + _userCustomSplitAdapterProducer.putMany(Stream.of(parsedSplit, parsedSplit2).collect(Collectors.toList())); + Mockito.verify(_userStorageWrapper, Mockito.times(2)).set(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(_userStorageWrapper, Mockito.times(2)).increment(Mockito.anyString(), Mockito.anyLong()); + + } + + @Test + public void testIncreaseTrafficType() { + _userCustomSplitAdapterProducer.increaseTrafficType("TrafficType"); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).increment(PrefixAdapter.buildTrafficTypeExists("TrafficType"), 1); + } + + @Test + public void testDecreaseTrafficType() { + _userCustomSplitAdapterProducer.decreaseTrafficType("TrafficType"); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).decrement(PrefixAdapter.buildTrafficTypeExists("TrafficType"), 1); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).delete(Mockito.anyObject()); + + } + + @Test + public void testClear() { + //Noop + UserCustomSplitAdapterProducer userCustomSplitAdapterProducer = new UserCustomSplitAdapterProducer(_customStorageWrapper); + userCustomSplitAdapterProducer.clear(); + } + + @Test + public void testGetSegments() { + //NoOp + UserCustomSplitAdapterProducer userCustomSplitAdapterProducer = new UserCustomSplitAdapterProducer(_customStorageWrapper); + Assert.assertEquals(0, userCustomSplitAdapterProducer.getSegments().size()); + } + + + + private Split getSplit(String name) { + Condition condition = ConditionsTestUtil.makeUserDefinedSegmentCondition(ConditionType.ROLLOUT, "Segment", Lists.newArrayList(ConditionsTestUtil.partition("on", 10))); + Split split = new Split(); + split.status = Status.ACTIVE; + split.trafficAllocation = 100; + split.trafficAllocationSeed = (int) 12L; + split.seed = (int) 12L; + split.conditions = Lists.newArrayList(condition); + split.name = name; + split.defaultTreatment = Treatments.OFF; + split.changeNumber = 12L; + split.trafficTypeName = "TrafficType"; + return split; + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducerTest.java b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducerTest.java new file mode 100644 index 00000000..b6aeca5e --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/adapters/UserCustomTelemetryAdapterProducerTest.java @@ -0,0 +1,51 @@ +package io.split.storages.pluggable.adapters; + +import io.split.client.utils.SDKMetadata; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import io.split.telemetry.domain.enums.MethodEnum; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class UserCustomTelemetryAdapterProducerTest { + + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private UserCustomTelemetryAdapterProducer _userCustomTelemetryAdapterProducer; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + _userCustomTelemetryAdapterProducer = new UserCustomTelemetryAdapterProducer(_customStorageWrapper, Mockito.mock(SDKMetadata.class)); + Field userCustomTelemetryAdapterProducer = UserCustomTelemetryAdapterProducer.class.getDeclaredField("_userStorageWrapper"); + userCustomTelemetryAdapterProducer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userCustomTelemetryAdapterProducer, userCustomTelemetryAdapterProducer.getModifiers() & ~Modifier.FINAL); + userCustomTelemetryAdapterProducer.set(_userCustomTelemetryAdapterProducer, _userStorageWrapper); + + } + + @Test + public void testRecordLatency() { + _userCustomTelemetryAdapterProducer.recordLatency(MethodEnum.TRACK, 10l); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).hIncrement(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong()); + } + + @Test + public void testRecordExceptionTreatmentByFlagSet() { + _userCustomTelemetryAdapterProducer.recordException(MethodEnum.TREATMENTS_BY_FLAG_SET); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).hIncrement(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong()); + } + + @Test + public void testRecordException() { + _userCustomTelemetryAdapterProducer.recordException(MethodEnum.TRACK); + Mockito.verify(_userStorageWrapper, Mockito.times(1)).hIncrement(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/domain/PrefixAdapterTest.java b/client/src/test/java/io/split/storages/pluggable/domain/PrefixAdapterTest.java new file mode 100644 index 00000000..8112e3fe --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/domain/PrefixAdapterTest.java @@ -0,0 +1,91 @@ +package io.split.storages.pluggable.domain; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class PrefixAdapterTest{ + + private static final String SPLIT_NAME = "SplitName"; + private static final String TRAFFIC_TYPE = "TrafficType"; + private static final String DEFAULT_PREFIX = "SPLITIO."; + private static final String SPLIT_PREFIX = "split."; + private static final String SPLITS_PREFIX = "splits."; + private static final String TRAFFIC_TYPE_PREFIX = "trafficType."; + private static final String EVENTS = "events"; + private static final String IMPRESSIONS = "impressions"; + private static final String SEGMENT = "segment." ; + + @Test + public void testBuildSplitKey() { + String prefix = PrefixAdapter.buildSplitKey(SPLIT_NAME); + String expectedPrefix = String.format(DEFAULT_PREFIX+SPLIT_PREFIX+"%s", SPLIT_NAME); + Assert.assertEquals(expectedPrefix,prefix); + } + + @Test + public void testBuildSplitGetChangeNumber() { + String prefix = PrefixAdapter.buildSplitChangeNumber(); + String expectedPrefix = DEFAULT_PREFIX+ SPLITS_PREFIX +"till"; + Assert.assertEquals(expectedPrefix,prefix); + } + + @Test + public void testBuildGetAllSplit() { + String prefix = PrefixAdapter.buildGetAllSplit(); + String expectedPrefix = DEFAULT_PREFIX+ "split.*"; + Assert.assertEquals(expectedPrefix,prefix); + } + + @Test + public void testBuildTrafficTypeExists() { + String prefix = PrefixAdapter.buildTrafficTypeExists(TRAFFIC_TYPE); + String expectedPrefix = String.format(DEFAULT_PREFIX+TRAFFIC_TYPE_PREFIX+"%s", TRAFFIC_TYPE); + Assert.assertEquals(expectedPrefix,prefix); + } + + @Test + public void testBuildFetchManySplits() { + List prefixes = PrefixAdapter.buildFetchManySplits(Stream.of(SPLIT_NAME+"1", SPLIT_NAME+"2").collect(Collectors.toList())); + String expectedPrefix1 = String.format(DEFAULT_PREFIX+ SPLIT_PREFIX +"%s", SPLIT_NAME+"1"); + String expectedPrefix2 = String.format(DEFAULT_PREFIX+ SPLIT_PREFIX +"%s", SPLIT_NAME+"2"); + Assert.assertEquals(2,prefixes.size()); + Assert.assertEquals(expectedPrefix1,prefixes.get(0)); + Assert.assertEquals(expectedPrefix2,prefixes.get(1)); + + } + + @Test + public void testBuildEvents() { + String expectedPrefix = DEFAULT_PREFIX + EVENTS; + Assert.assertEquals(expectedPrefix, PrefixAdapter.buildEvent()); + } + + @Test + public void testBuildImpressions() { + String expectedPrefix = DEFAULT_PREFIX + IMPRESSIONS; + Assert.assertEquals(expectedPrefix, PrefixAdapter.buildImpressions()); + } + + @Test + public void testBuildSegments() { + String expectedPrefix = DEFAULT_PREFIX + SEGMENT + "*"; + Assert.assertEquals(expectedPrefix, PrefixAdapter.buildSegmentAll()); + } + + @Test + public void testBuildTelemetryLatencies() { + String expectedPrefix = "SPLITIO.telemetry.latencies"; + Assert.assertEquals(expectedPrefix, PrefixAdapter.buildTelemetryLatenciesPrefix()); + } + + @Test + public void testBuildTelemetryExceptions() { + String expectedPrefix = "SPLITIO.telemetry.exceptions"; + Assert.assertEquals(expectedPrefix, PrefixAdapter.buildTelemetryExceptionsPrefix()); + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/domain/UserPipelineWrapperTest.java b/client/src/test/java/io/split/storages/pluggable/domain/UserPipelineWrapperTest.java new file mode 100644 index 00000000..eaa7561b --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/domain/UserPipelineWrapperTest.java @@ -0,0 +1,54 @@ +package io.split.storages.pluggable.domain; + +import io.split.storages.pluggable.CustomStorageWrapperHasPipeline; +import io.split.storages.pluggable.CustomStorageWrapperImp; +import org.junit.Assert; +import org.junit.Test; +import pluggable.NotPipelinedImpl; +import pluggable.Result; + +import java.util.List; +import java.util.Optional; + +public class UserPipelineWrapperTest { + private static final String KEY = "SPLITIO.impressions.counts"; + private static final String SET_KET = "SPLITIO.flagSet"; + private static final String HASH_COUNT_KEY = "countKey"; + + @Test + public void testHincrementWithPipeline() throws Exception { + CustomStorageWrapperHasPipeline customStorageWrapper = new CustomStorageWrapperHasPipeline(); + UserPipelineWrapper userPipelineWrapper = new UserPipelineWrapper(customStorageWrapper.pipeline()); + userPipelineWrapper.hIncrement(KEY, HASH_COUNT_KEY, 1); + List results = userPipelineWrapper.exec(); + Assert.assertEquals(Optional.of(1L), results.get(0).asLong()); + } + + @Test + public void testHincrementWithoutPipeline() throws Exception { + CustomStorageWrapperImp customStorageWrapper = new CustomStorageWrapperImp(); + NotPipelinedImpl notPipelined = new NotPipelinedImpl(customStorageWrapper); + UserPipelineWrapper userPipelineWrapper = new UserPipelineWrapper(notPipelined); + userPipelineWrapper.hIncrement(KEY, HASH_COUNT_KEY, 1); + List results = userPipelineWrapper.exec(); + Assert.assertEquals(Optional.of(1L), results.get(0).asLong()); + } + + @Test + public void testGetMembersWithPipeline() throws Exception { + CustomStorageWrapperHasPipeline customStorageWrapper = new CustomStorageWrapperHasPipeline(); + UserPipelineWrapper userPipelineWrapper = new UserPipelineWrapper(customStorageWrapper.pipeline()); + userPipelineWrapper.getMembers(SET_KET + ".set1"); + List results = userPipelineWrapper.exec(); + Assert.assertEquals(2, results.get(0).asHash().get().size()); + } + + @Test + public void testGetMembersWithoutPipeline() throws Exception { + CustomStorageWrapperImp customStorageWrapper = new CustomStorageWrapperImp(); + NotPipelinedImpl notPipelined = new NotPipelinedImpl(customStorageWrapper); + notPipelined.getMembers(SET_KET + ".set1"); + List results = notPipelined.exec(); + Assert.assertEquals(2, results.get(0).asHash().get().size()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/storages/pluggable/domain/UserStorageWrapperTest.java b/client/src/test/java/io/split/storages/pluggable/domain/UserStorageWrapperTest.java new file mode 100644 index 00000000..709d9f03 --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/domain/UserStorageWrapperTest.java @@ -0,0 +1,309 @@ +package io.split.storages.pluggable.domain; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class UserStorageWrapperTest { + + private static final String KEY = "KEY"; + private static final String RESPONSE = "Response"; + private static final String HASH_COUNT_KEY = "countKey"; + private static final String ITEM = "Item"; + private CustomStorageWrapper _customStorageWrapper; + private UserStorageWrapper _userStorageWrapper; + private Logger _log; + + @Before + public void setUp() throws NoSuchFieldException, IllegalAccessException { + _customStorageWrapper = Mockito.mock(CustomStorageWrapper.class); + _log = Mockito.mock(Logger.class); + _userStorageWrapper = new UserStorageWrapper(_customStorageWrapper); + Field userStorageWrapper = UserStorageWrapper.class.getDeclaredField("_log"); + userStorageWrapper.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(userStorageWrapper, userStorageWrapper.getModifiers() & ~Modifier.FINAL); + userStorageWrapper.set(_userStorageWrapper, _log); + } + + @Test + public void testGet() throws Exception { + Mockito.when(_customStorageWrapper.get(Mockito.anyString())).thenReturn(RESPONSE); + String result = _userStorageWrapper.get(KEY); + Assert.assertNotNull(result); + Assert.assertEquals(RESPONSE, result); + } + + @Test + public void testGetException() throws Exception { + Mockito.when(_customStorageWrapper.get(Mockito.anyString())).thenThrow(Exception.class); + String result = _userStorageWrapper.get(KEY); + Assert.assertNull(result); + } + + @Test + public void testGetMany() throws Exception { + Mockito.when(_customStorageWrapper.getMany(Mockito.anyObject())).thenReturn(Stream.of(RESPONSE).collect(Collectors.toList())); + List result = _userStorageWrapper.getMany(Stream.of(KEY).collect(Collectors.toList())); + Assert.assertNotNull(result); + Assert.assertEquals(1, result.size()); + Assert.assertEquals(RESPONSE, result.get(0)); + } + + @Test + public void testGetManyException() throws Exception { + Mockito.when(_customStorageWrapper.getMany(Mockito.anyObject())).thenThrow(Exception.class); + List result = _userStorageWrapper.getMany(Stream.of(KEY).collect(Collectors.toList())); + Assert.assertNull(result); + } + + @Test + public void testSet() { + + _userStorageWrapper.set(KEY, ITEM); + Mockito.verify(_log, Mockito.times(0)).error(Mockito.anyString()); + } + + @Test + public void testSetException() throws Exception { + Mockito.doThrow(Exception.class).when(_customStorageWrapper).set(Mockito.anyString(), Mockito.anyString()); + _userStorageWrapper.set(KEY, ITEM); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testDelete() { + _userStorageWrapper.delete(Stream.of(KEY).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(0)).error(Mockito.anyString()); + } + + @Test + public void testDeleteException() throws Exception { + Mockito.doThrow(Exception.class).when(_customStorageWrapper).delete(Mockito.anyObject()); + _userStorageWrapper.delete(Stream.of(KEY).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testGetAndSet() throws Exception { + Mockito.when(_customStorageWrapper.getAndSet(Mockito.anyString(), Mockito.anyObject())).thenReturn(RESPONSE); + String result = _userStorageWrapper.getAndSet(KEY, ITEM); + Assert.assertNotNull(result); + Assert.assertEquals(RESPONSE, result); + } + + @Test + public void testGetAndSetException() throws Exception { + Mockito.when(_customStorageWrapper.getAndSet(Mockito.anyString(), Mockito.anyObject())).thenThrow(Exception.class); + String result = _userStorageWrapper.getAndSet(KEY, ITEM); + Assert.assertNull(result); + } + + @Test + public void testGetKeysByPrefix() throws Exception { + Set response =new HashSet<>(); + response.add(RESPONSE); + Mockito.when(_customStorageWrapper.getKeysByPrefix(Mockito.anyString())).thenReturn(response); + Set result = _userStorageWrapper.getKeysByPrefix(KEY); + Assert.assertNotNull(result); + Assert.assertTrue(result.contains(RESPONSE)); + } + + @Test + public void testGetKeysByPrefixException() throws Exception { + Mockito.when(_customStorageWrapper.getKeysByPrefix(Mockito.anyString())).thenThrow(Exception.class); + Set result = _userStorageWrapper.getKeysByPrefix(KEY); + Assert.assertNull(result); + } + + @Test + public void testIncrement() throws Exception { + long response = 2L; + Mockito.when(_customStorageWrapper.increment(Mockito.anyString(), Mockito.anyLong())).thenReturn(response); + long result = _userStorageWrapper.increment(KEY, 1); + Assert.assertEquals(response, result); + } + + @Test + public void testIncrementException() throws Exception { + Mockito.when(_customStorageWrapper.increment(Mockito.anyString(), Mockito.anyLong())).thenThrow(Exception.class); + long result = _userStorageWrapper.increment(KEY, 1); + Assert.assertEquals(0L, result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testHIncrement() throws Exception { + long response = 2L; + Mockito.when(_customStorageWrapper.hIncrement(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong())).thenReturn(response); + long result = _userStorageWrapper.hIncrement(KEY, HASH_COUNT_KEY,1); + Assert.assertEquals(response, result); + } + + @Test + public void testHIncrementException() throws Exception { + Mockito.when(_customStorageWrapper.hIncrement(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong())).thenThrow(Exception.class); + long result = _userStorageWrapper.hIncrement(KEY, HASH_COUNT_KEY, 1); + Assert.assertEquals(0L, result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testDecrement() throws Exception { + long response = 2L; + Mockito.when(_customStorageWrapper.decrement(Mockito.anyString(), Mockito.anyLong())).thenReturn(response); + long result = _userStorageWrapper.decrement(KEY, 1); + Assert.assertEquals(response, result); + } + + @Test + public void testDecrementException() throws Exception { + Mockito.when(_customStorageWrapper.decrement(Mockito.anyString(), Mockito.anyLong())).thenThrow(Exception.class); + long result = _userStorageWrapper.decrement(KEY, 1); + Assert.assertEquals(0L, result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testPushItems() { + _userStorageWrapper.pushItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(0)).error(Mockito.anyString()); + } + + @Test + public void testPushItemsException() throws Exception { + Mockito.doThrow(Exception.class).when(_customStorageWrapper).pushItems(Mockito.anyString(), Mockito.anyObject()); + _userStorageWrapper.pushItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testPopItems() throws Exception { + Mockito.when(_customStorageWrapper.popItems(Mockito.anyString(), Mockito.anyLong())) + .thenReturn(Stream.of(RESPONSE).collect(Collectors.toList())); + List result = _userStorageWrapper.popItems(KEY, 1L); + Assert.assertNotNull(result); + Assert.assertEquals(RESPONSE, result.get(0)); + } + + @Test + public void testPopItemsException() throws Exception { + Mockito.when(_customStorageWrapper.popItems(Mockito.anyString(), Mockito.anyLong())).thenThrow(Exception.class); + List result = _userStorageWrapper.popItems(KEY, 1L); + Assert.assertNull(result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testGetItemsCount() throws Exception { + long response = 2L; + Mockito.when(_customStorageWrapper.getItemsCount(Mockito.anyString())).thenReturn(response); + long result = _userStorageWrapper.getItemsCount(KEY); + Assert.assertEquals(response, result); + } + + @Test + public void testGetItemsCountException() throws Exception { + Mockito.when(_customStorageWrapper.getItemsCount(Mockito.anyString())).thenThrow(Exception.class); + long result = _userStorageWrapper.getItemsCount(KEY); + Assert.assertEquals(-1L, result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testItemContains() throws Exception { + Mockito.when(_customStorageWrapper.itemContains(Mockito.anyString(), Mockito.anyString())).thenReturn(true); + boolean result = _userStorageWrapper.itemContains(KEY, ITEM); + Assert.assertTrue(result); + } + + @Test + public void testItemContainsException() throws Exception { + Mockito.when(_customStorageWrapper.itemContains(Mockito.anyString(), Mockito.anyString())).thenThrow(Exception.class); + boolean result = _userStorageWrapper.itemContains(KEY, ITEM); + Assert.assertFalse(result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testAddItems() { + _userStorageWrapper.addItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(0)).error(Mockito.anyString()); + } + + @Test + public void testAddItemsException() throws Exception { + Mockito.doThrow(Exception.class).when(_customStorageWrapper).addItems(Mockito.anyString(), Mockito.anyObject()); + _userStorageWrapper.addItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testRemoveItems() { + _userStorageWrapper.removeItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(0)).error(Mockito.anyString()); + } + + @Test + public void testRemoveItemsException() throws Exception { + Mockito.doThrow(Exception.class).when(_customStorageWrapper).removeItems(Mockito.anyString(), Mockito.anyObject()); + _userStorageWrapper.removeItems(KEY, Stream.of(ITEM).collect(Collectors.toList())); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testGetItems() throws Exception { + Mockito.when(_customStorageWrapper.getItems(Mockito.anyObject())).thenReturn(Stream.of(RESPONSE).collect(Collectors.toList())); + List result = _userStorageWrapper.getItems(Stream.of(KEY).collect(Collectors.toList())); + Assert.assertNotNull(result); + Assert.assertEquals(RESPONSE, result.get(0)); + } + + @Test + public void testGetItemsException() throws Exception { + Mockito.when(_customStorageWrapper.getItems(Mockito.anyObject())).thenThrow(Exception.class); + List result = _userStorageWrapper.getItems(Stream.of(KEY).collect(Collectors.toList())); + Assert.assertNull(result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testConnect() throws Exception { + Mockito.when(_customStorageWrapper.connect()).thenReturn(true); + boolean result = _userStorageWrapper.connect(); + Assert.assertTrue(result); + } + + @Test + public void testConnectFailed() throws Exception { + Mockito.when(_customStorageWrapper.connect()).thenThrow(Exception.class); + boolean result = _userStorageWrapper.connect(); + Assert.assertFalse(result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } + + @Test + public void testDisconnect() throws Exception { + Mockito.when(_customStorageWrapper.disconnect()).thenReturn(true); + boolean result = _userStorageWrapper.disconnect(); + Assert.assertTrue(result); + } + + @Test + public void testDisconnectFailed() throws Exception { + Mockito.when(_customStorageWrapper.disconnect()).thenThrow(Exception.class); + boolean result = _userStorageWrapper.disconnect(); + Assert.assertFalse(result); + Mockito.verify(_log, Mockito.times(1)).error(Mockito.anyString()); + } +} diff --git a/client/src/test/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitterTest.java b/client/src/test/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitterTest.java new file mode 100644 index 00000000..77dba1aa --- /dev/null +++ b/client/src/test/java/io/split/storages/pluggable/synchronizer/TelemetryConsumerSubmitterTest.java @@ -0,0 +1,77 @@ +package io.split.storages.pluggable.synchronizer; + +import io.split.client.ApiKeyCounter; +import io.split.client.SplitClientConfig; +import io.split.client.dtos.UniqueKeys; +import io.split.client.utils.SDKMetadata; +import io.split.storages.pluggable.domain.ConfigConsumer; +import io.split.storages.pluggable.domain.UserStorageWrapper; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import pluggable.CustomStorageWrapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class TelemetryConsumerSubmitterTest { + private static final String FIRST_KEY = "KEY_1"; + private static final String SECOND_KEY = "KEY_2"; + + @Test + public void testSynchronizeConfig() { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + TelemetryConsumerSubmitter telemetrySynchronizer = new TelemetryConsumerSubmitter(Mockito.mock(CustomStorageWrapper.class), new SDKMetadata("SDK 4.2.x", "22.215135.1", "testMachine")); + SplitClientConfig splitClientConfig = SplitClientConfig.builder().flagSetsFilter(Arrays.asList("set1", "set2", "set-3")).build(); + ConfigConsumer config = telemetrySynchronizer.generateConfig(splitClientConfig, ApiKeyCounter.getApiKeyCounterInstance().getFactoryInstances(), Stream.of("tag1", "tag2").collect(Collectors.toList())); + Assert.assertEquals(3, config.getRedundantFactories()); + Assert.assertEquals(2, config.getTags().size()); + } + + @Test + public void testTestSynchronizeConfig() throws NoSuchFieldException, IllegalAccessException { + UserStorageWrapper userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + TelemetryConsumerSubmitter telemetrySynchronizer = new TelemetryConsumerSubmitter(Mockito.mock(CustomStorageWrapper.class), new SDKMetadata("SDK 4.2.x", "22.215135.1", "testMachine")); + SplitClientConfig splitClientConfig = SplitClientConfig.builder().build(); + Field telemetryConsumerSubmitterHolder = TelemetryConsumerSubmitter.class.getDeclaredField("_userStorageWrapper"); + telemetryConsumerSubmitterHolder.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(telemetryConsumerSubmitterHolder, telemetryConsumerSubmitterHolder.getModifiers() & ~Modifier.FINAL); + telemetryConsumerSubmitterHolder.set(telemetrySynchronizer, userStorageWrapper); + telemetrySynchronizer.synchronizeConfig(splitClientConfig, 10L, new HashMap<>(), new ArrayList<>()); + Mockito.verify(userStorageWrapper, Mockito.times(1)).hSet(Mockito.eq("SPLITIO.telemetry.init"), Mockito.eq("SDK 4.2.x/testMachine/22.215135.1"), Mockito.anyObject()); + } + + @Test + public void testTestSynchronizeUniqueKeys() throws NoSuchFieldException, IllegalAccessException { + UserStorageWrapper userStorageWrapper = Mockito.mock(UserStorageWrapper.class); + TelemetryConsumerSubmitter telemetrySynchronizer = new TelemetryConsumerSubmitter(Mockito.mock(CustomStorageWrapper.class), new SDKMetadata("SDK 4.2.x", "22.215135.1", "testMachine")); + Field telemetryConsumerSubmitterHolder = TelemetryConsumerSubmitter.class.getDeclaredField("_userStorageWrapper"); + telemetryConsumerSubmitterHolder.setAccessible(true); + telemetryConsumerSubmitterHolder.set(telemetrySynchronizer, userStorageWrapper); + + List keys = new ArrayList<>(); + keys.add("key-1"); + keys.add("key-2"); + List uniqueKeys = new ArrayList<>(); + uniqueKeys.add(new UniqueKeys.UniqueKey("feature-1", keys)); + UniqueKeys uniqueKeysToSend = new UniqueKeys(uniqueKeys); + + telemetrySynchronizer.synchronizeUniqueKeys(uniqueKeysToSend); + List uniqueKeysJson = new ArrayList<>(Collections.singletonList("{\"f\":\"feature-1\",\"ks\":[\"key-1\",\"key-2\"]}")); + Mockito.verify(userStorageWrapper).pushItems(Mockito.eq("SPLITIO.uniquekeys"), Mockito.eq(uniqueKeysJson)); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/telemetry/storage/InMemoryTelemetryStorageTest.java b/client/src/test/java/io/split/telemetry/storage/InMemoryTelemetryStorageTest.java new file mode 100644 index 00000000..a74ee03a --- /dev/null +++ b/client/src/test/java/io/split/telemetry/storage/InMemoryTelemetryStorageTest.java @@ -0,0 +1,259 @@ +package io.split.telemetry.storage; + +import io.split.telemetry.domain.HTTPErrors; +import io.split.telemetry.domain.HTTPLatencies; +import io.split.telemetry.domain.LastSynchronization; +import io.split.telemetry.domain.MethodExceptions; +import io.split.telemetry.domain.MethodLatencies; +import io.split.telemetry.domain.StreamingEvent; +import io.split.telemetry.domain.UpdatesFromSSE; + +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class InMemoryTelemetryStorageTest{ + + @Test + public void testInMemoryTelemetryStorage() { + InMemoryTelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + + //MethodLatencies + telemetryStorage.recordLatency(MethodEnum.TREATMENT, 1500l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENT, 2000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS, 3000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS, 500l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENT_WITH_CONFIG, 800l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_BY_FLAG_SET, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_BY_FLAG_SETS, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, 1000l * 1000); + + MethodLatencies latencies = telemetryStorage.popLatencies(); + Assert.assertEquals(2, latencies.getTreatment().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, latencies.getTreatments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentsWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentByFlagSet().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentByFlagSets().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentWithConfigByFlagSet().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, latencies.getTreatmentWithConfigByFlagSets().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, latencies.getTrack().stream().mapToInt(Long::intValue).sum()); + + //Check empty has worked + latencies = telemetryStorage.popLatencies(); + Assert.assertEquals(0, latencies.getTreatment().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, latencies.getTreatments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, latencies.getTreatmentsWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, latencies.getTreatmentWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, latencies.getTrack().stream().mapToInt(Long::intValue).sum()); + + //HttpLatencies + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.TELEMETRY, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.TELEMETRY, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.EVENTS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.EVENTS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SEGMENTS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS_COUNT, 2000l * 1000); + + HTTPLatencies httpLatencies = telemetryStorage.popHTTPLatencies(); + + Assert.assertEquals(3, httpLatencies.getSplits().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, httpLatencies.getTelemetry().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, httpLatencies.getEvents().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, httpLatencies.getSegments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, httpLatencies.getImpressions().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, httpLatencies.getImpressionsCount().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getToken().stream().mapToInt(Long::intValue).sum()); + + httpLatencies = telemetryStorage.popHTTPLatencies(); + Assert.assertEquals(0, httpLatencies.getSplits().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getTelemetry().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getEvents().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getSegments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getImpressions().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getImpressionsCount().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, httpLatencies.getToken().stream().mapToInt(Long::intValue).sum()); + + + //Exceptions + telemetryStorage.recordException(MethodEnum.TREATMENT); + telemetryStorage.recordException(MethodEnum.TREATMENTS); + telemetryStorage.recordException(MethodEnum.TREATMENT); + telemetryStorage.recordException(MethodEnum.TREATMENTS); + telemetryStorage.recordException(MethodEnum.TREATMENT_WITH_CONFIG); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG); + telemetryStorage.recordException(MethodEnum.TREATMENTS_BY_FLAG_SET); + telemetryStorage.recordException(MethodEnum.TREATMENTS_BY_FLAG_SETS); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); + + MethodExceptions methodExceptions = telemetryStorage.popExceptions(); + Assert.assertEquals(2, methodExceptions.getTreatment()); + Assert.assertEquals(2, methodExceptions.getTreatments()); + Assert.assertEquals(1, methodExceptions.getTreatmentsWithConfig()); + Assert.assertEquals(1, methodExceptions.getTreatmentWithConfig()); + Assert.assertEquals(1, methodExceptions.getTreatmentByFlagSet()); + Assert.assertEquals(1, methodExceptions.getTreatmentByFlagSets()); + Assert.assertEquals(1, methodExceptions.getTreatmentWithConfigByFlagSet()); + Assert.assertEquals(1, methodExceptions.getTreatmentWithConfigByFlagSets()); + Assert.assertEquals(0, methodExceptions.getTrack()); + + //Check empty has worked + methodExceptions = telemetryStorage.popExceptions(); + Assert.assertEquals(0, methodExceptions.getTreatment()); + Assert.assertEquals(0, methodExceptions.getTreatments()); + Assert.assertEquals(0, methodExceptions.getTreatmentsWithConfig()); + Assert.assertEquals(0, methodExceptions.getTreatmentWithConfig()); + Assert.assertEquals(0, methodExceptions.getTreatmentByFlagSet()); + Assert.assertEquals(0, methodExceptions.getTreatmentByFlagSets()); + Assert.assertEquals(0, methodExceptions.getTreatmentWithConfigByFlagSet()); + Assert.assertEquals(0, methodExceptions.getTreatmentWithConfigByFlagSets()); + Assert.assertEquals(0, methodExceptions.getTrack()); + + //AuthRejections + telemetryStorage.recordAuthRejections(); + long authRejections = telemetryStorage.popAuthRejections(); + Assert.assertEquals(1, authRejections); + + //Check amount has been reseted + authRejections = telemetryStorage.popAuthRejections(); + Assert.assertEquals(0, authRejections); + + //AuthRejections + telemetryStorage.recordTokenRefreshes(); + telemetryStorage.recordTokenRefreshes(); + long tokenRefreshes = telemetryStorage.popTokenRefreshes(); + Assert.assertEquals(2, tokenRefreshes); + + //Check amount has been reseted + tokenRefreshes = telemetryStorage.popTokenRefreshes(); + Assert.assertEquals(0, tokenRefreshes); + + //Non Ready usages + telemetryStorage.recordNonReadyUsage(); + telemetryStorage.recordNonReadyUsage(); + long nonReadyUsages = telemetryStorage.getNonReadyUsages(); + Assert.assertEquals(2, nonReadyUsages); + + //BUR Timeouts + telemetryStorage.recordBURTimeout(); + long burTimeouts = telemetryStorage.getBURTimeouts(); + Assert.assertEquals(1, burTimeouts); + + //ImpressionStats + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, 3); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, 1); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 4); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 6); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 2); + + long impressionsDeduped = telemetryStorage.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED); + long impressionsDropped = telemetryStorage.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED); + long impressionsQueued = telemetryStorage.getImpressionsStats(ImpressionsDataTypeEnum.IMPRESSIONS_QUEUED); + + Assert.assertEquals(4, impressionsDeduped); + Assert.assertEquals(12, impressionsDropped); + Assert.assertEquals(0, impressionsQueued); + + //Event Stats + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 3); + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 7); + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_QUEUED, 3); + + long eventsDropped = telemetryStorage.getEventStats(EventsDataRecordsEnum.EVENTS_DROPPED); + long eventsQueued = telemetryStorage.getEventStats(EventsDataRecordsEnum.EVENTS_QUEUED); + + Assert.assertEquals(10, eventsDropped); + Assert.assertEquals(3, eventsQueued); + + //Successfuly sync + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.EVENTS, 1500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.EVENTS, 800); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS, 2500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS, 10500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT, 1500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.SEGMENTS, 1580); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.TELEMETRY, 265); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.TOKEN, 129); + + LastSynchronization lastSynchronization = telemetryStorage.getLastSynchronization(); + Assert.assertEquals(800, lastSynchronization.getEvents()); + Assert.assertEquals(129, lastSynchronization.getToken()); + Assert.assertEquals(1580, lastSynchronization.getSegments()); + Assert.assertEquals(0, lastSynchronization.getSplits()); + Assert.assertEquals(10500, lastSynchronization.getImpressions()); + Assert.assertEquals(1500, lastSynchronization.getImpressionsCount()); + Assert.assertEquals(265, lastSynchronization.getTelemetry()); + + //Session length + telemetryStorage.recordSessionLength(91218); + long sessionLength = telemetryStorage.getSessionLength(); + Assert.assertEquals(91218, sessionLength); + + //Sync Error + telemetryStorage.recordSyncError(ResourceEnum.TELEMETRY_SYNC, 400); + telemetryStorage.recordSyncError(ResourceEnum.TELEMETRY_SYNC, 400); + telemetryStorage.recordSyncError(ResourceEnum.SEGMENT_SYNC, 501); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.EVENT_SYNC, 503); + telemetryStorage.recordSyncError(ResourceEnum.SPLIT_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_COUNT_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.TOKEN_SYNC, 403); + + HTTPErrors httpErrors = telemetryStorage.popHTTPErrors(); + Assert.assertEquals(2, httpErrors.getTelemetry().get(400l).intValue()); + Assert.assertEquals(1, httpErrors.getSegments().get(501l).intValue()); + Assert.assertEquals(2, httpErrors.getImpressions().get(403l).intValue()); + Assert.assertEquals(1, httpErrors.getImpressionsCount().get(403l).intValue()); + Assert.assertEquals(1, httpErrors.getEvents().get(503l).intValue()); + Assert.assertEquals(1, httpErrors.getSplits().get(403l).intValue()); + Assert.assertEquals(1, httpErrors.getToken().get(403l).intValue()); + + //Streaming events + StreamingEvent streamingEvent = new StreamingEvent(1, 290, 91218); + telemetryStorage.recordStreamingEvents(streamingEvent); + + List streamingEvents = telemetryStorage.popStreamingEvents(); + Assert.assertEquals(290, streamingEvents.get(0).getData()); + Assert.assertEquals(1, streamingEvents.get(0).getType()); + Assert.assertEquals(91218, streamingEvents.get(0).getTimestamp()); + + //Check list has been cleared + streamingEvents = telemetryStorage.popStreamingEvents(); + Assert.assertEquals(0, streamingEvents.size()); + + //Tags + telemetryStorage.addTag("TAG_1"); + telemetryStorage.addTag("TAG_2"); + telemetryStorage.addTag("TAG_2"); + List tags = telemetryStorage.popTags(); + Assert.assertEquals(2, tags.size()); + + //Check tags have been cleared + tags = telemetryStorage.popTags(); + Assert.assertEquals(0, tags.size()); + + //UpdatesFromSSE + telemetryStorage.recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + telemetryStorage.recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + telemetryStorage.recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + + UpdatesFromSSE updatesFromSSE = telemetryStorage.popUpdatesFromSSE(); + Assert.assertEquals(3, updatesFromSSE.getSplits()); + } +} \ No newline at end of file diff --git a/client/src/test/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitterTest.java b/client/src/test/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitterTest.java new file mode 100644 index 00000000..39a6be14 --- /dev/null +++ b/client/src/test/java/io/split/telemetry/synchronizer/TelemetryInMemorySubmitterTest.java @@ -0,0 +1,300 @@ +package io.split.telemetry.synchronizer; + +import io.split.TestHelper; +import io.split.client.RequestDecorator; +import io.split.client.dtos.UniqueKeys; +import io.split.client.utils.SDKMetadata; +import io.split.service.SplitHttpClient; +import io.split.service.SplitHttpClientImpl; +import io.split.storages.SegmentCacheConsumer; +import io.split.storages.SplitCacheConsumer; +import io.split.client.ApiKeyCounter; +import io.split.client.SplitClientConfig; +import io.split.telemetry.domain.Config; +import io.split.telemetry.domain.Stats; +import io.split.telemetry.domain.StreamingEvent; + +import io.split.telemetry.domain.enums.EventsDataRecordsEnum; +import io.split.telemetry.domain.enums.HTTPLatenciesEnum; +import io.split.telemetry.domain.enums.ImpressionsDataTypeEnum; +import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum; +import io.split.telemetry.domain.enums.MethodEnum; +import io.split.telemetry.domain.enums.ResourceEnum; +import io.split.telemetry.domain.enums.UpdatesFromSSEEnum; +import io.split.telemetry.storage.InMemoryTelemetryStorage; +import io.split.telemetry.storage.TelemetryRuntimeProducer; +import io.split.telemetry.storage.TelemetryStorage; +import io.split.telemetry.storage.TelemetryStorageConsumer; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.core5.http.HttpStatus; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +public class TelemetryInMemorySubmitterTest { + private static final String FIRST_KEY = "KEY_1"; + private static final String SECOND_KEY = "KEY_2"; + public static final String TELEMETRY_ENDPOINT = "https://telemetry.split.io/api/v1"; + + @Test + public void testSynchronizeConfig() throws URISyntaxException, NoSuchMethodException, IOException, + IllegalAccessException, InvocationTargetException { + CloseableHttpClient httpClient = TestHelper.mockHttpClient(TELEMETRY_ENDPOINT, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + TelemetrySynchronizer telemetrySynchronizer = getTelemetrySynchronizer(splitHttpClient); + SplitClientConfig splitClientConfig = SplitClientConfig.builder().build(); + + telemetrySynchronizer.synchronizeConfig(splitClientConfig, 100l, new HashMap(), + new ArrayList()); + Mockito.verify(httpClient, Mockito.times(1)).execute(Mockito.any()); + } + + @Test + public void testSynchronizeStats() throws Exception { + CloseableHttpClient httpClient = TestHelper.mockHttpClient(TELEMETRY_ENDPOINT, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + TelemetrySynchronizer telemetrySynchronizer = getTelemetrySynchronizer(splitHttpClient); + + telemetrySynchronizer.synchronizeStats(); + Mockito.verify(httpClient, Mockito.times(1)).execute(Mockito.any()); + } + + @Test + public void testSynchronizeUniqueKeys() throws Exception { + CloseableHttpClient httpClient = TestHelper.mockHttpClient(TELEMETRY_ENDPOINT, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + TelemetrySynchronizer telemetrySynchronizer = getTelemetrySynchronizer(splitHttpClient); + + List keys = new ArrayList<>(); + keys.add("key-1"); + keys.add("key-2"); + List uniqueKeys = new ArrayList<>(); + uniqueKeys.add(new UniqueKeys.UniqueKey("feature-1", keys)); + UniqueKeys imp = new UniqueKeys(uniqueKeys); + + telemetrySynchronizer.synchronizeUniqueKeys(imp); + Mockito.verify(httpClient, Mockito.times(1)).execute(Mockito.any()); + } + + @Test + public void testConfig() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, + IOException, URISyntaxException, NoSuchFieldException { + ApiKeyCounter.getApiKeyCounterInstance().clearApiKeys(); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(FIRST_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + ApiKeyCounter.getApiKeyCounterInstance().add(SECOND_KEY); + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + CloseableHttpClient httpClient = TestHelper.mockHttpClient(TELEMETRY_ENDPOINT, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + TelemetryInMemorySubmitter telemetrySynchronizer = getTelemetrySynchronizer(splitHttpClient); + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .flagSetsFilter(Arrays.asList("a", "_b", "a", "a", "c", "d", "_d")).build(); + populateConfig(telemetryStorage); + Field telemetryStorageConsumer = TelemetryInMemorySubmitter.class.getDeclaredField("_telemetryStorageConsumer"); + telemetryStorageConsumer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(telemetryStorageConsumer, telemetryStorageConsumer.getModifiers() & ~Modifier.FINAL); + telemetryStorageConsumer.set(telemetrySynchronizer, telemetryStorage); + Config config = telemetrySynchronizer.generateConfig(splitClientConfig, 100l, + ApiKeyCounter.getApiKeyCounterInstance().getFactoryInstances(), new ArrayList<>()); + Assert.assertEquals(3, config.getRedundantFactories()); + Assert.assertEquals(2, config.getBurTimeouts()); + Assert.assertEquals(3, config.getNonReadyUsages()); + Assert.assertEquals(7, config.getFlagSetsTotal()); + Assert.assertEquals(4, config.getFlagSetsInvalid()); + } + + @Test + public void testStats() throws Exception { + TelemetryStorage telemetryStorage = new InMemoryTelemetryStorage(); + CloseableHttpClient httpClient = TestHelper.mockHttpClient(TELEMETRY_ENDPOINT, HttpStatus.SC_OK); + SplitHttpClient splitHttpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null), "qwerty", + metadata()); + TelemetryInMemorySubmitter telemetrySynchronizer = getTelemetrySynchronizer(splitHttpClient); + populateStats(telemetryStorage); + Field telemetryStorageConsumer = TelemetryInMemorySubmitter.class.getDeclaredField("_telemetryStorageConsumer"); + telemetryStorageConsumer.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(telemetryStorageConsumer, telemetryStorageConsumer.getModifiers() & ~Modifier.FINAL); + + telemetryStorageConsumer.set(telemetrySynchronizer, telemetryStorage); + Stats stats = telemetrySynchronizer.generateStats(); + Assert.assertEquals(2, stats.getMethodLatencies().getTreatment().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, stats.getMethodLatencies().getTreatments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentsWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentWithConfig().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentByFlagSet().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentByFlagSets().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentWithConfigByFlagSet().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, + stats.getMethodLatencies().getTreatmentWithConfigByFlagSets().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, stats.getMethodLatencies().getTrack().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(3, stats.getHttpLatencies().getSplits().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, stats.getHttpLatencies().getTelemetry().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, stats.getHttpLatencies().getEvents().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, stats.getHttpLatencies().getSegments().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, stats.getHttpLatencies().getImpressions().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(1, stats.getHttpLatencies().getImpressionsCount().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, stats.getHttpLatencies().getToken().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(2, stats.getMethodExceptions().getTreatment()); + Assert.assertEquals(2, stats.getMethodExceptions().getTreatments()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentsWithConfig()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentWithConfig()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentWithConfig()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentByFlagSet()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentByFlagSets()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentWithConfigByFlagSet()); + Assert.assertEquals(1, stats.getMethodExceptions().getTreatmentWithConfigByFlagSets()); + Assert.assertEquals(0, stats.getMethodExceptions().getTrack()); + Assert.assertEquals(1, stats.getAuthRejections()); + Assert.assertEquals(2, stats.getTokenRefreshes()); + Assert.assertEquals(4, stats.getImpressionsDeduped()); + Assert.assertEquals(12, stats.getImpressionsDropped()); + Assert.assertEquals(0, stats.getImpressionsQueued()); + Assert.assertEquals(10, stats.getEventsDropped()); + Assert.assertEquals(3, stats.getEventsQueued()); + Assert.assertEquals(800, stats.getLastSynchronization().getEvents()); + Assert.assertEquals(129, stats.getLastSynchronization().getToken()); + Assert.assertEquals(1580, stats.getLastSynchronization().getSegments()); + Assert.assertEquals(0, stats.getLastSynchronization().getSplits()); + Assert.assertEquals(10500, stats.getLastSynchronization().getImpressions()); + Assert.assertEquals(1500, stats.getLastSynchronization().getImpressionsCount()); + Assert.assertEquals(265, stats.getLastSynchronization().getTelemetry()); + Assert.assertEquals(91218, stats.getSessionLengthMs()); + Assert.assertEquals(2, stats.getHttpErrors().getTelemetry().get(400l).intValue()); + Assert.assertEquals(1, stats.getHttpErrors().getSegments().get(501l).intValue()); + Assert.assertEquals(2, stats.getHttpErrors().getImpressions().get(403l).intValue()); + Assert.assertEquals(1, stats.getHttpErrors().getImpressionsCount().get(403l).intValue()); + Assert.assertEquals(1, stats.getHttpErrors().getEvents().get(503l).intValue()); + Assert.assertEquals(1, stats.getHttpErrors().getSplits().get(403l).intValue()); + Assert.assertEquals(1, stats.getHttpErrors().getToken().get(403l).intValue()); + List streamingEvents = stats.getStreamingEvents(); + Assert.assertEquals(290, streamingEvents.get(0).getData()); + Assert.assertEquals(1, streamingEvents.get(0).getType()); + Assert.assertEquals(91218, streamingEvents.get(0).getTimestamp()); + Assert.assertEquals(1, stats.getUpdatesFromSSE().getSplits()); + } + + private TelemetryInMemorySubmitter getTelemetrySynchronizer(SplitHttpClient httpClient) throws URISyntaxException { + TelemetryStorageConsumer consumer = Mockito.mock(InMemoryTelemetryStorage.class); + TelemetryRuntimeProducer telemetryRuntimeProducer = Mockito.mock(TelemetryRuntimeProducer.class); + SplitCacheConsumer splitCacheConsumer = Mockito.mock(SplitCacheConsumer.class); + SegmentCacheConsumer segmentCacheConsumer = Mockito.mock(SegmentCacheConsumer.class); + TelemetryInMemorySubmitter telemetrySynchronizer = new TelemetryInMemorySubmitter(httpClient, + URI.create(TELEMETRY_ENDPOINT), consumer, splitCacheConsumer, segmentCacheConsumer, + telemetryRuntimeProducer, 0l); + return telemetrySynchronizer; + } + + private void populateStats(TelemetryStorage telemetryStorage) { + telemetryStorage.recordLatency(MethodEnum.TREATMENT, 1500l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENT, 2000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS, 3000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS, 500l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENT_WITH_CONFIG, 800l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_BY_FLAG_SET, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_BY_FLAG_SETS, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET, 1000l * 1000); + telemetryStorage.recordLatency(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS, 1000l * 1000); + + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.TELEMETRY, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.TELEMETRY, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.EVENTS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.EVENTS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SEGMENTS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.SPLITS, 2000l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS, 1500l * 1000); + telemetryStorage.recordSyncLatency(HTTPLatenciesEnum.IMPRESSIONS_COUNT, 2000l * 1000); + + telemetryStorage.recordException(MethodEnum.TREATMENT); + telemetryStorage.recordException(MethodEnum.TREATMENTS); + telemetryStorage.recordException(MethodEnum.TREATMENT); + telemetryStorage.recordException(MethodEnum.TREATMENTS); + telemetryStorage.recordException(MethodEnum.TREATMENT_WITH_CONFIG); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG); + telemetryStorage.recordException(MethodEnum.TREATMENTS_BY_FLAG_SET); + telemetryStorage.recordException(MethodEnum.TREATMENTS_BY_FLAG_SETS); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SET); + telemetryStorage.recordException(MethodEnum.TREATMENTS_WITH_CONFIG_BY_FLAG_SETS); + + telemetryStorage.recordAuthRejections(); + + telemetryStorage.recordTokenRefreshes(); + telemetryStorage.recordTokenRefreshes(); + + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, 3); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DEDUPED, 1); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 4); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 6); + telemetryStorage.recordImpressionStats(ImpressionsDataTypeEnum.IMPRESSIONS_DROPPED, 2); + + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 3); + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_DROPPED, 7); + telemetryStorage.recordEventStats(EventsDataRecordsEnum.EVENTS_QUEUED, 3); + + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.EVENTS, 1500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.EVENTS, 800); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS, 2500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS, 10500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.IMPRESSIONS_COUNT, 1500); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.SEGMENTS, 1580); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.TELEMETRY, 265); + telemetryStorage.recordSuccessfulSync(LastSynchronizationRecordsEnum.TOKEN, 129); + + telemetryStorage.recordSessionLength(91218); + + telemetryStorage.recordSyncError(ResourceEnum.TELEMETRY_SYNC, 400); + telemetryStorage.recordSyncError(ResourceEnum.TELEMETRY_SYNC, 400); + telemetryStorage.recordSyncError(ResourceEnum.SEGMENT_SYNC, 501); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.EVENT_SYNC, 503); + telemetryStorage.recordSyncError(ResourceEnum.SPLIT_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.IMPRESSION_COUNT_SYNC, 403); + telemetryStorage.recordSyncError(ResourceEnum.TOKEN_SYNC, 403); + + StreamingEvent streamingEvent = new StreamingEvent(1, 290, 91218); + telemetryStorage.recordStreamingEvents(streamingEvent); + telemetryStorage.recordUpdatesFromSSE(UpdatesFromSSEEnum.SPLITS); + } + + private void populateConfig(TelemetryStorage telemetryStorage) { + telemetryStorage.recordBURTimeout(); + telemetryStorage.recordBURTimeout(); + telemetryStorage.recordNonReadyUsage(); + telemetryStorage.recordNonReadyUsage(); + telemetryStorage.recordNonReadyUsage(); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + +} diff --git a/client/src/test/java/io/split/telemetry/synchronizer/TelemetrySyncTaskTest.java b/client/src/test/java/io/split/telemetry/synchronizer/TelemetrySyncTaskTest.java new file mode 100644 index 00000000..fbb6470b --- /dev/null +++ b/client/src/test/java/io/split/telemetry/synchronizer/TelemetrySyncTaskTest.java @@ -0,0 +1,31 @@ +package io.split.telemetry.synchronizer; + +import org.junit.Test; +import org.mockito.Mockito; + +public class TelemetrySyncTaskTest { + + @Test + public void testSynchronizationTask() throws Exception { + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); + Mockito.doNothing().when(telemetrySynchronizer).synchronizeStats(); + TelemetrySyncTask telemetrySyncTask = new TelemetrySyncTask(1, telemetrySynchronizer, null); + telemetrySyncTask.startScheduledTask(); + Thread.sleep(2900); + Mockito.verify(telemetrySynchronizer, Mockito.times(2)).synchronizeStats(); + } + + @Test + public void testStopSynchronizationTask() throws Exception { + TelemetrySynchronizer telemetrySynchronizer = Mockito.mock(TelemetryInMemorySubmitter.class); +// Mockito.doNothing().when(telemetrySynchronizer).synchronizeStats(); + TelemetrySyncTask telemetrySyncTask = new TelemetrySyncTask(1, telemetrySynchronizer, null); + telemetrySyncTask.startScheduledTask(); + Thread.sleep(2100); + Mockito.verify(telemetrySynchronizer, Mockito.times(2)).synchronizeStats(); + telemetrySyncTask.stopScheduledTask(); + Mockito.verify(telemetrySynchronizer, Mockito.times(2)).synchronizeStats(); + Mockito.verify(telemetrySynchronizer, Mockito.times(1)).finalSynchronization(); + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/telemetry/utils/AtomicLongArrayTest.java b/client/src/test/java/io/split/telemetry/utils/AtomicLongArrayTest.java new file mode 100644 index 00000000..5beea1dd --- /dev/null +++ b/client/src/test/java/io/split/telemetry/utils/AtomicLongArrayTest.java @@ -0,0 +1,51 @@ +package io.split.telemetry.utils; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class AtomicLongArrayTest { + + private static final int SIZE = 23; + + @Test + public void testAtomicLong() { + AtomicLongArray atomicLongArray = new AtomicLongArray(SIZE); + Assert.assertNotNull(atomicLongArray); + } + + @Test + public void testArraySizeError() { + AtomicLongArray atomicLongArray = new AtomicLongArray(0); + Logger log = Mockito.mock(Logger.class); + atomicLongArray.increment(2); + Assert.assertEquals(1, atomicLongArray.fetchAndClearAll().stream().mapToInt(Long::intValue).sum()); + } + + @Test + public void testIncrement() { + AtomicLongArray atomicLongArray = new AtomicLongArray(SIZE); + atomicLongArray.increment(2); + Assert.assertEquals(1, atomicLongArray.fetchAndClearAll().stream().mapToInt(Long::intValue).sum()); + } + + @Test + public void testIncrementError() { + AtomicLongArray atomicLongArray = new AtomicLongArray(SIZE); + atomicLongArray.increment(25); + Assert.assertEquals(0, atomicLongArray.fetchAndClearAll().stream().mapToInt(Long::intValue).sum()); + } + + @Test + public void testClearAll() { + AtomicLongArray atomicLongArray = new AtomicLongArray(SIZE); + atomicLongArray.increment(2); + Assert.assertEquals(1, atomicLongArray.fetchAndClearAll().stream().mapToInt(Long::intValue).sum()); + Assert.assertEquals(0, atomicLongArray.fetchAndClearAll().stream().mapToInt(Long::intValue).sum()); + } + +} \ No newline at end of file diff --git a/client/src/test/java/io/split/telemetry/utils/BucketCalculatorTest.java b/client/src/test/java/io/split/telemetry/utils/BucketCalculatorTest.java new file mode 100644 index 00000000..04ba7448 --- /dev/null +++ b/client/src/test/java/io/split/telemetry/utils/BucketCalculatorTest.java @@ -0,0 +1,22 @@ +package io.split.telemetry.utils; + +import org.junit.Assert; +import org.junit.Test; + +public class BucketCalculatorTest{ + + @Test + public void testBucketCalculator() { + int bucket = BucketCalculator.getBucketForLatency(500l * 1000); + Assert.assertEquals(0, bucket); + + bucket = BucketCalculator.getBucketForLatency(1500l * 1000); + Assert.assertEquals(1, bucket); + + bucket = BucketCalculator.getBucketForLatency(8000l * 1000); + Assert.assertEquals(6, bucket); + + bucket = BucketCalculator.getBucketForLatency(7481829l * 1000); + Assert.assertEquals(22, bucket); + } +} diff --git a/client/src/test/resources/keyStore.p12 b/client/src/test/resources/keyStore.p12 new file mode 100644 index 00000000..ce2b3417 Binary files /dev/null and b/client/src/test/resources/keyStore.p12 differ diff --git a/client/src/test/resources/org/powermock/extensions/configuration.properties b/client/src/test/resources/org/powermock/extensions/configuration.properties new file mode 100644 index 00000000..a8ebaeba --- /dev/null +++ b/client/src/test/resources/org/powermock/extensions/configuration.properties @@ -0,0 +1 @@ +powermock.global-ignore=jdk.internal.reflect.*,javax.net.ssl.* \ No newline at end of file diff --git a/client/src/test/resources/rule_base_segments.json b/client/src/test/resources/rule_base_segments.json new file mode 100644 index 00000000..65cd9a5d --- /dev/null +++ b/client/src/test/resources/rule_base_segments.json @@ -0,0 +1,61 @@ +{"ff": {"d": [], "t": -1, "s": -1}, +"rbs": {"t": -1, "s": -1, "d": + [{ + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{"keys":["mauro@split.io","gaston@split.io"],"segments":[]}, + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ]}, + { + "changeNumber": 5, + "name": "dependent_rbs", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded": { + "keys": [], + "segments": [] + }, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "IN_RULE_BASED_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "sample_rule_based_segment" + } + } + ] + } + } + ] + }] +}} diff --git a/client/src/test/resources/rule_base_segments2.json b/client/src/test/resources/rule_base_segments2.json new file mode 100644 index 00000000..991fa81b --- /dev/null +++ b/client/src/test/resources/rule_base_segments2.json @@ -0,0 +1,63 @@ +{"ff": {"d": [], "t": -1, "s": -1}, +"rbs": {"t": -1, "s": -1, "d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[{"type":"rule-based", "name":"no_excludes"}] + }, + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@harness.io" + ] + } + } + ] + } + } + ] + }, + { + "changeNumber": 5, + "name": "no_excludes", + "status": "ACTIVE", + "trafficTypeName": "user", + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ] + } +]}} diff --git a/client/src/test/resources/rule_base_segments3.json b/client/src/test/resources/rule_base_segments3.json new file mode 100644 index 00000000..f738f3f7 --- /dev/null +++ b/client/src/test/resources/rule_base_segments3.json @@ -0,0 +1,35 @@ +{"ff": {"d": [], "t": -1, "s": -1}, +"rbs": {"t": -1, "s": -1, "d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[{"type":"standard", "name":"segment1"}] + }, + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ] + } +]}} diff --git a/client/src/test/resources/sanitizer/sameInAddedAndRemoved.json b/client/src/test/resources/sanitizer/sameInAddedAndRemoved.json new file mode 100644 index 00000000..5e87f851 --- /dev/null +++ b/client/src/test/resources/sanitizer/sameInAddedAndRemoved.json @@ -0,0 +1,15 @@ +{ + "name": "segment3", + "added": [ + "testo2222", + "test_string_without_attr", + "Test_Save_1", + "test_in_segment" + ], + "removed": [ + "Test_Save_1", + "test_in_segment" + ], + "since": -1, + "till": 1585948850110 +} diff --git a/client/src/test/resources/sanitizer/segmentChangeSinceTill.json b/client/src/test/resources/sanitizer/segmentChangeSinceTill.json new file mode 100644 index 00000000..e8c9b3fe --- /dev/null +++ b/client/src/test/resources/sanitizer/segmentChangeSinceTill.json @@ -0,0 +1,12 @@ +{ + "name": "segment3", + "added": [ + "testo2222", + "test_string_without_attr", + "Test_Save_1", + "test_in_segment" + ], + "removed": [], + "since": -3223, + "till": 0 +} \ No newline at end of file diff --git a/client/src/test/resources/sanitizer/segmentNameNull.json b/client/src/test/resources/sanitizer/segmentNameNull.json new file mode 100644 index 00000000..8db5e86e --- /dev/null +++ b/client/src/test/resources/sanitizer/segmentNameNull.json @@ -0,0 +1,11 @@ +{ + "added": [ + "testo2222", + "test_string_without_attr", + "Test_Save_1", + "test_in_segment" + ], + "removed": [], + "since": -1, + "till": 1585948850110 +} \ No newline at end of file diff --git a/client/src/test/resources/sanitizer/splitChangeSplitsToSanitize.json b/client/src/test/resources/sanitizer/splitChangeSplitsToSanitize.json new file mode 100644 index 00000000..de35084e --- /dev/null +++ b/client/src/test/resources/sanitizer/splitChangeSplitsToSanitize.json @@ -0,0 +1,132 @@ +{"ff": { + "d": [ + { + "name": "test1", + "trafficAllocation": 101, + "killed": false, + "changeNumber": 1660326991072, + "algo": 2, + "configurations": {} + }, + { + "trafficTypeName": "user", + "trafficAllocation": 100, + "trafficAllocationSeed": -670005248, + "seed": -1297078412, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1650919058695, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V5", + "size": 0 + }, + { + "treatment": "v8", + "size": 0 + } + ], + "label": "default rule" + } + ] + } + ], + "s": -1, + "t": 1660326991072 + }, +"rbs":{ + "d": [ + {"changeNumber":5, + "name":"sample_rule_based_segment", + "status":"ACTIVE", + "trafficTypeName":"user", + "excluded":{"keys":["mauro@split.io"]} + }, + {"changeNumber":5, + "status":"ACTIVE", + "trafficTypeName":"user", + "excluded":{"keys":["mauro@split.io"],"segments":[]}, + "conditions":[ + {"conditionType":"ROLLOUT", + "matcherGroup":{"combiner":"AND", + "matchers":[ + {"keySelector":{"trafficType":"user","attribute":"email"}, + "matcherType":"ENDS_WITH", + "negate":false, + "whitelistMatcherData":{"whitelist":["@split.io"]} + } + ] + } + } + ] + } + + ], + "s": -1, + "t": -1} +} \ No newline at end of file diff --git a/client/src/test/resources/sanitizer/splitChangeTillSanitization.json b/client/src/test/resources/sanitizer/splitChangeTillSanitization.json new file mode 100644 index 00000000..5a1f806f --- /dev/null +++ b/client/src/test/resources/sanitizer/splitChangeTillSanitization.json @@ -0,0 +1,55 @@ +{"ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "test1", + "trafficAllocation": 100, + "trafficAllocationSeed": -1364119282, + "seed": -605938843, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1660326991072, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + } + ], + "s": 398, + "t": 0 +}, "rbs":{"d": [], "s": -1, "t": 0}} \ No newline at end of file diff --git a/client/src/test/resources/sanitizer/splitChangeWithoutSplits.json b/client/src/test/resources/sanitizer/splitChangeWithoutSplits.json new file mode 100644 index 00000000..29463bff --- /dev/null +++ b/client/src/test/resources/sanitizer/splitChangeWithoutSplits.json @@ -0,0 +1,4 @@ +{"ff": { + "s": -1, + "t": 2434234234 +}, "rbs":{"s": -1, "t": -1}} \ No newline at end of file diff --git a/client/src/test/resources/sanitizer/splitChangerMatchersNull.json b/client/src/test/resources/sanitizer/splitChangerMatchersNull.json new file mode 100644 index 00000000..282f3b54 --- /dev/null +++ b/client/src/test/resources/sanitizer/splitChangerMatchersNull.json @@ -0,0 +1,77 @@ +{"ff": { + "d": [ + { + "name": "test1", + "trafficTypeName": "user", + "trafficAllocation": 100, + "trafficAllocationSeed": -670005248, + "seed": -1297078412, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1650919058695, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND" + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V5", + "size": 0 + }, + { + "treatment": "v8", + "size": 0 + } + ], + "label": "default rule" + } + ] + } + ], + "s": -1, + "t": 1660326991072 +}, "rbs":{"d": [], "s": -1, "t": -1}} \ No newline at end of file diff --git a/client/src/test/resources/segmentFetcher/segment_test.json b/client/src/test/resources/segmentFetcher/segment_test.json new file mode 100644 index 00000000..add131bb --- /dev/null +++ b/client/src/test/resources/segmentFetcher/segment_test.json @@ -0,0 +1 @@ +{"name":"segment_test","added":["user-1"],"removed":["user-2","user-3"],"since":-1,"till":-1} \ No newline at end of file diff --git a/client/src/test/resources/segment_1.json b/client/src/test/resources/segment_1.json new file mode 100644 index 00000000..5ba63026 --- /dev/null +++ b/client/src/test/resources/segment_1.json @@ -0,0 +1,12 @@ +{ + "name": "segment_1", + "added": [ + "user1", + "user2", + "user3", + "user4" + ], + "removed": [], + "since": -1, + "till": 1585948850110 +} \ No newline at end of file diff --git a/client/src/test/resources/segment_2.json b/client/src/test/resources/segment_2.json new file mode 100644 index 00000000..7e8c81e7 --- /dev/null +++ b/client/src/test/resources/segment_2.json @@ -0,0 +1,10 @@ +{ + "name": "segment_2", + "added": [ + "user1", + "user4" + ], + "removed": [], + "since": -1, + "till": 1585948850110 +} \ No newline at end of file diff --git a/client/src/test/resources/segment_test.json b/client/src/test/resources/segment_test.json new file mode 100644 index 00000000..a1458fc4 --- /dev/null +++ b/client/src/test/resources/segment_test.json @@ -0,0 +1,12 @@ +{ + "name": "segment_test", + "added": [ + "user1", + "user2", + "user3", + "user4" + ], + "removed": [], + "since": -1, + "till": 1585948850110 +} \ No newline at end of file diff --git a/client/src/test/resources/semver/between-semver.csv b/client/src/test/resources/semver/between-semver.csv new file mode 100644 index 00000000..71bdf3b2 --- /dev/null +++ b/client/src/test/resources/semver/between-semver.csv @@ -0,0 +1,18 @@ +version1,version2,version3,expected +1.1.1,2.2.2,3.3.3,true +1.1.1-rc.1,1.1.1-rc.2,1.1.1-rc.3,true +1.0.0-alpha,1.0.0-alpha.1,1.0.0-alpha.beta,true +1.0.0-alpha.1,1.0.0-alpha.beta,1.0.0-beta,true +1.0.0-alpha.beta,1.0.0-beta,1.0.0-beta.2,true +1.0.0-beta,1.0.0-beta.2,1.0.0-beta.11,true +1.0.0-beta.2,1.0.0-beta.11,1.0.0-rc.1,true +1.0.0-beta.11,1.0.0-rc.1,1.0.0,true +1.1.2,1.1.3,1.1.4,true +1.2.1,1.3.1,1.4.1,true +2.0.0,3.0.0,4.0.0,true +2.2.2,2.2.3-rc1,2.2.3,true +2.2.2,2.3.2-rc100,2.3.3,true +1.0.0-rc.1+build.1,1.2.3-beta,1.2.3-rc.1+build.123,true +3.3.3,3.3.3-alpha,3.3.4,false +2.2.2-rc.1,2.2.2+metadata,2.2.2-rc.10,false +1.1.1-rc.1,1.1.1-rc.3,1.1.1-rc.2,false \ No newline at end of file diff --git a/client/src/test/resources/semver/equal-to-semver.csv b/client/src/test/resources/semver/equal-to-semver.csv new file mode 100644 index 00000000..87d8db5a --- /dev/null +++ b/client/src/test/resources/semver/equal-to-semver.csv @@ -0,0 +1,7 @@ +version1,version2,equals +1.1.1,1.1.1,true +1.1.1,1.1.1+metadata,false +1.1.1,1.1.1-rc.1,false +88.88.88,88.88.88,true +1.2.3----RC-SNAPSHOT.12.9.1--.12,1.2.3----RC-SNAPSHOT.12.9.1--.12,true +10.2.3-DEV-SNAPSHOT,10.2.3-SNAPSHOT-123,false \ No newline at end of file diff --git a/client/src/test/resources/semver/invalid-semantic-versions.csv b/client/src/test/resources/semver/invalid-semantic-versions.csv new file mode 100644 index 00000000..7a7f9fbc --- /dev/null +++ b/client/src/test/resources/semver/invalid-semantic-versions.csv @@ -0,0 +1,28 @@ +invalid +1 +1.2 +1.alpha.2 ++invalid +-invalid +-invalid+invalid +-invalid.01 +alpha +alpha.beta +alpha.beta.1 +alpha.1 +alpha+beta +alpha_beta +alpha. +alpha.. +beta +-alpha. +1.2 +1.2.3.DEV +1.2-SNAPSHOT +1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788 +1.2-RC-SNAPSHOT +-1.0.3-gamma+b7718 ++justmeta +1.1.1+ +1.1.1- +#99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12 \ No newline at end of file diff --git a/client/src/test/resources/semver/semver-splits.json b/client/src/test/resources/semver/semver-splits.json new file mode 100644 index 00000000..a266c567 --- /dev/null +++ b/client/src/test/resources/semver/semver-splits.json @@ -0,0 +1,740 @@ +{ "ff": { + "d":[ + { + "trafficTypeName":"user", + "name":"semver_between", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"BETWEEN_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":null, + "betweenStringMatcherData":{ + "start":"1.22.9", + "end":"2.1.0" + } + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label":"between semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":0 + }, + { + "treatment":"off", + "size":100 + } + ], + "label":"default rule" + } + ] + }, + { + "trafficTypeName":"user", + "name":"semver_equalto", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label":"equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":0 + }, + { + "treatment":"off", + "size":100 + } + ], + "label":"default rule" + } + ] + }, + { + "trafficTypeName":"user", + "name":"semver_greater_or_equalto", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"GREATER_THAN_OR_EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label":"greater than or equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":0 + }, + { + "treatment":"off", + "size":100 + } + ], + "label":"default rule" + } + ] + }, + { + "trafficTypeName":"user", + "name":"semver_inlist", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"IN_LIST_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":{ + "whitelist":[ + "1.22.9", + "2.1.0" + ] + }, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":null, + "betweenStringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label":"in list semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":0 + }, + { + "treatment":"off", + "size":100 + } + ], + "label":"default rule" + } + ] + }, + { + "trafficTypeName":"user", + "name":"semver_less_or_equalto", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"LESS_THAN_OR_EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label":"less than or equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":0 + }, + { + "treatment":"off", + "size":100 + } + ], + "label":"default rule" + } + ] + } + ], + "s":-1, + "t":1675259356568}, + "rbs": { + "t": 1675259356568, + "s": -1, + "d": [ + { + "trafficTypeName":"user", + "name":"rbs_semver_between", + "status":"ACTIVE", + "changeNumber":1675259356568, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"BETWEEN_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":null, + "betweenStringMatcherData":{ + "start":"1.22.9", + "end":"2.1.0" + } + } + ] + }, + "label":"between semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "label":"default rule" + } + ], + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + } + }, + { + "name":"rbs_semver_equalto", + "status":"ACTIVE", + "changeNumber":1675259356568, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + } + ] + }, + "label":"equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "label":"default rule" + } + ], + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + } + }, + { + "name":"rbs_semver_greater_or_equalto", + "status":"ACTIVE", + "defaultTreatment":"off", + "changeNumber":1675259356568, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[{ + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"GREATER_THAN_OR_EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + }]}, + "label":"greater than or equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ] + }, + "label":"default rule" + } + ], + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + } + }, + { + "trafficTypeName":"user", + "name":"rbs_semver_inlist", + "status":"ACTIVE", + "changeNumber":1675259356568, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"IN_LIST_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":{ + "whitelist":[ + "1.22.9", + "2.1.0" + ] + }, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":null, + "betweenStringMatcherData":null + }]}, + "label":"in list semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + } + ]}, + "label":"default rule" + }], + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + } + }, + { + "trafficTypeName":"user", + "name":"rbs_semver_less_or_equalto", + "trafficAllocation":100, + "trafficAllocationSeed":1068038034, + "seed":-1053389887, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"off", + "changeNumber":1675259356568, + "algo":2, + "configurations":null, + "conditions":[ + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":"version" + }, + "matcherType":"LESS_THAN_OR_EQUAL_TO_SEMVER", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "dependencyMatcherData":null, + "booleanMatcherData":null, + "stringMatcherData":"1.22.9" + }]}, + "label":"less than or equal to semver" + }, + { + "conditionType":"ROLLOUT", + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "keySelector":{ + "trafficType":"user", + "attribute":null + }, + "matcherType":"ALL_KEYS", + "negate":false, + "userDefinedSegmentMatcherData":null, + "whitelistMatcherData":null, + "unaryNumericMatcherData":null, + "betweenMatcherData":null, + "booleanMatcherData":null, + "dependencyMatcherData":null, + "stringMatcherData":null + }]}, + "label":"default rule" + }], + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + } + }] + } +} \ No newline at end of file diff --git a/client/src/test/resources/semver/valid-semantic-versions.csv b/client/src/test/resources/semver/valid-semantic-versions.csv new file mode 100644 index 00000000..f491e77f --- /dev/null +++ b/client/src/test/resources/semver/valid-semantic-versions.csv @@ -0,0 +1,25 @@ +higher,lower +1.1.2,1.1.1 +1.0.0,1.0.0-rc.1 +1.1.0-rc.1,1.0.0-beta.11 +1.0.0-beta.11,1.0.0-beta.2 +1.0.0-beta.2,1.0.0-beta +1.0.0-beta,1.0.0-alpha.beta +1.0.0-alpha.beta,1.0.0-alpha.1 +1.0.0-alpha.1,1.0.0-alpha +2.2.2-rc.2+metadata-lalala,2.2.2-rc.1.2 +1.2.3,0.0.4 +1.1.2+meta,1.1.2-prerelease+meta +1.0.0-beta,1.0.0-alpha +1.0.0-alpha0.valid,1.0.0-alpha.0valid +1.0.0-rc.1+build.1,1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay +10.2.3-DEV-SNAPSHOT,1.2.3-SNAPSHOT-123 +1.1.1-rc2,1.0.0-0A.is.legal +1.2.3----RC-SNAPSHOT.12.9.1--.12+788,1.2.3----R-S.12.9.1--.12+meta +1.2.3----RC-SNAPSHOT.12.9.1--.12.88,1.2.3----RC-SNAPSHOT.12.9.1--.12 +9223372036854775807.9223372036854775807.9223372036854775807,9223372036854775807.9223372036854775807.9223372036854775806 +1.1.1-alpha.beta.rc.build.java.pr.support.10,1.1.1-alpha.beta.rc.build.java.pr.support +1.1.2,1.1.1 +1.2.1,1.1.1 +2.1.1,1.1.1 +1.1.1-rc.1,1.1.1-rc.0 \ No newline at end of file diff --git a/client/src/test/resources/split-change-special-characters.json b/client/src/test/resources/split-change-special-characters.json index 276ab19e..ae99d7a7 100644 --- a/client/src/test/resources/split-change-special-characters.json +++ b/client/src/test/resources/split-change-special-characters.json @@ -1,5 +1,5 @@ -{ - "splits": [ +{ "ff": { + "d": [ { "trafficTypeName": "user", "name": "DEMO_MURMUR2", @@ -10,6 +10,10 @@ "killed": false, "defaultTreatment": "of", "changeNumber": 1491244291288, + "sets": [ + "set1", + "set2" + ], "algo": 2, "configurations": { "on": "{\"test\": \"blue\",\"grüne Straße\": 13}", @@ -50,6 +54,7 @@ ] } ], - "since": 1491244291288, - "till": 1491244291288 + "s": 1491244291288, + "t": 1491244291288}, + "rbs": {"d": [], "s": -1, "t": -1} } diff --git a/client/src/test/resources/splitFetcher/test_0.json b/client/src/test/resources/splitFetcher/test_0.json new file mode 100644 index 00000000..82e6bbad --- /dev/null +++ b/client/src/test/resources/splitFetcher/test_0.json @@ -0,0 +1,4 @@ +{"ff": {"d": +[{"trafficTypeName":"user","name":"SPLIT_1","trafficAllocation":100,"trafficAllocationSeed":-1780071202,"seed":-1442762199,"status":"ACTIVE","killed":false,"defaultTreatment":"off","changeNumber":1675443537882,"algo":2,"configurations":{},"conditions":[{"conditionType":"ROLLOUT","matcherGroup":{"combiner":"AND","matchers":[{"keySelector":{"trafficType":"user","attribute":null},"matcherType":"ALL_KEYS","negate":false,"userDefinedSegmentMatcherData":null,"whitelistMatcherData":null,"unaryNumericMatcherData":null,"betweenMatcherData":null,"booleanMatcherData":null,"dependencyMatcherData":null,"stringMatcherData":null}]},"partitions":[{"treatment":"on","size":0},{"treatment":"off","size":100}],"label":"default rule"}]},{"trafficTypeName":"user","name":"SPLIT_2","trafficAllocation":100,"trafficAllocationSeed":-1780071202,"seed":-1442762199,"status":"ACTIVE","killed":false,"defaultTreatment":"off","changeNumber":1675443537882,"algo":2,"configurations":{},"conditions":[{"conditionType":"ROLLOUT","matcherGroup":{"combiner":"AND","matchers":[{"keySelector":{"trafficType":"user","attribute":null},"matcherType":"ALL_KEYS","negate":false,"userDefinedSegmentMatcherData":null,"whitelistMatcherData":null,"unaryNumericMatcherData":null,"betweenMatcherData":null,"booleanMatcherData":null,"dependencyMatcherData":null,"stringMatcherData":null}]},"partitions":[{"treatment":"on","size":0},{"treatment":"off","size":100}],"label":"default rule"}]}], + "since":-1,"till":-1 +}, "rbs":{"d": [], "s": -1, "t": -1}} \ No newline at end of file diff --git a/client/src/test/resources/split_compact.yml b/client/src/test/resources/split_compact.yml new file mode 100644 index 00000000..bff410b0 --- /dev/null +++ b/client/src/test/resources/split_compact.yml @@ -0,0 +1,3 @@ +- split_1: {keys: user_c, treatment: 'off', config: '{ "size" : 10 }'} +- split_1: {keys: user_d, treatment: 'on'} +- split_2: {keys: user_e, treatment: 'off', config: '{ "size" : 55 }'} \ No newline at end of file diff --git a/client/src/test/resources/split_init.json b/client/src/test/resources/split_init.json new file mode 100644 index 00000000..01d57975 --- /dev/null +++ b/client/src/test/resources/split_init.json @@ -0,0 +1,605 @@ +{"ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "split_1", + "trafficAllocation": 100, + "trafficAllocationSeed": -1364119282, + "seed": -605938843, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1660326991072, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 50 + }, + { + "treatment": "off", + "size": 50 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_2", + "trafficAllocation": 100, + "trafficAllocationSeed": -92391491, + "seed": -1769377604, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1651003069855, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin", + "user_1", + "user_2" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "v5", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "IN_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "segment_1" + }, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V4", + "size": 0 + }, + { + "treatment": "v5", + "size": 0 + } + ], + "label": "in segment segment_1" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V4", + "size": 0 + }, + { + "treatment": "v5", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_3", + "trafficAllocation": 100, + "trafficAllocationSeed": -670005248, + "seed": -1297078412, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1650919058695, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V5", + "size": 0 + }, + { + "treatment": "v8", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_4", + "trafficAllocation": 50, + "trafficAllocationSeed": -1520910077, + "seed": -1785086567, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1647274074042, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_5", + "trafficAllocation": 100, + "trafficAllocationSeed": -3629915, + "seed": 816031817, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1622494310037, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "seba", + "tincho" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "user_3" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_6", + "trafficAllocation": 100, + "trafficAllocationSeed": -970151859, + "seed": -1258287669, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1605020019151, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_7", + "trafficAllocation": 100, + "trafficAllocationSeed": 291807630, + "seed": -134149800, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1603461301902, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + } + ], + "s": -1, + "t": 1660326991072 +}, "rbs":{"d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + }, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "IN_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "segment_2" + }, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + } + } + ] + } +], "s": -1, "t": -1}} \ No newline at end of file diff --git a/client/src/test/resources/split_old_spec.json b/client/src/test/resources/split_old_spec.json new file mode 100644 index 00000000..66a05ca8 --- /dev/null +++ b/client/src/test/resources/split_old_spec.json @@ -0,0 +1,566 @@ +{"splits": [ + { + "trafficTypeName": "user", + "name": "split_1", + "trafficAllocation": 100, + "trafficAllocationSeed": -1364119282, + "seed": -605938843, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1660326991072, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 50 + }, + { + "treatment": "off", + "size": 50 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_2", + "trafficAllocation": 100, + "trafficAllocationSeed": -92391491, + "seed": -1769377604, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1651003069855, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin", + "user_1", + "user_2" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "v5", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "IN_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "segment_1" + }, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V4", + "size": 0 + }, + { + "treatment": "v5", + "size": 0 + } + ], + "label": "in segment segment_1" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V4", + "size": 0 + }, + { + "treatment": "v5", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_3", + "trafficAllocation": 100, + "trafficAllocationSeed": -670005248, + "seed": -1297078412, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1650919058695, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + }, + { + "treatment": "V5", + "size": 0 + }, + { + "treatment": "v8", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_4", + "trafficAllocation": 50, + "trafficAllocationSeed": -1520910077, + "seed": -1785086567, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1647274074042, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_5", + "trafficAllocation": 100, + "trafficAllocationSeed": -3629915, + "seed": 816031817, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1622494310037, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "seba", + "tincho" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "user_3" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "off", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_6", + "trafficAllocation": 100, + "trafficAllocationSeed": -970151859, + "seed": -1258287669, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1605020019151, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "split_7", + "trafficAllocation": 100, + "trafficAllocationSeed": 291807630, + "seed": -134149800, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1603461301902, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + } + ], + "since": -1, + "till": 1660326991072 +} \ No newline at end of file diff --git a/client/src/test/resources/splits.json b/client/src/test/resources/splits.json index de9696b4..da2654f1 100644 --- a/client/src/test/resources/splits.json +++ b/client/src/test/resources/splits.json @@ -1,5 +1,5 @@ -{ - "splits": [ +{"ff": { + "d": [ { "trafficTypeName": "user", "name": "push_test", @@ -224,8 +224,113 @@ "label": "default label" } ] + }, + { + "changeNumber": 10, + "trafficTypeName": "user", + "name": "rbs_flag", + "trafficAllocation": 100, + "trafficAllocationSeed": 1828377380, + "seed": -286617921, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "algo": 2, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "IN_RULE_BASED_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "sample_rule_based_segment" + } + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "in rule based segment sample_rule_based_segment" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "ALL_KEYS", + "negate": false + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ], + "configurations": {}, + "sets": [], + "impressionsDisabled": false } ], - "since": -1, - "till": 1585948850109 + "s": -1, + "t": 1585948850109 +}, "rbs":{"d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + }, + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ] + }], "s": -1, "t": 1585948850109} } diff --git a/client/src/test/resources/splits2.json b/client/src/test/resources/splits2.json index a01787d4..afbc9299 100644 --- a/client/src/test/resources/splits2.json +++ b/client/src/test/resources/splits2.json @@ -1,5 +1,5 @@ -{ - "splits": [ +{"ff": { + "d": [ { "trafficTypeName": "user", "name": "push_test", @@ -115,6 +115,6 @@ ] } ], - "since": 1585948850110, - "till": 1585948850111 -} \ No newline at end of file + "s": 1585948850110, + "t": 1585948850111 +}, "rbs":{"d": [], "s": 1585948850110, "t": 1585948850111}} \ No newline at end of file diff --git a/client/src/test/resources/splits_imp_toggle.json b/client/src/test/resources/splits_imp_toggle.json new file mode 100644 index 00000000..18c5b0aa --- /dev/null +++ b/client/src/test/resources/splits_imp_toggle.json @@ -0,0 +1,156 @@ +{ + "ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "without_impression_toggle", + "trafficAllocation": 24, + "trafficAllocationSeed": -172559061, + "seed": -906334215, + "status": "ACTIVE", + "killed": true, + "defaultTreatment": "off", + "changeNumber": 1585948717645, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "impression_toggle_on", + "trafficAllocation": 24, + "trafficAllocationSeed": -172559061, + "seed": -906334215, + "status": "ACTIVE", + "killed": true, + "defaultTreatment": "off", + "changeNumber": 1585948717645, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ], + "impressionsDisabled": false + }, + { + "trafficTypeName": "user", + "name": "impression_toggle_off", + "trafficAllocation": 24, + "trafficAllocationSeed": -172559061, + "seed": -906334215, + "status": "ACTIVE", + "killed": true, + "defaultTreatment": "off", + "changeNumber": 1585948717645, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ], + "impressionsDisabled": true + } + ], + "s": -1, + "t": 1602796638344 + }, "rbs": {"s": -1, "t": -1, "d": []}} diff --git a/client/src/test/resources/splits_killed.json b/client/src/test/resources/splits_killed.json index 13eed1a6..6924afc6 100644 --- a/client/src/test/resources/splits_killed.json +++ b/client/src/test/resources/splits_killed.json @@ -1,5 +1,5 @@ -{ - "splits": [ +{"ff": { + "d": [ { "trafficTypeName": "user", "name": "push_test", @@ -86,6 +86,6 @@ ] } ], - "since": 1585948850111, - "till": 1585948850112 -} \ No newline at end of file + "s": 1585948850111, + "t": 1585948850112 +}, "rbs":{"d": [], "s": 1585948850111, "t": 1585948850112}} \ No newline at end of file diff --git a/client/src/test/resources/splits_localhost.json b/client/src/test/resources/splits_localhost.json new file mode 100644 index 00000000..6f4abdb2 --- /dev/null +++ b/client/src/test/resources/splits_localhost.json @@ -0,0 +1,295 @@ +{"ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "push_test", + "trafficAllocation": 100, + "trafficAllocationSeed": -2092979940, + "seed": 105482719, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "on_default", + "changeNumber": 1585948850109, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "admin", + "mauro" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on_whitelist", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + }, + { + "treatment": "V1", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "tinchotest", + "trafficAllocation": 24, + "trafficAllocationSeed": -172559061, + "seed": -906334215, + "status": "ACTIVE", + "killed": true, + "defaultTreatment": "off", + "changeNumber": 1585948717645, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "trafficTypeName": "user", + "name": "test_split", + "trafficAllocation": 100, + "trafficAllocationSeed": 1582960494, + "seed": 1842944006, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "changeNumber": 1582741588594, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ] + }, + { + "changeNumber": 10, + "trafficTypeName": "user", + "name": "rbs_flag", + "trafficAllocation": 100, + "trafficAllocationSeed": 1828377380, + "seed": -286617921, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "algo": 2, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "IN_RULE_BASED_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "sample_rule_based_segment" + } + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "in rule based segment sample_rule_based_segment" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "ALL_KEYS", + "negate": false + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ], + "configurations": {}, + "sets": [], + "impressionsDisabled": false + } + ], + "s": -1, + "t": -1 +}, "rbs":{"d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + }, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ] + }], "s": -1, "t": -1} +} diff --git a/client/src/test/resources/splits_prereq.json b/client/src/test/resources/splits_prereq.json new file mode 100644 index 00000000..5efa7fed --- /dev/null +++ b/client/src/test/resources/splits_prereq.json @@ -0,0 +1,293 @@ +{"ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "test_prereq", + "prerequisites": [ + { "n": "feature_segment", "ts": ["off", "def_test"] }, + { "n": "rbs_flag", "ts": ["on"] } + ], + "trafficAllocation": 100, + "trafficAllocationSeed": 1582960494, + "seed": 1842944006, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "def_treatment", + "changeNumber": 1582741588594, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "default rule" + } + ] + }, + { + "name":"feature_segment", + "trafficTypeId":"u", + "trafficTypeName":"User", + "trafficAllocation": 100, + "trafficAllocationSeed": 1582960494, + "seed":-1177551240, + "status":"ACTIVE", + "killed":false, + "defaultTreatment":"def_test", + "changeNumber": 1582741588594, + "algo": 2, + "configurations": {}, + "conditions":[ + { + "matcherGroup":{ + "combiner":"AND", + "matchers":[ + { + "matcherType":"IN_SEGMENT", + "negate":false, + "userDefinedSegmentMatcherData":{ + "segmentName":"segment-test" + }, + "whitelistMatcherData":null + } + ] + }, + "partitions":[ + { + "treatment":"on", + "size":100 + }, + { + "treatment":"off", + "size":0 + } + ], + "label": "default label" + } + ] + }, + { + "changeNumber": 10, + "trafficTypeName": "user", + "name": "rbs_flag", + "trafficAllocation": 100, + "trafficAllocationSeed": 1828377380, + "seed": -286617921, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "off", + "algo": 2, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "IN_RULE_BASED_SEGMENT", + "negate": false, + "userDefinedSegmentMatcherData": { + "segmentName": "sample_rule_based_segment" + } + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + } + ], + "label": "in rule based segment sample_rule_based_segment" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user" + }, + "matcherType": "ALL_KEYS", + "negate": false + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "off", + "size": 100 + } + ], + "label": "default rule" + } + ], + "configurations": {}, + "sets": [], + "impressionsDisabled": false + }, + { + "trafficTypeName": "user", + "name": "prereq_chain", + "prerequisites": [ + { "n": "test_prereq", "ts": ["on"] } + ], + "trafficAllocation": 100, + "trafficAllocationSeed": -2092979940, + "seed": 105482719, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "on_default", + "changeNumber": 1585948850109, + "algo": 2, + "configurations": {}, + "conditions": [ + { + "conditionType": "WHITELIST", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": null, + "matcherType": "WHITELIST", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": { + "whitelist": [ + "bilal@split.io" + ] + }, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on_whitelist", + "size": 100 + } + ], + "label": "whitelisted" + }, + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null, + "booleanMatcherData": null, + "dependencyMatcherData": null, + "stringMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 100 + }, + { + "treatment": "off", + "size": 0 + }, + { + "treatment": "V1", + "size": 0 + } + ], + "label": "default rule" + } + ] + } + ], + "s": -1, + "t": 1585948850109 +}, "rbs":{"d": [ + { + "changeNumber": 5, + "name": "sample_rule_based_segment", + "status": "ACTIVE", + "trafficTypeName": "user", + "excluded":{ + "keys":["mauro@split.io","gaston@split.io"], + "segments":[] + }, + "conditions": [ + { + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": "email" + }, + "matcherType": "ENDS_WITH", + "negate": false, + "whitelistMatcherData": { + "whitelist": [ + "@split.io" + ] + } + } + ] + } + } + ] + }], "s": -1, "t": 1585948850109} +} diff --git a/client/src/test/resources/streaming-auth-push-disabled-empty-token.json b/client/src/test/resources/streaming-auth-push-disabled-empty-token.json new file mode 100644 index 00000000..d40fd01c --- /dev/null +++ b/client/src/test/resources/streaming-auth-push-disabled-empty-token.json @@ -0,0 +1 @@ +{"pushEnabled":false,"token":""} \ No newline at end of file diff --git a/e2e/.gitignore b/e2e/.gitignore new file mode 100644 index 00000000..67bcc2f7 --- /dev/null +++ b/e2e/.gitignore @@ -0,0 +1,2 @@ +.gradle/ +build/ diff --git a/e2e/README.md b/e2e/README.md new file mode 100644 index 00000000..6750c038 --- /dev/null +++ b/e2e/README.md @@ -0,0 +1,30 @@ +# e2e + +Mimics a real consumer of java-client. It builds the SDK, installs it to mavenLocal, then consumes it from there +in a separate Gradle build: the `non-shaded` variant (`test`) and the fat jar (`fatJarTest`). Nothing in the +module provides `io.harness:events` or the other libraries the artifacts are meant to bundle. + +Each variant has three tests: it initialises the SDK in localhost mode, checks the jar's contents (bundled and +relocated libraries), and runs `jdeps` to fail on any referenced class that can't be resolved. + +``` +cd e2e && ./gradlew test fatJarTest +``` + +Requires Maven and JDK 17 on the path. The SDK version is read from the parent `pom.xml`. + +## Released testing compatibility + +`ReleasedTestingCompatE2ETest` checks the published `io.split.client:java-client-testing` release (pinned as +`compatTestingVersion` in `build.gradle`, resolved from Maven Central only) against the SDK built from this tree. It fails +when that release implements a `java-client` interface without the methods the SDK now requires, which would throw +`AbstractMethodError` at runtime for consumers. The failure message lists each missing method and the two ways to fix it: + +1. Keep the SDK change compatible with the pinned testing release. +2. Release a new `java-client-testing` built against this SDK, then set `compatTestingVersion` to it. The old version + is then unsupported with this SDK. + +The check only covers methods the released jar implements; behavioural changes need their own tests. To check a local +testing build without publishing it, run `./gradlew test -Pcompat.testing.version=X` after installing it to mavenLocal; +the override also lifts the Maven Central restriction, so mavenLocal is consulted. Without it, the default version +is resolved from Maven Central only. diff --git a/e2e/build.gradle b/e2e/build.gradle new file mode 100644 index 00000000..70d17ef9 --- /dev/null +++ b/e2e/build.gradle @@ -0,0 +1,122 @@ +// Mimics a real consumer of java-client: resolves it from mavenLocal, once with the +// `non-shaded` classifier and once as the fat jar. It builds and installs java-client first, so the test +// always runs against the current source. Nothing here provides io.harness:events: a real +// consumer only gets what the published pom declares, so the artifact must bundle it. +plugins { + id 'java' +} + +// Single source of truth: the parent pom's own version. +def sdkVersion = new XmlSlurper().parse(file('../pom.xml')).version.text() +assert sdkVersion : 'could not read the version from ../pom.xml' + +// The released java-client-testing that consumers use, checked against this SDK by ReleasedTestingCompatE2ETest. +// Bump it when a new testing release supersedes this one; -Pcompat.testing.version=X overrides it for a local check. +def compatTestingVersion = project.findProperty('compat.testing.version') ?: '5.0.1' + +// The local Maven repo, shared explicitly with the `mvn` calls below. In CI, HOME (where the events install lands) +// differs from the JVM's user.home, so mavenLocal() alone would look in a different repo than Maven installed to. +def localRepo = System.getProperty('maven.repo.local') ?: "${System.getenv('HOME') ?: System.getProperty('user.home')}/.m2/repository" + +repositories { + // The released java-client-testing comes from Maven Central only. mavenLocal can hold a local build of the same + // version (from this tree), which would hide a break in the published jar. An explicit -Pcompat.testing.version + // is a local check of an unpublished build, so it lifts the restriction and resolves from mavenLocal. + if (!project.hasProperty('compat.testing.version')) { + exclusiveContent { + forRepository { + mavenCentral() + } + filter { + includeModule 'io.split.client', 'java-client-testing' + } + } + } + maven { + url = uri(localRepo) + // Only what this build installs; Maven keeps poms without jars for everything else it resolved. + content { includeGroup 'io.split.client' } + } + mavenCentral() +} + +dependencies { + testImplementation("io.split.client:java-client:${sdkVersion}:non-shaded") { + exclude group: 'org.eclipse.jetty' + exclude group: 'javax.servlet', module: 'servlet-api' + // mavenLocal may hold io.harness:events from a local jvm-commons build; never let it mask a missing bundle. + exclude group: 'io.harness' + } + // Required by the non-shaded java-client (a real non-shaded consumer declares these itself). + testImplementation 'com.google.guava:guava:33.4.8-jre' + testImplementation 'org.apache.httpcomponents.client5:httpclient5:5.5' + // Only the localhost YAML file this test reads needs it; the real SDK path does not. + testImplementation 'org.yaml:snakeyaml:2.0' + testImplementation 'com.google.code.gson:gson:2.13.1' + testImplementation 'org.slf4j:slf4j-simple:1.7.36' + testImplementation 'junit:junit:4.13.1' + // Its own java-client dependency is excluded: the SDK built from this tree is the only one on the classpath. + testImplementation("io.split.client:java-client-testing:${compatTestingVersion}") { + exclude group: 'io.split.client', module: 'java-client' + } +} + +// The default (fully shaded) artifact has its own source set: the same coordinates resolve with and without the +// `non-shaded` classifier, which one configuration cannot do. +sourceSets { + fatJar // src/fatJar/{java,resources} +} + +dependencies { + // Deliberately nothing but the SDK: the fat jar must carry guava, httpclient5 and snakeyaml itself. + fatJarImplementation "io.split.client:java-client:${sdkVersion}" + fatJarImplementation 'org.slf4j:slf4j-simple:1.7.36' + fatJarImplementation 'junit:junit:4.13.1' +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +// Install the SDK modules into ~/.m2 before the tests resolve them. +tasks.register('installJavaClient', Exec) { + workingDir = file('..') + // Build from clean, then install again without `clean`: an incremental build is where the plain module jar used to + // be reused after shading, turning the non-shaded artifact into the fat jar (see forceCreation in client/pom.xml). + def mvn = "mvn --batch-mode -q install -DskipTests -Dcheckstyle.skip=true -Dmaven.repo.local=${localRepo}" + commandLine 'sh', '-c', "${mvn.replace(' install', ' clean install')} && ${mvn}" + inputs.files(fileTree('../client/src/main'), file('../client/pom.xml'), file('../pom.xml')) + outputs.upToDateWhen { false } +} + +// compileTestJava resolves the artifact, so the install has to come before it, not just before `test`. +tasks.named('compileTestJava') { + dependsOn 'installJavaClient' +} +tasks.named('compileFatJarJava') { + dependsOn 'installJavaClient' +} + +tasks.register('fatJarTest', Test) { + description = 'Runs the e2e tests against the default (fully shaded) java-client artifact.' + group = 'verification' + testClassesDirs = sourceSets.fatJar.output.classesDirs + classpath = sourceSets.fatJar.runtimeClasspath + testLogging { + events 'passed', 'failed' + exceptionFormat 'full' + } +} + +tasks.named('test') { + // `./gradlew test` covers both artifacts; the finalizer runs even when this task fails. + finalizedBy 'fatJarTest' + systemProperty 'compat.testing.version', compatTestingVersion + systemProperty 'compat.sdk.version', sdkVersion + testLogging { + events 'passed', 'failed' + exceptionFormat 'full' + showStandardStreams = true + } +} diff --git a/e2e/gradle/wrapper/gradle-wrapper.jar b/e2e/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..d64cd491 Binary files /dev/null and b/e2e/gradle/wrapper/gradle-wrapper.jar differ diff --git a/e2e/gradle/wrapper/gradle-wrapper.properties b/e2e/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..1af9e093 --- /dev/null +++ b/e2e/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/e2e/gradlew b/e2e/gradlew new file mode 100755 index 00000000..1aa94a42 --- /dev/null +++ b/e2e/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/e2e/gradlew.bat b/e2e/gradlew.bat new file mode 100644 index 00000000..6689b85b --- /dev/null +++ b/e2e/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/e2e/settings.gradle b/e2e/settings.gradle new file mode 100644 index 00000000..d0cf9b62 --- /dev/null +++ b/e2e/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'java-client-e2e' diff --git a/e2e/src/fatJar/java/io/split/e2e/FatJarConsumerE2ETest.java b/e2e/src/fatJar/java/io/split/e2e/FatJarConsumerE2ETest.java new file mode 100644 index 00000000..a4782d9e --- /dev/null +++ b/e2e/src/fatJar/java/io/split/e2e/FatJarConsumerE2ETest.java @@ -0,0 +1,37 @@ +package io.split.e2e; + +import io.split.client.SplitClient; +import io.split.client.SplitClientConfig; +import io.split.client.SplitFactory; +import io.split.client.SplitFactoryBuilder; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URL; + +/** + * Initialises the SDK from the default (fully shaded) artifact with nothing else on the classpath: guava, httpclient5 + * and snakeyaml must all come from inside the jar, which is what the fat jar promises its consumers. + */ +public class FatJarConsumerE2ETest { + + @Test + public void initialisesAndEvaluatesFromTheFatJarAlone() throws Exception { + URL splits = getClass().getClassLoader().getResource("splits.yaml"); + Assert.assertNotNull("splits.yaml test resource missing", splits); + + SplitClientConfig config = SplitClientConfig.builder() + .splitFile(splits.getPath()) + .setBlockUntilReadyTimeout(10000) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("localhost", config); + try { + SplitClient client = factory.client(); + client.blockUntilReady(); + Assert.assertEquals("on", client.getTreatment("any_user", "e2e_feature")); + } finally { + factory.destroy(); + } + } +} diff --git a/e2e/src/fatJar/java/io/split/e2e/FatJarShapeE2ETest.java b/e2e/src/fatJar/java/io/split/e2e/FatJarShapeE2ETest.java new file mode 100644 index 00000000..0a86e5c7 --- /dev/null +++ b/e2e/src/fatJar/java/io/split/e2e/FatJarShapeE2ETest.java @@ -0,0 +1,54 @@ +package io.split.e2e; + +import io.split.client.SplitFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +/** + * Pins what the default artifact is: every bundled library present under its relocated name and none left under its + * original one, where it would clash with the customer's own copy. + */ +public class FatJarShapeE2ETest { + + private static final String[] RELOCATED = {"split/com/google/", "split/org/apache/", "split/org/yaml/", + "split/org/jspecify/", "split/io/harness/events/"}; + private static final String[] MUST_NOT_LEAK = {"com/google/", "org/apache/", "org/yaml/", "org/jspecify/", + "io/harness/"}; + + @Test + public void bundlesEveryLibraryRelocatedAndLeaksNone() throws Exception { + File jar = new File(SplitFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + Assert.assertTrue("expected the fat jar on the classpath, got " + jar, + jar.getName().startsWith("java-client-") && !jar.getName().contains("non-shaded")); + + boolean[] found = new boolean[RELOCATED.length]; + List leaked = new ArrayList<>(); + try (JarFile jarFile = new JarFile(jar)) { + for (Enumeration entries = jarFile.entries(); entries.hasMoreElements(); ) { + String name = entries.nextElement().getName(); + for (int i = 0; i < RELOCATED.length; i++) { + found[i] |= name.startsWith(RELOCATED[i]); + } + for (String prefix : MUST_NOT_LEAK) { + if (name.startsWith(prefix)) { + leaked.add(name); + break; + } + } + } + } + + for (int i = 0; i < RELOCATED.length; i++) { + Assert.assertTrue(RELOCATED[i] + " is missing from " + jar, found[i]); + } + Assert.assertTrue("unrelocated classes in " + jar + " (" + leaked.size() + " entries, e.g. " + + (leaked.isEmpty() ? "" : leaked.get(0)) + ")", leaked.isEmpty()); + } +} diff --git a/e2e/src/fatJar/java/io/split/e2e/FatJarUnresolvedReferencesE2ETest.java b/e2e/src/fatJar/java/io/split/e2e/FatJarUnresolvedReferencesE2ETest.java new file mode 100644 index 00000000..92bfc0f6 --- /dev/null +++ b/e2e/src/fatJar/java/io/split/e2e/FatJarUnresolvedReferencesE2ETest.java @@ -0,0 +1,49 @@ +package io.split.e2e; + +import io.split.client.SplitFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** + * The fat jar must be self-contained, so jdeps gets no classpath. + * Runs jdeps over the jar and fails on any referenced class that cannot be resolved, so a package added to the + * SDK but left out of the build's include list is caught even though initialising the SDK would still work. + * Only references from the SDK's own classes count: the bundled libraries carry optional annotation references of + * their own. The optional pluggable-storage module and slf4j (a declared dependency) are the only expected gaps. + */ +public class FatJarUnresolvedReferencesE2ETest { + + @Test + public void everyReferencedClassIsResolvable() throws Exception { + File jar = new File(SplitFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + + List command = new ArrayList<>(); + command.add(new File(System.getProperty("java.home"), "bin/jdeps").getPath()); + command.add("--multi-release"); + command.add("base"); + command.add("-verbose:class"); + + command.add(jar.getPath()); + Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); + + List unresolved = new ArrayList<>(); + StringBuilder output = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { + for (String line; (line = reader.readLine()) != null; ) { + output.append(line).append('\n'); + if (line.endsWith("not found") && line.trim().startsWith("io.split.") && !line.contains("-> pluggable.") && !line.contains("-> org.slf4j.")) { + unresolved.add(line.trim()); + } + } + } + Assert.assertEquals("jdeps failed:\n" + output, 0, process.waitFor()); + Assert.assertTrue("classes referenced but missing from the self-contained fat jar " + jar + ": " + unresolved, unresolved.isEmpty()); + } +} diff --git a/e2e/src/fatJar/resources/splits.yaml b/e2e/src/fatJar/resources/splits.yaml new file mode 100644 index 00000000..cf943790 --- /dev/null +++ b/e2e/src/fatJar/resources/splits.yaml @@ -0,0 +1,2 @@ +- e2e_feature: + treatment: 'on' diff --git a/e2e/src/test/java/io/split/e2e/NonShadedConsumerE2ETest.java b/e2e/src/test/java/io/split/e2e/NonShadedConsumerE2ETest.java new file mode 100644 index 00000000..bcba2bc8 --- /dev/null +++ b/e2e/src/test/java/io/split/e2e/NonShadedConsumerE2ETest.java @@ -0,0 +1,38 @@ +package io.split.e2e; + +import io.split.client.SplitClient; +import io.split.client.SplitClientConfig; +import io.split.client.SplitFactory; +import io.split.client.SplitFactoryBuilder; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URL; + +/** + * Initialises the SDK the way a consumer of the published non-shaded artifact does (see build.gradle). + * Localhost mode needs no network or keys, yet still builds the SDK events manager, so a class the + * artifact does not bundle or declare surfaces here as a NoClassDefFoundError. + */ +public class NonShadedConsumerE2ETest { + + @Test + public void initialisesAndEvaluatesFromTheNonShadedArtifact() throws Exception { + URL splits = getClass().getClassLoader().getResource("splits.yaml"); + Assert.assertNotNull("splits.yaml test resource missing", splits); + + SplitClientConfig config = SplitClientConfig.builder() + .splitFile(splits.getPath()) + .setBlockUntilReadyTimeout(10000) + .build(); + + SplitFactory factory = SplitFactoryBuilder.build("localhost", config); + try { + SplitClient client = factory.client(); + client.blockUntilReady(); + Assert.assertEquals("on", client.getTreatment("any_user", "e2e_feature")); + } finally { + factory.destroy(); + } + } +} diff --git a/e2e/src/test/java/io/split/e2e/NonShadedJarShapeE2ETest.java b/e2e/src/test/java/io/split/e2e/NonShadedJarShapeE2ETest.java new file mode 100644 index 00000000..b4f0f29b --- /dev/null +++ b/e2e/src/test/java/io/split/e2e/NonShadedJarShapeE2ETest.java @@ -0,0 +1,53 @@ +package io.split.e2e; + +import io.split.client.SplitFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +/** + * Pins what the `non-shaded` artifact is: the thin jar plus the relocated io.harness:events, and nothing else + * bundled. Initialising the SDK (NonShadedConsumerE2ETest) cannot tell this apart from the fat jar, which also + * works, so a build that silently produces the fat jar under the non-shaded name would otherwise go unnoticed. + */ +public class NonShadedJarShapeE2ETest { + + private static final String[] BUNDLED_LIBRARIES = {"split/com/google/", "split/org/apache/", "split/org/yaml/", + "split/org/jspecify/"}; + + @Test + public void bundlesOnlyTheRelocatedEventsLibrary() throws Exception { + File jar = new File(SplitFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + Assert.assertTrue("expected the non-shaded jar on the classpath, got " + jar, jar.getName().endsWith("-non-shaded.jar")); + + boolean hasEventsConfig = false; + List unrelocatedEvents = new ArrayList<>(); + List bundledLibraries = new ArrayList<>(); + try (JarFile jarFile = new JarFile(jar)) { + for (Enumeration entries = jarFile.entries(); entries.hasMoreElements(); ) { + String name = entries.nextElement().getName(); + hasEventsConfig |= name.equals("split/io/harness/events/EventsManagerConfig.class"); + if (name.startsWith("io/harness/")) { + unrelocatedEvents.add(name); + } + for (String library : BUNDLED_LIBRARIES) { + if (name.startsWith(library)) { + bundledLibraries.add(name); + break; + } + } + } + } + + Assert.assertTrue("relocated io.harness.events classes are missing from " + jar, hasEventsConfig); + Assert.assertTrue("unrelocated io.harness classes in " + jar + ": " + unrelocatedEvents, unrelocatedEvents.isEmpty()); + Assert.assertTrue(jar + " bundles libraries that consumers of the non-shaded variant provide themselves (" + bundledLibraries.size() + + " entries, e.g. " + (bundledLibraries.isEmpty() ? "" : bundledLibraries.get(0)) + ")", bundledLibraries.isEmpty()); + } +} diff --git a/e2e/src/test/java/io/split/e2e/NonShadedUnresolvedReferencesE2ETest.java b/e2e/src/test/java/io/split/e2e/NonShadedUnresolvedReferencesE2ETest.java new file mode 100644 index 00000000..476adbb3 --- /dev/null +++ b/e2e/src/test/java/io/split/e2e/NonShadedUnresolvedReferencesE2ETest.java @@ -0,0 +1,49 @@ +package io.split.e2e; + +import io.split.client.SplitFactory; +import org.junit.Assert; +import org.junit.Test; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** + * The non-shaded jar leaves guava, httpclient5, snakeyaml and gson to the consumer, so they are resolved from the test classpath. + * Runs jdeps over the jar and fails on any referenced class that cannot be resolved, so a package added to the + * SDK but left out of the build's include list is caught even though initialising the SDK would still work. + * The optional pluggable-storage module is the only expected gap. + */ +public class NonShadedUnresolvedReferencesE2ETest { + + @Test + public void everyReferencedClassIsResolvable() throws Exception { + File jar = new File(SplitFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + + List command = new ArrayList<>(); + command.add(new File(System.getProperty("java.home"), "bin/jdeps").getPath()); + command.add("--multi-release"); + command.add("base"); + command.add("-verbose:class"); + command.add("-cp"); + command.add(System.getProperty("java.class.path")); + command.add(jar.getPath()); + Process process = new ProcessBuilder(command).redirectErrorStream(true).start(); + + List unresolved = new ArrayList<>(); + StringBuilder output = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { + for (String line; (line = reader.readLine()) != null; ) { + output.append(line).append('\n'); + if (line.endsWith("not found") && !line.startsWith(jar.getName()) && !line.contains("-> pluggable.")) { + unresolved.add(line.trim()); + } + } + } + Assert.assertEquals("jdeps failed:\n" + output, 0, process.waitFor()); + Assert.assertTrue("classes referenced but missing from the non-shaded jar and from the consumer dependencies, in " + jar + ": " + unresolved, unresolved.isEmpty()); + } +} diff --git a/e2e/src/test/java/io/split/e2e/ReleasedTestingCompatE2ETest.java b/e2e/src/test/java/io/split/e2e/ReleasedTestingCompatE2ETest.java new file mode 100644 index 00000000..e44d2b16 --- /dev/null +++ b/e2e/src/test/java/io/split/e2e/ReleasedTestingCompatE2ETest.java @@ -0,0 +1,120 @@ +package io.split.e2e; + +import io.split.client.testing.SplitClientForTest; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +/** + * Checks the released java-client-testing jar (pinned in build.gradle) against the java-client built from this tree. + * Every java-client interface method that a testing class implements must have a concrete implementation in that class. + * A method added to a java-client interface after the testing release is not implemented by it, and nothing fails at + * compile or load time: the call throws AbstractMethodError later. This test is the only place that surfaces it. + */ +public class ReleasedTestingCompatE2ETest { + + private static final String TESTING_PACKAGE = "io/split/client/testing/"; + private static final String TESTING_CLASS_PACKAGE = "io.split.client.testing."; + + @Test + public void releasedTestingImplementsEveryJavaClientInterfaceMethod() throws Exception { + String testingVersion = System.getProperty("compat.testing.version"); + String sdkVersion = System.getProperty("compat.sdk.version"); + Assert.assertNotNull("compat.testing.version system property missing (set in e2e/build.gradle)", testingVersion); + Assert.assertNotNull("compat.sdk.version system property missing (set in e2e/build.gradle)", sdkVersion); + + File jar = new File(SplitClientForTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + Assert.assertEquals("expected the java-client-testing " + testingVersion + " jar on the test classpath, found " + jar.getName(), + "java-client-testing-" + testingVersion + ".jar", jar.getName()); + + List missing = new ArrayList<>(); + try (JarFile jarFile = new JarFile(jar)) { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + String entry = entries.nextElement().getName(); + if (!entry.startsWith(TESTING_PACKAGE) || !entry.endsWith(".class")) { + continue; + } + String className = entry.substring(0, entry.length() - ".class".length()).replace('/', '.'); + Class type = Class.forName(className, false, getClass().getClassLoader()); + if (type.isInterface() || Modifier.isAbstract(type.getModifiers())) { + continue; + } + for (Class sdkInterface : sdkInterfaces(type)) { + for (Method method : sdkInterface.getDeclaredMethods()) { + if (Modifier.isAbstract(method.getModifiers()) && !isImplemented(type, method)) { + missing.add(" - " + type.getName() + " does not implement " + sdkInterface.getName() + "#" + + method.getName() + describeParameters(method)); + } + } + } + } + } + + Assert.assertTrue("java-client-testing " + testingVersion + " does not implement methods that java-client " + + sdkVersion + " requires:\n" + String.join("\n", missing) + "\n\n" + + "Calling any of them throws AbstractMethodError at runtime. Nothing fails at compile or load time, so this test is the only check.\n" + + "Fix one of two ways:\n" + + " 1. Keep the java-client change compatible with java-client-testing " + testingVersion + ".\n" + + " 2. Release a java-client-testing built against java-client " + sdkVersion + ", then set compatTestingVersion in " + + "e2e/build.gradle to it. " + testingVersion + " then stops being supported with java-client " + sdkVersion + ".\n" + + "Background: e2e/README.md, section \"Released testing compatibility\".", + missing.isEmpty()); + } + + /** + * Every java-client interface the class implements, directly or through a superinterface or superclass. + * Interfaces from the testing jar itself are skipped: they are released with it, not with the SDK. + */ + private static Set> sdkInterfaces(Class type) { + Set> found = new LinkedHashSet<>(); + for (Class current = type; current != null; current = current.getSuperclass()) { + collectInterfaces(current, found); + } + found.removeIf(iface -> iface.getName().startsWith(TESTING_CLASS_PACKAGE)); + return found; + } + + private static void collectInterfaces(Class type, Set> found) { + for (Class iface : type.getInterfaces()) { + if (found.add(iface)) { + collectInterfaces(iface, found); + } + } + } + + /** + * Class.getMethod returns the abstract interface method when nothing in the class implements it, so an abstract + * result means the method is missing. + */ + private static boolean isImplemented(Class type, Method interfaceMethod) { + try { + Method resolved = type.getMethod(interfaceMethod.getName(), interfaceMethod.getParameterTypes()); + return !Modifier.isAbstract(resolved.getModifiers()); + } catch (NoSuchMethodException e) { + return false; + } + } + + private static String describeParameters(Method method) { + StringBuilder description = new StringBuilder("("); + Class[] parameters = method.getParameterTypes(); + for (int i = 0; i < parameters.length; i++) { + if (i > 0) { + description.append(", "); + } + description.append(parameters[i].getSimpleName()); + } + return description.append(")").toString(); + } +} diff --git a/e2e/src/test/resources/splits.yaml b/e2e/src/test/resources/splits.yaml new file mode 100644 index 00000000..cf943790 --- /dev/null +++ b/e2e/src/test/resources/splits.yaml @@ -0,0 +1,2 @@ +- e2e_feature: + treatment: 'on' diff --git a/jvm-commons b/jvm-commons new file mode 160000 index 00000000..322dcf5e --- /dev/null +++ b/jvm-commons @@ -0,0 +1 @@ +Subproject commit 322dcf5e3b04ec16df9882633c6ef721f4b033a9 diff --git a/okhttp-modules/pom.xml b/okhttp-modules/pom.xml new file mode 100644 index 00000000..35c794ea --- /dev/null +++ b/okhttp-modules/pom.xml @@ -0,0 +1,92 @@ + + + + java-client-parent + io.split.client + 4.19.1 + + 4.0.0 + 4.19.1 + okhttp-modules + jar + http-modules + Alternative Http Modules + + + + release + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + false + central + false + published + + + + + + + + + com.squareup.okhttp3 + okhttp + 4.12.0 + + + com.squareup.okhttp3 + logging-interceptor + 4.12.0 + + + io.split.client + java-client + 4.19.1 + compile + + + org.apache.httpcomponents.client5 + httpclient5 + 5.6.4 + + + + junit + junit + test + + + org.mockito + mockito-core + 1.10.19 + test + + + org.powermock + powermock-module-junit4 + 1.7.4 + test + + + org.powermock + powermock-api-mockito + 1.7.4 + test + + + com.squareup.okhttp3 + mockwebserver + 4.8.0 + test + + + + diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/HTTPKerberosAuthInterceptor.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/HTTPKerberosAuthInterceptor.java new file mode 100644 index 00000000..26bd23ea --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/HTTPKerberosAuthInterceptor.java @@ -0,0 +1,273 @@ +package io.split.httpmodules.okhttp; + +import java.io.IOException; +import java.util.Map; +import java.util.Date; +import java.util.Set; +import java.util.Base64; + +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.security.Principal; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; +import javax.security.auth.Subject; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.kerberos.KerberosTicket; + +import org.ietf.jgss.GSSContext; +import org.ietf.jgss.GSSCredential; +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSManager; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; + +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.Authenticator; +import okhttp3.Route; + +/** + * + * An HTTP Request interceptor that modifies the request headers to enable + * Kerberos authentication. It appends the Kerberos authentication token to the + * 'Authorization' request header for Kerberos authentication + * + * Copyright 2024 MarkLogic Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +public class HTTPKerberosAuthInterceptor implements Authenticator { + String host; + Map krbOptions; + LoginContext loginContext; + public HTTPKerberosAuthInterceptor(String host, Map krbOptions) throws IOException { + this.host = host; + this.krbOptions = krbOptions; + try { + buildSubjectCredentials(); + } catch (LoginException e) { + throw new IOException(e.getMessage(), e); + } + } + + /** + * Class to create Kerberos Configuration object which specifies the Kerberos + * Login Module to be used for authentication. + * + */ + protected static class KerberosLoginConfiguration extends Configuration { + Map krbOptions = null; + + public KerberosLoginConfiguration() {} + + KerberosLoginConfiguration(Map krbOptions) { + + this.krbOptions = krbOptions; + } + @Override + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { + if (krbOptions == null) { + throw new IllegalStateException("Cannot create AppConfigurationEntry without Kerberos Options"); + } + return new AppConfigurationEntry[] { new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule", + AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, krbOptions) }; + } + } + + /** + * This method checks the validity of the TGT in the cache and build the + * Subject inside the LoginContext using Krb5LoginModule and the TGT cached by + * the Kerberos client. It assumes that a valid TGT is already present in the + * kerberos client's cache. + * + * @throws LoginException + */ + protected void buildSubjectCredentials() throws LoginException { + Subject subject = new Subject(); + /** + * We are not getting the TGT from KDC here. The actual TGT is got from the + * KDC using kinit or equivalent but we use the cached TGT in order to build + * the LoginContext and populate the TGT inside the Subject using + * Krb5LoginModule + */ + + LoginContext lc = getLoginContext(subject); + lc.login(); + loginContext = lc; + } + + protected LoginContext getLoginContext(Subject subject) throws LoginException { + return new LoginContext("Krb5LoginContext", subject, null, + (krbOptions != null) ? new KerberosLoginConfiguration(krbOptions) : new KerberosLoginConfiguration()); + } + /** + * This method is responsible for getting the client principal name from the + * subject's principal set + * + * @return String the Kerberos principal name populated in the subject + * @throws IllegalStateException if there is more than 0 or more than 1 + * principal is present + */ + protected String getClientPrincipalName() { + final Set principalSet = getContextSubject().getPrincipals(); + if (principalSet.size() != 1) + throw new IllegalStateException( + "Only one principal is expected. Found 0 or more than one principals :" + principalSet); + return principalSet.iterator().next().getName(); + } + + protected Subject getContextSubject() { + Subject subject = loginContext.getSubject(); + if (subject == null) + throw new IllegalStateException("Kerberos login context without subject"); + return subject; + } + + protected CreateAuthorizationHeaderAction getAuthorizationHeaderAction(String clientPrincipal, + String serverPrincipalName) { + return new CreateAuthorizationHeaderAction(clientPrincipal, + serverPrincipalName); + } + + /** + * This method builds the Authorization header for Kerberos. It + * generates a request token based on the service ticket, client principal name and + * time-stamp + * + * @param serverPrincipalName + * the name registered with the KDC of the service for which we + * need to authenticate + * @return the HTTP Authorization header token + */ + protected String buildAuthorizationHeader(String serverPrincipalName) throws LoginException, PrivilegedActionException { + /* + * Get the principal from the Subject's private credentials and populate the + * client and server principal name for the GSS API + */ + final String clientPrincipal = getClientPrincipalName(); + final CreateAuthorizationHeaderAction action = getAuthorizationHeaderAction(clientPrincipal, + serverPrincipalName); + + /* + * Check if the TGT in the Subject's private credentials are valid. If + * valid, then we use the TGT in the Subject's private credentials. If not, + * we build the Subject's private credentials again from valid TGT in the + * Kerberos client cache. + */ + Set privateCreds = getContextSubject().getPrivateCredentials(); + for (Object privateCred : privateCreds) { + if (privateCred instanceof KerberosTicket) { + String serverPrincipalTicketName = ((KerberosTicket) privateCred).getServer().getName(); + if ((serverPrincipalTicketName.startsWith("krbtgt")) + && ((KerberosTicket) privateCred).getEndTime().compareTo(new Date()) < 0) { + buildSubjectCredentials(); + break; + } + } + } + + /* + * Subject.doAs takes in the Subject context and the action to be run as + * arguments. This method executes the action as the Subject given in the + * argument. We do this in order to provide the Subject's context so that we + * reuse the service ticket which will be populated in the Subject rather + * than getting the service ticket from the KDC for each request. The GSS + * API populates the service ticket in the Subject and reuses it + * + */ + Subject.doAs(loginContext.getSubject(), action); + return action.getNegotiateToken(); + } + + /** + * Creates a privileged action which will be executed as the Subject using + * Subject.doAs() method. We do this in order to create a context of the user + * who has the service ticket and reuse this context for subsequent requests + */ + protected static class CreateAuthorizationHeaderAction implements PrivilegedExceptionAction { + String clientPrincipalName; + String serverPrincipalName; + + private StringBuilder outputToken = new StringBuilder(); + + protected CreateAuthorizationHeaderAction(final String clientPrincipalName, final String serverPrincipalName) { + this.clientPrincipalName = clientPrincipalName; + this.serverPrincipalName = serverPrincipalName; + } + + protected String getNegotiateToken() { + return outputToken.toString(); + } + + /* + * Here GSS API takes care of getting the service ticket from the Subject + * cache or by using the TGT information populated in the subject which is + * done by buildSubjectCredentials method. The service ticket received is + * populated in the subject's private credentials along with the TGT + * information since we will be executing this method as the Subject. For + * subsequent requests, the cached service ticket will be re-used. For this + * to work the System property javax.security.auth.useSubjectCredsOnly must + * be set to true. + */ + @Override + public Object run() throws KerberosAuthException { + try { + Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); + Oid krb5PrincipalNameType = new Oid("1.2.840.113554.1.2.2.1"); + final GSSManager manager = GSSManager.getInstance(); + final GSSName clientName = manager.createName(clientPrincipalName, krb5PrincipalNameType); + final GSSCredential clientCred = manager.createCredential(clientName, 8 * 3600, krb5Mechanism, + GSSCredential.INITIATE_ONLY); + final GSSName serverName = manager.createName(serverPrincipalName, krb5PrincipalNameType); + + final GSSContext context = manager.createContext(serverName, krb5Mechanism, clientCred, + GSSContext.DEFAULT_LIFETIME); + byte[] inToken = new byte[0]; + byte[] outToken = context.initSecContext(inToken, 0, inToken.length); + if (outToken == null) { + throw new IOException("could not initialize the security context"); + } + context.requestMutualAuth(true); + outputToken.append(new String(Base64.getEncoder().encode(outToken))); + context.dispose(); + } catch (GSSException | IOException exception) { + throw new KerberosAuthException(exception.getMessage(), exception); + } + return null; + } + } + + /* + * The server principal name which we pass as an argument to + * buildAuthorizationHeader method would always start with 'HTTP/' because we + * create the principal name for the Marklogic server starting with 'HTTP/' + * followed by the host name as mentioned in the External + * Security Guide. + */ + @Override public Request authenticate(Route route, Response response) throws IOException { + String authValue; + try { + authValue = "Negotiate " + buildAuthorizationHeader("HTTP/" + host); + } catch (Exception e) { + throw new IOException(e.getMessage(), e); + } + + return response.request().newBuilder() + .header("Proxy-authorization", authValue) + .build(); + } +} diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/KerberosAuthException.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/KerberosAuthException.java new file mode 100644 index 00000000..06fa2672 --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/KerberosAuthException.java @@ -0,0 +1,10 @@ +package io.split.httpmodules.okhttp; + +public class KerberosAuthException extends Exception { + public KerberosAuthException(String message) { + super(message); + } + public KerberosAuthException(String message, Throwable exception) { + super(message, exception); + } +} diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpClientImpl.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpClientImpl.java new file mode 100644 index 00000000..65a59921 --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpClientImpl.java @@ -0,0 +1,247 @@ +package io.split.httpmodules.okhttp; + +import io.split.client.RequestDecorator; +import io.split.client.dtos.SplitHttpResponse; +import io.split.client.utils.SDKMetadata; +import io.split.engine.common.FetchOptions; +import io.split.service.SplitHttpClient; + +import okhttp3.Authenticator; +import okhttp3.OkHttpClient; +import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.MediaType; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.Proxy; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OkHttpClientImpl implements SplitHttpClient { + protected OkHttpClient httpClient; + private static final Logger _log = LoggerFactory.getLogger(OkHttpClientImpl.class); + private static final String HEADER_CACHE_CONTROL_NAME = "Cache-Control"; + private static final String HEADER_CACHE_CONTROL_VALUE = "no-cache"; + private static final String HEADER_API_KEY = "Authorization"; + private static final String HEADER_CLIENT_KEY = "SplitSDKClientKey"; + private static final String HEADER_CLIENT_MACHINE_NAME = "SplitSDKMachineName"; + private static final String HEADER_CLIENT_MACHINE_IP = "SplitSDKMachineIP"; + private static final String HEADER_CLIENT_VERSION = "SplitSDKVersion"; + private String _apikey; + protected SDKMetadata _metadata; + private final RequestDecorator _decorator; + + public OkHttpClientImpl(String apiToken, SDKMetadata sdkMetadata, + Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled, + int readTimeout, int connectionTimeout, RequestDecorator decorator) throws IOException { + _apikey = apiToken; + _metadata = sdkMetadata; + _decorator = decorator; + setHttpClient(proxy, proxyAuthKerberosPrincipalName, debugEnabled, + readTimeout, connectionTimeout); + + } + protected void setHttpClient(Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled, + int readTimeout, int connectionTimeout) throws IOException { + httpClient = initializeClient(proxy, proxyAuthKerberosPrincipalName, debugEnabled, + readTimeout, connectionTimeout); + } + protected OkHttpClient initializeClient(Proxy proxy, String proxyAuthKerberosPrincipalName, boolean debugEnabled, + int readTimeout, int connectionTimeout) throws IOException { + HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); + if (debugEnabled) { + logging.setLevel(HttpLoggingInterceptor.Level.HEADERS); + } else { + logging.setLevel(HttpLoggingInterceptor.Level.NONE); + } + + Map kerberosOptions = new HashMap<>(); + kerberosOptions.put("com.sun.security.auth.module.Krb5LoginModule", "required"); + kerberosOptions.put("refreshKrb5Config", "false"); + kerberosOptions.put("doNotPrompt", "false"); + kerberosOptions.put("useTicketCache", "true"); + + Authenticator proxyAuthenticator = getProxyAuthenticator(proxyAuthKerberosPrincipalName, kerberosOptions); + + return new okhttp3.OkHttpClient.Builder() + .proxy(proxy) + .readTimeout(readTimeout, TimeUnit.MILLISECONDS) + .connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS) + .addInterceptor(logging) + .proxyAuthenticator(proxyAuthenticator) + .build(); + } + + public HTTPKerberosAuthInterceptor getProxyAuthenticator(String proxyKerberosPrincipalName, + Map kerberosOptions) throws IOException { + return new HTTPKerberosAuthInterceptor(proxyKerberosPrincipalName, kerberosOptions); + } + + @Override + public SplitHttpResponse get(URI uri, FetchOptions options, Map> additionalHeaders) { + try { + okhttp3.Request.Builder requestBuilder = getRequestBuilder(); + requestBuilder.url(uri.toString()); + Map> headers = mergeHeaders(buildBasicHeaders(), additionalHeaders); + Map> decorateHeaders = OkHttpRequestDecorator.decorate(headers, _decorator); + Map> finalHeaders; + if (decorateHeaders.isEmpty()) { + finalHeaders = headers; + } else { + finalHeaders = decorateHeaders; + } + for (Map.Entry> e : finalHeaders.entrySet()) { + for (String headerValue : e.getValue()) { + requestBuilder.addHeader(e.getKey(), headerValue); + } + } + + if (options.cacheControlHeadersEnabled()) { + requestBuilder.addHeader(HEADER_CACHE_CONTROL_NAME, HEADER_CACHE_CONTROL_VALUE); + } + + Request request = requestBuilder.build(); + _log.debug(String.format("Request Headers: %s", request.headers())); + + Response response = httpClient.newCall(request).execute(); + + int responseCode = response.code(); + + _log.debug(String.format("[GET] %s. Status code: %s", + request.url().toString(), + responseCode)); + + String statusMessage = ""; + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, + response.message())); + statusMessage = response.message(); + } + + String responseBody = response.body().string(); + response.close(); + + return new SplitHttpResponse(responseCode, + statusMessage, + responseBody, + getResponseHeaders(response)); + } catch (Exception e) { + throw new IllegalStateException(String.format("Problem in http get operation: %s", e), e); + } + } + + @Override + public SplitHttpResponse post(URI url, String entity, + Map> additionalHeaders) { + try { + okhttp3.Request.Builder requestBuilder = getRequestBuilder(); + requestBuilder.url(url.toString()); + Map> headers = mergeHeaders(buildBasicHeaders(), additionalHeaders); + Map> decorateHeaders = OkHttpRequestDecorator.decorate(headers, _decorator); + Map> finalHeaders; + if (decorateHeaders.isEmpty()) { + finalHeaders = headers; + } else { + finalHeaders = decorateHeaders; + } + for (Map.Entry> e : finalHeaders.entrySet()) { + for (String headerValue : e.getValue()) { + requestBuilder.addHeader(e.getKey(), headerValue); + } + } + requestBuilder.addHeader("Accept-Encoding", "gzip"); + requestBuilder.addHeader("Content-Type", "application/json"); + RequestBody postBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), entity); + requestBuilder.post(postBody); + + Request request = requestBuilder.build(); + _log.debug(String.format("Request Headers: %s", request.headers())); + + Response response = httpClient.newCall(request).execute(); + + int responseCode = response.code(); + + _log.debug(String.format("[GET] %s. Status code: %s", + request.url().toString(), + responseCode)); + + String statusMessage = ""; + if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { + _log.warn(String.format("Response status was: %s. Reason: %s", responseCode, + response.message())); + statusMessage = response.message(); + } + response.close(); + + return new SplitHttpResponse(responseCode, statusMessage, "", getResponseHeaders(response)); + } catch (Exception e) { + throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e); + } + } + + protected okhttp3.Request.Builder getRequestBuilder() { + return new okhttp3.Request.Builder(); + } + + protected Request getRequest(okhttp3.Request.Builder requestBuilder) { + return requestBuilder.build(); + } + + protected Map> buildBasicHeaders() { + Map> h = new HashMap<>(); + h.put(HEADER_API_KEY, Collections.singletonList("Bearer " + _apikey)); + h.put(HEADER_CLIENT_VERSION, Collections.singletonList(_metadata.getSdkVersion())); + h.put(HEADER_CLIENT_MACHINE_IP, Collections.singletonList(_metadata.getMachineIp())); + h.put(HEADER_CLIENT_MACHINE_NAME, Collections.singletonList(_metadata.getMachineName())); + h.put(HEADER_CLIENT_KEY, Collections.singletonList(_apikey.length() > 4 + ? _apikey.substring(_apikey.length() - 4) + : _apikey)); + return h; + } + + protected Map> mergeHeaders(Map> headers, + Map> toAdd) { + if (toAdd == null || toAdd.size() == 0) { + return headers; + } + + for (Map.Entry> entry : toAdd.entrySet()) { + headers.put(entry.getKey(), entry.getValue()); +// headers.computeIfPresent(entry.getKey(), +// (k, oldValue) -> Stream.concat(oldValue.stream(), entry.getValue().stream()) +// .collect(Collectors.toList())); + } + + return headers; + } + + protected SplitHttpResponse.Header[] getResponseHeaders(Response response) { + List responseHeaders = new ArrayList<>(); + Map> map = response.headers().toMultimap(); + for (Map.Entry> entry : map.entrySet()) { + if (entry.getKey() != null) { + responseHeaders.add(new SplitHttpResponse.Header(entry.getKey(), entry.getValue())); + } + } + return responseHeaders.toArray(new SplitHttpResponse.Header[0]); + } + + @Override + public void close() throws IOException { + httpClient.dispatcher().executorService().shutdown(); + } + +} diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpModule.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpModule.java new file mode 100644 index 00000000..9f512874 --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpModule.java @@ -0,0 +1,195 @@ +package io.split.httpmodules.okhttp; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; + +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; +import io.split.service.CustomHttpModule; + +public class OkHttpModule implements CustomHttpModule { + private static final int DEFAULT_CONNECTION_TIMEOUT = 15000; + private static final int DEFAULT_READ_TIMEOUT = 15000; + private final Boolean _debugEnabled; + private final Integer _connectionTimeout; + private final Integer _readTimeout; + private final Proxy _proxy; + private final ProxyAuthScheme _proxyAuthScheme; + private final String _proxyAuthKerberosPrincipalName; + + public static Builder builder() { + return new Builder(); + } + + private OkHttpModule(ProxyAuthScheme proxyAuthScheme, + String proxyAuthKerberosPrincipalName, + Proxy proxy, + Integer connectionTimeout, + Integer readTimeout, + Boolean debugEnabled) { + _proxyAuthScheme = proxyAuthScheme; + _proxyAuthKerberosPrincipalName = proxyAuthKerberosPrincipalName; + _proxy = proxy; + _connectionTimeout = connectionTimeout; + _readTimeout = readTimeout; + _debugEnabled = debugEnabled; + } + + @Override + public OkHttpClientImpl createClient(String apiToken, SDKMetadata sdkMetadata, RequestDecorator decorator) + throws IOException { + return new OkHttpClientImpl(apiToken, sdkMetadata, + _proxy, _proxyAuthKerberosPrincipalName, _debugEnabled, + _readTimeout, _connectionTimeout, decorator); + } + + public Proxy proxy() { + return _proxy; + } + + public ProxyAuthScheme proxyAuthScheme() { + return _proxyAuthScheme; + } + + public String proxyKerberosPrincipalName() { + return _proxyAuthKerberosPrincipalName; + } + + public Integer connectionTimeout() { + return _connectionTimeout; + } + + public Boolean debugEnabled() { + return _debugEnabled; + } + + public Integer readTimeout() { + return _readTimeout; + } + + public static final class Builder { + private Integer _connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; + private Integer _readTimeout = DEFAULT_READ_TIMEOUT; + private String _proxyHost = "localhost"; + private int _proxyPort = -1; + private ProxyAuthScheme _proxyAuthScheme = null; + private String _proxyAuthKerberosPrincipalName = null; + private Boolean _debugEnabled = false; + + public Builder() { + } + + public Builder debugEnabled() { + _debugEnabled = true; + return this; + } + + /** + * The host location of the proxy. Default is localhost. + * + * @param proxyHost location of the proxy + * @return this builder + */ + public Builder proxyHost(String proxyHost) { + _proxyHost = proxyHost; + return this; + } + + /** + * The port of the proxy. Default is -1. + * + * @param proxyPort port for the proxy + * @return this builder + */ + public Builder proxyPort(int proxyPort) { + _proxyPort = proxyPort; + return this; + } + + Proxy proxy() { + if (_proxyPort != -1) { + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(_proxyHost, _proxyPort)); + } + // Default is no proxy. + return null; + } + + /** + * Authentication Scheme + * + * @param proxyAuthScheme + * @return this builder + */ + public Builder proxyAuthScheme(ProxyAuthScheme proxyAuthScheme) { + _proxyAuthScheme = proxyAuthScheme; + return this; + } + + /** + * Kerberos Principal Account Name + * + * @param proxyAuthKerberosPrincipalName + * @return this builder + */ + public Builder proxyAuthKerberosPrincipalName(String proxyAuthKerberosPrincipalName) { + _proxyAuthKerberosPrincipalName = proxyAuthKerberosPrincipalName; + return this; + } + + /** + * HTTP Connection Timeout + * + * @param connectionTimeout + * @return this builder + */ + public Builder connectionTimeout(int connectionTimeout) { + _connectionTimeout = connectionTimeout; + return this; + } + + /** + * HTTP Read Timeout + * + * @param readTimeout + * @return this builder + */ + public Builder readTimeout(int readTimeout) { + _readTimeout = readTimeout; + return this; + } + + private void verifyAuthScheme() { + if (_proxyAuthScheme == ProxyAuthScheme.KERBEROS) { + if (proxy() == null) { + throw new IllegalStateException("Kerberos mode require Proxy parameters."); + } + if (_proxyAuthKerberosPrincipalName == null) { + throw new IllegalStateException("Kerberos mode require Kerberos Principal Name."); + } + } + } + + private void verifyTimeouts() { + if (_connectionTimeout <= 0) { + _connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; + } + if (_readTimeout <= 0) { + _readTimeout = DEFAULT_READ_TIMEOUT; + } + } + + public OkHttpModule build() { + verifyTimeouts(); + verifyAuthScheme(); + + return new OkHttpModule( + _proxyAuthScheme, + _proxyAuthKerberosPrincipalName, + proxy(), + _connectionTimeout, + _readTimeout, + _debugEnabled); + } + } +} diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpRequestDecorator.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpRequestDecorator.java new file mode 100644 index 00000000..efe9b807 --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/OkHttpRequestDecorator.java @@ -0,0 +1,15 @@ +package io.split.httpmodules.okhttp; + +import java.util.List; +import java.util.Map; + +import io.split.client.RequestDecorator; +import io.split.client.dtos.RequestContext; + +class OkHttpRequestDecorator { + + public static Map> decorate(Map> headers, + RequestDecorator decorator) { + return decorator.decorateHeaders(new RequestContext(headers)).headers(); + } +} diff --git a/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/ProxyAuthScheme.java b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/ProxyAuthScheme.java new file mode 100644 index 00000000..4340829a --- /dev/null +++ b/okhttp-modules/src/main/java/io/split/httpmodules/okhttp/ProxyAuthScheme.java @@ -0,0 +1,5 @@ +package io.split.httpmodules.okhttp; + +public enum ProxyAuthScheme { + KERBEROS +} diff --git a/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/HTTPKerberosAuthIntercepterTest.java b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/HTTPKerberosAuthIntercepterTest.java new file mode 100644 index 00000000..94fcb85a --- /dev/null +++ b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/HTTPKerberosAuthIntercepterTest.java @@ -0,0 +1,111 @@ +package io.split.httpmodules.okhttp; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosPrincipal; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.internal.verification.VerificationModeFactory.times; +import static org.powermock.api.mockito.PowerMockito.*; + +import java.security.PrivilegedActionException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(HTTPKerberosAuthInterceptor.class) +public class HTTPKerberosAuthIntercepterTest { + @Test + public void testBasicFlow() throws Exception { + System.setProperty("java.security.krb5.conf", "src/test/resources/krb5.conf"); + + HTTPKerberosAuthInterceptor kerberosAuthInterceptor = mock(HTTPKerberosAuthInterceptor.class); + LoginContext loginContext = PowerMockito.mock(LoginContext.class); + when(kerberosAuthInterceptor.getLoginContext(any())).thenReturn((loginContext)); + + doCallRealMethod().when(kerberosAuthInterceptor).buildSubjectCredentials(); + kerberosAuthInterceptor.buildSubjectCredentials(); + verify(loginContext, times(1)).login(); + + Subject subject = new Subject(); + when(loginContext.getSubject()).thenReturn(subject); + doCallRealMethod().when(kerberosAuthInterceptor).getContextSubject(); + kerberosAuthInterceptor.getContextSubject(); + verify(loginContext, times(1)).getSubject(); + + subject.getPrincipals().add(new KerberosPrincipal("bilal")); + subject.getPublicCredentials().add(new KerberosPrincipal("name")); + subject.getPrivateCredentials().add(new KerberosPrincipal("name")); + + doCallRealMethod().when(kerberosAuthInterceptor).getClientPrincipalName(); + assertThat(kerberosAuthInterceptor.getClientPrincipalName(), is(equalTo("bilal@ATHENA.MIT.EDU"))) ; + verify(loginContext, times(2)).getSubject(); + + when(kerberosAuthInterceptor.buildAuthorizationHeader(any())).thenReturn("secured-token"); + okhttp3.Request originalRequest = new okhttp3.Request.Builder().url("http://somthing").build(); + okhttp3.Response response = new okhttp3.Response.Builder().code(200).request(originalRequest). + protocol(okhttp3.Protocol.HTTP_1_1).message("ok").build(); + doCallRealMethod().when(kerberosAuthInterceptor).authenticate(null, response); + okhttp3.Request request = kerberosAuthInterceptor.authenticate(null, response); + assertThat(request.headers("Proxy-authorization"), is(equalTo(Arrays.asList("Negotiate secured-token")))); + } +/* + @Test + public void testKerberosLoginConfiguration() { + Map kerberosOptions = new HashMap(); + kerberosOptions.put("com.sun.security.auth.module.Krb5LoginModule", "required"); + kerberosOptions.put("refreshKrb5Config", "false"); + kerberosOptions.put("doNotPrompt", "false"); + kerberosOptions.put("useTicketCache", "true"); + + HTTPKerberosAuthInterceptor.KerberosLoginConfiguration kerberosConfig = new HTTPKerberosAuthInterceptor.KerberosLoginConfiguration(kerberosOptions); + AppConfigurationEntry[] appConfig = kerberosConfig.getAppConfigurationEntry(""); + assertThat("com.sun.security.auth.module.Krb5LoginModule", is(equalTo(appConfig[0].getLoginModuleName()))); + assertThat(AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, is(equalTo(appConfig[0].getControlFlag()))); + } + + @Test(expected = IllegalStateException.class) + public void testKerberosLoginConfigurationException() { + HTTPKerberosAuthInterceptor.KerberosLoginConfiguration kerberosConfig = new HTTPKerberosAuthInterceptor.KerberosLoginConfiguration(); + AppConfigurationEntry[] appConfig = kerberosConfig.getAppConfigurationEntry(""); + } +*/ + @Test + public void testBuildAuthorizationHeader() throws LoginException, PrivilegedActionException { + System.setProperty("java.security.krb5.conf", "src/test/resources/krb5.conf"); + + HTTPKerberosAuthInterceptor kerberosAuthInterceptor = mock(HTTPKerberosAuthInterceptor.class); + HTTPKerberosAuthInterceptor.CreateAuthorizationHeaderAction ahh = mock(HTTPKerberosAuthInterceptor.CreateAuthorizationHeaderAction.class); + when(ahh.getNegotiateToken()).thenReturn("secret-token"); + when(kerberosAuthInterceptor.getAuthorizationHeaderAction(any(), any())).thenReturn(ahh); + + LoginContext loginContext = PowerMockito.mock(LoginContext.class); + doCallRealMethod().when(kerberosAuthInterceptor).buildAuthorizationHeader("bilal"); + Subject subject = new Subject(); + when(loginContext.getSubject()).thenReturn(subject); + when(kerberosAuthInterceptor.getContextSubject()).thenReturn(subject); + when(kerberosAuthInterceptor.getLoginContext(subject)).thenReturn((loginContext)); + doCallRealMethod().when(kerberosAuthInterceptor).buildSubjectCredentials(); + kerberosAuthInterceptor.buildSubjectCredentials(); + + subject.getPrincipals().add(new KerberosPrincipal("bilal")); + subject.getPublicCredentials().add(new KerberosPrincipal("name")); + subject.getPrivateCredentials().add(new KerberosPrincipal("name")); + doCallRealMethod().when(kerberosAuthInterceptor).getClientPrincipalName(); + + assertThat("secret-token", is(equalTo(kerberosAuthInterceptor.buildAuthorizationHeader("bilal")))); + } +} diff --git a/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpClientImplTest.java b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpClientImplTest.java new file mode 100644 index 00000000..edfddf30 --- /dev/null +++ b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpClientImplTest.java @@ -0,0 +1,429 @@ +package io.split.httpmodules.okhttp; + +import org.powermock.api.mockito.PowerMockito; +import org.powermock.reflect.Whitebox; + +import io.split.client.CustomHeaderDecorator; +import io.split.client.RequestDecorator; +import io.split.client.dtos.*; +import io.split.client.impressions.Impression; +import io.split.client.utils.Json; +import io.split.client.utils.SDKMetadata; +import io.split.client.dtos.SplitHttpResponse.Header; +import io.split.engine.common.FetchOptions; + +import okhttp3.OkHttpClient; +import okhttp3.OkHttpClient.*; +import okhttp3.HttpUrl; +import okhttp3.Headers; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.Assert; +import org.junit.Test; + +import java.io.*; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.HttpURLConnection; +import java.util.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.Matchers.any; +import static org.powermock.api.mockito.PowerMockito.mock; + +public class OkHttpClientImplTest { + + @Test + public void testGetWithSpecialCharacters() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + BufferedReader br = new BufferedReader(new FileReader("src/test/resources/split-change-special-characters.json")); + String body; + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + sb.append(System.lineSeparator()); + line = br.readLine(); + } + body = sb.toString(); + } finally { + br.close(); + } + + server.enqueue(new MockResponse().setBody(body).addHeader("via", "HTTP/1.1 s_proxy_rio1")); + server.start(); + HttpUrl baseUrl = server.url("/v1/"); + URI rootTarget = baseUrl.uri(); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + + Map> additionalHeaders = Collections.singletonMap("AdditionalHeader", + Collections.singletonList("add")); + FetchOptions fetchOptions = new FetchOptions.Builder().cacheControlHeaders(true).build(); + RequestDecorator requestDecorator = new RequestDecorator(null); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).get(rootTarget, fetchOptions, additionalHeaders); + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + PowerMockito.doReturn(requestBuilder.build()).when(okHttpClientImpl).getRequest(requestBuilder); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getRequest(requestBuilder); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.get(rootTarget, fetchOptions, additionalHeaders); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + Headers requestHeaders = request.getHeaders(); + + assertThat(splitHttpResponse.statusCode(), is(equalTo(HttpURLConnection.HTTP_OK))); + Assert.assertEquals("/v1/", request.getPath()); + assertThat(requestHeaders.get("Authorization"), is(equalTo("Bearer qwerty"))) ; + assertThat(requestHeaders.get("SplitSDKClientKey"), is(equalTo("erty"))); + assertThat(requestHeaders.get("SplitSDKVersion"), is(equalTo("java-1.2.3"))); + assertThat(requestHeaders.get("SplitSDKMachineIP"), is(equalTo("1.2.3.4"))); + assertThat(requestHeaders.get("SplitSDKMachineName"), is(equalTo("someIP"))); + assertThat(requestHeaders.get("AdditionalHeader"), is(equalTo("add"))); + + SplitChange change = Json.fromJson(splitHttpResponse.body(), SplitChange.class); + + Header[] headers = splitHttpResponse.responseHeaders(); + assertThat(headers[1].getName(), is(equalTo("via"))); + assertThat(headers[1].getValues().get(0), is(equalTo("HTTP/1.1 s_proxy_rio1"))); + assertThat(splitHttpResponse.statusCode(), is(equalTo(200))); + Assert.assertNotNull(change); + Assert.assertEquals(1, change.featureFlags.d.size()); + Assert.assertNotNull(change.featureFlags.d.get(0)); + + Split split = change.featureFlags.d.get(0); + Map configs = split.configurations; + Assert.assertEquals(2, configs.size()); + Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on")); + Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off")); + Assert.assertEquals(2, split.sets.size()); + okHttpClientImpl.close(); + } + + @Test + public void testGetErrors() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR)); + server.start(); + HttpUrl baseUrl = server.url("/v1/"); + URI rootTarget = baseUrl.uri(); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + + Map> additionalHeaders = Collections.singletonMap("AdditionalHeader", + Collections.singletonList("add")); + FetchOptions fetchOptions = new FetchOptions.Builder().cacheControlHeaders(true).build(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).get(rootTarget, fetchOptions, additionalHeaders); + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders); + RequestDecorator requestDecorator = new RequestDecorator(null); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.get(rootTarget, + fetchOptions, additionalHeaders); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + assertThat(splitHttpResponse.statusCode(), is(equalTo(HttpURLConnection.HTTP_INTERNAL_ERROR))); + okHttpClientImpl.close(); + } + + @Test + public void testGetParameters() throws IOException, InterruptedException { + class MyCustomHeaders implements CustomHeaderDecorator { + public MyCustomHeaders() {} + @Override + public Map> getHeaderOverrides(RequestContext context) { + Map> additionalHeaders = context.headers(); + additionalHeaders.put("first", Arrays.asList("1")); + additionalHeaders.put("second", Arrays.asList("2.1", "2.2")); + additionalHeaders.put("third", Arrays.asList("3")); + return additionalHeaders; + } + } + MockWebServer server = new MockWebServer(); + BufferedReader br = new BufferedReader(new FileReader("src/test/resources/split-change-special-characters.json")); + String body; + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + sb.append(System.lineSeparator()); + line = br.readLine(); + } + body = sb.toString(); + } finally { + br.close(); + } + + server.enqueue(new MockResponse().setBody(body).addHeader("via", "HTTP/1.1 s_proxy_rio1")); + server.start(); + HttpUrl baseUrl = server.url("/splitChanges?since=1234567"); + URI rootTarget = baseUrl.uri(); + RequestDecorator requestDecorator = new RequestDecorator(new MyCustomHeaders()); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + + FetchOptions fetchOptions = new FetchOptions.Builder().cacheControlHeaders(true).build(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).get(rootTarget, fetchOptions, null); + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), null); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + FetchOptions options = new FetchOptions.Builder().cacheControlHeaders(true).build(); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.get(rootTarget, options, null); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + Headers requestHeaders = request.getHeaders(); + + assertThat(requestHeaders.get("Cache-Control"), is(equalTo("no-cache"))); + assertThat(requestHeaders.get("first"), is(equalTo("1"))); + assertThat(requestHeaders.values("second"), is(equalTo(Arrays.asList("2.1","2.2")))); + assertThat(requestHeaders.get("third"), is(equalTo("3"))); + Assert.assertEquals("/splitChanges?since=1234567", request.getPath()); + assertThat(request.getMethod(), is(equalTo("GET"))); + } + + @Test(expected = IllegalStateException.class) + public void testException() throws URISyntaxException, IOException { + URI rootTarget = new URI("https://api.split.io/splitChanges?since=1234567"); + RequestDecorator requestDecorator = null; + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + + FetchOptions fetchOptions = new FetchOptions.Builder().cacheControlHeaders(true).build(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).get(rootTarget, fetchOptions, null); + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), null); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + FetchOptions options = new FetchOptions.Builder().cacheControlHeaders(true).build(); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.get(rootTarget, + new FetchOptions.Builder().cacheControlHeaders(true).build(), null); + } + + + + @Test + public void testPost() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + + server.enqueue(new MockResponse().addHeader("via", "HTTP/1.1 s_proxy_rio1")); + server.start(); + HttpUrl baseUrl = server.url("/impressions"); + URI rootTarget = baseUrl.uri(); + RequestDecorator requestDecorator = new RequestDecorator(null); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + Map> additionalHeaders = Collections.singletonMap("SplitSDKImpressionsMode", + Collections.singletonList("OPTIMIZED")); + + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + // Send impressions + List toSend = Arrays.asList(new TestImpressions("t1", Arrays.asList( + KeyImpression.fromImpression(new Impression("k1", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t1", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t1", "on", 123L, "r1", 456L, null, null)))), + new TestImpressions("t2", Arrays.asList( + KeyImpression.fromImpression(new Impression("k1", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k2", null, "t2", "on", 123L, "r1", 456L, null, null)), + KeyImpression.fromImpression(new Impression("k3", null, "t2", "on", 123L, "r1", 456L, null, null))))); + String data = Json.toJson(toSend); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).post(rootTarget, data, + additionalHeaders); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.post(rootTarget, data, + additionalHeaders); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + Headers requestHeaders = request.getHeaders(); + + Assert.assertEquals("POST /impressions HTTP/1.1", request.getRequestLine()); + Assert.assertEquals(data, request.getBody().readUtf8()); + assertThat(requestHeaders.get("Authorization"), is(equalTo("Bearer qwerty"))) ; + assertThat(requestHeaders.get("SplitSDKClientKey"), is(equalTo("erty"))); + assertThat(requestHeaders.get("SplitSDKVersion"), is(equalTo("java-1.2.3"))); + assertThat(requestHeaders.get("SplitSDKMachineIP"), is(equalTo("1.2.3.4"))); + assertThat(requestHeaders.get("SplitSDKMachineName"), is(equalTo("someIP"))); + assertThat(requestHeaders.get("SplitSDKImpressionsMode"), is(equalTo("OPTIMIZED"))); + + Header[] headers = splitHttpResponse.responseHeaders(); + assertThat(headers[1].getName(), is(equalTo("via"))); + assertThat(headers[1].getValues().get(0), is(equalTo("HTTP/1.1 s_proxy_rio1"))); + assertThat(splitHttpResponse.statusCode(), is(equalTo(200))); + } + + @Test + public void testPostErrors() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR)); + server.start(); + HttpUrl baseUrl = server.url("/v1/"); + URI rootTarget = baseUrl.uri(); + RequestDecorator requestDecorator = new RequestDecorator(null); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + Map> additionalHeaders = Collections.singletonMap("SplitSDKImpressionsMode", + Collections.singletonList("OPTIMIZED")); + + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + + String data = Json.toJson("<>"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).post(rootTarget, data, + additionalHeaders); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.post(rootTarget, data, + additionalHeaders); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + assertThat(splitHttpResponse.statusCode(), is(equalTo(HttpURLConnection.HTTP_INTERNAL_ERROR))); + okHttpClientImpl.close(); + } + + @Test(expected = IllegalStateException.class) + public void testPosttException() throws URISyntaxException, IOException { + URI rootTarget = new URI("https://kubernetesturl.com/split/api/testImpressions/bulk"); + + OkHttpClientImpl okHttpClientImpl = mock(OkHttpClientImpl.class); + OkHttpClient client = new OkHttpClient.Builder().build(); + PowerMockito.doReturn(client).when(okHttpClientImpl).initializeClient(null, "bilal", false, + 0, 0); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).setHttpClient(null, "bilal", false, + 0, 0); + okHttpClientImpl.setHttpClient(null, "bilal", false, + 0, 0); + Map> additionalHeaders = Collections.singletonMap("SplitSDKImpressionsMode", + Collections.singletonList("OPTIMIZED")); + + okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder(); + RequestDecorator requestDecorator = new RequestDecorator(null); + requestBuilder.url(rootTarget.toString()); + PowerMockito.doReturn(requestBuilder).when(okHttpClientImpl).getRequestBuilder(); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).buildBasicHeaders(); + Whitebox.setInternalState(okHttpClientImpl, "_metadata", metadata()); + Whitebox.setInternalState(okHttpClientImpl, "_apikey", "qwerty"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).mergeHeaders(buildBasicHeaders(), additionalHeaders); + Whitebox.setInternalState(okHttpClientImpl, "_decorator", requestDecorator); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).getResponseHeaders(any()); + + String data = Json.toJson("<>"); + PowerMockito.doCallRealMethod().when(okHttpClientImpl).post(rootTarget, data, + additionalHeaders); + + SplitHttpResponse splitHttpResponse = okHttpClientImpl.post(rootTarget, data, + additionalHeaders); + } + + private SDKMetadata metadata() { + return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + } + + private Map> buildBasicHeaders() { + Map> h = new HashMap<>(); + h.put("Authorization", Collections.singletonList("Bearer qwerty")); + h.put("SplitSDKVersion", Collections.singletonList(metadata().getSdkVersion())); + h.put("SplitSDKMachineIP", Collections.singletonList(metadata().getMachineIp())); + h.put("SplitSDKMachineName", Collections.singletonList(metadata().getMachineName())); + h.put("SplitSDKClientKey", Collections.singletonList("qwerty".length() > 4 + ? "qwerty".substring("qwerty".length() - 4) + : "qwerty")); + return h; + } + +} diff --git a/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpModuleTests.java b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpModuleTests.java new file mode 100644 index 00000000..d8c5b524 --- /dev/null +++ b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/OkHttpModuleTests.java @@ -0,0 +1,111 @@ +package io.split.httpmodules.okhttp; + +import io.split.client.RequestDecorator; +import io.split.client.utils.SDKMetadata; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.mockito.stubbing.Answer; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.whenNew; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(OkHttpModule.class) +public class OkHttpModuleTests { + @Test + public void checkProxySettings() { + OkHttpModule module = OkHttpModule.builder() + .proxyAuthScheme(ProxyAuthScheme.KERBEROS) + .proxyAuthKerberosPrincipalName("bilal@bilal") + .proxyHost("some-proxy") + .proxyPort(3128) + .build(); + Assert.assertEquals(ProxyAuthScheme.KERBEROS, module.proxyAuthScheme()); + Assert.assertEquals("bilal@bilal", module.proxyKerberosPrincipalName()); + Assert.assertEquals("HTTP @ some-proxy:3128", module.proxy().toString()); + } + + @Test + public void checkDebugLog() { + OkHttpModule module = OkHttpModule.builder() + .debugEnabled() + .build(); + Assert.assertEquals(true, module.debugEnabled()); + + module = OkHttpModule.builder() + .build(); + Assert.assertEquals(false, module.debugEnabled()); + } + + @Test + public void checkTimeouts() { + OkHttpModule module = OkHttpModule.builder() + .build(); + Assert.assertEquals(15000, (int) module.connectionTimeout()); + Assert.assertEquals(15000, (int) module.readTimeout()); + + module = OkHttpModule.builder() + .connectionTimeout(13000) + .readTimeout(14000) + .build(); + Assert.assertEquals(13000, (int) module.connectionTimeout()); + Assert.assertEquals(14000, (int) module.readTimeout()); + + module = OkHttpModule.builder() + .connectionTimeout(-1) + .readTimeout(-10) + .build(); + Assert.assertEquals(15000, (int) module.connectionTimeout()); + Assert.assertEquals(15000, (int) module.readTimeout()); + } + + @Test + public void testCreateClient() throws Exception { + OkHttpClientImpl mockclient = mock(OkHttpClientImpl.class); + AtomicBoolean argsCaptured = new AtomicBoolean(false); + + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("some-proxy", 3128)); + String apiToken = "qwerty"; + SDKMetadata sdkMetadata = new SDKMetadata("1.1.1", "ip", "name"); + RequestDecorator requestDecorator = new RequestDecorator(null); + + whenNew(OkHttpClientImpl.class).withAnyArguments() + .then((Answer) invocationOnMock -> { + assertThat("qwerty", is(equalTo((String) invocationOnMock.getArguments()[0]))); + assertThat(sdkMetadata, is(equalTo((SDKMetadata) invocationOnMock.getArguments()[1]))); + assertThat(proxy, is(equalTo((Proxy) invocationOnMock.getArguments()[2]))); + assertThat("bilal@bilal", is(equalTo((String) invocationOnMock.getArguments()[3]))); + assertThat(false, is(equalTo((Boolean) invocationOnMock.getArguments()[4]))); + assertThat(11000, is(equalTo((Integer) invocationOnMock.getArguments()[5]))); + assertThat(12000, is(equalTo((Integer) invocationOnMock.getArguments()[6]))); + assertThat(requestDecorator, is(equalTo((RequestDecorator) invocationOnMock.getArguments()[7]))); + argsCaptured.set(true); + return mockclient; + } + ); + + OkHttpModule module = OkHttpModule.builder() + .proxyAuthScheme(ProxyAuthScheme.KERBEROS) + .proxyAuthKerberosPrincipalName("bilal@bilal") + .proxyHost("some-proxy") + .proxyPort(3128) + .connectionTimeout(12000) + .readTimeout(11000) + .build(); + + module.createClient(apiToken, sdkMetadata, requestDecorator); + assertThat(true, is(equalTo(argsCaptured.get()))); + } +} diff --git a/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitConfigTests.java b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitConfigTests.java new file mode 100644 index 00000000..20feddb3 --- /dev/null +++ b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitConfigTests.java @@ -0,0 +1,45 @@ +package io.split.httpmodules.okhttp; + +import io.split.client.SplitClientConfig; +import org.junit.Assert; +import org.junit.Test; + +public class SplitConfigTests { + + @Test + public void checkExpectedAuthScheme() { + SplitClientConfig cfg = SplitClientConfig.builder() + .alternativeHTTPModule(OkHttpModule.builder() + .proxyAuthScheme(ProxyAuthScheme.KERBEROS) + .proxyAuthKerberosPrincipalName("bilal@bilal") + .proxyHost("some-proxy") + .proxyPort(3128) + .debugEnabled() + .build() + ) + .streamingEnabled(false) + .build(); + OkHttpModule module = (OkHttpModule) cfg.alternativeHTTPModule(); + Assert.assertEquals(ProxyAuthScheme.KERBEROS, module.proxyAuthScheme()); + Assert.assertEquals("bilal@bilal", module.proxyKerberosPrincipalName()); + Assert.assertEquals("HTTP @ some-proxy:3128", module.proxy().toString()); + + cfg = SplitClientConfig.builder() + .build(); + Assert.assertEquals(null, cfg.alternativeHTTPModule()); + } + + @Test(expected = IllegalArgumentException.class) + public void checkStreamingEnabled() { + SplitClientConfig cfg = SplitClientConfig.builder() + .alternativeHTTPModule(OkHttpModule.builder() + .proxyAuthScheme(ProxyAuthScheme.KERBEROS) + .proxyAuthKerberosPrincipalName("bilal@bilal") + .proxyHost("some-proxy") + .proxyPort(3128) + .debugEnabled() + .build()) + .streamingEnabled(true) + .build(); + } +} diff --git a/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitFactoryTests.java b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitFactoryTests.java new file mode 100644 index 00000000..c233942b --- /dev/null +++ b/okhttp-modules/src/test/java/io/split/httpmodules/okhttp/SplitFactoryTests.java @@ -0,0 +1,71 @@ +package io.split.httpmodules.okhttp; + +import io.split.client.*; +import io.split.client.utils.SDKMetadata; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.stubbing.Answer; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.whenNew; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(OkHttpModule.class) +// jdk.net loads a JDK native library, which cannot be loaded again from PowerMock's classloader. +@PowerMockIgnore("jdk.net.*") +public class SplitFactoryTests { + @Test + public void testFactoryCreatingClient() throws Exception { + OkHttpClientImpl mockclient = mock(OkHttpClientImpl.class); + AtomicBoolean argsCaptured = new AtomicBoolean(false); + + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("some-proxy", 3128)); + String apiToken = "qwerty"; + + whenNew(OkHttpClientImpl.class).withAnyArguments() + .then((Answer) invocationOnMock -> { + assertThat("qwerty", is(equalTo((String) invocationOnMock.getArguments()[0]))); + assertThat((SDKMetadata) invocationOnMock.getArguments()[1], instanceOf(SDKMetadata.class)); + assertThat(proxy, is(equalTo((Proxy) invocationOnMock.getArguments()[2]))); + assertThat("bilal@bilal", is(equalTo((String) invocationOnMock.getArguments()[3]))); + assertThat(false, is(equalTo((Boolean) invocationOnMock.getArguments()[4]))); + assertThat(11000, is(equalTo((Integer) invocationOnMock.getArguments()[5]))); + assertThat(12000, is(equalTo((Integer) invocationOnMock.getArguments()[6]))); + assertThat((RequestDecorator) invocationOnMock.getArguments()[7], instanceOf(RequestDecorator.class)); + argsCaptured.set(true); + return mockclient; + } + ); + + OkHttpModule module = OkHttpModule.builder() + .proxyAuthScheme(ProxyAuthScheme.KERBEROS) + .proxyAuthKerberosPrincipalName("bilal@bilal") + .proxyHost("some-proxy") + .proxyPort(3128) + .connectionTimeout(12000) + .readTimeout(11000) + .build(); + + SplitClientConfig cfg = SplitClientConfig.builder() + .alternativeHTTPModule(module) + .streamingEnabled(false) + .build(); + + SplitFactoryImpl factory = (SplitFactoryImpl) SplitFactoryBuilder.build(apiToken, cfg); + +// module.createClient(apiToken, sdkMetadata, requestDecorator); + assertThat(true, is(equalTo(argsCaptured.get()))); + } +} diff --git a/okhttp-modules/src/test/resources/krb5.conf b/okhttp-modules/src/test/resources/krb5.conf new file mode 100644 index 00000000..78d63ba8 --- /dev/null +++ b/okhttp-modules/src/test/resources/krb5.conf @@ -0,0 +1,37 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +[libdefaults] + kdc_realm = ATHENA.MIT.EDU + default_realm = ATHENA.MIT.EDU + kdc_tcp_port = 88 + kdc_udp_port = 88 + dns_lookup_realm = false + dns_lookup_kdc = false + udp_preference_limit = 1 + +[logging] + default = FILE:/var/logs/krb5kdc.log + +[realms] + ATHENA.MIT.EDU = { +# kdc = 10.12.4.76:88 +# kdc = tcp/10.12.4.76:88 +# kdc = tcp/192.168.1.19:88 + kdc = 192.168.1.19:88 + } \ No newline at end of file diff --git a/okhttp-modules/src/test/resources/org/powermock/extensions/configuration.properties b/okhttp-modules/src/test/resources/org/powermock/extensions/configuration.properties new file mode 100644 index 00000000..a8ebaeba --- /dev/null +++ b/okhttp-modules/src/test/resources/org/powermock/extensions/configuration.properties @@ -0,0 +1 @@ +powermock.global-ignore=jdk.internal.reflect.*,javax.net.ssl.* \ No newline at end of file diff --git a/okhttp-modules/src/test/resources/split-change-special-characters.json b/okhttp-modules/src/test/resources/split-change-special-characters.json new file mode 100644 index 00000000..ae99d7a7 --- /dev/null +++ b/okhttp-modules/src/test/resources/split-change-special-characters.json @@ -0,0 +1,60 @@ +{ "ff": { + "d": [ + { + "trafficTypeName": "user", + "name": "DEMO_MURMUR2", + "trafficAllocation": 100, + "trafficAllocationSeed": 1314112417, + "seed": -2059033614, + "status": "ACTIVE", + "killed": false, + "defaultTreatment": "of", + "changeNumber": 1491244291288, + "sets": [ + "set1", + "set2" + ], + "algo": 2, + "configurations": { + "on": "{\"test\": \"blue\",\"grüne Straße\": 13}", + "off": "{\"test\": \"blue\",\"size\": 15}" + }, + "conditions": [ + { + "conditionType": "ROLLOUT", + "matcherGroup": { + "combiner": "AND", + "matchers": [ + { + "keySelector": { + "trafficType": "user", + "attribute": null + }, + "matcherType": "ALL_KEYS", + "negate": false, + "userDefinedSegmentMatcherData": null, + "whitelistMatcherData": null, + "unaryNumericMatcherData": null, + "betweenMatcherData": null + } + ] + }, + "partitions": [ + { + "treatment": "on", + "size": 0 + }, + { + "treatment": "of", + "size": 100 + } + ], + "label": "in segment all" + } + ] + } + ], + "s": 1491244291288, + "t": 1491244291288}, + "rbs": {"d": [], "s": -1, "t": -1} +} diff --git a/pluggable-storage/pom.xml b/pluggable-storage/pom.xml new file mode 100644 index 00000000..c6cf2261 --- /dev/null +++ b/pluggable-storage/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + java-client-parent + io.split.client + 4.19.1 + + + 2.1.0 + pluggable-storage + jar + Package for Pluggable Storage + Wrapper interface to implement Pluggable Storage + + + + release + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + true + central + false + published + + + + + + + + diff --git a/pluggable-storage/src/main/java/pluggable/CustomStorageWrapper.java b/pluggable-storage/src/main/java/pluggable/CustomStorageWrapper.java new file mode 100644 index 00000000..67f81d7a --- /dev/null +++ b/pluggable-storage/src/main/java/pluggable/CustomStorageWrapper.java @@ -0,0 +1,35 @@ +package pluggable; + +import java.util.List; +import java.util.Set; + +public interface CustomStorageWrapper { + // key-value operations + String get(String key) throws Exception; + List getMany(List keys) throws Exception; + void set(String key, String item) throws Exception; + void hSet(String key, String field, String item) throws Exception; + void delete(List keys) throws Exception; + String getAndSet(String key, String item) throws Exception; + Set getKeysByPrefix(String prefix) throws Exception; + + // integer operations + long increment(String key, long value) throws Exception; + long decrement(String key, long value) throws Exception; + long hIncrement(String key, String field, long value) throws Exception; + + // queue operations + long pushItems(String key, List items) throws Exception; + List popItems(String key, long count) throws Exception; + long getItemsCount(String key) throws Exception; + + // set operations + boolean itemContains(String key, String item) throws Exception; + void addItems(String key, List items) throws Exception; + void removeItems(String key, List items) throws Exception; + List getItems(List keys) throws Exception; + Set getMembers(String key) throws Exception; + boolean connect() throws Exception; + boolean disconnect() throws Exception; + Pipeline pipeline() throws Exception; +} \ No newline at end of file diff --git a/pluggable-storage/src/main/java/pluggable/HasPipelineSupport.java b/pluggable-storage/src/main/java/pluggable/HasPipelineSupport.java new file mode 100644 index 00000000..ad162deb --- /dev/null +++ b/pluggable-storage/src/main/java/pluggable/HasPipelineSupport.java @@ -0,0 +1,5 @@ +package pluggable; + +public interface HasPipelineSupport { + Pipeline pipeline() throws Exception; +} diff --git a/pluggable-storage/src/main/java/pluggable/NotPipelinedImpl.java b/pluggable-storage/src/main/java/pluggable/NotPipelinedImpl.java new file mode 100644 index 00000000..8c7bd0a6 --- /dev/null +++ b/pluggable-storage/src/main/java/pluggable/NotPipelinedImpl.java @@ -0,0 +1,35 @@ +package pluggable; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; + +public class NotPipelinedImpl implements Pipeline { + + private final List> _methods; + private final CustomStorageWrapper _storage; + + public NotPipelinedImpl(CustomStorageWrapper storage) { + _methods = new ArrayList<>(); + _storage = storage; + } + + @Override + public List exec() throws Exception { + List result = new ArrayList<>(); + for (Callable method : _methods) { + result.add(new Result(method.call())); + } + return result; + } + + @Override + public void hIncrement(String key, String field, long value) { + _methods.add(() -> { return _storage.hIncrement(key, field, value);}); + } + + @Override + public void getMembers(String key) throws Exception { + _methods.add(() -> { return _storage.getMembers(key);}); + } +} \ No newline at end of file diff --git a/pluggable-storage/src/main/java/pluggable/Pipeline.java b/pluggable-storage/src/main/java/pluggable/Pipeline.java new file mode 100644 index 00000000..09b40bbf --- /dev/null +++ b/pluggable-storage/src/main/java/pluggable/Pipeline.java @@ -0,0 +1,9 @@ +package pluggable; + +import java.util.List; + +public interface Pipeline { + List exec() throws Exception; + void hIncrement(String key, String field, long value); + void getMembers(String key) throws Exception; +} diff --git a/pluggable-storage/src/main/java/pluggable/Result.java b/pluggable-storage/src/main/java/pluggable/Result.java new file mode 100644 index 00000000..26b5d6d3 --- /dev/null +++ b/pluggable-storage/src/main/java/pluggable/Result.java @@ -0,0 +1,33 @@ +package pluggable; + +import java.util.HashSet; +import java.util.Optional; + +public class Result { + private final Object _item; + + public Result(Object item) { + _item = item; + } + + public Optional asString() { + if (_item instanceof String) { + return Optional.ofNullable((String)_item); + } + return Optional.empty(); + } + + public Optional> asHash() { + if (_item instanceof HashSet) { + return Optional.ofNullable((HashSet) _item); + } + return Optional.empty(); + } + + public Optional asLong() { + if (_item instanceof Long) { + return Optional.ofNullable((Long)_item); + } + return Optional.empty(); + } +} diff --git a/pom.xml b/pom.xml index 1a40cb3c..be1d16cd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 io.split.client java-client-parent - 4.1.6 + 4.19.1 @@ -12,11 +12,6 @@ junit 4.13.1 - - org.slf4j - slf4j-api - 1.7.25 - pom @@ -52,16 +47,15 @@ scm:git@github.com:splitio/java-client.git git@github.com:splitio/java-client.git - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - + + - sonatype releases + ossrh https://oss.sonatype.org/content/repositories/releases - + + + UTF-8 UTF-8 @@ -70,7 +64,10 @@ 1.8 + pluggable-storage + redis-wrapper testing + okhttp-modules client @@ -80,20 +77,8 @@ maven-compiler-plugin 3.3 - 1.8 - 1.8 - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.3 - true - - ossrh - https://oss.sonatype.org/ - true + ${maven.compiler.source} + ${maven.compiler.target} @@ -128,11 +113,66 @@ + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.3.0 + + + checkstyle + validate + + check + + + + + .harness/linter/google-java-style.xml + warning + true + + + + + harness-maven + + + harness-maven + https://harness0.harness.io/pkg/l7B_kbSEQD2wjrM7PShm5w/fme-mvn/maven + + + + + + test + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + -Xdoclint:none + 1.8 + + + + + + + + release + @@ -165,6 +205,18 @@ + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + central + false + published + + diff --git a/redis-wrapper/pom.xml b/redis-wrapper/pom.xml new file mode 100644 index 00000000..0fe70579 --- /dev/null +++ b/redis-wrapper/pom.xml @@ -0,0 +1,112 @@ + + + 4.0.0 + + java-client-parent + io.split.client + 4.19.1 + + redis-wrapper + 3.1.2 + jar + Package for Redis Wrapper Implementation + Implements Redis Pluggable Storage + + + 8 + 8 + + + + io.split.client + pluggable-storage + 2.1.0 + compile + + + redis.clients + jedis + 4.4.8 + + + junit + junit + test + + + + + + release + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + true + central + false + published + + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + true + + + redis.clients:* + + + + + + + + + redis.clients.jedis + redis.clients.jedis + + + + + *:* + + META-INF/license/** + META-INF/* + META-INF/maven/** + META-INF/services/** + LICENSE + NOTICE + /*.txt + build.properties + + + + + + + + + + diff --git a/redis-wrapper/src/main/java/redis/RedisCluster.java b/redis-wrapper/src/main/java/redis/RedisCluster.java new file mode 100644 index 00000000..c63f1b87 --- /dev/null +++ b/redis-wrapper/src/main/java/redis/RedisCluster.java @@ -0,0 +1,270 @@ +package redis; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pluggable.CustomStorageWrapper; +import pluggable.Pipeline; +import redis.clients.jedis.JedisCluster; +import redis.common.CommonRedis; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +class RedisCluster implements CustomStorageWrapper { + private final CommonRedis _commonRedis; + private final JedisCluster jedis; + + public static final String DEFAULT_HASHTAG = "{SPLITIO}" ; + + private static final Logger _log = LoggerFactory.getLogger(RedisCluster.class); + + private String validateHashtag(String hashtag) { + if (hashtag == null) { + _log.warn("The hashtag wasn't set. It's going to use the default hashtag"); + return DEFAULT_HASHTAG; + } + if (hashtag.length() <= 2) { + _log.warn("The hashtag is too short. It's going to use the default hashtag"); + return DEFAULT_HASHTAG; + } + if (!hashtag.startsWith("{")) { + _log.warn("The hashtag doesn't have '{'. It's going to use the default hashtag"); + return DEFAULT_HASHTAG; + } + if (!hashtag.endsWith("}")) { + _log.warn("The hashtag doesn't have '}'. It's going to use the default hashtag"); + return DEFAULT_HASHTAG; + } + + return hashtag; + } + + public RedisCluster(JedisCluster jedisCluster, String prefix, String hashtag) { + this.jedis = jedisCluster; + _commonRedis = CommonRedis.create(validateHashtag(hashtag) + prefix); + } + + @Override + public String get(String key) throws Exception { + try { + return jedis.get(_commonRedis.buildKeyWithPrefix(key)); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List getMany(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return new ArrayList<>(); + } + try { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + return jedis.mget(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void set(String key, String item) throws Exception { + try { + if(key.contains(CommonRedis.TELEMETRY_INIT)) { + String[] splittedKey = key.split("::"); + jedis.hset(_commonRedis.buildKeyWithPrefix(splittedKey[0]), splittedKey[1], item); + return; + } + jedis.set(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void hSet(String key, String field, String item) throws Exception { + try { + jedis.hset(_commonRedis.buildKeyWithPrefix(key), field, item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void delete(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return ; + } + try { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + jedis.del(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public String getAndSet(String key, String item) throws Exception { + //Todo if this method isn't used we should deprecated + try { + return jedis.getSet(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Set getKeysByPrefix(String prefix) throws Exception { + try { + Set keysWithPrefix = jedis.keys(_commonRedis.buildKeyWithPrefix(prefix)); + keysWithPrefix = keysWithPrefix.stream().map(key -> key.replace(_commonRedis.getPrefix() + ".", "")).collect(Collectors.toSet()); + return keysWithPrefix; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long increment(String key, long value) throws Exception { + try { + return jedis.incrBy(_commonRedis.buildKeyWithPrefix(key), value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long hIncrement(String key, String field, long value) throws RedisException { + try { + return jedis.hincrBy(_commonRedis.buildKeyWithPrefix(key), field, value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long decrement(String key, long value) throws Exception { + try { + return jedis.decrBy(_commonRedis.buildKeyWithPrefix(key), value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long pushItems(String key, List items) throws Exception { + try { + long addedItems = jedis.rpush(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + if((CommonRedis.EVENTS_KEY.equals(key) || CommonRedis.IMPRESSIONS_KEY.equals(key)) && addedItems == items.size()) { + jedis.pexpire(key, CommonRedis.IMPRESSIONS_OR_EVENTS_DEFAULT_TTL); + } + return addedItems; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List popItems(String key, long count) throws Exception { + try { + String keyWithPrefix = _commonRedis.buildKeyWithPrefix(key); + List items = jedis.lrange(keyWithPrefix, 0, count-1); + int fetchedCount = items.size(); + jedis.ltrim(keyWithPrefix, fetchedCount, -1); + return items; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + // Return length of redis set. + @Override + public long getItemsCount(String key) throws Exception { + try { + return jedis.scard(_commonRedis.buildKeyWithPrefix(key)); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean itemContains(String key, String item) throws Exception { + try { + return jedis.sismember(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void addItems(String key, List items) throws Exception { + try { + jedis.sadd(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void removeItems(String key, List items) throws Exception { + try { + jedis.srem(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List getItems(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return new ArrayList<>(); + } + try { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + return jedis.mget(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Set getMembers(String key) throws Exception { + Set items; + try { + items = jedis.smembers(_commonRedis.buildKeyWithPrefix(key)); + return new HashSet<>(items); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean connect() throws Exception { + try { + return jedis.getClusterNodes().entrySet().stream().findFirst().map(e -> e.getValue().getResource().isConnected()).orElse(false); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean disconnect() throws Exception { + try { + jedis.close(); + + return true; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Pipeline pipeline() throws Exception { + return null; + } +} \ No newline at end of file diff --git a/redis-wrapper/src/main/java/redis/RedisException.java b/redis-wrapper/src/main/java/redis/RedisException.java new file mode 100644 index 00000000..43518f1e --- /dev/null +++ b/redis-wrapper/src/main/java/redis/RedisException.java @@ -0,0 +1,7 @@ +package redis; + +public class RedisException extends Exception{ + public RedisException(String message) { + super(message); + } +} diff --git a/redis-wrapper/src/main/java/redis/RedisInstance.java b/redis-wrapper/src/main/java/redis/RedisInstance.java new file mode 100644 index 00000000..bc3eb278 --- /dev/null +++ b/redis-wrapper/src/main/java/redis/RedisInstance.java @@ -0,0 +1,115 @@ +package redis; + +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import pluggable.CustomStorageWrapper; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +public class RedisInstance { + + private static final int TIMEOUT = 1000; + + private RedisInstance() { + throw new IllegalStateException("Utility class"); + } + + public static Builder builder() { + return new Builder(); + } + + private static CustomStorageWrapper getRedisInstance(String host, int port, int timeout, String password, int database, + String prefix, int maxTotal) { + JedisPoolConfig poolConfig = new JedisPoolConfig(); + poolConfig.setMaxTotal(maxTotal); + JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, password, database); + return new RedisSingle(jedisPool, prefix); + } + + private static CustomStorageWrapper getRedisInstance(JedisPool jedisPool, String prefix) { + return new RedisSingle(jedisPool, prefix); + } + + private static CustomStorageWrapper getRedisInstance(JedisCluster jedisCluster, String prefix, String hashtag) { + return new RedisCluster(jedisCluster, prefix, hashtag); + } + + public static final class Builder { + private int _timeout = TIMEOUT; + private String _host = "localhost"; + private int _port = 6379; + private String _user = null; + private String _password = null; + private int _database = 0; + private String _prefix = ""; + private String _hashtag = ""; + private JedisPool _jedisPool = null; + private JedisCluster _jedisCluster = null; + private int _maxTotal = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL; + + public Builder timeout(int timeout) { + _timeout = timeout; + return this; + } + + public Builder host(String host) { + _host = host; + return this; + } + + public Builder port(int port) { + _port = port; + return this; + } + + public Builder user(String user) { + _user = user; + return this; + } + + public Builder password(String password) { + _password = password; + return this; + } + + public Builder database(int database) { + _database = database; + return this; + } + + public Builder prefix(String prefix) { + _prefix = prefix; + return this; + } + + public Builder hashtag(String hashtag) { + _hashtag = hashtag; + return this; + } + + public Builder jedisPool(JedisPool jedisPool) { + _jedisPool = jedisPool; + return this; + } + + public Builder jedisCluster(JedisCluster jedisCluster) { + _jedisCluster = jedisCluster; + return this; + } + + public Builder maxTotal(int maxTotal) { + _maxTotal = maxTotal; + return this; + } + + public CustomStorageWrapper build() { + if(_jedisPool != null) { + return RedisInstance.getRedisInstance(_jedisPool, _prefix); + } + if(_jedisCluster != null) { + return RedisInstance.getRedisInstance(_jedisCluster, _prefix, _hashtag); + } + return RedisInstance.getRedisInstance(_host, _port, _timeout, _password, _database, _prefix, _maxTotal); + } + } +} \ No newline at end of file diff --git a/redis-wrapper/src/main/java/redis/RedisPipeline.java b/redis-wrapper/src/main/java/redis/RedisPipeline.java new file mode 100644 index 00000000..e58dfbfa --- /dev/null +++ b/redis-wrapper/src/main/java/redis/RedisPipeline.java @@ -0,0 +1,59 @@ +package redis; + +import pluggable.Result; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.Pipeline; +import redis.common.CommonRedis; + +import java.util.List; +import java.util.stream.Collectors; + +public class RedisPipeline implements pluggable.Pipeline { + private Pipeline _pipelined; + private final JedisPool _jedisPool; + private final CommonRedis _commonRedis; + + public RedisPipeline(JedisPool jedisPool, String prefix) throws RedisException { + _jedisPool = jedisPool; + _commonRedis = CommonRedis.create(prefix); + try (Jedis jedis = _jedisPool.getResource()) { + _pipelined = jedis.pipelined(); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void hIncrement(String key, String field, long value) { + _pipelined.hincrBy(_commonRedis.buildKeyWithPrefix(key), field, value); + } + + @Override + public void getMembers(String key) { + _pipelined.smembers(_commonRedis.buildKeyWithPrefix(key)); + } + + public void delete(List keys) throws RedisException { + if(keys == null || keys.isEmpty()){ + return ; + } + try (Jedis jedis = _jedisPool.getResource()) { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + jedis.del(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List exec() throws Exception { + try{ + List executionResult = _pipelined.syncAndReturnAll(); + return executionResult.stream().map(i -> new Result(i)).collect(Collectors.toList()); + } catch (Exception e) { + throw new RedisException(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/redis-wrapper/src/main/java/redis/RedisSingle.java b/redis-wrapper/src/main/java/redis/RedisSingle.java new file mode 100644 index 00000000..97fdbbf9 --- /dev/null +++ b/redis-wrapper/src/main/java/redis/RedisSingle.java @@ -0,0 +1,251 @@ +package redis; + +import pluggable.CustomStorageWrapper; +import pluggable.HasPipelineSupport; +import pluggable.Pipeline; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.common.CommonRedis; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +class RedisSingle implements CustomStorageWrapper, HasPipelineSupport { + private final CommonRedis _commonRedis; + private final JedisPool jedisPool; + + public RedisSingle(JedisPool jedisPool, String prefix) { + this.jedisPool = jedisPool; + _commonRedis = CommonRedis.create(prefix); + } + + @Override + public String get(String key) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.get(_commonRedis.buildKeyWithPrefix(key)); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List getMany(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return new ArrayList<>(); + } + try (Jedis jedis = this.jedisPool.getResource()) { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + return jedis.mget(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void set(String key, String item) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + if(key.contains(CommonRedis.TELEMETRY_INIT)) { + String[] splittedKey = key.split("::"); + jedis.hset(_commonRedis.buildKeyWithPrefix(splittedKey[0]), splittedKey[1], item); + return; + } + jedis.set(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void hSet(String key, String field, String item) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.hset(_commonRedis.buildKeyWithPrefix(key), field, item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void delete(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return ; + } + try (Jedis jedis = this.jedisPool.getResource()) { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + jedis.del(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public String getAndSet(String key, String item) throws Exception { + //Todo if this method isn't used we should deprecated + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.getSet(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Set getKeysByPrefix(String prefix) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + Set keysWithPrefix = jedis.keys(_commonRedis.buildKeyWithPrefix(prefix)); + if (!_commonRedis.getPrefix().isEmpty()) { + keysWithPrefix = keysWithPrefix.stream().map(key -> key.replace(_commonRedis.getPrefix() + ".", "")).collect(Collectors.toSet()); + } + return keysWithPrefix; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long increment(String key, long value) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.incrBy(_commonRedis.buildKeyWithPrefix(key), value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long hIncrement(String key, String field, long value) throws RedisException { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.hincrBy(_commonRedis.buildKeyWithPrefix(key), field, value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long decrement(String key, long value) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.decrBy(_commonRedis.buildKeyWithPrefix(key), value); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public long pushItems(String key, List items) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + long addedItems = jedis.rpush(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + if(CommonRedis.EVENTS_KEY.equals(key) || CommonRedis.IMPRESSIONS_KEY.equals(key)) { + if(addedItems == items.size()) { + jedis.pexpire(key, CommonRedis.IMPRESSIONS_OR_EVENTS_DEFAULT_TTL); + } + } + return addedItems; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List popItems(String key, long count) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + String keyWithPrefix = _commonRedis.buildKeyWithPrefix(key); + List items = jedis.lrange(keyWithPrefix, 0, count-1); + int fetchedCount = items.size(); + jedis.ltrim(keyWithPrefix, fetchedCount, -1); + return items; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + // Return length of redis set. + @Override + public long getItemsCount(String key) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.scard(_commonRedis.buildKeyWithPrefix(key)); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean itemContains(String key, String item) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return jedis.sismember(_commonRedis.buildKeyWithPrefix(key), item); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void addItems(String key, List items) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.sadd(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public void removeItems(String key, List items) throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + jedis.srem(_commonRedis.buildKeyWithPrefix(key), items.toArray(new String[items.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public List getItems(List keys) throws Exception { + if(keys == null || keys.isEmpty()){ + return new ArrayList<>(); + } + try (Jedis jedis = this.jedisPool.getResource()) { + keys = keys.stream().map(key -> _commonRedis.buildKeyWithPrefix(key)).collect(Collectors.toList()); + + return jedis.mget(keys.toArray(new String[keys.size()])); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Set getMembers(String key) throws Exception { + Set items; + try (Jedis jedis = this.jedisPool.getResource()) { + items = jedis.smembers(_commonRedis.buildKeyWithPrefix(key)); + return new HashSet<>(items); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean connect() throws Exception { + try (Jedis jedis = this.jedisPool.getResource()) { + return "PONG".equalsIgnoreCase(jedis.ping()); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public boolean disconnect() throws Exception { + try { + jedisPool.close(); + return true; + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } + + @Override + public Pipeline pipeline() throws Exception { + try { + return new RedisPipeline(this.jedisPool, _commonRedis.getPrefix()); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } + } +} \ No newline at end of file diff --git a/redis-wrapper/src/main/java/redis/common/CommonRedis.java b/redis-wrapper/src/main/java/redis/common/CommonRedis.java new file mode 100644 index 00000000..895205b9 --- /dev/null +++ b/redis-wrapper/src/main/java/redis/common/CommonRedis.java @@ -0,0 +1,29 @@ +package redis.common; + +public class CommonRedis { + + public static final String TELEMETRY_INIT = "SPLITIO.telemetry.init" ; + public static final String EVENTS_KEY = "SPLITIO.events" ; + public static final String IMPRESSIONS_KEY = "SPLITIO.impressions" ; + public static final long IMPRESSIONS_OR_EVENTS_DEFAULT_TTL = 3600000L; + + private final String _prefix; + + private CommonRedis (String prefix){ + _prefix = prefix; + } + public static CommonRedis create(String prefix) { + return new CommonRedis(prefix); + } + + public String buildKeyWithPrefix(String key) { + if (_prefix.isEmpty()) { + return key; + } + return String.format("%s.%s", _prefix, key); + } + + public String getPrefix() { + return _prefix; + } +} \ No newline at end of file diff --git a/redis-wrapper/src/test/java/redis/RedisInstanceTest.java b/redis-wrapper/src/test/java/redis/RedisInstanceTest.java new file mode 100644 index 00000000..b42292fd --- /dev/null +++ b/redis-wrapper/src/test/java/redis/RedisInstanceTest.java @@ -0,0 +1,30 @@ +package redis; + +import org.junit.Assert; +import org.junit.Test; +import pluggable.CustomStorageWrapper; +import redis.clients.jedis.JedisPool; + +public class RedisInstanceTest { + + @Test + public void testRedisInstanceBuilder() { + CustomStorageWrapper redisInstance = RedisInstance.builder() + .host("localhost") + .timeout(1500) + .database(0) + .build(); + Assert.assertNotNull(redisInstance); + } + + @Test + public void testRedisInstanceBuilderWithJedisPool() { + JedisPool jedisPool = new JedisPool(); + CustomStorageWrapper redisInstance = RedisInstance.builder() + .jedisPool(jedisPool) + .prefix("test") + .build(); + Assert.assertNotNull(redisInstance); + } + +} \ No newline at end of file diff --git a/redis-wrapper/src/test/java/redis/RedisPipelineTest.java b/redis-wrapper/src/test/java/redis/RedisPipelineTest.java new file mode 100644 index 00000000..b93f80a2 --- /dev/null +++ b/redis-wrapper/src/test/java/redis/RedisPipelineTest.java @@ -0,0 +1,53 @@ +package redis; + +import org.junit.Assert; +import org.junit.Test; +import pluggable.Result; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class RedisPipelineTest { + + @Test + public void testHincrement() throws Exception { + RedisPipeline redisPipeline = new RedisPipeline(new JedisPool(), "test-prefix:."); + redisPipeline.hIncrement("test", "key1", 1L); + redisPipeline.hIncrement("test", "key1", 1L); + + List results = redisPipeline.exec(); + Assert.assertEquals(Optional.of(1L), results.get(0).asLong()); + Assert.assertEquals(Optional.of(2L), results.get(1).asLong()); + List keys = new ArrayList<>(); + + keys.add("test"); + redisPipeline.delete(keys); + } + + @Test + public void testGetMembers() throws Exception { + JedisPool jedisPool = new JedisPool(); + RedisPipeline redisPipeline = new RedisPipeline(jedisPool, ""); + try (Jedis jedis = jedisPool.getResource()) { + jedis.sadd("set1", "flag1", "flag2", "flag3"); + jedis.sadd("set2", "flag6", "flag5"); + + redisPipeline.getMembers("set1"); + redisPipeline.getMembers("set2"); + + List results = redisPipeline.exec(); + + Assert.assertEquals(3, results.get(0).asHash().get().size()); + Assert.assertEquals(2, results.get(1).asHash().get().size()); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } finally { + redisPipeline.delete(new ArrayList<>(Arrays.asList("set1", "set2"))); + } + } + +} \ No newline at end of file diff --git a/redis-wrapper/src/test/java/redis/RedisSingleTest.java b/redis-wrapper/src/test/java/redis/RedisSingleTest.java new file mode 100644 index 00000000..8f70c324 --- /dev/null +++ b/redis-wrapper/src/test/java/redis/RedisSingleTest.java @@ -0,0 +1,314 @@ +package redis; + +import org.junit.Assert; +import org.junit.Test; +import pluggable.CustomStorageWrapper; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class RedisSingleTest { + + @Test + public void testSetAndGet() throws Exception { + Map map = new HashMap<>(); + map.put("test-5", "5"); + map.put("test-6", "6"); + map.put("test-7", "7"); + map.put("test-8", "8"); + + CustomStorageWrapper storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + String result = storageWrapper.get("test-7"); + + Assert.assertEquals("7", result); + + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + + @Test + public void testSetAndGetMany() throws Exception { + Map map = new HashMap<>(); + map.put("test-5", "5"); + map.put("test-6", "6"); + map.put("test-7", "7"); + map.put("test-8", "8"); + + CustomStorageWrapper storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + ArrayList keys = new ArrayList<>(); + keys.add("test-5"); + keys.add("test-6"); + keys.add("test-10"); + List expectedResult = Stream.of("5", "6", null).collect(Collectors.toList()); + List result = storageWrapper.getMany(keys); + + Assert.assertEquals(expectedResult, result); + + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + + @Test + public void testGetSet() throws Exception { + String key = "test-getSet"; + Map map = new HashMap<>(); + map.put(key, "5"); + + CustomStorageWrapper storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + storageWrapper.set(key, "5"); + String result = storageWrapper.getAndSet(key, "7"); + Assert.assertEquals("5", result); + Assert.assertEquals("7", storageWrapper.get(key)); + + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + + @Test + public void testGetKeysByPrefix() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + map.put("item-2", "2"); + map.put("item-3", "3"); + map.put("i-4", "4"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + try { + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + Set result = storageWrapper.getKeysByPrefix("item*"); + + Assert.assertEquals(3, result.size()); + Assert.assertTrue(result.contains("item-1")); + Assert.assertTrue(result.contains("item-2")); + Assert.assertTrue(result.contains("item-3")); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testGetKeysByPrefixWithoutCustomPrefix() throws Exception { + Map map = new HashMap<>(); + map.put("SPLITIO.item-1", "1"); + map.put("SPLITIO.item-2", "2"); + map.put("SPLITIO.item-3", "3"); + map.put("SPLITIO.i-4", "4"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), ""); + try { + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + Set result = storageWrapper.getKeysByPrefix("SPLITIO.item*"); + + Assert.assertEquals(3, result.size()); + Assert.assertTrue(result.contains("SPLITIO.item-1")); + Assert.assertTrue(result.contains("SPLITIO.item-2")); + Assert.assertTrue(result.contains("SPLITIO.item-3")); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testIncrementAndDecrement() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "2"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + try { + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + long result = storageWrapper.increment("item-1", 2L); + Assert.assertEquals(4L, result); + + result = storageWrapper.decrement("item-1", 3L); + Assert.assertEquals(1L, result); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testHIncrement() throws Exception { + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + Map map = new HashMap<>(); + map.put("count", "test::12232"); + try { + long result = storageWrapper.hIncrement("count", "test::12232", 2L); + Assert.assertEquals(2L, result); + result = storageWrapper.hIncrement("count", "test::12232", 1L); + Assert.assertEquals(3L, result); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testPushAndPopItems() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + try { + long push = storageWrapper.pushItems("item-1", Arrays.asList("1", "2", "3", "4")); + Assert.assertEquals(4L, push); + + List result = storageWrapper.popItems("item-1", 3); + Assert.assertEquals(3L, result.size()); + + push = storageWrapper.pushItems("item-1", Arrays.asList("5")); + Assert.assertEquals(2L, push); + + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testGetItemsCount() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + try { + storageWrapper.addItems("item-1", Arrays.asList("1", "2", "3", "4")); + long result = storageWrapper.getItemsCount("item-1"); + + Assert.assertEquals(4L, result); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testItemContains() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + try { + storageWrapper.addItems("item-1", Arrays.asList("1", "2", "3", "4")); + boolean result = storageWrapper.itemContains("item-1", "2"); + + Assert.assertTrue(result); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testRemoveItems() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + try { + storageWrapper.addItems("item-1", Arrays.asList("1", "2", "3", "4")); + boolean result = storageWrapper.itemContains("item-1", "2"); + Assert.assertTrue(result); + + storageWrapper.removeItems("item-1", Arrays.asList("2", "4")); + result = storageWrapper.itemContains("item-1", "2"); + Assert.assertFalse(result); + result = storageWrapper.itemContains("item-1", "4"); + Assert.assertFalse(result); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testGetItems() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + map.put("item-2", "2"); + map.put("item-3", "3"); + map.put("i-4", "4"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix:"); + try { + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + Set result = storageWrapper.getKeysByPrefix("item*"); + + Assert.assertEquals(3, result.size()); + List keys = new ArrayList<>(); + keys.add("item-1"); + keys.add("item-2"); + keys.add("item-3"); + List items = storageWrapper.getItems(new ArrayList<>(keys)); + Assert.assertEquals(3, items.size()); + Assert.assertTrue(items.containsAll(Arrays.asList("1", "2", "3"))); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testConnect() throws Exception { + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + Assert.assertTrue(storageWrapper.connect()); + } + + @Test + public void testDisconnect() throws Exception { + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), "test-prefix"); + Assert.assertTrue(storageWrapper.disconnect()); + } + + @Test + public void testWithoutPrefix() throws Exception { + Map map = new HashMap<>(); + map.put("item-1", "1"); + map.put("item-2", "2"); + map.put("item-3", "3"); + map.put("i-4", "4"); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), ""); + try { + for (Map.Entry entry : map.entrySet()) { + storageWrapper.set(entry.getKey(), entry.getValue()); + } + + Set result = storageWrapper.getKeysByPrefix("item*"); + + Assert.assertEquals(3, result.size()); + Assert.assertTrue(result.contains("item-1")); + Assert.assertTrue(result.contains("item-2")); + Assert.assertTrue(result.contains("item-3")); + } finally { + storageWrapper.delete(new ArrayList<>(map.keySet())); + } + } + + @Test + public void testGetMembers() throws Exception { + JedisPool jedisPool = new JedisPool(); + RedisSingle storageWrapper = new RedisSingle(new JedisPool(), ""); + try (Jedis jedis = jedisPool.getResource()) { + jedis.sadd("set1", "flag1", "flag2", "flag3"); + Set flags = storageWrapper.getMembers("set1"); + Assert.assertEquals(3, flags.size()); + } catch (Exception ex) { + throw new RedisException(ex.getMessage()); + } finally { + storageWrapper.delete(new ArrayList<>(Arrays.asList("set1"))); + } + } +} \ No newline at end of file diff --git a/redis-wrapper/src/test/java/redis/common/CommonRedisTest.java b/redis-wrapper/src/test/java/redis/common/CommonRedisTest.java new file mode 100644 index 00000000..4b36b0e9 --- /dev/null +++ b/redis-wrapper/src/test/java/redis/common/CommonRedisTest.java @@ -0,0 +1,17 @@ +package redis.common; + +import org.junit.Assert; +import org.junit.Test; + + +public class CommonRedisTest { + + @Test + public void testBuildKey(){ + CommonRedis commonRedisWithPrefix = CommonRedis.create("testing:"); + Assert.assertEquals("testing:.feature_flag1", commonRedisWithPrefix.buildKeyWithPrefix("feature_flag1")); + + CommonRedis commonRedisWithoutPrefix = CommonRedis.create(""); + Assert.assertEquals("feature_flag2", commonRedisWithoutPrefix.buildKeyWithPrefix("feature_flag2")); + } +} \ No newline at end of file diff --git a/scripts/merge-jacoco.py b/scripts/merge-jacoco.py new file mode 100644 index 00000000..4fdf69a4 --- /dev/null +++ b/scripts/merge-jacoco.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +"""Merge JaCoCo XML reports into one jacoco.xml.""" +import sys +import xml.etree.ElementTree as ET +from pathlib import Path + + +def add_counters(dst, src): + existing = {c.get("type"): c for c in dst.findall("counter")} + for counter in src.findall("counter"): + kind = counter.get("type") + if kind in existing: + current = existing[kind] + current.set("missed", str(int(current.get("missed") or 0) + int(counter.get("missed") or 0))) + current.set("covered", str(int(current.get("covered") or 0) + int(counter.get("covered") or 0))) + else: + dst.append(counter) + + +def main(): + if len(sys.argv) < 3: + sys.exit("usage: merge-jacoco.py ... ") + inputs = [Path(path) for path in sys.argv[1:-1]] + output = Path(sys.argv[-1]) + root = ET.Element("report", {"name": "jacoco"}) + packages = {} + used = 0 + for path in inputs: + if not path.is_file() or path.resolve() == output.resolve(): + continue + used += 1 + report = ET.parse(path).getroot() + for package in report.findall("package"): + name = package.get("name") + dest = packages.get(name) + if dest is None: + dest = ET.Element("package", {"name": name}) + root.append(dest) + packages[name] = dest + classes = {item.get("name") for item in dest.findall("class")} + sourcefiles = {item.get("name") for item in dest.findall("sourcefile")} + for child in list(package): + if child.tag == "counter": + continue + if child.tag == "class" and child.get("name") in classes: + continue + if child.tag == "sourcefile" and child.get("name") in sourcefiles: + continue + dest.append(child) + add_counters(dest, package) + add_counters(root, report) + if used == 0: + sys.exit("no JaCoCo XML reports found") + output.parent.mkdir(parents=True, exist_ok=True) + ET.ElementTree(root).write(output, encoding="utf-8", xml_declaration=True) + print("wrote %s from %s reports" % (output, used)) + + +if __name__ == "__main__": + main() diff --git a/sonar-scanner.sh b/sonar-scanner.sh deleted file mode 100755 index 38905228..00000000 --- a/sonar-scanner.sh +++ /dev/null @@ -1,24 +0,0 @@ -#/bin/bash -e - -if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then - mvn --batch-mode sonar:sonar -DskipTests \ - -Dsonar.pullrequest.provider='GitHub' \ - -Dsonar.pullrequest.github.repository='splitio/java-client' \ - -Dsonar.pullrequest.key=$TRAVIS_PULL_REQUEST \ - -Dsonar.pullrequest.branch=$TRAVIS_PULL_REQUEST_BRANCH \ - -Dsonar.pullrequest.base=$TRAVIS_BRANCH -else - if [ "$TRAVIS_BRANCH" == 'master' ]; then - mvn --batch-mode sonar:sonar -DskipTests \ - -Dsonar.branch.name=$TRAVIS_BRANCH - else - if [ "$TRAVIS_BRANCH" == 'development' ]; then - TARGET_BRANCH='master' - else - TARGET_BRANCH='development' - fi - mvn --batch-mode sonar:sonar -DskipTests \ - -Dsonar.branch.name=$TRAVIS_BRANCH \ - -Dsonar.branch.target=$TARGET_BRANCH - fi -fi diff --git a/testing/pom.xml b/testing/pom.xml index 3d74dc7a..8e436d85 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -2,26 +2,48 @@ 4.0.0 - io.split.client java-client-parent - 4.1.6 + 4.19.1 - java-client-testing jar + 5.0.1 Java Client For Testing Testing suite for Java SDK for Split io.split.client java-client - ${project.version} + ${project.parent.version} junit junit + provided + + + + release + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + central + false + published + false + + + + + + diff --git a/testing/src/main/java/io/split/client/testing/SplitClientForTest.java b/testing/src/main/java/io/split/client/testing/SplitClientForTest.java index 42cf2c92..5b2cc784 100644 --- a/testing/src/main/java/io/split/client/testing/SplitClientForTest.java +++ b/testing/src/main/java/io/split/client/testing/SplitClientForTest.java @@ -1,74 +1,320 @@ package io.split.client.testing; +import io.split.client.SplitAndKey; import io.split.client.SplitClient; import io.split.client.api.Key; +import io.split.client.api.SdkEvent; +import io.split.client.api.SdkEventListener; import io.split.client.api.SplitResult; +import io.split.client.dtos.EvaluationOptions; import io.split.grammar.Treatments; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeoutException; +import static java.util.stream.Collectors.toMap; + public class SplitClientForTest implements SplitClient { - private Map _tests; + private static final SplitResult CONTROL_RESULT = new SplitResult(Treatments.CONTROL, null); + + private final Map _tests; public SplitClientForTest() { _tests = new HashMap<>(); } - public Map tests() { + public Map tests() { return _tests; } public void clearTreatments() { - _tests = new HashMap<>(); + _tests.clear(); } public void registerTreatments(Map treatments) { - _tests.putAll(treatments); + for (Map.Entry entry : treatments.entrySet()) { + registerTreatment(entry.getKey(), entry.getValue()); + } } public void registerTreatment(String feature, String treatment) { - _tests.put(feature, treatment); + registerTreatment(feature, null, treatment); + } + + public void registerTreatment(String feature, String key, String treatment) { + registerTreatment(feature, key, treatment, null); + } + + public void registerTreatment(String feature, String key, String treatment, String config) { + registerTreatment(SplitAndKey.of(feature, key), new SplitResult(treatment, config)); + } + + public void registerTreatment(SplitAndKey splitAndKey, SplitResult splitResult) { + _tests.put(splitAndKey, splitResult); + } + + public String getTreatment(String key, String featureFlagName) { + return getTreatment(key, featureFlagName, Collections.emptyMap()); + } + + public String getTreatment(String key, String featureFlagName, Map attributes) { + return getTreatmentWithConfig(key, featureFlagName, attributes).treatment(); + } + + public String getTreatment(Key key, String featureFlagName, Map attributes) { + return getTreatment(key.matchingKey(), featureFlagName, attributes); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName) { + return getTreatmentWithConfig(key, featureFlagName, Collections.emptyMap()); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes) { + if (_tests.containsKey(SplitAndKey.of(featureFlagName, key))) { + return _tests.get(SplitAndKey.of(featureFlagName, key)); + } + else { + return _tests.getOrDefault(SplitAndKey.of(featureFlagName), CONTROL_RESULT); + } + } + + @Override + public SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes) { + return getTreatmentWithConfig(key.matchingKey(), featureFlagName, attributes); + } + + @Override + public Map getTreatments(String key, List featureFlagNames) { + return getTreatments(key, featureFlagNames, Collections.emptyMap()); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, Map attributes){ + Map treatments = new HashMap<>(); + for (String split : featureFlagNames) { + treatments.put(split, getTreatment(key, split, attributes)); + } + return treatments; + } + + @Override + public Map getTreatments(Key key, List featureFlagNames, Map attributes) { + return getTreatments(key.matchingKey(), featureFlagNames, attributes); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames) { + return getTreatmentsWithConfig(key, featureFlagNames, Collections.emptyMap()); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes) { + Map treatments = new HashMap<>(); + for (String split : featureFlagNames) { + treatments.put(split, getTreatmentWithConfig(key, split, attributes)); + } + return treatments; + } + + @Override + public Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes) { + return getTreatmentsWithConfig(key.matchingKey(), featureFlagNames, attributes); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets) { + return new HashMap<>(); } - public String getTreatment(String key, String split) { - return _tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL; + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes) { + return new HashMap<>(); + } + + @Override + public String getTreatment(String key, String featureFlagName, EvaluationOptions evaluationOptions) { + return getTreatment(key, featureFlagName); + } + + @Override + public String getTreatment(String key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions) { + return getTreatment(key, featureFlagName, attributes); + } + + @Override + public String getTreatment(Key key, String featureFlagName, Map attributes, EvaluationOptions evaluationOptions) { + return getTreatment(key, featureFlagName, attributes); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, EvaluationOptions evaluationOptions) { + return getTreatments(key, featureFlagNames); + } + + @Override + public Map getTreatments(String key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatments(key, featureFlagNames, attributes); + } + + @Override + public Map getTreatments(Key key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatments(key, featureFlagNames, attributes); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, EvaluationOptions evaluationOptions) { + return getTreatmentWithConfig(key, featureFlagName); + } + + @Override + public SplitResult getTreatmentWithConfig(Key key, String featureFlagName, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentWithConfig(key, featureFlagName, attributes); + } + + @Override + public SplitResult getTreatmentWithConfig(String key, String featureFlagName, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentWithConfig(key, featureFlagName, attributes); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfig(key, featureFlagNames, attributes); + } + + @Override + public Map getTreatmentsWithConfig(String key, List featureFlagNames, EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfig(key, featureFlagNames); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSet(String key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, EvaluationOptions evaluationOptions) { + return new HashMap<>(); } - public String getTreatment(String key, String split, Map attributes) { - return _tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL; + @Override + public Map getTreatmentsWithConfigByFlagSets(String key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); + } + + @Override + public Map getTreatmentsByFlagSet(String key, String flagSet, EvaluationOptions evaluationOptions) { + return new HashMap<>(); } - public String getTreatment(Key key, String split, Map attributes) { - return _tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL; + @Override + public Map getTreatmentsWithConfig(Key key, List featureFlagNames, Map attributes, + EvaluationOptions evaluationOptions) { + return getTreatmentsWithConfig(key.matchingKey(), featureFlagNames); + } + + @Override + public Map getTreatmentsByFlagSet(Key key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); } @Override - public SplitResult getTreatmentWithConfig(String key, String split) { - return new SplitResult(_tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL, null); + public Map getTreatmentsByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); } @Override - public SplitResult getTreatmentWithConfig(String key, String split, Map attributes) { - return new SplitResult(_tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL, null); + public Map getTreatmentsWithConfigByFlagSet(Key key, String flagSet, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); } @Override - public SplitResult getTreatmentWithConfig(Key key, String split, Map attributes) { - return new SplitResult(_tests.containsKey(split) - ? _tests.get(split) - : Treatments.CONTROL, null); + public Map getTreatmentsWithConfigByFlagSets(Key key, List flagSets, Map attributes, + EvaluationOptions evaluationOptions) { + return new HashMap<>(); } @Override @@ -76,6 +322,21 @@ public void destroy() { } + @Override + public void on(SdkEvent event, Runnable callback) { + // events are not produced by the test client + } + + @Override + public void on(SdkEvent event, SdkEventListener listener) { + // events are not produced by the test client + } + + @Override + public void off(SdkEvent event) { + // events are not produced by the test client + } + @Override public boolean track(String key, String trafficType, String eventType) { return false; diff --git a/testing/src/main/java/io/split/client/testing/runner/RunWithSplits.java b/testing/src/main/java/io/split/client/testing/runner/RunWithSplits.java index d6b864ad..2ca5aa5f 100644 --- a/testing/src/main/java/io/split/client/testing/runner/RunWithSplits.java +++ b/testing/src/main/java/io/split/client/testing/runner/RunWithSplits.java @@ -1,5 +1,7 @@ package io.split.client.testing.runner; +import io.split.client.SplitAndKey; +import io.split.client.api.SplitResult; import io.split.client.testing.SplitClientForTest; import io.split.client.testing.annotations.SplitTestClient; import org.junit.runners.model.Statement; @@ -24,7 +26,7 @@ public void evaluate() throws Throwable { SplitClientForTest splitClient = findFirstSplitClient(target, target.getClass()); // Preserve the Split state between Test runs - Map priorTests = new HashMap<>(splitClient.tests()); + Map priorTests = new HashMap<>(splitClient.tests()); // Apply the Active Scenario for this if (scenario != null) { @@ -36,7 +38,9 @@ public void evaluate() throws Throwable { } finally { // Clear any Scenario specific changes and re-apply existing splits splitClient.clearTreatments(); - splitClient.registerTreatments(priorTests); + for (Map.Entry entry : priorTests.entrySet()) { + splitClient.registerTreatment(entry.getKey(), entry.getValue()); + } } } @@ -57,4 +61,4 @@ private static SplitClientForTest findFirstSplitClient(Object target, Class t throw new IllegalArgumentException("No SplitTestClient found in hierarchy"); } } -} \ No newline at end of file +} diff --git a/testing/src/test/java/io/split/client/testing/SplitScenarioAnnotationTest.java b/testing/src/test/java/io/split/client/testing/SplitScenarioAnnotationTest.java index 27da6c06..c6caa98e 100644 --- a/testing/src/test/java/io/split/client/testing/SplitScenarioAnnotationTest.java +++ b/testing/src/test/java/io/split/client/testing/SplitScenarioAnnotationTest.java @@ -1,5 +1,8 @@ package io.split.client.testing; +import io.split.client.api.Key; +import io.split.client.api.SplitResult; +import io.split.client.dtos.EvaluationOptions; import io.split.client.testing.annotations.SplitScenario; import io.split.client.testing.annotations.SplitSuite; import io.split.client.testing.annotations.SplitTest; @@ -10,6 +13,9 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; import java.util.Objects; /** @@ -58,8 +64,57 @@ public void testDefaultScenario() { Assert.assertEquals(3, splitClient.tests().size()); Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, DEFAULT_PARENT_FEATURE)); Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE)); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new HashMap<>())); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(new Key(ARBITRARY_KEY, ARBITRARY_KEY), DEFAULT_CLIENT_FEATURE, new HashMap<>())); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentWithConfig(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new HashMap<>()).treatment()); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentWithConfig(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE).treatment()); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentWithConfig(new Key(ARBITRARY_KEY, ARBITRARY_KEY), DEFAULT_CLIENT_FEATURE, new HashMap<>()).treatment()); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatments(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE)).get(DEFAULT_CLIENT_FEATURE)); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatments(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>()).get(DEFAULT_CLIENT_FEATURE)); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatments(new Key(ARBITRARY_KEY, ARBITRARY_KEY), Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>()).get(DEFAULT_CLIENT_FEATURE)); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentsWithConfig(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE)).get(DEFAULT_CLIENT_FEATURE).treatment()); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentsWithConfig(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>()).get(DEFAULT_CLIENT_FEATURE).treatment()); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatmentsWithConfig(new Key(ARBITRARY_KEY, ARBITRARY_KEY), Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>()).get(DEFAULT_CLIENT_FEATURE).treatment()); Assert.assertEquals(OFF_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, OVERRIDDEN_PARENT_FEATURE)); Assert.assertEquals(CONTROL_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, CONTROL_FEATURE)); + + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset", new HashMap<>())); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset")); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset")); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new HashMap<>())); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset", new HashMap<>())); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset")); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset")); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new HashMap<>())); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"))); + + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(new Key(ARBITRARY_KEY, ARBITRARY_KEY), DEFAULT_CLIENT_FEATURE, new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new SplitResult(ON_TREATMENT, null), splitClient.getTreatmentWithConfig(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new SplitResult(ON_TREATMENT, null), splitClient.getTreatmentWithConfig(ARBITRARY_KEY, DEFAULT_CLIENT_FEATURE, new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new SplitResult(ON_TREATMENT, null), splitClient.getTreatmentWithConfig(new Key(ARBITRARY_KEY, ARBITRARY_KEY), DEFAULT_CLIENT_FEATURE, new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(Collections.singletonMap(DEFAULT_CLIENT_FEATURE, ON_TREATMENT), splitClient.getTreatments(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(Collections.singletonMap(DEFAULT_CLIENT_FEATURE, ON_TREATMENT), splitClient.getTreatments(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(Collections.singletonMap(DEFAULT_CLIENT_FEATURE, new SplitResult(ON_TREATMENT, null)), splitClient.getTreatmentsWithConfig(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new SplitResult(ON_TREATMENT, null), splitClient.getTreatmentsWithConfig(ARBITRARY_KEY, Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>(), new EvaluationOptions(new HashMap<>())).get(DEFAULT_CLIENT_FEATURE)); + Assert.assertEquals(Collections.singletonMap(DEFAULT_CLIENT_FEATURE, new SplitResult(ON_TREATMENT, null)), splitClient.getTreatmentsWithConfig(new Key(ARBITRARY_KEY, ARBITRARY_KEY), Arrays.asList(DEFAULT_CLIENT_FEATURE), new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset", new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset", new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSet(ARBITRARY_KEY, "flagset", new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset", new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset", new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSet(ARBITRARY_KEY, "flagset", new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new HashMap<>(), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new EvaluationOptions(new HashMap<>()))); + Assert.assertEquals(new HashMap<>(), splitClient.getTreatmentsWithConfigByFlagSets(ARBITRARY_KEY, Arrays.asList("flagset"), new EvaluationOptions(new HashMap<>()))); } /** @@ -166,4 +221,4 @@ public void testScenariosAnnotation() { Assert.assertEquals(ON_TREATMENT, splitClient.getTreatment(ARBITRARY_KEY, "SCENARIO_FEATURE_3")); } } -} \ No newline at end of file +} diff --git a/update_maven_settings.sh b/update_maven_settings.sh new file mode 100755 index 00000000..8bea975f --- /dev/null +++ b/update_maven_settings.sh @@ -0,0 +1,161 @@ +#!/bin/bash + +# Script to update Maven settings.xml with Central Repository credentials using xmlstarlet + +# ANSI color codes +GREEN='\033[0;32m' +YELLOW='\033[0;33m' +BLUE='\033[0;34m' +RED='\033[0;31m' +NC='\033[0m' # No Color + +# Check if xmlstarlet is installed +if ! command -v xmlstarlet &> /dev/null; then + echo -e "${RED}Error: xmlstarlet is not installed.${NC}" + echo "Please install xmlstarlet first:" + echo " macOS: brew install xmlstarlet" + echo " Debian/Ubuntu: sudo apt-get install xmlstarlet" + echo " RHEL/CentOS/Fedora: sudo yum install xmlstarlet" + echo "Then run this script again." + exit 1 +fi + +# Default values +DEFAULT_SETTINGS_PATH="$HOME/.m2/settings.xml" +DEFAULT_SERVER_ID="central" + +echo -e "${BLUE}Maven Settings.xml Update Script${NC}" +echo "This script will update your Maven settings.xml with Central Repository credentials." +echo + +# Ask for settings.xml path or use default +read -p "Path to settings.xml [$DEFAULT_SETTINGS_PATH]: " SETTINGS_PATH +SETTINGS_PATH=${SETTINGS_PATH:-$DEFAULT_SETTINGS_PATH} + +# Variables to store existing values +EXISTING_USERNAME="" +EXISTING_PASSWORD="" + +# Extract existing values if settings.xml exists +if [ -f "$SETTINGS_PATH" ] && command -v xmlstarlet &> /dev/null; then + # Check if the file is valid XML + if xmlstarlet val "$SETTINGS_PATH" &> /dev/null; then + echo -e "${YELLOW}Reading existing settings from ${SETTINGS_PATH}...${NC}" + + # Extract existing server ID + DEFAULT_SERVER_ID=$(xmlstarlet sel -t -v "/settings/servers/server[1]/id" "$SETTINGS_PATH" 2>/dev/null || echo "$DEFAULT_SERVER_ID") + + # Extract existing username and password for the server + EXISTING_USERNAME=$(xmlstarlet sel -t -v "/settings/servers/server[id='$DEFAULT_SERVER_ID']/username" "$SETTINGS_PATH" 2>/dev/null || echo "") + EXISTING_PASSWORD=$(xmlstarlet sel -t -v "/settings/servers/server[id='$DEFAULT_SERVER_ID']/password" "$SETTINGS_PATH" 2>/dev/null || echo "") + fi +fi + +# Ask for server ID or use default/existing +read -p "Server ID [$DEFAULT_SERVER_ID]: " SERVER_ID +SERVER_ID=${SERVER_ID:-$DEFAULT_SERVER_ID} + +# Ask for username (show existing if available) +USERNAME_PROMPT="Username" +if [ -n "$EXISTING_USERNAME" ]; then + USERNAME_PROMPT="Username (current: $EXISTING_USERNAME)" +fi +read -p "$USERNAME_PROMPT: " USERNAME +USERNAME=${USERNAME:-$EXISTING_USERNAME} + +# Ask for password (indicate if existing) +PASSWORD_PROMPT="Password" +if [ -n "$EXISTING_PASSWORD" ]; then + PASSWORD_PROMPT="Password (leave empty to keep current)" +fi +read -s -p "$PASSWORD_PROMPT: " PASSWORD +echo +# Only use existing password if the user didn't enter a new one +if [ -z "$PASSWORD" ] && [ -n "$EXISTING_PASSWORD" ]; then + PASSWORD="$EXISTING_PASSWORD" +fi + +# Create .m2 directory if it doesn't exist +M2_DIR=$(dirname "$SETTINGS_PATH") +mkdir -p "$M2_DIR" + +# No GPG configuration needed + +# Function to create a new settings.xml file +create_new_settings() { + echo -e "${YELLOW}Creating new settings.xml file...${NC}" + cat > "$SETTINGS_PATH" << EOF + + + + + $SERVER_ID + $USERNAME + $PASSWORD + + + +EOF +} + +# Check if settings.xml exists +if [ -f "$SETTINGS_PATH" ]; then + echo -e "${YELLOW}Existing settings.xml found. Backing up to ${SETTINGS_PATH}.bak${NC}" + cp "$SETTINGS_PATH" "${SETTINGS_PATH}.bak" + + # Check if the file is valid XML + if ! xmlstarlet val "$SETTINGS_PATH" &> /dev/null; then + echo -e "${RED}Warning: The existing settings.xml is not valid XML.${NC}" + read -p "Do you want to create a new settings.xml file? (y/n): " CREATE_NEW + if [[ $CREATE_NEW =~ ^[Yy]$ ]]; then + create_new_settings + else + echo -e "${RED}Exiting without making changes.${NC}" + exit 1 + fi + else + # Check if servers element exists + if ! xmlstarlet sel -t -v "/settings/servers" "$SETTINGS_PATH" &> /dev/null; then + echo -e "${YELLOW}No servers section found. Adding servers section...${NC}" + xmlstarlet ed --inplace \ + -s "/settings" -t elem -n "servers" \ + -s "/settings/servers" -t elem -n "server" \ + -s "/settings/servers/server" -t elem -n "id" -v "$SERVER_ID" \ + -s "/settings/servers/server" -t elem -n "username" -v "$USERNAME" \ + -s "/settings/servers/server" -t elem -n "password" -v "$PASSWORD" \ + "$SETTINGS_PATH" + else + # Check if server with this ID already exists + if xmlstarlet sel -t -v "/settings/servers/server[id='$SERVER_ID']" "$SETTINGS_PATH" &> /dev/null; then + echo -e "${YELLOW}Server with ID '$SERVER_ID' already exists. Updating credentials...${NC}" + # Update existing server credentials + xmlstarlet ed --inplace \ + -u "/settings/servers/server[id='$SERVER_ID']/username" -v "$USERNAME" \ + -u "/settings/servers/server[id='$SERVER_ID']/password" -v "$PASSWORD" \ + "$SETTINGS_PATH" + else + echo -e "${YELLOW}Adding new server with ID '$SERVER_ID'...${NC}" + # Add new server to existing servers section + xmlstarlet ed --inplace \ + -s "/settings/servers" -t elem -n "server" \ + -s "/settings/servers/server[last()]" -t elem -n "id" -v "$SERVER_ID" \ + -s "/settings/servers/server[last()]" -t elem -n "username" -v "$USERNAME" \ + -s "/settings/servers/server[last()]" -t elem -n "password" -v "$PASSWORD" \ + "$SETTINGS_PATH" + fi + fi + fi +else + create_new_settings +fi + +# Make sure the file has the right permissions +chmod 600 "$SETTINGS_PATH" + +echo -e "${GREEN}Maven settings.xml updated successfully at $SETTINGS_PATH${NC}" +echo -e "${GREEN}Server ID: $SERVER_ID${NC}" +echo -e "${GREEN}Username: $USERNAME${NC}" +echo -e "${GREEN}Password: ********${NC}" +