Skip to content

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

Merged
lpcox merged 1 commit intomainfrom
test-improver/improve-session-tests-7d2c7543584d1fd6
Apr 3, 2026
Merged

[test-improver] Improve tests for server session package#2989
lpcox merged 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>
@lpcox lpcox marked this pull request as ready for review April 3, 2026 04:53
Copilot AI review requested due to automatic review settings April 3, 2026 04:53
@lpcox lpcox merged commit 442641b into main Apr 3, 2026
3 checks passed
@lpcox lpcox deleted the test-improver/improve-session-tests-7d2c7543584d1fd6 branch April 3, 2026 04:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands unit test coverage for the internal/server session lifecycle by adding focused tests for previously untested UnifiedServer session helpers.

Changes:

  • Added newMinimalUnifiedServer helper to reduce setup boilerplate across session tests.
  • Added table-driven and subtest-based coverage for getSessionID, ensureSessionDirectory, requireSession (including concurrency), and getSessionKeys.
  • Introduced filesystem-isolated tests using t.TempDir() to validate per-session directory creation behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +288 to +295
const goroutines = 20
wg.Add(goroutines)
for range goroutines {
go func() {
defer wg.Done()
assert.NoError(t, us.requireSession(ctx))
}()
}
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for range goroutines does not compile because range can’t be used on an int. Use a conventional index loop (for i := 0; i < goroutines; i++ { ... }) or range over a slice/channel of length goroutines.

Copilot uses AI. Check for mistakes.
}
us, err := NewUnified(context.Background(), cfg)
require.NoError(t, err, "NewUnified should not fail with an empty config")
t.Cleanup(func() { us.Close() })
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.Cleanup(func() { us.Close() }) drops the error returned by Close(), which can hide cleanup failures. Consider asserting/require-ing that Close() returns nil inside the cleanup function (e.g., assert.NoError(t, us.Close())).

Suggested change
t.Cleanup(func() { us.Close() })
t.Cleanup(func() {
require.NoError(t, us.Close())
})

Copilot uses AI. Check for mistakes.
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.

2 participants