github.com/dfklegend/cell2/utils@v0.0.0-20240402033734-a0a9f3d9335d/event/event_test.go (about) 1 package event 2 3 import ( 4 "log" 5 "sync" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 // 测试timer基本流程 13 // 创建,cancel 14 func Test_Normal(t *testing.T) { 15 center := NewLocalEventCenter(false) 16 17 id1 := center.Subscribe("event1", func(args ...interface{}) { 18 log.Println(args) 19 }, 1, "haha") 20 id1_2 := center.Subscribe("event1", func(args ...interface{}) { 21 log.Println(args) 22 }) 23 24 center.Publish("event1", 3, 4) 25 center.Publish("event1", 5, 6) 26 center.Unsubscribe("event1", id1) 27 center.Publish("event1") 28 29 log.Printf("ids:%v %v", id1, id1_2) 30 } 31 32 func localRun(index int) { 33 center := NewLocalEventCenter(false) 34 center.SetLocalUseChan(true) 35 36 id1 := center.Subscribe("eventLocal", func(args ...interface{}) { 37 log.Printf("%v eventLocal %v", index, args) 38 }, "selfArg1") 39 40 id2 := center.GSubscribe("event2", func(args ...interface{}) { 41 log.Printf("%v event2 %v", index, args) 42 }, "selfArg2") 43 44 running := true 45 go func() { 46 time.Sleep(5 * time.Second) 47 running = false 48 }() 49 50 go func() { 51 time.Sleep(2 * time.Second) 52 center.Publish("eventLocal", "from local") 53 center.Publish("event2", "from local") 54 }() 55 56 for running { 57 select { 58 case e := <-center.GetChanEvent(): 59 center.DoEvent(e) 60 default: 61 } 62 } 63 64 log.Printf("running:%v", running) 65 log.Printf("ids:%v %v", id1, id2) 66 center.Clear() 67 } 68 69 func Test_Global(t *testing.T) { 70 71 go func() { 72 time.Sleep(1 * time.Second) 73 GetGlobalEC().Publish("eventLocal", "fromGlobal") 74 GetGlobalEC().Publish("event2", "fromGlobal") 75 time.Sleep(100 * time.Millisecond) 76 }() 77 78 go localRun(1) 79 go localRun(2) 80 81 time.Sleep(3 * time.Second) 82 GetGlobalEC().DumpInfo() 83 time.Sleep(4 * time.Second) 84 GetGlobalEC().DumpInfo() 85 } 86 87 // 测试并发安全性 88 // 并发订阅/取消订阅,publish 89 func dotestConcurrence(t *testing.T, fakeMutex bool) { 90 gotPanic := false 91 92 useFakeMutex = fakeMutex 93 center := NewLocalEventCenter(false) 94 95 running := true 96 ids := make([]uint64, 9999) 97 var mutex sync.Mutex 98 99 // 一直订阅 100 for i := 0; i < 3; i++ { 101 go func() { 102 defer func() { 103 if err := recover(); err != nil { 104 gotPanic = true 105 } 106 }() 107 108 for running { 109 id := center.Subscribe("event1", func(args ...interface{}) { 110 //log.Printf("1") 111 }) 112 mutex.Lock() 113 ids = append(ids, id) 114 mutex.Unlock() 115 } 116 }() 117 } 118 119 // 一直取消订阅 120 for i := 0; i < 1; i++ { 121 go func() { 122 defer func() { 123 if err := recover(); err != nil { 124 gotPanic = true 125 } 126 }() 127 128 for running { 129 mutex.Lock() 130 if len(ids) > 0 { 131 id := ids[0] 132 ids = ids[1:] 133 134 center.Unsubscribe("event1", id) 135 } 136 mutex.Unlock() 137 } 138 }() 139 } 140 141 // 发布事件 142 for i := 0; i < 3; i++ { 143 go func() { 144 defer func() { 145 if err := recover(); err != nil { 146 gotPanic = true 147 } 148 }() 149 150 for running { 151 center.Publish("event1", 1) 152 } 153 }() 154 } 155 156 // 每秒输出信息 157 go func() { 158 defer func() { 159 if err := recover(); err != nil { 160 gotPanic = true 161 } 162 }() 163 164 t := time.NewTicker(1 * time.Second) 165 166 for running { 167 select { 168 case <-t.C: 169 center.DumpInfo("event1") 170 } 171 } 172 }() 173 174 // 5秒结束 175 time.Sleep(5 * time.Second) 176 running = false 177 178 if gotPanic { 179 panic("got panic") 180 } 181 } 182 183 func Test_ConcurrenceNormal(t *testing.T) { 184 log.Printf("---- Test_ConcurrenceNormal ----") 185 dotestConcurrence(t, false) 186 } 187 188 // concurrent map iteration and map write 不是panic 189 func NoTest_ConcurrenceFakeMutex(t *testing.T) { 190 log.Printf("---- Test_ConcurrenceFakeMutex ----") 191 log.Printf("---- 应该有map竞争冲突 ----") 192 193 assert.Panics(t, func() { 194 dotestConcurrence(t, true) 195 }) 196 }