Exemple #1
0
// Clone returns a copy of Region.
func (r *Region) Clone() *Region {
	return &Region{
		meta:       proto.Clone(r.meta).(*metapb.Region),
		peer:       proto.Clone(r.peer).(*metapb.Peer),
		addr:       r.addr,
		curPeerIdx: r.curPeerIdx,
	}
}
Exemple #2
0
// GetRegionByKey returns the Region and its leader whose range contains the key.
func (c *Cluster) GetRegionByKey(key []byte) (*metapb.Region, *metapb.Peer) {
	c.RLock()
	defer c.RUnlock()

	for _, r := range c.regions {
		if regionContains(r.meta.StartKey, r.meta.EndKey, key) {
			return proto.Clone(r.meta).(*metapb.Region), proto.Clone(r.leaderPeer()).(*metapb.Peer)
		}
	}
	return nil, nil
}
Exemple #3
0
// MaybeExtractQ extracts proto from HTTP request and returns it.
// Nil indicates failure, and appropriate status / description is written
// to response.
func MaybeExtractQ(w http.ResponseWriter, r *http.Request, defaultQ proto.Message) *proto.Message {
	q := proto.Clone(defaultQ)
	if r.Method == "POST" {
		reqBody, err := ioutil.ReadAll(r.Body)
		if err != nil {
			fmt.Fprintf(w, "Failed to read POST body %v", err)
			return nil
		}
		err = proto.Unmarshal(reqBody, q)
		if err != nil {
			fmt.Fprintf(w, "Failed to parse POST body as binary proto: %v", err)
			return nil
		}
	} else {
		err := r.ParseForm()
		if err != nil {
			http.NotFound(w, r)
			fmt.Fprintf(w, "strange query %v", err)
			return nil
		}
		pb := r.Form.Get("pb")
		if pb == "" {
			http.NotFound(w, r)
			fmt.Fprintf(w, "Non-empty jsonpb-encoded pb param required for GET")
			return nil
		}
		err = jsonpb.UnmarshalString(pb, q)
		if err != nil {
			fmt.Fprintf(w, "Failed to parse pb param %v", err)
			return nil
		}
	}
	return &q
}
Exemple #4
0
func (s *server) updateConfig(user string, updater func(*pb.ServiceConfig) error) error {
	s.Lock()
	clonedCfg := proto.Clone(s.cfgs).(*pb.ServiceConfig)
	currentVersion := clonedCfg.Version
	s.Unlock()

	err := updater(clonedCfg)

	if err != nil {
		return err
	}

	config.ApplyDefaults(clonedCfg)

	clonedCfg.User = user
	clonedCfg.Date = time.Now().Unix()
	clonedCfg.Version = currentVersion + 1

	r, e := config.Marshal(clonedCfg)

	if e != nil {
		return e
	}

	return s.persister.PersistAndNotify(r)
}
Exemple #5
0
func TestProto3SetDefaults(t *testing.T) {
	in := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: new(tpb.SubDefaults),
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": new(tpb.SubDefaults),
		},
	}

	got := proto.Clone(in).(*pb.Message)
	proto.SetDefaults(got)

	// There are no defaults in proto3.  Everything should be the zero value, but
	// we need to remember to set defaults for nested proto2 messages.
	want := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": {N: proto.Int64(7)},
		},
	}

	if !proto.Equal(got, want) {
		t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
	}
}
Exemple #6
0
func PrivacyFilterSnapshot(snapshot pb.Snapshot) pb.Snapshot {
	result := proto.Clone(&snapshot).(*pb.Snapshot)
	for _, session := range result.Sessions {
		session.Pass = "******"
	}
	return *result
}
Exemple #7
0
func TestClone(t *testing.T) {
	m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
	if !proto.Equal(m, cloneTestMessage) {
		t.Errorf("Clone(%v) = %v", cloneTestMessage, m)
	}

	// Verify it was a deep copy.
	*m.Inner.Port++
	if proto.Equal(m, cloneTestMessage) {
		t.Error("Mutating clone changed the original")
	}
	// Byte fields and repeated fields should be copied.
	if &m.Pet[0] == &cloneTestMessage.Pet[0] {
		t.Error("Pet: repeated field not copied")
	}
	if &m.Others[0] == &cloneTestMessage.Others[0] {
		t.Error("Others: repeated field not copied")
	}
	if &m.Others[0].Value[0] == &cloneTestMessage.Others[0].Value[0] {
		t.Error("Others[0].Value: bytes field not copied")
	}
	if &m.RepBytes[0] == &cloneTestMessage.RepBytes[0] {
		t.Error("RepBytes: repeated field not copied")
	}
	if &m.RepBytes[0][0] == &cloneTestMessage.RepBytes[0][0] {
		t.Error("RepBytes[0]: bytes field not copied")
	}
}
// addHealthResponse adds a mocked health response to the buffer channel.
func (q *fakeQueryService) addHealthResponse(qps float64) {
	q.healthResponses <- &querypb.StreamHealthResponse{
		Target:  proto.Clone(&q.target).(*querypb.Target),
		Serving: true,
		RealtimeStats: &querypb.RealtimeStats{
			Qps: qps,
		},
	}
}
// AddDefaultHealthResponse adds a faked health response to the buffer channel.
// The response will have default values typical for a healthy tablet.
func (q *StreamHealthQueryService) AddDefaultHealthResponse() {
	q.healthResponses <- &querypb.StreamHealthResponse{
		Target:  proto.Clone(&q.target).(*querypb.Target),
		Serving: true,
		RealtimeStats: &querypb.RealtimeStats{
			SecondsBehindMaster: DefaultSecondsBehindMaster,
		},
	}
}
Exemple #10
0
// GetStore returns a Store's meta.
func (c *Cluster) GetStore(storeID uint64) *metapb.Store {
	c.mu.RLock()
	defer c.mu.RUnlock()

	if store := c.stores[storeID]; store != nil {
		return proto.Clone(store.meta).(*metapb.Store)
	}
	return nil
}
Exemple #11
0
func TestMerge(t *testing.T) {
	for _, m := range mergeTests {
		got := proto.Clone(m.dst)
		proto.Merge(got, m.src)
		if !proto.Equal(got, m.want) {
			t.Errorf("Merge(%v, %v)\n got %v\nwant %v\n", m.dst, m.src, got, m.want)
		}
	}
}
Exemple #12
0
// Compile all information exported through the hackerspace API and send it
// back to the requestor.
func (a *SpaceAPI) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	var msg = proto.Clone(a.conf.SpaceapiMd)
	var md *doorky.SpaceAPIMetadata
	var door *doorky.DoorSecret
	var out []byte
	var err error
	var ok bool

	md, ok = msg.(*doorky.SpaceAPIMetadata)
	if !ok {
		log.Print("Error: message is not of type SpaceAPIMetadata")
		http.Error(rw, http.StatusText(http.StatusInternalServerError),
			http.StatusInternalServerError)
	}

	for _, door = range a.conf.Secret {
		var lockInfo = new(doorky.SpaceAPIDoorLockSensor)
		var name = door.GetName()
		var ts time.Time
		var isOpen bool

		ts, isOpen, err = a.ts.LastValue(name)
		if err != nil {
			log.Print("Error fetching door status for ", name, ": ", err)
			continue
		}

		lockInfo.Value = proto.Bool(!isOpen)
		lockInfo.Location = proto.String(door.GetLocation())
		lockInfo.Name = proto.String(name)
		lockInfo.Description = proto.String("Last update: " + ts.String())

		if md.Sensors == nil {
			md.Sensors = new(doorky.SpaceAPISensors)
		}
		md.Sensors.DoorLocked = append(md.Sensors.DoorLocked, lockInfo)

		if a.conf.PrimaryDoor != nil && a.conf.GetPrimaryDoor() == name {
			md.State.Open = proto.Bool(isOpen)
			md.State.Lastchange = proto.Int64(ts.Unix())
		}
	}

	out, err = json.MarshalIndent(md, "    ", "    ")
	if err != nil {
		log.Print("Error marshalling JSON: ", err)
		http.Error(rw, http.StatusText(http.StatusInternalServerError)+": "+
			"Error marshalling response: "+err.Error(),
			http.StatusInternalServerError)
	}

	_, err = rw.Write(out)
	if err != nil {
		log.Print("Error writing response: ", err)
	}
}
Exemple #13
0
func TestReadDelimited(t *testing.T) {
	t.Parallel()
	for _, test := range []struct {
		buf []byte
		msg proto.Message
		n   int
		err error
	}{
		{
			buf: []byte{0},
			msg: &Empty{},
			n:   1,
		},
		{
			n:   3,
			buf: []byte{2, 8, 1},
			msg: &GoEnum{Foo: FOO_FOO1.Enum()},
		},
		{
			buf: []byte{141, 2, 10, 138, 2, 84, 104, 105, 115, 32, 105, 115, 32, 109,
				121, 32, 103, 105, 103, 97, 110, 116, 105, 99, 44, 32, 117, 110, 104,
				97, 112, 112, 121, 32, 115, 116, 114, 105, 110, 103, 46, 32, 32, 73,
				116, 32, 101, 120, 99, 101, 101, 100, 115, 10, 116, 104, 101, 32, 101,
				110, 99, 111, 100, 105, 110, 103, 32, 115, 105, 122, 101, 32, 111, 102,
				32, 97, 32, 115, 105, 110, 103, 108, 101, 32, 98, 121, 116, 101, 32,
				118, 97, 114, 105, 110, 116, 46, 32, 32, 87, 101, 32, 97, 114, 101, 32,
				117, 115, 105, 110, 103, 32, 105, 116, 32, 116, 111, 32, 102, 117, 122,
				122, 32, 116, 101, 115, 116, 32, 116, 104, 101, 10, 99, 111, 114, 114,
				101, 99, 116, 110, 101, 115, 115, 32, 111, 102, 32, 116, 104, 101, 32,
				104, 101, 97, 100, 101, 114, 32, 100, 101, 99, 111, 100, 105, 110, 103,
				32, 109, 101, 99, 104, 97, 110, 105, 115, 109, 115, 44, 32, 119, 104,
				105, 99, 104, 32, 109, 97, 121, 32, 112, 114, 111, 118, 101, 32, 112,
				114, 111, 98, 108, 101, 109, 97, 116, 105, 99, 46, 10, 73, 32, 101, 120,
				112, 101, 99, 116, 32, 105, 116, 32, 109, 97, 121, 46, 32, 32, 76, 101,
				116, 39, 115, 32, 104, 111, 112, 101, 32, 121, 111, 117, 32, 101, 110,
				106, 111, 121, 32, 116, 101, 115, 116, 105, 110, 103, 32, 97, 115, 32,
				109, 117, 99, 104, 32, 97, 115, 32, 119, 101, 32, 100, 111, 46},
			msg: &Strings{
				StringField: proto.String(`This is my gigantic, unhappy string.  It exceeds
the encoding size of a single byte varint.  We are using it to fuzz test the
correctness of the header decoding mechanisms, which may prove problematic.
I expect it may.  Let's hope you enjoy testing as much as we do.`),
			},
			n: 271,
		},
	} {
		msg := proto.Clone(test.msg)
		msg.Reset()
		if n, err := ReadDelimited(bytes.NewBuffer(test.buf), msg); n != test.n || err != test.err {
			t.Fatalf("ReadDelimited(%v, msg) = %v, %v; want %v, %v", test.buf, n, err, test.n, test.err)
		}
		if !proto.Equal(msg, test.msg) {
			t.Fatalf("ReadDelimited(%v, msg); msg = %v; want %v", test.buf, msg, test.msg)
		}
	}
}
Exemple #14
0
// GetRegion returns a Region's meta and leader ID.
func (c *Cluster) GetRegion(regionID uint64) (*metapb.Region, uint64) {
	c.mu.RLock()
	defer c.mu.RUnlock()

	r := c.regions[regionID]
	if r == nil {
		return nil, 0
	}
	return proto.Clone(r.meta).(*metapb.Region), r.leader
}
Exemple #15
0
// VName returns a VName for obj relative to that of its package.
func (pi *PackageInfo) VName(obj types.Object) *spb.VName {
	sig := pi.Signature(obj)
	base := pi.VNames[obj.Pkg()]
	if base == nil {
		return govname.ForBuiltin(sig)
	}
	vname := proto.Clone(base).(*spb.VName)
	vname.Signature = sig
	return vname
}
// subtractSystemBatteryLevel sets start level to the current level of the first proto.
func subtractSystemBatteryLevel(p1, p2 *bspb.BatteryStats_System_BatteryLevel) *bspb.BatteryStats_System_BatteryLevel {
	if p1 == nil && p2 == nil {
		return nil
	}
	if p2 == nil {
		return proto.Clone(p1).(*bspb.BatteryStats_System_BatteryLevel)
	}
	if p1 == nil {
		return proto.Clone(p2).(*bspb.BatteryStats_System_BatteryLevel)

	}
	d := &bspb.BatteryStats_System_BatteryLevel{}

	// CurrentLevel is set as the diff between the current level of the 2 protos.
	d.CurrentLevel = proto.Float32(p1.GetCurrentLevel() - p2.GetCurrentLevel())
	// Startlevel is set to the level of the first proto which is our main proto against which we want to diff the other one
	d.StartLevel = proto.Float32(p1.GetCurrentLevel())
	return d
}
// subtractRepeatedMessage subtracts protos in list2 from the corresponding protos in list1.
// The input lists should only contain protos, and the protos should have their identifiers
// be their first field.
func subtractRepeatedMessage(list1, list2 interface{}) reflect.Value {
	if list1 == nil && list2 == nil {
		return reflect.ValueOf([]proto.Message{})
	}
	l1 := genericListOrDie(list1)
	if list2 == nil {
		return l1
	}
	l2 := genericListOrDie(list2)
	if list1 == nil {
		// Need to make negatives of the elements in list2, so can't just return here.
		l1 = reflect.MakeSlice(l2.Type(), 0, 0)
	}

	t1, t2 := l1.Type(), l2.Type()
	if t1 != t2 {
		log.Fatalf("Mismatched list types: %v vs %v", t1, t2)
	}

	// All entries may not occur in both files, so use maps to keep track of everything.
	m1, m2 := make(map[string]proto.Message), make(map[string]proto.Message)
	for i := 0; i < l1.Len(); i++ {
		item := l1.Index(i)
		m1[name(item)] = item.Interface().(proto.Message)
	}
	for i := 0; i < l2.Len(); i++ {
		item := l2.Index(i)
		m2[name(item)] = item.Interface().(proto.Message)
	}

	out := reflect.MakeSlice(t1, 0, l1.Len()+l2.Len())
	for n, p1 := range m1 {
		p2, ok := m2[n]
		if !ok {
			// In list1 but not list2.
			out = reflect.Append(out, reflect.ValueOf(proto.Clone(p1)))
			continue
		}
		if diff := subtractMessage(p1, p2); diff != nil {
			out = reflect.Append(out, reflect.ValueOf(diff))
		}
	}
	for n, p2 := range m2 {
		if _, ok := m1[n]; !ok {
			// In list2 but not list1. Subtract to get negative values.
			if diff := subtractMessage(nil, p2); diff != nil {
				out = reflect.Append(out, reflect.ValueOf(diff))
			}
		}
	}
	if out.Len() == 0 {
		return reflect.Zero(l1.Type())
	}
	return out
}
Exemple #18
0
// GetStoreByAddr returns a Store's meta by an addr.
func (c *Cluster) GetStoreByAddr(addr string) *metapb.Store {
	c.mu.RLock()
	defer c.mu.RUnlock()

	for _, s := range c.stores {
		if s.meta.GetAddress() == addr {
			return proto.Clone(s.meta).(*metapb.Store)
		}
	}
	return nil
}
Exemple #19
0
// GetRegionByKey returns the Region whose range contains the key.
func (c *Cluster) GetRegionByKey(key []byte) *metapb.Region {
	c.mu.RLock()
	defer c.mu.RUnlock()

	for _, r := range c.regions {
		if regionContains(r.meta.StartKey, r.meta.EndKey, key) {
			return proto.Clone(r.meta).(*metapb.Region)
		}
	}
	return nil
}
Exemple #20
0
// IsHealthy returns nil if the query service is healthy (able to
// connect to the database and serving traffic) or an error explaining
// the unhealthiness otherwise.
func (tsv *TabletServer) IsHealthy() error {
	tsv.mu.Lock()
	target := proto.Clone(&tsv.target).(*querypb.Target)
	tsv.mu.Unlock()
	_, err := tsv.Execute(
		context.Background(),
		target,
		"select 1 from dual",
		nil,
		0,
	)
	return err
}
func TestProtoCopy(t *testing.T) {
	person := InitData("ronaflx", 195936, "*****@*****.**")
	copyPerson := person
	if !proto.Equal(person, copyPerson) {
		t.Errorf("expect:\n%s\nactual:\n%s\n", proto.MarshalTextString(person), proto.MarshalTextString(
			copyPerson))
	}
	msg := proto.Clone(person)
	if !proto.Equal(person, msg) {
		t.Errorf("expect:\n%s\nactual:\n%s\n", proto.MarshalTextString(person), proto.MarshalTextString(
			msg))
	}
}
Exemple #22
0
// Write implements part of the graphstore.Service interface.
func (s *GraphStore) Write(ctx context.Context, req *spb.WriteRequest) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	for _, u := range req.Update {
		s.insert(proto.Clone(&spb.Entry{
			Source:    req.Source,
			EdgeKind:  u.EdgeKind,
			Target:    u.Target,
			FactName:  u.FactName,
			FactValue: u.FactValue,
		}).(*spb.Entry))
	}
	return nil
}
Exemple #23
0
func (c *Client) reader() {
	var err error
	var nr int
	var buf []byte

	defer c.Close()

	for {
		if nr, err = c.nc.Read(buf); err != nil {
			return
		}

		if nr > 0 {
			c.decBuf.WriteRawBytes(buf[0:nr])
			// c.codec.OnMessage(c, c.decBuf, time.Now().UnixNano())

			reader := c.decBuf
			pid, err := reader.ReadU16()
			if err != nil {
				return
			}

			var exists bool
			var callback func(proto.Message)
			if callback, exists = c.callbacks[pid]; !exists {
				return
			}

			prototype := c.prototypes[pid]
			if prototype != nil {
				raw, err := reader.ReadRawBytes()
				if err != nil {
					return
				}

				clone := proto.Clone(prototype)
				err = proto.Unmarshal(raw, clone)
				if err != nil {
					return
				}

				callback(clone)
			} else {
				callback(nil)
			}
		}
	}
}
// Normalize app data
func normalizeApp(a *bspb.BatteryStats_App, totalTimeHour float64) *bspb.BatteryStats_App {
	if a == nil {
		return nil
	}
	res := proto.Clone(a).(*bspb.BatteryStats_App)

	if norm := normalizeMessage(a.GetForeground(), totalTimeHour); norm != nil {
		res.Foreground = norm.(*bspb.BatteryStats_App_Foreground)
	}
	if norm := normalizeAppApk(a.GetApk(), totalTimeHour); norm != nil {
		res.Apk = norm
	}
	normalizeAppChildren(res.GetChild(), totalTimeHour)
	if norm := normalizeMessage(a.GetNetwork(), totalTimeHour); norm != nil {
		res.Network = norm.(*bspb.BatteryStats_App_Network)
	}
	if norm := normalizeMessage(a.GetPowerUseItem(), totalTimeHour); norm != nil {
		res.PowerUseItem = norm.(*bspb.BatteryStats_App_PowerUseItem)
	}
	if norm := normalizeRepeatedMessage(a.GetProcess(), totalTimeHour).Interface(); norm != nil {
		res.Process = norm.([]*bspb.BatteryStats_App_Process)
	}
	if norm := normalizeRepeatedMessage(a.GetSensor(), totalTimeHour).Interface(); norm != nil {
		res.Sensor = norm.([]*bspb.BatteryStats_App_Sensor)
	}
	if norm := normalizeMessage(a.GetStateTime(), totalTimeHour); norm != nil {
		res.StateTime = norm.(*bspb.BatteryStats_App_StateTime)
	}
	if norm := normalizeMessage(a.GetVibrator(), totalTimeHour); norm != nil {
		res.Vibrator = norm.(*bspb.BatteryStats_App_Vibrator)
	}
	if norm := normalizeRepeatedMessage(a.GetWakelock(), totalTimeHour).Interface(); norm != nil {
		res.Wakelock = norm.([]*bspb.BatteryStats_App_Wakelock)
	}
	if norm := normalizeRepeatedMessage(a.GetWakeupAlarm(), totalTimeHour).Interface(); norm != nil {
		res.WakeupAlarm = norm.([]*bspb.BatteryStats_App_WakeupAlarm)
	}
	if norm := normalizeMessage(a.GetWifi(), totalTimeHour); norm != nil {
		res.Wifi = norm.(*bspb.BatteryStats_App_Wifi)
	}
	if norm := normalizeRepeatedMessage(a.GetUserActivity(), totalTimeHour).Interface(); norm != nil {
		res.UserActivity = norm.([]*bspb.BatteryStats_App_UserActivity)
	}
	if norm := normalizeRepeatedMessage(a.GetScheduledJob(), totalTimeHour).Interface(); norm != nil {
		res.ScheduledJob = norm.([]*bspb.BatteryStats_App_ScheduledJob)
	}
	return res
}
Exemple #25
0
// agentRPCTestIsTimeoutErrorDialTimeout verifies that client.IsTimeoutError()
// returns true for RPCs failed due to a connect timeout during .Dial().
func agentRPCTestIsTimeoutErrorDialTimeout(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, ti *topo.TabletInfo) {
	// Connect to a non-existing tablet.
	// For example, this provokes gRPC to return error grpc.ErrClientConnTimeout.
	invalidTi := topo.NewTabletInfo(ti.Tablet, ti.Version())
	invalidTi.Tablet = proto.Clone(invalidTi.Tablet).(*topodatapb.Tablet)
	invalidTi.Tablet.Hostname = "Non-Existent.Server"

	shortCtx, cancel := context.WithTimeout(ctx, time.Millisecond)
	defer cancel()
	err := client.Ping(shortCtx, invalidTi)
	if err == nil {
		t.Fatal("agentRPCTestIsTimeoutErrorDialTimeout: connect to non-existant tablet did not fail")
	}
	if !client.IsTimeoutError(err) {
		t.Errorf("agentRPCTestIsTimeoutErrorDialTimeout: want: IsTimeoutError() = true. error: %v", err)
	}
}
Exemple #26
0
func TestUnmarshalGolden(t *testing.T) {
	for _, tt := range goldenMessages {
		want := tt.m
		got := proto.Clone(tt.m)
		got.Reset()
		if err := proto.UnmarshalText(tt.t, got); err != nil {
			t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.t, err)
		}
		if !anyEqual(got, want) {
			t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.t, got, want)
		}
		got.Reset()
		if err := proto.UnmarshalText(tt.c, got); err != nil {
			t.Errorf("failed to unmarshal\n%s\nerror: %v", tt.c, err)
		}
		if !anyEqual(got, want) {
			t.Errorf("message:\n%s\ngot:\n%s\nwant:\n%s", tt.c, got, want)
		}
	}
}
Exemple #27
0
func TestUnmarshalling(t *testing.T) {
	for _, tt := range unmarshallingTests {
		// Make a new instance of the type of our expected object.
		p := proto.Clone(tt.pb)
		p.Reset()

		err := UnmarshalString(tt.json, p)
		if err != nil {
			t.Error(err)
			continue
		}

		// For easier diffs, compare text strings of the protos.
		exp := proto.MarshalTextString(tt.pb)
		act := proto.MarshalTextString(p)
		if string(exp) != string(act) {
			t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
		}
	}
}
Exemple #28
0
// UpdateStream is part of tabletconn.TabletConn.
func (itc *internalTabletConn) UpdateStream(ctx context.Context, target *querypb.Target, position string, timestamp int64) (tabletconn.StreamEventReader, error) {
	result := make(chan *querypb.StreamEvent, 10)
	var finalErr error

	go func() {
		finalErr = itc.tablet.qsc.QueryService().UpdateStream(ctx, target, position, timestamp, func(reply *querypb.StreamEvent) error {
			// We need to deep-copy the reply before returning,
			// because the underlying buffers are reused.
			result <- proto.Clone(reply).(*querypb.StreamEvent)
			return nil
		})
		finalErr = tabletconn.TabletErrorFromGRPC(vterrors.ToGRPCError(finalErr))

		// the client will only access finalErr after the
		// channel is closed, and then it's already set.
		close(result)
	}()

	return &updateStreamAdapter{result, &finalErr}, nil
}
Exemple #29
0
func TestEndToEndValid(t *testing.T) {
	t.Parallel()
	for _, test := range [][]proto.Message{
		{&Empty{}},
		{&GoEnum{Foo: FOO_FOO1.Enum()}, &Empty{}, &GoEnum{Foo: FOO_FOO1.Enum()}},
		{&GoEnum{Foo: FOO_FOO1.Enum()}},
		{&Strings{
			StringField: proto.String(`This is my gigantic, unhappy string.  It exceeds
the encoding size of a single byte varint.  We are using it to fuzz test the
correctness of the header decoding mechanisms, which may prove problematic.
I expect it may.  Let's hope you enjoy testing as much as we do.`),
		}},
	} {
		var buf bytes.Buffer
		var written int
		for i, msg := range test {
			n, err := WriteDelimited(&buf, msg)
			if err != nil {
				// Assumption: TestReadDelimited and TestWriteDelimited are sufficient
				//             and inputs for this test are explicitly exercised there.
				t.Fatalf("WriteDelimited(buf, %v[%d]) = ?, %v; wanted ?, nil", test, i, err)
			}
			written += n
		}
		var read int
		for i, msg := range test {
			out := proto.Clone(msg)
			out.Reset()
			n, _ := ReadDelimited(&buf, out)
			// Decide to do EOF checking?
			read += n
			if !proto.Equal(out, msg) {
				t.Fatalf("out = %v; want %v[%d] = %#v", out, test, i, msg)
			}
		}
		if read != written {
			t.Fatalf("%v read = %d; want %d", test, read, written)
		}
	}
}
Exemple #30
0
func (codec *GPBCodec) OnMessage(conn *Conn, buf *Buffer, receiveTime int64) {

	// header
	size, err := buf.ReadU16()
	if err != nil {
		return
	}

	// pid
	pid, err := buf.ReadU16()
	if err != nil {
		return
	}

	var exists bool
	var callback func(*Conn, proto.Message)
	if callback, exists = codec.callbacks[pid]; !exists {
		return
	}

	prototype := codec.prototypes[pid]
	if prototype != nil {
		raw, err := buf.ReadSizedBytes(int(size - 2))
		if err != nil {
			return
		}

		clone := proto.Clone(prototype)
		err = proto.Unmarshal(raw, clone)
		if err != nil {
			return
		}

		callback(conn, clone)
	} else {
		callback(conn, nil)
	}
}