fix race in test and add import comments

This commit is contained in:
Menghan Li 2016-10-31 15:32:47 -07:00
parent 17ee5a3637
commit 46e80bf1f6
2 changed files with 24 additions and 4 deletions

View File

@ -34,13 +34,14 @@
// Package stats reports stats for gRPC. // Package stats reports stats for gRPC.
// This package is for monitoring purpose only. // This package is for monitoring purpose only.
// All APIs are experimental. // All APIs are experimental.
package stats package stats // import "google.golang.org/grpc/stats"
import ( import (
"context"
"net" "net"
"sync/atomic" "sync/atomic"
"time" "time"
"golang.org/x/net/context"
) )
// Stats contains stats information about RPCs. // Stats contains stats information about RPCs.

View File

@ -36,6 +36,7 @@ package stats_test
import ( import (
"io" "io"
"net" "net"
"sync"
"testing" "testing"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@ -442,8 +443,13 @@ func checkOutgoingTrailerStats(t *testing.T, s stats.Stats, e *expectedData) {
} }
func TestServerStatsUnaryRPC(t *testing.T) { func TestServerStatsUnaryRPC(t *testing.T) {
var got []stats.Stats var (
mu sync.Mutex
got []stats.Stats
)
stats.RegisterCallBack(func(s stats.Stats) { stats.RegisterCallBack(func(s stats.Stats) {
mu.Lock()
defer mu.Unlock()
got = append(got, s) got = append(got, s)
}) })
@ -452,6 +458,8 @@ func TestServerStatsUnaryRPC(t *testing.T) {
defer te.tearDown() defer te.tearDown()
req, resp := te.doUnaryCall() req, resp := te.doUnaryCall()
te.srv.GracefulStop() // Wait for the server to stop.
expect := &expectedData{ expect := &expectedData{
method: "/grpc.testing.TestService/UnaryCall", method: "/grpc.testing.TestService/UnaryCall",
localAddr: te.srvAddr, localAddr: te.srvAddr,
@ -467,15 +475,22 @@ func TestServerStatsUnaryRPC(t *testing.T) {
checkOutgoingPayloadStats, checkOutgoingPayloadStats,
checkOutgoingTrailerStats, checkOutgoingTrailerStats,
} { } {
mu.Lock()
f(t, got[i], expect) f(t, got[i], expect)
mu.Unlock()
} }
stats.Stop() stats.Stop()
} }
func TestServerStatsStreamingRPC(t *testing.T) { func TestServerStatsStreamingRPC(t *testing.T) {
var got []stats.Stats var (
mu sync.Mutex
got []stats.Stats
)
stats.RegisterCallBack(func(s stats.Stats) { stats.RegisterCallBack(func(s stats.Stats) {
mu.Lock()
defer mu.Unlock()
got = append(got, s) got = append(got, s)
}) })
@ -485,6 +500,8 @@ func TestServerStatsStreamingRPC(t *testing.T) {
count := 5 count := 5
reqs, resps := te.doFullDuplexCallRoundtrip(count) reqs, resps := te.doFullDuplexCallRoundtrip(count)
te.srv.GracefulStop() // Wait for the server to stop.
expect := &expectedData{ expect := &expectedData{
method: "/grpc.testing.TestService/FullDuplexCall", method: "/grpc.testing.TestService/FullDuplexCall",
localAddr: te.srvAddr, localAddr: te.srvAddr,
@ -508,7 +525,9 @@ func TestServerStatsStreamingRPC(t *testing.T) {
checkFuncs = append(checkFuncs, checkOutgoingTrailerStats) checkFuncs = append(checkFuncs, checkOutgoingTrailerStats)
for i, f := range checkFuncs { for i, f := range checkFuncs {
mu.Lock()
f(t, got[i], expect) f(t, got[i], expect)
mu.Unlock()
} }
stats.Stop() stats.Stop()