MCPcopy Index your code
hub / github.com/opencontainers/runc / testMemoryNotification

Function testMemoryNotification

libcontainer/notify_linux_test.go:16–93  ·  view source on GitHub ↗
(t *testing.T, evName string, notify notifyFunc, targ string)

Source from the content-addressed store, hash-verified

14type notifyFunc func(path string) (<-chan struct{}, error)
15
16func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ string) {
17 memoryPath := t.TempDir()
18 evFile := filepath.Join(memoryPath, evName)
19 eventPath := filepath.Join(memoryPath, "cgroup.event_control")
20 if err := os.WriteFile(evFile, []byte{}, 0o700); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(eventPath, []byte{}, 0o700); err != nil {
24 t.Fatal(err)
25 }
26 ch, err := notify(memoryPath)
27 if err != nil {
28 t.Fatal("expected no error, got:", err)
29 }
30
31 data, err := os.ReadFile(eventPath)
32 if err != nil {
33 t.Fatal("couldn't read event control file:", err)
34 }
35
36 var eventFd, evFd int
37 var arg string
38 if targ != "" {
39 _, err = fmt.Sscanf(string(data), "%d %d %s", &eventFd, &evFd, &arg)
40 } else {
41 _, err = fmt.Sscanf(string(data), "%d %d", &eventFd, &evFd)
42 }
43 if err != nil || arg != targ {
44 t.Fatalf("invalid control data %q: %s", data, err)
45 }
46
47 // dup the eventfd
48 efd, err := unix.Dup(eventFd)
49 if err != nil {
50 t.Fatal("unable to dup eventfd:", err)
51 }
52 defer unix.Close(efd)
53
54 buf := make([]byte, 8)
55 binary.LittleEndian.PutUint64(buf, 1)
56
57 if _, err := unix.Write(efd, buf); err != nil {
58 t.Fatal("unable to write to eventfd:", err)
59 }
60
61 select {
62 case <-ch:
63 case <-time.After(100 * time.Millisecond):
64 t.Fatal("no notification on channel after 100ms")
65 }
66
67 // simulate what happens when a cgroup is destroyed by cleaning up and then
68 // writing to the eventfd.
69 if err := os.RemoveAll(memoryPath); err != nil {
70 t.Fatal(err)
71 }
72 if _, err := unix.Write(efd, buf); err != nil {
73 t.Fatal("unable to write to eventfd:", err)

Callers 2

TestNotifyOnOOMFunction · 0.85
TestNotifyMemoryPressureFunction · 0.85

Calls 4

WriteMethod · 0.80
CloseMethod · 0.45
SyscallMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…