Welcome to mirror list, hosted at ThFree Co, Russian Federation.

counting_command_factory.go « gittest « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5943079a251cca4eb4a6145f7760a4778a23916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gittest

import (
	"context"
	"sync"
	"testing"

	"gitlab.com/gitlab-org/gitaly/v16/internal/command"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git/repository"
	"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
)

var _ git.CommandFactory = &CountingCommandFactory{}

// CountingCommandFactory embeds a regular git command factory, but it keeps
// count of each call of New*() with their git command.
type CountingCommandFactory struct {
	git.CommandFactory

	counts map[string]uint64
	m      sync.Mutex
}

// NewCountingCommandFactory creates a CountingCommandFactory
func NewCountingCommandFactory(tb testing.TB, cfg config.Cfg, opts ...git.ExecCommandFactoryOption) *CountingCommandFactory {
	return &CountingCommandFactory{
		CommandFactory: NewCommandFactory(tb, cfg, opts...),
		counts:         make(map[string]uint64),
	}
}

// CommandCount returns the current count
func (f *CountingCommandFactory) CommandCount(cmd string) uint64 {
	f.m.Lock()
	defer f.m.Unlock()

	c := f.counts[cmd]

	return c
}

// ResetCount resets all counts to zero
func (f *CountingCommandFactory) ResetCount() {
	f.m.Lock()
	defer f.m.Unlock()

	f.counts = make(map[string]uint64)
}

// New creates a new git command and increments the command counter
func (f *CountingCommandFactory) New(ctx context.Context, repo repository.GitRepo, sc git.Command, opts ...git.CmdOpt) (*command.Command, error) {
	f.m.Lock()
	defer f.m.Unlock()
	f.counts[sc.Name]++

	return f.CommandFactory.New(ctx, repo, sc, opts...)
}

// NewWithoutRepo creates a new git command and increments the command counter
func (f *CountingCommandFactory) NewWithoutRepo(ctx context.Context, sc git.Command, opts ...git.CmdOpt) (*command.Command, error) {
	f.m.Lock()
	defer f.m.Unlock()
	f.counts[sc.Name]++

	return f.CommandFactory.NewWithoutRepo(ctx, sc, opts...)
}