============== 测试用例 ==============
(t *testing.T)
| 97 | // ============== 测试用例 ============== |
| 98 | |
| 99 | func TestSystem_SpawnAndSend(t *testing.T) { |
| 100 | system := NewSystem("test") |
| 101 | defer system.Shutdown() |
| 102 | |
| 103 | // 创建计数器 Actor |
| 104 | counter := &CounterActor{} |
| 105 | pid := system.Spawn(counter, "counter") |
| 106 | |
| 107 | assert.NotNil(t, pid) |
| 108 | assert.Equal(t, "counter", pid.ID) |
| 109 | |
| 110 | // 发送消息 |
| 111 | system.Send(pid, &CountMsg{Value: 10}) |
| 112 | system.Send(pid, &CountMsg{Value: 20}) |
| 113 | system.Send(pid, &CountMsg{Value: 30}) |
| 114 | |
| 115 | // 等待处理 |
| 116 | time.Sleep(100 * time.Millisecond) |
| 117 | |
| 118 | // 获取计数 |
| 119 | replyCh := make(chan int, 1) |
| 120 | system.Send(pid, &GetCountMsg{ReplyTo: replyCh}) |
| 121 | |
| 122 | select { |
| 123 | case count := <-replyCh: |
| 124 | assert.Equal(t, 60, count) |
| 125 | case <-time.After(time.Second): |
| 126 | t.Fatal("timeout waiting for count") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestSystem_RequestResponse(t *testing.T) { |
| 131 | system := NewSystem("test") |