You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.1 KiB

3 years ago
package watcher_test
import (
"context"
5 months ago
"strconv"
3 years ago
"sync"
"sync/atomic"
"testing"
"time"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/watcher"
5 months ago
"gitoa.ru/go-4devs/config/test/require"
3 years ago
"gitoa.ru/go-4devs/config/value"
)
5 months ago
var _ config.Provider = (*provider)(nil)
3 years ago
type provider struct {
cnt int32
}
func (p *provider) Name() string {
return "test"
}
func (p *provider) Value(context.Context, ...string) (config.Value, error) {
3 years ago
p.cnt++
5 months ago
return value.JString(strconv.Itoa(int(p.cnt))), nil
3 years ago
}
func TestWatcher(t *testing.T) {
t.Parallel()
5 months ago
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer func() {
cancel()
}()
3 years ago
prov := &provider{}
w := watcher.New(time.Second, prov)
wg := sync.WaitGroup{}
wg.Add(2)
var cnt int32
err := w.Watch(
ctx,
5 months ago
func(ctx context.Context, oldVar, newVar config.Value) error {
3 years ago
atomic.AddInt32(&cnt, 1)
wg.Done()
5 months ago
if atomic.LoadInt32(&cnt) == 2 {
return config.ErrStopWatch
}
return nil
3 years ago
},
"tmpname",
3 years ago
)
5 months ago
3 years ago
wg.Wait()
5 months ago
require.NoError(t, err)
3 years ago
require.Equal(t, int32(2), cnt)
}