Exemplo n.º 1
0
// Send a batch of events to the bulkWorker and verify that a single
// message is distributed (not triggered by flush timeout).
func TestBulkWorkerSendBatch(t *testing.T) {
	// Setup
	ws := common.NewWorkerSignal()
	defer ws.Stop()

	mh := &testMessageHandler{
		response: CompletedResponse,
		msgs:     make(chan message, queueSize),
	}
	bw := newBulkWorker(ws, queueSize, 0, mh, time.Duration(time.Hour), maxBatchSize)

	events := make([]common.MapStr, maxBatchSize)
	for i := range events {
		events[i] = testEvent()
	}
	s := newTestSignaler()
	m := testBulkMessage(s, events)
	bw.send(m)

	// Validate
	outMsgs, err := mh.waitForMessages(1)
	if err != nil {
		t.Fatal(err)
	}
	assert.True(t, s.wait())
	assert.Len(t, outMsgs[0].events, maxBatchSize)
	assert.Equal(t, m.events[0], outMsgs[0].events[0])
}
Exemplo n.º 2
0
// Test that events sent before shutdown are pushed to the messageHandler.
func TestMessageWorkerShutdownSend(t *testing.T) {
	enableLogging([]string{"*"})

	// Setup
	ws := common.NewWorkerSignal()
	mh := &testMessageHandler{msgs: make(chan message, 10), response: true}
	mw := newMessageWorker(ws, 10, 0, mh)

	// Send an event.
	s1 := newTestSignaler()
	m1 := message{context: Context{Signal: s1}}
	mw.send(m1)

	// Send another event.
	s2 := newTestSignaler()
	m2 := message{context: Context{Signal: s2}}
	mw.send(m2)

	ws.Stop()
	assert.True(t, atomic.LoadUint32(&mh.stopped) == 1)

	// Verify that the messageWorker pushed the two messages to the
	// messageHandler.
	close(mh.msgs)
	assert.Equal(t, 2, len(mh.msgs))

	// Verify the messages and the signals.
	assert.Equal(t, <-mh.msgs, m1)
	assert.True(t, s1.wait())
	assert.Equal(t, <-mh.msgs, m2)
	assert.True(t, s2.wait())
}
Exemplo n.º 3
0
// Test sending events through the messageWorker.
func TestMessageWorkerSend(t *testing.T) {
	enableLogging([]string{"*"})

	// Setup
	ws := common.NewWorkerSignal()
	mh := &testMessageHandler{msgs: make(chan message, 10), response: true}
	mw := newMessageWorker(ws, 10, 0, mh)

	// Send an event.
	s1 := newTestSignaler()
	m1 := message{context: Context{Signal: s1}}
	mw.send(m1)

	// Send another event.
	s2 := newTestSignaler()
	m2 := message{context: Context{Signal: s2}}
	mw.send(m2)

	// Verify that the messageWorker pushed the two messages to the
	// messageHandler.
	msgs, err := mh.waitForMessages(2)
	if err != nil {
		t.Fatal(err)
	}

	// Verify the messages and the signals.
	assert.Contains(t, msgs, m1)
	assert.True(t, s1.wait())
	assert.Contains(t, msgs, m2)
	assert.True(t, s2.wait())

	ws.Stop()
	assert.True(t, atomic.LoadUint32(&mh.stopped) == 1)
}
Exemplo n.º 4
0
// Test OutputWorker by calling onStop() and onMessage() with various inputs.
func TestOutputWorker(t *testing.T) {
	outputer := &testOutputer{events: make(chan common.MapStr, 10)}
	ow := newOutputWorker(
		ucfg.New(),
		outputer,
		common.NewWorkerSignal(),
		1, 0)

	ow.onStop() // Noop

	var testCases = []message{
		testMessage(newTestSignaler(), nil),
		testMessage(newTestSignaler(), testEvent()),
		testBulkMessage(newTestSignaler(), []common.MapStr{testEvent()}),
	}

	for _, m := range testCases {
		sig := m.context.Signal.(*testSignaler)
		ow.onMessage(m)
		assert.True(t, sig.wait())

		if m.event != nil {
			assert.Equal(t, m.event, <-outputer.events)
		} else {
			for _, e := range m.events {
				assert.Equal(t, e, <-outputer.events)
			}
		}
	}
}
Exemplo n.º 5
0
// Send a single event to a bulkWorker and verify that the event
// is sent (flushed) after shutdown.
func TestBulkWorkerShutdownSendSingle(t *testing.T) {
	ws := common.NewWorkerSignal()
	mh := &testMessageHandler{
		response: CompletedResponse,
		msgs:     make(chan message, queueSize),
	}
	bw := newBulkWorker(ws, queueSize, bulkQueueSize, mh, flushInterval, maxBatchSize)

	s := newTestSignaler()
	m := testMessage(s, testEvent())
	bw.send(m)

	ws.Stop()

	close(mh.msgs)
	assert.Equal(t, 1, len(mh.msgs))
	assert.True(t, s.wait())
	assert.Equal(t, m.event, (<-mh.msgs).events[0])
}
Exemplo n.º 6
0
// Send a single event to the bulkWorker and verify that the event
// is sent after the flush timeout occurs.
func TestBulkWorkerSendSingle(t *testing.T) {
	enableLogging([]string{"*"})
	ws := common.NewWorkerSignal()
	defer ws.Stop()

	mh := &testMessageHandler{
		response: CompletedResponse,
		msgs:     make(chan message, queueSize),
	}
	bw := newBulkWorker(ws, queueSize, bulkQueueSize, mh, flushInterval, maxBatchSize)

	s := newTestSignaler()
	m := testMessage(s, testEvent())
	bw.send(m)
	msgs, err := mh.waitForMessages(1)
	if err != nil {
		t.Fatal(err)
	}
	assert.True(t, s.wait())
	assert.Equal(t, m.event, msgs[0].events[0])
}
Exemplo n.º 7
0
// Send more events than the configured maximum batch size and then validate
// that the events are split across two messages.
func TestBulkWorkerSendBatchGreaterThanMaxBatchSize(t *testing.T) {
	// Setup
	ws := common.NewWorkerSignal()
	defer ws.Stop()

	mh := &testMessageHandler{
		response: CompletedResponse,
		msgs:     make(chan message),
	}
	bw := newBulkWorker(ws, queueSize, 0, mh, flushInterval, maxBatchSize)

	// Send
	events := make([]common.MapStr, maxBatchSize+1)
	for i := range events {
		events[i] = testEvent()
	}
	s := newTestSignaler()
	m := testBulkMessage(s, events)
	bw.send(m)

	// Read first message and verify no Completed or Failed signal has
	// been received in the sent message.
	outMsgs, err := mh.waitForMessages(1)
	if err != nil {
		t.Fatal(err)
	}
	assert.False(t, s.isDone())
	assert.Len(t, outMsgs[0].events, maxBatchSize)
	assert.Equal(t, m.events[0:maxBatchSize], outMsgs[0].events[0:maxBatchSize])

	// Read the next message and verify the sent message received the
	// Completed signal.
	outMsgs, err = mh.waitForMessages(1)
	if err != nil {
		t.Fatal(err)
	}
	assert.True(t, s.wait())
	assert.Len(t, outMsgs[0].events, 1)
	assert.Equal(t, m.events[maxBatchSize], outMsgs[0].events[0])
}