Files
gitea/modules/process/manager_test.go
T
silverwind 8abda62312 test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are
around 5% faster while being 7% smaller. No non-test code is touched.
2026-10-24 13:48:19 +00:00

83 lines
2.3 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package process
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetManager(t *testing.T) {
go func() {
// test race protection
_ = GetManager()
}()
pm := GetManager()
assert.NotNil(t, pm)
}
func TestManager_AddContext(t *testing.T) {
pm := Manager{processMap: make(map[IDType]*process), next: 1}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
p1Ctx, _, finished := pm.AddContext(ctx, "foo")
defer finished()
assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to get non-empty pid")
p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar")
defer finished()
assert.NotEmpty(t, GetContext(p2Ctx).GetPID(), "expected to get non-empty pid")
assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID())
assert.Equal(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID(), "expected to get pid %s got %s", GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID())
}
func TestManager_Cancel(t *testing.T) {
pm := Manager{processMap: make(map[IDType]*process), next: 1}
for _, managerCancel := range []bool{true, false} {
ctx, cancel, finished := pm.AddContext(t.Context(), "foo")
defer finished()
if managerCancel {
pm.Cancel(GetPID(ctx))
} else {
cancel()
}
select {
case <-ctx.Done():
default:
assert.FailNow(t, "Cancel should cancel the provided context")
}
finished()
}
}
func TestManager_Remove(t *testing.T) {
pm := Manager{processMap: make(map[IDType]*process), next: 1}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
p1Ctx, _, finished := pm.AddContext(ctx, "foo")
defer finished()
assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to have non-empty PID")
p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar")
defer finished()
assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids got %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID())
finished()
_, exists := pm.processMap[GetPID(p2Ctx)]
assert.False(t, exists, "PID %d is in the list but shouldn't", GetPID(p2Ctx))
}