github.com/polarismesh/polaris@v1.17.8/common/eventhub/subscription_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  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  type noopEventHandler struct {
    29  }
    30  
    31  // PreProcess do preprocess logic for event
    32  func (p *noopEventHandler) PreProcess(ctx context.Context, value any) any {
    33  	return value
    34  }
    35  
    36  // OnEvent event process logic
    37  func (p *noopEventHandler) OnEvent(ctx context.Context, any2 any) error {
    38  	return nil
    39  }
    40  
    41  func Test_newSubscription(t *testing.T) {
    42  	type args struct {
    43  		name    string
    44  		handler Handler
    45  		opts    []SubOption
    46  	}
    47  	tests := []struct {
    48  		name string
    49  		args args
    50  		want *subscription
    51  	}{
    52  		{
    53  			name: "new sub",
    54  			args: args{
    55  				name:    "sub1",
    56  				handler: &noopEventHandler{},
    57  				opts:    []SubOption{WithQueueSize(100)},
    58  			},
    59  			want: &subscription{
    60  				name:    "sub1",
    61  				queue:   make(chan Event, 100),
    62  				closeCh: make(chan struct{}),
    63  				handler: &noopEventHandler{},
    64  				opts: &SubOptions{
    65  					QueueSize: 100,
    66  				},
    67  			},
    68  		},
    69  		{
    70  			name: "new sub no sub option",
    71  			args: args{
    72  				name:    "sub2",
    73  				handler: &noopEventHandler{},
    74  			},
    75  			want: &subscription{
    76  				name:    "sub2",
    77  				queue:   make(chan Event, defaultQueueSize),
    78  				closeCh: make(chan struct{}),
    79  				handler: &noopEventHandler{},
    80  				opts: &SubOptions{
    81  					QueueSize: defaultQueueSize,
    82  				},
    83  			},
    84  		},
    85  		{
    86  			name: "new sub with invalid sub option",
    87  			args: args{
    88  				name:    "sub3",
    89  				handler: &noopEventHandler{},
    90  				opts:    []SubOption{WithQueueSize(0)},
    91  			},
    92  			want: &subscription{
    93  				name:    "sub3",
    94  				queue:   make(chan Event, defaultQueueSize),
    95  				closeCh: make(chan struct{}),
    96  				handler: &noopEventHandler{},
    97  				opts: &SubOptions{
    98  					QueueSize: defaultQueueSize,
    99  				},
   100  			},
   101  		},
   102  	}
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			got := newSubscription(tt.args.name, tt.args.handler, tt.args.opts...)
   106  			assert.Equal(t, tt.want.name, got.name)
   107  			assert.Equal(t, tt.want.opts, got.opts)
   108  		})
   109  	}
   110  }
   111  
   112  func Test_subscription_send(t *testing.T) {
   113  	type args struct {
   114  		ctx context.Context
   115  		num int
   116  	}
   117  	tests := []struct {
   118  		name string
   119  		args args
   120  	}{
   121  		{
   122  			name: "send msg to subscription",
   123  			args: args{
   124  				ctx: context.Background(),
   125  				num: 1000,
   126  			},
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			s := newSubscription("sub1", &noopEventHandler{})
   132  			for i := 0; i < tt.args.num; i++ {
   133  				s.send(tt.args.ctx, i)
   134  			}
   135  			assert.Equal(t, tt.args.num, len(s.queue))
   136  		})
   137  	}
   138  }
   139  
   140  type counterEventHandler struct {
   141  	got int
   142  }
   143  
   144  // PreProcess do preprocess logic for event
   145  func (p *counterEventHandler) PreProcess(ctx context.Context, value any) any {
   146  	return value
   147  }
   148  
   149  // OnEvent event process logic
   150  func (p *counterEventHandler) OnEvent(ctx context.Context, any2 any) error {
   151  	p.got++
   152  	return nil
   153  }
   154  
   155  func Test_subscription_receive(t *testing.T) {
   156  	type args struct {
   157  		ctx context.Context
   158  		num int
   159  	}
   160  	tests := []struct {
   161  		name string
   162  		args args
   163  		want int
   164  	}{
   165  		{
   166  			name: "subscription receive msg",
   167  			args: args{
   168  				ctx: context.Background(),
   169  				num: 1000,
   170  			},
   171  			want: 1000,
   172  		},
   173  	}
   174  	for _, tt := range tests {
   175  		t.Run(tt.name, func(t *testing.T) {
   176  			handler := &counterEventHandler{}
   177  			s := newSubscription("sub1", handler)
   178  			go func() {
   179  				for i := 0; i < tt.args.num; i++ {
   180  					s.send(tt.args.ctx, i)
   181  				}
   182  			}()
   183  			go s.receive(tt.args.ctx)
   184  			time.Sleep(2 * time.Second)
   185  			s.close()
   186  			assert.Equal(t, tt.want, handler.got)
   187  		})
   188  	}
   189  }