fix: fix http cache dir creation order in coderdtest (#17303)

fixes coder/internal#565

Fixes the ordering of creating the HTTP cache temp dir with respect to
starting the Coderd HTTP server, so that they are cleaned up in the
correct (reverse) order.
This commit is contained in:
Spike Curtis
2025-04-09 15:19:48 +04:00
committed by GitHub
parent 43b1a034b1
commit 3f3e2017bd

View File

@ -405,6 +405,12 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
workspacestats.TrackerWithTickFlush(options.WorkspaceUsageTrackerTick, options.WorkspaceUsageTrackerFlush),
)
// create the TempDir for the HTTP file cache BEFORE we start the server and set a t.Cleanup to close it. TempDir()
// registers a Cleanup function that deletes the directory, and Cleanup functions are called in reverse order. If
// we don't do this, then we could try to delete the directory before the HTTP server is done with all files in it,
// which on Windows will fail (can't delete files until all programs have closed handles to them).
cacheDir := t.TempDir()
var mutex sync.RWMutex
var handler http.Handler
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -515,7 +521,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can
AppHostname: options.AppHostname,
AppHostnameRegex: appHostnameRegex,
Logger: *options.Logger,
CacheDir: t.TempDir(),
CacheDir: cacheDir,
RuntimeConfig: runtimeManager,
Database: options.Database,
Pubsub: options.Pubsub,