github.com/polarismesh/polaris@v1.17.8/plugin/discoverevent/local/event_local_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 local
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/golang/protobuf/ptypes/wrappers"
    27  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    28  	"github.com/stretchr/testify/assert"
    29  
    30  	"github.com/polarismesh/polaris/common/model"
    31  )
    32  
    33  func Test_discoverEventLocal_Run(t *testing.T) {
    34  
    35  	totalCnt := 10
    36  
    37  	wait := sync.WaitGroup{}
    38  	wait.Add(totalCnt)
    39  
    40  	testFn := func(eb *eventBufferHolder) {
    41  		for eb.HasNext() {
    42  			wait.Done()
    43  			et := eb.Next()
    44  			t.Logf("%v", et)
    45  			_, ok := subscribeEvents[et.EType]
    46  			assert.True(t, ok)
    47  		}
    48  	}
    49  
    50  	ctx, cancel := context.WithCancel(context.Background())
    51  
    52  	l := &discoverEventLocal{
    53  		eventCh: make(chan model.InstanceEvent, 32),
    54  		bufferPool: sync.Pool{
    55  			New: func() interface{} { return newEventBufferHolder(defaultBufferSize) },
    56  		},
    57  		cursor:       0,
    58  		syncLock:     sync.Mutex{},
    59  		eventHandler: testFn,
    60  		cancel:       cancel,
    61  	}
    62  	l.switchEventBuffer()
    63  
    64  	t.Cleanup(func() {
    65  		l.Destroy()
    66  	})
    67  
    68  	go l.Run(ctx)
    69  
    70  	for i := 0; i < totalCnt; i++ {
    71  		l.PublishEvent(model.InstanceEvent{
    72  			Id:        "123456",
    73  			Namespace: "DemoNamespace",
    74  			Service:   "DemoService",
    75  			Instance: &apiservice.Instance{
    76  				Host: &wrappers.StringValue{Value: "127.0.0.1"},
    77  				Port: &wrappers.UInt32Value{Value: 8080},
    78  			},
    79  			EType:      model.EventInstanceCloseIsolate,
    80  			CreateTime: time.Time{},
    81  		})
    82  
    83  		l.PublishEvent(model.InstanceEvent{
    84  			Id:        "111111",
    85  			Namespace: "DemoNamespace",
    86  			Service:   "DemoService",
    87  			Instance: &apiservice.Instance{
    88  				Host: &wrappers.StringValue{Value: "127.0.0.1"},
    89  				Port: &wrappers.UInt32Value{Value: 8080},
    90  			},
    91  			EType:      model.EventInstanceSendHeartbeat,
    92  			CreateTime: time.Time{},
    93  		})
    94  
    95  		l.PublishEvent(model.InstanceEvent{
    96  			Id:        "111111",
    97  			Namespace: "DemoNamespace",
    98  			Service:   "DemoService",
    99  			Instance: &apiservice.Instance{
   100  				Host: &wrappers.StringValue{Value: "127.0.0.1"},
   101  				Port: &wrappers.UInt32Value{Value: 8080},
   102  			},
   103  			EType:      model.EventInstanceUpdate,
   104  			CreateTime: time.Time{},
   105  		})
   106  	}
   107  
   108  	wait.Wait()
   109  }