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
|
package suture
import (
"context"
"fmt"
"testing"
)
const (
JobLimit = 2
)
type IncrementorJob struct {
current int
next chan int
}
func (i *IncrementorJob) Serve(ctx context.Context) error {
for {
select {
case i.next <- i.current + 1:
i.current++
if i.current >= JobLimit {
fmt.Println("Stopping the service")
return ErrDoNotRestart
}
}
}
}
func TestCompleteJob(t *testing.T) {
supervisor := NewSimple("Supervisor")
service := &IncrementorJob{0, make(chan int)}
supervisor.Add(service)
ctx, myCancel := context.WithCancel(context.Background())
supervisor.ServeBackground(ctx)
fmt.Println("Got:", <-service.next)
fmt.Println("Got:", <-service.next)
myCancel()
// Output:
// Got: 1
// Got: 2
// Stopping the service
}
|