File: helpers_test.go

package info (click to toggle)
golang-github-ibm-sarama 1.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: makefile: 35; sh: 19
file content (87 lines) | stat: -rw-r--r-- 1,446 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
package sarama

import (
	"io"
	"strconv"
	"sync"
	"testing"
	"time"
)

func safeClose(t testing.TB, c io.Closer) {
	t.Helper()
	err := c.Close()
	if err != nil {
		t.Error(err)
	}
}

func closeProducerWithTimeout(t *testing.T, p AsyncProducer, timeout time.Duration) {
	var wg sync.WaitGroup
	p.AsyncClose()

	closer := make(chan struct{})
	timer := time.AfterFunc(timeout, func() {
		t.Error("timeout")
		close(closer)
	})
	defer timer.Stop()

	wg.Add(2)
	go func() {
		defer wg.Done()
		for {
			select {
			case <-closer:
				return
			case _, ok := <-p.Successes():
				if !ok {
					return
				}
				t.Error("Unexpected message on Successes()")
			}
		}
	}()
	go func() {
		defer wg.Done()
		for {
			select {
			case <-closer:
				return
			case msg, ok := <-p.Errors():
				if !ok {
					return
				}
				t.Error(msg.Err)
			}
		}
	}()
	wg.Wait()
}

func closeProducer(t *testing.T, p AsyncProducer) {
	closeProducerWithTimeout(t, p, 5*time.Minute)
}

const TestMessage = "ABC THE MESSAGE"

type appendInterceptor struct {
	i int
}

func (b *appendInterceptor) OnSend(msg *ProducerMessage) {
	if b.i < 0 {
		panic("hey, the interceptor has failed")
	}
	v, _ := msg.Value.Encode()
	msg.Value = StringEncoder(string(v) + strconv.Itoa(b.i))
	b.i++
}

func (b *appendInterceptor) OnConsume(msg *ConsumerMessage) {
	if b.i < 0 {
		panic("hey, the interceptor has failed")
	}
	msg.Value = []byte(string(msg.Value) + strconv.Itoa(b.i))
	b.i++
}