MCPcopy
hub / github.com/tuna/tunasync / TestCgroup

Function TestCgroup

worker/cgroup_test.go:89–328  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

87}
88
89func TestCgroup(t *testing.T) {
90 var cgcf *cgroupConfig
91 Convey("init cgroup", t, func(ctx C) {
92 _, useCurrentCgroup := os.LookupEnv("USECURCGROUP")
93 cgcf = &cgroupConfig{BasePath: "/sys/fs/cgroup", Group: "tunasync", Subsystem: "cpu"}
94 if useCurrentCgroup {
95 cgcf.Group = ""
96 }
97 err := initCgroup(cgcf)
98 So(err, ShouldBeNil)
99 if cgcf.isUnified {
100 So(cgcf.cgMgrV2, ShouldNotBeNil)
101 } else {
102 So(cgcf.cgMgrV1, ShouldNotBeNil)
103 }
104
105 Convey("Cgroup Should Work", func(ctx C) {
106 tmpDir, err := os.MkdirTemp("", "tunasync")
107 defer os.RemoveAll(tmpDir)
108 So(err, ShouldBeNil)
109 cmdScript := filepath.Join(tmpDir, "cmd.sh")
110 daemonScript := filepath.Join(tmpDir, "daemon.sh")
111 tmpFile := filepath.Join(tmpDir, "log_file")
112 bgPidfile := filepath.Join(tmpDir, "bg.pid")
113
114 c := cmdConfig{
115 name: "tuna-cgroup",
116 upstreamURL: "http://mirrors.tuna.moe/",
117 command: cmdScript + " " + daemonScript,
118 workingDir: tmpDir,
119 logDir: tmpDir,
120 logFile: tmpFile,
121 interval: 600 * time.Second,
122 env: map[string]string{
123 "BG_PIDFILE": bgPidfile,
124 },
125 }
126 cmdScriptContent := `#!/bin/bash
127redirect-std() {
128 [[ -t 0 ]] && exec </dev/null
129 [[ -t 1 ]] && exec >/dev/null
130 [[ -t 2 ]] && exec 2>/dev/null
131}
132
133# close all non-std* fds
134close-fds() {
135 eval exec {3..255}\>\&-
136}
137
138# full daemonization of external command with setsid
139daemonize() {
140 (
141 redirect-std
142 cd /
143 close-fds
144 exec setsid "$@"
145 ) &
146}

Callers

nothing calls this directly

Calls 12

initCgroupFunction · 0.85
newCmdProviderFunction · 0.85
newCgroupHookFunction · 0.85
newRsyncProviderFunction · 0.85
DebugfMethod · 0.80
AddHookMethod · 0.65
preExecMethod · 0.65
RunMethod · 0.65
TerminateMethod · 0.65
postExecMethod · 0.65
NameMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected