MCPcopy
hub / github.com/etcd-io/bbolt / doPowerFailure

Function doPowerFailure

tests/robustness/powerfailure_test.go:142–214  ·  view source on GitHub ↗
(t *testing.T, du time.Duration, fsType dmflakey.FSType, mkfsOpt string, fsMountOpt string, useFailpoint bool)

Source from the content-addressed store, hash-verified

140}
141
142func doPowerFailure(t *testing.T, du time.Duration, fsType dmflakey.FSType, mkfsOpt string, fsMountOpt string, useFailpoint bool) {
143 flakey := initFlakeyDevice(t, strings.ReplaceAll(t.Name(), "/", "_"), fsType, mkfsOpt, fsMountOpt)
144 root := flakey.RootFS()
145
146 dbPath := filepath.Join(root, "boltdb")
147
148 args := []string{"bbolt", "bench",
149 "--work", // keep the database
150 "--path", dbPath,
151 "--count=1000000000",
152 "--batch-size=5", // separate total count into multiple truncation
153 "--value-size=512",
154 }
155
156 logPath := filepath.Join(t.TempDir(), fmt.Sprintf("%s.log", t.Name()))
157 require.NoError(t, os.MkdirAll(path.Dir(logPath), 0600))
158
159 logFd, err := os.Create(logPath)
160 require.NoError(t, err)
161 defer logFd.Close()
162
163 fpURL := "127.0.0.1:12345"
164
165 cmd := exec.Command(args[0], args[1:]...)
166 cmd.Stdout = logFd
167 cmd.Stderr = logFd
168 cmd.Env = append(cmd.Env, "GOFAIL_HTTP="+fpURL)
169 t.Logf("start %s", strings.Join(args, " "))
170 require.NoError(t, cmd.Start(), "args: %v", args)
171
172 errCh := make(chan error, 1)
173 go func() {
174 errCh <- cmd.Wait()
175 }()
176
177 defer func() {
178 if t.Failed() {
179 logData, err := os.ReadFile(logPath)
180 assert.NoError(t, err)
181 t.Logf("dump log:\n: %s", string(logData))
182 }
183 }()
184
185 time.Sleep(du)
186 t.Logf("simulate power failure")
187
188 if useFailpoint {
189 fpURL = "http://" + fpURL
190 targetFp := panicFailpoints[randomInt(t, math.MaxInt32)%len(panicFailpoints)]
191 t.Logf("random pick failpoint: %s", targetFp)
192 activeFailpoint(t, fpURL, targetFp, "panic")
193 } else {
194 t.Log("kill bbolt")
195 assert.NoError(t, cmd.Process.Kill())
196 }
197
198 select {
199 case <-time.After(10 * time.Second):

Callers 2

Calls 8

initFlakeyDeviceFunction · 0.85
randomIntFunction · 0.85
activeFailpointFunction · 0.85
SizeMethod · 0.80
RootFSMethod · 0.65
ErrorMethod · 0.65
PowerFailureMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected