github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/internal/queue/goring/queue_test.go (about)

     1  package goring
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPushPop(t *testing.T) {
    11  	q := New(10)
    12  	q.Push("hello")
    13  	res, _ := q.Pop()
    14  	assert.Equal(t, "hello", res)
    15  	assert.True(t, q.Empty())
    16  }
    17  
    18  func TestPushPopRepeated(t *testing.T) {
    19  	q := New(10)
    20  	for i := 0; i < 100; i++ {
    21  		q.Push("hello")
    22  		res, _ := q.Pop()
    23  		assert.Equal(t, "hello", res)
    24  		assert.True(t, q.Empty())
    25  	}
    26  }
    27  
    28  func TestPushPopMany(t *testing.T) {
    29  	q := New(10)
    30  	for i := 0; i < 10000; i++ {
    31  		item := fmt.Sprintf("hello%v", i)
    32  		q.Push(item)
    33  		res, _ := q.Pop()
    34  		assert.Equal(t, item, res)
    35  	}
    36  	assert.True(t, q.Empty())
    37  }
    38  
    39  func TestPushPopMany2(t *testing.T) {
    40  	q := New(10)
    41  	for i := 0; i < 10000; i++ {
    42  		item := fmt.Sprintf("hello%v", i)
    43  		q.Push(item)
    44  	}
    45  	for i := 0; i < 10000; i++ {
    46  		item := fmt.Sprintf("hello%v", i)
    47  		res, _ := q.Pop()
    48  		assert.Equal(t, item, res)
    49  	}
    50  	assert.True(t, q.Empty())
    51  }
    52  
    53  //func TestLfQueueConsistency(t *testing.T) {
    54  //	max := 1000000
    55  //	c := 100
    56  //	var wg sync.WaitGroup
    57  //	wg.Add(1)
    58  //	q := New(2)
    59  //	go func() {
    60  //		i := 0
    61  //		seen := make(map[string]string)
    62  //		for {
    63  //			r, ok := q.Pop()
    64  //			if !ok {
    65  //				runtime.Gosched()
    66  //
    67  //				continue
    68  //			}
    69  //			i++
    70  //			if r == nil {
    71  //				log.Printf("%#v, %#v", q, q.content)
    72  //				panic("consistency failure")
    73  //			}
    74  //			s := r.(string)
    75  //			_, present := seen[s]
    76  //			if present {
    77  //				log.Printf("item have already been seen %v", s)
    78  //				t.FailNow()
    79  //			}
    80  //			seen[s] = s
    81  //
    82  //			if i == max {
    83  //				wg.Done()
    84  //				return
    85  //			}
    86  //		}
    87  //	}()
    88  //
    89  //	for j := 0; j < c; j++ {
    90  //		jj := j
    91  //		cmax := max / c
    92  //		go func() {
    93  //			for i := 0; i < cmax; i++ {
    94  //				if rand.Intn(10) == 0 {
    95  //					time.Sleep(time.Duration(rand.Intn(1000)))
    96  //				}
    97  //				q.Push(fmt.Sprintf("%v %v", jj, i))
    98  //			}
    99  //		}()
   100  //	}
   101  //
   102  //	wg.Wait()
   103  //	time.Sleep(500 * time.Millisecond)
   104  //	// queue should be empty
   105  //	for i := 0; i < 100; i++ {
   106  //		r, ok := q.Pop()
   107  //		if ok {
   108  //			log.Printf("unexpected result %+v", r)
   109  //			t.FailNow()
   110  //		}
   111  //	}
   112  //}