This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
140 lines (120 loc) · 2.72 KB
/
main_test.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"testing"
"time"
)
const pkg = "qvl.io/sleepto"
var tmpbin string
// Provide temporary binary
func TestMain(m *testing.M) {
tmpbin = filepath.Join(os.TempDir(), "sleepto-"+strconv.FormatInt(time.Now().Unix(), 10))
cmd := exec.Command("go", "build", "-o", tmpbin, pkg)
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
code := m.Run()
if err := os.Remove(tmpbin); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(code)
}
func TestEcho(t *testing.T) {
done := make(chan struct{})
// Run binary
go func() {
now := time.Now()
s := now.Second()
want := "hello test"
cmd := exec.Command(tmpbin, "-second", fmt.Sprintf("%d,%d", (s+3)%60, (s+50)%60), "echo", want)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
t.Error(err)
}
close(done)
equal(t, want+"\n", string(out), "stdout")
want = fmt.Sprintf("sleeping until: %s\n", now.Add(3*time.Second).Format(time.RFC1123))
equal(t, want, stderr.String(), "stderr")
}()
if err := timing(done, 2, 3); err != nil {
t.Error(err)
}
}
func TestInvalid(t *testing.T) {
done := make(chan struct{})
// Run binary
go func() {
now := time.Now()
y := strconv.Itoa(now.Year())
cmd := exec.Command(tmpbin, "-year", y)
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
equal(t, "exit status 1", err.Error(), "exit code")
equal(t, "", string(out), "stdout")
equal(t, "year must be > current year ("+y+")", stderr.String(), "stderr")
close(done)
}()
if err := timing(done, 0, 1); err != nil {
t.Error(err)
}
}
func TestAlarm(t *testing.T) {
done := make(chan struct{})
m := strconv.Itoa(int(time.Now().Month()))
want := "hello alarm"
cmd := exec.Command(tmpbin, "-month", m, "-silent", "echo", want)
// Run binary
go func() {
var stderr bytes.Buffer
cmd.Stderr = &stderr
out, err := cmd.Output()
if err != nil {
t.Error(err)
}
close(done)
equal(t, want+"\n", string(out), "stdout")
equal(t, "", stderr.String(), "stderr")
}()
go func() {
time.Sleep(2 * time.Second)
if err := cmd.Process.Signal(syscall.SIGALRM); err != nil {
t.Error(err)
}
}()
if err := timing(done, 2, 3); err != nil {
t.Error(err)
}
}
func equal(t *testing.T, want, got, msg string) {
if want != got {
t.Errorf(`%s
Expected: %s
Got: %s
`, msg, want, got)
}
}
func timing(done chan struct{}, min, max time.Duration) error {
select {
case <-done:
return errors.New("too quick")
case <-time.After(min * time.Second):
}
select {
case <-time.After((max - min) * time.Second):
return errors.New("too slow")
case <-done:
}
return nil
}