gitea源码

testhelper.go 809B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package queue
  4. import (
  5. "fmt"
  6. "sync"
  7. )
  8. // testStateRecorder is used to record state changes for testing, to help debug async behaviors
  9. type testStateRecorder struct {
  10. records []string
  11. mu sync.Mutex
  12. }
  13. var testRecorder = &testStateRecorder{}
  14. func (t *testStateRecorder) Record(format string, args ...any) {
  15. t.mu.Lock()
  16. t.records = append(t.records, fmt.Sprintf(format, args...))
  17. if len(t.records) > 1000 {
  18. t.records = t.records[len(t.records)-1000:]
  19. }
  20. t.mu.Unlock()
  21. }
  22. func (t *testStateRecorder) Records() []string {
  23. t.mu.Lock()
  24. r := make([]string, len(t.records))
  25. copy(r, t.records)
  26. t.mu.Unlock()
  27. return r
  28. }
  29. func (t *testStateRecorder) Reset() {
  30. t.mu.Lock()
  31. t.records = nil
  32. t.mu.Unlock()
  33. }