-
Notifications
You must be signed in to change notification settings - Fork 0
/
failing_service_test.go
62 lines (53 loc) · 1.27 KB
/
failing_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
package routines_test
import (
"fmt"
"github.com/nofeaturesonlybugs/routines"
)
// FailingService shows a service failing to start.
type FailingService struct {
C <-chan int
routines.Service
}
// NewFailingService creates an instance of our service.
func NewFailingService() *FailingService {
rv := &FailingService{}
// Passing nil to NewService() will cause the call to Start() to return an error.
rv.Service = routines.NewService(nil)
return rv
}
func Example_failingService() {
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 := NewFailingService()
err := service.Start(routines)
if err != nil {
fmt.Println("Error starting service")
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
// Error starting service
// waiting
// wait done
// main returned
}