mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg: address golangci var-naming issues
Signed-off-by: Ivan Valdes <ivan@vald.es>
This commit is contained in:
69
pkg/grpctesting/recorder.go
Normal file
69
pkg/grpctesting/recorder.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2021 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package grpctesting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type GRPCRecorder struct {
|
||||
mux sync.RWMutex
|
||||
requests []RequestInfo
|
||||
}
|
||||
|
||||
type RequestInfo struct {
|
||||
FullMethod string
|
||||
Authority string
|
||||
}
|
||||
|
||||
func (ri *GRPCRecorder) UnaryInterceptor() grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
||||
ri.record(toRequestInfo(ctx, info))
|
||||
resp, err := handler(ctx, req)
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
|
||||
func (ri *GRPCRecorder) RecordedRequests() []RequestInfo {
|
||||
ri.mux.RLock()
|
||||
defer ri.mux.RUnlock()
|
||||
reqs := make([]RequestInfo, len(ri.requests))
|
||||
copy(reqs, ri.requests)
|
||||
return reqs
|
||||
}
|
||||
|
||||
func toRequestInfo(ctx context.Context, info *grpc.UnaryServerInfo) RequestInfo {
|
||||
req := RequestInfo{
|
||||
FullMethod: info.FullMethod,
|
||||
}
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if ok {
|
||||
as := md.Get(":authority")
|
||||
if len(as) != 0 {
|
||||
req.Authority = as[0]
|
||||
}
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func (ri *GRPCRecorder) record(r RequestInfo) {
|
||||
ri.mux.Lock()
|
||||
defer ri.mux.Unlock()
|
||||
ri.requests = append(ri.requests, r)
|
||||
}
|
||||
116
pkg/grpctesting/stub_server.go
Normal file
116
pkg/grpctesting/stub_server.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright 2021 The etcd Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package grpctesting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
testpb "google.golang.org/grpc/interop/grpc_testing"
|
||||
)
|
||||
|
||||
// StubServer is borrowed from the interal package of grpc-go.
|
||||
// See https://github.com/grpc/grpc-go/blob/master/internal/stubserver/stubserver.go
|
||||
// Since it cannot be imported directly, we have to copy and paste it here,
|
||||
// and useless code for our testing is removed.
|
||||
|
||||
// StubServer is a server that is easy to customize within individual test
|
||||
// cases.
|
||||
type StubServer struct {
|
||||
testService testpb.TestServiceServer
|
||||
|
||||
// Network and Address are parameters for Listen. Defaults will be used if these are empty before Start.
|
||||
Network string
|
||||
Address string
|
||||
|
||||
s *grpc.Server
|
||||
|
||||
cleanups []func() // Lambdas executed in Stop(); populated by Start().
|
||||
started chan struct{}
|
||||
}
|
||||
|
||||
func New(testService testpb.TestServiceServer) *StubServer {
|
||||
return &StubServer{
|
||||
testService: testService,
|
||||
started: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts the server and creates a client connected to it.
|
||||
func (ss *StubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption) error {
|
||||
if ss.Network == "" {
|
||||
ss.Network = "tcp"
|
||||
}
|
||||
if ss.Address == "" {
|
||||
ss.Address = "localhost:0"
|
||||
}
|
||||
|
||||
lis, err := net.Listen(ss.Network, ss.Address)
|
||||
if err != nil {
|
||||
return fmt.Errorf("net.Listen(%q, %q) = %v", ss.Network, ss.Address, err)
|
||||
}
|
||||
ss.Address = lis.Addr().String()
|
||||
ss.cleanups = append(ss.cleanups, func() { lis.Close() })
|
||||
|
||||
s := grpc.NewServer(sopts...)
|
||||
testpb.RegisterTestServiceServer(s, ss.testService)
|
||||
go func() {
|
||||
close(ss.started)
|
||||
s.Serve(lis)
|
||||
}()
|
||||
ss.cleanups = append(ss.cleanups, s.Stop)
|
||||
ss.s = s
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops ss and cleans up all resources it consumed.
|
||||
func (ss *StubServer) Stop() {
|
||||
<-ss.started
|
||||
for i := len(ss.cleanups) - 1; i >= 0; i-- {
|
||||
ss.cleanups[i]()
|
||||
}
|
||||
}
|
||||
|
||||
// Addr gets the address the server listening on.
|
||||
func (ss *StubServer) Addr() string {
|
||||
return ss.Address
|
||||
}
|
||||
|
||||
type dummyStubServer struct {
|
||||
testpb.UnimplementedTestServiceServer
|
||||
counter uint64
|
||||
}
|
||||
|
||||
func (d *dummyStubServer) UnaryCall(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
|
||||
newCount := atomic.AddUint64(&d.counter, 1)
|
||||
|
||||
return &testpb.SimpleResponse{
|
||||
Payload: &testpb.Payload{
|
||||
Type: testpb.PayloadType_COMPRESSABLE,
|
||||
Body: []byte(strconv.FormatUint(newCount, 10)),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewDummyStubServer creates a simple test server that serves Unary calls with
|
||||
// responses with the given payload.
|
||||
func NewDummyStubServer(body []byte) *StubServer {
|
||||
return New(&dummyStubServer{})
|
||||
}
|
||||
Reference in New Issue
Block a user