Fix: make selector react immediately
This commit is contained in:
parent
7d51ab5846
commit
94e0e4b000
3 changed files with 22 additions and 2 deletions
|
@ -59,6 +59,7 @@ func (s *Selector) Set(name string) error {
|
||||||
for _, proxy := range getProvidersProxies(s.providers) {
|
for _, proxy := range getProvidersProxies(s.providers) {
|
||||||
if proxy.Name() == name {
|
if proxy.Name() == name {
|
||||||
s.selected = name
|
s.selected = name
|
||||||
|
s.single.Reset()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,10 @@ func (s *Single) Do(fn func() (interface{}, error)) (v interface{}, err error, s
|
||||||
return call.val, call.err, false
|
return call.val, call.err, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Single) Reset() {
|
||||||
|
s.last = time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
func NewSingle(wait time.Duration) *Single {
|
func NewSingle(wait time.Duration) *Single {
|
||||||
return &Single{wait: wait}
|
return &Single{wait: wait}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func TestBasic(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
const n = 10
|
const n = 5
|
||||||
wg.Add(n)
|
wg.Add(n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -33,7 +33,7 @@ func TestBasic(t *testing.T) {
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
assert.Equal(t, 1, foo)
|
assert.Equal(t, 1, foo)
|
||||||
assert.Equal(t, 9, shardCount)
|
assert.Equal(t, 4, shardCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTimer(t *testing.T) {
|
func TestTimer(t *testing.T) {
|
||||||
|
@ -51,3 +51,18 @@ func TestTimer(t *testing.T) {
|
||||||
assert.Equal(t, 1, foo)
|
assert.Equal(t, 1, foo)
|
||||||
assert.True(t, shard)
|
assert.True(t, shard)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReset(t *testing.T) {
|
||||||
|
single := NewSingle(time.Millisecond * 30)
|
||||||
|
foo := 0
|
||||||
|
call := func() (interface{}, error) {
|
||||||
|
foo++
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
single.Do(call)
|
||||||
|
single.Reset()
|
||||||
|
single.Do(call)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, foo)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue