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

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