github.com/polarismesh/polaris@v1.17.8/common/eventhub/topic_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 "testing" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func Test_newTopic(t *testing.T) { 28 type args struct { 29 name string 30 } 31 tests := []struct { 32 name string 33 args args 34 want *topic 35 }{ 36 { 37 name: "new topic", 38 args: args{ 39 name: "topic1", 40 }, 41 want: &topic{ 42 name: "topic1", 43 queue: make(chan Event, defaultQueueSize), 44 closeCh: make(chan struct{}), 45 subs: make(map[string]*subscription), 46 }, 47 }, 48 } 49 for _, tt := range tests { 50 t.Run(tt.name, func(t *testing.T) { 51 got := newTopic(tt.args.name) 52 assert.Equal(t, tt.want.name, got.name) 53 }) 54 } 55 } 56 57 func Test_topic_publish(t *testing.T) { 58 type args struct { 59 ctx context.Context 60 num int 61 } 62 tests := []struct { 63 name string 64 args args 65 }{ 66 { 67 name: "publish msg to topic", 68 args: args{ 69 ctx: context.Background(), 70 num: 100, 71 }, 72 }, 73 } 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 tp := newTopic("topic1") 77 for i := 0; i < tt.args.num; i++ { 78 tp.publish(tt.args.ctx, i) 79 } 80 assert.Equal(t, tt.args.num, len(tp.queue)) 81 }) 82 } 83 } 84 85 func Test_topic_subscribe(t *testing.T) { 86 type args struct { 87 ctx context.Context 88 name string 89 handler Handler 90 opts []SubOption 91 } 92 tests := []struct { 93 name string 94 args args 95 wantErr error 96 }{ 97 98 { 99 name: "topic subscribe", 100 args: args{ 101 ctx: context.Background(), 102 handler: &printEventHandler{}, 103 opts: []SubOption{WithQueueSize(100)}, 104 }, 105 wantErr: nil, 106 }, 107 } 108 for _, tt := range tests { 109 t.Run(tt.name, func(t *testing.T) { 110 tr := newTopic("topic2") 111 subCtx, err := tr.subscribe(tt.args.ctx, tt.args.handler, tt.args.opts...) 112 assert.Equal(t, tt.wantErr, err) 113 subCtx.Cancel() 114 }) 115 } 116 } 117 118 func Test_topic_unsubscribe(t *testing.T) { 119 type args struct { 120 ctx context.Context 121 name string 122 handler Handler 123 } 124 tests := []struct { 125 name string 126 args args 127 wantErr error 128 }{ 129 { 130 name: "topic unsubscribe", 131 args: args{ 132 ctx: context.Background(), 133 handler: &printEventHandler{}, 134 }, 135 wantErr: nil, 136 }, 137 } 138 for _, tt := range tests { 139 t.Run(tt.name, func(t *testing.T) { 140 tr := newTopic("topic3") 141 subCtx, err := tr.subscribe(tt.args.ctx, tt.args.handler) 142 assert.NoError(t, err) 143 _, ok := tr.subs[subCtx.subID] 144 assert.True(t, ok) 145 subCtx.Cancel() 146 _, ok = tr.subs[subCtx.subID] 147 assert.False(t, ok) 148 }) 149 } 150 } 151 152 func Test_topic_run(t *testing.T) { 153 type args struct { 154 ctx context.Context 155 name string 156 handler Handler 157 opts []SubOption 158 num int 159 } 160 tests := []struct { 161 name string 162 args args 163 }{ 164 { 165 name: "topic run", 166 args: args{ 167 ctx: context.Background(), 168 name: "sub1", 169 handler: &printEventHandler{}, 170 opts: []SubOption{WithQueueSize(100)}, 171 num: 100, 172 }, 173 }, 174 } 175 for _, tt := range tests { 176 t.Run(tt.name, func(t *testing.T) { 177 tr := newTopic("topic4") 178 subCtx, err := tr.subscribe(tt.args.ctx, tt.args.handler, tt.args.opts...) 179 assert.Nil(t, err) 180 go tr.run(tt.args.ctx) 181 for i := 0; i < tt.args.num; i++ { 182 tr.publish(tt.args.ctx, i) 183 } 184 subCtx.Cancel() 185 tr.close(tt.args.ctx) 186 assert.Zero(t, len(tr.subs)) 187 }) 188 } 189 }