discovery: change Advertise() to return done channel

  Advertising is stopped when the given context done, but there is no way
  to know when it is actually stopped. This makes difficult to update
  the existing advertisement since it should be stopped before
  advertising a new one.

  This CL change Advertise() to return done channel which will be closed
  when it is stopped.

MultiPart: 3/3
Change-Id: I1c7b2dd554b0d47c4d79138267a52966b6eb83f5
diff --git a/go/src/vanadium/discovery/internal/discovery.go b/go/src/vanadium/discovery/internal/discovery.go
index 59da5fa..0512875 100644
--- a/go/src/vanadium/discovery/internal/discovery.go
+++ b/go/src/vanadium/discovery/internal/discovery.go
@@ -7,13 +7,13 @@
 import (
 	"sync"
 
-	"v.io/v23/context"
-	"v.io/v23/discovery"
-	"v.io/v23/verror"
-
 	"mojo/public/go/bindings"
 	mojom "mojom/vanadium/discovery"
+
+	"v.io/v23/context"
+	"v.io/v23/discovery"
 	"v.io/v23/security"
+	"v.io/v23/verror"
 )
 
 type id uint32
@@ -73,14 +73,17 @@
 	for i, pattern := range patterns {
 		perms[i] = security.BlessingPattern(pattern)
 	}
-	err := d.s.Advertise(ctx, vService, perms)
+	done, err := d.s.Advertise(ctx, vService, perms)
 	if err != nil {
 		cancel()
 		return 0, v2mError(err), nil
 	}
 	d.mu.Lock()
 	currId := d.nextAdv
-	d.activeAdvs[currId] = cancel
+	d.activeAdvs[currId] = func() {
+		cancel()
+		<-done
+	}
 	d.nextAdv += 2
 	d.mu.Unlock()
 	return uint32(currId), nil, nil
@@ -88,11 +91,11 @@
 
 func (d *DiscoveryService) stopAdvertising(handle uint32) error {
 	d.mu.Lock()
-	cancel := d.activeAdvs[id(handle)]
+	stop := d.activeAdvs[id(handle)]
 	delete(d.activeAdvs, id(handle))
 	d.mu.Unlock()
-	if cancel != nil {
-		cancel()
+	if stop != nil {
+		stop()
 	}
 	return nil
 }
@@ -137,7 +140,7 @@
 
 // Stop stops the scan.
 func (d *DiscoveryService) Stop(handle uint32) error {
-	if handle % 2 == 0 {
+	if handle%2 == 0 {
 		return d.stopScan(handle)
 	}
 	return d.stopAdvertising(handle)
diff --git a/go/src/vanadium/discovery/internal/discovery_test.go b/go/src/vanadium/discovery/internal/discovery_test.go
index 585eb48..f2c9783 100644
--- a/go/src/vanadium/discovery/internal/discovery_test.go
+++ b/go/src/vanadium/discovery/internal/discovery_test.go
@@ -6,19 +6,20 @@
 
 import (
 	"reflect"
+	"sync"
 	"testing"
 
+	"third_party/go/tool/android_arm/src/fmt"
+
+	mojom "mojom/vanadium/discovery"
+
 	"v.io/v23/context"
 	"v.io/v23/discovery"
-	idiscovery "v.io/x/ref/lib/discovery"
-	vtest "v.io/x/ref/test"
-
-	_ "v.io/x/ref/runtime/factories/generic"
-
-	mojom "mojom/vanadium/discovery"
-	"sync"
-	"third_party/go/tool/android_arm/src/fmt"
 	"v.io/v23/security"
+
+	idiscovery "v.io/x/ref/lib/discovery"
+	_ "v.io/x/ref/runtime/factories/generic"
+	vtest "v.io/x/ref/test"
 )
 
 type mockAdv struct {
@@ -34,20 +35,23 @@
 	deleteCh chan struct{}
 }
 
-func (d *discoveryMock) Advertise(ctx *context.T, s discovery.Service, perms []security.BlessingPattern) error {
+func (d *discoveryMock) Advertise(ctx *context.T, s discovery.Service, perms []security.BlessingPattern) (<-chan struct{}, error) {
 	d.mu.Lock()
 	currId := d.id
 	d.services[currId] = s
 	d.id++
 	d.mu.Unlock()
-	c := func() {
+	done := make(chan struct{})
+	stop := func() {
 		d.mu.Lock()
 		delete(d.services, currId)
 		d.mu.Unlock()
-		d.deleteCh <- struct{}{}
+		close(done)
+
+		go func() { d.deleteCh <- struct{}{} }()
 	}
-	d.trigger.Add(c, ctx.Done())
-	return nil
+	d.trigger.Add(stop, ctx.Done())
+	return done, nil
 }
 
 func (*discoveryMock) Scan(ctx *context.T, query string) (<-chan discovery.Update, error) {