コード例 #1
0
ファイル: unixlistener_test.go プロジェクト: mistifyio/acomm
func (s *UnixListenerTestSuite) TestSendAndUnmarshalConnData() {
	if !s.NoError(s.Listener.Start(), "should start successfully") {
		return
	}

	in := map[string]string{
		"foo": "bar",
	}

	addr, _ := net.ResolveUnixAddr("unix", s.Listener.Addr())
	conn, err := net.DialUnix("unix", nil, addr)
	if !s.NoError(err, "failed to dial listener") {
		return
	}
	_ = acomm.SendConnData(conn, in)
	_ = conn.Close()

	lConn := s.Listener.NextConn()
	if !s.NotNil(lConn, "connection should not be nil") {
		return
	}

	out := map[string]string{}
	s.NoError(acomm.UnmarshalConnData(lConn, &out), "should succeed unmarshalling")

	s.Equal(in, out, "should have unmarshaled the correct data")

	s.Listener.DoneConn(lConn)
}
コード例 #2
0
ファイル: task.go プロジェクト: mistifyio/provider
func (t *task) acceptRequest(conn net.Conn) {
	defer t.reqListener.DoneConn(conn)
	var respErr error

	req := &acomm.Request{}
	if err := acomm.UnmarshalConnData(conn, req); err != nil {
		respErr = err
	}

	if err := req.Validate(); err != nil {
		respErr = err
	}

	// Respond to the initial request
	resp, err := acomm.NewResponse(req, nil, nil, respErr)
	if err != nil {
		log.WithFields(log.Fields{
			"error":   err,
			"req":     req,
			"respErr": respErr,
		}).Error("failed to create initial response")
		return
	}

	if err := acomm.SendConnData(conn, resp); err != nil {
		return
	}

	if respErr != nil {
		return
	}
	// Actually perform the task
	t.waitgroup.Add(1)
	go t.handleRequest(req)
}
コード例 #3
0
ファイル: server.go プロジェクト: mistifyio/coordinator
func (s *Server) acceptInternalRequest(conn net.Conn) {
	defer s.internal.DoneConn(conn)
	var respErr error
	req := &acomm.Request{}
	defer func() {
		// Respond to the initial request
		resp, err := acomm.NewResponse(req, nil, nil, respErr)
		if err != nil {
			log.WithFields(log.Fields{
				"error":   err,
				"req":     req,
				"respErr": respErr,
			}).Error("failed to create initial response")
			return
		}

		if err := acomm.SendConnData(conn, resp); err != nil {
			log.WithFields(log.Fields{
				"error":   err,
				"req":     req,
				"respErr": respErr,
			}).Error("failed to create initial response")
			return
		}
	}()

	if err := acomm.UnmarshalConnData(conn, req); err != nil {
		respErr = err
		return
	}

	if err := req.Validate(); err != nil {
		respErr = err
		return
	}

	if err := s.handleRequest(req); err != nil {
		respErr = err
		return
	}

	return
}
コード例 #4
0
ファイル: server_test.go プロジェクト: mistifyio/coordinator
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
}
コード例 #5
0
ファイル: response_test.go プロジェクト: mistifyio/acomm
func (s *ResponseTestSuite) TestSend() {
	// Mock HTTP response server
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		resp := &acomm.Response{}
		body, err := ioutil.ReadAll(r.Body)
		s.NoError(err, "should not fail reading body")
		s.NoError(json.Unmarshal(body, resp), "should not fail unmarshalling response")
		ack, _ := json.Marshal(&acomm.Response{})
		_, _ = w.Write(ack)
		s.Responses <- resp
	}))
	defer ts.Close()

	// Mock Unix response listener
	f, err := ioutil.TempFile("", "acommTest-")
	if !s.NoError(err, "failed to create test unix socket") {
		return
	}
	_ = f.Close()
	_ = os.Remove(f.Name())
	socketPath := fmt.Sprintf("%s.sock", f.Name())
	listener, err := net.Listen("unix", socketPath)
	if !s.NoError(err, "failed to listen on unix socket") {
		return
	}
	defer func() { _ = listener.Close() }()
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				return
			}
			resp := &acomm.Response{}
			s.NoError(acomm.UnmarshalConnData(conn, resp), "should not fail unmarshalling conn data")
			_ = acomm.SendConnData(conn, &acomm.Response{})
			s.Responses <- resp
			_ = conn.Close()
		}
	}()

	resultJ, _ := json.Marshal(map[string]string{"foo": "bar"})
	response := &acomm.Response{
		ID:     uuid.New(),
		Result: (*json.RawMessage)(&resultJ),
	}

	tests := []struct {
		responseHook string
		expectedErr  bool
	}{
		{ts.URL, false},
		{"http://badpath", true},
		{fmt.Sprintf("unix://%s", socketPath), false},
		{fmt.Sprintf("unix://%s", "badpath"), true},
		{"foobar://", true},
	}

	for _, test := range tests {
		msg := testMsgFunc(test.responseHook)
		u, _ := url.ParseRequestURI(test.responseHook)
		err := acomm.Send(u, response)
		resp := s.NextResp()
		if test.expectedErr {
			s.Error(err, msg("send should fail"))
			s.Nil(resp, msg("response hook should not receive a response"))
		} else {
			if !s.NoError(err, msg("send should not fail")) {
				continue
			}
			s.Equal(response.ID, resp.ID, msg("response should be what was sent"))
		}
	}
}