本文整理匯總了Golang中github.com/gogo/protobuf/proto.Equal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Equal函數的具體用法?Golang Equal怎麽用?Golang Equal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Equal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestClone
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")
}
}
示例2: TestAddInfoSameKeyDifferentHops
// Verify that adding two infos with different hops but same keys
// always chooses the minimum hops.
func TestAddInfoSameKeyDifferentHops(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper := stop.NewStopper()
defer stopper.Stop()
is := newInfoStore(context.TODO(), 1, emptyAddr, stopper)
info1 := is.newInfo(nil, time.Second)
info1.Hops = 1
info2 := is.newInfo(nil, time.Second)
info2.Value.Timestamp.WallTime = info1.Value.Timestamp.WallTime
info2.Hops = 2
if err := is.addInfo("a", info1); err != nil {
t.Errorf("failed insert: %s", err)
}
if err := is.addInfo("a", info2); err == nil {
t.Errorf("shouldn't have inserted info 2: %s", err)
}
i := is.getInfo("a")
if i.Hops != info1.Hops || !proto.Equal(i, info1) {
t.Error("failed to properly combine hops and value", i)
}
// Try yet another info, with lower hops yet (0).
info3 := is.newInfo(nil, time.Second)
if err := is.addInfo("a", info3); err != nil {
t.Error(err)
}
i = is.getInfo("a")
if i.Hops != info3.Hops || !proto.Equal(i, info3) {
t.Error("failed to properly combine hops and value", i)
}
}
示例3: TestSystemTableLiterals
// TestSystemTableLiterals compares the result of evaluating the `CREATE TABLE`
// statement strings that describe each system table with the TableDescriptor
// literals that are actually used at runtime. This ensures we can use the hand-
// written literals instead of having to evaluate the `CREATE TABLE` statements
// before initialization and with limited SQL machinery bootstraped, while still
// confident that the result is the same as if `CREATE TABLE` had been run.
//
// This test may also be useful when writing a new system table:
// adding the new schema along with a trivial, empty TableDescriptor literal
// will print the expected proto which can then be used to replace the empty
// one (though pruning the explicit zero values may make it more readable).
func TestSystemTableLiterals(t *testing.T) {
defer leaktest.AfterTest(t)()
type testcase struct {
id sqlbase.ID
schema string
pkg sqlbase.TableDescriptor
}
// test the tables with specific permissions
for _, test := range []testcase{
{keys.NamespaceTableID, sqlbase.NamespaceTableSchema, sqlbase.NamespaceTable},
{keys.DescriptorTableID, sqlbase.DescriptorTableSchema, sqlbase.DescriptorTable},
{keys.UsersTableID, sqlbase.UsersTableSchema, sqlbase.UsersTable},
{keys.ZonesTableID, sqlbase.ZonesTableSchema, sqlbase.ZonesTable},
} {
gen, err := sql.CreateTestTableDescriptor(
keys.SystemDatabaseID,
test.id,
test.schema,
sqlbase.NewPrivilegeDescriptor(security.RootUser, sqlbase.SystemConfigAllowedPrivileges[test.id]),
)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(&test.pkg, &gen) {
t.Fatalf(
"mismatch between re-generated version and pkg version of %s:\npkg:\n\t%#v\ngenerated\n\t%#v",
test.pkg.Name, test.pkg, gen)
}
}
// test the tables with non-specific NewDefaultPrivilegeDescriptor
for _, test := range []testcase{
{keys.LeaseTableID, sqlbase.LeaseTableSchema, sqlbase.LeaseTable},
{keys.EventLogTableID, sqlbase.EventLogTableSchema, sqlbase.EventLogTable},
{keys.RangeEventTableID, sqlbase.RangeEventTableSchema, sqlbase.RangeEventTable},
{keys.UITableID, sqlbase.UITableSchema, sqlbase.UITable},
} {
gen, err := sql.CreateTestTableDescriptor(
keys.SystemDatabaseID, test.id, test.schema, sqlbase.NewDefaultPrivilegeDescriptor(),
)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(&test.pkg, &gen) {
s1 := fmt.Sprintf("%#v", test.pkg)
s2 := fmt.Sprintf("%#v", gen)
for i := range s1 {
if s1[i] != s2[i] {
t.Fatalf(
"mismatch between %s:\npkg:\n\t%#v\npartial-gen\n\t%#v\ngen\n\t%#v",
test.pkg.Name, s1[:i+3], s2[:i+3], gen)
}
}
panic("did not locate mismatch between re-generated version and pkg version")
}
}
}
示例4: TestClientGetAndPutProto
// TestClientGetAndPutProto verifies gets and puts of protobufs using the
// client's convenience methods.
func TestClientGetAndPutProto(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
db := createTestClient(t, s)
zoneConfig := config.ZoneConfig{
NumReplicas: 2,
Constraints: config.Constraints{Constraints: []config.Constraint{{Value: "mem"}}},
RangeMinBytes: 1 << 10, // 1k
RangeMaxBytes: 1 << 18, // 256k
}
key := roachpb.Key(testUser + "/zone-config")
if err := db.Put(context.TODO(), key, &zoneConfig); err != nil {
t.Fatalf("unable to put proto: %s", err)
}
var readZoneConfig config.ZoneConfig
if err := db.GetProto(context.TODO(), key, &readZoneConfig); err != nil {
t.Fatalf("unable to get proto: %s", err)
}
if !proto.Equal(&zoneConfig, &readZoneConfig) {
t.Errorf("expected %+v, but found %+v", zoneConfig, readZoneConfig)
}
}
示例5: TestClientGetAndPutProto
// TestClientGetAndPutProto verifies gets and puts of protobufs using the
// client's convenience methods.
func TestClientGetAndPutProto(t *testing.T) {
defer leaktest.AfterTest(t)
s := server.StartTestServer(t)
defer s.Stop()
db := createTestClient(t, s.Stopper(), s.ServingAddr())
zoneConfig := &config.ZoneConfig{
ReplicaAttrs: []roachpb.Attributes{
{Attrs: []string{"dc1", "mem"}},
{Attrs: []string{"dc2", "mem"}},
},
RangeMinBytes: 1 << 10, // 1k
RangeMaxBytes: 1 << 18, // 256k
}
key := roachpb.Key(testUser + "/zone-config")
if pErr := db.Put(key, zoneConfig); pErr != nil {
t.Fatalf("unable to put proto: %s", pErr)
}
readZoneConfig := &config.ZoneConfig{}
if pErr := db.GetProto(key, readZoneConfig); pErr != nil {
t.Fatalf("unable to get proto: %v", pErr)
}
if !proto.Equal(zoneConfig, readZoneConfig) {
t.Errorf("expected %+v, but found %+v", zoneConfig, readZoneConfig)
}
}
示例6: TestClientForwardUnresolved
// TestClientForwardUnresolved verifies that a client does not resolve a forward
// address prematurely.
func TestClientForwardUnresolved(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper := stop.NewStopper()
defer stopper.Stop()
const nodeID = 1
local := startGossip(nodeID, stopper, t, metric.NewRegistry())
addr := local.GetNodeAddr()
client := newClient(log.AmbientContext{}, addr, makeMetrics()) // never started
newAddr := util.UnresolvedAddr{
NetworkField: "tcp",
AddressField: "localhost:2345",
}
reply := &Response{
NodeID: nodeID,
Addr: *addr,
AlternateNodeID: nodeID + 1,
AlternateAddr: &newAddr,
}
if err := client.handleResponse(
context.TODO(), local, reply,
); !testutils.IsError(err, "received forward") {
t.Fatal(err)
}
if !proto.Equal(client.forwardAddr, &newAddr) {
t.Fatalf("unexpected forward address %v, expected %v", client.forwardAddr, &newAddr)
}
}
示例7: TestRoundTripProto3
func TestRoundTripProto3(t *testing.T) {
m := &pb.Message{
Name: "David", // (2 | 1<<3): 0x0a 0x05 "David"
Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01
HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01
Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
ResultCount: 47, // (0 | 7<<3): 0x38 0x2f
TrueScotsman: true, // (0 | 8<<3): 0x40 0x01
Score: 8.1, // (5 | 9<<3): 0x4d <8.1>
Key: []uint64{1, 0xdeadbeef},
Nested: &pb.Nested{
Bunny: "Monty",
},
}
t.Logf(" m: %v", m)
b, err := proto.Marshal(m)
if err != nil {
t.Fatalf("proto.Marshal: %v", err)
}
t.Logf(" b: %q", b)
m2 := new(pb.Message)
if err := proto.Unmarshal(b, m2); err != nil {
t.Fatalf("proto.Unmarshal: %v", err)
}
t.Logf("m2: %v", m2)
if !proto.Equal(m, m2) {
t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
}
}
示例8: TestProto3SetDefaults
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)
}
}
示例9: TestClientForwardUnresolved
// TestClientForwardUnresolved verifies that a client does not resolve a forward
// address prematurely.
func TestClientForwardUnresolved(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper := stop.NewStopper()
defer stopper.Stop()
const nodeID = 1
local := startGossip(nodeID, stopper, t)
local.mu.Lock()
addr := local.is.NodeAddr
local.mu.Unlock()
client := newClient(&addr) // never started
newAddr := util.UnresolvedAddr{
NetworkField: "tcp",
AddressField: "localhost:2345",
}
reply := &Response{
NodeID: nodeID,
Addr: addr,
AlternateNodeID: nodeID + 1,
AlternateAddr: &newAddr,
}
if err := client.handleResponse(local, reply); !testutils.IsError(err, "received forward") {
t.Fatal(err)
}
if !proto.Equal(client.forwardAddr, &newAddr) {
t.Fatalf("unexpected forward address %v, expected %v", client.forwardAddr, &newAddr)
}
}
示例10: TestKVClientGetAndPutProto
// TestKVClientGetAndPutProto verifies gets and puts of protobufs using the
// KV client's convenience methods.
func TestKVClientGetAndPutProto(t *testing.T) {
s := StartTestServer(t)
defer s.Stop()
kvClient := createTestClient(s.HTTPAddr)
kvClient.User = storage.UserRoot
zoneConfig := &proto.ZoneConfig{
ReplicaAttrs: []proto.Attributes{
{Attrs: []string{"dc1", "mem"}},
{Attrs: []string{"dc2", "mem"}},
},
RangeMinBytes: 1 << 10, // 1k
RangeMaxBytes: 1 << 18, // 256k
}
key := proto.Key("zone-config")
if err := kvClient.PutProto(key, zoneConfig); err != nil {
t.Fatalf("unable to put proto: %s", err)
}
readZoneConfig := &proto.ZoneConfig{}
ok, ts, err := kvClient.GetProto(key, readZoneConfig)
if !ok || err != nil {
t.Fatalf("unable to get proto ok? %t: %s", ok, err)
}
if ts.Equal(proto.ZeroTimestamp) {
t.Error("expected non-zero timestamp")
}
if !gogoproto.Equal(zoneConfig, readZoneConfig) {
t.Errorf("expected zone configs equal; %+v != %+v", zoneConfig, readZoneConfig)
}
}
示例11: TestBatchProto
func TestBatchProto(t *testing.T) {
defer leaktest.AfterTest(t)()
stopper := stop.NewStopper()
defer stopper.Stop()
e := NewInMem(roachpb.Attributes{}, 1<<20)
stopper.AddCloser(e)
b := e.NewBatch()
defer b.Close()
val := roachpb.MakeValueFromString("value")
if _, _, err := PutProto(b, mvccKey("proto"), &val); err != nil {
t.Fatal(err)
}
getVal := &roachpb.Value{}
ok, keySize, valSize, err := b.GetProto(mvccKey("proto"), getVal)
if !ok || err != nil {
t.Fatalf("expected GetProto to success ok=%t: %s", ok, err)
}
if keySize != 6 {
t.Errorf("expected key size 6; got %d", keySize)
}
data, err := protoutil.Marshal(&val)
if err != nil {
t.Fatal(err)
}
if valSize != int64(len(data)) {
t.Errorf("expected value size %d; got %d", len(data), valSize)
}
if !proto.Equal(getVal, &val) {
t.Errorf("expected %v; got %v", &val, getVal)
}
// Before commit, proto will not be available via engine.
if ok, _, _, err := e.GetProto(mvccKey("proto"), getVal); ok || err != nil {
t.Fatalf("expected GetProto to fail ok=%t: %s", ok, err)
}
// Commit and verify the proto can be read directly from the engine.
if err := b.Commit(); err != nil {
t.Fatal(err)
}
if ok, _, _, err := e.GetProto(mvccKey("proto"), getVal); !ok || err != nil {
t.Fatalf("expected GetProto to success ok=%t: %s", ok, err)
}
if !proto.Equal(getVal, &val) {
t.Errorf("expected %v; got %v", &val, getVal)
}
}
示例12: TestTimeSeriesToValue
func TestTimeSeriesToValue(t *testing.T) {
tsOriginal := &InternalTimeSeriesData{
StartTimestampNanos: 1415398729000000000,
SampleDurationNanos: 1000000000,
Samples: []*InternalTimeSeriesSample{
{
Offset: 1,
Count: 1,
Sum: 64,
},
{
Offset: 2,
Count: 1,
Sum: 2,
},
{
Offset: 3,
Count: 1,
Sum: 3,
},
},
}
// Wrap the TSD into a Value
valueOriginal, err := tsOriginal.ToValue()
if err != nil {
t.Fatalf("error marshaling InternalTimeSeriesData: %s", err.Error())
}
if a, e := valueOriginal.GetTag(), ValueType_TIMESERIES; a != e {
t.Errorf("Value did not have expected tag value of %s, had %s", e, a)
}
// Ensure the Value's 'bytes' field contains the marshalled TSD
tsEncoded, err := gogoproto.Marshal(tsOriginal)
if err != nil {
t.Fatalf("error marshaling TimeSeriesData: %s", err.Error())
}
if a, e := valueOriginal.Bytes, tsEncoded; !bytes.Equal(a, e) {
t.Errorf("bytes field was not properly encoded: expected %v, got %v", e, a)
}
// Extract the TSD from the Value
tsNew, err := InternalTimeSeriesDataFromValue(valueOriginal)
if err != nil {
t.Errorf("error extracting Time Series: %s", err.Error())
}
if !gogoproto.Equal(tsOriginal, tsNew) {
t.Errorf("extracted time series not equivalent to original; %v != %v", tsNew, tsOriginal)
}
// Make sure ExtractTimeSeries doesn't work on non-TimeSeries values
valueNotTs := &Value{
Bytes: []byte("testvalue"),
}
if _, err := InternalTimeSeriesDataFromValue(valueNotTs); err == nil {
t.Errorf("did not receive expected error when extracting TimeSeries from regular Byte value.")
}
}
示例13: TestMerge
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)
}
}
}
示例14: TestUpdateOffsetOnHeartbeat
func TestUpdateOffsetOnHeartbeat(t *testing.T) {
defer leaktest.AfterTest(t)
stopper := stop.NewStopper()
defer stopper.Stop()
ctx := newNodeTestContext(nil, stopper)
_, ln := newTestServer(t, ctx, false)
// Create a client and set its remote offset. On first heartbeat,
// it will update the server's remote clocks map. We create the
// client manually here to allow us to set the remote offset
// before the first heartbeat.
tlsConfig, err := ctx.GetClientTLSConfig()
if err != nil {
t.Fatal(err)
}
serverAddr := ln.Addr()
client := &Client{
Closed: make(chan struct{}),
addr: util.MakeUnresolvedAddr(serverAddr.Network(), serverAddr.String()),
tlsConfig: tlsConfig,
clock: ctx.localClock,
remoteClocks: ctx.RemoteClocks,
remoteOffset: RemoteOffset{
Offset: 10,
Uncertainty: 5,
MeasuredAt: 20,
},
}
if err = client.connect(); err != nil {
t.Fatal(err)
}
ctx.RemoteClocks.mu.Lock()
remoteAddr := client.RemoteAddr().String()
o := ctx.RemoteClocks.offsets[remoteAddr]
ctx.RemoteClocks.mu.Unlock()
expServerOffset := RemoteOffset{Offset: -10, Uncertainty: 5, MeasuredAt: 20}
if proto.Equal(&o, &expServerOffset) {
t.Errorf("expected updated offset %v, instead %v", expServerOffset, o)
}
ln.Close()
// Remove the offset from RemoteClocks and close the connection from the
// remote end. A new offset for the server should not be added to the clock
// monitor.
ctx.RemoteClocks.mu.Lock()
delete(ctx.RemoteClocks.offsets, remoteAddr)
ln.Close()
ctx.RemoteClocks.mu.Unlock()
ctx.RemoteClocks.mu.Lock()
if offset, ok := ctx.RemoteClocks.offsets[remoteAddr]; ok {
t.Errorf("unexpected updated offset: %v", offset)
}
ctx.RemoteClocks.mu.Unlock()
}
示例15: anyEqual
// anyEqual reports whether two messages which may be google.protobuf.Any or may
// contain google.protobuf.Any fields are equal. We can't use proto.Equal for
// comparison, because semantically equivalent messages may be marshaled to
// binary in different tag order. Instead, trust that TextMarshaler with
// ExpandAny option works and compare the text marshaling results.
func anyEqual(got, want proto.Message) bool {
// if messages are proto.Equal, no need to marshal.
if proto.Equal(got, want) {
return true
}
g := expandedMarshaler.Text(got)
w := expandedMarshaler.Text(want)
return g == w
}