on:
  push:
    branches: ["master", "tokio-*.x"]
  pull_request:
    branches: ["master", "tokio-*.x"]

name: CI

env:
  RUSTFLAGS: -Dwarnings
  RUST_BACKTRACE: 1
  # Change to specific Rust release to pin
  rust_stable: stable
  rust_nightly: nightly-2022-11-03
  rust_clippy: 1.65.0
  # When updating this, also update:
  # - README.md
  # - tokio/README.md
  # - CONTRIBUTING.md
  # - tokio/Cargo.toml
  # - tokio-util/Cargo.toml
  # - tokio-test/Cargo.toml
  # - tokio-stream/Cargo.toml
  rust_min: 1.56.0

defaults:
  run:
    shell: bash

permissions:
  contents: read

jobs:
  # Depends on all action sthat are required for a "successful" CI run.
  tests-pass:
    name: all systems go
    runs-on: ubuntu-latest
    needs:
      - test
      - test-parking_lot
      - valgrind
      - test-unstable
      - miri
      - asan
      - semver
      - cross-check
      - cross-test
      - no-atomic-u64
      - features
      - minrust
      - minimal-versions
      - fmt
      - clippy
      - docs
      - loom-compile
      - check-readme
      - test-hyper
      - x86_64-fortanix-unknown-sgx
      - wasm32-unknown-unknown
      - wasm32-wasi
      - check-external-types
    steps:
      - run: exit 0

  test:
    name: test tokio full
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
            toolchain: ${{ env.rust_stable }}
      - name: Install Rust
        run: rustup update stable
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack

      # Run `tokio` with `full` features. This excludes testing utilities which
      # can alter the runtime behavior of Tokio.
      - name: test tokio full
        run: cargo test --features full
        working-directory: tokio

      # Test **all** crates in the workspace with all features.
      - name: test all --all-features
        run: cargo test --workspace --all-features

      # Run integration tests for each feature
      - name: test tests-integration --each-feature
        run: cargo hack test --each-feature
        working-directory: tests-integration

      # Run macro build tests
      - name: test tests-build --each-feature
        run: cargo hack test --each-feature
        working-directory: tests-build

      # Build benchmarks. Run of benchmarks is done by bench.yml workflow.
      - name: build benches
        run: cargo build --benches
        working-directory: benches
        # bench.yml workflow runs benchmarks only on linux.
        if: startsWith(matrix.os, 'ubuntu')

  test-parking_lot:
    # The parking_lot crate has a feature called send_guard which changes when
    # some of its types are Send. Tokio has some measures in place to prevent
    # this from affecting when Tokio types are Send, and this test exists to
    # ensure that those measures are working.
    #
    # This relies on the potentially affected Tokio type being listed in
    # `tokio/tokio/tests/async_send_sync.rs`.
    name: compile tests with parking lot send guards
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
            toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2
      - name: Enable parking_lot send_guard feature
        # Inserts the line "plsend = ["parking_lot/send_guard"]" right after [features]
        run: sed -i '/\[features\]/a plsend = ["parking_lot/send_guard"]' tokio/Cargo.toml
      - name: Compile tests with all features enabled
        run: cargo build --workspace --all-features --tests

  valgrind:
    name: valgrind
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
            toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2

      - name: Install Valgrind
        uses: taiki-e/install-action@valgrind

      # Compile tests
      - name: cargo build test-mem
        run: cargo build --features rt-net --bin test-mem
        working-directory: tests-integration

      # Run with valgrind
      - name: Run valgrind test-mem
        run: valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./target/debug/test-mem

      # Compile tests
      - name: cargo build test-process-signal
        run: cargo build --features rt-process-signal --bin test-process-signal
        working-directory: tests-integration

      # Run with valgrind
      - name: Run valgrind test-process-signal
        run: valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all ./target/debug/test-process-signal

  test-unstable:
    name: test tokio full --unstable
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
            toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2
      # Run `tokio` with "unstable" cfg flag.
      - name: test tokio full --cfg unstable
        run: cargo test --all-features
        working-directory: tokio
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings
          # in order to run doctests for unstable features, we must also pass
          # the unstable cfg to RustDoc
          RUSTDOCFLAGS: --cfg tokio_unstable

  miri:
    name: miri
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
          components: miri
      - uses: Swatinem/rust-cache@v2
      - name: miri
        # Many of tests in tokio/tests and doctests use #[tokio::test] or
        # #[tokio::main] that calls epoll_create1 that Miri does not support.
        run: cargo miri test --features full --lib --no-fail-fast
        working-directory: tokio
        env:
          MIRIFLAGS: -Zmiri-disable-isolation -Zmiri-strict-provenance -Zmiri-retag-fields

  asan:
    name: asan
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install llvm
        # Required to resolve symbols in sanitizer output
        run: sudo apt-get install -y llvm
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
      - uses: Swatinem/rust-cache@v2
      - name: asan
        run: cargo test --workspace --all-features --target x86_64-unknown-linux-gnu --tests -- --test-threads 1
        env:
          RUSTFLAGS: -Z sanitizer=address
          # Ignore `trybuild` errors as they are irrelevant and flaky on nightly
          TRYBUILD: overwrite

  semver:
    name: semver
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
      - name: Install cargo-semver-checks
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-semver-checks
      - name: Check semver compatibility
        run: |
          cargo semver-checks check-release --release-type minor

  cross-check:
    name: cross-check
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target:
          - powerpc-unknown-linux-gnu
          - powerpc64-unknown-linux-gnu
          - mips-unknown-linux-gnu
          - arm-linux-androideabi
          - mipsel-unknown-linux-musl
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
          target: ${{ matrix.target }}
      - name: Install cross
        uses: taiki-e/install-action@cross
      - run: cross check --workspace --all-features --target ${{ matrix.target }}
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings

  cross-test:
    name: cross-test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - target: i686-unknown-linux-gnu
          - target: arm-unknown-linux-gnueabihf
          - target: armv7-unknown-linux-gnueabihf
          - target: aarch64-unknown-linux-gnu

          # Run a platform without AtomicU64 and no const Mutex::new
          - target: arm-unknown-linux-gnueabihf
            rustflags: --cfg tokio_no_const_mutex_new
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
          target: ${{ matrix.target }}
      - name: Install cross
        uses: taiki-e/install-action@cross
      # First run with all features (including parking_lot)
      - run: cross test -p tokio --all-features --target ${{ matrix.target }} --tests
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings --cfg tokio_no_ipv6 ${{ matrix.rustflags }}
      # Now run without parking_lot
      - name: Remove `parking_lot` from `full` feature
        run: sed -i '0,/parking_lot/{/parking_lot/d;}' tokio/Cargo.toml
      # The `tokio_no_parking_lot` cfg is here to ensure the `sed` above does not silently break.
      - run: cross test -p tokio --features full,test-util --target ${{ matrix.target }} --tests
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings --cfg tokio_no_ipv6 --cfg tokio_no_parking_lot ${{ matrix.rustflags }}

  # See https://github.com/tokio-rs/tokio/issues/5187
  no-atomic-u64:
    name: Test i686-unknown-linux-gnu without AtomicU64
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
          components: rust-src
      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack
      # Install linker and libraries for i686-unknown-linux-gnu
      - uses: taiki-e/setup-cross-toolchain-action@v1
        with:
          target: i686-unknown-linux-gnu
      - run: cargo test -Zbuild-std --target target-specs/i686-unknown-linux-gnu.json -p tokio --all-features
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings --cfg tokio_no_atomic_u64
      # https://github.com/tokio-rs/tokio/pull/5356
      # https://github.com/tokio-rs/tokio/issues/5373
      - run: cargo hack build -p tokio --feature-powerset --depth 2 -Z avoid-dev-deps --keep-going
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings --cfg tokio_no_atomic_u64 --cfg tokio_no_const_mutex_new
      - run: cargo hack build -p tokio --feature-powerset --depth 2 -Z avoid-dev-deps --keep-going
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings --cfg tokio_no_atomic_u64

  features:
    name: features
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
          target: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack
      - name: check --feature-powerset
        run: cargo hack check --all --feature-powerset --depth 2 -Z avoid-dev-deps --keep-going
      # Try with unstable feature flags
      - name: check --feature-powerset --unstable
        run: cargo hack check --all --feature-powerset --depth 2 -Z avoid-dev-deps --keep-going
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings

  minrust:
    name: minrust
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_min }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_min }}
      - uses: Swatinem/rust-cache@v2
      # First compile just the main tokio crate with minrust and newest version
      # of all dependencies, then pin once_cell and compile the rest of the
      # crates with the pinned once_cell version.
      #
      # This is necessary because tokio-util transitively depends on once_cell,
      # which is not compatible with the current minrust after the 1.15.0
      # release.
      - name: "check -p tokio --all-features"
        run: cargo check -p tokio --all-features
        env:
          RUSTFLAGS: "" # remove -Dwarnings
      - name: "pin once_cell version"
        run: cargo update -p once_cell --precise 1.14.0
      - name: "check --workspace --all-features"
        run: cargo check --workspace --all-features
        env:
          RUSTFLAGS: "" # remove -Dwarnings

  minimal-versions:
    name: minimal-versions
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack
      - name: "check --all-features -Z minimal-versions"
        run: |
          # Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
          # from determining minimal versions based on dev-dependencies.
          cargo hack --remove-dev-deps --workspace
          # Update Cargo.lock to minimal version dependencies.
          cargo update -Z minimal-versions
          cargo hack check --all-features --ignore-private
      - name: "check --all-features --unstable -Z minimal-versions"
        env:
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings
        run: |
          # Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
          # from determining minimal versions based on dev-dependencies.
          cargo hack --remove-dev-deps --workspace
          # Update Cargo.lock to minimal version dependencies.
          cargo update -Z minimal-versions
          cargo hack check --all-features --ignore-private

  fmt:
    name: fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
          components: rustfmt
      - uses: Swatinem/rust-cache@v2
      # Check fmt
      - name: "rustfmt --check"
        # Workaround for rust-lang/cargo#7732
        run: |
          if ! rustfmt --check --edition 2018 $(git ls-files '*.rs'); then
            printf "Please run \`rustfmt --edition 2018 \$(git ls-files '*.rs')\` to fix rustfmt errors.\nSee CONTRIBUTING.md for more details.\n" >&2
            exit 1
          fi

  clippy:
    name: clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_clippy }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_clippy }}
          components: clippy
      - uses: Swatinem/rust-cache@v2
      # Run clippy
      - name: "clippy --all"
        run: cargo clippy --all --tests --all-features

  docs:
    name: docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
      - uses: Swatinem/rust-cache@v2
      - name: "doc --lib --all-features"
        run: cargo doc --lib --no-deps --all-features --document-private-items
        env:
          RUSTFLAGS: --cfg docsrs --cfg tokio_unstable
          RUSTDOCFLAGS: --cfg docsrs --cfg tokio_unstable -Dwarnings

  loom-compile:
    name: build loom tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2
      - name: build --cfg loom
        run: cargo test --no-run --lib --features full
        working-directory: tokio
        env:
          RUSTFLAGS: --cfg loom --cfg tokio_unstable -Dwarnings

  check-readme:
    name: Check README
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Verify that both READMEs are identical
        run: diff README.md tokio/README.md

      - name: Verify that Tokio version is up to date in README
        working-directory: tokio
        run: grep -q "$(sed '/^version = /!d' Cargo.toml | head -n1)" README.md

  test-hyper:
    name: Test hyper
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
          - macos-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2
      - name: Test hyper
        run: |
          set -x
          git clone https://github.com/hyperium/hyper.git
          cd hyper
          # checkout the latest release because HEAD maybe contains breakage.
          tag=$(git describe --abbrev=0 --tags)
          git checkout "${tag}"
          echo '[workspace]' >>Cargo.toml
          echo '[patch.crates-io]' >>Cargo.toml
          echo 'tokio = { path = "../tokio" }' >>Cargo.toml
          echo 'tokio-util = { path = "../tokio-util" }' >>Cargo.toml
          echo 'tokio-stream = { path = "../tokio-stream" }' >>Cargo.toml
          echo 'tokio-test = { path = "../tokio-test" }' >>Cargo.toml
          git diff
          cargo test --features full

  x86_64-fortanix-unknown-sgx:
    name: build tokio for x86_64-fortanix-unknown-sgx
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_nightly }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_nightly }}
          target: x86_64-fortanix-unknown-sgx
      - uses: Swatinem/rust-cache@v2
      # NOTE: Currently the only test we can run is to build tokio with rt and sync features.
      - name: build tokio
        run: cargo build --target x86_64-fortanix-unknown-sgx --features rt,sync
        working-directory: tokio

  wasm32-unknown-unknown:
    name: test tokio for wasm32-unknown-unknown
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2
      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
      - name: test tokio
        run: wasm-pack test --node -- --features "macros sync"
        working-directory: tokio

  wasm32-wasi:
    name: wasm32-wasi
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust ${{ env.rust_stable }}
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.rust_stable }}
      - uses: Swatinem/rust-cache@v2

      # Install dependencies
      - name: Install cargo-hack
        uses: taiki-e/install-action@cargo-hack

      - name: Install wasm32-wasi target
        run: rustup target add wasm32-wasi

      - name: Install wasmtime
        uses: taiki-e/install-action@wasmtime

      - name: Install cargo-wasi
        run: cargo install cargo-wasi

      - name: WASI test tokio full
        run: cargo test -p tokio --target wasm32-wasi --features full
        env:
          CARGO_TARGET_WASM32_WASI_RUNNER: "wasmtime run --"
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings

      - name: WASI test tokio-util full
        run: cargo test -p tokio-util --target wasm32-wasi --features full
        env:
          CARGO_TARGET_WASM32_WASI_RUNNER: "wasmtime run --"
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings

      - name: WASI test tokio-stream
        run: cargo test -p tokio-stream --target wasm32-wasi --features time,net,io-util,sync
        env:
          CARGO_TARGET_WASM32_WASI_RUNNER: "wasmtime run --"
          RUSTFLAGS: --cfg tokio_unstable -Dwarnings

      - name: test tests-integration --features wasi-rt
        # TODO: this should become: `cargo hack wasi test --each-feature`
        run: cargo wasi test --test rt_yield --features wasi-rt
        working-directory: tests-integration

  check-external-types:
    name: check-external-types
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - windows-latest
          - ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust nightly-2022-11-16
        uses: dtolnay/rust-toolchain@master
        with:
          # `check-external-types` requires a specific Rust nightly version. See
          # the README for details: https://github.com/awslabs/cargo-check-external-types
          toolchain: nightly-2022-11-16
      - uses: Swatinem/rust-cache@v2
      - name: check-external-types
        run: |
          set -x
          cargo install cargo-check-external-types --locked --version 0.1.6
          cargo check-external-types --all-features --config external-types.toml
        working-directory: tokio
