예제 #1
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
}
예제 #2
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)
		}
	}
}
예제 #3
0
파일: main.go 프로젝트: xiaohua546/bfgo
func (kvserver *KvServer) PingStreamCS(stream BfKvService_PingStreamCSServer) error {
	log.Printf("===PingStreamCS===,clientid=(%s)", getClientId(stream.Context()))
	pingResp := &BfPingData{Message: message}
	anyResp, err := ptypes.MarshalAny(pingResp)
	if err != nil {
		log.Fatalf("MarshalAny fail,%v", err)
		return err
	}

	for {
		anyReq, err := stream.Recv()
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}

		pingReq := &BfPingData{}
		if ptypes.Is(anyReq, pingReq) {
			ptypes.UnmarshalAny(anyReq, pingReq)
			log.Printf("recv,%s", pingReq.Message)
		} else {
			log.Fatalf("PingStreamS,%v", anyReq)
			return err
		}

		if err := stream.Send(anyResp); err != nil {
			return err
		}
	}
}
예제 #4
0
파일: main.go 프로젝트: xiaohua546/bfgo
func (kvserver *KvServer) PingStreamS(anyReq *Any, stream BfKvService_PingStreamSServer) error {
	log.Printf("===PingStreamS===,clientid=(%s)", getClientId(stream.Context()))

	pingReq := &BfPingData{}
	if ptypes.Is(anyReq, pingReq) {
		ptypes.UnmarshalAny(anyReq, pingReq)
		log.Printf("recv,%s", pingReq.Message)
	} else {
		log.Fatalf("PingStreamS,%v", anyReq)
		return nil
	}

	pingResp := &BfPingData{Message: message}
	anyResp, err := ptypes.MarshalAny(pingResp)
	if err != nil {
		log.Fatalf("MarshalAny fail,%v", err)
		return err
	}
	rd := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < 10; i++ {
		log.Printf("send,%d", i)
		if err := stream.Send(anyResp); err != nil {
			return err
		}
		s := 500 + rd.Int31n(500) - 1
		time.Sleep(time.Duration(s) * time.Millisecond)
	}

	return nil
}
예제 #5
0
func TestSpeechAsyncRecognize(t *testing.T) {
	var expectedResponse *speechpb.AsyncRecognizeResponse = &speechpb.AsyncRecognizeResponse{}

	mockSpeech.err = nil
	mockSpeech.reqs = nil

	any, err := ptypes.MarshalAny(expectedResponse)
	if err != nil {
		t.Fatal(err)
	}
	mockSpeech.resps = append(mockSpeech.resps[:0], &longrunningpb.Operation{
		Name:   "longrunning-test",
		Done:   true,
		Result: &longrunningpb.Operation_Response{Response: any},
	})

	var config *speechpb.RecognitionConfig = &speechpb.RecognitionConfig{}
	var audio *speechpb.RecognitionAudio = &speechpb.RecognitionAudio{}
	var request = &speechpb.AsyncRecognizeRequest{
		Config: config,
		Audio:  audio,
	}

	c, err := NewClient(context.Background(), clientOpt)
	if err != nil {
		t.Fatal(err)
	}

	respLRO, err := c.AsyncRecognize(context.Background(), request)
	if err != nil {
		t.Fatal(err)
	}
	resp, err := respLRO.Wait(context.Background())

	if err != nil {
		t.Fatal(err)
	}

	if want, got := request, mockSpeech.reqs[0]; !proto.Equal(want, got) {
		t.Errorf("wrong request %q, want %q", got, want)
	}

	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
		t.Errorf("wrong response %q, want %q)", got, want)
	}
}
예제 #6
0
파일: ptypes.go 프로젝트: benjyw/kythe
// MarshalAny converts pb to a google.protobuf.Any message, fixing the URLs of
// Kythe protobuf types as needed.
func MarshalAny(pb proto.Message) (*anypb.Any, error) {
	// The ptypes package vendors generated code for the Any type, so we have
	// to convert the type. The pointers are convertible, but since we need to
	// do surgery on the URL anyway, we just construct the output separately.
	internalAny, err := ptypes.MarshalAny(pb)
	if err != nil {
		return nil, err
	}

	// Fix up messages in the Kythe namespace.
	url := internalAny.TypeUrl
	if name, _ := ptypes.AnyMessageName(internalAny); strings.HasPrefix(name, "kythe.") {
		url = "kythe.io/proto/" + name
	}
	return &anypb.Any{
		TypeUrl: url,
		Value:   internalAny.Value,
	}, nil
}
예제 #7
0
// BenchmarkAny creates increasingly large arbitrary Any messages.  The type is always the
// same.
func BenchmarkAny(b *testing.B) {
	data := make([]byte, 1<<20)
	quantum := 1 << 10
	for i := uint(0); i <= 10; i++ {
		b.Run(strconv.Itoa(quantum<<i), func(b *testing.B) {
			for k := 0; k < b.N; k++ {
				inner := &tpb.Message{
					Data: data[:quantum<<i],
				}
				outer, err := ptypes.MarshalAny(inner)
				if err != nil {
					b.Error("wrong encode", err)
				}
				raw, err := proto.Marshal(&tpb.Message{
					Anything: outer,
				})
				if err != nil {
					b.Error("wrong encode", err)
				}
				blackhole = raw
			}
		})
	}
}
예제 #8
0
파일: config.go 프로젝트: v2ray/v2ray-core
func (v *HTTPResponse) AsAny() *any.Any {
	r, _ := ptypes.MarshalAny(v)
	return r
}
예제 #9
0
func TestFromLogEntry(t *testing.T) {
	now := time.Now()
	res := &mrpb.MonitoredResource{Type: "global"}
	ts, err := ptypes.TimestampProto(now)
	if err != nil {
		t.Fatal(err)
	}
	logEntry := logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Payload:   &logpb.LogEntry_TextPayload{"hello"},
		Timestamp: ts,
		Severity:  logtypepb.LogSeverity_INFO,
		InsertId:  "123",
		HttpRequest: &logtypepb.HttpRequest{
			RequestMethod:                  "GET",
			RequestUrl:                     "http:://example.com/path?q=1",
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			Latency:                        &durpb.Duration{Seconds: 100},
			UserAgent:                      "user-agent",
			RemoteIp:                       "127.0.0.1",
			Referer:                        "referer",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
	}
	u, err := url.Parse("http:://example.com/path?q=1")
	if err != nil {
		t.Fatal(err)
	}
	want := &logging.Entry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: now.In(time.UTC),
		Severity:  logging.Info,
		Payload:   "hello",
		Labels: map[string]string{
			"a": "1",
			"b": "two",
			"c": "true",
		},
		InsertID: "123",
		HTTPRequest: &logging.HTTPRequest{
			Request: &http.Request{
				Method: "GET",
				URL:    u,
				Header: map[string][]string{
					"User-Agent": []string{"user-agent"},
					"Referer":    []string{"referer"},
				},
			},
			RequestSize:                    100,
			Status:                         200,
			ResponseSize:                   25,
			Latency:                        100 * time.Second,
			RemoteIP:                       "127.0.0.1",
			CacheHit:                       true,
			CacheValidatedWithOriginServer: true,
		},
	}
	got, err := fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	// Test sub-values separately because %+v and %#v do not follow pointers.
	// TODO(jba): use a differ or pretty-printer.
	if !reflect.DeepEqual(got.HTTPRequest.Request, want.HTTPRequest.Request) {
		t.Fatalf("HTTPRequest.Request:\ngot  %+v\nwant %+v", got.HTTPRequest.Request, want.HTTPRequest.Request)
	}
	if !reflect.DeepEqual(got.HTTPRequest, want.HTTPRequest) {
		t.Fatalf("HTTPRequest:\ngot  %+v\nwant %+v", got.HTTPRequest, want.HTTPRequest)
	}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("FullEntry:\ngot  %+v\nwant %+v", got, want)
	}

	// Proto payload.
	alog := &audit.AuditLog{
		ServiceName:  "svc",
		MethodName:   "method",
		ResourceName: "shelves/S/books/B",
	}
	any, err := ptypes.MarshalAny(alog)
	if err != nil {
		t.Fatal(err)
	}
	logEntry = logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: ts,
		Payload:   &logpb.LogEntry_ProtoPayload{any},
	}
	got, err = fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(got.Payload, alog) {
		t.Errorf("got %+v, want %+v", got.Payload, alog)
	}

	// JSON payload.
	jstruct := &structpb.Struct{map[string]*structpb.Value{
		"f": &structpb.Value{&structpb.Value_NumberValue{3.1}},
	}}
	logEntry = logpb.LogEntry{
		LogName:   "projects/PROJECT_ID/logs/LOG_ID",
		Resource:  res,
		Timestamp: ts,
		Payload:   &logpb.LogEntry_JsonPayload{jstruct},
	}
	got, err = fromLogEntry(&logEntry)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(got.Payload, jstruct) {
		t.Errorf("got %+v, want %+v", got.Payload, jstruct)
	}
}
예제 #10
0
func (this *Account) AsAny() (*google_protobuf.Any, error) {
	return ptypes.MarshalAny(this)
}
예제 #11
0
func (this *HTTPResponse) AsAny() *any.Any {
	r, _ := ptypes.MarshalAny(this)
	return r
}