-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
240 lines (209 loc) · 4.99 KB
/
main_test.go
File metadata and controls
240 lines (209 loc) · 4.99 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"bytes"
"context"
"flag"
"io"
"os"
"strings"
"testing"
"time"
)
func TestSleepContext(t *testing.T) {
t.Run("completes after duration", func(t *testing.T) {
ctx := context.Background()
start := time.Now()
err := sleepContext(ctx, 50*time.Millisecond)
elapsed := time.Since(start)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if elapsed < 50*time.Millisecond {
t.Errorf("sleep too short: %v", elapsed)
}
})
t.Run("cancels immediately on context cancel", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
start := time.Now()
err := sleepContext(ctx, 5*time.Second)
elapsed := time.Since(start)
if err == nil {
t.Error("expected error on cancelled context")
}
if elapsed > 100*time.Millisecond {
t.Errorf("cancellation took too long: %v", elapsed)
}
})
t.Run("zero duration returns immediately", func(t *testing.T) {
ctx := context.Background()
start := time.Now()
err := sleepContext(ctx, 0)
elapsed := time.Since(start)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if elapsed > 10*time.Millisecond {
t.Errorf("zero sleep took too long: %v", elapsed)
}
})
}
func TestRunWithFlags(t *testing.T) {
tests := []struct {
name string
args []string
stdin string
wantCode int
wantOut string
}{
{
name: "help flag",
args: []string{"-h"},
wantCode: 0,
},
{
name: "version flag",
args: []string{"-v"},
wantCode: 0,
wantOut: "popstr",
},
{
name: "invalid delay",
args: []string{"-d", "notaduration"},
wantCode: 2,
},
{
name: "invalid jitter",
args: []string{"-j", "bad"},
wantCode: 2,
},
{
name: "invalid batch",
args: []string{"-n", "0"},
wantCode: 2,
},
{
name: "simple stdin",
args: []string{},
stdin: "line1\nline2\nline3\n",
wantCode: 0,
wantOut: "line1\nline2\nline3\n",
},
{
name: "batch mode",
args: []string{"-n", "2"},
stdin: "a\nb\nc\nd\n",
wantCode: 0,
wantOut: "a\nb\nc\nd\n",
},
{
name: "file not found",
args: []string{"nonexistent_file.txt"},
wantCode: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save and restore os.Args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = append([]string{"popstr"}, tt.args...)
// Save and restore stdin
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
if tt.stdin != "" {
r, w, _ := os.Pipe()
go func() {
_, _ = w.WriteString(tt.stdin)
_ = w.Close()
}()
os.Stdin = r
}
// Capture stdout
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
r, w, _ := os.Pipe()
os.Stdout = w
// Reset flag state
resetFlags()
code := run()
_ = w.Close()
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
if code != tt.wantCode {
t.Errorf("exit code = %d, want %d", code, tt.wantCode)
}
if tt.wantOut != "" && !strings.Contains(buf.String(), tt.wantOut) {
t.Errorf("output = %q, want containing %q", buf.String(), tt.wantOut)
}
})
}
}
func TestRunWithFile(t *testing.T) {
// Create temp file
f, err := os.CreateTemp("", "popstr_test_*.txt")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Remove(f.Name()) }()
content := "file line 1\nfile line 2\n"
_, _ = f.WriteString(content)
_ = f.Close()
// Save and restore os.Args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{"popstr", f.Name()}
// Capture stdout
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
r, w, _ := os.Pipe()
os.Stdout = w
resetFlags()
code := run()
_ = w.Close()
var buf bytes.Buffer
_, _ = io.Copy(&buf, r)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
if buf.String() != content {
t.Errorf("output = %q, want %q", buf.String(), content)
}
}
func TestDelayBetweenBatches(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{"popstr", "-d", "50ms", "-n", "1"}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
r, w, _ := os.Pipe()
go func() {
_, _ = w.WriteString("a\nb\nc\n")
_ = w.Close()
}()
os.Stdin = r
oldStdout := os.Stdout
defer func() { os.Stdout = oldStdout }()
outR, outW, _ := os.Pipe()
os.Stdout = outW
resetFlags()
start := time.Now()
code := run()
elapsed := time.Since(start)
_ = outW.Close()
_, _ = io.Copy(io.Discard, outR)
if code != 0 {
t.Errorf("exit code = %d, want 0", code)
}
// 3 lines with batch=1 means 2 delays of 50ms each = ~100ms minimum
if elapsed < 100*time.Millisecond {
t.Errorf("elapsed = %v, want >= 100ms (2 delays)", elapsed)
}
}
// resetFlags resets the flag package state for testing
func resetFlags() {
// Create a new flag set to reset state
oldCommandLine := flag.CommandLine
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
_ = oldCommandLine // silence unused warning
}