本文整理匯總了Golang中github.com/cockroachdb/cockroach/server.TestServer.RPCContext方法的典型用法代碼示例。如果您正苦於以下問題:Golang TestServer.RPCContext方法的具體用法?Golang TestServer.RPCContext怎麽用?Golang TestServer.RPCContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cockroachdb/cockroach/server.TestServer
的用法示例。
在下文中一共展示了TestServer.RPCContext方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestRequestToUninitializedRange
// TestRequestToUninitializedRange tests the behavior when a request
// is sent to a node which should be a replica of the correct range
// but has not yet received its initial snapshot. This would
// previously panic due to a malformed error response from the server,
// as seen in https://github.com/cockroachdb/cockroach/issues/6027.
//
// Prior to the other changes in the commit that introduced it, this
// test would reliable trigger the panic from #6027. However, it
// relies on some hacky tricks to both trigger the panic and shut down
// cleanly. If this test needs a lot of maintenance in the future we
// should be willing to get rid of it.
func TestRequestToUninitializedRange(t *testing.T) {
defer leaktest.AfterTest(t)()
s := server.TestServer{StoresPerNode: 2}
if err := s.Start(); err != nil {
t.Fatalf("Could not start server: %v", err)
}
defer s.Stop()
// Choose a range ID that is much larger than any that would be
// created by initial splits.
const rangeID = roachpb.RangeID(1000)
// Set up a range with replicas on two stores of the same node. This
// ensures that the DistSender will consider both replicas healthy
// and will try to talk to both (so we can get a non-retryable error
// from the second store).
replica1 := roachpb.ReplicaDescriptor{
NodeID: 1,
StoreID: 1,
ReplicaID: 1,
}
replica2 := roachpb.ReplicaDescriptor{
NodeID: 1,
StoreID: 2,
ReplicaID: 2,
}
// HACK: remove the second store from the node to generate a
// non-retryable error when we try to talk to it.
store2, err := s.Stores().GetStore(2)
if err != nil {
t.Fatal(err)
}
s.Stores().RemoveStore(store2)
// Create the uninitialized range by sending an isolated raft
// message to the first store.
conn, err := s.RPCContext().GRPCDial(s.ServingAddr())
if err != nil {
t.Fatal(err)
}
raftClient := storage.NewMultiRaftClient(conn)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := raftClient.RaftMessage(ctx)
if err != nil {
t.Fatal(err)
}
msg := storage.RaftMessageRequest{
GroupID: rangeID,
ToReplica: replica1,
FromReplica: replica2,
Message: raftpb.Message{
Type: raftpb.MsgApp,
To: 1,
},
}
if err := stream.Send(&msg); err != nil {
t.Fatal(err)
}
// Make sure the replica was created.
store1, err := s.Stores().GetStore(1)
if err != nil {
t.Fatal(err)
}
util.SucceedsSoon(t, func() error {
if replica, err := store1.GetReplica(rangeID); err != nil {
return util.Errorf("failed to look up replica: %s", err)
} else if replica.IsInitialized() {
return util.Errorf("expected replica to be uninitialized")
}
return nil
})
// Create our own DistSender so we can force some requests to the
// bogus range. The DistSender needs to be in scope for its own
// MockRangeDescriptorDB closure.
var sender *kv.DistSender
sender = kv.NewDistSender(&kv.DistSenderContext{
Clock: s.Clock(),
RPCContext: s.RPCContext(),
RangeDescriptorDB: kv.MockRangeDescriptorDB(
func(key roachpb.RKey, considerIntents, useReverseScan bool,
) ([]roachpb.RangeDescriptor, []roachpb.RangeDescriptor, *roachpb.Error) {
if key.Equal(roachpb.RKeyMin) {
// Pass through requests for the first range to the real sender.
desc, err := sender.FirstRange()
if err != nil {
//.........這裏部分代碼省略.........
示例2: TestQuery
//.........這裏部分代碼省略.........
Query: tspb.Query{
Name: "test.metric",
Sources: []string{"source1", "source2"},
Downsampler: tspb.TimeSeriesQueryAggregator_AVG.Enum(),
SourceAggregator: tspb.TimeSeriesQueryAggregator_SUM.Enum(),
Derivative: tspb.TimeSeriesQueryDerivative_NONE.Enum(),
},
Datapoints: []tspb.TimeSeriesDatapoint{
{
TimestampNanos: 505 * 1e9,
Value: 400.0,
},
{
TimestampNanos: 515 * 1e9,
Value: 500.0,
},
{
TimestampNanos: 525 * 1e9,
Value: 600.0,
},
},
},
{
Query: tspb.Query{
Name: "other.metric",
Sources: []string{""},
Downsampler: tspb.TimeSeriesQueryAggregator_AVG.Enum(),
SourceAggregator: tspb.TimeSeriesQueryAggregator_SUM.Enum(),
Derivative: tspb.TimeSeriesQueryDerivative_NONE.Enum(),
},
Datapoints: []tspb.TimeSeriesDatapoint{
{
TimestampNanos: 505 * 1e9,
Value: 200.0,
},
{
TimestampNanos: 515 * 1e9,
Value: 250.0,
},
},
},
{
Query: tspb.Query{
Name: "test.metric",
Sources: []string{"source1", "source2"},
Downsampler: tspb.TimeSeriesQueryAggregator_MAX.Enum(),
SourceAggregator: tspb.TimeSeriesQueryAggregator_MAX.Enum(),
Derivative: tspb.TimeSeriesQueryDerivative_DERIVATIVE.Enum(),
},
Datapoints: []tspb.TimeSeriesDatapoint{
{
TimestampNanos: 505 * 1e9,
Value: 1.0,
},
{
TimestampNanos: 515 * 1e9,
Value: 5.0,
},
{
TimestampNanos: 525 * 1e9,
Value: 5.0,
},
},
},
},
}
conn, err := tsrv.RPCContext().GRPCDial(tsrv.Ctx.Addr)
if err != nil {
t.Fatal(err)
}
response, err := tspb.NewTimeSeriesClient(conn).Query(context.Background(), &tspb.TimeSeriesQueryRequest{
StartNanos: 500 * 1e9,
EndNanos: 526 * 1e9,
Queries: []tspb.Query{
{
Name: "test.metric",
},
{
Name: "other.metric",
},
{
Name: "test.metric",
Downsampler: tspb.TimeSeriesQueryAggregator_MAX.Enum(),
SourceAggregator: tspb.TimeSeriesQueryAggregator_MAX.Enum(),
Derivative: tspb.TimeSeriesQueryDerivative_DERIVATIVE.Enum(),
},
},
})
if err != nil {
t.Fatal(err)
}
for _, r := range response.Results {
sort.Strings(r.Sources)
}
if !proto.Equal(response, expectedResult) {
t.Fatalf("actual response \n%v\n did not match expected response \n%v",
response, expectedResult)
}
}