github.com/GoWebProd/gip@v0.0.0-20230623090727-b60d41d5d320/queue/queue_test.go (about)

     1  package queue
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func TestQueue(t *testing.T) {
     9  	queue := New[string]()
    10  	test1 := "test1"
    11  	test2 := "test2"
    12  	test3 := "test3"
    13  
    14  	if str, ok := queue.Take(); ok {
    15  		t.Fatalf("queue must be empty, but returned: %s", *str)
    16  	}
    17  
    18  	queue.Put(&test1)
    19  	queue.Put(&test2)
    20  
    21  	str, ok := queue.Take()
    22  	if !ok {
    23  		t.Fatalf("queue must be not empty, but data not returned")
    24  	}
    25  
    26  	if *str != test1 {
    27  		t.Fatalf("string must be %q but returned %q", test1, *str)
    28  	}
    29  
    30  	queue.Put(&test3)
    31  
    32  	str, ok = queue.Take()
    33  	if !ok {
    34  		t.Fatalf("queue must be not empty, but data not returned")
    35  	}
    36  
    37  	if *str != test2 {
    38  		t.Fatalf("string must be %q but returned %q", test2, *str)
    39  	}
    40  
    41  	str, ok = queue.Take()
    42  	if !ok {
    43  		t.Fatalf("queue must be not empty, but data not returned")
    44  	}
    45  
    46  	if *str != test3 {
    47  		t.Fatalf("string must be %q but returned %q", test2, *str)
    48  	}
    49  
    50  	if str, ok = queue.Take(); ok {
    51  		t.Fatalf("queue must be empty, but returned: %s", *str)
    52  	}
    53  }
    54  
    55  func TestParallel(t *testing.T) {
    56  	const n = 100
    57  	const elements = 100000
    58  
    59  	queue := New[int]()
    60  	wg := sync.WaitGroup{}
    61  	mu := sync.Mutex{}
    62  	m := make(map[int]struct{})
    63  
    64  	for i := 0; i < n; i++ {
    65  		wg.Add(2)
    66  
    67  		n := elements * 100 * (i + 1)
    68  
    69  		go func(base int) {
    70  			defer wg.Done()
    71  
    72  			for i := 0; i < elements; i++ {
    73  				item := base + i
    74  
    75  				queue.Put(&item)
    76  			}
    77  		}(n)
    78  
    79  		go func() {
    80  			defer wg.Done()
    81  
    82  			localM := make(map[int]struct{})
    83  
    84  			for i := 0; i < elements; i++ {
    85  				item, ok := queue.Take()
    86  				if !ok {
    87  					i--
    88  					continue
    89  				}
    90  
    91  				localM[*item] = struct{}{}
    92  			}
    93  
    94  			mu.Lock()
    95  
    96  			for k, v := range localM {
    97  				m[k] = v
    98  			}
    99  
   100  			mu.Unlock()
   101  		}()
   102  	}
   103  
   104  	wg.Wait()
   105  
   106  	if len(m) != elements*n {
   107  		t.Fatalf("bad elements count in map: %d, expected: %d", len(m), elements*n)
   108  	}
   109  }
   110  
   111  func BenchmarkNew(b *testing.B) {
   112  	queue := New[int]()
   113  
   114  	for i := 0; i < b.N; i++ {
   115  		queue.Put(&i)
   116  
   117  		v, ok := queue.Take()
   118  		if !ok || *v != i {
   119  			b.Fatal("bad value")
   120  		}
   121  	}
   122  }