-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathjob_test.go
48 lines (40 loc) · 1 KB
/
job_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
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package scheduler
import (
"github.com/stretchr/testify/require"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestPreventConcurrentInvocation(t *testing.T) {
callCount := int32(0)
job := WithoutConcurrentRun(JobFunc(func() error {
atomic.AddInt32(&callCount, 1)
time.Sleep(50 * time.Millisecond)
return nil
}))
invocations := 3
wg := sync.WaitGroup{}
wg.Add(invocations)
for i := 0; i < invocations; i++ {
go func() {
_ = job.Run()
wg.Done()
}()
}
wg.Wait()
require.Equal(t, int32(1), callCount)
}
func TestPreventConcurrentInvocation_CanRunRepeatedly(t *testing.T) {
callCount := int32(0)
job := WithoutConcurrentRun(JobFunc(func() error {
atomic.AddInt32(&callCount, 1)
return nil
}))
require.NoError(t, job.Run())
require.NoError(t, job.Run())
require.Equal(t, int32(2), callCount)
}