-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
64 lines (58 loc) · 1.92 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
package main
import (
"context"
"fmt"
"sync"
"testing"
"github.com/baalimago/repeater/pkg/filetools"
)
func Test_do(t *testing.T) {
t.Run("it should run command am amount of times, one workers", func(t *testing.T) {
expectedCalls := 123
testFilePath := fmt.Sprintf("%v/testFile", t.TempDir())
// Add one line per run, anticipate a certain amount of lines in the test file...
cOper := configuredOper{
am: expectedCalls,
args: []string{"/bin/bash", "-c", fmt.Sprintf("echo 'line' >> %v", testFilePath)},
amIdleWorkers: 1,
workerWg: &sync.WaitGroup{},
workPlanMu: &sync.Mutex{},
}
cOper.workerWg.Add(1)
cOper.run(context.Background())
// ... anticipate a certain amount of lines in the file when it's done
got, err := filetools.CheckAmLines(testFilePath)
if err != nil {
t.Fatalf("failed to check amount of lines: %v", err)
}
if got != expectedCalls {
t.Fatalf("expected: %v, got: %v", expectedCalls, got)
}
})
for i := 0; i < 10; i++ {
t.Run("it should run command am amount of times, 10 workers", func(t *testing.T) {
expectedCalls := 123
amWorkers := 10
testFilePath := fmt.Sprintf("%v/testFile", t.TempDir())
// Add one line per run, anticipate a certain amount of lines in the test file...
cOper := configuredOper{
am: expectedCalls,
workers: amWorkers,
amIdleWorkers: amWorkers,
args: []string{"/bin/bash", "-c", fmt.Sprintf("echo 'line' >> %v", testFilePath)},
workerWg: &sync.WaitGroup{},
workPlanMu: &sync.Mutex{},
}
cOper.workerWg.Add(amWorkers)
cOper.run(context.Background())
// ... anticipate a certain amount of lines in the file when it's done
got, err := filetools.CheckAmLines(testFilePath)
if err != nil {
t.Fatalf("failed to check amount of lines: %v", err)
}
if got != expectedCalls {
t.Fatalf("expected: %v, got: %v", expectedCalls, got)
}
})
}
}