Skip to content

feat: improve token validation with exponential backoff and add unit tests#272

Open
travellingsoldier85 wants to merge 1 commit intoentrius:testfrom
travellingsoldier85:improve-token-mgmt-and-tests
Open

feat: improve token validation with exponential backoff and add unit tests#272
travellingsoldier85 wants to merge 1 commit intoentrius:testfrom
travellingsoldier85:improve-token-mgmt-and-tests

Conversation

@travellingsoldier85
Copy link

@travellingsoldier85 travellingsoldier85 commented Mar 7, 2026

…tests

  • Refactor token_mgmt.py with exponential backoff retry logic
  • Add validate_token() returning diagnostic info (username, rate limits)
  • Handle specific exceptions (Timeout, ConnectionError) instead of bare Exception
  • Add rate limit monitoring with low-remaining warnings
  • Add 12 unit tests for token_mgmt module
  • Add 13 unit tests for utils module (mask_secret, parse_repo_name)
  • All existing tests continue to pass

Summary

Improves miner token management reliability and adds comprehensive unit test coverage for two previously untested modules.

Changes

gittensor/miner/token_mgmt.py — Improved token validation:

  • Added validate_token() function that returns diagnostic info (authenticated username, rate limit status)
  • Replaced fixed 3s retry sleep with exponential backoff (2s → 4s → 8s)
  • Handle specific exceptions (Timeout, ConnectionError, RequestException) instead of bare Exception
  • Immediate failure on HTTP 401 (no wasteful retries for revoked tokens)
  • Rate limit detection: warns when API calls are running low, reports reset time on 403
  • is_token_valid() preserved as backward-compatible convenience wrapper

tests/miner/test_token_mgmt.py — 12 new tests:

  • Valid/invalid/rate-limited token scenarios
  • Retry behavior on timeouts and connection errors
  • Exponential backoff timing verification
  • No-retry on definitive 401 failures

tests/utils/test_utils.py — 13 new tests:

  • mask_secret(): format, length, determinism, edge cases
  • parse_repo_name(): parsing, case normalization, error handling

Testing

All 356 tests pass (331 existing + 25 new).

Related Issues

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Other (describe below)

Testing

  • Tests added/updated
  • Manually tested

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Changes are documented (if applicable)

…tests

- Refactor token_mgmt.py with exponential backoff retry logic
- Add validate_token() returning diagnostic info (username, rate limits)
- Handle specific exceptions (Timeout, ConnectionError) instead of bare Exception
- Add rate limit monitoring with low-remaining warnings
- Add 12 unit tests for token_mgmt module
- Add 13 unit tests for utils module (mask_secret, parse_repo_name)
- All existing tests continue to pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant