-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge stopcontrol + breaker packages (#1485)
* Merge stopcontrol + breaker packages * lint
- Loading branch information
Showing
6 changed files
with
83 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
package stopcontrol | ||
|
||
import "sync/atomic" | ||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// StopControl allows for any instance to thread-safely check if the status is stopping or not | ||
type StopControl struct { | ||
stop *int32 | ||
stop *int32 | ||
stopChan chan interface{} | ||
closer sync.Once | ||
} | ||
|
||
func New() *StopControl { | ||
return &StopControl{ | ||
stop: new(int32), | ||
stop: new(int32), | ||
stopChan: make(chan interface{}), | ||
} | ||
} | ||
|
||
func (s *StopControl) Stop() { | ||
atomic.StoreInt32(s.stop, 1) | ||
s.closer.Do(func() { | ||
atomic.StoreInt32(s.stop, 1) | ||
close(s.stopChan) | ||
}) | ||
} | ||
|
||
func (s *StopControl) IsStopping() bool { | ||
return atomic.LoadInt32(s.stop) == 1 | ||
} | ||
|
||
func (s *StopControl) Done() chan interface{} { | ||
return s.stopChan | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package stopcontrol | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestStopControl_Stop(t *testing.T) { | ||
sc := New() | ||
sc.Stop() | ||
|
||
if !sc.IsStopping() { | ||
t.Error("Expected IsStopping to return true after Stop, but got false") | ||
} | ||
|
||
// Ensure it's safe to call Stop multiple times | ||
func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
t.Error("Expected no panic when calling Stop multiple times") | ||
} | ||
}() | ||
sc.Stop() | ||
}() | ||
} | ||
|
||
func TestStopControl_IsStopping(t *testing.T) { | ||
sc := New() | ||
|
||
if sc.IsStopping() { | ||
t.Error("Expected IsStopping to return false initially, but got true") | ||
} | ||
|
||
sc.Stop() | ||
|
||
if !sc.IsStopping() { | ||
t.Error("Expected IsStopping to return true after Stop, but got false") | ||
} | ||
} | ||
|
||
func TestStopControl_Done(t *testing.T) { | ||
sc := New() | ||
|
||
select { | ||
case <-sc.Done(): | ||
t.Error("Expected Done channel to be blocking initially") | ||
case <-time.After(50 * time.Millisecond): // Allow a small delay to check the non-blocking state | ||
} | ||
|
||
sc.Stop() | ||
|
||
select { | ||
case _, ok := <-sc.Done(): | ||
if ok { | ||
t.Error("Expected Done channel to be closed after Stop") | ||
} | ||
case <-time.After(50 * time.Millisecond): | ||
t.Error("Expected Done channel to be closed immediately after Stop") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters