// Run exercises a YARPC client against a tchannel server.
486167
func Run(t crossdock.T) {
	fatals := crossdock.Fatals(t)

	encoding := t.Param(params.Encoding)
	server := t.Param(params.Server)
	serverHostPort := fmt.Sprintf("%v:%v", server, serverPort)

	tchannelTransport, err := tchannel.NewChannelTransport(tchannel.ServiceName("yarpc-client"))
	fatals.NoError(err, "Failed to build ChannelTransport")

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "yarpc-client",
		Outbounds: yarpc.Outbounds{
			serverName: {
				Unary: tchannelTransport.NewSingleOutbound(serverHostPort),
			},
		},
	})
	fatals.NoError(dispatcher.Start(), "could not start Dispatcher")
	defer dispatcher.Stop()

	switch encoding {
	case "raw":
		runRaw(t, dispatcher)
	case "json":
		runJSON(t, dispatcher)
	case "thrift":
		runThrift(t, dispatcher)
	default:
		fatals.Fail("", "unknown encoding %q", encoding)
	}
}
504948
func (client *SSHClient) Run(command string, stdout io.Writer, stderr io.Writer) error {
	session, err := client.cryptoClient.NewSession()
	if err != nil {
		return err
	}

	defer session.Close()

	session.Stdout = stdout
	session.Stderr = stderr

	if client.Options.Pty {
		modes := cssh.TerminalModes{
			cssh.ECHO:          0,
			cssh.TTY_OP_ISPEED: 14400,
			cssh.TTY_OP_OSPEED: 14400,
		}
		// Request pseudo terminal
		if err := session.RequestPty(os.Getenv("TERM"), 80, 40, modes); err != nil {
			return err
		}
	}

	return session.Run(command)
}
980534
func Run(engine *Engine, protocol string) (string, chan struct{}, chan struct{}) {
	// launch server
	server, err := transport.Launch(protocol + "://localhost:0")
	if err != nil {
		panic(err)
	}

	// prepare channels
	quit := make(chan struct{})
	done := make(chan struct{})

	// start accepting connections
	engine.Accept(server)

	// prepare shutdown
	go func() {
		// wait for signal
		<-quit

		// errors from close are ignored
		_ = server.Close()

		// close broker
		engine.Close()

		close(done)
	}()

	// get random port
	_, port, _ := net.SplitHostPort(server.Addr().String())

	return port, quit, done
}
857023
func Run(t crossdock.T) {
	client := buildHTTPClient(t)
	assert := crossdock.Assert(t)

	// one valid request before we throw the errors at it
	res := client.Call(t, map[string]string{
		"RPC-Caller":     "yarpc-test",
		"RPC-Service":    "yarpc-test",
		"RPC-Procedure":  "echo",
		"RPC-Encoding":   "json",
		"Context-TTL-MS": "100",
	}, `{"token":"10"}`)
	assert.Equal(200, res.Status,
		"valid request: should respond with status 200")

	// TODO: Uncomment. Currently failing with Node.
	// assert.Equal(`{"token":"10"}`+"\n", res.Body,
	//	"valid request: exact response body")

	assert.JSONEq(`{"token":"10"}`, res.Body,
		"valid request: response matches")

	tests := []struct {
		name    string
		headers map[string]string
		body    string

		wantStatus int
		skipStatus int

		skipBody         string
		wantBody         string
		wantBodyContains string

		// hack to get java/node crossdock tests passing for now :(
		// this is because we're changing when we validate the TTL
		wantBodyOneOf []string
	}{
		{
			name:       "no service",
			headers:    map[string]string{},
			body:       "{}",
			wantStatus: 400,
			wantBodyOneOf: []string{
				"BadRequest: missing service name, procedure, " +
					"caller name, and encoding\n",
				"BadRequest: missing service name, procedure, " +
					"caller name, TTL, and encoding\n",
			},
		},
		{
			name: "wrong service",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "not-yarpc-test",
				"RPC-Procedure":  "echo",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body:       `{"token":"10"}`,
			wantStatus: 501,
			wantBody:   `unrecognized service name "not-yarpc-test", available services: "yarpc-test"` + "\n",
		},
		{
			name: "no procedure",
			headers: map[string]string{
				"RPC-Service": "yarpc-test",
			},
			body:       "{}",
			wantStatus: 400,
			wantBodyOneOf: []string{
				"BadRequest: missing procedure, caller name, and encoding\n",
				"BadRequest: missing procedure, caller name, TTL, and encoding\n",
			},
		},
		{
			name: "no caller",
			headers: map[string]string{
				"RPC-Service":   "yarpc-test",
				"RPC-Procedure": "echo",
			},
			body:       "{}",
			wantStatus: 400,
			wantBodyOneOf: []string{
				"BadRequest: missing caller name and encoding\n",
				"BadRequest: missing caller name, TTL, and encoding\n",
			},
		},
		{
			name: "no handler",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "no-such-procedure",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body:       "{}",
			wantStatus: 501,
			wantBody: `unrecognized procedure ` +
				`"no-such-procedure" for service "yarpc-test"` + "\n",
		},
		{
			name: "no timeout",
			headers: map[string]string{
				"RPC-Caller":    "yarpc-test",
				"RPC-Service":   "yarpc-test",
				"RPC-Procedure": "echo",
			},
			body:       "{}",
			wantStatus: 400,
			wantBodyOneOf: []string{
				"BadRequest: missing encoding\n",
				"BadRequest: missing TTL and encoding\n",
			},
		},
		{
			name: "no encoding",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "echo",
				"Context-TTL-MS": "100",
			},
			body:       "{}",
			wantStatus: 400,
			wantBody:   "missing encoding\n",
		},
		{
			name: "invalid timeout",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "echo",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "moo",
			},
			body:       "{}",
			wantStatus: 400,
			wantBody:   `invalid TTL "moo" for service "yarpc-test" and procedure "echo"` + "\n",
		},
		{
			name: "invalid request",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "echo",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body:       "i am not json",
			wantStatus: 400,
			wantBodyContains: `failed to decode "json" request body ` +
				`for procedure "echo" of service "yarpc-test" from ` +
				`caller "yarpc-test":`,
		},
		{
			name: "encoding mismatch",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "echo",
				"RPC-Encoding":   "thrift",
				"Context-TTL-MS": "100",
			},
			body:       "{}",
			wantStatus: 400,
			wantBody: `failed to decode "json" request body ` +
				`for procedure "echo" of service "yarpc-test" from ` +
				`caller "yarpc-test": expected encoding "json" but got "thrift"` + "\n",
		},
		{
			name: "unexpected error",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "unexpected-error",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body:       "{}",
			wantStatus: 500,
			wantBody:   `error for service "yarpc-test" and procedure "unexpected-error": error` + "\n",
		},
		{
			name: "bad response",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "bad-response",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body:       "{}",
			wantStatus: 400,
			wantBodyContains: `failed to encode "json" response ` +
				`body for procedure "bad-response" of service "yarpc-test" ` +
				`from caller "yarpc-test":`,
		},
		{
			name: "remote bad request",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "phone",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body: `{
				"service": "yarpc-test",
				"procedure": "Echo::echo",
				"body": "not a Thrift payload",
				"transport": {"http": {"host": "` + t.Param(params.Server) + `", "port": 8081}}
			}`,
			wantStatus: 400,
			wantBodyContains: `failed to decode "thrift" request body for procedure "Echo::echo" ` +
				`of service "yarpc-test" from caller "yarpc-test": `,
		},
		{
			name: "remote unexpected error",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "phone",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body: `{
				"service": "yarpc-test",
				"procedure": "unexpected-error",
				"body": "{}",
				"transport": {"http": {"host": "` + t.Param(params.Server) + `", "port": 8081}}
			}`,
			wantStatus:       500,
			wantBodyContains: `error` + "\n",
		},
		{
			name: "remote timeout",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "waitfortimeout/raw",
				"RPC-Encoding":   "raw",
				"Context-TTL-MS": "100",
			},
			wantStatus: 504,
			skipStatus: 400,
			wantBodyContains: `call to procedure "waitfortimeout/raw"` +
				` of service "yarpc-test" from caller "yarpc-test" timed out after`,
		},
		{
			// We call sleep through the proxy Phone.
			// Phone will timeout waiting after Sleep, and yarpc should return an unexpected error.
			// This verifies that we do not forward (aka bubble up) timeout errors.
			name: "unexpected client timeout",
			headers: map[string]string{
				"RPC-Caller":     "yarpc-test",
				"RPC-Service":    "yarpc-test",
				"RPC-Procedure":  "phone",
				"RPC-Encoding":   "json",
				"Context-TTL-MS": "100",
			},
			body: `{
				"service": "yarpc-test",
				"procedure": "sleep",
				"body": "unused",
				"transport": {"http": {"host": "` + t.Param(params.Server) + `", "port": 8081}}
			}`,
			skipBody: `UnexpectedError: error for procedure "phone" of service` +
				` "yarpc-test": BadRequest: unrecognized procedure "sleep" for` +
				` service "yarpc-test"` + "\n",
			wantStatus:       504,
			wantBodyContains: `client timeout for procedure "sleep" of service "yarpc-test" after`,
		},
	}

	for _, tt := range tests {
		res := client.Call(t, tt.headers, tt.body)
		t.Tag("scenario", tt.name)

		if res.Status == tt.skipStatus {
			t.Skipf("%q skipped (skip this status): %v", tt.name, res.Body)
			continue
		}
		if tt.skipBody != "" && tt.skipBody == res.Body {
			t.Skipf("%q skipped (skip this body): %v", tt.name, res.Body)
			continue
		}

		assert.Equal(tt.wantStatus, res.Status, "should respond with expected status")
		if tt.wantBody != "" {
			assert.Equal(tt.wantBody, res.Body, "response body should be informative error")
		}
		if tt.wantBodyContains != "" {
			assert.Contains(res.Body, tt.wantBodyContains,
				"%s: response body should be informative error", tt.name)
		}
	}
}
4541
func Run() {
	mux := NewServeMux(viper.GetString("static"))
	err := ServeMux(
		mux,
		viper.GetString("addr"),
		viper.GetString("port"),
		viper.GetString("cert"),
		viper.GetString("key"),
	)
	if err != nil {
		log.Println(err)
		return
	}
}
1246987
func Run(c *Config) {
	if c != nil {
		SetUp(c)
	}

	err := ircConn.Connect(config.Server)
	if err != nil {
		log.Fatal(err)
	}
	ircConn.Loop()
}
676319
func (c *AddDRACCmd) Run(app subcommands.Application, args []string, env subcommands.Env) int {
	ctx := cli.GetContext(app, c, env)
	// TODO(smut): Validate required fields client-side.
	req := &crimson.CreateDRACRequest{
		Drac: &c.drac,
	}
	client := getClient(ctx)
	resp, err := client.CreateDRAC(ctx, req)
	if err != nil {
		errors.Log(ctx, err)
		return 1
	}
	printDRACs(c.f.tsv, resp)
	return 0
}
980116
func (e *MirrorSecrets) Run(c *fi.Context) error {
	return fi.DefaultDeltaRunMethod(e, c)
}
52607
func (em *clientEventMonitor) run(c *Client) error {
	em.Lock()
	defer em.Unlock()

	if em.active {
		return nil
	}

	go func() {
		r, w := io.Pipe()

		go listenAndDispatch(c, em, r, w)

		select {
		case crc := <-em.closeChannel:
			w.Close()
			r.Close()
			close(em.done)
			crc <- struct{}{}
			return
		}
	}()

	em.active = true
	return nil
}
13339
func (c *GetIPsCmd) Run(app subcommands.Application, args []string, env subcommands.Env) int {
	ctx := cli.GetContext(app, c, env)
	client := getClient(ctx)
	resp, err := client.ListFreeIPs(ctx, &c.req)
	if err != nil {
		errors.Log(ctx, err)
		return 1
	}
	printIPs(c.f.tsv, resp.Ips...)
	return 0
}
1227581
func (e *EventServer) Run(ctx context.Context) error {

	// Create the handler
	e.server = &http.Server{
		Handler: e,
	}

	// Start a custom listener
	addr, _ := net.ResolveUnixAddr("unix", e.socketPath)
	nl, err := net.ListenUnix("unix", addr)
	if err != nil {
		return fmt.Errorf("Unable to start API server: %s", err)
	}

	// We make the socket accesible to all users of the system.
	// TODO: create a trireme group for this
	if err := os.Chmod(addr.String(), 0766); err != nil {
		return fmt.Errorf("Cannot make the socket accessible to all users: %s", err)
	}

	listener := &UIDListener{nl}

	// Start serving HTTP requests in the background
	go e.server.Serve(listener) // nolint

	// Listen for context cancellation to close the socket.
	go func() {
		<-ctx.Done()
		nl.Close() // nolint
	}()

	return nil
}
676779
func Run(sOpts *Options, nOpts *natsd.Options) (*StanServer, error) {
	return RunServerWithOpts(sOpts, nOpts)
}
891829
func (s *SSHRunner) Run(cmd string) error {
	glog.Infof("SSH: %s", cmd)
	sess, err := s.c.NewSession()
	if err != nil {
		return errors.Wrap(err, "NewSession")
	}

	defer func() {
		if err := sess.Close(); err != nil {
			if err != io.EOF {
				glog.Errorf("session close: %v", err)
			}
		}
	}()
	var outB bytes.Buffer
	var errB bytes.Buffer
	err = teeSSH(sess, cmd, &outB, &errB)
	if err != nil {
		return errors.Wrapf(err, "command failed: %s\nstdout: %s\nstderr: %s", cmd, outB.String(), errB.String())
	}
	return nil
}
1083554
func (ipt *IPTables) run(args ...string) error {
	return ipt.runWithOutput(args, nil)
}
714807
func (rp *Reprovider) Run(proc goprocess.Process) {
	ctx := goprocessctx.WithProcessClosing(rp.ctx, proc)
	defer close(rp.closing)

	// dont reprovide immediately.
	// may have just started the daemon and shutting it down immediately.
	// probability( up another minute | uptime ) increases with uptime.
	after := time.After(time.Minute)
	var done doneFunc
	for {
		if rp.tick == 0 {
			after = make(chan time.Time)
		}

		select {
		case <-ctx.Done():
			return
		case done = <-rp.trigger:
		case <-after:
		}

		// 'mute' the trigger channel so when `ipfs bitswap reprovide` is called
		// a 'reprovider is already running' error is returned
		unmute := rp.muteTrigger()

		err := rp.reprovide(ctx)
		if err != nil {
			log.Debug(err)
		}

		if done != nil {
			done(err)
		}

		unmute()

		after = time.After(rp.tick)
	}
}
312607
func (c *GetNICsCmd) Run(app subcommands.Application, args []string, env subcommands.Env) int {
	ctx := cli.GetContext(app, c, env)
	client := getClient(ctx)
	resp, err := client.ListNICs(ctx, &c.req)
	if err != nil {
		errors.Log(ctx, err)
		return 1
	}
	printNICs(c.f.tsv, resp.Nics...)
	return 0
}
809781
func (a *adapter) Run() error {
	// set up a connection to the IRC gateway
	hal.Logger.Debug("irc - starting IRC connection")
	go a.startIRCConnection()
	hal.Logger.Debug("irc - started IRC connection")

	return nil
}