From 451a2e416b74ae6435cfc698ffc50ecdfa0494f6 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Thu, 16 Jun 2016 15:10:13 -0700 Subject: [PATCH] Minor change in allExtensionNumbersForType(), add TestAllServiceNames() --- reflection/serverreflection.go | 9 ++++++--- server_test.go | 33 ++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index 83d0edf98..02853a7e8 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 f536abe79..d4db64ad4 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) + } +}