-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_locker_test.go
145 lines (123 loc) · 3.37 KB
/
command_locker_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
141
142
143
144
145
package main
import (
"testing"
"time"
"github.com/viafintech/cronlocker/testutils"
)
func TestConsulCommandLockerLockAndExecute(t *testing.T) {
cases := []struct {
title string
runConcurrent func(locker *ConsulCommandLocker)
key string
command string
expectedOutputString string
expectedErrorString string
}{
{
title: "success",
key: "test/cron/service/job_name1",
command: "echo 1",
expectedOutputString: "1\n",
},
{
title: "cannot aquire lock",
runConcurrent: func(locker *ConsulCommandLocker) {
locker.LockAndExecute("test/cron/service/job_name2", "sleep 2")
},
key: "test/cron/service/job_name2",
command: "echo 1",
expectedOutputString: "Nothing was executed\n",
},
{
title: "command fails",
key: "test/cron/service/job_name3",
command: "false",
expectedErrorString: "exit status 1",
},
}
commandLocker, _ := NewConsulCommandLocker(
testutils.CONSULURI,
"", // Blank token
300*time.Millisecond,
time.Millisecond,
0,
false,
)
for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
if c.runConcurrent != nil {
go c.runConcurrent(commandLocker)
time.Sleep(500 * time.Millisecond)
}
outputString, err := commandLocker.LockAndExecute(
c.key,
c.command,
)
if outputString != c.expectedOutputString {
t.Errorf(
"Did not received expected output string:\n%s\n\nReceived:\n%s",
c.expectedOutputString,
outputString,
)
}
if err != nil && err.Error() != c.expectedErrorString {
t.Errorf(
"Did not received expected error:%#v, Received: %#v",
c.expectedErrorString,
err,
)
}
})
}
}
func TestConsulCommandLockerMinimumLockAndExecuteTime(t *testing.T) {
commandLocker, _ := NewConsulCommandLocker(
testutils.CONSULURI,
"", // blank token
300*time.Millisecond,
500*time.Millisecond,
0,
false,
)
startTime := time.Now()
commandLocker.LockAndExecute("test/cron/service/min_time_job", "echo 1")
if time.Since(startTime) <= 500*time.Millisecond {
t.Errorf("Locker did not wait the minimum time the lock should have been held")
}
}
func TestConsulCommandLockerMaximumExecutionTime(t *testing.T) {
commandLocker, _ := NewConsulCommandLocker(
testutils.CONSULURI,
"", // blank token
100*time.Millisecond,
300*time.Millisecond,
500*time.Millisecond,
false,
)
startTime := time.Now()
commandLocker.LockAndExecute("test/cron/service/max_time_job", "sleep 5")
if time.Since(startTime) > 700*time.Millisecond {
t.Errorf("Locker did not abort after the maximum execution time was reached")
}
}
func TestConsulCommandLockerFailOnLockWaitTimeExpired(t *testing.T) {
commandLocker, _ := NewConsulCommandLocker(
testutils.CONSULURI,
"", // blank token
300*time.Millisecond,
500*time.Millisecond,
0,
true,
)
go func() {
commandLocker.LockAndExecute("test/cron/service/lock_wait_time_job", "sleep 10")
}()
time.Sleep(100 * time.Millisecond)
output, err := commandLocker.LockAndExecute("test/cron/service/lock_wait_time_job", "sleep 2")
if output != "" {
t.Errorf("Expected no output but received: %s", output)
}
if err != errLockWaitTimeExpired {
t.Errorf("Expected to received errLockWaitTimeExpired, but received: %+v", err)
}
}