github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/auth/test_helpers_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  	"testing"
     7  )
     8  
     9  // syncBuffer is a goroutine-safe bytes.Buffer for capturing log output.
    10  type syncBuffer struct {
    11  	mu  sync.Mutex
    12  	buf bytes.Buffer
    13  }
    14  
    15  func (b *syncBuffer) Write(p []byte) (int, error) {
    16  	b.mu.Lock()
    17  	defer b.mu.Unlock()
    18  	return b.buf.Write(p)
    19  }
    20  
    21  func (b *syncBuffer) String() string {
    22  	b.mu.Lock()
    23  	defer b.mu.Unlock()
    24  	return b.buf.String()
    25  }
    26  
    27  // resetWarnOnceForTest clears the warnOnce dedupe set so a test can observe
    28  // the next warning, and restores the prior state when the test ends.
    29  func resetWarnOnceForTest(t *testing.T) {
    30  	t.Helper()
    31  	warnOnceMu.Lock()
    32  	prev := warnOnceSet
    33  	warnOnceSet = map[string]bool{}
    34  	warnOnceMu.Unlock()
    35  	t.Cleanup(func() {
    36  		warnOnceMu.Lock()
    37  		warnOnceSet = prev
    38  		warnOnceMu.Unlock()
    39  	})
    40  }