|
| 1 | +/* |
| 2 | +Package counter provides a tiny, thread-safe counter focused on |
| 3 | +testing scenarios. It supports atomic increments/decrements, direct |
| 4 | +reads/sets, and simple wait helpers that block until the value crosses a |
| 5 | +threshold. |
| 6 | +
|
| 7 | + github.com/madflojo/testlazy/helpers/counter |
| 8 | +
|
| 9 | +# Why use it |
| 10 | +
|
| 11 | +- Minimal API designed for tests. |
| 12 | +- Safe for concurrent use by multiple goroutines. |
| 13 | +- Non-blocking "wait" helpers that return a channel for easy select/timeout. |
| 14 | +
|
| 15 | +Quick example |
| 16 | +
|
| 17 | + c := counter.New() |
| 18 | + c.Increment() |
| 19 | + c.Add(9) |
| 20 | + <-c.WaitAbove(10, time.Second) // wait until value >= 10 |
| 21 | +*/ |
| 22 | +package counter |
| 23 | + |
| 24 | +import ( |
| 25 | + "errors" |
| 26 | + "sync/atomic" |
| 27 | + "time" |
| 28 | +) |
| 29 | + |
| 30 | +// defaultPollInterval controls how often waiters check the counter value. |
| 31 | +const defaultPollInterval = 10 * time.Millisecond |
| 32 | + |
| 33 | +// ErrTimeout indicates a WaitAbove or WaitBelow call timed out before the |
| 34 | +// condition was met. |
| 35 | +var ErrTimeout = errors.New("timeout waiting for counter condition") |
| 36 | + |
| 37 | +// Counter is a thread-safe counter. |
| 38 | +// All methods are safe to call from multiple goroutines. |
| 39 | +type Counter struct { |
| 40 | + value int64 |
| 41 | +} |
| 42 | + |
| 43 | +// New creates a new Counter with initial value 0. |
| 44 | +func New() *Counter { |
| 45 | + return &Counter{} |
| 46 | +} |
| 47 | + |
| 48 | +// Increment increases the counter by 1. |
| 49 | +func (c *Counter) Increment() { |
| 50 | + atomic.AddInt64(&c.value, 1) |
| 51 | +} |
| 52 | + |
| 53 | +// Decrement decreases the counter by 1. |
| 54 | +func (c *Counter) Decrement() { |
| 55 | + atomic.AddInt64(&c.value, -1) |
| 56 | +} |
| 57 | + |
| 58 | +// Value returns the current value of the counter. |
| 59 | +func (c *Counter) Value() int64 { |
| 60 | + return atomic.LoadInt64(&c.value) |
| 61 | +} |
| 62 | + |
| 63 | +// Reset sets the counter back to 0. |
| 64 | +func (c *Counter) Reset() { |
| 65 | + atomic.StoreInt64(&c.value, 0) |
| 66 | +} |
| 67 | + |
| 68 | +// Add increases the counter by the given delta. |
| 69 | +func (c *Counter) Add(delta int64) { |
| 70 | + atomic.AddInt64(&c.value, delta) |
| 71 | +} |
| 72 | + |
| 73 | +// Subtract decreases the counter by the given delta. |
| 74 | +func (c *Counter) Subtract(delta int64) { |
| 75 | + atomic.AddInt64(&c.value, -delta) |
| 76 | +} |
| 77 | + |
| 78 | +// Set sets the counter to the given value. |
| 79 | +func (c *Counter) Set(value int64) { |
| 80 | + atomic.StoreInt64(&c.value, value) |
| 81 | +} |
| 82 | + |
| 83 | +// WaitAbove returns a channel that will receive a single error when the counter |
| 84 | +// value is >= target (inclusive) or when the timeout elapses. |
| 85 | +// |
| 86 | +// On success the error is nil. On timeout the error is ErrTimeout. |
| 87 | +func (c *Counter) WaitAbove(target int64, timeout time.Duration) <-chan error { |
| 88 | + result := make(chan error, 1) |
| 89 | + go func() { |
| 90 | + ticker := time.NewTicker(defaultPollInterval) |
| 91 | + defer ticker.Stop() |
| 92 | + timeoutCh := time.After(timeout) |
| 93 | + for { |
| 94 | + select { |
| 95 | + case <-ticker.C: |
| 96 | + if c.Value() >= target { |
| 97 | + result <- nil |
| 98 | + return |
| 99 | + } |
| 100 | + case <-timeoutCh: |
| 101 | + result <- ErrTimeout |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | + }() |
| 106 | + return result |
| 107 | +} |
| 108 | + |
| 109 | +// WaitBelow returns a channel that will receive a single error when the counter |
| 110 | +// value is <= target (inclusive) or when the timeout elapses. |
| 111 | +// |
| 112 | +// On success the error is nil. On timeout the error is ErrTimeout. |
| 113 | +func (c *Counter) WaitBelow(target int64, timeout time.Duration) <-chan error { |
| 114 | + result := make(chan error, 1) |
| 115 | + go func() { |
| 116 | + ticker := time.NewTicker(defaultPollInterval) |
| 117 | + defer ticker.Stop() |
| 118 | + timeoutCh := time.After(timeout) |
| 119 | + for { |
| 120 | + select { |
| 121 | + case <-ticker.C: |
| 122 | + if c.Value() <= target { |
| 123 | + result <- nil |
| 124 | + return |
| 125 | + } |
| 126 | + case <-timeoutCh: |
| 127 | + result <- ErrTimeout |
| 128 | + return |
| 129 | + } |
| 130 | + } |
| 131 | + }() |
| 132 | + return result |
| 133 | +} |
0 commit comments