mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-02 13:44:16 +09:00
Backport #40125 by @silverwind A failed contributor stats generation never released its lock, so after the cached error expired, the contributors and code frequency pages kept saying stats are being generated until a restart. A generation outlasting the 5 second wait also leaked its goroutine on an unread channel. The lock is now claimed atomically and released together with the completion signal when generation ends, and a cache miss after the wait counts as still generating. Co-authored-by: silverwind <me@silverwind.io>
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"testing"
|
|
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/models/unittest"
|
|
"gitea.dev/modules/cache"
|
|
"gitea.dev/modules/setting"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRepository_ContributorsGraph(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
assert.NoError(t, repo.LoadOwner(t.Context()))
|
|
mockCache, err := cache.NewStringCache(setting.Cache{})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = GetContributorStats(t.Context(), mockCache, repo, "404ref")
|
|
assert.ErrorContains(t, err, "object does not exist")
|
|
_, locked := generateLock.Load(fmt.Sprintf(contributorStatsCacheKey, repo.FullName(), "404ref"))
|
|
assert.False(t, locked)
|
|
|
|
data, err := GetContributorStats(t.Context(), mockCache, repo, "master")
|
|
assert.NoError(t, err)
|
|
var keys []string
|
|
for k := range data {
|
|
keys = append(keys, k)
|
|
}
|
|
slices.Sort(keys)
|
|
assert.Equal(t, []string{
|
|
"ethantkoenig@gmail.com",
|
|
"jimmy.praet@telenet.be",
|
|
"jon@allspice.io",
|
|
"total", // generated summary
|
|
}, keys)
|
|
|
|
assert.Equal(t, &ContributorData{
|
|
Name: "Ethan Koenig",
|
|
AvatarLink: "/assets/img/avatar_default.png",
|
|
TotalCommits: 1,
|
|
Weeks: map[int64]*WeekData{
|
|
1511654400000: {
|
|
Week: 1511654400000, // sunday 2017-11-26
|
|
Additions: 3,
|
|
Deletions: 0,
|
|
Commits: 1,
|
|
},
|
|
},
|
|
}, data["ethantkoenig@gmail.com"])
|
|
assert.Equal(t, &ContributorData{
|
|
Name: "Total",
|
|
AvatarLink: "",
|
|
TotalCommits: 3,
|
|
Weeks: map[int64]*WeekData{
|
|
1511654400000: {
|
|
Week: 1511654400000, // sunday 2017-11-26 (2017-11-26 20:31:18 -0800)
|
|
Additions: 3,
|
|
Deletions: 0,
|
|
Commits: 1,
|
|
},
|
|
1607817600000: {
|
|
Week: 1607817600000, // sunday 2020-12-13 (2020-12-15 15:23:11 -0500)
|
|
Additions: 10,
|
|
Deletions: 0,
|
|
Commits: 1,
|
|
},
|
|
1624752000000: {
|
|
Week: 1624752000000, // sunday 2021-06-27 (2021-06-29 21:54:09 +0200)
|
|
Additions: 2,
|
|
Deletions: 0,
|
|
Commits: 1,
|
|
},
|
|
},
|
|
}, data["total"])
|
|
}
|