@@ -2,6 +2,7 @@ package ctxwatch
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ContextWatcher watches a context and performs an action when the context is canceled. It can watch one context at a
|
// ContextWatcher watches a context and performs an action when the context is canceled. It can watch one context at a
|
||||||
@@ -10,8 +11,10 @@ type ContextWatcher struct {
|
|||||||
onCancel func()
|
onCancel func()
|
||||||
onUnwatchAfterCancel func()
|
onUnwatchAfterCancel func()
|
||||||
unwatchChan chan struct{}
|
unwatchChan chan struct{}
|
||||||
watchInProgress bool
|
|
||||||
onCancelWasCalled bool
|
lock sync.Mutex
|
||||||
|
watchInProgress bool
|
||||||
|
onCancelWasCalled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContextWatcher returns a ContextWatcher. onCancel will be called when a watched context is canceled.
|
// NewContextWatcher returns a ContextWatcher. onCancel will be called when a watched context is canceled.
|
||||||
@@ -29,6 +32,9 @@ func NewContextWatcher(onCancel func(), onUnwatchAfterCancel func()) *ContextWat
|
|||||||
|
|
||||||
// Watch starts watching ctx. If ctx is canceled then the onCancel function passed to NewContextWatcher will be called.
|
// Watch starts watching ctx. If ctx is canceled then the onCancel function passed to NewContextWatcher will be called.
|
||||||
func (cw *ContextWatcher) Watch(ctx context.Context) {
|
func (cw *ContextWatcher) Watch(ctx context.Context) {
|
||||||
|
cw.lock.Lock()
|
||||||
|
defer cw.lock.Unlock()
|
||||||
|
|
||||||
if cw.watchInProgress {
|
if cw.watchInProgress {
|
||||||
panic("Watch already in progress")
|
panic("Watch already in progress")
|
||||||
}
|
}
|
||||||
@@ -54,6 +60,9 @@ func (cw *ContextWatcher) Watch(ctx context.Context) {
|
|||||||
// Unwatch stops watching the previously watched context. If the onCancel function passed to NewContextWatcher was
|
// Unwatch stops watching the previously watched context. If the onCancel function passed to NewContextWatcher was
|
||||||
// called then onUnwatchAfterCancel will also be called.
|
// called then onUnwatchAfterCancel will also be called.
|
||||||
func (cw *ContextWatcher) Unwatch() {
|
func (cw *ContextWatcher) Unwatch() {
|
||||||
|
cw.lock.Lock()
|
||||||
|
defer cw.lock.Unlock()
|
||||||
|
|
||||||
if cw.watchInProgress {
|
if cw.watchInProgress {
|
||||||
cw.unwatchChan <- struct{}{}
|
cw.unwatchChan <- struct{}{}
|
||||||
if cw.onCancelWasCalled {
|
if cw.onCancelWasCalled {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func TestContextWatcherMultipleWatchPanics(t *testing.T) {
|
|||||||
require.Panics(t, func() { cw.Watch(ctx2) }, "Expected panic when Watch called multiple times")
|
require.Panics(t, func() { cw.Watch(ctx2) }, "Expected panic when Watch called multiple times")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextWatcherUnwatchIsAlwaysSafe(t *testing.T) {
|
func TestContextWatcherUnwatchWhenNotWatchingIsSafe(t *testing.T) {
|
||||||
cw := ctxwatch.NewContextWatcher(func() {}, func() {})
|
cw := ctxwatch.NewContextWatcher(func() {}, func() {})
|
||||||
cw.Unwatch() // unwatch when not / never watching
|
cw.Unwatch() // unwatch when not / never watching
|
||||||
|
|
||||||
@@ -70,6 +70,19 @@ func TestContextWatcherUnwatchIsAlwaysSafe(t *testing.T) {
|
|||||||
cw.Unwatch() // double unwatch
|
cw.Unwatch() // double unwatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextWatcherUnwatchIsConcurrencySafe(t *testing.T) {
|
||||||
|
cw := ctxwatch.NewContextWatcher(func() {}, func() {})
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
cw.Watch(ctx)
|
||||||
|
|
||||||
|
go cw.Unwatch()
|
||||||
|
go cw.Unwatch()
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextWatcherStress(t *testing.T) {
|
func TestContextWatcherStress(t *testing.T) {
|
||||||
var cancelFuncCalls int64
|
var cancelFuncCalls int64
|
||||||
var cleanupFuncCalls int64
|
var cleanupFuncCalls int64
|
||||||
|
|||||||
Reference in New Issue
Block a user