Skip to content

Commit

Permalink
Add a NewContext method to ShutdownOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSnowden committed Dec 14, 2022
1 parent 532c422 commit 9439607
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions common/channel/shutdown_once.go
Expand Up @@ -25,6 +25,8 @@
package channel

import (
"context"
"sync"
"sync/atomic"
)

Expand All @@ -41,11 +43,16 @@ type (
IsShutdown() bool
// Channel for shutdown notification
Channel() <-chan struct{}
// NewContext create a new context which will be canceled when shutdown is called.
// This is useful when creating contexts for background tasks that should be terminated when the service that
// started them is shutdown.
NewContext() (context.Context, context.CancelFunc)
}

ShutdownOnceImpl struct {
status int32
channel chan struct{}
wg sync.WaitGroup
}
)

Expand Down Expand Up @@ -73,3 +80,19 @@ func (c *ShutdownOnceImpl) IsShutdown() bool {
func (c *ShutdownOnceImpl) Channel() <-chan struct{} {
return c.channel
}

// NewContext create a new context which will be canceled when shutdown is called.
// It does this by creating a new context with a cancel function and then calling cancel when shutdown is called.
func (c *ShutdownOnceImpl) NewContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
c.wg.Add(1)
go func() {
defer c.wg.Done()
select {
case <-c.channel:
cancel()
case <-ctx.Done():
}
}()
return ctx, cancel
}
69 changes: 69 additions & 0 deletions common/channel/shutdown_once_test.go
@@ -0,0 +1,69 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package channel

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestShutdownOnceImpl_NewContext(t *testing.T) {
t.Run("context is canceled when shutdown is called", func(t *testing.T) {
so := NewShutdownOnce()
ctx, cancel := so.NewContext()
defer cancel()
select {
case <-ctx.Done():
t.Fatal("ctx should not be done")
default:
}
so.Shutdown()
<-ctx.Done()
err := ctx.Err()
assert.ErrorIs(t, err, context.Canceled)
require.True(t, so.IsShutdown())
select {
case _, ok := <-so.Channel():
assert.False(t, ok, "channel should be closed")
default:
t.Fatal("channel should be closed")
}
})
t.Run("goroutine is not leaked when context is canceled", func(t *testing.T) {
so := NewShutdownOnce()
_, cancel := so.NewContext()
cancel()
so.wg.Wait()
require.False(t, so.IsShutdown(), "should not shutdown when spawned context is canceled")
select {
case <-so.Channel():
t.Fatal("channel should not be closed")
default:
}
})
}

0 comments on commit 9439607

Please sign in to comment.