Example #1
0
func TestDumpReturnsAllRecentMessagesToMultipleDumpRequests(t *testing.T) {
	var bufferSize uint
	bufferSize = 2
	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())
	dump.Run()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages := dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")
}
func TestThatItSends(t *testing.T) {
	receivedChan := make(chan []byte, 2)

	expectedMessageString := "Some data"
	message := messagetesthelpers.NewMessage(t, expectedMessageString, "myApp01")
	otherMessageString := "Some more stuff"
	otherMessage := messagetesthelpers.NewMessage(t, otherMessageString, "myApp01")

	_, dontKeepAliveChan, _ := testhelpers.AddWSSink(t, receivedChan, SERVER_PORT, "/tail/?app=myApp01")
	WaitForWebsocketRegistration()

	dataReadChannel <- message
	dataReadChannel <- otherMessage

	select {
	case <-time.After(1 * time.Second):
		t.Errorf("Did not get message 1.")
	case message := <-receivedChan:
		messagetesthelpers.AssertProtoBufferMessageEquals(t, expectedMessageString, message)
	}

	select {
	case <-time.After(1 * time.Second):
		t.Errorf("Did not get message 2.")
	case message := <-receivedChan:
		messagetesthelpers.AssertProtoBufferMessageEquals(t, otherMessageString, message)
	}

	dontKeepAliveChan <- true
}
Example #3
0
func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) {
	var bufferSize uint
	bufferSize = 200
	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())
	dump.Run()

	for i := 0; i < 1000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801")

	for i := 1000; i < 2000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")
}
Example #4
0
func TestSendingToErrorChannelDoesNotBlock(t *testing.T) {
	testMessageRouter := NewMessageRouter(1024, false, nil, loggertesthelper.Logger())
	testMessageRouter.errorChannel = make(chan *logmessage.Message, 1)
	go testMessageRouter.Start()

	sinkChannel := make(chan *logmessage.Message, 10)
	ourSink := testSink{sinkChannel, false}

	testMessageRouter.sinkOpenChan <- ourSink
	<-time.After(1 * time.Millisecond)

	for i := 0; i < 10; i++ {
		badMessage := messagetesthelpers.NewMessage(t, "error msg", "appIdWeDontCareAbout")
		badMessage.GetLogMessage().DrainUrls = []string{fmt.Sprintf("<nil%d>", i)}
		testMessageRouter.parsedMessageChan <- badMessage
	}

	goodMessage := messagetesthelpers.NewMessage(t, "error msg", "appId")
	testMessageRouter.parsedMessageChan <- goodMessage

	select {
	case _ = <-ourSink.Channel():
	case <-time.After(1000 * time.Millisecond):
		t.Error("Should have received a message")
	}
}
func TestDumpReturnsAllRecentMessagesToMultipleDumpRequestsWithMessagesCloningInInTheMeantime(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger(), make(chan Sink, 1), time.Second)

	go dump.Run()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")

	logMessage = messagetesthelpers.NewMessage(t, "4", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "3")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "4")
}
Example #6
0
func TestDumpsMostRecentMessagesWithSmallerOutputChan(t *testing.T) {
	var bufferSize uint
	bufferSize = 4
	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())
	dump.Run()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "4", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	receivedChan := make(chan *logmessage.Message, 2)
	dump.Dump(receivedChan)

	firstMessage := <-receivedChan

	assert.Equal(t, string(firstMessage.GetLogMessage().GetMessage()), "3")

	secondMessage := <-receivedChan

	assert.Equal(t, string(secondMessage.GetLogMessage().GetMessage()), "4")
}
Example #7
0
func TestDumpWithLotsOfMessages(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger())

	for i := 0; i < 100; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "98")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "99")

	for i := 100; i < 200; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")

	logMessages = dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")
}
Example #8
0
func TestStopsRetryingWhenSinkIsUnregistered(t *testing.T) {
	testMessageRouter := NewMessageRouter(1024, false, []iprange.IPRange{iprange.IPRange{Start: "10.10.123.1", End: "10.10.123.1"}}, loggertesthelper.Logger())
	go testMessageRouter.Start()

	ourSink := testSink{make(chan *logmessage.Message, 100), true}
	testMessageRouter.sinkOpenChan <- ourSink
	runtime.Gosched()

	go func() {
		message := messagetesthelpers.NewMessage(t, "error msg", "appId")
		message.GetLogMessage().DrainUrls = []string{"syslog://localhost:41223"}
		testMessageRouter.parsedMessageChan <- message

		newMessage := messagetesthelpers.NewMessage(t, "RemoveSyslogSink", "appId")
		testMessageRouter.parsedMessageChan <- newMessage

	}()

	for {
		readMessage := <-ourSink.Channel()
		if string(readMessage.GetLogMessage().GetMessage()) == "RemoveSyslogSink" {
			break
		}
	}

	select {
	case message := <-ourSink.Channel():
		t.Errorf("Should not receive another message after removal; message was %v", string(message.GetLogMessage().GetMessage()))
	case <-time.After(2 * time.Second):
	}
}
Example #9
0
func TestDumpReturnsAllRecentMessagesToMultipleDumpRequests(t *testing.T) {
	testDump := dump.NewDumpSink("myApp", 2, loggertesthelper.Logger(), time.Second)

	dumpRunnerDone := make(chan struct{})
	inputChan := make(chan *logmessage.Message)

	go func() {
		testDump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	inputChan <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	inputChan <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	inputChan <- logMessage

	close(inputChan)
	<-dumpRunnerDone

	logMessages := testDump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")

	logMessages = testDump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")
}
func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) {
	dump := NewDumpSink("myApp", 200, loggertesthelper.Logger(), make(chan Sink, 1), time.Second)

	go dump.Run()

	for i := 0; i < 1000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801")

	for i := 1000; i < 2000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")
}
Example #11
0
func TestDumpAlwaysReturnsTheNewestMessages(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger(), time.Second, fakeTimeProvider)

	dumpRunnerDone := make(chan struct{})

	inputChan := make(chan *logmessage.Message)

	go func() {
		dump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	inputChan <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	inputChan <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	inputChan <- logMessage

	close(inputChan)
	<-dumpRunnerDone

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")

}
func TestSimpleBlacklistRule(t *testing.T) {
	logger := loggertesthelper.Logger()
	sinkManager := NewSinkManager(1024, false, []iprange.IPRange{iprange.IPRange{Start: "10.10.123.1", End: "10.10.123.1"}}, logger)
	oldActiveSyslogSinksCounter := sinkManager.Metrics.SyslogSinks
	go sinkManager.Start()

	incomingLogChan := make(chan []byte, 10)
	testMessageRouter := NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, 2048, logger)
	go testMessageRouter.Start()

	ourSink := testSink{make(chan *logmessage.Message, 100), true}
	sinkManager.sinkOpenChan <- ourSink
	<-time.After(1 * time.Millisecond)

	message := messagetesthelpers.NewMessage(t, "error msg", "appId")
	message.GetLogMessage().DrainUrls = []string{"http://10.10.123.1"}
	testMessageRouter.outgoingLogChan <- message

	select {
	case _ = <-ourSink.Channel():
	case <-time.After(100 * time.Millisecond):
		t.Error("Did not receive real message")
	}

	select {
	case _ = <-ourSink.Channel():
	case <-time.After(100 * time.Millisecond):
		t.Error("Did not message about blacklisted syslog drain")
	}

	testMessageRouter.outgoingLogChan <- message

	select {
	case _ = <-ourSink.Channel():
	case <-time.After(100 * time.Millisecond):
		t.Error("Did not receive real message")
	}

	select {
	case _ = <-ourSink.Channel():
		t.Error("Should not receive another message about the blacklisted url since we cache blacklisted urls")
	case <-time.After(100 * time.Millisecond):
	}

	assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter)

	message = messagetesthelpers.NewMessage(t, "error msg", "appId")
	message.GetLogMessage().DrainUrls = []string{"http://10.10.123.2"}
	testMessageRouter.outgoingLogChan <- message
	waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond)

	assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1)
}
Example #13
0
func TestSysLoggerDiesAndComesBack(t *testing.T) {
	sysLogger := NewSyslogWriterRecorder()
	sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger, make(chan *logmessage.Message))
	sysLogger.SetUp(true)

	done := make(chan bool)
	go func() {
		sink.Run()
		done <- true
	}()

	msg := fmt.Sprintf("first message")
	logMessage := messagetesthelpers.NewMessage(t, msg, "appId")
	sink.Channel() <- logMessage

	select {
	case <-sysLogger.receivedChannel:
		break
	case <-time.After(10 * time.Millisecond):
		t.Error("Should have received a message by now")
	}
	sysLogger.SetUp(false)

	assert.Equal(t, len(sysLogger.ReceivedMessages()), 1)

	for i := 0; i < 11; i++ {
		msg := fmt.Sprintf("message no %v", i)
		logMessage := messagetesthelpers.NewMessage(t, msg, "appId")

		sink.Channel() <- logMessage
	}

	sysLogger.SetUp(true)
	close(sink.Channel())
	<-done

	assert.Equal(t, len(sysLogger.ReceivedMessages()), 11)

	stringOfMessages := fmt.Sprintf("%v", sysLogger.ReceivedMessages())
	assert.Contains(t, stringOfMessages, "first message", "This message should have been there, since the server was up")
	assert.NotContains(t, stringOfMessages, "out: message no 0", "This message should have been lost because the connection problem was detected while trying to send it.")
	assert.Contains(t, stringOfMessages, "out: message no 1", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 2", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 3", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 4", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 5", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 6", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 7", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 8", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 9", "This message should have been there, since it was in the ringbuffer while the server was down")
	assert.Contains(t, stringOfMessages, "out: message no 10", "This message should have been there, since it was in the ringbuffer while the server was down")
}
func TestBufferDropsOldMessages(t *testing.T) {
	channel, buffer := setup(1)
	message1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	channel <- message1
	message2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	channel <- message2

	close(channel)
	buffer.waitForClose()

	dump := buffer.Dump()
	assert.Equal(t, 1, len(dump))
	assert.Equal(t, message2, dump[0])
}
Example #15
0
func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) {
	dump := NewDumpSink("myApp", 200, loggertesthelper.Logger(), time.Second, fakeTimeProvider)

	dumpRunnerDone := make(chan struct{})
	inputChan := make(chan *logmessage.Message)

	go func() {
		dump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	for i := 0; i < 1000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		inputChan <- logMessage
	}

	close(inputChan)
	<-dumpRunnerDone

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801")

	dumpRunnerDone = make(chan struct{})
	inputChan = make(chan *logmessage.Message)

	go func() {
		dump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	for i := 1000; i < 2000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		inputChan <- logMessage
	}

	close(inputChan)
	<-dumpRunnerDone

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")
}
Example #16
0
func TestDumpWithLotsOfMessages(t *testing.T) {
	testDump := dump.NewDumpSink("myApp", 2, loggertesthelper.Logger(), time.Second)

	dumpRunnerDone := make(chan struct{})
	inputChan := make(chan *logmessage.Message)

	go func() {
		testDump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	for i := 0; i < 100; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		inputChan <- logMessage
	}

	close(inputChan)
	<-dumpRunnerDone

	logMessages := testDump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "98")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "99")

	dumpRunnerDone = make(chan struct{})
	inputChan = make(chan *logmessage.Message)

	go func() {
		testDump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	for i := 100; i < 200; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		inputChan <- logMessage
	}

	close(inputChan)
	<-dumpRunnerDone

	logMessages = testDump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")

	logMessages = testDump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")
}
func TestThatItWorksLikeAChannel(t *testing.T) {
	inMessageChan := make(chan *logmessage.Message)
	buffer := NewTruncatingBuffer(inMessageChan, 2, nil)
	go buffer.Run()

	logMessage1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	inMessageChan <- logMessage1
	readMessage := <-buffer.GetOutputChannel()
	assert.Contains(t, string(readMessage.GetRawMessage()), "message 1")

	logMessage2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	inMessageChan <- logMessage2
	readMessage2 := <-buffer.GetOutputChannel()
	assert.Contains(t, string(readMessage2.GetRawMessage()), "message 2")
}
func TestBufferDropsOldMessages(t *testing.T) {
	channel, buffer := setup(1)
	message1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	channel <- message1
	message2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	channel <- message2

	close(channel)
	buffer.WaitForClose()
	dumpChan := make(chan *logmessage.Message, 10)

	buffer.Dump(dumpChan)
	assert.Equal(t, 1, len(dumpChan))
	assert.Equal(t, message2, <-dumpChan)
}
Example #19
0
func TestDumpForTwoMessages(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger())

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages := dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "2")
}
func TestInputChannelAddToBuffer(t *testing.T) {
	channel, buffer := setup(10)
	message1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	channel <- message1
	message2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	channel <- message2

	close(channel)
	buffer.waitForClose()

	dump := buffer.Dump()
	assert.Equal(t, 2, len(dump))
	assert.Equal(t, message1, dump[0])
	assert.Equal(t, message2, dump[1])
}
Example #21
0
func TestSysLoggerComesUpLate(t *testing.T) {
	sysLogger := NewSyslogWriterRecorder()
	sysLogger.SetUp(false)
	sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger)

	done := make(chan bool)
	go func() {
		sink.Run()
		done <- true
	}()

	for i := 0; i < 15; i++ {
		msg := fmt.Sprintf("message no %v", i)
		logMessage := messagetesthelpers.NewMessage(t, msg, "appId")

		sink.Channel() <- logMessage
	}
	close(sink.Channel())

	sysLogger.SetUp(true)
	<-done

	data := sysLogger.ReceivedMessages()
	assert.Equal(t, len(data), 10)

	for i := 0; i < 10; i++ {
		msg := fmt.Sprintf("out: message no %v", i+5)
		assert.Equal(t, data[i], msg)
	}
}
Example #22
0
func xTestDumpSinkClosingTimeIsResetWhenAMessageArrives(t *testing.T) {
	dump := NewDumpSink("myApp", 5, loggertesthelper.Logger(), 10*time.Millisecond, fakeTimeProvider)

	dumpRunnerDone := make(chan struct{})
	inputChan := make(chan *logmessage.Message)

	go func() {
		dump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	fakeTimeProvider.IncrementBySeconds(uint64(5))
	logMessage := messagetesthelpers.NewMessage(t, "0", "appId")
	inputChan <- logMessage
	fakeTimeProvider.IncrementBySeconds(uint64(8))

	fakeTimeProvider.IncrementBySeconds(uint64(3))

	select {
	case sink := <-dumpRunnerDone:
		assert.Equal(t, sink, dump)
	case <-time.After(5 * time.Millisecond):
		assert.Fail(t, "Should have closed")
	}
}
Example #23
0
func TestSinkReportsErrorsOnTheErrorChannel(t *testing.T) {
	sysLogger := NewSyslogWriterRecorder()
	sysLogger.SetUp(false)

	errorChannel := make(chan *logmessage.Message, 10)
	sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger, errorChannel)

	done := make(chan bool)
	go func() {
		sink.Run()
		done <- true
	}()

	msg := fmt.Sprintf("first message")
	logMessage := messagetesthelpers.NewMessage(t, msg, "appId")
	sink.Channel() <- logMessage

	select {
	case errorLog := <-errorChannel:
		errorMsg := string(errorLog.GetLogMessage().GetMessage())
		assert.Contains(t, errorMsg, "Syslog Sink url_not_used: Error when dialing out. Backing off for")
		assert.Contains(t, errorMsg, "Err: Error connecting.")
		assert.Equal(t, errorLog.GetLogMessage().GetSourceName(), "LGR")
	case <-time.After(10 * time.Millisecond):
		t.Error("Should have received an error message by now")
	}
}
Example #24
0
func TestDumpWithLotsOfDumps(t *testing.T) {
	runtime.GOMAXPROCS(runtime.NumCPU())
	dump := NewDumpSink("myApp", 5, loggertesthelper.Logger(), time.Second, fakeTimeProvider)
	dumpRunnerDone := make(chan struct{})
	inputChan := make(chan *logmessage.Message)

	go func() {
		dump.Run(inputChan)
		close(dumpRunnerDone)
	}()

	for i := 0; i < 10; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		inputChan <- logMessage
	}

	close(inputChan)
	<-dumpRunnerDone

	for i := 0; i < 200; i++ {
		go func() {
			logMessages := dump.Dump()

			assert.Equal(t, len(logMessages), 5)
			assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "5")
			assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "6")
		}()
	}
}
func TestThatItDoesNotCreateAnotherSyslogDrainIfItIsAlreadyThere(t *testing.T) {
	logger := loggertesthelper.Logger()
	sinkManager := NewSinkManager(1024, false, nil, logger)
	oldActiveSyslogSinksCounter := sinkManager.Metrics.SyslogSinks
	go sinkManager.Start()

	incomingLogChan := make(chan []byte, 10)
	testMessageRouter := NewMessageRouter(incomingLogChan, testhelpers.UnmarshallerMaker("secret"), sinkManager, 2048, logger)

	go testMessageRouter.Start()
	ourSink := testSink{make(chan *logmessage.Message, 100), false}
	sinkManager.sinkOpenChan <- ourSink
	<-time.After(1 * time.Millisecond)

	message := messagetesthelpers.NewMessage(t, "error msg", "appId")
	message.GetLogMessage().DrainUrls = []string{"http://10.10.123.1"}
	testMessageRouter.outgoingLogChan <- message
	waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond)

	assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1)

	testMessageRouter.outgoingLogChan <- message
	waitForMessageGettingProcessed(t, ourSink, 10*time.Millisecond)

	assert.Equal(t, sinkManager.Metrics.SyslogSinks, oldActiveSyslogSinksCounter+1)
}
func TestBufferHasChannelListenerWithLimitOne(t *testing.T) {
	inChannel, buffer := setup(1)
	message1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	inChannel <- message1
	message2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	inChannel <- message2
	message3 := messagetesthelpers.NewMessage(t, "message 3", "appId")
	inChannel <- message3
	close(inChannel)
	assert.Equal(t, message3, <-buffer.OutputChannel())
	select {
	case <-buffer.OutputChannel():
	case <-time.After(2 * time.Millisecond):
		t.Error("OutputChannel should be closed")
	}
}
Example #27
0
func TestSysLoggerComesUpLate(t *testing.T) {
	sysLogger := NewSyslogWriterRecorder()
	sysLogger.SetUp(false)
	sink := NewSyslogSink("appId", "url_not_used", loggertesthelper.Logger(), sysLogger, make(chan *logmessage.Message, 10))

	done := make(chan bool)
	go func() {
		sink.Run()
		done <- true
	}()

	for i := 0; i < 105; i++ {
		msg := fmt.Sprintf("message no %v", i)
		logMessage := messagetesthelpers.NewMessage(t, msg, "appId")

		sink.Channel() <- logMessage
	}
	close(sink.Channel())

	sysLogger.SetUp(true)
	<-done

	data := sysLogger.ReceivedMessages()
	assert.Equal(t, len(data), 6)

	msg := "err: Log message output too high. We've dropped 100 messages"
	assert.Equal(t, data[0], msg)
	for i := 0; i < 5; i++ {
		msg := fmt.Sprintf("out: message no %v", i+100)
		assert.Equal(t, data[i+1], msg)
	}
}
Example #28
0
func TestThatItWorksLikeAChannel(t *testing.T) {
	inMessageChan := make(chan *logmessage.Message)
	outMessageChan := make(chan *logmessage.Message, 2)
	ringBufferChannel := NewRingBuffer(inMessageChan, outMessageChan, nil)
	go ringBufferChannel.Run()

	logMessage1 := messagetesthelpers.NewMessage(t, "message 1", "appId")
	inMessageChan <- logMessage1
	readMessage := <-outMessageChan
	assert.Contains(t, string(readMessage.GetRawMessage()), "message 1")

	logMessage2 := messagetesthelpers.NewMessage(t, "message 2", "appId")
	inMessageChan <- logMessage2
	readMessage2 := <-outMessageChan
	assert.Contains(t, string(readMessage2.GetRawMessage()), "message 2")

}
Example #29
0
func TestTheDumpSinkNeverFillsUp(t *testing.T) {
	bufferSize := 3
	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())

	logMessage := messagetesthelpers.NewMessage(t, "hi", "appId")

	for i := 0; i < bufferSize+1; i++ {
		dump.Channel() <- logMessage
	}
}
func TestDumpAlwaysReturnsTheNewestMessages(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger(), make(chan Sink, 1), time.Second)

	go dump.Run()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")
}