github.com/cloudwego/kitex@v0.9.0/pkg/event/queue_test.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package event
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/cloudwego/kitex/internal/test"
    24  )
    25  
    26  func TestQueueInvalidCapacity(t *testing.T) {
    27  	defer func() {
    28  		e := recover()
    29  		test.Assert(t, e != nil)
    30  	}()
    31  	NewQueue(-1)
    32  }
    33  
    34  func TestQueue(t *testing.T) {
    35  	size := 10
    36  	q := NewQueue(size)
    37  
    38  	for i := 0; i < size; i++ {
    39  		n := "E." + strconv.Itoa(i)
    40  		q.Push(&Event{
    41  			Name: n,
    42  		})
    43  		es := q.Dump().([]*Event)
    44  		test.Assert(t, len(es) == i+1)
    45  		test.Assert(t, es[0].Name == n)
    46  	}
    47  
    48  	for i := 0; i < size; i++ {
    49  		q.Push(&Event{
    50  			Name: strconv.Itoa(i),
    51  		})
    52  		es := q.Dump().([]*Event)
    53  		test.Assert(t, len(es) == size)
    54  	}
    55  }
    56  
    57  func BenchmarkQueue(b *testing.B) {
    58  	q := NewQueue(100)
    59  	e := &Event{}
    60  	b.ReportAllocs()
    61  	b.ResetTimer()
    62  	for i := 0; i < b.N; i++ {
    63  		q.Push(e)
    64  	}
    65  }
    66  
    67  func BenchmarkQueueConcurrent(b *testing.B) {
    68  	q := NewQueue(100)
    69  	e := &Event{}
    70  	b.ResetTimer()
    71  	b.RunParallel(func(pb *testing.PB) {
    72  		for pb.Next() {
    73  			q.Push(e)
    74  		}
    75  	})
    76  	b.StopTimer()
    77  }