Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
1. Correct argument references in function comments
2. Adjust test cases
  • Loading branch information
leventeliu committed Sep 1, 2021
1 parent e6cd22c commit d6f0d77
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
34 changes: 20 additions & 14 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Controller struct {
wg *sync.WaitGroup
}

// NewController creates a new Controller.
// NewController creates a new goproc Controller.
func NewController(ctx context.Context, name string) *Controller {
child, cancel := context.WithCancel(ctx)
return &Controller{
Expand All @@ -32,7 +32,7 @@ func NewController(ctx context.Context, name string) *Controller {
}
}

// Go initiates a new goroutine for f and gains control on the goroutine through
// Go initiates a new goroutine for g and gains control on the goroutine through
// a context.Context argument.
func (c *Controller) Go(g Goroutine) *Controller {
if err := c.ctx.Err(); err != nil {
Expand All @@ -46,9 +46,9 @@ func (c *Controller) Go(g Goroutine) *Controller {
return c
}

// GoWithRecover initiates a new goroutine for f and gains control on the goroutine
// GoWithRecover initiates a new goroutine for g and gains control on the goroutine
// through a context.Context argument.
// Any panic from f will be captured and handled by h.
// Any panic from g will be captured and handled by rf.
func (c *Controller) GoWithRecover(g Goroutine, rf Recover) *Controller {
if err := c.ctx.Err(); err != nil {
panic(err)
Expand Down Expand Up @@ -93,12 +93,15 @@ func (c *Controller) WithDeadline(deadline time.Time) *Controller {
if err := c.ctx.Err(); err != nil {
panic(err)
}
var child, _ = context.WithDeadline(c.ctx, deadline)
var child, cancel = context.WithDeadline(c.ctx, deadline)
return &Controller{
name: c.name,
ctx: child,
cancel: c.cancel,
wg: c.wg,
name: c.name,
ctx: child,
cancel: func() {
cancel()
c.cancel()
},
wg: c.wg,
}
}

Expand All @@ -111,12 +114,15 @@ func (c *Controller) WithTimeout(timeout time.Duration) *Controller {
if err := c.ctx.Err(); err != nil {
panic(err)
}
var child, _ = context.WithTimeout(c.ctx, timeout)
var child, cancel = context.WithTimeout(c.ctx, timeout)
return &Controller{
name: c.name,
ctx: child,
cancel: c.cancel,
wg: c.wg,
name: c.name,
ctx: child,
cancel: func() {
cancel()
c.cancel()
},
wg: c.wg,
}
}

Expand Down
4 changes: 4 additions & 0 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ func TestBackgroundController(t *testing.T) {
ctrl.WithTimeout(3 * time.Second).
Go(hangingAround).
Wait()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test shutdown", func() {
ctrl.Go(hangingAround).Shutdown()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test passing value", func() {
ctrl.WithDeadline(time.Now().Add(3*time.Second)).
WithValue(hangingAroundKey1, "Let's play!").
Go(hangingAround).
Wait()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test passing value with anonymous key", func() {
ctrl.WithTimeout(3*time.Second).
WithValue(&testKeyType{}, "panic").
Go(hangingAround).
Wait() // should not receive key
So(ctrl.Die(), ShouldBeTrue)
})
})
}
8 changes: 4 additions & 4 deletions timeout_chan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ func TestTimeoutChanStarving(t *testing.T) {
func TestTimeoutChanChaos(t *testing.T) {
Convey("Test chaos", t, func(c C) {
const (
testConcurrency = 100
testRounds = 100
testConcurrency = 10
testRounds = 10
)
var (
tc = NewTimeoutChan(context.Background(), 100*time.Millisecond, 0)
Expand All @@ -264,9 +264,9 @@ func TestTimeoutChanChaos(t *testing.T) {
RandDurationRange: 10 * time.Second,
}
writeCtrl := NewController(context.Background(), t.Name()+"-W")
for i := 0; i < 100; i++ {
for i := 0; i < testConcurrency; i++ {
writeCtrl.Go(func(ctx context.Context) {
for i := 0; i < 100; i++ {
for i := 0; i < testRounds; i++ {
time.Sleep(time.Duration(rand.Int63()) % (1 * time.Second))
in := factory.NewRandTestDeadliner()
tc.In <- in
Expand Down

0 comments on commit d6f0d77

Please sign in to comment.