• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang thrift.NewContext函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/uber/tchannel-go/thrift.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewContext函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: BenchmarkBothSerial

func BenchmarkBothSerial(b *testing.B) {
	serverAddr, err := setupBenchServer()
	require.NoError(b, err, "setupBenchServer failed")

	opts := testutils.NewOpts().SetFramePool(tchannel.NewSyncFramePool())
	clientCh := testutils.NewClient(b, opts)
	for _, addr := range serverAddr {
		clientCh.Peers().Add(addr)
	}

	thriftClient := thrift.NewClient(clientCh, "bench-server", nil)
	client := gen.NewTChanSecondServiceClient(thriftClient)
	ctx, cancel := thrift.NewContext(10 * time.Millisecond)
	client.Echo(ctx, "make connection")
	cancel()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		ctx, cancel := thrift.NewContext(10 * time.Millisecond)
		defer cancel()

		_, err := client.Echo(ctx, "hello world")
		if err != nil {
			b.Errorf("Echo failed: %v", err)
		}
	}
}
开发者ID:gosuper,项目名称:tchannel-go,代码行数:27,代码来源:thrift_bench_test.go


示例2: TestMakeRemoteClient

func TestMakeRemoteClient(t *testing.T) {
	rp := &mocks.Ringpop{}
	rp.On("RegisterListener", mock.Anything).Return()
	rp.On("Lookup", "hello").Return("127.0.0.1:3001")
	rp.On("WhoAmI").Return("127.0.0.1:3000")

	serviceImpl := &servicemocks.TChanRemoteService{}
	serviceImpl.On("RemoteCall", mock.Anything, "hello").Return(nil)
	ctx, _ := thrift.NewContext(0 * time.Second)

	ch, err := tchannel.NewChannel("remote", nil)
	assert.Equal(t, err, nil, "could not create tchannel")

	adapter, err := NewRingpopRemoteServiceAdapter(serviceImpl, rp, ch, RemoteServiceConfiguration{
		RemoteCall: &RemoteServiceRemoteCallConfiguration{
			Key: func(ctx thrift.Context, name string) (string, error) {
				return name, nil
			},
		},
	})

	tchanClient := &mocks.TChanClient{}
	tchanClient.On("Call", mock.Anything, "RemoteService", "RemoteCall", &RemoteServiceRemoteCallArgs{Name: "hello"}, mock.Anything).Return(true, nil)

	cf := adapter.(router.ClientFactory)
	remoteClient := cf.MakeRemoteClient(tchanClient).(TChanRemoteService)
	err = remoteClient.RemoteCall(ctx, "hello")
	assert.Equal(t, err, nil, "calling the remote client gave an error")

	tchanClient.AssertCalled(t, "Call", mock.Anything, "RemoteService", "RemoteCall", &RemoteServiceRemoteCallArgs{Name: "hello"}, mock.Anything)
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:31,代码来源:remoteservice_test.go


示例3: TestRingpopRemoteServiceAdapterCallRemote

func TestRingpopRemoteServiceAdapterCallRemote(t *testing.T) {
	rp := &mocks.Ringpop{}
	rp.On("RegisterListener", mock.Anything).Return()
	rp.On("Lookup", "hello").Return("127.0.0.1:3001", nil)
	rp.On("WhoAmI").Return("127.0.0.1:3000", nil)

	serviceImpl := &servicemocks.TChanRemoteService{}
	serviceImpl.On("RemoteCall", mock.Anything, "hello").Return(nil)
	ctx, _ := thrift.NewContext(0 * time.Second)

	ch, err := tchannel.NewChannel("remote", nil)
	assert.Equal(t, err, nil, "could not create tchannel")

	adapter, err := NewRingpopRemoteServiceAdapter(serviceImpl, rp, ch, RemoteServiceConfiguration{
		RemoteCall: &RemoteServiceRemoteCallConfiguration{
			Key: func(ctx thrift.Context, name string) (string, error) {
				return name, nil
			},
		},
	})
	assert.Equal(t, err, nil, "creation of adator gave an error")

	// Because it is not easily possible to stub a remote call we assert that the remote call failed.
	// If it didn't fail it is likely that the serviceImpl was called, which we assert that it isn't called either
	err = adapter.RemoteCall(ctx, "hello")
	assert.NotEqual(t, err, nil, "we expected an error from the remote call since it could not reach anything over the network")
	serviceImpl.AssertNotCalled(t, "RemoteCall", mock.Anything, "hello")
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:28,代码来源:remoteservice_test.go


示例4: thriftCall

func thriftCall(clientt thrift.TChanClient, headers map[string]string, token string) (*echo.Pong, map[string]string, error) {
	client := echo.NewTChanEchoClient(clientt)

	ctx, cancel := thrift.NewContext(time.Second)
	ctx = thrift.WithHeaders(ctx, headers)
	defer cancel()

	pong, err := client.Echo(ctx, &echo.Ping{Beep: token})
	return pong, ctx.ResponseHeaders(), err
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:10,代码来源:thrift.go


示例5: TestSetForwardedHeader

func TestSetForwardedHeader(t *testing.T) {
	ctx, _ := thrift.NewContext(0 * time.Second)
	ctx = SetForwardedHeader(ctx)
	if ctx.Headers()["ringpop-forwarded"] != "true" {
		t.Errorf("ringpop forwarding header is not set")
	}

	ctx, _ = thrift.NewContext(0 * time.Second)
	ctx = thrift.WithHeaders(ctx, map[string]string{
		"keep": "this key",
	})
	ctx = SetForwardedHeader(ctx)

	if ctx.Headers()["ringpop-forwarded"] != "true" {
		t.Errorf("ringpop forwarding header is not set if there were headers set already")
	}
	if ctx.Headers()["keep"] != "this key" {
		t.Errorf("ringpop forwarding header removed a header that was already present")
	}
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:20,代码来源:forwarder_test.go


示例6: TestHasForwardedHeader

func TestHasForwardedHeader(t *testing.T) {
	ctx, _ := thrift.NewContext(0 * time.Second)
	if HasForwardedHeader(ctx) {
		t.Errorf("ringpop claimed that the forwarded header was set before it was set")
	}
	ctx = SetForwardedHeader(ctx)
	if !HasForwardedHeader(ctx) {
		t.Errorf("ringpop was not able to identify that the forwarded header was set")
	}

	ctx, _ = thrift.NewContext(0 * time.Second)
	ctx = thrift.WithHeaders(ctx, map[string]string{
		"keep": "this key",
	})
	if HasForwardedHeader(ctx) {
		t.Errorf("ringpop claimed that the forwarded header was set before it was set in the case of alread present headers")
	}
	ctx = SetForwardedHeader(ctx)
	if !HasForwardedHeader(ctx) {
		t.Errorf("ringpop was not able to identify that the forwarded header was set in the case of alread present headers")
	}
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:22,代码来源:forwarder_test.go


示例7: Discover

// Discover queries Hyperbahn for a list of peers that are currently
// advertised with the specified service name.
func (c *Client) Discover(serviceName string) ([]string, error) {
	ctx, cancel := thrift.NewContext(time.Second)
	defer cancel()

	result, err := c.hyperbahnClient.Discover(ctx, &hyperbahn.DiscoveryQuery{ServiceName: serviceName})
	if err != nil {
		return nil, err
	}

	var hostPorts []string
	for _, peer := range result.GetPeers() {
		hostPorts = append(hostPorts, servicePeerToHostPort(peer))
	}

	return hostPorts, nil
}
开发者ID:gosuper,项目名称:tchannel-go,代码行数:18,代码来源:discover.go


示例8: makeCall

func makeCall(client gen.TChanSecondService) {
	ctx, cancel := thrift.NewContext(*timeout)
	defer cancel()

	arg := makeArg()
	started := time.Now()
	res, err := client.Echo(ctx, arg)
	if err != nil {
		fmt.Println("failed:", err)
		return
	}
	if res != arg {
		log.Fatalf("Echo gave different string!")
	}
	duration := time.Since(started)
	fmt.Println(duration)
}
开发者ID:glycerine,项目名称:tchannel-go,代码行数:17,代码来源:main.go


示例9: runClient2

func runClient2(hyperbahnService string, addr net.Addr) error {
	tchan, err := tchannel.NewChannel("client2", optsFor("client2"))
	if err != nil {
		return err
	}
	tchan.Peers().Add(addr.String())
	tclient := thrift.NewClient(tchan, hyperbahnService, nil)
	client := gen.NewTChanSecondClient(tclient)

	go func() {
		for {
			ctx, cancel := thrift.NewContext(time.Second)
			client.Test(ctx)
			cancel()
			time.Sleep(100 * time.Millisecond)
		}
	}()
	return nil
}
开发者ID:uber,项目名称:tchannel-go,代码行数:19,代码来源:main.go


示例10: withSetup

func withSetup(t *testing.T, f func(ctx thrift.Context, args testArgs)) {
	args := testArgs{
		s: new(mocks.TChanTCollector),
	}

	ctx, cancel := thrift.NewContext(time.Second * 10)
	defer cancel()

	// Start server
	tchan := setupServer(t, args.s)
	defer tchan.Close()

	// Get client1
	args.c = getClient(t, tchan.PeerInfo().HostPort)

	f(ctx, args)

	args.s.AssertExpectations(t)
}
开发者ID:gosuper,项目名称:tchannel-go,代码行数:19,代码来源:reporter_test.go


示例11: ThriftCallBuffer

func (c *internalClient) ThriftCallBuffer(latencies []time.Duration) error {
	return c.makeCalls(latencies, func() (time.Duration, error) {
		ctx, cancel := thrift.NewContext(c.opts.timeout)
		defer cancel()

		started := time.Now()
		res, err := c.tClient.Echo(ctx, c.argStr)
		duration := time.Since(started)

		if err != nil {
			return 0, err
		}
		if c.checkResult {
			if res != c.argStr {
				panic("thrift Echo returned wrong result")
			}
		}
		return duration, nil
	})
}
开发者ID:uber,项目名称:tchannel-go,代码行数:20,代码来源:internal_client.go


示例12: runClient1

func runClient1(hyperbahnService string, addr net.Addr) error {
	tchan, err := tchannel.NewChannel("client1", optsFor("client1"))
	if err != nil {
		return err
	}
	tchan.Peers().Add(addr.String())
	tclient := thrift.NewClient(tchan, hyperbahnService, nil)
	client := gen.NewTChanFirstClient(tclient)

	go func() {
		for {
			ctx, cancel := thrift.NewContext(time.Second)
			res, err := client.Echo(ctx, "Hi")
			log.Println("Echo(Hi) = ", res, ", err: ", err)
			log.Println("AppError() = ", client.AppError(ctx))
			log.Println("BaseCall() = ", client.BaseCall(ctx))
			cancel()
			time.Sleep(100 * time.Millisecond)
		}
	}()
	return nil
}
开发者ID:uber,项目名称:tchannel-go,代码行数:22,代码来源:main.go


示例13: main

func main() {
	ch, err := tchannel.NewChannel("tcheck", nil)
	if err != nil {
		fmt.Println("failed to create tchannel:", err)
		os.Exit(1)
	}

	hostsFile := flag.String("hostsFile", "/etc/uber/hyperbahn/hosts.json", "hyperbahn hosts file")
	serviceName := flag.String("serviceName", "hyperbahn", "service name to check health of")
	peer := flag.String("peer", "", "peer to hit directly")

	flag.Parse()

	var config hyperbahn.Configuration
	if *peer != "" {
		config = hyperbahn.Configuration{InitialNodes: []string{*peer}}
	} else {
		config = hyperbahn.Configuration{InitialNodesFile: *hostsFile}
	}
	hyperbahn.NewClient(ch, config, nil)

	thriftClient := thrift.NewClient(ch, *serviceName, nil)
	client := meta.NewTChanMetaClient(thriftClient)

	ctx, cancel := thrift.NewContext(time.Second)
	defer cancel()

	val, err := client.Health(ctx)

	if err != nil {
		fmt.Printf("NOT OK %v\nError: %v\n", *serviceName, err)
		os.Exit(2)
	} else if val.Ok != true {
		fmt.Printf("NOT OK %v\n", *val.Message)
		os.Exit(3)
	} else {
		fmt.Printf("OK\n")
	}
}
开发者ID:uber,项目名称:tcheck,代码行数:39,代码来源:tcheck.go


示例14: TestRingpopRemoteServiceAdapterCallLocal

func TestRingpopRemoteServiceAdapterCallLocal(t *testing.T) {
	rp := &mocks.Ringpop{}
	rp.On("RegisterListener", mock.Anything).Return()
	rp.On("Lookup", "hello").Return("127.0.0.1:3000", nil)
	rp.On("WhoAmI").Return("127.0.0.1:3000", nil)

	serviceImpl := &servicemocks.TChanRemoteService{}
	serviceImpl.On("RemoteCall", mock.Anything, "hello").Return(nil)
	ctx, _ := thrift.NewContext(0 * time.Second)

	adapter, err := NewRingpopRemoteServiceAdapter(serviceImpl, rp, nil, RemoteServiceConfiguration{
		RemoteCall: &RemoteServiceRemoteCallConfiguration{
			Key: func(ctx thrift.Context, name string) (string, error) {
				return name, nil
			},
		},
	})
	assert.Equal(t, err, nil, "creation of adator gave an error")

	err = adapter.RemoteCall(ctx, "hello")
	assert.Equal(t, err, nil, "calling RemoteCall gave an error")

	serviceImpl.AssertCalled(t, "RemoteCall", mock.Anything, "hello")
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:24,代码来源:remoteservice_test.go


示例15: runGauntlet


//.........这里部分代码省略.........
			Function: "TestSet",
			Give: []interface{}{
				map[int32]bool{
					1:  true,
					2:  true,
					-1: true,
					-2: true,
				},
			},
			Want: map[int32]bool{
				1:  true,
				2:  true,
				-1: true,
				-2: true,
			},
		},
		{
			Function: "TestString",
			Give:     []interface{}{token},
			Want:     token,
		},
		{
			Function: "TestStringMap",
			Give: []interface{}{
				map[string]string{
					"foo":   "bar",
					"hello": "world",
				},
			},
			Want: map[string]string{
				"foo":   "bar",
				"hello": "world",
			},
		},
		{
			Function: "TestStruct",
			Give: []interface{}{
				&gauntlet_tchannel.Xtruct{
					StringThing: ptr.String("0"),
					ByteThing:   ptr.Int8(1),
					I32Thing:    ptr.Int32(2),
					I64Thing:    ptr.Int64(3),
				},
			},
			Want: &gauntlet_tchannel.Xtruct{
				StringThing: ptr.String("0"),
				ByteThing:   ptr.Int8(1),
				I32Thing:    ptr.Int32(2),
				I64Thing:    ptr.Int64(3),
			},
		},
		{
			Function: "TestTypedef",
			Give:     []interface{}{gauntlet_tchannel.UserId(42)},
			Want:     gauntlet_tchannel.UserId(42),
		},
		{
			Function: "TestVoid",
			Give:     []interface{}{},
		},
		{
			Service:  "SecondService",
			Function: "BlahBlah",
			Give:     []interface{}{},
		},
		{
			Service:  "SecondService",
			Function: "SecondtestString",
			Give:     []interface{}{"hello"},
			Want:     "hello",
		},
	}

	for _, tt := range tests {
		desc := gauntlet.BuildDesc(tt)

		client := buildClient(t, desc, tt.Service, clientt)
		f := client.MethodByName(tt.Function)
		if !checks.True(f.IsValid(), "%v: invalid function", desc) {
			continue
		}

		ctx, cancel := thrift.NewContext(time.Second)
		defer cancel()

		args := []reflect.Value{reflect.ValueOf(ctx)}
		if give, ok := gauntlet.BuildArgs(t, desc, f.Type(), tt.Give, 1); ok {
			args = append(args, give...)
		} else {
			continue
		}

		got, err := extractCallResponse(t, desc, f.Call(args))
		if isUnrecognizedProcedure(err) {
			t.Skipf("%v: procedure not defined", desc)
			continue
		}
		gauntlet.Assert(t, tt, desc, got, err)
	}
}
开发者ID:yarpc,项目名称:yarpc-go,代码行数:101,代码来源:thrift.go


示例16: createContext

func createContext() (thrift.Context, func()) {
	ctx, cancel := thrift.NewContext(time.Second)
	ctx = thrift.WithHeaders(ctx, map[string]string{"user": curUser})
	return ctx, cancel
}
开发者ID:dansimau,项目名称:ringpop-go,代码行数:5,代码来源:client.go



注:本文中的github.com/uber/tchannel-go/thrift.NewContext函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang thrift.Context类代码示例发布时间:2022-05-28
下一篇:
Golang testutils.WithTestServer函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap