blob: c7886ec66036157810d7501a8f085204c18a53be [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"fmt"
"testing"
"time"
"v.io/x/ref/runtime/internal/rpc/proxy"
"v.io/x/ref/runtime/internal/rpc/stream/manager"
tnaming "v.io/x/ref/runtime/internal/testing/mocks/naming"
"v.io/x/ref/test/testutil"
"v.io/v23"
"v.io/v23/naming"
"v.io/v23/rpc"
)
func TestProxyRPCX(t *testing.T) {
ctx, shutdown := initForTest()
defer shutdown()
p := testutil.NewPrincipal("test")
ctx, err := v23.WithPrincipal(ctx, p)
if err != nil {
t.Fatal(err)
}
srid := naming.FixedRoutingID(0x5555)
crid := naming.FixedRoutingID(0x4444)
prid := naming.FixedRoutingID(0x6666)
sm := manager.XInternalNew(ctx, srid)
cm := manager.XInternalNew(ctx, crid)
ns := tnaming.NewSimpleNamespace()
client, err := XInternalNewClient(cm, ns)
if err != nil {
t.Fatal(err)
}
server, err := XInternalNewServer(ctx, sm, ns, nil, "", client, p)
if err != nil {
t.Fatal(err)
}
_, sd, pep, err := proxy.New("tcp", "127.0.0.1:0", prid, ctx, p)
if err != nil {
t.Fatal(err)
}
defer sd()
fmt.Printf("proxy ep: %v\n", pep)
spec := rpc.ListenSpec{Proxy: pep.Name()}
// the endpoint to the proxy wont be returned here because connecting to the
// proxy happens asynchronously.
_, err = server.Listen(spec)
if err != nil {
t.Fatal(err)
}
if err := server.Serve("", &testServer{}, nil); err != nil {
t.Fatal(err)
}
var proxies []rpc.ProxyStatus
for len(proxies) == 0 {
time.Sleep(100 * time.Millisecond)
t.Log("checking for proxies")
proxies = server.Status().Proxies
}
ep := proxies[0].Endpoint
var got string
if err := client.Call(ctx, ep.Name(), "Echo", []interface{}{"arg"}, []interface{}{&got}); err != nil {
t.Fatal(err)
}
if want := fmt.Sprintf("method:%q,suffix:%q,arg:%q", "Echo", "", "arg"); got != want {
t.Errorf("got %v, want %v", got, want)
}
}