This commit is contained in:
1
error.go
1
error.go
@@ -7,4 +7,5 @@ var (
|
||||
ErrInvalidValue = errors.New("invalid value")
|
||||
ErrUnknowType = errors.New("unknow type")
|
||||
ErrInitFactory = errors.New("init factory")
|
||||
ErrStopWatch = errors.New("stop watch")
|
||||
)
|
||||
|
||||
@@ -11,7 +11,7 @@ type NamedProvider interface {
|
||||
Provider
|
||||
}
|
||||
|
||||
type WatchCallback func(ctx context.Context, oldVar, newVar Value)
|
||||
type WatchCallback func(ctx context.Context, oldVar, newVar Value) error
|
||||
|
||||
type WatchProvider interface {
|
||||
Watch(ctx context.Context, callback WatchCallback, path ...string) error
|
||||
|
||||
@@ -2,6 +2,7 @@ package watcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -61,7 +62,13 @@ func (p *Provider) Watch(ctx context.Context, callback config.WatchCallback, key
|
||||
if err != nil {
|
||||
p.logger(ctx, "get value%v:%v", key, err.Error())
|
||||
} else if !newVar.IsEquals(oldVar) {
|
||||
callback(ctx, oldVar, newVar)
|
||||
if err := callback(ctx, oldVar, newVar); err != nil {
|
||||
if errors.Is(err, config.ErrStopWatch) {
|
||||
return
|
||||
}
|
||||
p.logger(ctx, "callback %v:%v", key, err)
|
||||
|
||||
}
|
||||
oldVar = newVar
|
||||
}
|
||||
case <-ctx.Done():
|
||||
|
||||
@@ -31,7 +31,11 @@ func (p *provider) Value(context.Context, ...string) (config.Value, error) {
|
||||
func TestWatcher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
|
||||
defer func() {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
prov := &provider{}
|
||||
|
||||
w := watcher.New(time.Second, prov)
|
||||
@@ -42,14 +46,19 @@ func TestWatcher(t *testing.T) {
|
||||
|
||||
err := w.Watch(
|
||||
ctx,
|
||||
func(ctx context.Context, oldVar, newVar config.Value) {
|
||||
func(ctx context.Context, oldVar, newVar config.Value) error {
|
||||
atomic.AddInt32(&cnt, 1)
|
||||
wg.Done()
|
||||
if atomic.LoadInt32(&cnt) == 2 {
|
||||
return config.ErrStopWatch
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
"tmpname",
|
||||
)
|
||||
require.NoError(t, err)
|
||||
wg.Wait()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int32(2), cnt)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user