github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/tools/flusher_test.go (about)

     1  package tools
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"sync/atomic"
     7  	"testing"
     8  	"time"
     9  
    10  	. "github.com/smartystreets/goconvey/convey"
    11  )
    12  
    13  /*
    14     Creation Time: 2021 - Jan - 01
    15     Created by:  (ehsan)
    16     Maintainers:
    17        1.  Ehsan N. Moosa (E2)
    18     Auditor: Ehsan N. Moosa (E2)
    19     Copyright Ronak Software Group 2020
    20  */
    21  
    22  func TestNewFlusherPool(t *testing.T) {
    23  	Convey("Flusher", t, func(c C) {
    24  		Convey("Without WaitTime", func(c C) {
    25  			var out, in int64
    26  			f := NewFlusherPool(10, 20, func(targetID string, entries []FlushEntry) {
    27  				time.Sleep(time.Millisecond * 100)
    28  				atomic.AddInt64(&out, int64(len(entries)))
    29  			})
    30  
    31  			wg := sync.WaitGroup{}
    32  			total := 10000
    33  			for i := 0; i < total; i++ {
    34  				wg.Add(1)
    35  				go func() {
    36  					f.EnterAndWait(fmt.Sprintf("T%d", RandomInt(3)), NewEntry(RandomInt(10)))
    37  					atomic.AddInt64(&in, 1)
    38  					wg.Done()
    39  				}()
    40  			}
    41  			wg.Wait()
    42  			for _, q := range f.pool {
    43  				c.So(q.entryChan, ShouldHaveLength, 0)
    44  			}
    45  			c.So(in, ShouldEqual, total)
    46  			c.So(out, ShouldEqual, total)
    47  		})
    48  		Convey("With WaitTime", func(c C) {
    49  			var out, in int64
    50  			f := NewFlusherPoolWithWaitTime(10, 20, 250*time.Millisecond, func(targetID string, entries []FlushEntry) {
    51  				time.Sleep(time.Millisecond * 100)
    52  				atomic.AddInt64(&out, int64(len(entries)))
    53  			})
    54  
    55  			wg := sync.WaitGroup{}
    56  			total := 10000
    57  			for i := 0; i < total; i++ {
    58  				wg.Add(1)
    59  				go func() {
    60  					f.EnterAndWait(fmt.Sprintf("T%d", RandomInt(3)), NewEntry(RandomInt(10)))
    61  					atomic.AddInt64(&in, 1)
    62  					wg.Done()
    63  				}()
    64  			}
    65  			wg.Wait()
    66  			for _, q := range f.pool {
    67  				c.So(q.entryChan, ShouldHaveLength, 0)
    68  			}
    69  			c.So(in, ShouldEqual, total)
    70  			c.So(out, ShouldEqual, total)
    71  		})
    72  	})
    73  }