Пример #1
0
// TestRouterBehavior tests the router behavior.
func TestRouterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("router-behavior")
	defer env.Stop()

	rf := func(emitterID, subscriberID string, event cells.Event) (bool, error) {
		ok := strings.Contains(event.Topic(), subscriberID)
		return ok, nil
	}
	env.StartCell("router", behaviors.NewRouterBehavior(rf))
	env.StartCell("test-1", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-2", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-3", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-4", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-5", behaviors.NewCollectorBehavior(10))
	env.Subscribe("router", "test-1", "test-2", "test-3", "test-4", "test-5")

	env.EmitNew("router", "test-1:test-2", "a")
	env.EmitNew("router", "test-1:test-2:test-3", "b")
	env.EmitNew("router", "test-3:test-4:test-5", "c")

	time.Sleep(100 * time.Millisecond)

	test := func(id string, length int) {
		collected, err := env.Request(id, cells.CollectedTopic, nil, cells.DefaultTimeout)
		assert.Nil(err)
		assert.Length(collected, length)
	}

	test("test-1", 2)
	test("test-2", 2)
	test("test-3", 2)
	test("test-4", 1)
	test("test-5", 1)
}
Пример #2
0
// TestRateBehavior tests the event rate behavior.
func TestRateBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("rate-behavior")
	defer env.Stop()

	matches := func(event cells.Event) bool {
		return event.Topic() == "now"
	}
	topics := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "now"}

	env.StartCell("rater", behaviors.NewRateBehavior(matches, 5))
	env.StartCell("collector", behaviors.NewCollectorBehavior(1000))
	env.Subscribe("rater", "collector")

	for i := 0; i < 1000; i++ {
		topic := topics[rand.Intn(len(topics))]
		env.EmitNew("rater", topic, nil)
		time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond)
	}

	collected, err := env.Request("collector", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	events := collected.([]behaviors.EventData)
	assert.True(len(events) <= 1000)
	for _, event := range events {
		assert.Equal(event.Topic, "event-rate!")
		_, ok := event.Payload.GetDuration(behaviors.EventRateAveragePayload)
		assert.True(ok)
		_, ok = event.Payload.GetDuration(behaviors.EventRateAveragePayload)
		assert.True(ok)
		_, ok = event.Payload.GetDuration(behaviors.EventRateLowPayload)
		assert.True(ok)
	}
}
Пример #3
0
// TestFilterBehavior tests the filter behavior.
func TestFilterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("filter-behavior")
	defer env.Stop()

	ff := func(id string, event cells.Event) bool {
		dp, ok := event.Payload().Get(cells.DefaultPayload)
		if !ok {
			return false
		}
		payload := dp.(string)
		return event.Topic() == payload
	}
	env.StartCell("filter", behaviors.NewFilterBehavior(ff))
	env.StartCell("collector", behaviors.NewCollectorBehavior(10))
	env.Subscribe("filter", "collector")

	env.EmitNew("filter", "a", "a")
	env.EmitNew("filter", "a", "b")
	env.EmitNew("filter", "b", "b")

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 2, "two collected events")
}
Пример #4
0
// TestRoundRobinBehavior tests the round robin behavior.
func TestRoundRobinBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("round-robin-behavior")
	defer env.Stop()

	env.StartCell("round-robin", behaviors.NewRoundRobinBehavior())
	env.StartCell("round-robin-1", behaviors.NewCollectorBehavior(10))
	env.StartCell("round-robin-2", behaviors.NewCollectorBehavior(10))
	env.StartCell("round-robin-3", behaviors.NewCollectorBehavior(10))
	env.StartCell("round-robin-4", behaviors.NewCollectorBehavior(10))
	env.StartCell("round-robin-5", behaviors.NewCollectorBehavior(10))
	env.Subscribe("round-robin", "round-robin-1", "round-robin-2", "round-robin-3", "round-robin-4", "round-robin-5")

	time.Sleep(100 * time.Millisecond)

	// Just 23 to let two cells receive less events.
	for i := 0; i < 25; i++ {
		err := env.EmitNew("round-robin", "round", i)
		assert.Nil(err)
	}

	time.Sleep(100 * time.Millisecond)

	test := func(id string) int {
		collected, err := env.Request(id, cells.CollectedTopic, nil, cells.DefaultTimeout)
		assert.Nil(err)
		l := len(collected.([]behaviors.EventData))
		assert.Equal(l, 5)
		return l
	}

	l1 := test("round-robin-1")
	l2 := test("round-robin-2")
	l3 := test("round-robin-3")
	l4 := test("round-robin-4")
	l5 := test("round-robin-5")

	assert.Equal(l1+l2+l3+l4+l5, 25)
}
Пример #5
0
// TestTickerBehavior tests the ticker behavior.
func TestTickerBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("ticker-behavior")
	defer env.Stop()

	env.StartCell("ticker", behaviors.NewTickerBehavior(50*time.Millisecond))
	env.StartCell("test", behaviors.NewCollectorBehavior(10))
	env.Subscribe("ticker", "test")

	time.Sleep(125 * time.Millisecond)

	collected, err := env.Request("test", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 2)
}
Пример #6
0
// TestBroadcasterBehavior tests the broadcast behavior.
func TestBroadcasterBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("broadcaster-behavior")
	defer env.Stop()

	env.StartCell("broadcast", behaviors.NewBroadcasterBehavior())
	env.StartCell("test-1", behaviors.NewCollectorBehavior(10))
	env.StartCell("test-2", behaviors.NewCollectorBehavior(10))
	env.Subscribe("broadcast", "test-1", "test-2")

	env.EmitNew("broadcast", "test", "a")
	env.EmitNew("broadcast", "test", "b")
	env.EmitNew("broadcast", "test", "c")

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("test-1", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3)

	collected, err = env.Request("test-2", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 3)
}
Пример #7
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.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")
}
Пример #8
0
// TestCollectorBehavior tests the collector behavior.
func TestCollectorBehavior(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	env := cells.NewEnvironment("collector-behavior")
	defer env.Stop()

	env.StartCell("collector", behaviors.NewCollectorBehavior(10))

	for i := 0; i < 25; i++ {
		env.EmitNew("collector", "collect", i)
	}

	time.Sleep(100 * time.Millisecond)

	collected, err := env.Request("collector", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 10)

	err = env.EmitNew("collector", cells.ResetTopic, nil)
	assert.Nil(err)

	collected, err = env.Request("collector", cells.CollectedTopic, nil, cells.DefaultTimeout)
	assert.Nil(err)
	assert.Length(collected, 0)
}