name: Daily

on:
  pull_request:
    branches:
      # any PR to a release branch.
      - "[0-9].[0-9]"
      - "unstable"
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:
    inputs:
      skipjobs:
        description: "jobs to skip (delete the ones you wanna keep, do not leave empty)"
        default: "valgrind,sanitizer,tls,freebsd,macos,alpine,32bit,iothreads,ubuntu,rpm-distros,malloc,specific,fortify,reply-schema"
      skiptests:
        description: "tests to skip (delete the ones you wanna keep, do not leave empty)"
        default: "valkey,modules,sentinel,cluster,unittest"
      test_args:
        description: "extra test arguments"
        default: ""
      cluster_test_args:
        description: "extra cluster / sentinel test arguments"
        default: ""
      use_repo:
        description: "repo owner and name"
        default: "valkey-io/valkey"
      use_git_ref:
        description: "git branch or sha to use"
        default: "unstable"

concurrency:
  group: daily-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  test-ubuntu-jemalloc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'ubuntu')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make all-with-unit-tests SERVER_CFLAGS='-Werror -DSERVER_TEST'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}
      - name: legacy unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-server test all --accurate
      - name: new unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-unit-tests --accurate

  test-ubuntu-jemalloc-fortify:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'fortify')
    container: ubuntu:plucky
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          apt-get update && apt-get install -y make gcc-13
          update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100
          make all-with-unit-tests CC=gcc OPT=-O3 SERVER_CFLAGS='-Werror -DSERVER_TEST -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3'
      - name: testprep
        run: apt-get install -y tcl8.6 tclx procps
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}
      - name: legacy unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-server test all --accurate
      - name: new unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-unit-tests --accurate

  test-ubuntu-libc-malloc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'malloc')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make MALLOC=libc SERVER_CFLAGS='-Werror'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-ubuntu-no-malloc-usable-size:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'malloc')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make MALLOC=libc CFLAGS=-DNO_MALLOC_USABLE_SIZE SERVER_CFLAGS='-Werror'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-ubuntu-32bit:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, '32bit')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          sudo apt-get update && sudo apt-get install libc6-dev-i386
          make 32bit SERVER_CFLAGS='-Werror -DSERVER_TEST'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: |
          make -C tests/modules 32bit # the script below doesn't have an argument, we must build manually ahead of time
          CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}
      - name: legacy unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-server test all --accurate
      - name: new unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-unit-tests --accurate

  test-ubuntu-tls:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'tls')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          make BUILD_TLS=yes SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          sudo apt-get install tcl8.6 tclx tcl-tls
          ./utils/gen-test-certs.sh
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: |
          ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs --tls --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: |
          CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs --tls --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: |
          ./runtest-sentinel --tls ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: |
          ./runtest-cluster --tls ${{github.event.inputs.cluster_test_args}}

  test-ubuntu-tls-no-tls:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'tls')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          make BUILD_TLS=yes SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          sudo apt-get install tcl8.6 tclx tcl-tls
          ./utils/gen-test-certs.sh
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: |
          ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: |
          CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: |
          ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: |
          ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-ubuntu-io-threads:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'iothreads')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          make SERVER_CFLAGS='-Werror'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --io-threads --accurate --verbose --tags network --dump-logs ${{github.event.inputs.test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster --io-threads ${{github.event.inputs.cluster_test_args}}
  test-ubuntu-reclaim-cache:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'specific')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          make SERVER_CFLAGS='-Werror'
      - name: testprep
        run: "sudo apt-get install vmtouch\nmkdir /tmp/master \nmkdir /tmp/slave\n"
      - name: warm up
        run: |
          ./src/valkey-server --daemonize yes --logfile /dev/null
          ./src/valkey-benchmark -n 1 > /dev/null
          ./src/valkey-cli save | grep OK > /dev/null
          vmtouch -v ./dump.rdb > /dev/null
      - name: test
        run: |
          echo "test SAVE doesn't increase cache"
          CACHE0=$(grep -w file /sys/fs/cgroup/memory.stat | awk '{print $2}')
          echo "$CACHE0"
          ./src/valkey-server --daemonize yes --logfile /dev/null --dir /tmp/master --port 8080 --repl-diskless-sync no --pidfile /tmp/master/valkey.pid --rdbcompression no --enable-debug-command yes
          sleep 1 # wait for server startup
          ./src/valkey-cli -p 8080 debug populate 10000 k 102400
          ./src/valkey-server --daemonize yes --logfile /dev/null --dir /tmp/slave --port 8081 --repl-diskless-load disabled --rdbcompression no
          sleep 1 # wait for server startup
          ./src/valkey-cli -p 8080 save > /dev/null
          VMOUT=$(vmtouch -v /tmp/master/dump.rdb)
          echo $VMOUT
          grep -q " 0%" <<< $VMOUT
          CACHE=$(grep -w file /sys/fs/cgroup/memory.stat | awk '{print $2}')
          echo "$CACHE"
          if [ "$(( $CACHE-$CACHE0 ))" -gt "8000000" ]; then exit 1; fi
          echo "test replication doesn't increase cache"
          ./src/valkey-cli -p 8081 REPLICAOF 127.0.0.1 8080 > /dev/null
          while [ $(./src/valkey-cli -p 8081 info replication | grep "master_link_status:down") ]; do sleep 1; done;
          sleep 1 # wait for the completion of cache reclaim bio
          VMOUT=$(vmtouch -v /tmp/master/dump.rdb)
          echo $VMOUT
          grep -q " 0%" <<< $VMOUT
          VMOUT=$(vmtouch -v /tmp/slave/dump.rdb)
          echo $VMOUT
          grep -q " 0%" <<< $VMOUT
          CACHE=$(grep -w file /sys/fs/cgroup/memory.stat | awk '{print $2}')
          echo "$CACHE"
          if [ "$(( $CACHE-$CACHE0 ))" -gt "8000000" ]; then exit 1; fi

          echo "test reboot doesn't increase cache"
          PID=$(cat /tmp/master/valkey.pid)
          kill -15 $PID
          while [ -x /proc/${PID} ]; do sleep 1; done
          ./src/valkey-server --daemonize yes --logfile /dev/null --dir /tmp/master --port 8080
          sleep 1 # wait for server startup
          while [ $(./src/valkey-cli -p 8080 info persistence | grep "loading:1") ]; do sleep 1; done;
          sleep 1 # wait for the completion of cache reclaim bio
          VMOUT=$(vmtouch -v /tmp/master/dump.rdb)
          echo $VMOUT
          grep -q " 0%" <<< $VMOUT
          CACHE=$(grep -w file /sys/fs/cgroup/memory.stat | awk '{print $2}')
          echo "$CACHE"
          if [ "$(( $CACHE-$CACHE0 ))" -gt "8000000" ]; then exit 1; fi

  test-valgrind-test:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'valgrind') && !contains(github.event.inputs.skiptests, 'valkey')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make valgrind SERVER_CFLAGS='-Werror -DSERVER_TEST'
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx valgrind -y
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --valgrind --no-latency --verbose --clients 1 --timeout 2400 --dump-logs ${{github.event.inputs.test_args}}

  test-valgrind-misc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'valgrind') && !(contains(github.event.inputs.skiptests, 'modules') && contains(github.event.inputs.skiptests, 'unittest'))
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make valgrind SERVER_CFLAGS='-Werror -DSERVER_TEST'
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx valgrind -y
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --valgrind --no-latency --verbose --clients 1 --timeout 2400 --dump-logs ${{github.event.inputs.test_args}}
      - name: unittest
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: |
          valgrind --track-origins=yes --suppressions=./src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full --log-file=err.txt ./src/valkey-server test all --valgrind
          if grep -q 0x err.txt; then cat err.txt; exit 1; fi

  test-valgrind-no-malloc-usable-size-test:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'valgrind') && !contains(github.event.inputs.skiptests, 'valkey')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make valgrind CFLAGS="-DNO_MALLOC_USABLE_SIZE -DSERVER_TEST" SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx valgrind -y
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --valgrind --no-latency --verbose --clients 1 --timeout 2400 --dump-logs ${{github.event.inputs.test_args}}

  test-valgrind-no-malloc-usable-size-misc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'valgrind') && !(contains(github.event.inputs.skiptests, 'modules') && contains(github.event.inputs.skiptests, 'unittest'))
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make valgrind CFLAGS="-DNO_MALLOC_USABLE_SIZE -DSERVER_TEST" SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx valgrind -y
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --valgrind --no-latency --verbose --clients 1 --timeout 2400 --dump-logs ${{github.event.inputs.test_args}}
      - name: unittest
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: |
          valgrind --track-origins=yes --suppressions=./src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full --log-file=err.txt ./src/valkey-server test all --valgrind
          if grep -q 0x err.txt; then cat err.txt; exit 1; fi

  test-sanitizer-address:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'sanitizer')
    timeout-minutes: 14400
    strategy:
      matrix:
        compiler: [gcc, clang]
    env:
      CC: ${{ matrix.compiler }}
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make all-with-unit-tests OPT=-O3 SANITIZER=address SERVER_CFLAGS='-DSERVER_TEST -Werror'
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx -y
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --accurate --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}
      - name: legacy unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-server test all
      - name: new unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-unit-tests

  test-sanitizer-undefined:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'sanitizer')
    timeout-minutes: 14400
    strategy:
      matrix:
        compiler: [gcc, clang]
    env:
      CC: ${{ matrix.compiler }}
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make all-with-unit-tests OPT=-O3 SANITIZER=undefined SERVER_CFLAGS='-DSERVER_TEST -Werror' LUA_DEBUG=yes # we (ab)use this flow to also check Lua C API violations
      - name: testprep
        run: |
          sudo apt-get update
          sudo apt-get install tcl8.6 tclx -y
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --accurate --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}
      - name: legacy unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-server test all --accurate
      - name: new unit tests
        if: true && !contains(github.event.inputs.skiptests, 'unittest')
        run: ./src/valkey-unit-tests --accurate
  test-rpm-distros-jemalloc:
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'rpm-distros')
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: test-almalinux8-jemalloc
            container: almalinux:8
            install_epel: true
          - name: test-almalinux9-jemalloc
            container: almalinux:8
            install_epel: true
          - name: test-centosstream9-jemalloc
            container: quay.io/centos/centos:stream9
            install_epel: true
          - name: test-fedoralatest-jemalloc
            container: fedora:latest
          - name: test-fedorarawhide-jemalloc
            container: fedora:rawhide

    name: ${{ matrix.name }}
    runs-on: ubuntu-latest

    container: ${{ matrix.container }}
    timeout-minutes: 14400

    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: Install EPEL
        if: matrix.install_epel
        run: dnf -y install epel-release
      - name: make
        run: |
          dnf -y install gcc make procps-ng which /usr/bin/kill /usr/bin/awk
          make -j SERVER_CFLAGS='-Werror'
      - name: testprep
        run: dnf -y install tcl tcltls
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-rpm-distros-tls-module:
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'tls')
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: test-almalinux8-tls-module
            container: almalinux:8
            install_epel: true
          - name: test-almalinux9-tls-module
            container: almalinux:8
            install_epel: true
          - name: test-centosstream9-tls-module
            container: quay.io/centos/centos:stream9
            install_epel: true
          - name: test-fedoralatest-tls-module
            container: fedora:latest
          - name: test-fedorarawhide-tls-module
            container: fedora:rawhide

    name: ${{ matrix.name }}
    runs-on: ubuntu-latest

    container: ${{ matrix.container }}
    timeout-minutes: 14400

    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: Install EPEL
        if: matrix.install_epel
        run: dnf -y install epel-release
      - name: make
        run: |
          dnf -y install make gcc openssl-devel openssl procps-ng which /usr/bin/kill /usr/bin/awk
          make -j BUILD_TLS=module SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          dnf -y install tcl tcltls
          ./utils/gen-test-certs.sh
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: |
          ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs --tls-module --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: |
          CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs --tls-module --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: |
          ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: |
          ./runtest-cluster --tls-module ${{github.event.inputs.cluster_test_args}}

  test-rpm-distros-tls-module-no-tls:
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && github.event.pull_request.base.ref != 'unstable')) &&
      !contains(github.event.inputs.skipjobs, 'tls')
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: test-almalinux8-tls-module-no-tls
            container: almalinux:8
            install_epel: true
          - name: test-almalinux9-tls-module-no-tls
            container: almalinux:8
            install_epel: true
          - name: test-centosstream9-tls-module-no-tls
            container: quay.io/centos/centos:stream9
            install_epel: true
          - name: test-fedoralatest-tls-module-no-tls
            container: fedora:latest
          - name: test-fedorarawhide-tls-module-no-tls
            container: fedora:rawhide

    name: ${{ matrix.name }}
    runs-on: ubuntu-latest

    container: ${{ matrix.container }}
    timeout-minutes: 14400

    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: Install EPEL
        if: matrix.install_epel
        run: dnf -y install epel-release
      - name: make
        run: |
          dnf -y install make gcc openssl-devel openssl procps-ng which /usr/bin/kill /usr/bin/awk
          make -j BUILD_TLS=module SERVER_CFLAGS='-Werror'
      - name: testprep
        run: |
          dnf -y install tcl tcltls
          ./utils/gen-test-certs.sh
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: |
          ./runtest --accurate --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: |
          CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: |
          ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: |
          ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-macos-latest:
    runs-on: macos-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'macos') && !(contains(github.event.inputs.skiptests, 'valkey') && contains(github.event.inputs.skiptests, 'modules'))
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make SERVER_CFLAGS='-Werror'
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --clients 1 --no-latency --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --clients 1 --no-latency --dump-logs ${{github.event.inputs.test_args}}

  test-macos-latest-sentinel:
    runs-on: macos-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'macos') && !contains(github.event.inputs.skiptests, 'sentinel')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make SERVER_CFLAGS='-Werror'
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}

  test-macos-latest-cluster:
    runs-on: macos-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'macos') && !contains(github.event.inputs.skiptests, 'cluster')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make SERVER_CFLAGS='-Werror'
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  build-macos:
    strategy:
      matrix:
        os: [macos-14]
    runs-on: ${{ matrix.os }}
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'macos')
    timeout-minutes: 14400
    steps:
      - uses: maxim-lobanov/setup-xcode@60606e260d2fc5762a71e64e74b2174e8ea3c8bd # v1.6.0
        with:
          xcode-version: latest
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make SERVER_CFLAGS='-Werror -DSERVER_TEST'

  test-freebsd:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'freebsd')
    timeout-minutes: 14400
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: test
        uses: cross-platform-actions/action@39a2a80642eca0947594ad03e4355dc3d28c617a # v0.32.0
        with:
          operating_system: freebsd
          environment_variables: MAKE
          version: 13.2
          shell: bash
          run: |
            sudo pkg install -y bash gmake lang/tcl86 lang/tclX
            gmake
            ./runtest --single unit/keyspace --single unit/auth --single unit/networking --single unit/protocol

  test-alpine-jemalloc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'alpine')
    container: alpine:latest
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          apk add build-base
          make SERVER_CFLAGS='-Werror'
      - name: testprep
        run: apk add tcl procps tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  test-alpine-libc-malloc:
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'alpine')
    container: alpine:latest
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: |
          apk add build-base
          make SERVER_CFLAGS='-Werror' USE_JEMALLOC=no CFLAGS=-DUSE_MALLOC_USABLE_SIZE
      - name: testprep
        run: apk add tcl procps tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest ${{ github.event_name != 'pull_request' && '--accurate' || '' }} --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster ${{github.event.inputs.cluster_test_args}}

  reply-schemas-validator:
    runs-on: ubuntu-latest
    timeout-minutes: 14400
    if: |
      (github.event_name == 'workflow_dispatch' ||
        (github.event_name == 'schedule' && github.repository == 'valkey-io/valkey') ||
        (github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'run-extra-tests') || github.event.pull_request.base.ref != 'unstable'))) &&
      !contains(github.event.inputs.skipjobs, 'reply-schema')
    steps:
      - name: prep
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "GITHUB_REPOSITORY=${{github.event.inputs.use_repo}}" >> $GITHUB_ENV
          echo "GITHUB_HEAD_REF=${{github.event.inputs.use_git_ref}}" >> $GITHUB_ENV
          echo "skipjobs: ${{github.event.inputs.skipjobs}}"
          echo "skiptests: ${{github.event.inputs.skiptests}}"
          echo "test_args: ${{github.event.inputs.test_args}}"
          echo "cluster_test_args: ${{github.event.inputs.cluster_test_args}}"
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: ${{ env.GITHUB_REPOSITORY }}
          ref: ${{ env.GITHUB_HEAD_REF }}
      - name: make
        run: make SERVER_CFLAGS='-Werror -DLOG_REQ_RES'
      - name: testprep
        run: sudo apt-get install tcl8.6 tclx
      - name: test
        if: true && !contains(github.event.inputs.skiptests, 'valkey')
        run: ./runtest --log-req-res --no-latency --dont-clean --force-resp3 --tags -slow --verbose --dump-logs  ${{github.event.inputs.test_args}}
      - name: module api test
        if: true && !contains(github.event.inputs.skiptests, 'modules')
        run: CFLAGS='-Werror' ./runtest-moduleapi --log-req-res --no-latency --dont-clean --force-resp3 --dont-pre-clean --verbose --dump-logs ${{github.event.inputs.test_args}}
      - name: sentinel tests
        if: true && !contains(github.event.inputs.skiptests, 'sentinel')
        run: ./runtest-sentinel --log-req-res --dont-clean --force-resp3 ${{github.event.inputs.cluster_test_args}}
      - name: cluster tests
        if: true && !contains(github.event.inputs.skiptests, 'cluster')
        run: ./runtest-cluster --log-req-res --dont-clean --force-resp3 ${{github.event.inputs.cluster_test_args}}
      - name: Install Python dependencies
        uses: py-actions/py-dependency-install@30aa0023464ed4b5b116bd9fbdab87acf01a484e # v4.1.0
        with:
          path: "./utils/req-res-validator/requirements.txt"
      - name: validator
        run: ./utils/req-res-log-validator.py --verbose --fail-missing-reply-schemas ${{ (!contains(github.event.inputs.skiptests, 'valkey') && !contains(github.event.inputs.skiptests, 'module') && !contains(github.event.inputs.sentinel, 'valkey') && !contains(github.event.inputs.skiptests, 'cluster')) && github.event.inputs.test_args == '' && github.event.inputs.cluster_test_args == '' && '--fail-commands-not-all-hit' || '' }}

  notify-about-job-results:
    runs-on: ubuntu-latest
    if: always() && github.event_name == 'schedule' && github.repository == 'valkey-io/valkey'
    needs: [test-ubuntu-jemalloc, test-ubuntu-jemalloc-fortify, test-ubuntu-libc-malloc, test-ubuntu-no-malloc-usable-size, test-ubuntu-32bit, test-ubuntu-tls, test-ubuntu-tls-no-tls, test-ubuntu-io-threads, test-ubuntu-reclaim-cache, test-valgrind-test, test-valgrind-misc, test-valgrind-no-malloc-usable-size-test, test-valgrind-no-malloc-usable-size-misc, test-sanitizer-address, test-sanitizer-undefined, test-rpm-distros-jemalloc, test-rpm-distros-tls-module, test-rpm-distros-tls-module-no-tls, test-macos-latest, test-macos-latest-sentinel, test-macos-latest-cluster, build-macos, test-freebsd, test-alpine-jemalloc, test-alpine-libc-malloc, reply-schemas-validator]
    steps:
      - name: Collect job status
        run: |
          FAILED_JOBS=()
          NEEDS_JSON='${{ toJSON(needs) }}'
          JOBS=($(echo "$NEEDS_JSON" | jq 'keys' | tr -d '[] ,'))
          for JOB in ${JOBS[@]}; do
            JOB_RESULT=$(echo "$NEEDS_JSON" | jq ".[$JOB][\"result\"]" | tr -d '"')
            if [ $JOB_RESULT = "failure" ]; then
              FAILED_JOBS+=($JOB)
            fi
          done

          if [[ ${#FAILED_JOBS[@]} -ne 0 ]]; then
            echo "FAILED_JOBS=${FAILED_JOBS[@]}" >> $GITHUB_ENV
            echo "STATUS=failure" >> $GITHUB_ENV
          else
            echo "STATUS=success" >> $GITHUB_ENV
          fi
      - name: Notify about results
        uses: ravsamhq/notify-slack-action@042f29088bb3bdbda5b4ff7b4818466a277fa8f7 # v2.5.0
        with:
          status: ${{ env.STATUS }}
          notify_when: "failure"
          notification_title: "Daily test run <${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}|Failure>"
          message_format: ":fire: Tests failed: ${{ env.FAILED_JOBS }}"
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_NOTIFICATIONS_WEBHOOK_URL }}
