small fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2022-03-25 14:24:20 +03:00
parent 54c4287fab
commit 2d292db7bd
5 changed files with 51 additions and 27 deletions

View File

@@ -2,7 +2,6 @@ package register
import (
"context"
"errors"
"sync"
"time"
@@ -438,7 +437,7 @@ func (m *watcher) Next() (*Result, error) {
return r, nil
}
case <-m.exit:
return nil, errors.New("watcher stopped")
return nil, ErrWatcherStopped
}
}
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
)
@@ -284,29 +285,39 @@ func TestMemoryWildcard(t *testing.T) {
}
func TestWatcher(t *testing.T) {
w := &watcher{
id: "test",
res: make(chan *Result),
exit: make(chan bool),
wo: WatchOptions{
Domain: WildcardDomain,
},
}
testSrv := &Service{Name: "foo", Version: "1.0.0"}
ctx := context.TODO()
m := NewRegister()
m.Init()
m.Connect(ctx)
wc, err := m.Watch(ctx)
if err != nil {
t.Fatalf("cant watch: %v", err)
}
defer wc.Stop()
var wg sync.WaitGroup
wg.Add(1)
go func() {
w.res <- &Result{
Service: &Service{Name: "foo"},
for {
ch, err := wc.Next()
if err != nil {
t.Fatal("unexpected err", err)
}
t.Logf("changes %#+v", ch.Service)
wc.Stop()
wg.Done()
return
}
}()
_, err := w.Next()
if err != nil {
t.Fatal("unexpected err", err)
if err := m.Register(ctx, testSrv); err != nil {
t.Fatalf("Register err: %v", err)
}
w.Stop()
if _, err := w.Next(); err == nil {
wg.Wait()
if _, err := wc.Next(); err == nil {
t.Fatal("expected error on Next()")
}
}