Files
gitea/modules/globallock/globallock_test.go
T
85211296dd test: speed up go tests (#39914)
- Skip race instrumentation of the transpiled SQLite engine
`modernc.org/sqlite/lib` when `GOTEST_FLAGS` contains `-race`, Gitea
code and the driver boundary stay instrumented. `models/issues` drops
from ~10s to ~5s.
- Hash OAuth2 client secrets with `bcrypt.MinCost` in tests, like user
passwords already use the `dummy` hasher. `TestOAuth2` drops from ~22s
to ~2s on the PostgreSQL job.
- Stop the Redis locker's extension loop directly on `Close` instead of
waiting for its next tick, and run the memory locker tests under
`testing/synctest`. `modules/globallock` drops from ~12s to ~4s.

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-10-26 11:22:44 +00:00

59 lines
1.2 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package globallock
import (
"context"
"sync"
"testing"
"testing/synctest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLockAndDo(t *testing.T) {
t.Run("redis", func(t *testing.T) {
locker := newTestRedisLocker(t)
defaultLocker.Store(new(locker))
testLockAndDo(t)
rl, ok := locker.(*redisLocker)
require.True(t, ok)
require.NoError(t, rl.Close())
})
t.Run("memory", func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
defaultLocker.Store(new(NewMemoryLocker()))
testLockAndDo(t)
})
})
}
func testLockAndDo(t *testing.T) {
const concurrency = 50
ctx := t.Context()
count := 0
wg := sync.WaitGroup{}
for range concurrency {
wg.Go(func() {
err := LockAndDo(ctx, "test", func(ctx context.Context) error {
count++
// It's impossible to acquire the lock inner the function
ok, err := TryLockAndDo(ctx, "test", func(ctx context.Context) error {
assert.Fail(t, "should not acquire the lock")
return nil
})
assert.False(t, ok)
assert.NoError(t, err)
return nil
})
assert.NoError(t, err)
})
}
wg.Wait()
assert.Equal(t, concurrency, count)
}