Skip to content

Commit d5d1642

Browse files
committed
✨ further features
1 parent 81abfd7 commit d5d1642

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

utils/parallelisation/cancel_functions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ func (s *CancelFunctionStore) RegisterCancelFunction(cancel ...context.CancelFun
1515
s.ExecutionGroup.RegisterFunction(cancel...)
1616
}
1717

18+
func (s *CancelFunctionStore) RegisterCancelStore(store *CancelFunctionStore) {
19+
if store == nil {
20+
return
21+
}
22+
s.RegisterCancelFunction(func() {
23+
store.Cancel()
24+
})
25+
}
26+
1827
// Cancel will execute the cancel functions in the store. Any errors will be ignored and Execute() is recommended if you need to know if a cancellation failed
1928
func (s *CancelFunctionStore) Cancel() {
2029
_ = s.Execute(context.Background())

utils/parallelisation/cancel_functions_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13+
"go.uber.org/atomic"
1314

1415
"github.com/ARM-software/golang-utils/utils/commonerrors"
1516
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
@@ -19,24 +20,35 @@ func testCancelStore(t *testing.T, store *CancelFunctionStore) {
1920
t.Helper()
2021
require.NotNil(t, store)
2122
// Set up some fake CancelFuncs to make sure they are called
22-
called1 := false
23-
called2 := false
23+
called1 := atomic.NewBool(false)
24+
called2 := atomic.NewBool(false)
25+
called3 := atomic.NewBool(false)
26+
2427
cancelFunc1 := func() {
25-
called1 = true
28+
called1.Store(true)
2629
}
2730
cancelFunc2 := func() {
28-
called2 = true
31+
called2.Store(true)
32+
}
33+
cancelFunc3 := func() {
34+
called3.Store(true)
2935
}
36+
subStore := NewCancelFunctionsStore()
37+
subStore.RegisterCancelFunction(cancelFunc3)
3038

3139
store.RegisterCancelFunction(cancelFunc1, cancelFunc2)
40+
store.RegisterCancelStore(subStore)
41+
store.RegisterCancelStore(nil)
3242

33-
assert.Equal(t, 2, store.Len())
34-
assert.False(t, called1)
35-
assert.False(t, called2)
43+
assert.Equal(t, 3, store.Len())
44+
assert.False(t, called1.Load())
45+
assert.False(t, called2.Load())
46+
assert.False(t, called3.Load())
3647
store.Cancel()
3748

38-
assert.True(t, called1)
39-
assert.True(t, called2)
49+
assert.True(t, called1.Load())
50+
assert.True(t, called2.Load())
51+
assert.True(t, called3.Load())
4052
}
4153

4254
// Given a CancelFunctionsStore

0 commit comments

Comments
 (0)