code.gitea.io/gitea@v1.22.3/modules/queue/testhelper.go (about)

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