Skip to content

Commit

Permalink
Merge pull request #45 from k1LoW/unuse-dg.ctxw
Browse files Browse the repository at this point in the history
doneGroup.ctxw is not used anymore, so remove it.
  • Loading branch information
k1LoW authored Jun 2, 2024
2 parents a04645e + 417675e commit 9c89c33
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 3 additions & 6 deletions donegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ var ErrNotContainDoneGroup = errors.New("donegroup: context does not contain a d

// doneGroup is cleanup function groups per Context.
type doneGroup struct {
cancel context.CancelCauseFunc
// ctxw is the context used to call the cleanup functions
ctxw context.Context
cancel context.CancelCauseFunc
cleanupGroups []*sync.WaitGroup
errors error
mu sync.Mutex
Expand Down Expand Up @@ -178,17 +176,16 @@ func WaitWithContextAndKey(ctx, ctxw context.Context, key any) error {
if !ok {
return ErrNotContainDoneGroup
}
dg.mu.Lock()
dg.ctxw = ctxw
dg.mu.Unlock()
<-ctx.Done()
wg := &sync.WaitGroup{}
for _, g := range dg.cleanupGroups {
wg.Add(1)
dg.mu.Lock()
go func() {
g.Wait()
wg.Done()
}()
dg.mu.Unlock()
}
ch := make(chan struct{})
go func() {
Expand Down
6 changes: 6 additions & 0 deletions donegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func TestNestedWithCancel(t *testing.T) {
thirdCancel()
<-thirdCtx.Done()

mu.Lock()
if firstCleanup != 0 {
t.Error("cleanup function for first called")
}
Expand All @@ -209,6 +210,7 @@ func TestNestedWithCancel(t *testing.T) {
if thirdCleanup != 0 {
t.Error("cleanup function for third called")
}
mu.Unlock()

secondCancel()
<-secondCtx.Done()
Expand All @@ -217,6 +219,7 @@ func TestNestedWithCancel(t *testing.T) {
t.Error(err)
}

mu.Lock()
if thirdCleanup != 3 {
t.Error("cleanup function for third not called")
}
Expand All @@ -226,6 +229,7 @@ func TestNestedWithCancel(t *testing.T) {
if firstCleanup != 0 {
t.Error("cleanup function for first called")
}
mu.Unlock()

firstCancel()
<-firstCtx.Done()
Expand All @@ -234,6 +238,7 @@ func TestNestedWithCancel(t *testing.T) {
t.Error(err)
}

mu.Lock()
if thirdCleanup != 3 {
t.Error("cleanup function for third not called")
}
Expand All @@ -243,6 +248,7 @@ func TestNestedWithCancel(t *testing.T) {
if firstCleanup != 10 {
t.Error("cleanup function for first not called")
}
mu.Unlock()
}()
}

Expand Down

0 comments on commit 9c89c33

Please sign in to comment.