// 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) }
// 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) } }
// 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.Context()) } env.StartCell("mapper", behaviors.NewMapperBehavior(mf)) env.StartCell("collector", behaviors.NewCollectorBehavior(10)) env.Subscribe("mapper", "collector") env.EmitNew("mapper", "a", "abc") env.EmitNew("mapper", "b", "def") env.EmitNew("mapper", "c", "ghi") time.Sleep(100 * time.Millisecond) collected, err := env.Request("collector", cells.CollectedTopic, 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") }