## Testing

The tests under this component require an etcd endpoint setup.

#### 1. Start a minimal TiDB Playground where PD plays a role as etcd endpoint

Before running test under this component, start a minimal PD + TiKV cluster (`tikv-slim`). Most test cases use PD address 127.0.0.1:2379 by default.

**Must run in background (do not omit `&`)**:

```bash
tiup playground --mode tikv-slim &
```

(Optional) Use `--tag` to distinguish different playgrounds:

```bash
tiup playground --mode tikv-slim --tag tici_discovery &
```

**Note:** Using `--tag` will keep the data dir after exit. Remember to remove `${HOME}/.tiup/data/<tag>` in the cleanup step.

If `127.0.0.1:2379` is not available (for example: shared dev machine, port conflict, or multiple playgrounds on one host), you can change the PD address:
- Use `--pd.port` to set PD port explicitly
- Or use `--port-offset` to shift all default ports

Examples:

```bash
tiup playground --mode tikv-slim --pd.port 12379 &
# or
tiup playground --mode tikv-slim --port-offset 10000 &
```

#### (Optional) Verify cluster is ready

Recommended to avoid flaky failures caused by PD/TiKV not ready yet:

```bash
PD_ADDR=127.0.0.1:2379
curl -f "http://${PD_ADDR}/pd/api/v1/version"
until curl -sf "http://${PD_ADDR}/pd/api/v1/version" >/dev/null; do sleep 1; done
```

#### 2. Run Tests

```bash
ETCD_ADDR=127.0.0.1:2379 cargo test --package tici_service_discovery --lib -- <TestName>
```

#### 3. Cleanup

**Required**: If you started TiUP Playground, you must clean up after tests.

```bash
# 1) Stop playground (will also stop pd/tikv)
pkill -f "tiup playground" || true

# 2) If you used `--tag <tag>`, remove its data dir (TiUP will not auto-clean it)
#    Example: rm -rf "${HOME}/.tiup/data/tici_discovery"
rm -rf "${HOME}/.tiup/data/<tag>"

# 3) (Optional) Clean component data directories
tiup clean --all
```

Verify stopped (should fail to connect):

```bash
PD_ADDR=127.0.0.1:2379
curl -f "http://${PD_ADDR}/pd/api/v1/version"
```

## Definition of Done (DoD)
You MUST NOT mark a task as complete or ask for review until:
1. The code compiles without errors (`make format && make check`).
2. All `tici_service_discovery` tests pass (see "Run Tests" above; do not use `make ci-ut` for this module's verification loop).

## The Verification Loop (tici_service_discovery only)
When code changes are limited to `.rs` files under `components/tici_service_discovery/`, follow this loop instead of the repo-level one:

1. **Syntax Check (Fast Fail)**:
   - Run: `make format && make check`
   - If it fails: Read the compiler error, fix the code, and **restart Step 1**.
2. **Logic Verification**:
   - Run the component tests: `ETCD_ADDR=127.0.0.1:2379 cargo test --package tici_service_discovery --lib`
   - If it fails: Analyze the panic/assertion failure, fix the logic, and **go back to Step 1**.
3. **Completion**:
   - ONLY when the component tests output "test result: ok", present the code for review.
