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

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-12-08 15:10:06 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-12-13 09:47:19 +0300
commit8b719005d03a0c6f17d5bd2a00dcf34d51d58695 (patch)
tree0dfa41b9587037a474f8c7f2eb956121dd56bd80 /internal
parent27dddad834d99e9901b4a9b137748b850e71849a (diff)
transaction: Introduce tracking manager
A lot of tests need to create a mock transaction manager to track the transactional votes and assert that we've got the votes we expected. This is quite repetitive and ultimately requires us to change a lot of function signatures if the `transaction.Manager` interface changes. Improve the situation by introducing a new `TrackingManager` which records all votes.
Diffstat (limited to 'internal')
-rw-r--r--internal/gitaly/transaction/mock.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/gitaly/transaction/mock.go b/internal/gitaly/transaction/mock.go
index 101d4b291..0d12b9f91 100644
--- a/internal/gitaly/transaction/mock.go
+++ b/internal/gitaly/transaction/mock.go
@@ -3,6 +3,7 @@ package transaction
import (
"context"
"errors"
+ "sync"
"gitlab.com/gitlab-org/gitaly/v14/internal/transaction/txinfo"
"gitlab.com/gitlab-org/gitaly/v14/internal/transaction/voting"
@@ -29,3 +30,44 @@ func (m *MockManager) Stop(ctx context.Context, tx txinfo.Transaction) error {
}
return m.StopFn(ctx, tx)
}
+
+// TrackingManager is a transaction manager which tracks all votes. Voting functions never return
+// an error.
+type TrackingManager struct {
+ MockManager
+
+ votesLock sync.Mutex
+ votes []voting.Vote
+}
+
+// NewTrackingManager creates a new TrackingManager which is ready for use.
+func NewTrackingManager() *TrackingManager {
+ manager := &TrackingManager{}
+
+ manager.VoteFn = func(_ context.Context, _ txinfo.Transaction, vote voting.Vote) error {
+ manager.votesLock.Lock()
+ defer manager.votesLock.Unlock()
+ manager.votes = append(manager.votes, vote)
+ return nil
+ }
+
+ return manager
+}
+
+// Votes returns a copy of all votes which have been cast.
+func (m *TrackingManager) Votes() []voting.Vote {
+ m.votesLock.Lock()
+ defer m.votesLock.Unlock()
+
+ votes := make([]voting.Vote, len(m.votes))
+ copy(votes, m.votes)
+
+ return votes
+}
+
+// Reset resets all votes which have been recorded up to this point.
+func (m *TrackingManager) Reset() {
+ m.votesLock.Lock()
+ defer m.votesLock.Unlock()
+ m.votes = nil
+}