(t *testing.T)
| 203 | } |
| 204 | |
| 205 | func TestSystem_ConcurrentMessages(t *testing.T) { |
| 206 | // 使用更大的邮箱配置 |
| 207 | config := DefaultSystemConfig() |
| 208 | config.MailboxSize = 50000 |
| 209 | config.DefaultActorMailboxSize = 20000 |
| 210 | system := NewSystemWithConfig("test", config) |
| 211 | defer system.Shutdown() |
| 212 | |
| 213 | counter := &CounterActor{} |
| 214 | props := &Props{ |
| 215 | Name: "counter", |
| 216 | MailboxSize: 20000, |
| 217 | } |
| 218 | pid := system.SpawnWithProps(counter, props) |
| 219 | |
| 220 | // 并发发送消息 |
| 221 | var wg sync.WaitGroup |
| 222 | numGoroutines := 100 |
| 223 | messagesPerGoroutine := 100 |
| 224 | |
| 225 | for range numGoroutines { |
| 226 | wg.Add(1) |
| 227 | go func() { |
| 228 | defer wg.Done() |
| 229 | for range messagesPerGoroutine { |
| 230 | system.Send(pid, &CountMsg{Value: 1}) |
| 231 | } |
| 232 | }() |
| 233 | } |
| 234 | |
| 235 | wg.Wait() |
| 236 | time.Sleep(200 * time.Millisecond) |
| 237 | |
| 238 | // 获取计数 |
| 239 | replyCh := make(chan int, 1) |
| 240 | system.Send(pid, &GetCountMsg{ReplyTo: replyCh}) |
| 241 | |
| 242 | select { |
| 243 | case count := <-replyCh: |
| 244 | assert.Equal(t, numGoroutines*messagesPerGoroutine, count) |
| 245 | case <-time.After(time.Second): |
| 246 | t.Fatal("timeout waiting for count") |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func TestSystem_Stop(t *testing.T) { |
| 251 | system := NewSystem("test") |
nothing calls this directly
no test coverage detected