Ejemplo n.º 1
0
// TestEvent tests the event construction.
func TestEvent(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	event, err := cells.NewEvent("foo", "bar", nil)
	assert.Nil(err)
	assert.Equal(event.Topic(), "foo")
	assert.Equal(event.String(), "<topic: \"foo\" / payload: <\"default\": bar>>")

	bar, ok := event.Payload().Get(cells.DefaultPayload)
	assert.True(ok)
	assert.Equal(bar, "bar")

	_, err = cells.NewEvent("", nil, nil)
	assert.True(cells.IsNoTopicError(err))

	_, err = cells.NewEvent("yadda", nil, nil)
	assert.Nil(err)
}
Ejemplo n.º 2
0
// BenchmarkSmpleEmitNullMonitoring is a simple emitting to one cell
// with the null monitor.
func BenchmarkSmpleEmitNullMonitoring(b *testing.B) {
	env := cells.NewEnvironment("simple-emit")
	defer env.Stop()

	env.StartCell("collector", newCollectBehavior())

	event, _ := cells.NewEvent("foo", "bar", nil)

	for i := 0; i < b.N; i++ {
		env.Emit("collector", event)
	}
}
Ejemplo n.º 3
0
// BenchmarkSmpleEmitStandardMonitoring is a simple emitting to one cell
// with the standard monitor.
func BenchmarkSmpleEmitStandardMonitoring(b *testing.B) {
	monitoring.SetBackend(monitoring.NewStandardBackend())
	env := cells.NewEnvironment("simple-emit-standard")
	defer env.Stop()

	env.StartCell("null", &nullBehavior{})

	event, _ := cells.NewEvent("foo", "bar", nil)

	for i := 0; i < b.N; i++ {
		env.Emit("null", event)
	}
}
Ejemplo n.º 4
0
// TestMapperBehavior tests the mapping of events.
func TestMapperBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	assertPayload := func(collected interface{}, index int, value string) {
		eventData, ok := collected.([]behaviors.EventData)
		assert.True(ok)
		payload, ok := eventData[index].Payload.(cells.Payload)
		assert.True(ok)
		upperText, ok := payload.Get("upper-text")
		assert.True(ok)
		assert.Equal(upperText, value)
	}
	env := cells.NewEnvironment("mapper-behavior")
	defer env.Stop()

	mf := func(id string, event cells.Event) (cells.Event, error) {
		text, ok := event.Payload().Get(cells.DefaultPayload)
		if !ok {
			return event, nil
		}
		pv := cells.PayloadValues{
			"upper-text": strings.ToUpper(text.(string)),
		}
		payload := event.Payload().Apply(pv)
		return cells.NewEvent(event.Topic(), payload, event.Scene())
	}

	env.StartCell("mapper", behaviors.NewMapperBehavior(mf))
	env.StartCell("collector", behaviors.NewCollectorBehavior(10))
	env.Subscribe("mapper", "collector")

	env.EmitNew("mapper", "a", "abc", nil)
	env.EmitNew("mapper", "b", "def", nil)
	env.EmitNew("mapper", "c", "ghi", nil)

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3, "three mapped events")
	assertPayload(collected, 0, "ABC")
	assertPayload(collected, 1, "DEF")
	assertPayload(collected, 2, "GHI")
}