()
| 57 | } |
| 58 | |
| 59 | func (s *ActorSuite) TestCounterConcurrency() { |
| 60 | // 使用更大的邮箱配置 |
| 61 | s.system.Shutdown() |
| 62 | config := actor.DefaultSystemConfig() |
| 63 | config.MailboxSize = 20000 |
| 64 | config.DefaultActorMailboxSize = 5000 |
| 65 | s.system = actor.NewSystemWithConfig("test", config) |
| 66 | |
| 67 | // 创建计数器 Actor(使用更大的邮箱) |
| 68 | counter := &CounterActor{name: "counter", count: 0} |
| 69 | props := &actor.Props{ |
| 70 | Name: "counter", |
| 71 | MailboxSize: 5000, |
| 72 | } |
| 73 | pid := s.system.SpawnWithProps(counter, props) |
| 74 | |
| 75 | // 并发发送增量消息 |
| 76 | var wg sync.WaitGroup |
| 77 | numGoroutines := 10 |
| 78 | incrementsPerGoroutine := 100 |
| 79 | |
| 80 | for range numGoroutines { |
| 81 | wg.Add(1) |
| 82 | go func() { |
| 83 | defer wg.Done() |
| 84 | for range incrementsPerGoroutine { |
| 85 | pid.Tell(&IncrementMsg{Value: 1}) |
| 86 | } |
| 87 | }() |
| 88 | } |
| 89 | |
| 90 | wg.Wait() |
| 91 | time.Sleep(200 * time.Millisecond) // 等待消息处理完成 |
| 92 | |
| 93 | // 获取最终计数 |
| 94 | replyCh := make(chan int, 1) |
| 95 | pid.Tell(&GetCountMsg{ReplyTo: replyCh}) |
| 96 | |
| 97 | select { |
| 98 | case count := <-replyCh: |
| 99 | expected := numGoroutines * incrementsPerGoroutine |
| 100 | s.Equal(expected, count, "并发计数应正确") |
| 101 | case <-time.After(time.Second): |
| 102 | s.T().Fatal("获取计数超时") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func (s *ActorSuite) TestSupervisorRestart() { |
| 107 | // 使用静默 panic 处理器 |
nothing calls this directly
no test coverage detected