mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-08 15:57:42 +00:00
ec869e3052
- The pub registry reported the oldest version as `latest`, because the descriptor slice is sorted ascending but the first element was used. - Verifying a GPG or SSH key flashed success and redirected after already writing an error response, so a failure was reported as a success with an empty key id. - Test packages sharing redis could tear down each other's server. `PrepareTestRedis` started its own on the well-known port, so a package running in parallel borrowed it and lost it when the owner's cleanup fired. It now listens on a socket of its own.
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package globallock
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dev/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLockAndDo(t *testing.T) {
|
|
t.Run("redis", func(t *testing.T) {
|
|
defer test.MockVariableValue(&redisLockExpiry, 5*time.Second)() // Close waits for the extend goroutine's next tick
|
|
locker := newTestRedisLocker(t)
|
|
defaultLocker.Store(new(locker))
|
|
testLockAndDo(t)
|
|
require.NoError(t, locker.(*redisLocker).Close())
|
|
})
|
|
t.Run("memory", 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)
|
|
}
|