github.com/polarismesh/polaris@v1.17.8/plugin/discoverevent/local/eventbuffer_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  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"github.com/polarismesh/polaris/common/model"
    27  )
    28  
    29  func TestEventBufferTest(t *testing.T) {
    30  
    31  	bufferHolder := newEventBufferHolder(20)
    32  
    33  	expectCnt := int64(0)
    34  	for i := 0; i < 10; i++ {
    35  		now := time.Now()
    36  		bufferHolder.Put(model.InstanceEvent{
    37  			CreateTime: now,
    38  		})
    39  
    40  		expectCnt += now.Unix()
    41  	}
    42  
    43  	actualCnt := int64(0)
    44  
    45  	for bufferHolder.HasNext() {
    46  		event := bufferHolder.Next()
    47  		actualCnt += event.CreateTime.Unix()
    48  	}
    49  
    50  	assert.Equal(t, expectCnt, actualCnt, "cnt must be equla")
    51  
    52  	bufferHolder.Reset()
    53  
    54  	expectCnt = int64(0)
    55  	for i := 20; i < 40; i++ {
    56  		now := time.Now()
    57  		bufferHolder.Put(model.InstanceEvent{
    58  			CreateTime: now,
    59  		})
    60  
    61  		expectCnt += now.Unix()
    62  	}
    63  
    64  	actualCnt = int64(0)
    65  
    66  	for bufferHolder.HasNext() {
    67  		event := bufferHolder.Next()
    68  		actualCnt += event.CreateTime.Unix()
    69  	}
    70  
    71  	assert.Equal(t, expectCnt, actualCnt, "cnt must be equla")
    72  }