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.

56 lines
872 B

4 years ago
package watcher_test
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitoa.ru/go-4devs/config"
"gitoa.ru/go-4devs/config/provider/watcher"
"gitoa.ru/go-4devs/config/value"
)
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++
return value.JString(fmt.Sprint(p.cnt)), nil
4 years ago
}
func TestWatcher(t *testing.T) {
t.Parallel()
ctx := context.Background()
prov := &provider{}
w := watcher.New(time.Second, prov)
wg := sync.WaitGroup{}
wg.Add(2)
var cnt int32
err := w.Watch(
ctx,
func(ctx context.Context, oldVar, newVar config.Value) {
4 years ago
atomic.AddInt32(&cnt, 1)
wg.Done()
},
"tmpname",
4 years ago
)
require.NoError(t, err)
wg.Wait()
require.Equal(t, int32(2), cnt)
}