Skip to content

Commit 3fabd9a

Browse files
committed
tests for watch function. Introduced a new timer mainly for testing purposes. can be turned into a feature later.
1 parent cc5a19b commit 3fabd9a

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

cmd/l0la/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ func main() {
4545
}
4646
}
4747

48+
func parseSecs() (int, error) {
49+
if len(flag.Args()) != 1 {
50+
return 0, errors.New("pid not supplied")
51+
}
52+
pid, err := strconv.Atoi(flag.Args()[0])
53+
return pid, err
54+
}
55+
4856
func parsePid() (int, error) {
4957
if len(flag.Args()) != 1 {
5058
return 0, errors.New("pid not supplied")

cmd/l0la/main_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ func TestMainFuncWithUsage(t *testing.T) {
1818
main()
1919
}
2020

21-
//infinite loop
22-
//func TestMainFuncWithPositionalArgsBeforeFlagArgs(t *testing.T) {
23-
// //first arg is command
24-
// os.Args = []string{"l0la", fmt.Sprintf("%v", os.Getpid())}
25-
// main()
26-
//}
27-
2821
func TestPrintVersion(t *testing.T) {
2922
printVersion()
3023
}

l0la.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
const Version = "v0.1.3"
1212

13-
func Watch(pid int) {
13+
func Watch(pid int, seconds ...int) {
1414
lines := make([]io.Writer, 0)
1515
l0 := uilive.New()
1616
l0.RefreshInterval = time.Hour * 24 * 30
@@ -25,23 +25,35 @@ func Watch(pid int) {
2525
pad := 26
2626
s := NewSpinnerAnim()
2727

28+
timer := time.NewTimer(time.Hour * 24)
29+
if len(seconds) > 0 && seconds[0] >= 1 {
30+
timer = time.NewTimer(time.Second * time.Duration(seconds[0]))
31+
}
32+
2833
if PidgrepActive(pid) {
34+
Watch:
2935
for {
30-
fmt.Fprint(lines[0], Logo()+"\n")
31-
fmt.Fprintf(lines[0], "%s watching pid: %v %s \n", s.Next(), pid, Pidgrepstatus(pid))
32-
fmt.Fprintf(lines[0], "┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n")
33-
fmt.Fprintf(lines[0], "┃ TCP conns ┃ %s ┃\n", Red(LPad(pad, FGroup(Conngrep(pid)))))
34-
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
35-
fmt.Fprintf(lines[0], "┃ Open files ┃ %s ┃\n", Yellow(LPad(pad, FGroup(Filegrep(pid)))))
36-
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
37-
fmt.Fprintf(lines[0], "┃ Threads ┃ %s ┃\n", Blue(LPad(pad, FGroup(Threadgrep(pid)))))
38-
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
39-
fmt.Fprintf(lines[0], "┃ RSS bytes ┃ %s ┃\n", Gray(8, LPad(pad, FGroup(Memgrep(pid)))))
40-
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
41-
fmt.Fprintf(lines[0], "┃ CPU percent ┃ %s ┃\n", Magenta(LPad(pad, Cpugrep(pid))))
42-
fmt.Fprintf(lines[0], "┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n")
43-
l0.Flush()
44-
time.Sleep(time.Millisecond * 500)
36+
select {
37+
case <-timer.C:
38+
break Watch
39+
default:
40+
41+
fmt.Fprint(lines[0], Logo()+"\n")
42+
fmt.Fprintf(lines[0], "%s watching pid: %v %s \n", s.Next(), pid, Pidgrepstatus(pid))
43+
fmt.Fprintf(lines[0], "┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n")
44+
fmt.Fprintf(lines[0], "┃ TCP conns ┃ %s ┃\n", Red(LPad(pad, FGroup(Conngrep(pid)))))
45+
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
46+
fmt.Fprintf(lines[0], "┃ Open files ┃ %s ┃\n", Yellow(LPad(pad, FGroup(Filegrep(pid)))))
47+
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
48+
fmt.Fprintf(lines[0], "┃ Threads ┃ %s ┃\n", Blue(LPad(pad, FGroup(Threadgrep(pid)))))
49+
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
50+
fmt.Fprintf(lines[0], "┃ RSS bytes ┃ %s ┃\n", Gray(8, LPad(pad, FGroup(Memgrep(pid)))))
51+
fmt.Fprintf(lines[0], "┣━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n")
52+
fmt.Fprintf(lines[0], "┃ CPU percent ┃ %s ┃\n", Magenta(LPad(pad, Cpugrep(pid))))
53+
fmt.Fprintf(lines[0], "┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n")
54+
l0.Flush()
55+
time.Sleep(time.Millisecond * 500)
56+
}
4557
}
4658
} else {
4759
fmt.Printf(Logo())

l0la_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package l0la
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestWatch(t *testing.T) {
9+
Watch(os.Getpid(), 4)
10+
}

0 commit comments

Comments
 (0)