github.com/polarismesh/polaris@v1.17.8/common/eventhub/eventhub_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package eventhub 19 20 import ( 21 "context" 22 "fmt" 23 "runtime" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestEventHub_Init(t *testing.T) { 31 tests := []struct { 32 name string 33 }{ 34 { 35 name: "init eventhub", 36 }, 37 } 38 for _, tt := range tests { 39 t.Run(tt.name, func(t *testing.T) { 40 eh := createEventhub() 41 assert.NotNil(t, eh) 42 }) 43 } 44 } 45 46 func TestEventHub_Publish(t *testing.T) { 47 type args struct { 48 topic string 49 name string 50 handler Handler 51 opts []SubOption 52 eventNum int 53 } 54 tests := []struct { 55 name string 56 args args 57 }{ 58 { 59 name: "publish event", 60 args: args{ 61 topic: "test1", 62 name: "subscribe1", 63 handler: &printEventHandler{}, 64 eventNum: 100, 65 }, 66 }, 67 } 68 for _, tt := range tests { 69 t.Run(tt.name, func(t *testing.T) { 70 eh := createEventhub() 71 subCtx, err := eh.Subscribe(tt.args.topic, tt.args.handler, tt.args.opts...) 72 assert.Nil(t, err) 73 for i := 0; i < tt.args.eventNum; i++ { 74 eh.Publish(tt.args.topic, i) 75 } 76 subCtx.Cancel() 77 time.Sleep(5 * time.Second) 78 fmt.Println("number of goroutines:", runtime.NumGoroutine()) 79 eh.shutdown() 80 }) 81 } 82 } 83 84 type printEventHandler struct { 85 } 86 87 // PreProcess do preprocess logic for event 88 func (p *printEventHandler) PreProcess(ctx context.Context, value any) any { 89 return value 90 } 91 92 // OnEvent event process logic 93 func (p *printEventHandler) OnEvent(ctx context.Context, any2 any) error { 94 fmt.Printf("handle event=%v", any2) 95 return nil 96 } 97 98 func TestEventHub_Subscribe(t *testing.T) { 99 type args struct { 100 topic string 101 name string 102 handler Handler 103 opts []SubOption 104 } 105 tests := []struct { 106 name string 107 args args 108 wantErr error 109 }{ 110 { 111 name: "subscribe topic", 112 args: args{ 113 topic: "test2", 114 name: "subscribe2", 115 handler: &printEventHandler{}, 116 opts: []SubOption{WithQueueSize(100)}, 117 }, 118 wantErr: nil, 119 }, 120 } 121 for _, tt := range tests { 122 t.Run(tt.name, func(t *testing.T) { 123 eh := createEventhub() 124 subCtx, err := eh.Subscribe(tt.args.topic, tt.args.handler, tt.args.opts...) 125 assert.Equal(t, tt.wantErr, err) 126 subCtx.Cancel() 127 }) 128 } 129 } 130 131 func TestEventHub_Unsubscribe(t *testing.T) { 132 type args struct { 133 topic string 134 name string 135 handler Handler 136 } 137 tests := []struct { 138 name string 139 args args 140 wantErr error 141 }{ 142 { 143 name: "unsubscribe topic", 144 args: args{ 145 topic: "test3", 146 name: "subscribe3", 147 handler: &printEventHandler{}, 148 }, 149 wantErr: nil, 150 }, 151 } 152 for _, tt := range tests { 153 t.Run(tt.name, func(t *testing.T) { 154 eh := createEventhub() 155 subCtx, err := eh.Subscribe(tt.args.topic, tt.args.handler) 156 assert.Nil(t, err) 157 subCtx.Cancel() 158 time.Sleep(2 * time.Second) 159 }) 160 } 161 } 162 163 func TestEventHub_Shutdown(t *testing.T) { 164 tests := []struct { 165 name string 166 wantTopicNum int 167 }{ 168 { 169 name: "shutdown topic", 170 wantTopicNum: 0, 171 }, 172 } 173 for _, tt := range tests { 174 t.Run(tt.name, func(t *testing.T) { 175 eh := createEventhub() 176 eh.shutdown() 177 assert.Equal(t, tt.wantTopicNum, len(eh.topics)) 178 }) 179 } 180 } 181 182 func TestEventHub_createTopic(t *testing.T) { 183 type args struct { 184 name string 185 } 186 tests := []struct { 187 name string 188 args args 189 want *topic 190 wantErr error 191 }{ 192 { 193 name: "create topic", 194 args: args{ 195 name: "topic1", 196 }, 197 want: &topic{ 198 name: "topic1", 199 subs: make(map[string]*subscription), 200 }, 201 }, 202 } 203 eh := createEventhub() 204 for _, tt := range tests { 205 t.Run(tt.name, func(t *testing.T) { 206 got := eh.createTopic(tt.args.name) 207 assert.Equal(t, tt.want.name, got.name) 208 got = eh.createTopic(tt.args.name) 209 assert.Equal(t, tt.want.name, got.name) 210 }) 211 } 212 } 213 214 func TestEventHub_getTopic(t *testing.T) { 215 type args struct { 216 name string 217 } 218 tests := []struct { 219 name string 220 args args 221 want *topic 222 }{ 223 { 224 name: "get topic exsit", 225 args: args{ 226 name: "topic1", 227 }, 228 want: &topic{ 229 name: "topic1", 230 subs: make(map[string]*subscription), 231 }, 232 }, 233 { 234 name: "get topic not exsit", 235 args: args{ 236 name: "topic2", 237 }, 238 want: &topic{ 239 name: "topic2", 240 subs: make(map[string]*subscription), 241 }, 242 }, 243 } 244 for _, tt := range tests { 245 t.Run(tt.name, func(t *testing.T) { 246 eh := createEventhub() 247 got := eh.getTopic(tt.args.name) 248 assert.Equal(t, tt.want.name, got.name) 249 }) 250 } 251 }