Exemplo n.º 1
0
func TestWait(t *testing.T) {
	responseDur := ptypes.DurationProto(42 * time.Second)
	responseAny, err := ptypes.MarshalAny(responseDur)
	if err != nil {
		t.Fatal(err)
	}

	s := &getterService{
		results: []*pb.Operation{
			{Name: "foo"},
			{Name: "foo"},
			{Name: "foo"},
			{Name: "foo"},
			{Name: "foo"},
			{
				Name: "foo",
				Done: true,
				Result: &pb.Operation_Response{
					Response: responseAny,
				},
			},
		},
	}
	op := &Operation{
		c:     s,
		proto: &pb.Operation{Name: "foo"},
	}
	if op.Done() {
		t.Fatal("operation should not have completed yet")
	}

	var resp duration.Duration
	bo := gax.Backoff{
		Initial: 1 * time.Second,
		Max:     3 * time.Second,
	}
	if err := op.wait(context.Background(), &resp, &bo, s.sleeper()); err != nil {
		t.Fatal(err)
	}
	if !proto.Equal(&resp, responseDur) {
		t.Errorf("response, got %v, want %v", resp, responseDur)
	}
	if !op.Done() {
		t.Errorf("operation should have completed")
	}

	maxWait := []time.Duration{
		1 * time.Second,
		2 * time.Second,
		3 * time.Second,
		3 * time.Second,
		3 * time.Second,
	}
	for i := 0; i < len(s.getTimes)-1; i++ {
		w := s.getTimes[i+1] - s.getTimes[i]
		if mw := maxWait[i]; w > mw {
			t.Errorf("backoff, waited %s, max %s", w, mw)
		}
	}
}
Exemplo n.º 2
0
func fromHTTPRequest(r *HTTPRequest) *logtypepb.HttpRequest {
	if r == nil {
		return nil
	}
	if r.Request == nil {
		panic("HTTPRequest must have a non-nil Request")
	}
	u := *r.Request.URL
	u.Fragment = ""
	pb := &logtypepb.HttpRequest{
		RequestMethod:                  r.Request.Method,
		RequestUrl:                     u.String(),
		RequestSize:                    r.RequestSize,
		Status:                         int32(r.Status),
		ResponseSize:                   r.ResponseSize,
		UserAgent:                      r.Request.UserAgent(),
		RemoteIp:                       r.RemoteIP, // TODO(jba): attempt to parse http.Request.RemoteAddr?
		Referer:                        r.Request.Referer(),
		CacheHit:                       r.CacheHit,
		CacheValidatedWithOriginServer: r.CacheValidatedWithOriginServer,
	}
	if r.Latency != 0 {
		pb.Latency = ptypes.DurationProto(r.Latency)
	}
	return pb
}
Exemplo n.º 3
0
func bestMomentInHistory() (*Operation, error) {
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2009-11-10 23:00:00 +0000 UTC")
	if err != nil {
		return nil, err
	}
	resp, err := ptypes.TimestampProto(t)
	if err != nil {
		return nil, err
	}
	respAny, err := ptypes.MarshalAny(resp)
	if err != nil {
		return nil, err
	}
	metaAny, err := ptypes.MarshalAny(ptypes.DurationProto(1 * time.Hour))
	return &Operation{
		proto: &pb.Operation{
			Name:     "best-moment",
			Done:     true,
			Metadata: metaAny,
			Result: &pb.Operation_Response{
				Response: respAny,
			},
		},
	}, err
}
Exemplo n.º 4
0
func NewUnaryServerInterceptor(auth Authorizer, inst Instrumenter) grpc.UnaryServerInterceptor {
	return func(
		ctx context.Context,
		in interface{},
		info *grpc.UnaryServerInfo,
		handler grpc.UnaryHandler,
	) (interface{}, error) {
		requester, ok := in.(interface {
			GetRequest() *Request
		})
		req := requester.GetRequest()
		if !ok || req == nil {
			return nil, ErrInvalidRequest
		}
		if req.GetSource() == nil {
			return nil, ErrInvalidRequest
		}
		s := strings.Split(info.FullMethod, "/")
		if len(s) != 3 || s[0] != "" || s[1] == "" || s[2] == "" {
			return nil, ErrInvalidRequest
		}
		req.Call = &Call{Service: s[1], Method: s[2]}
		if err := auth.Authorize(ctx, req); err != nil {
			inst.Instrument(&Event{Key: "request_unauthorized", Request: req, Error: err})
			return nil, err
		}
		start := time.Now()
		resp, err := handler(ctx, in)
		if err != nil {
			req.Call.Error = err.Error()
		}
		req.Call.Duration = ptypes.DurationProto(time.Since(start))
		inst.Instrument(&Event{Key: "request_handled", Request: req, Error: err})
		return resp, err
	}
}