Ejemplo n.º 1
0
func (s *ServerSuite) createResponseHandlers(result chan *params) (*httptest.Server, *acomm.UnixListener) {
	// HTTP response
	responseServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resp := &acomm.Response{}
		decoder := json.NewDecoder(r.Body)
		_ = decoder.Decode(resp)

		p := &params{}
		_ = resp.UnmarshalResult(p)
		result <- p
	}))

	// Unix response
	responseListener := acomm.NewUnixListener(filepath.Join(s.configData.SocketDir, "testResponse.sock"), 0)
	if !s.NoError(responseListener.Start(), "failed to start task listener") {
		return responseServer, nil
	}

	go func() {
		conn := responseListener.NextConn()
		if conn == nil {
			return
		}
		defer responseListener.DoneConn(conn)

		resp := &acomm.Response{}
		_ = acomm.UnmarshalConnData(conn, resp)
		p := &params{}
		_ = resp.UnmarshalResult(p)
		result <- p
	}()

	return responseServer, responseListener
}
Ejemplo n.º 2
0
func (s *UnixListenerTestSuite) TestStart() {
	s.NoError(s.Listener.Start(), "should start successfully")
	s.Error(s.Listener.Start(), "should error calling start again")

	bad := acomm.NewUnixListener(s.Listener.Addr(), 0)
	s.Error(bad.Start(), "should not be able to start on a used socket")
}
Ejemplo n.º 3
0
// newTask creates and initializes a new task.
func newTask(name, socketPath string, reqTimeout time.Duration, handler TaskHandler) *task {
	return &task{
		name:        name,
		handler:     handler,
		reqTimeout:  reqTimeout,
		reqListener: acomm.NewUnixListener(socketPath, 0),
	}
}
Ejemplo n.º 4
0
func (s *UnixListenerTestSuite) SetupTest() {
	f, err := ioutil.TempFile("", "acommTest-")
	s.Require().NoError(err, "failed to create temp socket")
	s.Require().NoError(f.Close(), "failed to close temp socket file")
	s.Require().NoError(os.Remove(f.Name()), "failed to remove temp socket file")

	s.Socket = fmt.Sprintf("%s.sock", f.Name())
	s.Listener = acomm.NewUnixListener(s.Socket, 0)
}
Ejemplo n.º 5
0
// NewServer creates and initializes a new instance of Server.
func NewServer(config *Config) (*Server, error) {
	if err := config.Validate(); err != nil {
		return nil, err
	}

	var err error
	s := &Server{
		config: config,
	}

	// Internal socket for requests from providers
	internalSocket := filepath.Join(
		config.SocketDir(),
		"coordinator",
		config.ServiceName()+".sock")
	s.internal = acomm.NewUnixListener(internalSocket, 0)

	// External server for requests from outside
	mux := http.NewServeMux()
	mux.HandleFunc("/stream", acomm.ProxyStreamHandler)
	mux.HandleFunc("/", s.externalHandler)
	s.external = &graceful.Server{
		Server: &http.Server{
			Addr:    fmt.Sprintf(":%d", config.ExternalPort()),
			Handler: mux,
		},
		NoSignalHandling: true,
	}

	// Response socket for proxied requests
	responseSocket := filepath.Join(
		config.SocketDir(),
		"response",
		config.ServiceName()+".sock")

	streamURL, err := url.ParseRequestURI(
		fmt.Sprintf("http://%s:%d/stream", getLocalIP(), config.ExternalPort()))
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("failed to generate stream url")
	}
	s.proxy, err = acomm.NewTracker(responseSocket, streamURL, config.RequestTimeout())
	if err != nil {
		return nil, err
	}
	log.WithFields(log.Fields{
		"response": responseSocket,
		"stream":   streamURL.String(),
		"internal": internalSocket,
		"external": fmt.Sprintf(":%d", config.ExternalPort()),
	}).Info("server addresses")

	return s, nil
}
Ejemplo n.º 6
0
func (s *ServerSuite) createTaskListener(taskName string, result chan *params) *acomm.UnixListener {
	taskListener := acomm.NewUnixListener(filepath.Join(s.configData.SocketDir, taskName, "test.sock"), 0)
	if !s.NoError(taskListener.Start(), "failed to start task listener") {
		return nil
	}

	go func() {
		for {
			conn := taskListener.NextConn()
			if conn == nil {
				break
			}
			defer taskListener.DoneConn(conn)
			req := &acomm.Request{}
			if err := acomm.UnmarshalConnData(conn, req); err != nil {
				result <- nil
				continue
			}

			params := &params{}
			_ = req.UnmarshalArgs(params)

			// Respond to the initial request
			resp, _ := acomm.NewResponse(req, nil, nil, nil)
			if err := acomm.SendConnData(conn, resp); err != nil {
				result <- nil
				continue
			}

			// Response to hook
			resp, _ = acomm.NewResponse(req, req.Args, nil, nil)
			if err := req.Respond(resp); err != nil {
				result <- nil
				continue
			}
		}
	}()

	time.Sleep(time.Second)
	return taskListener
}
Ejemplo n.º 7
0
func (s *UnixListenerTestSuite) TestNewUnixListener() {
	s.NotNil(acomm.NewUnixListener("foobar", 0), "should have returned a new UnixListener")
	_ = s.Listener.Start()
}