diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index 83d0edf9..02853a7e 100644 --- a/reflection/serverreflection.go +++ b/reflection/serverreflection.go @@ -172,9 +172,12 @@ func (s *serverReflectionServer) allExtensionNumbersForType(st reflect.Type) ([] return nil, fmt.Errorf("failed to create message from type: %v", st) } - var out []int32 - for id := range proto.RegisteredExtensions(m) { - out = append(out, id) + exts := proto.RegisteredExtensions(m) + out := make([]int32, len(exts)) + i := 0 + for id := range exts { + out[i] = id + i++ } return out, nil } diff --git a/server_test.go b/server_test.go index f536abe7..d4db64ad 100644 --- a/server_test.go +++ b/server_test.go @@ -88,7 +88,7 @@ func TestStopBeforeServe(t *testing.T) { } } -func TestFileDesc(t *testing.T) { +func TestMetadata(t *testing.T) { server := NewServer() server.RegisterService(&testSd, &testServer{}) @@ -106,15 +106,15 @@ func TestFileDesc(t *testing.T) { ok bool ) if fd, ok = meta.([]byte); !ok { - t.Errorf("FileDesc(%q)=%v, want %v", test.name, meta, test.want) + t.Errorf("Metadata(%q)=%v, want %v", test.name, meta, test.want) } if !reflect.DeepEqual(fd, test.want) { - t.Errorf("FileDesc(%q)=%v, want %v", test.name, fd, test.want) + t.Errorf("Metadata(%q)=%v, want %v", test.name, fd, test.want) } } } -func TestFileDescNotFound(t *testing.T) { +func TestMetadataNotFound(t *testing.T) { server := NewServer() server.RegisterService(&testSd, &testServer{}) @@ -127,7 +127,30 @@ func TestFileDescNotFound(t *testing.T) { } { meta := server.Metadata(test) if meta != nil { - t.Errorf("FileDesc(%q)=%v, want ", test, meta) + t.Errorf("Metadata(%q)=%v, want ", test, meta) } } } + +func TestAllServiceNames(t *testing.T) { + server := NewServer() + server.RegisterService(&testSd, &testServer{}) + server.RegisterService(&ServiceDesc{ + ServiceName: "another.EmptyService", + HandlerType: (*emptyServiceServer)(nil), + }, &testServer{}) + services := server.AllServiceNames() + want := []string{"grpc.testing.EmptyService", "another.EmptyService"} + // Compare string slices. + m := make(map[string]int) + for _, s := range services { + m[s]++ + } + for _, s := range want { + if m[s] > 0 { + m[s]-- + continue + } + t.Fatalf("AllServiceNames() = %q, want: %q", services, want) + } +}