github.com/mundipagg/tracer-splunk-writer@v1.0.6/buffer/buffer_test.go (about)

     1  package buffer
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestBuffer_Write(t *testing.T) {
    13  	t.Parallel()
    14  	t.Run("when the buffer is not full", func(t *testing.T) {
    15  		t.Parallel()
    16  		is := assert.New(t)
    17  		subject := &buffer{
    18  			Locker: &sync.Mutex{},
    19  			size:   0,
    20  			cap:    10,
    21  			items:  make([]interface{}, 10),
    22  			chunks: make(chan entry, 10),
    23  		}
    24  		subject.Write("something")
    25  		is.Equal(1, subject.size, "it should increment the size of the buffer in one unit")
    26  		is.Equal([]interface{}{"something", nil, nil, nil, nil, nil, nil, nil, nil, nil}, subject.items, "it should change the buffer's inner slice")
    27  	})
    28  	t.Run("when the buffer is full", func(t *testing.T) {
    29  		t.Parallel()
    30  		is := assert.New(t)
    31  		subject := &buffer{
    32  			Locker: &sync.Mutex{},
    33  			size:   0,
    34  			cap:    1,
    35  			items:  make([]interface{}, 1),
    36  			chunks: make(chan entry, 10),
    37  		}
    38  		subject.Write("something")
    39  		is.Equal(0, subject.size, "it should remain zero")
    40  		is.Equal([]interface{}{nil}, subject.items, "it should clean the buffer's inner slice")
    41  		timeout := time.NewTimer(10 * time.Millisecond)
    42  		select {
    43  		case actual := <-subject.chunks:
    44  			is.Equal(entry{items: []interface{}{"something"}, retries: 10}, actual, "it should read the expected slice")
    45  		case <-timeout.C:
    46  			is.Fail("nothing was published")
    47  		}
    48  	})
    49  }
    50  
    51  func TestNew(t *testing.T) {
    52  	t.Parallel()
    53  	t.Run("when the buffer expires", func(t *testing.T) {
    54  		t.Parallel()
    55  		t.Run("but the consumer returns an error", func(t *testing.T) {
    56  			t.Parallel()
    57  			is := assert.New(t)
    58  			called := make(chan []interface{}, 1)
    59  			err := errors.New("")
    60  			subject := New(Config{
    61  				OnOverflow: func(items []interface{}) error {
    62  					called <- items
    63  					err1 := err
    64  					err = nil
    65  					return err1
    66  				},
    67  				BackOff:    10 * time.Millisecond,
    68  				Expiration: 10 * time.Millisecond,
    69  				Cap:        10,
    70  				OnWait:     10,
    71  			})
    72  			subject.Write(1)
    73  			subject.Write(2)
    74  			subject.Write(3)
    75  			timeout := time.NewTimer(20 * time.Millisecond)
    76  			select {
    77  			case items := <-called:
    78  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
    79  			case <-timeout.C:
    80  				is.Fail("nothing was published")
    81  			}
    82  			timeout = time.NewTimer(20 * time.Millisecond)
    83  			select {
    84  			case items := <-called:
    85  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
    86  			case <-timeout.C:
    87  				is.Fail("nothing was published")
    88  			}
    89  		})
    90  		t.Run("and the consumer returns no error", func(t *testing.T) {
    91  			t.Parallel()
    92  			is := assert.New(t)
    93  			called := make(chan []interface{}, 1)
    94  			subject := New(Config{
    95  				OnOverflow: func(items []interface{}) error {
    96  					called <- items
    97  					return nil
    98  				},
    99  				BackOff:    10 * time.Millisecond,
   100  				Expiration: 10 * time.Millisecond,
   101  				Cap:        10,
   102  				OnWait:     10,
   103  			})
   104  			subject.Write(1)
   105  			subject.Write(2)
   106  			subject.Write(3)
   107  			timeout := time.NewTimer(20 * time.Millisecond)
   108  			select {
   109  			case items := <-called:
   110  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
   111  			case <-timeout.C:
   112  				is.Fail("nothing was published")
   113  			}
   114  		})
   115  
   116  	})
   117  	t.Run("when the buffer overflow", func(t *testing.T) {
   118  		t.Run("but the consumer returns an error", func(t *testing.T) {
   119  			t.Parallel()
   120  			is := assert.New(t)
   121  			called := make(chan []interface{}, 1)
   122  			err := errors.New("")
   123  			subject := New(Config{
   124  				OnOverflow: func(items []interface{}) error {
   125  					called <- items
   126  					err1 := err
   127  					err = nil
   128  					return err1
   129  				},
   130  				BackOff:    10 * time.Millisecond,
   131  				Expiration: 100 * time.Millisecond,
   132  				Cap:        3,
   133  				OnWait:     10,
   134  			})
   135  			subject.Write(1)
   136  			subject.Write(2)
   137  			subject.Write(3)
   138  			timeout := time.NewTimer(20 * time.Millisecond)
   139  			select {
   140  			case items := <-called:
   141  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
   142  			case <-timeout.C:
   143  				is.Fail("nothing was published")
   144  			}
   145  			timeout = time.NewTimer(20 * time.Millisecond)
   146  			select {
   147  			case items := <-called:
   148  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
   149  			case <-timeout.C:
   150  				is.Fail("nothing was published")
   151  			}
   152  		})
   153  		t.Run("and the consumer returns no error", func(t *testing.T) {
   154  			t.Parallel()
   155  			is := assert.New(t)
   156  			called := make(chan []interface{}, 1)
   157  			subject := New(Config{
   158  				OnOverflow: func(items []interface{}) error {
   159  					called <- items
   160  					return nil
   161  				},
   162  				BackOff:    10 * time.Millisecond,
   163  				Expiration: 100 * time.Millisecond,
   164  				Cap:        3,
   165  				OnWait:     10,
   166  			})
   167  			subject.Write(1)
   168  			subject.Write(2)
   169  			subject.Write(3)
   170  			timeout := time.NewTimer(20 * time.Millisecond)
   171  			select {
   172  			case items := <-called:
   173  				is.Equal([]interface{}{1, 2, 3}, items, "it should return the expected array")
   174  			case <-timeout.C:
   175  				is.Fail("nothing was published")
   176  			}
   177  		})
   178  	})
   179  }