-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocs_test.go
More file actions
117 lines (91 loc) · 1.74 KB
/
Copy pathprocs_test.go
File metadata and controls
117 lines (91 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// MIT License
// Copyright (c) 2025 Pooyan Khanjankhani
package main
import (
"context"
"io"
"os"
"strconv"
"sync"
"testing"
"time"
)
type ProcsTest struct {
t *testing.T
count int
sleep time.Duration
}
func (this *ProcsTest) Run() {
procs := NewProcs()
this.assertLast(procs)
readerStopped := this.runReader(procs)
processStarted := this.runProcesses(procs)
loop:
for {
select {
case <-readerStopped:
this.t.Log("reader closed when process exited")
this.t.FailNow()
break loop
case _, ok := <-processStarted:
if !ok {
break loop
}
continue
}
}
procs.Shutdown()
select {
case <-readerStopped:
break
case <-time.After(time.Second * 2):
this.t.Log("reader did not close")
this.t.FailNow()
}
}
func (this *ProcsTest) runReader(procs *Procs) chan struct{} {
ret := make(chan struct{})
go func() {
defer close(ret)
r := procs.StdoutPipe()
defer r.Close()
_, err := io.Copy(os.Stdout, r)
if err != nil {
this.t.Log(err)
}
}()
return ret
}
func (this *ProcsTest) runProcesses(procs *Procs) chan struct{} {
ret := make(chan struct{})
go func() {
defer close(ret)
for i := 0; i < this.count; i++ {
ctx, cancel := context.WithCancel(this.t.Context())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ret <- struct{}{}
proc := NewProc("/usr/bin/echo", strconv.Itoa(i+1))
procs.Push(proc)
proc.Run(ctx)
}()
time.Sleep(this.sleep)
cancel()
wg.Wait()
}
}()
return ret
}
func (this *ProcsTest) assertLast(procs *Procs) {
_, err := procs.Last()
if err == nil {
this.t.Error("expected to return an error")
this.t.FailNow()
}
}
func TestProcs(t *testing.T) {
pt := ProcsTest{t, 50, time.Millisecond * 50}
pt.Run()
}