File: unbounded_executor_test.go

package info (click to toggle)
golang-github-modern-go-concurrent 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, experimental
  • size: 104 kB
  • sloc: sh: 9; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,145 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
package concurrent_test

import (
	"context"
	"fmt"
	"time"
	"github.com/modern-go/concurrent"
)

func ExampleUnboundedExecutor_Go() {
	executor := concurrent.NewUnboundedExecutor()
	executor.Go(func(ctx context.Context) {
		fmt.Println("abc")
	})
	time.Sleep(time.Second)
	// output: abc
}

func ExampleUnboundedExecutor_StopAndWaitForever() {
	executor := concurrent.NewUnboundedExecutor()
	executor.Go(func(ctx context.Context) {
		everyMillisecond := time.NewTicker(time.Millisecond)
		for {
			select {
			case <-ctx.Done():
				fmt.Println("goroutine exited")
				return
			case <-everyMillisecond.C:
				// do something
			}
		}
	})
	time.Sleep(time.Second)
	executor.StopAndWaitForever()
	fmt.Println("executor stopped")
	// output:
	// goroutine exited
	// executor stopped
}

func ExampleUnboundedExecutor_Go_panic() {
	concurrent.HandlePanic = func(recovered interface{}, funcName string) {
		fmt.Println(funcName)
	}
	executor := concurrent.NewUnboundedExecutor()
	executor.Go(willPanic)
	time.Sleep(time.Second)
	// output:
	// github.com/modern-go/concurrent_test.willPanic
}

func willPanic(ctx context.Context) {
	panic("!!!")
}