|
| 1 | +package animation |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func resetGlobalCoordinator(t *testing.T) { |
| 14 | + t.Helper() |
| 15 | + globalCoordinator.mu.Lock() |
| 16 | + globalCoordinator.active = 0 |
| 17 | + globalCoordinator.frame = 0 |
| 18 | + globalCoordinator.mu.Unlock() |
| 19 | +} |
| 20 | + |
| 21 | +func getActiveCount() int32 { |
| 22 | + globalCoordinator.mu.Lock() |
| 23 | + defer globalCoordinator.mu.Unlock() |
| 24 | + return globalCoordinator.active |
| 25 | +} |
| 26 | + |
| 27 | +func runCmdWithTimeout(t *testing.T, cmd tea.Cmd) tea.Msg { |
| 28 | + t.Helper() |
| 29 | + require.NotNil(t, cmd) |
| 30 | + |
| 31 | + done := make(chan tea.Msg, 1) |
| 32 | + go func() { |
| 33 | + done <- cmd() |
| 34 | + }() |
| 35 | + |
| 36 | + timeout := time.NewTimer(250 * time.Millisecond) |
| 37 | + defer timeout.Stop() |
| 38 | + |
| 39 | + select { |
| 40 | + case msg := <-done: |
| 41 | + return msg |
| 42 | + case <-timeout.C: |
| 43 | + t.Fatal("timed out waiting for tick command") |
| 44 | + } |
| 45 | + |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +func runTickCmd(t *testing.T, cmd tea.Cmd) TickMsg { |
| 50 | + t.Helper() |
| 51 | + |
| 52 | + msg := runCmdWithTimeout(t, cmd) |
| 53 | + tickMsg, ok := msg.(TickMsg) |
| 54 | + require.True(t, ok) |
| 55 | + |
| 56 | + return tickMsg |
| 57 | +} |
| 58 | + |
| 59 | +func TestGlobalCoordinatorLifecycle(t *testing.T) { |
| 60 | + resetGlobalCoordinator(t) |
| 61 | + |
| 62 | + // No active animations = no tick |
| 63 | + require.Nil(t, StartTick()) |
| 64 | + |
| 65 | + // First registration starts tick |
| 66 | + firstTick := StartTickIfFirst() |
| 67 | + tickMsg := runTickCmd(t, firstTick) |
| 68 | + assert.Equal(t, 1, tickMsg.Frame) |
| 69 | + |
| 70 | + // Subsequent tick continues |
| 71 | + nextTick := StartTick() |
| 72 | + tickMsg = runTickCmd(t, nextTick) |
| 73 | + assert.Equal(t, 2, tickMsg.Frame) |
| 74 | + |
| 75 | + // Second StartTickIfFirst registers but doesn't return tick (not first) |
| 76 | + cmd := StartTickIfFirst() |
| 77 | + require.Nil(t, cmd) |
| 78 | + assert.Equal(t, int32(2), getActiveCount()) |
| 79 | + |
| 80 | + // Unregister one, still active |
| 81 | + Unregister() |
| 82 | + require.True(t, HasActive()) |
| 83 | + require.NotNil(t, StartTick()) |
| 84 | + |
| 85 | + // Unregister last one |
| 86 | + Unregister() |
| 87 | + require.False(t, HasActive()) |
| 88 | + require.Nil(t, StartTick()) |
| 89 | +} |
| 90 | + |
| 91 | +func TestUnregisterNeverGoesNegative(t *testing.T) { |
| 92 | + resetGlobalCoordinator(t) |
| 93 | + |
| 94 | + // Multiple unregisters when already at 0 |
| 95 | + Unregister() |
| 96 | + Unregister() |
| 97 | + Unregister() |
| 98 | + |
| 99 | + assert.Equal(t, int32(0), getActiveCount()) |
| 100 | + require.False(t, HasActive()) |
| 101 | +} |
| 102 | + |
| 103 | +func TestConcurrentRegisterUnregister(t *testing.T) { |
| 104 | + resetGlobalCoordinator(t) |
| 105 | + |
| 106 | + const goroutines = 100 |
| 107 | + const opsPerGoroutine = 100 |
| 108 | + |
| 109 | + var wg sync.WaitGroup |
| 110 | + wg.Add(goroutines * 2) |
| 111 | + |
| 112 | + // Half goroutines do register |
| 113 | + for range goroutines { |
| 114 | + go func() { |
| 115 | + defer wg.Done() |
| 116 | + for range opsPerGoroutine { |
| 117 | + Register() |
| 118 | + } |
| 119 | + }() |
| 120 | + } |
| 121 | + |
| 122 | + // Half goroutines do unregister |
| 123 | + for range goroutines { |
| 124 | + go func() { |
| 125 | + defer wg.Done() |
| 126 | + for range opsPerGoroutine { |
| 127 | + Unregister() |
| 128 | + } |
| 129 | + }() |
| 130 | + } |
| 131 | + |
| 132 | + wg.Wait() |
| 133 | + |
| 134 | + // Should have exactly goroutines * opsPerGoroutine registers |
| 135 | + // minus whatever unregisters succeeded (capped at 0) |
| 136 | + // Final count should be >= 0 |
| 137 | + count := getActiveCount() |
| 138 | + assert.GreaterOrEqual(t, count, int32(0), "active count should never be negative") |
| 139 | +} |
| 140 | + |
| 141 | +func TestConcurrentStartTickIfFirst(t *testing.T) { |
| 142 | + resetGlobalCoordinator(t) |
| 143 | + |
| 144 | + const goroutines = 50 |
| 145 | + var wg sync.WaitGroup |
| 146 | + wg.Add(goroutines) |
| 147 | + |
| 148 | + cmds := make(chan tea.Cmd, goroutines) |
| 149 | + |
| 150 | + // Many goroutines race to be "first" |
| 151 | + for range goroutines { |
| 152 | + go func() { |
| 153 | + defer wg.Done() |
| 154 | + cmd := StartTickIfFirst() |
| 155 | + cmds <- cmd |
| 156 | + }() |
| 157 | + } |
| 158 | + |
| 159 | + wg.Wait() |
| 160 | + close(cmds) |
| 161 | + |
| 162 | + // Count non-nil commands (ticks started) |
| 163 | + ticksStarted := 0 |
| 164 | + for cmd := range cmds { |
| 165 | + if cmd != nil { |
| 166 | + ticksStarted++ |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Exactly one should have started the tick |
| 171 | + assert.Equal(t, 1, ticksStarted, "exactly one goroutine should start the tick") |
| 172 | + // All should have registered |
| 173 | + assert.Equal(t, int32(goroutines), getActiveCount()) |
| 174 | +} |
0 commit comments