mirror of
https://github.com/gitpod-io/gitpod.git
synced 2025-12-08 17:36:30 +00:00
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
// 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)
|
|
}
|