func TestThatWeListenToStdErrUnixSocket(t *testing.T) {
	task, tmpdir := setupTask(t, 4)
	defer os.RemoveAll(tmpdir)

	stdoutListener, stderrListener := setupSockets(t, task)
	defer stdoutListener.Close()
	defer stderrListener.Close()

	expectedMessage := "Some Output"
	secondLogMessage := "toally different"

	emitter, receiveChannel := setupEmitter()
	taskListner := deaagent.NewTaskListener(*task, emitter, testLogger)
	go taskListner.StartListening()

	connection, err := stderrListener.Accept()
	defer connection.Close()
	assert.NoError(t, err)

	_, err = connection.Write([]byte(SOCKET_PREFIX + expectedMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)
	receivedMessage := <-receiveChannel
	assert.Equal(t, expectedMessage, string(receivedMessage.GetMessage()))

	_, err = connection.Write([]byte(secondLogMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)
	receivedMessage = <-receiveChannel
	assert.Equal(t, secondLogMessage, string(receivedMessage.GetMessage()))
}
func TestThatWeListenToStdOutUnixSocket(t *testing.T) {
	task, tmpdir := setupTask(t, 3)
	defer os.RemoveAll(tmpdir)

	stdoutListener, stderrListener := setupSockets(t, task)
	defer stdoutListener.Close()
	defer stderrListener.Close()

	emitter, receiveChannel := setupEmitter()
	taskListner := deaagent.NewTaskListener(*task, emitter, testLogger)
	go taskListner.StartListening()

	expectedMessage := "Some Output"
	secondLogMessage := "toally different"

	connection, err := stdoutListener.Accept()
	defer connection.Close()
	assert.NoError(t, err)

	_, err = connection.Write([]byte(SOCKET_PREFIX + expectedMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)

	receivedMessage := <-receiveChannel

	assert.Equal(t, "1234", receivedMessage.GetAppId())
	assert.Equal(t, "App", receivedMessage.GetSourceName())
	assert.Equal(t, logmessage.LogMessage_OUT, receivedMessage.GetMessageType())
	assert.Equal(t, expectedMessage, string(receivedMessage.GetMessage()))
	assert.Equal(t, []string{"syslog://10.20.30.40:8050"}, receivedMessage.GetDrainUrls())
	assert.Equal(t, "3", receivedMessage.GetSourceId())

	_, err = connection.Write([]byte(secondLogMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)

	receivedMessage = <-receiveChannel

	assert.Equal(t, "1234", receivedMessage.GetAppId())
	assert.Equal(t, "App", receivedMessage.GetSourceName())
	assert.Equal(t, logmessage.LogMessage_OUT, receivedMessage.GetMessageType())
	assert.Equal(t, secondLogMessage, string(receivedMessage.GetMessage()))
	assert.Equal(t, []string{"syslog://10.20.30.40:8050"}, receivedMessage.GetDrainUrls())
	assert.Equal(t, "3", receivedMessage.GetSourceId())

	_, err = connection.Write([]byte("a single line\nthat should be split\n"))
	assert.NoError(t, err)

	receivedMessage = <-receiveChannel
	assert.Equal(t, "a single line", string(receivedMessage.GetMessage()))
	receivedMessage = <-receiveChannel
	assert.Equal(t, "that should be split", string(receivedMessage.GetMessage()))
}
func TestThatWeHandleFourByteOffset(t *testing.T) {
	task, tmpdir := setupTask(t, 3)
	defer os.RemoveAll(tmpdir)

	stdoutListener, stderrListener := setupSockets(t, task)
	defer stdoutListener.Close()
	defer stderrListener.Close()

	emitter, receiveChannel := setupEmitter()
	taskListner := deaagent.NewTaskListener(*task, emitter, testLogger)
	go taskListner.StartListening()

	expectedMessage := "Some Output"
	secondLogMessage := "toally different"

	connection, err := stdoutListener.Accept()
	defer connection.Close()

	assert.NoError(t, err)

	_, err = connection.Write([]byte("\n"))
	_, err = connection.Write([]byte("\n"))

	select {
	case _ = <-receiveChannel:
		t.Error("Should not receive a message")
	case <-time.After(200 * time.Millisecond):
	}

	_, err = connection.Write([]byte("\n\n"))
	_, err = connection.Write([]byte(expectedMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)

	receivedMessage := <-receiveChannel
	assert.Equal(t, expectedMessage, string(receivedMessage.GetMessage()))

	_, err = connection.Write([]byte(secondLogMessage))
	_, err = connection.Write([]byte("\n"))
	assert.NoError(t, err)

	receivedMessage = <-receiveChannel
	assert.Equal(t, secondLogMessage, string(receivedMessage.GetMessage()))
}
示例#4
0
	Describe("StartListening", func() {
		var task *domain.Task
		var tmpdir string
		var stdoutListener, stderrListener net.Listener
		var stdoutConnection, stderrConnection net.Conn
		var message1 = "one"
		var message2 = "two"
		var taskListener *deaagent.TaskListener

		BeforeEach(func() {
			fakeLogSender.Reset()
			task, tmpdir = setupTask(3)

			stdoutListener, stderrListener = setupTaskSockets(task)

			taskListener, _ = deaagent.NewTaskListener(*task, testLogger)
			go taskListener.StartListening()

			var err error
			stdoutConnection, err = stdoutListener.Accept()
			Expect(err).NotTo(HaveOccurred())

			stderrConnection, err = stderrListener.Accept()
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			os.RemoveAll(tmpdir)
			stdoutListener.Close()
			stderrListener.Close()
			stdoutConnection.Close()