Ejemplo n.º 1
0
func doPublishWith(
	t *testing.T,
	mode mode.ConnectionMode,
	opts outputs.Options,
	data []EventInfo,
	expectedSignals func(int) bool,
) ([]bool, [][]outputs.Data) {
	if data == nil {
		return nil, nil
	}

	numSignals := 0
	for _, pubEvents := range data {
		if pubEvents.Single {
			numSignals += len(pubEvents.Data)
		} else {
			numSignals++
		}
	}

	var expectedData [][]outputs.Data
	ch := make(chan op.SignalResponse, numSignals)
	signal := &op.SignalChannel{ch}
	idx := 0
	for _, pubEvents := range data {
		if pubEvents.Single {
			for _, event := range pubEvents.Data {
				_ = mode.PublishEvent(signal, opts, event)
				if expectedSignals(idx) {
					expectedData = append(expectedData, []outputs.Data{event})
				}
				idx++
			}
		} else {
			_ = mode.PublishEvents(signal, opts, pubEvents.Data)
			if expectedSignals(idx) {
				expectedData = append(expectedData, pubEvents.Data)
			}
			idx++
		}
	}

	var signals []bool
	for i := 0; i < idx; i++ {
		signals = append(signals, <-ch == op.SignalCompleted)
	}

	return signals, expectedData
}
Ejemplo n.º 2
0
func TestMode(
	t *testing.T,
	mode mode.ConnectionMode,
	opts outputs.Options,
	events []EventInfo,
	expectedSignals []bool,
	collectedEvents *[][]common.MapStr,
) {
	defer func() {
		err := mode.Close()
		if err != nil {
			t.Fatal(err)
		}
	}()

	if events == nil {
		return
	}

	numSignals := 0
	for _, pubEvents := range events {
		if pubEvents.Single {
			numSignals += len(pubEvents.Events)
		} else {
			numSignals++
		}
	}

	var expectedEvents [][]common.MapStr
	ch := make(chan op.SignalResponse, numSignals)
	signal := &op.SignalChannel{ch}
	idx := 0
	for _, pubEvents := range events {
		if pubEvents.Single {
			for _, event := range pubEvents.Events {
				_ = mode.PublishEvent(signal, opts, event)
				if expectedSignals[idx] {
					expectedEvents = append(expectedEvents, []common.MapStr{event})
				}
				idx++
			}
		} else {
			_ = mode.PublishEvents(signal, opts, pubEvents.Events)
			if expectedSignals[idx] {
				expectedEvents = append(expectedEvents, pubEvents.Events)
			}
			idx++
		}
	}

	results := make([]bool, len(expectedSignals))
	for i := 0; i < len(expectedSignals); i++ {
		results[i] = <-ch == op.SignalCompleted
	}
	assert.Equal(t, expectedSignals, results)

	if collectedEvents != nil {
		assert.Equal(t, len(expectedEvents), len(*collectedEvents))
		if len(expectedEvents) == len(*collectedEvents) {
			for i := range *collectedEvents {
				expected := expectedEvents[i]
				actual := (*collectedEvents)[i]
				assert.Equal(t, expected, actual)
			}
		}
	}
}