MCPcopy Index your code
hub / github.com/coder/coder / init

Function init

coderd/database/dbtestutil/cleaner.go:29–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27)
28
29func init() {
30 // We are hijacking the init() function here to do something very non-standard.
31 //
32 // We want to be able to run the cleaner as a subprocess of the test process so that it can outlive the test binary
33 // and still clean up, even if the test process times out or is killed. So, what we do is in startCleaner() below,
34 // which is called in the parent process, we exec our own binary and set a collision-resistant environment variable.
35 // Then here in the init(), which will run before main() and therefore before executing tests, we check for the
36 // environment variable, and if present we know this is the child process and we exec the cleaner. Instead of
37 // returning normally from init() we call os.Exit(). This prevents tests from being re-run in the child process (and
38 // recursion).
39 //
40 // If the magic value is not present, we know we are the parent process and init() returns normally.
41 magicValue := os.Getenv(envCleanerMagic)
42 if magicValue == envCleanerMagicValue {
43 RunCleaner()
44 os.Exit(0)
45 }
46}
47
48// startCleaner starts the cleaner in a subprocess. holdThis is an opaque reference that needs to be kept from being
49// garbage collected until we are done with all test databases (e.g. the end of the process).

Callers

nothing calls this directly

Calls 2

RunCleanerFunction · 0.85
ExitMethod · 0.80

Tested by

no test coverage detected