[fidl/bindings] add BindingSet#Size
...enabling callers to know how many clients a given service has without
making a copy via BindingSet#BindingKeys.
NET-2429 #progress
Change-Id: I17cefff36b3bf98bb86691b9f051da6b171cdc47
diff --git a/src/syscall/zx/fidl/bindings.go b/src/syscall/zx/fidl/bindings.go
index 2b947dc..ea16421 100644
--- a/src/syscall/zx/fidl/bindings.go
+++ b/src/syscall/zx/fidl/bindings.go
@@ -253,6 +253,13 @@
return key, nil
}
+// Size returns the number of BindingKeys in the BindingSet.
+func (b *BindingSet) Size() int {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return len(b.bindings)
+}
+
// BindingKeys returns a copy slice of all the BindingKeys in the BindingSet.
func (b *BindingSet) BindingKeys() []BindingKey {
b.mu.Lock()
diff --git a/src/syscall/zx/fidl/fidl_test/bindings_test.go b/src/syscall/zx/fidl/fidl_test/bindings_test.go
index 40bf0f4..8ae7883 100644
--- a/src/syscall/zx/fidl/fidl_test/bindings_test.go
+++ b/src/syscall/zx/fidl/fidl_test/bindings_test.go
@@ -25,6 +25,34 @@
return s, nil
}
+func TestBindingSize(t *testing.T) {
+ clientEnd, serverEnd, err := zx.NewChannel(0)
+ if err != nil {
+ t.Fatalf("could not create channel: %v", err)
+ }
+ defer func() {
+ clientEnd.Close()
+ serverEnd.Close()
+ }()
+ var server Test1Service
+ if got, want := server.Size(), 0; got != want {
+ t.Fatalf("got server.Size() = %d, want %d", got, want)
+ }
+ key, err := server.Add(&Test1Impl{}, serverEnd, nil)
+ if err != nil {
+ t.Fatalf("could not add to server: %v", err)
+ }
+ if got, want := server.Size(), 1; got != want {
+ t.Fatalf("got server.Size() = %d, want %d", got, want)
+ }
+ if got := server.Remove(key); !got {
+ t.Fatalf("got server.Remove(%v) = %t, want %t", key, got, true)
+ }
+ if got, want := server.Size(), 0; got != want {
+ t.Fatalf("got server.Size() = %d, want %d", got, want)
+ }
+}
+
func TestEmptyWriteErrors(t *testing.T) {
t.Parallel()
chErr := make(chan error)
@@ -50,7 +78,7 @@
server := Test1Service{}
if _, err := server.Add(&Test1Impl{}, sh, errFunc); err != nil {
- t.Fatalf("coudl not add to server: %v", err)
+ t.Fatalf("could not add to server: %v", err)
}
select {