Skip to content

[test-improver] Improve tests for server session package#2989

Draft
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/improve-session-tests-7d2c7543584d1fd6
Draft

[test-improver] Improve tests for server session package#2989
github-actions[bot] wants to merge 1 commit intomainfrom
test-improver/improve-session-tests-7d2c7543584d1fd6

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 1, 2026

File Analyzed

  • Test File: internal/server/session_test.go
  • Package: internal/server
  • Lines Added: 208 (137 → 345)

Improvements Made

1. Increased Coverage

Four methods in session.go had zero test coverage. This PR adds tests for all of them:

Method Before After
getSessionID ❌ Not tested ✅ 5 table-driven cases
ensureSessionDirectory ❌ Not tested ✅ 3 sub-cases
requireSession ❌ Not tested ✅ 4 sub-cases incl. concurrency
getSessionKeys ❌ Not tested ✅ 3 sub-cases

2. Better Testing Patterns

  • ✅ Added newMinimalUnifiedServer helper with t.Helper() and t.Cleanup() to reduce boilerplate across tests
  • ✅ Used require.NoError for critical setup steps, assert for non-fatal checks
  • ✅ Proper subtests via t.Run() with descriptive names
  • t.TempDir() for filesystem isolation — no leftover state between runs

3. Cleaner & More Stable Tests

  • ✅ Each subtest creates its own UnifiedServer (or resets payloadDir) so tests are fully independent
  • ✅ Concurrency test for requireSession verifies the double-checked locking ensures sessions are created exactly once under load
  • assert.Same used to verify pointer identity (not just value equality) for session reuse

Key New Tests

TestGetSessionID — confirms getSessionID is a transparent wrapper for SessionIDFromContext across absent, empty, non-string, and valid inputs.

TestEnsureSessionDirectory — confirms the function creates payloadDir/sessionID/, is idempotent, and handles multiple sessions independently.

TestRequireSession — confirms sessions are auto-created on first call, reused on subsequent calls, and that 20 concurrent goroutines all see the same single session instance.

TestGetSessionKeys — confirms an empty server returns no keys, and that keys match exactly the set of sessions created via requireSession.

Why This File?

session.go implements the session lifecycle for the gateway (creation, directory management, lookup). The existing session_test.go only tested the two pure functions (SessionIDFromContext, NewSession) and left the four UnifiedServer methods completely uncovered. These methods contain non-trivial logic: directory creation, syncutil.GetOrCreate double-checked locking, and map iteration.


Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests

Generated by Test Improver ·

Add tests for four previously-untested methods in session.go:
- getSessionID: verifies it delegates correctly to SessionIDFromContext
- ensureSessionDirectory: verifies it creates per-session dirs under payloadDir
- requireSession: verifies auto-creation, deduplication, and concurrency safety
- getSessionKeys: verifies correct enumeration of active session IDs

Introduces a newMinimalUnifiedServer helper to reduce boilerplate in
tests that need a real UnifiedServer instance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants