1
1
package process
2
2
3
3
import (
4
+ "context"
4
5
"os/exec"
5
6
"testing"
6
7
"time"
@@ -11,20 +12,35 @@ import (
11
12
func TestManager_Add (t * testing.T ) {
12
13
pm := Manager {Processes : make (map [int64 ]* Process )}
13
14
14
- pid := pm .Add ("foo" , exec .Command ("foo" ))
15
+ pid := pm .Add ("foo" , exec .Command ("foo" ), nil )
15
16
assert .Equal (t , int64 (1 ), pid , "expected to get pid 1 got %d" , pid )
16
17
17
- pid = pm .Add ("bar" , exec .Command ("bar" ))
18
+ pid = pm .Add ("bar" , exec .Command ("bar" ), nil )
18
19
assert .Equal (t , int64 (2 ), pid , "expected to get pid 2 got %d" , pid )
19
20
}
20
21
22
+ func TestManager_Cancel (t * testing.T ) {
23
+ pm := Manager {Processes : make (map [int64 ]* Process )}
24
+
25
+ ctx , cancel := context .WithCancel (context .Background ())
26
+ pid := pm .Add ("foo" , exec .Command ("foo" ), cancel )
27
+
28
+ pm .Cancel (pid )
29
+
30
+ select {
31
+ case <- ctx .Done ():
32
+ default :
33
+ assert .Fail (t , "Cancel should cancel the provided context" )
34
+ }
35
+ }
36
+
21
37
func TestManager_Remove (t * testing.T ) {
22
38
pm := Manager {Processes : make (map [int64 ]* Process )}
23
39
24
- pid1 := pm .Add ("foo" , exec .Command ("foo" ))
40
+ pid1 := pm .Add ("foo" , exec .Command ("foo" ), nil )
25
41
assert .Equal (t , int64 (1 ), pid1 , "expected to get pid 1 got %d" , pid1 )
26
42
27
- pid2 := pm .Add ("bar" , exec .Command ("bar" ))
43
+ pid2 := pm .Add ("bar" , exec .Command ("bar" ), nil )
28
44
assert .Equal (t , int64 (2 ), pid2 , "expected to get pid 2 got %d" , pid2 )
29
45
30
46
pm .Remove (pid2 )
0 commit comments