Exemple #1
0
func (cli *grpcClient) OnStart() error {
	cli.QuitService.OnStart()
RETRY_LOOP:

	for {
		conn, err := grpc.Dial(cli.addr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
		if err != nil {
			if cli.mustConnect {
				return err
			} else {
				log.Warn(Fmt("tmsp.grpcClient failed to connect to %v.  Retrying...\n", cli.addr))
				time.Sleep(time.Second * 3)
				continue RETRY_LOOP
			}
		}

		client := types.NewTMSPApplicationClient(conn)

	ENSURE_CONNECTED:
		for {
			_, err := client.Echo(context.Background(), &types.RequestEcho{"hello"}, grpc.FailFast(true))
			if err == nil {
				break ENSURE_CONNECTED
			}
			time.Sleep(time.Second)
		}

		cli.client = client
		return nil
	}
}
Exemple #2
0
func testGRPCSync(t *testing.T, app *types.GRPCApplication) {

	numAppendTxs := 2000

	// Start the listener
	server, err := server.NewGRPCServer("unix://test.sock", app)
	if err != nil {
		Exit(Fmt("Error starting GRPC server: %v", err.Error()))
	}
	defer server.Stop()

	// Connect to the socket
	conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
	if err != nil {
		Exit(Fmt("Error dialing GRPC server: %v", err.Error()))
	}
	defer conn.Close()

	client := types.NewTMSPApplicationClient(conn)

	// Write requests
	for counter := 0; counter < numAppendTxs; counter++ {
		// Send request
		response, err := client.AppendTx(context.Background(), &types.RequestAppendTx{[]byte("test")})
		if err != nil {
			t.Fatalf("Error in GRPC AppendTx: %v", err.Error())
		}
		counter += 1
		if response.Code != types.CodeType_OK {
			t.Error("AppendTx failed with ret_code", response.Code)
		}
		if counter > numAppendTxs {
			t.Fatal("Too many AppendTx responses")
		}
		t.Log("response", counter)
		if counter == numAppendTxs {
			go func() {
				time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
			}()
		}

	}
}