File: batcher_test.go

package info (click to toggle)
golang-gopkg-eapache-go-resiliency.v1 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: makefile: 2
file content (175 lines) | stat: -rw-r--r-- 3,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package batcher

import (
	"errors"
	"sync"
	"sync/atomic"
	"testing"
	"time"
)

var errSomeError = errors.New("errSomeError")

func returnsError(params []interface{}) error {
	return errSomeError
}

func returnsSuccess(params []interface{}) error {
	return nil
}

func TestBatcherSuccess(t *testing.T) {
	b := New(10*time.Millisecond, returnsSuccess)

	wg := &sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			if err := b.Run(nil); err != nil {
				t.Error(err)
			}
			wg.Done()
		}()
	}
	wg.Wait()

	b = New(0, returnsSuccess)
	for i := 0; i < 10; i++ {
		if err := b.Run(nil); err != nil {
			t.Error(err)
		}
	}
}

func TestShutdownSuccess(t *testing.T) {
	sleepDuration := 5 * time.Millisecond
	durationLimit := 2 * sleepDuration
	timeout := 2 * durationLimit
	total := 0
	doSum := func(params []interface{}) error {
		for _, param := range params {
			intValue, ok := param.(int)
			if !ok {
				t.Error("expected type int")
			}
			total += intValue
		}
		return nil
	}

	b := New(timeout, doSum)
	go func() {
		time.Sleep(sleepDuration)
		b.Shutdown(true)
	}()

	wg := &sync.WaitGroup{}
	expectedTotal := 0
	start := time.Now()
	for i := 0; i < 10; i++ {
		expectedTotal += i
		wg.Add(1)
		go func(i int) {
			if err := b.Run(i); err != nil {
				t.Error(err)
			}
			wg.Done()
		}(i)
	}
	wg.Wait()

	duration := time.Since(start)
	if duration >= durationLimit {
		t.Errorf("expected duration[%v] < durationLimit[%v]", duration, durationLimit)
	}

	if total != expectedTotal {
		t.Errorf("expected processed count[%v] < actual[%v]", expectedTotal, total)
	}
}

func TestShutdownEmpty(t *testing.T) {
	b := New(10*time.Millisecond, returnsSuccess)
	b.Shutdown(true)
}

func TestBatcherError(t *testing.T) {
	b := New(10*time.Millisecond, returnsError)

	wg := &sync.WaitGroup{}
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func() {
			if err := b.Run(nil); err != errSomeError {
				t.Error(err)
			}
			wg.Done()
		}()
	}
	wg.Wait()
}

func TestBatcherPrefilter(t *testing.T) {
	b := New(1*time.Millisecond, returnsSuccess)

	b.Prefilter(func(param interface{}) error {
		if param == nil {
			return errSomeError
		}
		return nil
	})

	if err := b.Run(nil); err != errSomeError {
		t.Error(err)
	}

	if err := b.Run(1); err != nil {
		t.Error(err)
	}
}

func TestBatcherMultipleBatches(t *testing.T) {
	var iters uint32

	b := New(10*time.Millisecond, func(params []interface{}) error {
		atomic.AddUint32(&iters, 1)
		return nil
	})

	wg := &sync.WaitGroup{}

	for group := 0; group < 5; group++ {
		for i := 0; i < 10; i++ {
			wg.Add(1)
			go func() {
				if err := b.Run(nil); err != nil {
					t.Error(err)
				}
				wg.Done()
			}()
		}
		time.Sleep(15 * time.Millisecond)
	}

	wg.Wait()

	if iters != 5 {
		t.Error("Wrong number of iters:", iters)
	}
}

func ExampleBatcher() {
	b := New(10*time.Millisecond, func(params []interface{}) error {
		// do something with the batch of parameters
		return nil
	})

	b.Prefilter(func(param interface{}) error {
		// do some sort of sanity check on the parameter, and return an error if it fails
		return nil
	})

	for i := 0; i < 10; i++ {
		go b.Run(i)
	}
}