-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_service_test.go
102 lines (90 loc) · 2.14 KB
/
sample_service_test.go
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
package routines_test
import (
"fmt"
"github.com/nofeaturesonlybugs/routines"
)
// SampleService shows how to use the Service and Routines interfaces.
type SampleService struct {
C <-chan int
routines.Service
}
// NewSampleService creates an instance of our service.
func NewSampleService() *SampleService {
rv := &SampleService{}
rv.Service = routines.NewService(rv.start)
return rv
}
// start starts the sample service.
func (me *SampleService) start(routines routines.Routines) error {
fmt.Println("SampleService.start")
defer fmt.Println("SampleService.start returned")
// syncCh is used to prevent start() from returning until me.C has been set below; that
// is the point at which this service is considered "ready."
syncCh := make(chan struct{}, 1)
routines.Go(func() {
fmt.Println("goroutine start")
defer fmt.Println("goroutine returned")
c := make(chan int, 3)
me.C = c
// With me.C assigned we can safely return from start(); closing the channel will do so.
close(syncCh)
c <- 1
c <- 2
c <- 3
for {
select {
case <-routines.Done():
goto done
}
}
done:
me.C = nil
})
// Waiting on the channel to be closed before returning.
<-syncCh
return nil
}
func Example_sampleService() {
fmt.Println("main start")
defer fmt.Println("main returned")
routines := routines.NewRoutines()
defer fmt.Println("wait done")
defer routines.Wait()
defer fmt.Println("waiting")
service := NewSampleService()
err := service.Start(routines)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer fmt.Println("SampleService stopped")
defer service.Stop()
defer fmt.Println("SampleService stopping")
count := 0
for {
select {
case v := <-service.C:
fmt.Println(v)
// After 3 ints we set ch to nil so we'll print no more ints; we also call routines.Stop()
// to shut down this function and the service.
count++
if count == 3 {
goto done
}
}
}
done:
// Output: main start
// SampleService.start
// goroutine start
// SampleService.start returned
// 1
// 2
// 3
// SampleService stopping
// goroutine returned
// SampleService stopped
// waiting
// wait done
// main returned
}