File: rungroup_test.go

package info (click to toggle)
golang-github-segmentio-kafka-go 0.2.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 572 kB
  • sloc: makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,157 bytes parent folder | download | duplicates (2)
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
package kafka

import (
	"context"
	"testing"
	"time"
)

func TestRunGroup(t *testing.T) {
	t.Run("Wait returns on empty group", func(t *testing.T) {
		rg := &runGroup{}
		rg.Wait()
	})

	t.Run("Stop returns on empty group", func(t *testing.T) {
		rg := &runGroup{}
		rg.Stop()
	})

	t.Run("Stop cancels running tasks", func(t *testing.T) {
		rg := &runGroup{}
		rg.Go(func(stop <-chan struct{}) {
			<-stop
		})
		rg.Stop()
	})

	t.Run("Honors parent context", func(t *testing.T) {
		now := time.Now()
		timeout := time.Millisecond * 100

		ctx, cancel := context.WithTimeout(context.Background(), timeout)
		defer cancel()

		rg := &runGroup{}
		rg = rg.WithContext(ctx)
		rg.Go(func(stop <-chan struct{}) {
			<-stop
		})
		rg.Wait()

		elapsed := time.Now().Sub(now)
		if elapsed < timeout {
			t.Errorf("expected elapsed > %v; got %v", timeout, elapsed)
		}
	})

	t.Run("Any death kills all; one for all and all for one", func(t *testing.T) {
		rg := &runGroup{}
		rg.Go(func(stop <-chan struct{}) {
			<-stop
		})
		rg.Go(func(stop <-chan struct{}) {
			<-stop
		})
		rg.Go(func(stop <-chan struct{}) {
			// return immediately
		})
		rg.Wait()
	})
}