Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/pkg/scheduler/mutex_test.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package scheduler
6
7
import (
8
"context"
9
"sync"
10
"sync/atomic"
11
"testing"
12
"time"
13
14
"github.com/alicebob/miniredis/v2"
15
"github.com/go-redis/redis"
16
"github.com/go-redsync/redsync/v4"
17
"github.com/go-redsync/redsync/v4/redis/goredis"
18
"github.com/stretchr/testify/require"
19
)
20
21
func TestWithRefreshingMutex(t *testing.T) {
22
s := miniredis.RunT(t)
23
client := redis.NewClient(&redis.Options{Addr: s.Addr()})
24
25
pool := goredis.NewPool(client)
26
rs := redsync.New(pool)
27
28
invocationCount := int32(0)
29
errCount := int32(0)
30
okCount := int32(0)
31
32
wg := sync.WaitGroup{}
33
expiry := 2 * time.Second
34
for i := 0; i < 3; i++ {
35
wg.Add(1)
36
go func() {
37
defer wg.Done()
38
39
err := WithRefreshingMutex(context.Background(), rs, "mutex-name", expiry, func(_ context.Context) error {
40
atomic.AddInt32(&invocationCount, 1)
41
time.Sleep(expiry + 1*time.Second)
42
43
return nil
44
})
45
if err != nil {
46
atomic.AddInt32(&errCount, 1)
47
} else {
48
atomic.AddInt32(&okCount, 1)
49
}
50
51
}()
52
}
53
54
wg.Wait()
55
56
require.EqualValues(t, 1, invocationCount)
57
require.EqualValues(t, 2, errCount)
58
require.EqualValues(t, 1, okCount)
59
}
60
61