Path: blob/main/components/usage/pkg/scheduler/mutex_test.go
2498 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package scheduler56import (7"context"8"sync"9"sync/atomic"10"testing"11"time"1213"github.com/alicebob/miniredis/v2"14"github.com/go-redis/redis"15"github.com/go-redsync/redsync/v4"16"github.com/go-redsync/redsync/v4/redis/goredis"17"github.com/stretchr/testify/require"18)1920func TestWithRefreshingMutex(t *testing.T) {21s := miniredis.RunT(t)22client := redis.NewClient(&redis.Options{Addr: s.Addr()})2324pool := goredis.NewPool(client)25rs := redsync.New(pool)2627invocationCount := int32(0)28errCount := int32(0)29okCount := int32(0)3031wg := sync.WaitGroup{}32expiry := 2 * time.Second33for i := 0; i < 3; i++ {34wg.Add(1)35go func() {36defer wg.Done()3738err := WithRefreshingMutex(context.Background(), rs, "mutex-name", expiry, func(_ context.Context) error {39atomic.AddInt32(&invocationCount, 1)40time.Sleep(expiry + 1*time.Second)4142return nil43})44if err != nil {45atomic.AddInt32(&errCount, 1)46} else {47atomic.AddInt32(&okCount, 1)48}4950}()51}5253wg.Wait()5455require.EqualValues(t, 1, invocationCount)56require.EqualValues(t, 2, errCount)57require.EqualValues(t, 1, okCount)58}596061