@@ -13,24 +13,25 @@ import (
1313 "time"
1414)
1515
16- func TestNotifyOnOOM (t * testing.T ) {
17- memoryPath , err := ioutil .TempDir ("" , "testnotifyoom-" )
16+ type notifyFunc func (paths map [string ]string ) (<- chan struct {}, error )
17+
18+ func testMemoryNotification (t * testing.T , evName string , notify notifyFunc , targ string ) {
19+ memoryPath , err := ioutil .TempDir ("" , "testmemnotification-" + evName )
1820 if err != nil {
1921 t .Fatal (err )
2022 }
21- oomPath := filepath .Join (memoryPath , "memory.oom_control" )
23+ evFile := filepath .Join (memoryPath , evName )
2224 eventPath := filepath .Join (memoryPath , "cgroup.event_control" )
23- if err := ioutil .WriteFile (oomPath , []byte {}, 0700 ); err != nil {
25+ if err := ioutil .WriteFile (evFile , []byte {}, 0700 ); err != nil {
2426 t .Fatal (err )
2527 }
2628 if err := ioutil .WriteFile (eventPath , []byte {}, 0700 ); err != nil {
2729 t .Fatal (err )
2830 }
29- var eventFd , oomControlFd int
3031 paths := map [string ]string {
3132 "memory" : memoryPath ,
3233 }
33- ooms , err := notifyOnOOM (paths )
34+ ch , err := notify (paths )
3435 if err != nil {
3536 t .Fatal ("expected no error, got:" , err )
3637 }
@@ -40,7 +41,14 @@ func TestNotifyOnOOM(t *testing.T) {
4041 t .Fatal ("couldn't read event control file:" , err )
4142 }
4243
43- if _ , err := fmt .Sscanf (string (data ), "%d %d" , & eventFd , & oomControlFd ); err != nil {
44+ var eventFd , evFd int
45+ var arg string
46+ if targ != "" {
47+ _ , err = fmt .Sscanf (string (data ), "%d %d %s" , & eventFd , & evFd , & arg )
48+ } else {
49+ _ , err = fmt .Sscanf (string (data ), "%d %d" , & eventFd , & evFd )
50+ }
51+ if err != nil || arg != targ {
4452 t .Fatalf ("invalid control data %q: %s" , data , err )
4553 }
4654
@@ -63,9 +71,9 @@ func TestNotifyOnOOM(t *testing.T) {
6371 }
6472
6573 select {
66- case <- ooms :
74+ case <- ch :
6775 case <- time .After (100 * time .Millisecond ):
68- t .Fatal ("no notification on oom channel after 100ms" )
76+ t .Fatal ("no notification on channel after 100ms" )
6977 }
7078
7179 // simulate what happens when a cgroup is destroyed by cleaning up and then
@@ -79,18 +87,42 @@ func TestNotifyOnOOM(t *testing.T) {
7987
8088 // give things a moment to shut down
8189 select {
82- case _ , ok := <- ooms :
90+ case _ , ok := <- ch :
8391 if ok {
84- t .Fatal ("expected no oom to be triggered" )
92+ t .Fatal ("expected no notification to be triggered" )
8593 }
8694 case <- time .After (100 * time .Millisecond ):
8795 }
8896
89- if _ , _ , err := syscall .Syscall (syscall .SYS_FCNTL , uintptr (oomControlFd ), syscall .F_GETFD , 0 ); err != syscall .EBADF {
90- t .Error ("expected oom control to be closed" )
97+ if _ , _ , err := syscall .Syscall (syscall .SYS_FCNTL , uintptr (evFd ), syscall .F_GETFD , 0 ); err != syscall .EBADF {
98+ t .Error ("expected event control to be closed" )
9199 }
92100
93101 if _ , _ , err := syscall .Syscall (syscall .SYS_FCNTL , uintptr (eventFd ), syscall .F_GETFD , 0 ); err != syscall .EBADF {
94102 t .Error ("expected event fd to be closed" )
95103 }
96104}
105+
106+ func TestNotifyOnOOM (t * testing.T ) {
107+ f := func (paths map [string ]string ) (<- chan struct {}, error ) {
108+ return notifyOnOOM (paths )
109+ }
110+
111+ testMemoryNotification (t , "memory.oom_control" , f , "" )
112+ }
113+
114+ func TestNotifyMemoryPressure (t * testing.T ) {
115+ tests := map [PressureLevel ]string {
116+ LowPressure : "low" ,
117+ MediumPressure : "medium" ,
118+ CriticalPressure : "critical" ,
119+ }
120+
121+ for level , arg := range tests {
122+ f := func (paths map [string ]string ) (<- chan struct {}, error ) {
123+ return notifyMemoryPressure (paths , level )
124+ }
125+
126+ testMemoryNotification (t , "memory.pressure_level" , f , arg )
127+ }
128+ }
0 commit comments